Home > Software engineering >  Generic Json Map Setter in Java
Generic Json Map Setter in Java

Time:10-10

Suppose I have an empty Map<String, Object> map or a Map<String, Object> map with some fields added.

In this case, I want to create a generic java method that I can set to the map I have using the values such as map, path, value type and value object that I will give as input.

The method outline I intend to create can be seen below.

public void genericJsonMapSet(Map<String, Object> jsonMap, String setPath, String setValueType, Object setValue) {
    //Set JsonMap code
}

The json representation of the map before and after the following calls of the method to be created should be as follows.

    genericJsonMapSet(jsonMap, "setid()", "string", "a9a9940d-593f-4546-8065-6d70370d9f64");
    genericJsonMapSet(jsonMap, "setrequestDate()", "string", "2022-10-10 16:20:15");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[1].setvalueType()", "string", "string");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[1].setname()", "string", "username");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[1].setvalue()", "string", "taner.turan");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[2].setvalueType()", "string", "integer");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[2].setname()", "string", "id");
    genericJsonMapSet(jsonMap, "setuserCharacteristic[2].setvalue()", "integer", "23");

Before:

{

}

After:

{
    "id": "a9a9940d-593f-4546-8065-6d70370d9f64",
    "requestDate": "2022-10-10 16:20:15",
    "userCharacteristic": [
        {
            "valueType": "string",
            "name": "username",
            "value": "taner.turan"
        },
        {
            "valueType": "integer",
            "name": "id",
            "value": 23
        }
    ]
}

Can anyone provide code for this?

CodePudding user response:

This project works similarly. I wrote it.. a long long time ago, as far as I know nobody uses it. Map<String, Object> is not java-like at all; there is almost no purpose to such a thing. I encapsulated the notion into a JSON object instead. Also, you don't use strings to 'set' things, again, un-java like. Thus, this code:

Map<String, Object> jsonMap = new HashMap<>();
genericJsonMapSet(jsonMap, "setid()", "string", "a9a9940d-593f-4546-8065-6d70370d9f64");
genericJsonMapSet(jsonMap, "setuserCharacteristic[1].setvalueType()", "string", "string");
genericJsonMapSet(jsonMap, "setuserCharacteristic[2].setvalue()", "integer", "23");

Becomes:

JSON j = JSON.newMap();
j.get("setId").setString("a9a9940d-593f-4546-8065-6d70370d9f64");
j.get("setuserCharacteristic").add().setString("string");
j.get("setuserCharacteristic").get(2).setInteger(23);

add() is the same as .get(i) where i is one larger than whatever is there already. You can get non-existent things; they are created automatically once you .set anywhere in the 'chain'. i.e.:

JSON j = JSON.newMap();
j.get("foo").get("bar").setString("hello");

Gives you JSON structure:

{
  "foo": {
    "bar": "hello"
  }
}

Link to code on github

CodePudding user response:

You can make use of the function unflatten() of library Josson to do what you want.

https://github.com/octomix/josson

Josson josson = Josson.create();
josson.put("id", "a9a9940d-593f-4546-8065-6d70370d9f64");
josson.put("requestDate", "2022-10-10 16:20:15");
josson.put("userCharacteristic[1].valueType", "string");
josson.put("userCharacteristic[1].name", "username");
josson.put("userCharacteristic[1].value", "taner.turan");
josson.put("userCharacteristic[2].valueType", "integer");
josson.put("userCharacteristic[2].name", "id");
josson.put("userCharacteristic[2].value", 23);
JsonNode node = josson.getNode("unflatten('.[]')");
System.out.println(node.toPrettyString());

Output

{
  "id" : "a9a9940d-593f-4546-8065-6d70370d9f64",
  "requestDate" : "2022-10-10 16:20:15",
  "userCharacteristic" : [ {
    "valueType" : "string",
    "name" : "username",
    "value" : "taner.turan"
  }, {
    "valueType" : "integer",
    "name" : "id",
    "value" : 23
  } ]
}
  • Related