This is the fourth day of my participation in the November Gwen Challenge. See details: The last Gwen Challenge 2021.

The stream serialization of a piece of Gson was introduced earlier. Now let’s look at stream deserialization, mainly with the help of JsonReader

1. Key classes and methods

Stream deserialization, the key implementation class is JsonReader, each time before and after the use of beginObject/endObject to declare a serialization process

JsonReader reader = new JsonReader(new StringReader(json));
reader.beginObject(); // throws IOException
/ /...
reader.endObject();
Copy the code

2. Example demonstration

Next, an example is used to demonstrate the stream deserialization process

Json string “{\”user\”: \” blog\”, \”age\ : 18}”

Deserialization process

@Data
public static class User {
    private String user;
    private int age;
}

@Test
public void testReader(a) throws IOException {
    String str = "{\"user\ : \" ashes blog\", \"age\ : 18}";

    User user = new User();
    JsonReader reader = new JsonReader(new StringReader(str));
    reader.beginObject();
    while(reader.hasNext()) {
        String key = reader.nextName();
        if ("user".equalsIgnoreCase(key)) {
            user.setUser(reader.nextString());
        } else if ("age".equalsIgnoreCase(key)) {
            user.setAge(reader.nextInt());
        }
    }
    reader.endObject();
    System.out.println(user);
}
Copy the code

If the value in the json string is an object, or an array, what should be done?

For example, in the User class, we add a list object

@Data
public static class User {
    private String user;
    private int age;
    private List<String> skills;
}

@Test
public void testReader(a) throws IOException {
    String str = "{\" user \ ": \" a dusty blog \ ", \ "age \" : 18, \ "skills \" : [\ Java \ ", \ "python \]}";

    User user = new User();
    JsonReader reader = new JsonReader(new StringReader(str));
    reader.beginObject();
    while(reader.hasNext()) {
        String key = reader.nextName();
        if ("user".equalsIgnoreCase(key)) {
            user.setUser(reader.nextString());
        } else if ("age".equalsIgnoreCase(key)) {
            user.setAge(reader.nextInt());
        } else if ("skills".equalsIgnoreCase(key)) {
            // Note that this implementation supports nested operations
            reader.beginArray();
            List<String> skills = new ArrayList<>();
            while (reader.hasNext()) {
                skills.add(reader.nextString());
            }
            user.setSkills(skills);
            reader.endArray();
        }
    }
    reader.endObject();
    System.out.println(user);
}

Copy the code

A gray contact information

All letter is better than no book, the above content, purely one’s words, due to the limited personal ability, it is hard to avoid omissions and mistakes, such as finding bugs or better suggestions, welcome criticism and correction, not grudging gratitude

  • Personal site: blog.hhui.top
  • Micro Blog address: Small Gray Blog
  • QQ: a gray /3302797840
  • Wechat official account: One Grey Blog