List of usage examples for com.google.gson JsonDeserializer JsonDeserializer
JsonDeserializer
From source file:com.github.api.v2.services.impl.BaseGitHubService.java
License:Apache License
/** * Gets the gson builder./*from w ww. j a v a 2 s . c om*/ * * @return the gson builder */ protected GsonBuilder getGsonBuilder() { GsonBuilder builder = new GsonBuilder(); builder.setDateFormat(ApplicationConstants.DATE_FORMAT); builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); builder.setFieldNamingStrategy(new FieldNamingStrategy() { @Override public String translateName(Field field) { if (field.getType().equals(Repository.Visibility.class)) { return "private"; } else if (field.getType().equals(Gist.Visibility.class)) { return "public"; } else { return field.getName(); } } }); builder.registerTypeAdapter(Issue.State.class, new JsonDeserializer<Issue.State>() { @Override public Issue.State deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return Issue.State.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(Repository.Visibility.class, new JsonDeserializer<Repository.Visibility>() { @Override public Repository.Visibility deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return (arg0.getAsBoolean()) ? Repository.Visibility.PRIVATE : Repository.Visibility.PUBLIC; } }); builder.registerTypeAdapter(Gist.Visibility.class, new JsonDeserializer<Gist.Visibility>() { @Override public Gist.Visibility deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return (arg0.getAsBoolean()) ? Gist.Visibility.PUBLIC : Gist.Visibility.PRIVATE; } }); builder.registerTypeAdapter(Language.class, new JsonDeserializer<Language>() { @Override public Language deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return Language.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(Tree.Type.class, new JsonDeserializer<Tree.Type>() { @Override public Tree.Type deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return Tree.Type.fromValue(arg0.getAsString()); } }); return builder; }
From source file:com.gmx.library.GsonUtil.java
License:Apache License
@SuppressWarnings("unchecked") public static <T> T jsonToBeanDateSerializer(String jsonStr, Class<T> cl, final String pattern) { Object obj = null;/* w w w.ja v a2 s . com*/ gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { SimpleDateFormat format = new SimpleDateFormat(pattern); String dateStr = json.getAsString(); try { return format.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return null; } }).setDateFormat(pattern).create(); obj = gson.fromJson(jsonStr, cl); return (T) obj; }
From source file:com.google.code.stackexchange.client.impl.StackExchangeApiJsonClient.java
License:Apache License
protected GsonBuilder getGsonBuilder() { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { @Override//from w ww .j a va 2s . c o m public Date deserialize(JsonElement source, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return new Date(source.getAsLong() * 1000); } }); builder.registerTypeAdapter(BadgeRank.class, new JsonDeserializer<BadgeRank>() { @Override public BadgeRank deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return BadgeRank.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(PostType.class, new JsonDeserializer<PostType>() { @Override public PostType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return PostType.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(PostTimelineType.class, new JsonDeserializer<PostTimelineType>() { @Override public PostTimelineType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return PostTimelineType.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(UserTimelineType.class, new JsonDeserializer<UserTimelineType>() { @Override public UserTimelineType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return UserTimelineType.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(UserType.class, new JsonDeserializer<UserType>() { @Override public UserType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return UserType.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(RevisionType.class, new JsonDeserializer<RevisionType>() { @Override public RevisionType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return RevisionType.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(TagRestriction.class, new JsonDeserializer<TagRestriction>() { @Override public TagRestriction deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return TagRestriction.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(SiteState.class, new JsonDeserializer<SiteState>() { @Override public SiteState deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return SiteState.fromValue(arg0.getAsString()); } }); builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); return builder; }
From source file:com.googleapis.ajax.services.impl.BaseGoogleSearchApiQuery.java
License:Apache License
/** * Gets the gson builder./*from w w w. ja v a 2 s.c o m*/ * * @return the gson builder */ protected GsonBuilder getGsonBuilder() { GsonBuilder builder = new GsonBuilder(); builder.setDateFormat(ApplicationConstants.RFC822DATEFORMAT); builder.registerTypeAdapter(ListingType.class, new JsonDeserializer<ListingType>() { @Override public ListingType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return ListingType.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(PatentStatus.class, new JsonDeserializer<PatentStatus>() { @Override public PatentStatus deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return PatentStatus.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(VideoType.class, new JsonDeserializer<VideoType>() { @Override public VideoType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return VideoType.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(ViewPortMode.class, new JsonDeserializer<ViewPortMode>() { @Override public ViewPortMode deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return ViewPortMode.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(GsearchResultClass.class, new JsonDeserializer<GsearchResultClass>() { @Override public GsearchResultClass deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return GsearchResultClass.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(PhoneNumberType.class, new JsonDeserializer<PhoneNumberType>() { @Override public PhoneNumberType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return PhoneNumberType.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(Language.class, new JsonDeserializer<Language>() { @Override public Language deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return Language.fromValue(arg0.getAsString()); } }); return builder; }
From source file:com.googleapis.maps.services.impl.BaseGoogleMapsApiQuery.java
License:Apache License
/** * Gets the gson builder.// w w w . ja v a2 s . c o m * * @return the gson builder */ protected GsonBuilder getGsonBuilder() { GsonBuilder builder = new GsonBuilder(); builder.setDateFormat(ApplicationConstants.RFC822DATEFORMAT); builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); builder.registerTypeAdapter(LocationType.class, new JsonDeserializer<LocationType>() { @Override public LocationType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return LocationType.fromValue(arg0.getAsString()); } }); builder.registerTypeAdapter(AddressComponentType.class, new JsonDeserializer<AddressComponentType>() { @Override public AddressComponentType deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { return AddressComponentType.fromValue(arg0.getAsString()); } }); // builder.registerTypeAdapter(TravelMode.class, new JsonDeserializer<TravelMode>() { // @Override // public TravelMode deserialize(JsonElement arg0, Type arg1, // JsonDeserializationContext arg2) throws JsonParseException { // return TravelMode.fromValue(arg0.getAsString()); // } // }); return builder; }
From source file:com.hagreve.android.lib.HaGreveApi.java
License:Apache License
/** * Obtains the list of current strikes./*from w ww. j a va 2 s .c o m*/ * @return Array of Strike objects */ public static Strike[] getStrikes() { Strike[] items = new Strike[0]; String result = doGetRequest(BASE_URL + "/strikes"); if (result != null) { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return formatter.parse(json.getAsString()); } catch (ParseException e) { throw new JsonParseException(e.getMessage()); } } }); Gson gson = builder.create(); items = gson.fromJson(result, Strike[].class); } return items; }
From source file:com.luan.thermospy.android.core.rest.GetActiveLogSessionReq.java
License:Open Source License
@Override public void onOkResponse(JSONObject response) { GsonBuilder builder = new GsonBuilder(); // Register an adapter to manage the date types as long values builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong()); }//from ww w . j ava2s. c o m }); Gson gson = builder.create(); mListener.onActiveLogSessionRecv(gson.fromJson(response.toString(), LogSession.class)); }
From source file:com.luan.thermospy.android.core.rest.GetLogSessionListReq.java
License:Open Source License
@Override public void onOkResponse(JSONArray response) { List<LogSession> logSessionsList = new ArrayList<LogSession>(); // Creates the json object which will manage the information received GsonBuilder builder = new GsonBuilder(); // Register an adapter to manage the date types as long values builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong()); }/*from w w w.j a va 2 s . c o m*/ }); Gson gson = builder.create(); try { for (int i = 0; i < response.length(); i++) { logSessionsList.add(gson.fromJson(response.getJSONObject(i).toString(), LogSession.class)); } mListener.onLogSessionsRecv(logSessionsList); } catch (JSONException ex) { mListener.onLogSessionsError(); } }
From source file:com.luan.thermospy.android.core.rest.GetLogSessionReq.java
License:Open Source License
@Override public void onOkResponse(JSONObject response) { GsonBuilder builder = new GsonBuilder(); // Register an adapter to manage the date types as long values builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong()); }//from w w w .j a v a 2s . c om }); Gson gson = builder.create(); mListener.onLogSessionRecv(gson.fromJson(response.toString(), LogSession.class)); }
From source file:com.luan.thermospy.android.core.rest.GetTemperatureEntryListReq.java
License:Open Source License
@Override public void onOkResponse(JSONArray response) { List<TemperatureEntry> logSessionsList = new ArrayList<TemperatureEntry>(); GsonBuilder builder = new GsonBuilder(); // Register an adapter to manage the date types as long values builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong()); }//from ww w. ja v a 2 s .c o m }); Gson gson = builder.create(); try { for (int i = 0; i < response.length(); i++) { logSessionsList.add(gson.fromJson(response.getJSONObject(i).toString(), TemperatureEntry.class)); } mListener.onTemperatureEntryRecv(logSessionsList); } catch (JSONException ex) { mListener.onTemperatureEntryError(); } }