public Gson create() { //TODO: configuration database _gsonBuilder.setVersion(1.7); return _gsonBuilder.create(); } }
public JsonMessageSerializer() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setVersion(1.5); _gson = gsonBuilder.create(); }
try { GsonBuilder gb = new GsonBuilder(); gb.setVersion(1.3); Gson gson = gb.create();
static Gson setDefaultGsonConfig(GsonBuilder builder) { builder.setVersion(1.5); InterfaceTypeAdaptor<DataStoreTO> dsAdaptor = new InterfaceTypeAdaptor<DataStoreTO>(); builder.registerTypeAdapter(DataStoreTO.class, dsAdaptor); InterfaceTypeAdaptor<DataTO> dtAdaptor = new InterfaceTypeAdaptor<DataTO>(); builder.registerTypeAdapter(DataTO.class, dtAdaptor); ArrayTypeAdaptor<Command> cmdAdaptor = new ArrayTypeAdaptor<Command>(); builder.registerTypeAdapter(Command[].class, cmdAdaptor); ArrayTypeAdaptor<Answer> ansAdaptor = new ArrayTypeAdaptor<Answer>(); builder.registerTypeAdapter(Answer[].class, ansAdaptor); builder.registerTypeAdapter(new TypeToken<List<PortConfig>>() { }.getType(), new PortConfigListTypeAdaptor()); builder.registerTypeAdapter(new TypeToken<Pair<Long, Long>>() { }.getType(), new NwGroupsCommandTypeAdaptor()); Gson gson = builder.create(); dsAdaptor.initGson(gson); dtAdaptor.initGson(gson); cmdAdaptor.initGson(gson); ansAdaptor.initGson(gson); return gson; }
try { GsonBuilder gb = new GsonBuilder(); gb.setVersion(1.3); Gson gson = gb.create(); status = gson.fromJson(cmd.getLoadInfo(), ConsoleProxyStatus.class);
try { GsonBuilder gb = new GsonBuilder(); gb.setVersion(1.3); Gson gson = gb.create(); status = gson.fromJson(answer.getDetails(), ConsoleProxyStatus.class);
public JsonMessageSerializer() { final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setVersion(1.5); _gson = gsonBuilder.create(); }
private Gson buildGson() { final GsonBuilder gson = new GsonBuilder(); gson.setVersion(version.asFloat()); return gson.create(); }
@Override public void version(double versionNumber) { getGsonBuilder().setVersion(versionNumber); }
public static GsonBuilder createGerritGsonBuilder( boolean nonExecutable, PlatformAbstractionLayer abstractionLayer) { GsonBuilder builder = new GsonBuilder() .setVersion(GerritApi.API_VERSION) .registerTypeAdapter(Date.class, new GerritUtcDateAdapter()) .registerTypeAdapter(ServerVersion.class, new GerritServerVersionAdapter()) .registerTypeAdapter(ApprovalInfo.class, new GerritApprovalInfoAdapter()) .registerTypeAdapter(Base64Data.class, new GerritBas64Adapter(abstractionLayer)) .setLenient(); if (nonExecutable) { builder.generateNonExecutableJson(); } return builder; } }
private static Object fromJson(String json, Class<?> clazz, int complexType) { GsonBuilder builder = new GsonBuilder() .setVersion(GlobalSetting.getDefault().getGsonVersion());
/** * Generate JSON format as result of the scan. * * @since 1.2 */ private static void generateJson(CellScanner cellScanner, Function<Bytes, String> encoder, PrintStream out) throws JsonIOException { Gson gson = new GsonBuilder().serializeNulls().setDateFormat(DateFormat.LONG) .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setVersion(1.0) .create(); Map<String, String> json = new LinkedHashMap<>(); for (RowColumnValue rcv : cellScanner) { json.put(FLUO_ROW, encoder.apply(rcv.getRow())); json.put(FLUO_COLUMN_FAMILY, encoder.apply(rcv.getColumn().getFamily())); json.put(FLUO_COLUMN_QUALIFIER, encoder.apply(rcv.getColumn().getQualifier())); json.put(FLUO_COLUMN_VISIBILITY, encoder.apply(rcv.getColumn().getVisibility())); json.put(FLUO_VALUE, encoder.apply(rcv.getValue())); gson.toJson(json, out); out.append("\n"); if (out.checkError()) { break; } } out.flush(); }
builder.serializeNulls(); if (version != null) builder.setVersion(version.doubleValue()); if (isEmpty(datePattern)) datePattern = DEFAULT_DATE_PATTERN;
/** * convert the target object to json. currently support simple object, array , list. SparseArray * @param t the object. must have annotation {@linkplain JsonAdapter} * @return the json string. * @see SparseArray */ public static String toJson(Object t) { GsonBuilder builder = new GsonBuilder() .setVersion(GlobalSetting.getDefault().getGsonVersion()); if(t instanceof SparseArray){ SparseArray sa = (SparseArray) t; if(sa.size() == 0){ return "{}"; } Class<?> clazz = sa.valueAt(0).getClass(); /* builder.registerTypeAdapter( TypeToken.getParameterized(SparseArray.class, clazz).getType(), new SparseArrayTypeAdapter<>(TypeHandler.getTypeAdapter(clazz)));*/ builder.registerTypeAdapter(SparseArray.class, new SparseArrayTypeAdapter<>(TypeHandler.getTypeAdapter(clazz))); } return builder.create().toJson(t); }
public IString toJSON(IValue value, IBool compact) { // System.err.println("hallo"); IValueAdapter adap = new IValueAdapter(compact.getValue()); // System.err.println(adap); Gson gson = new GsonBuilder() .registerTypeAdapter(IValue.class, adap) .enableComplexMapKeySerialization() .setDateFormat(DateFormat.LONG) .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .setVersion(1.0) .disableHtmlEscaping() // Bert Lisser .create(); try { String json = gson.toJson(value, new TypeToken<IValue>() {}.getType()); return values.string(json); } catch (Exception e) { throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); } }
public IValue fromJSON(IValue type, IString src, RascalExecutionContext rex) { TypeStore store = rex.getTypeStore(); // new TypeStore(); IConstructor type_cons = ((IConstructor) type); Type start = tr.valueToType(type_cons, store); //TypeStore store = ctx.getCurrentEnvt().getStore(); //Type start = new TypeReifier(ctx.getValueFactory()).valueToType((IConstructor) type, store); //System.err.println("fromJSON0:"+start); Gson gson = new GsonBuilder() .enableComplexMapKeySerialization() .setDateFormat(DateFormat.LONG) .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .setVersion(1.0) .create(); //System.err.println("fromJSON1:"+src.getValue()); Object obj = gson.fromJson(src.getValue(), Object.class); //System.err.println("fromJSON2:"+start); try { return JSONReadingTypeVisitor.read(obj, values, store, start); } catch (IOException e) { throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); } }
public IValue fromJSON(IValue type, IString src, IEvaluatorContext ctx) { TypeStore store = ctx.getCurrentEnvt().getStore(); Type start = new TypeReifier(ctx.getValueFactory()).valueToType((IConstructor) type, store); Gson gson = new GsonBuilder() .enableComplexMapKeySerialization() .setDateFormat(DateFormat.LONG) .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .setVersion(1.0) .create(); Object obj = gson.fromJson(src.getValue(), Object.class); try { return JSONReadingTypeVisitor.read(obj, values, store, start); } catch (IOException e) { throw RuntimeExceptionFactory.io(values.string(e.getMessage()), null, null); } }
private static void test2() { //gson 还可以根据版本去选择是否序列化/反序列化 Gson gson = new GsonBuilder().setVersion(2.0).create(); Car3 car = new Car3(); car.setMark("AUDI"); car.setModel(2014); //2,1 car.setType("DIESEL"); car.setMaker("AUDI GERMANY"); car.setCost(55000); car.getColors().add("GREY"); car.getColors().add("BLACK"); car.getColors().add("WHITE"); /* Serialize */ String jsonString = gson.toJson(car); System.out.println("Serialized jsonString : " + jsonString); /* Deserialize */ String inputJson = "{\"mark\":\"AUDI\",\"model\":2014,\"type\":\"DIESEL\",\"maker\":\"AUDI Germany\",\"cost\":55000,\"colors\":[\"GRAY\",\"BLACK\",\"WHITE\"]}"; car = gson.fromJson(inputJson, Car3.class); System.out.println("Deserialized Car : " + car); }