List of usage examples for com.google.gson GsonBuilder create
public Gson create()
From source file:com.github.vbauer.yta.service.transport.impl.DataConverterImpl.java
License:Open Source License
private static Gson createConverter() { final GsonBuilder builder = new GsonBuilder(); ServiceLoader.load(TypeAdapterFactory.class).forEach(builder::registerTypeAdapterFactory); return builder.create(); }
From source file:com.github.zhizheng.json.JsonSchemaGeneratorImpl.java
License:Apache License
/** * Json to Json Schema/*from w w w . j av a 2 s . co m*/ * * @param jsonElement * @return */ private String fromJsonElement(JsonElement jsonElement) { GsonBuilder gsonBuilder = new GsonBuilder(); if (jsonSchemaConfig.isPrettyPrint()) { gsonBuilder.setPrettyPrinting(); } gsonBuilder.disableHtmlEscaping(); Gson gson = gsonBuilder.create(); JsonObject jsonSchemaElement = makeSchemaElement(jsonElement, null, true, null); String jsonSchemaString = gson.toJson(jsonSchemaElement); return jsonSchemaString; }
From source file:com.github.zk1931.zabkv.JsonPutCommand.java
License:Apache License
@Override public void execute(Database db) { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); HashMap<String, byte[]> map = gson.fromJson(json, HashMap.class); db.put(map);/*from www . ja v a 2s. c om*/ }
From source file:com.goforer.base.model.BaseModel.java
License:Apache License
public static Gson gson() { GsonBuilder builder = BaseModel.gsonBuilder(); return builder.create(); }
From source file:com.google.api.ads.adwords.keywordoptimizer.api.JsonUtil.java
License:Open Source License
/** * Initializes Gson to convert objects to and from JSON. This method customizes a "plain" Gson by * adding appropriate exclusions strategies / adapters as needed in this project for a "pretty" * output. //from ww w. ja va 2 s.com */ private static Gson initGson(boolean prettyPrint) { GsonBuilder builder = new GsonBuilder(); // Exclude superclasses. ExclusionStrategy superclassExclusionStrategy = new SuperclassExclusionStrategy(); builder.addDeserializationExclusionStrategy(superclassExclusionStrategy); builder.addSerializationExclusionStrategy(superclassExclusionStrategy); // Exclude underscore fields in client lib objects. ExclusionStrategy underscoreExclusionStrategy = new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes field) { if (field.getName().startsWith("_")) { return true; } return false; } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }; builder.addDeserializationExclusionStrategy(underscoreExclusionStrategy); builder.addSerializationExclusionStrategy(underscoreExclusionStrategy); // Render KeywordCollection as an array of KeywordInfos. builder.registerTypeAdapter(KeywordCollection.class, new JsonSerializer<KeywordCollection>() { @Override public JsonElement serialize(KeywordCollection src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) { JsonArray out = new JsonArray(); for (KeywordInfo info : src.getListSortedByScore()) { out.add(context.serialize(info)); } return out; } }); // Render Money as a primitive. builder.registerTypeAdapter(Money.class, new JsonSerializer<Money>() { @Override public JsonElement serialize(Money src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) { JsonElement out = new JsonPrimitive(src.getMicroAmount() / 1000000); return out; } }); // Render Keyword in a simple way. builder.registerTypeAdapter(Keyword.class, new JsonSerializer<Keyword>() { @Override public JsonElement serialize(Keyword src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) { JsonObject out = new JsonObject(); out.addProperty("text", src.getText()); out.addProperty("matchtype", src.getMatchType().toString()); return out; } }); // Render Throwable in a simple way (for all subclasses). builder.registerTypeHierarchyAdapter(Throwable.class, new JsonSerializer<Throwable>() { @Override public JsonElement serialize(Throwable src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) { JsonObject out = new JsonObject(); out.addProperty("message", src.getMessage()); out.addProperty("type", src.getClass().getName()); JsonArray stack = new JsonArray(); for (StackTraceElement stackTraceElement : src.getStackTrace()) { JsonObject stackElem = new JsonObject(); stackElem.addProperty("file", stackTraceElement.getFileName()); stackElem.addProperty("line", stackTraceElement.getLineNumber()); stackElem.addProperty("method", stackTraceElement.getMethodName()); stackElem.addProperty("class", stackTraceElement.getClassName()); stack.add(stackElem); } out.add("stack", stack); if (src.getCause() != null) { out.add("cause", context.serialize(src.getCause())); } return out; } }); if (prettyPrint) { builder.setPrettyPrinting(); } return builder.create(); }
From source file:com.google.caliper.json.GsonModule.java
License:Apache License
@Provides Gson provideGson(Set<TypeAdapterFactory> typeAdapterFactories, ExclusionStrategy exclusionStrategy) { GsonBuilder gsonBuilder = new GsonBuilder().setExclusionStrategies(exclusionStrategy); for (TypeAdapterFactory typeAdapterFactory : typeAdapterFactories) { gsonBuilder.registerTypeAdapterFactory(typeAdapterFactory); }/* ww w .j a v a2 s .c om*/ return gsonBuilder.create(); }
From source file:com.google.drive.samples.dredit.model.ClientFile.java
License:Apache License
/** * Construct a new ClientFile from its JSON representation. * * @param in Reader of JSON string to parse. *//*from w ww . j ava 2s . co m*/ public ClientFile(Reader in) { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); ClientFile other = gson.fromJson(in, ClientFile.class); this.resource_id = other.resource_id; this.title = other.title; this.description = other.description; this.mimeType = other.mimeType; this.content = other.content; }
From source file:com.google.enterprise.adaptor.secmgr.config.ConfigSingleton.java
License:Apache License
public static synchronized void setGsonRegistrations(GsonRegistrations registrations) { GsonBuilder builder = new GsonBuilder(); builder.setPrettyPrinting();//from ww w .j a v a2s. co m registrations.register(builder); gson = builder.create(); }
From source file:com.google.gerrit.httpd.restapi.RestApiServlet.java
License:Apache License
private static Gson newGson(Multimap<String, String> config, @Nullable HttpServletRequest req) { GsonBuilder gb = OutputFormat.JSON_COMPACT.newGsonBuilder(); enablePrettyPrint(gb, config, req);/*from w w w . jav a 2 s.c o m*/ enablePartialGetFields(gb, config); return gb.create(); }
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 {/* w w w .j av a 2 s . c o 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"); } }