com.jdkhome.blzo.ex.basic.tools.gson.StringNullAdapter Maven / Gradle / Ivy
package com.jdkhome.blzo.ex.basic.tools.gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
/**
* @author YC 将null转换为""适配器
*/
public class StringNullAdapter extends TypeAdapter {
@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);
}
}