List of usage examples for com.google.gson GsonBuilder registerTypeAdapter
@SuppressWarnings({ "unchecked", "rawtypes" }) public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter)
From source file:com.google.belay.server.GsonUtil.java
License:Open Source License
public static GsonBuilder getGsonBuilder() { GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(Capability.class, new CapabilityAdapter()); gb.setExclusionStrategies(new ExclusionStrategy() { @Override/* w w w . j av a 2 s . c o m*/ public boolean shouldSkipField(FieldAttributes attrs) { return attrs.getAnnotation(HideFromClient.class) != null; } @Override public boolean shouldSkipClass(Class<?> cls) { return false; } }); return gb; }
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 v a2 s .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.google.enterprise.adaptor.secmgr.authncontroller.ExportedState.java
License:Apache License
public static void registerTypeAdapters(GsonBuilder builder) { builder.registerTypeAdapter(Credentials.class, ProxyTypeAdapter.make(Credentials.class, Credentials.LocalProxy.class)); builder.registerTypeAdapter(ExportedState.class, ProxyTypeAdapter.make(ExportedState.class, LocalProxy.class)); builder.registerTypeAdapter(new TypeToken<ImmutableSet<Group>>() { }.getType(), TypeAdapters.immutableSet()); builder.registerTypeAdapter(new TypeToken<ImmutableList<Credentials>>() { }.getType(), TypeAdapters.immutableList()); }
From source file:com.google.gerrit.httpd.rpc.GerritJsonServlet.java
License:Apache License
private static GsonBuilder gerritDefaultGsonBuilder() { final GsonBuilder g = defaultGsonBuilder(); g.registerTypeAdapter(org.eclipse.jgit.diff.Edit.class, new org.eclipse.jgit.diff.EditDeserializer()); return g;// www.j a v a 2s . co m }
From source file:com.google.gwtjsonrpc.server.JsonServlet.java
License:Apache License
/** Create a default GsonBuilder with some extra types defined. */ public static GsonBuilder defaultGsonBuilder() { final GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(java.util.Set.class, new InstanceCreator<java.util.Set<Object>>() { public Set<Object> createInstance(final Type arg0) { return new HashSet<Object>(); }//from w ww . j a va 2s . c o m }); gb.registerTypeAdapter(java.util.Map.class, new MapDeserializer()); gb.registerTypeAdapter(java.sql.Date.class, new SqlDateDeserializer()); gb.registerTypeAdapter(java.sql.Timestamp.class, new SqlTimestampDeserializer()); return gb; }
From source file:com.google.gwtjsonrpc.server.JsonServlet.java
License:Apache License
private void parseGetRequest(final CallType call) { final HttpServletRequest req = call.httpRequest; if ("2.0".equals(req.getParameter("jsonrpc"))) { final JsonObject d = new JsonObject(); d.addProperty("jsonrpc", "2.0"); d.addProperty("method", req.getParameter("method")); d.addProperty("id", req.getParameter("id")); try {/*from ww w. j a v a 2 s. co m*/ final byte[] params = req.getParameter("params").getBytes("ISO-8859-1"); final String p = new String(Base64.decodeBase64(params), "UTF-8"); d.add("params", new JsonParser().parse(p)); } catch (UnsupportedEncodingException e) { throw new JsonParseException("Cannot parse params", e); } try { final GsonBuilder gb = createGsonBuilder(); gb.registerTypeAdapter(ActiveCall.class, // new CallDeserializer<CallType>(call, this)); gb.create().fromJson(d, ActiveCall.class); } catch (JsonParseException err) { call.method = null; call.params = null; throw err; } } else { /* JSON-RPC 1.1 */ final Gson gs = createGsonBuilder().create(); call.method = lookupMethod(req.getParameter("method")); if (call.method == null) { throw new NoSuchRemoteMethodException(); } final Type[] paramTypes = call.method.getParamTypes(); final Object[] r = new Object[paramTypes.length]; for (int i = 0; i < r.length; i++) { final String v = req.getParameter("param" + i); if (v == null) { r[i] = null; } else if (paramTypes[i] == String.class) { r[i] = v; } else if (paramTypes[i] instanceof Class<?> && ((Class<?>) paramTypes[i]).isPrimitive()) { // Primitive type, use the JSON representation of that type. // r[i] = gs.fromJson(v, paramTypes[i]); } else { // Assume it is like a java.sql.Timestamp or something and treat // the value as JSON string. // r[i] = gs.fromJson(gs.toJson(v), paramTypes[i]); } } call.params = r; call.callback = req.getParameter("callback"); } }
From source file:com.google.gwtjsonrpc.server.JsonServlet.java
License:Apache License
private void parsePostRequest(final CallType call) throws UnsupportedEncodingException, IOException { try {//from w w w . j av a 2s . c o m final GsonBuilder gb = createGsonBuilder(); gb.registerTypeAdapter(ActiveCall.class, new CallDeserializer<CallType>(call, this)); gb.create().fromJson(readBody(call), ActiveCall.class); } catch (JsonParseException err) { call.method = null; call.params = null; throw err; } }
From source file:com.google.gwtjsonrpc.server.JsonServlet.java
License:Apache License
private String formatResult(final ActiveCall call) throws UnsupportedEncodingException, IOException { final GsonBuilder gb = createGsonBuilder(); gb.registerTypeAdapter(call.getClass(), new JsonSerializer<ActiveCall>() { public JsonElement serialize(final ActiveCall src, final Type typeOfSrc, final JsonSerializationContext context) { if (call.callback != null) { if (src.externalFailure != null) { return new JsonNull(); }/*from w ww . j a va 2s . c om*/ return context.serialize(src.result); } final JsonObject r = new JsonObject(); r.add(src.versionName, src.versionValue); if (src.id != null) { r.add("id", src.id); } if (src.xsrfKeyOut != null) { r.addProperty("xsrfKey", src.xsrfKeyOut); } if (src.externalFailure != null) { final JsonObject error = new JsonObject(); if ("jsonrpc".equals(src.versionName)) { final int code = to2_0ErrorCode(src); error.addProperty("code", code); error.addProperty("message", src.externalFailure.getMessage()); } else { error.addProperty("name", "JSONRPCError"); error.addProperty("code", 999); error.addProperty("message", src.externalFailure.getMessage()); } r.add("error", error); } else { r.add("result", context.serialize(src.result)); } return r; } }); final StringWriter o = new StringWriter(); if (call.callback != null) { o.write(call.callback); o.write("("); } gb.create().toJson(call, o); if (call.callback != null) { o.write(");"); } o.close(); return o.toString(); }
From source file:com.google.mr4c.serialize.json.JsonConfigSerializer.java
License:Open Source License
private Gson buildGson() { GsonBuilder builder = new GsonBuilder(); builder.setPrettyPrinting();/* w w w.j a va 2s .com*/ builder.registerTypeAdapter(Document.class, new DocumentSerializer()); return builder.create(); }
From source file:com.google.mr4c.serialize.json.JsonDatasetBeanSerializer.java
License:Open Source License
public DatasetBean deserializeDatasetBean(Reader reader) throws IOException { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(MetadataEntryBean.class, new MetadataEntryBeanDeserializer()); Gson gson = builder.create();//from ww w .j a v a2s. co m return gson.fromJson(reader, DatasetBean.class); }