gson使用教程-翻译7
来源:互联网 发布:Linux修改jvm内存大小 编辑:程序博客网 时间:2024/05/16 19:52
原文地址:http://www.studytrails.com/java/json/java-google-json-custom-serializer-deserializer.jsp
就如我们之前看到的教程,Gson提供自定义序列化器和反序列化器.如果我们不需要Gson默认的转换方式,我们可以通过自定义序列化器和反序列化器来转换java对象和json.下面的第一个demo是展示自定义序列化器和第二个demo是展示自定义反序列化器.
Custom Serializer
自定义序列化器通过实现JsonSerializer这个接口和实现public JsonElement serialize(T src,Type typeOfSrc,JsonSerializationContext context)方法,src这个是转换源对象,而Type是转换源对象的类型.下面的demo展示如何创建和使用自定义序列化器.
package com.studytrails.json.gson;import java.lang.reflect.Type;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.google.gson.JsonElement;import com.google.gson.JsonObject;import com.google.gson.JsonSerializationContext;import com.google.gson.JsonSerializer;import com.google.gson.reflect.TypeToken;public class DogSerializer implements JsonSerializer<dog> { @Override public JsonElement serialize(Dog src, Type typeOfSrc, JsonSerializationContext context) { // This method gets involved whenever the parser encounters the Dog // object (for which this serializer is registered) JsonObject object = new JsonObject(); String name = src.getName().replaceAll(" ", "_"); object.addProperty("name", name); // we create the json object for the dog and send it back to the // Gson serializer return object; } public static void main(String[] args) { Animall<Dog> animal = new Animall<Dog>(); Dog dog = new Dog("I am a dog"); animal.setAnimal(dog); // Create the GsonBuilder and register a serializer for the Dog class. // Whenever the Dog class is encountered Gson calls the DogSerializer // we set pretty printing own to format the json Gson gson = new GsonBuilder().registerTypeAdapter(Dog.class, new DogSerializer()).setPrettyPrinting().create(); // Since Animal contains generic type create the type using TypeToken // class. Type animalType = new TypeToken<Animal<Dog>>() { }.getType(); System.out.println(gson.toJson(animal, animalType)); }}
Animal类
package com.studytrails.json.gson;public class Animal<t> { public T animal; public void setAnimal(T animal) { this.animal = animal; } public T get() { return animal; }}
Dog类
package com.studytrails.json.gson;public class Dog { private String name; public Dog(String name) { this.name = name; } public String getName() { return name; }}
Custom DeSerializer
使用一个自定义反序列化器把一个json转换成Dog对象.通过实现JsonDeserializer接口来自定义反序列化器.
package com.studytrails.json.gson;import java.lang.reflect.Type;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.google.gson.JsonDeserializationContext;import com.google.gson.JsonDeserializer;import com.google.gson.JsonElement;import com.google.gson.JsonParseException;import com.google.gson.reflect.TypeToken;public class DogDeserialiser implements JsonDeserializer<Dog> { @Override public Dog deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String name = json.getAsJsonObject().get("name").getAsString(); name = name.replace(" ", "_"); Dog dog = new Dog(name); return dog; } public static void main(String[] args) { String json = "{\"animal\":{\"name\":\"I am a dog\"}}"; Gson gson = new GsonBuilder().registerTypeAdapter(Dog.class, new DogDeserialiser()).create(); Type animalType = new TypeToken<Animal<Dog>>() { }.getType(); Animal<Dog> animal = gson.fromJson(json, animalType); System.out.println(animal.get().getName()); }}
0 0
- gson使用教程-翻译7
- gson使用教程-翻译1
- gson使用教程-翻译2
- gson使用教程-翻译3
- gson使用教程-翻译4
- gson使用教程-翻译5
- gson使用教程-翻译6
- gson使用教程-翻译8
- Gson使用教程
- GSON 使用教程
- Gson使用简单教程
- FastJson Jackson Gson使用教程
- Gson 教程
- 详解Gson使用(五)实现百度翻译功能
- 【Gson】Gson 使用笔记
- Gson源码学习笔记(1)——Gson使用教程(译)
- Gson教程十一(译):轻松使用仁慈的Gson(容错机制)
- Android Retrofit 2.0(二)使用教程OkHttp3 + Gson + RxJava
- ios键盘不遮挡输入框
- leetcode 165: Compare Version Numbers
- opbwwipylqbquhqd
- VM虚拟机鼠标体验差
- 支持向量机:Duality
- gson使用教程-翻译7
- 数据结构_队列
- Windows7上使用VS2013编译Caffe源码(不带GPU支持)步骤
- gson使用教程-翻译8
- lightoj 1050 - Marbles (概率DP)
- Python中通过注释修改编码格式的底层来源
- 3种实用常用的清除浮动方法
- Leetcode: Dungeon Game
- Pascal开关灯问题