阻止GSON序列化JSON字符串 [英] Prevent GSON from serializing JSON string

查看:553
本文介绍了阻止GSON序列化JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是gson的新手,并且有新的问题,我没有找到答案,所以请耐心等待。 StackOverflow和Google不是我的朋友:(



我有一个java类User,它的一个属性externalProfile是一个包含已经序列化的JSON 。当gson序列化User对象时,它会将externalProfile视为原始对象,从而转义JSON并添加额外的斜线等。
我希望gson将字符串单独留下,只是使用它原样,因为它已经是有效的和可用的JSON。

为了区分JSON字符串,我创建了一个名为JSONString的简单类,并且我尝试使用reader / writers,registerTypeAdapter,但没有任何效果。 b $ b你可以帮我吗?

  public class User {
private JSONString externalProfile;
public void setExternalProfile(JSONString externalProfile){this.externalProfile = externalProfile;}

}

public final class JSONString {
private String simpleString;
public JSONString (String simpleString){this.simpleStrin g = simpleString; }
}

public customJsonBuilder(Object object){
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(GregorianCalendar.class,new JsonSerializer< GregorianCalendar>(){
public JsonElement serialize(GregorianCalendar src,Type type,JsonSerializationContext context){
if(src == null){
return null;
}
返回新的JsonPrimitive(新的SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(src.getTime()));
}
});
Gson gson = builder.create();
return gson.toJson(object);
}

例如,externalProfile将会保持(如字符串值): p>

  {profile:{registrationNumber:11111}} 

在将它作为JSONString存储在User对象中后,我们将用户对象转换为JSON:

  User user = new User(); 
user.setExternalProfile(new JSONString(externalProfile)),
String json = customJsonBuilder(user);

json将包含以下内容:

  {\profile \:{\registrationNumber \:11111}} 

因此,externalProfile JSONString由gson作为String原始序列化,在双引号前添加额外的斜线。
我希望gson按原样离开这个JSONString,因为它已经是可用的JSON。
我正在寻找一个类型适配器/读写器来做到这一点,但我无法让它工作。

解决方案正如Alexis C所说:



首先将externalProfile作为JsonObject存储:

  new Gson()。fromJson(externalProfile,JsonObject.class)); 

让gson在输出User对象时再次序列化它。
将产生完全相同的JSON!

I'm new to gson, and have newby question which I have not found an answer to, so please bear with me. StackOverflow and google were not my friend :(

I have a java class "User", and one of its properties, "externalProfile" is a Java String containing already serialized JSON. When gson serializes the User object, it will treat externalProfile as primitive and thus escaping the JSON adding extra slashes etc. I want gson to leave the string alone, just using it "as is", because it is already valid and usable JSON.

To distinguish the JSON string, I created a simple class called JSONString, and I've tried using reader/writers, registerTypeAdapter, but nothing works. Can you help me out?

public class User {
    private JSONString externalProfile;
    public void setExternalProfile(JSONString externalProfile) { this.externalProfile = externalProfile; }

}

public final class JSONString {
    private String simpleString;
    public JSONString(String simpleString) { this.simpleString = simpleString; }
}

public customJsonBuilder(Object object) {
    GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(GregorianCalendar.class, new JsonSerializer<GregorianCalendar>() {
            public JsonElement serialize(GregorianCalendar src, Type type, JsonSerializationContext context) {
                if (src == null) {
                    return null;
                }
                return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(src.getTime()));
            }
        });
        Gson gson = builder.create();
        return gson.toJson(object);
}

As en example, the externalProfile will hold (as String value):

{"profile":{"registrationNumber": 11111}}

After I store it as JSONString in the User object, and we convert the user object to JSON:

User user = new User();
user.setExternalProfile(new JSONString(externalProfile)),  
String json = customJsonBuilder(user);

json will hold something like:

{\"profile\":{\"registrationNumber\": 11111}}

So, the externalProfile JSONString is serialized by gson as String primitive, adding the extra slashes in front of the doublequotes. I want gson to leave this JSONString as is, because it already is usable JSON. I'm looking for a type adapter / reader-writer to do this, but I can't get it to work.

解决方案

As stated by Alexis C:

store the externalProfile as a JsonObject first:

new Gson().fromJson(externalProfile, JsonObject.class));

And let gson serialize this again when outputting the User object. Will produce exactly the same JSON!

这篇关于阻止GSON序列化JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆