1
votes

I am trying to serialize a list of JSON blobs and put certain keys into a HashTable during the serialization. Here is an example of my JSON:

[
    {
        "name": "sally",
        "id": 1,
        "eye_color": "green"
    },
    {
        "name": "jack",
        "id": 2,
        "eye_color": "blue"
    },
    {
        "name": "jane",
        "id": 3,
        "eye_color": "brown"
    }
]

What I am looking for specifically is a POJO (or set of POJOs) which can serialize the above JSON like so with Jackson assuming the above JSON is in a file called above_json.json:

MyPOJO pojo = objectMapper.readValue(new File("above_json.json"), MyPOJO.class);

I want the result of the serialization to give me a HashTable (or an Object which encapsulates the HashTable) where the HashTable key is the value of name and the Hashtable value is the value of the corresponding id above.

Assuming we serialized the above JSON in this fashion, I would want to access the HashTable like so:

myTable.get("jane")

result: 3

myTable.get("jack")

result: 2

myTable.get("Jill")

result: null

I know how to serialize basic JSON with Jackson. I have an example like below:

JSON Input:

"Parameter":{
    "Name":"Parameter-Name",
    "Value":"Parameter-Value"
}

POJO to serialize above simple JSON:

public class Parameter {
    @JsonProperty("Name")
    public String name;
    @JsonProperty("Value")
    public String value; 
}

But obviously this type of setup does not put the results into a HashTable. I need a POJO like what I have in this example which will serialize JSON directly into a HashTable

3

3 Answers

0
votes

I don't think that is possible.

You serialize this json into a list of pojos, and have a utility function to generate the hashtable in the way you desire from the list of pojos.

0
votes

Create a POJO for holding the properties you are interested in.

@JsonIgnoreProperties(ignoreUnknown = true)
private static class MyPOJO {
    @JsonProperty("name")
    private String name;
    @JsonProperty("id")
    private Integer id;

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

Deserialize the contents of the file into List<MyPOJO>

 List<MyPOJO> myPOJO = mapper.readValue(new File(".."), new TypeReference<List<MyPOJO>>(){});

Stream the contents of the map to construct a map whose key is the name and value is the id.

Map<String, Integer> map = myPOJO.stream()
            .collect(Collectors.toMap(MyPOJO::getName, MyPOJO::getId));
0
votes

First of all, you probably don't want to use a HashTable, as it's considered to be an obsolete type (see here).

Either use a HashMap or if you want thread safety, a ConcurrentHashMap or a thread-unsafe Map backed by Collections.synchronized[...] and referenced to within synchronized statements.

Secondly, you can use a TypeReference to de-serialize as your desired type.

Finally, your JSON's syntax is incorrect: it starts with a square bracket ([) and ends with a curly bracket (}), which is technically unparseable.

Assuming you want an array of Maps here (e.g. HashMap<String, String>[]), here is some suitable code, provided you replace the last curly bracket with a square one:

// the map array
Map<String, String>[] map = null;
map = om.readValue(yourFile, new TypeReference<HashMap<String, String>[]>() {});
// prints the de-serialized contents
System.out.println(Arrays.toString(map));

Edit

Since you have now edited your JSON to remove the first square bracket and replace it with a curly bracket, no you can't parse as a Map as is.

Edit 2

Since you have now re-edited your JSON to feature square brackets once again instead of curly brackets in the wrapping object, you can once again de-serialize as a Map[]. Until the next edit, I guess...