Json in Scheme

Our first program will demonstrate JSON encoding and decoding in Java. Here’s the full source code:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.*;

class Response1 {
    public int page;
    public List<String> fruits;

    public Response1(int page, List<String> fruits) {
        this.page = page;
        this.fruits = fruits;
    }
}

class Response2 {
    private int page;
    private List<String> fruits;

    public int getPage() { return page; }
    public void setPage(int page) { this.page = page; }
    public List<String> getFruits() { return fruits; }
    public void setFruits(List<String> fruits) { this.fruits = fruits; }
}

public class JsonExample {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();

        // First we'll look at encoding basic data types to JSON strings.
        System.out.println(mapper.writeValueAsString(true));
        System.out.println(mapper.writeValueAsString(1));
        System.out.println(mapper.writeValueAsString(2.34));
        System.out.println(mapper.writeValueAsString("gopher"));

        // And here are some for lists and maps, which encode to JSON arrays and objects.
        List<String> slcD = Arrays.asList("apple", "peach", "pear");
        System.out.println(mapper.writeValueAsString(slcD));

        Map<String, Integer> mapD = new HashMap<>();
        mapD.put("apple", 5);
        mapD.put("lettuce", 7);
        System.out.println(mapper.writeValueAsString(mapD));

        // The Jackson library can automatically encode your custom data types.
        Response1 res1D = new Response1(1, Arrays.asList("apple", "peach", "pear"));
        System.out.println(mapper.writeValueAsString(res1D));

        Response2 res2D = new Response2();
        res2D.setPage(1);
        res2D.setFruits(Arrays.asList("apple", "peach", "pear"));
        System.out.println(mapper.writeValueAsString(res2D));

        // Now let's look at decoding JSON data into Java values.
        String byt = "{\"num\":6.13,\"strs\":[\"a\",\"b\"]}";

        Map<String, Object> dat = mapper.readValue(byt, new TypeReference<Map<String, Object>>(){});
        System.out.println(dat);

        double num = (Double) dat.get("num");
        System.out.println(num);

        List<String> strs = (List<String>) dat.get("strs");
        String str1 = strs.get(0);
        System.out.println(str1);

        // We can also decode JSON into custom data types.
        String str = "{\"page\": 1, \"fruits\": [\"apple\", \"peach\"]}";
        Response2 res = mapper.readValue(str, Response2.class);
        System.out.println(res.getPage());
        System.out.println(res.getFruits().get(0));
    }
}

This program demonstrates JSON encoding and decoding using the Jackson library, which is a popular JSON library for Java.

First, we define two classes Response1 and Response2 to demonstrate encoding and decoding of custom types.

In the main method, we create an ObjectMapper instance, which is the main class for JSON processing in Jackson.

We then demonstrate encoding of basic data types (boolean, integer, float, string) to JSON strings using writeValueAsString().

Next, we show how to encode lists and maps to JSON arrays and objects.

We then encode custom objects (Response1 and Response2) to JSON.

For decoding, we first demonstrate how to decode a JSON string into a Map<String, Object>, which is similar to decoding into a generic data structure.

We also show how to access nested data in the decoded map.

Finally, we demonstrate decoding JSON directly into a custom Java class (Response2).

To run this program, you’ll need to include the Jackson library in your project. You can do this by adding the following dependency to your Maven pom.xml file:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.5</version>
</dependency>

Or if you’re using Gradle, add this to your build.gradle file:

implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.5'

After setting up the project with the Jackson library, you can compile and run the program:

$ javac JsonExample.java
$ java JsonExample

This will output the JSON encodings and decodings demonstrated in the program.