Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Gson创建JSON时POJO中黑色字段的影响_Java_Json_Gson - Fatal编程技术网

Java 使用Gson创建JSON时POJO中黑色字段的影响

Java 使用Gson创建JSON时POJO中黑色字段的影响,java,json,gson,Java,Json,Gson,我正在玩一个将JSON字符串转换成POJO和从POJO转换成JSON字符串的游戏。我想知道对于不同类型的json是否最好有一个新的类定义。因为我认为google gson必须解析空字段会减慢速度(虽然不明显,但仍然如此。我认为这是值得一试的,并在这里发表文章。)例如,登录客户机可能会更慢 public class CustomJSON { private String name; public String getName() { return name; } publ

我正在玩一个将JSON字符串转换成POJO和从POJO转换成JSON字符串的游戏。我想知道对于不同类型的json是否最好有一个新的类定义。因为我认为google gson必须解析空字段会减慢速度(虽然不明显,但仍然如此。我认为这是值得一试的,并在这里发表文章。)例如,登录客户机可能会更慢

public class CustomJSON {
    private String name;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}
但是我可以从客户那里得到其他东西,比如。喜欢的颜色、食物等

public class CommonJSON {
    private String name;
    private String food;
    private String music;

    //getters and setters.
}
所以我进行了测试

CustomJson与上面的相同,我在CommonJSON中添加了50个其他空字段,这些字段将不用于ax

public class CommonJSON {
    private String name;
    private String a;
          ...
    private String ax;

    //getters and setters.
}
我运行测试的主要方法如下

public static void main(String[] args) {
        // TODO Auto-generated method stub


        String theJSON = "";

        int passes = 100;
        long start;
        long finish;
        long exeTime;

        Gson gson = new Gson();

        //custom JSON
        start = System.nanoTime();
        for(int i=0;i<passes;i++)
        {
            CustomJSON custom = new CustomJSON();
            custom.setName("StackOverFlow");            
            theJSON = gson.toJson(custom);
        }
        finish = System.nanoTime();
        System.out.println(theJSON);
        exeTime = finish-start;
        System.out.println("Custom JSON \n\t>\t "+(exeTime)+" Nano seconds");
        System.out.println(" \t>\t "+Math.round((exeTime)/1000000.0)+" Micro seconds\n");

    //common JSON
    start= System.nanoTime();
    for(int i=0;i<passes;i++)
    {
        CommonJSON toClientJSONd = new CommonJSON();
        toClientJSONd.setName("StackOverFlow");
        theJSON = gson.toJson(toClientJSONd);
    }
    finish = System.nanoTime();
    System.out.println(theJSON);
    exeTime = finish-start;
    System.out.println("Common JSON \n\t>\t "+(exeTime)+" Nano seconds");
    System.out.println(" \t>\t "+Math.round((exeTime)/1000000.0)+" Micro seconds");

}
publicstaticvoidmain(字符串[]args){
//TODO自动生成的方法存根
字符串theJSON=“”;
int passs=100;
长起点;
长余辉;
时间长;
Gson Gson=新的Gson();
//自定义JSON
start=System.nanoTime();

对于(int i=0;i您似乎正在经历JVM预热的影响。有关更多信息,请参阅SO post。

回答我问题的另一部分。对于简单的JSON对象,使用GSON库中的JsonObject并添加属性更为方便

JsonObject json = new JsonObject();
json.addProperty("myBool", false);

问题在于你的测试方法,而不是代码(见下面的答案)。此外,你可以简单地告诉Gson不要序列化
null
字段。我试过了,但根本没有加快速度/