包含AtomicInteger的类的对象无法使用GSON转换为JSON [英] Object of class containing AtomicInteger's fails to be converted to JSON using GSON

查看:372
本文介绍了包含AtomicInteger的类的对象无法使用GSON转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里大概是MyClass.java的样子:

  public class MyClass {

private static final AtomicInteger variableOne = new AtomicInteger();
private static final AtomicInteger variableTwo = new AtomicInteger();
private static final AtomicInteger variableThree = new AtomicInteger();
private static final AtomicInteger variableFour = new AtomicInteger();


/ *
*让这里的获取者和设置者
* /
}

我试图使用GSON将上面的类的对象转换为JSON



以下是代码:

  GsonBuilder gsonBuilder = new GsonBuilder(); 
gsonBuilder.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT);
Gson gson = gsonBuilder.create();
String jsonObject = gson.toJson(new MyClass());

但它会抛出以下异常:

 类java.util.concurrent.atomic.AtomicInteger声明多个名为serialVersionUID的JSON字段

我不知道如何处理这个问题,因为SO和其他论坛上的大多数答案都要求创建变量TRANSIENT,这基本上不是我想要实现的想法。

解决方案

对于任何遇到这种问题的人,我找到了答案:

使用FieldExcluder注册您的GsonBuilder(我已将其命名为FieldExtractor)
$ b $ pre $ g $ g $ gsonBuilder gsonBuilder = new GsonBuilder()。 setExclusionStrategies(新的FieldExtractor());

FieldExcluder看起来像这样:



<$私人类FieldExtractor实现ExclusionStrategy {

@Override
public boolean shouldSkipClass(Class<> arg0){
return false;
}

@Override
public boolean shouldSkipField(FieldAttributes f){

if(serialVersionUID.equals(f.getName())) {
返回true;
}

return false;
}

}

基本上你可以忽略任何字段以下格式给你例外:

 声明了多个名为serialVersionUID的JSON字段

您可以进一步修改if条件以进一步满足您的要求。

I am trying to serialize an Object of Class (let's say MyClass)

Here is roughly how MyClass.java looks like:

public class MyClass {

    private static final AtomicInteger variableOne = new AtomicInteger();
    private static final AtomicInteger variableTwo = new AtomicInteger();
    private static final AtomicInteger variableThree = new AtomicInteger();
    private static final AtomicInteger variableFour = new AtomicInteger();


    /*
    * Have the getters and setters here
    */
}

I am trying to convert object of the above class to JSON using GSON

Here is the code:

GsonBuilder gsonBuilder  = new GsonBuilder();
gsonBuilder.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT);
Gson gson = gsonBuilder.create();
String jsonObject = gson.toJson(new MyClass());

But it throws the following exception:

class java.util.concurrent.atomic.AtomicInteger declares multiple JSON fields named serialVersionUID

I am not sure how to deal with this issue as most of the answers on S.O and other forums ask to make the variable TRANSIENT and that's basically not the idea of what I want to achieve.

解决方案

For anyone who would come across such a problem, I found an answer:

Register your GsonBuilder with the FieldExcluder (I have named it FieldExtractor)

GsonBuilder gsonBuilder = new GsonBuilder().setExclusionStrategies(new FieldExtractor()); 

The FieldExcluder will look something like:

 private class FieldExtractor implements ExclusionStrategy {

            @Override
            public boolean shouldSkipClass(Class<?> arg0) {
                return false;
            }

            @Override
            public boolean shouldSkipField(FieldAttributes f) {

                if ("serialVersionUID".equals(f.getName())) {
                    return true;
                }

                return false;
            }

        }

Basically you can ignore any field that gives you exception in the format of:

declares multiple JSON fields named serialVersionUID

You can further modify the if condition to further suit your requirement.

这篇关于包含AtomicInteger的类的对象无法使用GSON转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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