Skip to content

Instantly share code, notes, and snippets.

@laaptu
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laaptu/b7b0efe5fd1ae333e169 to your computer and use it in GitHub Desktop.
Save laaptu/b7b0efe5fd1ae333e169 to your computer and use it in GitHub Desktop.
Gson Deserializer test
package com.zala.utils.json;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.zala.model.base.Location;
import com.zala.model.base.Venue;
import java.lang.reflect.Type;
/**
* Created by laaptu on 6/12/15.
*/
public class DetailDeserializer implements JsonDeserializer<Venue> {
//http://stackoverflow.com/questions/6096940/how-do-i-write-a-custom-json-deserializer-for-gson
//http://stackoverflow.com/questions/24362254/gson-is-it-possible-to-modify-the-parsed-data-while-mapping-on-the-given-class
@Override
public Venue deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Venue venue = new Venue();
JsonObject jsonObject = (JsonObject) json;
venue.id = jsonObject.get("id").getAsLong();
venue.name = jsonObject.get("name").getAsString();
venue.url = jsonObject.get("link").getAsString();
JsonObject locationObject = jsonObject.getAsJsonObject("location");
venue.location = new Location();
venue.location.address = locationObject.get("address").getAsString();
venue.location.address = "Santu Rocks !";
venue.location.latitude = locationObject.get("latitude").getAsDouble();
venue.location.longitude = locationObject.get("longitude").getAsDouble();
return venue;
}
}
public static Venue getDetailsWithSerializer(Context context){
Venue venue = new Venue();
String fileData = getStringFromAssests(context,"details.json");
if (fileData != null) {
Gson gson = new GsonBuilder().registerTypeAdapter(Venue.class,new DetailDeserializer()).create();
venue = gson.fromJson(fileData, Venue.class);
}
return venue;
}
public static String getStringFromAssests(Context context, String fileName) {
InputStream is = null;
try {
is = context.getAssets().open(fileName);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String bufferString = new String(buffer);
return bufferString;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
package com.zala.model.base;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
import com.zala.model.response.OpeningTime;
import java.util.ArrayList;
/**
* Created by laaptu on 4/16/15.
*/
public class Venue implements Parcelable {
@SerializedName("id")
public long id;
@SerializedName("name")
public String name;
@SerializedName("link")
public String url;
@SerializedName("phone")
public String phone;
@SerializedName("location")
public Location location;
@SerializedName("tags")
public String[] tags = new String[0];
@SerializedName("opening_time")
public OpeningTime openingTime;
@SerializedName("details")
public VenueDetails details;
@SerializedName("now_you_know")
public String[] trivia = new String[0];
@SerializedName("notes")
public String notes;
@SerializedName("social")
public Social social;
@SerializedName("media")
public ArrayList<Media> mediaList;
@SerializedName("user_media")
public ArrayList<Media> userMediaList;
public Venue() {
}
public Venue(String name, ArrayList<Media> mediaList) {
this.name = name;
this.mediaList = mediaList;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Venue oth = (Venue) obj;
return id == oth.id;
}
@Override
public int hashCode() {
return (int) id;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeLong(id);
parcel.writeString(name);
parcel.writeString(url);
parcel.writeString(phone);
parcel.writeParcelable(location, i);
parcel.writeInt(tags.length);
parcel.writeStringArray(tags);
parcel.writeParcelable(openingTime, i);
parcel.writeParcelable(details, i);
parcel.writeInt(trivia.length);
parcel.writeStringArray(trivia);
parcel.writeString(notes);
parcel.writeParcelable(social, i);
parcel.writeTypedList(mediaList);
parcel.writeTypedList(userMediaList);
}
protected Venue(Parcel in) {
id = in.readLong();
name = in.readString();
url = in.readString();
phone = in.readString();
location = in.readParcelable(Location.class.getClassLoader());
tags = new String[in.readInt()];
in.readStringArray(tags);
openingTime = in.readParcelable(OpeningTime.class.getClassLoader());
details = in.readParcelable(VenueDetails.class.getClassLoader());
trivia = new String[in.readInt()];
in.readStringArray(trivia);
notes = in.readString();
social = in.readParcelable(Social.class.getClassLoader());
mediaList = new ArrayList<Media>();
in.readTypedList(mediaList, Media.CREATOR);
userMediaList = new ArrayList<Media>();
in.readTypedList(userMediaList, Media.CREATOR);
}
public static Creator<Venue> CREATOR = new Creator<Venue>() {
@Override
public Venue createFromParcel(Parcel parcel) {
return new Venue(parcel);
}
@Override
public Venue[] newArray(int i) {
return new Venue[i];
}
};
}
{
"success": true,
"next_page": 2,
"places": [
{
"id": 5921,
"name": "Red Tomato Pizza House",
"link": "http://www.redtomatopizzahouse.com/",
"location": {
"address": "2017 University Avenue, Berkeley, CA 94704, United States",
"latitude": "37.872152",
"longitude": "-122.270128"
},
"tags": [
"restaurant",
"food",
"establishment"
],
"opening_time": {
"periods": [
{
"close": {
"day": 0,
"time": "2130"
},
"open": {
"day": 0,
"time": "1100"
}
},
{
"close": {
"day": 1,
"time": "2130"
},
"open": {
"day": 1,
"time": "1100"
}
},
{
"close": {
"day": 2,
"time": "2130"
},
"open": {
"day": 2,
"time": "1100"
}
},
{
"close": {
"day": 3,
"time": "2130"
},
"open": {
"day": 3,
"time": "1100"
}
},
{
"close": {
"day": 4,
"time": "2200"
},
"open": {
"day": 4,
"time": "1100"
}
},
{
"close": {
"day": 5,
"time": "2200"
},
"open": {
"day": 5,
"time": "1100"
}
},
{
"close": {
"day": 6,
"time": "2200"
},
"open": {
"day": 6,
"time": "1100"
}
}
],
"weekday_text": [
"Monday: 11:00 am – 9:30 pm",
"Tuesday: 11:00 am – 9:30 pm",
"Wednesday: 11:00 am – 9:30 pm",
"Thursday: 11:00 am – 10:00 pm",
"Friday: 11:00 am – 10:00 pm",
"Saturday: 11:00 am – 10:00 pm",
"Sunday: 11:00 am – 9:30 pm"
]
},
"media": [
{
"type": "image",
"link": "https://zala-staging.s3.amazonaws.com/uploads/note/image/7033/photo.jpg",
"thumbnail": "https://zala-staging.s3.amazonaws.com/uploads/note/image/7033/thumb_photo.jpg"
}
]
},
{
"id": 5922,
"name": "Pizzetta 211",
"link": "http://www.pizzetta211.com/",
"location": {
"address": "211 23rd Avenue, San Francisco, CA 94121, United States",
"latitude": "37.783704",
"longitude": "-122.483052"
},
"tags": [
"restaurant",
"food",
"establishment",
"meal_takeaway"
],
"opening_time": {
"periods": [
{
"close": {
"day": 0,
"time": "2100"
},
"open": {
"day": 0,
"time": "1200"
}
},
{
"close": {
"day": 1,
"time": "1430"
},
"open": {
"day": 1,
"time": "1200"
}
},
{
"close": {
"day": 1,
"time": "2100"
},
"open": {
"day": 1,
"time": "1700"
}
},
{
"close": {
"day": 3,
"time": "1430"
},
"open": {
"day": 3,
"time": "1200"
}
},
{
"close": {
"day": 3,
"time": "2100"
},
"open": {
"day": 3,
"time": "1700"
}
},
{
"close": {
"day": 4,
"time": "1430"
},
"open": {
"day": 4,
"time": "1200"
}
},
{
"close": {
"day": 4,
"time": "2100"
},
"open": {
"day": 4,
"time": "1700"
}
},
{
"close": {
"day": 5,
"time": "1430"
},
"open": {
"day": 5,
"time": "1200"
}
},
{
"close": {
"day": 5,
"time": "2100"
},
"open": {
"day": 5,
"time": "1700"
}
},
{
"close": {
"day": 6,
"time": "2100"
},
"open": {
"day": 6,
"time": "1200"
}
}
],
"weekday_text": [
"Monday: 12:00 – 2:30 pm, 5:00 – 9:00 pm",
"Tuesday: Closed",
"Wednesday: 12:00 – 2:30 pm, 5:00 – 9:00 pm",
"Thursday: 12:00 – 2:30 pm, 5:00 – 9:00 pm",
"Friday: 12:00 – 2:30 pm, 5:00 – 9:00 pm",
"Saturday: 12:00 – 9:00 pm",
"Sunday: 12:00 – 9:00 pm"
]
},
"media": [
{
"type": "image",
"link": "https://zala-staging.s3.amazonaws.com/uploads/note/image/7034/photo.jpg",
"thumbnail": "https://zala-staging.s3.amazonaws.com/uploads/note/image/7034/thumb_photo.jpg"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment