public Gson create() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Id.class, new IdSerializer()); gsonBuilder.registerTypeAdapter(Type.class, new TypeSerializer()); gsonBuilder.registerTypeAdapter(Visitor.class, new VisitorSerializer()); gsonBuilder.registerTypeAdapter(Session.class, new SessionSerializer()); gsonBuilder.registerTypeAdapter(Map.class, new MapSerializer()); gsonBuilder.registerTypeAdapter(Payload.class, new MapSerializer()); gsonBuilder.serializeNulls(); gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING); gsonBuilder.enableComplexMapKeySerialization(); return gsonBuilder.create(); }
static Gson createGson () { final GsonBuilder builder = new GsonBuilder (); builder.setPrettyPrinting (); builder.setLongSerializationPolicy ( LongSerializationPolicy.STRING ); builder.setDateFormat ( DATE_FORMAT ); builder.registerTypeAdapter ( MetaKey.class, new JsonDeserializer<MetaKey> () { @Override public MetaKey deserialize ( final JsonElement json, final Type type, final JsonDeserializationContext ctx ) throws JsonParseException { return MetaKey.fromString ( json.getAsString () ); } } ); return builder.create (); }
ProtoSingleton() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING); // gsonBuilder.setPrettyPrinting(); gson = gsonBuilder.create(); }
public void testLongAsStringDeserialization() throws Exception { long value = oson.fromJson("\"15\"", long.class); assertEquals(15, value); gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create(); value = oson.fromJson("\"25\"", long.class); assertEquals(25, value); }
public void testAtomicLongWithStringSerializationPolicy() throws Exception { Gson gson = new GsonBuilder() .setLongSerializationPolicy(LongSerializationPolicy.STRING) .create(); AtomicLongHolder target = oson.fromJson("{'value':'10'}", AtomicLongHolder.class); assertEquals(10, target.value.get()); String json = oson.toJson(target); assertEquals("{\"value\":10}", json); }
public void testAtomicLongArrayWithStringSerializationPolicy() throws Exception { Gson gson = new GsonBuilder() .setLongSerializationPolicy(LongSerializationPolicy.STRING) .create(); AtomicLongArray target = oson.fromJson("['10', '13', '14']", AtomicLongArray.class); assertEquals(3, target.length()); assertEquals(10, target.get(0)); assertEquals(13, target.get(1)); assertEquals(14, target.get(2)); String json = oson.toJson(target); assertEquals("[10,13,14]", json); }
@Before public void setUp() throws Exception { gson = new GsonBuilder() .registerTypeAdapterFactory(StaticGsonTypeAdapterFactory.newInstance()) .setLongSerializationPolicy(LongSerializationPolicy.STRING) .create(); }
private static GsonBuilder pcGsonBuilder() { String module = "http.pc"; DateTimeTypeAdapter da = new DateTimeTypeAdapter(); String format = AppInfo.get(module + ".json.date.format", SumkDate.DATE_TIME_MILS); if (StringUtil.isNotEmpty(format)) { da.setDateFormat(format); } GsonBuilder gb = new GsonBuilder().registerTypeAdapter(Date.class, da); if (AppInfo.getBoolean(module + ".json.disableHtmlEscaping", true)) { gb.disableHtmlEscaping(); } if (AppInfo.getBoolean(module + ".json.shownull", false)) { gb.serializeNulls(); } if (AppInfo.getBoolean(module + ".json.disableInnerClassSerialization", false)) { gb.disableInnerClassSerialization(); } if (AppInfo.getBoolean(module + ".json.generateNonExecutableJson", false)) { gb.generateNonExecutableJson(); } if (AppInfo.getBoolean(module + ".json.serializeSpecialFloatingPointValues", false)) { gb.serializeSpecialFloatingPointValues(); } if (AppInfo.getBoolean(module + ".json.longSerialize2String", true)) { gb.setLongSerializationPolicy(LongSerializationPolicy.STRING); } if (AppInfo.getBoolean(module + ".json.prettyPrinting", false)) { gb.setPrettyPrinting(); } return gb; }
public static GsonBuilder gsonBuilder(String module) { if (module == null || module.isEmpty()) { module = "sumk"; } DateTimeTypeAdapter da = new DateTimeTypeAdapter(); String format = AppInfo.get(module + ".json.date.format"); if (StringUtil.isNotEmpty(format)) { da.setDateFormat(format); } GsonBuilder gb = new GsonBuilder().registerTypeAdapter(Date.class, da); if (AppInfo.getBoolean(module + ".json.disableHtmlEscaping", true)) { gb.disableHtmlEscaping(); } if (AppInfo.getBoolean(module + ".json.shownull", false)) { gb.serializeNulls(); } if (AppInfo.getBoolean(module + ".json.disableInnerClassSerialization", false)) { gb.disableInnerClassSerialization(); } if (AppInfo.getBoolean(module + ".json.generateNonExecutableJson", false)) { gb.generateNonExecutableJson(); } if (AppInfo.getBoolean(module + ".json.serializeSpecialFloatingPointValues", false)) { gb.serializeSpecialFloatingPointValues(); } if (AppInfo.getBoolean(module + ".json.longSerialize2String", false)) { gb.setLongSerializationPolicy(LongSerializationPolicy.STRING); } if (AppInfo.getBoolean(module + ".json.prettyPrinting", false)) { gb.setPrettyPrinting(); } DateAdapters.register(gb, module); return gb; }
public void testLongAsStringSerialization() throws Exception { gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create(); String result = gson.toJson(15L); assertEquals("\"15\"", result); // Test with an integer and ensure its still a number result = gson.toJson(2); assertEquals("2", result); }
public void testLongAsStringDeserialization() throws Exception { long value = gson.fromJson("\"15\"", long.class); assertEquals(15, value); gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create(); value = gson.fromJson("\"25\"", long.class); assertEquals(25, value); }
public LongSerializationPolicy getLongSerializationPolicy() { return longSerializationPolicy; }
public void setLongSerializationPolicy(LongSerializationPolicy longSerializationPolicy) { this.longSerializationPolicy = longSerializationPolicy; }
/** * Configures Gson to apply a specific serialization policy for {@code Long} and {@code long} * objects. * * @param serializationPolicy the particular policy to use for serializing longs. * @return a reference to this {@code HyperDataGsonBuilder} object to fulfill the "Builder" pattern * @since 1.3 */ public HyperDataGsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) { builder.setLongSerializationPolicy(serializationPolicy); return this; }