Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"};
(Serialization)
gson.toJson(ints); ==> prints [1,2,3,4,5]
gson.toJson(strings); ==> prints ["abc", "def", "ghi"]
(Deserialization)
int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class);
Collections Examples
Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);
(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]
(Deserialization)
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
Serializing and Deserializing Generic Types
Type fooType = new TypeToken<Foo<Bar>>() {}.getType();
gson.toJson(foo, fooType);
gson.fromJson(json, fooType);
Serializing and Deserializing Collection with Objects of Arbitrary Types
Sometimes you are dealing with JSON array that contains mixed types. For example:
['hello',5,{name:'GREETINGS',source:'guest'}]
public class RawCollectionsExample {
static class Event {
private String name;
private String source;
private Event(String name, String source) {
this.name = name;
this.source = source;
}
@Override
public String toString() {
return String.format("(name=%s, source=%s)", name, source);
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
Gson gson = new Gson();
Collection collection = new ArrayList();
collection.add("hello");
collection.add(5);
collection.add(new Event("GREETINGS", "guest"));
String json = gson.toJson(collection);
System.out.println("Using Gson.toJson() on a raw collection: " + json);
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(json).getAsJsonArray();
String message = gson.fromJson(array.get(0), String.class);
int number = gson.fromJson(array.get(1), int.class);
Event event = gson.fromJson(array.get(2), Event.class);
System.out.printf("Using Gson.fromJson() to get: %s, %d, %s", message, number, event);
}
}
Compact Vs. Pretty Printing for JSON Output Format
默认情况下Gson的json输出是没有空格与忽略null值得,所以不是很友好。我们可以采用pretty输出,同时输出null
Gson gson = new GsonBuilder().setPrettyPrinting().serialzeNulls().create();
NOTE: when serializing nulls with Gson, it will add a JsonNull element to the JsonElement structure. Therefore, this object can be used in custom serialization/deserialization.
Versioning Support
本文不做介绍
Excluding Fields From Serialization and Deserialization
默认情况下transient 和static field是被忽略的。但是如果你想包含某些transient域可以采取如下形式:
import java.lang.reflect.Modifier;
Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.STATIC)
.create();
NOTE: you can use any number of the Modifier constants to "excludeFieldsWithModifiers" method. For example:
Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE)
.create();
不过有更好的形式可以采取
@Expose注解:然后调用 new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()创建Gson凡
是被@Expose注解的域都会被包括,没有被注解的都被忽略。
JSON Field Naming Support @SerializedName
private class SomeObject {
@SerializedName("custom_naming") private final String someField;
private final String someOtherField;
public SomeObject(String a, String b) {
this.someField = a;
this.someOtherField = b;
}
}
SomeObject someObject = new SomeObject("first", "second");
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
String jsonRepresentation = gson.toJson(someObject);
System.out.println(jsonRepresentation);
======== OUTPUT ========
{"custom_naming":"first","SomeOtherField":"second"}
Gson2.3新的功能
New Methods in JsonArray
@TypeAdapter Annotation
JsonPath Support
参考:
https://sites.google.com/site/gson/gson-user-guide
http://www.studytrails.com/java/json/java-google-json-new-2.3.jsp