简体   繁体   English

用反序列化编写Java类以表示简单的JSON文档的最简洁方法是什么?

[英]What is the most concise way to write a Java class to represent a simple JSON document, with deserialization?

Here's the current style I've got for simple messages, using Jackson: 这是我使用杰克逊获得的简单消息的当前样式:

public class FooRequest {
    public final String foo;
    public final int bar;

    @JsonCreator
    public FooRequest(
            @JsonProperty("foo") String foo,
            @JsonProperty("bar") int bar) {
        this.foo = foo;
        this.bar = bar;
    }
}

Is there a class annotation out there that will handle the boiler plate constructor? 是否有一个类注释可以处理样板构造函数?

A quick Google search brought up this: 快速的Google搜索显示了以下内容:

Creating a json object using jackson 使用杰克逊创建json对象

Looks like you can handle the construction with JsonNodeFactory. 看起来您可以使用JsonNodeFactory处理构造。

final JsonNodeFactory factory = JsonNodeFactory.instance;

Is that what you were looking for? 那是您要找的东西吗?

You can try using Jackson annotation . 您可以尝试使用Jackson annotation The Jackson annotation @JsonCreator is used to tell Jackson that the Java object has a constructor (a "creator") which can match the fields of a JSON object to the fields of the Java object. Jackson批注@JsonCreator用于告诉Jackson该Java对象具有一个构造函数(“创建者”),该构造函数可以将JSON对象的字段与Java对象的字段进行匹配。

Let's consider the following JSON object that we want to deserialize. 让我们考虑以下要反序列化的JSON对象。

{
    "name":"Mr.Bond",
    "age":"30"
}

You can unmarshall the message by annotating the constructor with @JsonCreator and using the @JsonProperty 您可以通过使用@JsonCreator注释构造@JsonCreator并使用@JsonProperty来解组消息。

public class UserProfileCreator {
    public int age;
    public String name;

    @JsonCreator
    public UserProfileCreator(
        @JsonProperty("age") int age, 
        @JsonProperty("name") String name) {
            this.age = age;
            this.name = name;
    }
}

How will it work? 如何运作?

Let's write a small test program. 让我们编写一个小的测试程序。

@Test
public void deserialize()
    throws JsonProcessingException, IOException {

    String json = "{"age":30,"name":"Mr.Bond"}";
    UserProfileCreator obj = new ObjectMapper().reader(UserProfileCreator.class).readValue(json);
    assertEquals("Mr.Bond", obj.name);
}

Google's gson library doesn't require you to use annotations or constructors: Google的gson库不需要您使用注释或构造函数:

import com.google.gson.Gson;
import java.util.Date;
public class Test {
    public static void main(String[] args) {
        Gson gson = new Gson();

        String toJson = gson.toJson(new Thing());
        System.out.println(toJson);
        // prints {"appleType":"granny","date":"Apr 21, 2016 10:31:17 AM","numPies":6}

        Thing fromJson = gson.fromJson(toJson, Thing.class);
        System.out.println(fromJson);
        // prints com.mycompany.mavenproject1.Thing@506c589e
    }
}
class Thing {
    String appleType = "granny";
    Date date = new Date();
    int numPies = 6;
}

Jackson may have similar features, but I don't know. 杰克逊可能具有类似的功能,但我不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写此 java 代码的最简洁/最佳方式是什么? - What's the most concise / best way to write this java code? Java中最简洁的方法是从“AlphaSuffix”中获取“Alpha”? - What's the most concise way in Java to get the “Alpha” out of “AlphaSuffix”? 在 Java 中比较版本的最简洁方法 - Most concise way of comparing versions in Java 在Java中提取JSON URL的最简单方法? - Most simple way to pull JSON URL in Java? 实现二维数组中的值支持的模型类的最快,最简洁/正确的方法是什么? - What is the fastest and most concise/correct way to implement this model class backed by values in a 2-dimensional array? 使用Java 8,打印文件中所有行的最优选和简洁方法是什么? - Using Java 8, what is the most preferred and concise way of printing all the lines in a file? 使用Java 8,创建排序和分组字符串列表的最简洁方法是什么 - Using Java 8, what is the most concise way of creating a sorted AND grouped list of Strings 使用Java 8,迭代地图中所有条目的最简洁方法是什么? - Using Java 8, what is the most concise way of iterating through all the entries in a map? 在Java 8中编写算术运算函数的简明方法 - A concise way to write functions for arithmetical operations in Java 8 有没有更简洁的方法来编写这个 Java 代码? - Is there a more concise way to write this Java code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM