com.google.gson.LongSerializationPolicy Java Examples

The following examples show how to use com.google.gson.LongSerializationPolicy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChannelModelProvider.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
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 ();
}
 
Example #2
Source File: GsonHttpMessageConverter.java    From reactor-guice with Apache License 2.0 5 votes vote down vote up
public GsonHttpMessageConverter() {
    this.gson = new GsonBuilder()
        .serializeNulls()
        .setDateFormat("yyyy-MM-dd HH:mm:ss")
        .setLongSerializationPolicy(LongSerializationPolicy.STRING)
        .create();
}
 
Example #3
Source File: Module.java    From reactor-guice with Apache License 2.0 5 votes vote down vote up
@Singleton
@Provides
public Gson gson () {
    return new GsonBuilder()
        .serializeNulls()
        .setDateFormat("yyyy-MM-dd HH:mm:ss")
        .setLongSerializationPolicy(LongSerializationPolicy.STRING)
        .create();
}
 
Example #4
Source File: MyGsonHttpMessageConverter.java    From reactor-guice with Apache License 2.0 5 votes vote down vote up
public MyGsonHttpMessageConverter() {
    this.gson = new GsonBuilder()
        .serializeNulls()
        .setDateFormat("yyyy-MM-dd HH:mm:ss")
        .setLongSerializationPolicy(LongSerializationPolicy.STRING)
        // .excludeFieldsWithoutExposeAnnotation()
        .create();
}
 
Example #5
Source File: DefaultMessageStore.java    From qmq with Apache License 2.0 5 votes vote down vote up
DefaultMessageStore(DataSource datasource, RouterSelector routerSelector, SqlStatementProvider sqlStatementProvider) {
    this.sqlStatementProvider = sqlStatementProvider;
    this.platform = new JdbcTemplate(datasource);
    this.insertStatementFactory = createFactory();
    this.gson = new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create();
    this.routerSelector = routerSelector;
}
 
Example #6
Source File: GSONUtil.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
GSONUtil() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
    // gsonBuilder.setPrettyPrinting();

    gson = gsonBuilder.create();
}
 
Example #7
Source File: JsonSerializer.java    From exonum-java-binding with Apache License 2.0 5 votes vote down vote up
/**
 * Returns preconfigured {@link Gson} builder instance. Can be useful in cases when
 * some customization is required. For example, type adapters should be extended or replaced.
 */
public static GsonBuilder builder() {
  return new GsonBuilder()
      .registerTypeHierarchyAdapter(TransactionMessage.class,
          new TransactionMessageJsonSerializer())
      .registerTypeHierarchyAdapter(HashCode.class, new HashCodeJsonSerializer())
      .registerTypeAdapter(PublicKey.class, new PublicKeyJsonSerializer())
      .registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeJsonSerializer())
      .registerTypeAdapterFactory(CommonTypeAdapterFactory.create())
      .setLongSerializationPolicy(LongSerializationPolicy.STRING);
}
 
Example #8
Source File: HttpGson.java    From sumk with Apache License 2.0 5 votes vote down vote up
private static GsonBuilder gsonBuilder() {
	GsonBuilder gb = GsonHelper.builder("sumk.http");

	if (AppInfo.getBoolean("sumk.http.json.long2String", true)) {
		gb.setLongSerializationPolicy(LongSerializationPolicy.STRING);
	}
	return gb;
}
 
Example #9
Source File: PrimitiveTest.java    From gson with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #10
Source File: PrimitiveTest.java    From gson with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #11
Source File: JavaUtilConcurrentAtomicTest.java    From gson with Apache License 2.0 5 votes vote down vote up
public void testAtomicLongWithStringSerializationPolicy() throws Exception {
  Gson gson = new GsonBuilder()
      .setLongSerializationPolicy(LongSerializationPolicy.STRING)
      .create();
  AtomicLongHolder target = gson.fromJson("{'value':'10'}", AtomicLongHolder.class);
  assertEquals(10, target.value.get());
  String json = gson.toJson(target);
  assertEquals("{\"value\":\"10\"}", json);
}
 
Example #12
Source File: JavaUtilConcurrentAtomicTest.java    From gson with Apache License 2.0 5 votes vote down vote up
public void testAtomicLongArrayWithStringSerializationPolicy() throws Exception {
  Gson gson = new GsonBuilder()
      .setLongSerializationPolicy(LongSerializationPolicy.STRING)
      .create();
  AtomicLongArray target = gson.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 = gson.toJson(target);
  assertEquals("[\"10\",\"13\",\"14\"]", json);
}
 
Example #13
Source File: GsonHelper.java    From sumk with Apache License 2.0 4 votes vote down vote up
public static GsonBuilder builder(String module) {
	if (module == null || module.isEmpty()) {
		module = "sumk";
	}

	DateTimeTypeAdapter da = new DateTimeTypeAdapter();
	String format = AppInfo.get(module + ".gson.date.format");
	if (StringUtil.isNotEmpty(format)) {
		da.setDateFormat(format);
	}

	GsonBuilder gb = new GsonBuilder().registerTypeAdapter(Date.class, da);

	if (AppInfo.getBoolean(module + ".gson.disableHtmlEscaping", false)) {
		gb.disableHtmlEscaping();
	}
	if (AppInfo.getBoolean(module + ".gson.shownull", false)) {
		gb.serializeNulls();
	}
	if (AppInfo.getBoolean(module + ".gson.disableInnerClassSerialization", false)) {
		gb.disableInnerClassSerialization();
	}
	if (AppInfo.getBoolean(module + ".gson.generateNonExecutableJson", false)) {
		gb.generateNonExecutableJson();
	}
	if (AppInfo.getBoolean(module + ".gson.serializeSpecialFloatingPointValues", false)) {
		gb.serializeSpecialFloatingPointValues();
	}

	if (AppInfo.getBoolean(module + ".gson.longSerialize2String", false)) {
		gb.setLongSerializationPolicy(LongSerializationPolicy.STRING);
	}

	if (AppInfo.getBoolean(module + ".gson.prettyPrinting", false)) {
		gb.setPrettyPrinting();
	}
	if (AppInfo.getBoolean(module + ".gson.date.adaper", true)) {
		DateAdapters.registerAll(gb);
	}
	return gb;
}