我正在Java 7中使用Gson解析器。我想将我的属性之一保留为原始JSON字符串,以便稍后可以将其反序列化为所需的类。
输入JSON示例:
{
"foo":"bar",
"body":["blah"]
}
Run Code Online (Sandbox Code Playgroud)
要么
{
"foo":"bar",
"body":{"a":"b"}
}
Run Code Online (Sandbox Code Playgroud)
反序列化为这样的类:
public class Message {
public String foo;
public String bar; //I want this to be a raw JSON string, not parsed
}
Run Code Online (Sandbox Code Playgroud)
码:
String input = "{\"foo\":\"bar\", \"body\":[\"blah\"]}";
Message message = gson.fromJson(input, Message.class);
message.foo; //"bar"
message.body; //"[\"blah\"]"
Run Code Online (Sandbox Code Playgroud)
这可能吗?
更新:
我目前正在执行此操作,但是如果它可以“更干净”并String以某种方式使用本机类型,那将很好。
public class Message {
public String foo;
public JsonString body;
}
public static class JsonString {
public String body;
}
public static class JsonStringDeserializer implements JsonDeserializer<JsonString> {
@Override
public JsonString deserialize(JsonElement json, Type type, JsonDeserializationContext context) {
JsonString a = new JsonString();
a.body = json.toString();
return a;
}
}
Run Code Online (Sandbox Code Playgroud)
我对此不满意的是,我必须像这样使用反序列化的对象:
Message message = gson.fromJson(input, Message.class);
//notice how i have to use the inner string
SomeClass cls = gson.fromJson(message.body.body, SomeClass.class);
Run Code Online (Sandbox Code Playgroud)
如果您不介意将body属性声明为 anObject而不是 aString来解决 JSON 解码问题;您可以执行以下操作。
的注释定义JSONRawString:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface JSONRawString {
}
Run Code Online (Sandbox Code Playgroud)
班上Message:
public class Message {
public String foo;
@JSONRawString
public Object body;
}
Run Code Online (Sandbox Code Playgroud)
解串器:
public class JSONRawStringDeserializer<T> implements JsonDeserializer<T> {
@Override
public T deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
//We decode using the normal deserializer; but will run over all the fields and check if our annotation exists. This is a bit hacky; but should work if you don't mind declaring your objects which you'd like to maintain as deserialized as Object.
T serializedObject = new Gson().fromJson(jsonElement, type);
Field[] fields = serializedObject.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.getAnnotation(JSONRawString.class) != null) {
try {
String fieldName = field.getName();
if(field.getAnnotation(SerializedName.class) != null) {
fieldName = field.getAnnotation(SerializedName.class).value();
}
String element = new Gson().toJson(jsonElement.getAsJsonObject().get(fieldName).toString());
field.set(serializedObject, element);
} catch(IllegalAccessException e) {
throw new JsonParseException(e);
}
}
}
return serializedObject;
}
}
Run Code Online (Sandbox Code Playgroud)
最后在Main:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Main {
public static void main(String[] args) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Message.class, new JSONRawStringDeserializer<Message>())
.create();
String input = "{\"foo\":\"bar\", \"body\":[\"blah\"]}";
Message message = gson.fromJson(input, Message.class);
System.out.printf("message.foo: %s, message.body: %s", message.foo, message.body);
}
}
Run Code Online (Sandbox Code Playgroud)
导致message.foo: bar, message.body: "[\"blah\"]"
这有点hacky;因为我们用我们自己的方法覆盖了 Gson 会吐出的内容;我不认为这可以递归地起作用。希望它能引导您找到解决方案。