gson序列化对象
/**
*YC
*2017年8月1日 下午4:59:14
* <p>Description: </p>
*/
package com.huiw.core.uic.common.utils.gson;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.huiw.core.uic.common.adapter.NullStringToEmptyAdapterFactory;
import com.huiw.core.uic.common.utils.converter.AnnotationExclusion;
import com.huiw.core.uic.common.utils.converter.FieldExclusion;
/**
* @author YC 获取一个完美gson:
*
*/
public class PerfectGson {
/**
*
* YC 2017年8月1日 下午5:09:34
* <p>
* Title: getGson
* </p>
* <p>
* Description:
* <h1>完美gson具有如下功能:</h1>
* <p>
* 1、serializeNulls(值为空也序列化)
* </p>
* <p>
* 2、registerTypeAdapterFactory(new
* NullStringToEmptyAdapterFactory())(值为null转换为"")
* </p>
* <p>
* 3、.setExclusionStrategies(new TargetStrategy())(排除策略 )
* </p>
*
* @return
*/
public static Gson getGson() {
return new GsonBuilder().serializeNulls()
.registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory<Object>())
.setExclusionStrategies(new AnnotationExclusion()).create();
}
/**
*
* YC 2017年8月11日 下午2:39:33
* <p>
* Title: getGson
* </p>
* <p>
* Description: 带参
* </p>
*
* @param strs
* @return
*/
public static Gson getGson(String[] strs) {
return new GsonBuilder().serializeNulls()
.registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory<Object>())
.setExclusionStrategies(new FieldExclusion(strs)).create();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
AnnotationExclusion类
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.huiw.archives.common.annotation.MyExclus;
/**
* YC 2017年8月1日 下午2:39:22
* <p>
* Title:排除策略
* </p>
* <p>
* Description: 用于排除一个类不用转换的字段
* </p>
*/
public class AnnotationExclusion implements ExclusionStrategy {
/**
*
* YC 2017年8月1日 下午2:41:39
* <p>
* Title: shouldSkipClass
* </p>
* <p>
* Description: 应该排除的类
* </p>
*
* @param class1
* @return
* @see com.google.gson.ExclusionStrategy#shouldSkipClass(java.lang.Class)
*/
@Override
public boolean shouldSkipClass(Class<?> class1) {
return false;
}
/**
*
* YC 2017年8月1日 下午2:41:57
* <p>
* Title: shouldSkipField
* </p>
* <p>
* Description: 应该排除的字段
* </p>
*
* @param fieldattributes
* @return
* @see com.google.gson.ExclusionStrategy#shouldSkipField(com.google.gson.FieldAttributes)
*/
@Override
public boolean shouldSkipField(FieldAttributes f) {
//如果属性带有MyExclus 注解,则排除
return f.getAnnotation(MyExclus.class) != null;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
FieldExclusion类
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.huiw.archives.common.annotation.MyExclus;
/**
* YC 2017年8月1日 下午2:39:22
* <p>
* Title:手动字段排除策略
* </p>
* <p>
* Description: 用于排除一个类不用转换的字段
* </p>
*/
public class FieldExclusion implements ExclusionStrategy {
String[] inStr;
public FieldExclusion(String[] outStr) {
this.inStr = outStr;
}
/**
*
* YC 2017年8月1日 下午2:41:39
* <p>
* Title: shouldSkipClass
* </p>
* <p>
* Description: 应该排除的类
* </p>
*
* @param class1
* @return
* @see com.google.gson.ExclusionStrategy#shouldSkipClass(java.lang.Class)
*/
@Override
public boolean shouldSkipClass(Class<?> class1) {
return false;
}
/**
*
* YC 2017年8月1日 下午2:41:57
* <p>
* Title: shouldSkipField
* </p>
* <p>
* Description: 应该排除的字段
* </p>
*
* @param fieldattributes
* @return
* @see com.google.gson.ExclusionStrategy#shouldSkipField(com.google.gson.FieldAttributes)
*/
@Override
public boolean shouldSkipField(FieldAttributes f) {
boolean flag = false;
for (int i = 0; i < inStr.length; i++) {
// 手动排除inStr串指定字段
if (f.getName().equals(inStr[i])) {
flag = true;
System.out.println("排除字段===============>" + f.getName());
} else {
// 如果属性带有MyExclus 注解,则排除
flag = f.getAnnotation(MyExclus.class) != null;
}
}
return flag;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
MyExclus接口
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author YC 如果属性带有MyExclus 注解,则GSON转换时排除
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
public @interface MyExclus {
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
值为空也序列化
public static Gson getGson(String[] strs) {
return new GsonBuilder().serializeNulls().create();
}
- 1
- 2
- 3
值为null转换为”“
public static Gson getGson(String[] strs) {
return new GsonBuilder()
.registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory<Object>())
.create();
}
/**
*YC
*2017年8月1日 下午4:41:21
* <p>Description: </p>
*/
package com.huiw.core.uic.common.adapter;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
/**
* @author YC 将null转换为""工厂适配器
*/
public class NullStringToEmptyAdapterFactory<T> implements TypeAdapterFactory {
@SuppressWarnings({ "unchecked", "hiding" })
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<T> rawType = (Class<T>) type.getRawType();
if (rawType != String.class) {
return null;
}
return (TypeAdapter<T>) new StringNullAdapter();
}
}
/**
*YC
*2017年8月1日 下午4:36:50
* <p>Description: </p>
*/
package com.huiw.core.uic.common.adapter;
import java.io.IOException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
/**
* @author YC 将null转换为""适配器
*/
public class StringNullAdapter extends TypeAdapter<String> {
@Override
public String read(JsonReader reader) throws IOException {
// TODO Auto-generated method stub
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return "";
}
return reader.nextString();
}
@Override
public void write(JsonWriter writer, String value) throws IOException {
// TODO Auto-generated method stub
if (value == null) {
writer.nullValue();
return;
}
writer.value(value);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
排除策略
public static Gson getGson() {
return new GsonBuilder().setExclusionStrategies(new AnnotationExclusion()).create();
}
- 1
- 2
- 3
- 4
- 5
@你这个只能将String类型的null转为空字符串啊,如果是Int、Long、ArrayList呢?那岂不是要写全部的数据类型,如果不管是什么类型都转换为空字符串,这个就不行了,比如Int类型的null只能转为Int不能转为空字符串。
public class Person implements Serializable {
<span class="hljs-javadoc">/**
*
*/</span>
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = <span class="hljs-number">1</span>L;
<span class="hljs-keyword">private</span> String name;
<span class="hljs-keyword">private</span> Integer age;
<span class="hljs-comment">//User类似Person对象有name、age字段</span>
<span class="hljs-keyword">private</span> List<User> list;
<span class="hljs-comment">//set、get略</span>
}
public static void main(String[] args) {
// 获取PerfectGson对象
Gson gson = PerfectGson.getGson();
// 实例化一个人(name[String]、age[Intager]、list[List<User>]),未赋值
Person person = new Person();
// PerfectGson转换出一个值为空的json串
gson = gson.fromJson(gson.toJson(person), new TypeToken<Person>() {
}.getType());
// 模拟spring MVC的controller层返回给ajax的数据
System.out.println(JSONObject.fromObject(gson));
}
//控制台:
//{“name”:”“,”age”:0,”list”:[]}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28