0

What Java data structure can be used to serialize to the following Json which have a set of child objects?

Example:

{
    "John": {
        "Type": "Person",
        "age": 30,
        "Sean": {
            "Type": "child",
            "age": 3
        },
        "Julian": {
            "Type": "child",
            "age": 4
        }
    },
    "Paul": {
        "Type": "Person",
        "age": 64,
        "Stella": {
            "Type": "child",
            "age": 10
        },
        "James": {
            "Type": "child",
            "age": 12
        }
    }
}

Writing John and Paul can be done by: Map<String,Person> but i cannot figure out how to nest the Child without having the 'children' property.

Example:

{
    "John": {
        "Type": "Person",
        "age": 30,
        "children": {
            "Sean": {
                "Type": "child",
                "age": 3
            },
            "Julian": {
                "Type": "child",
                "age": 4
            }
        }
    }
}

I am not sure it is relevant, but Gson is being used to create the Json file

2
  • let us know what you have tried Commented Jun 9, 2020 at 13:02
  • I have tried registering a Gson TypeAdapter that override the write for the children Map, I wasn't able to get it to work as expected and it seems like a very cumbersome solution. Commented Jun 9, 2020 at 16:00

1 Answer 1

1

I'm not sure wheher this is possible with GSON though it's possible with Jackson.

With GSON you can try custom JsonSerializer, which might look something like this:

private static class PersonTypeSerializer implements JsonSerializer<Person> {

    @Override
    public JsonElement serialize(Person person, Type type, JsonSerializationContext jsonSerializationContext) {
        JsonObject personJson = personToJson(person);
        for (Map.Entry<String, Person> child : person.getChildren().entrySet()) {
            personJson.add(child.getKey(), personToJson(child.getValue()));
        }
        return personJson;
    }

    private static JsonObject personToJson(Person person) {
        JsonObject personJson = new JsonObject();
        personJson.addProperty("Type", person.getType());
        personJson.addProperty("age", person.getAge());

        return personJson;
    }
}

and register to GSON instance like this:

Gson gson = new GsonBuilder().registerTypeAdapter(Person.class, new PersonTypeSerializer())
                             .create();

Note that code assumes that both type "Person" and "child" are represented by the same Person class. It should be pretty easy to modify this if not.

Sign up to request clarification or add additional context in comments.

2 Comments

I am looking to serialize some data structure to the Example Json, the question is how to represent the Json example with a Java class
Ah, right! Sorry for misunderstanding. I've edited the original answer. The idea behind stays the same. Again I'm not sure whether this can be done by single datastructre using GSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.