下面列出了怎么用com.google.gson.LongSerializationPolicy的API类实例代码及写法,或者点击链接到github查看源代码。
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 ();
}
public GsonHttpMessageConverter() {
this.gson = new GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
.create();
}
@Singleton
@Provides
public Gson gson () {
return new GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
.create();
}
public MyGsonHttpMessageConverter() {
this.gson = new GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.setLongSerializationPolicy(LongSerializationPolicy.STRING)
// .excludeFieldsWithoutExposeAnnotation()
.create();
}
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;
}
GSONUtil() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
// gsonBuilder.setPrettyPrinting();
gson = gsonBuilder.create();
}
/**
* 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);
}
private static GsonBuilder gsonBuilder() {
GsonBuilder gb = GsonHelper.builder("sumk.http");
if (AppInfo.getBoolean("sumk.http.json.long2String", true)) {
gb.setLongSerializationPolicy(LongSerializationPolicy.STRING);
}
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 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);
}
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);
}
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;
}