ducthinh2111 / weekend-project

0 stars 0 forks source link

Read json file and deserialize json to correct type #24

Open nganori opened 1 week ago

nganori commented 1 week ago

Given a json file, think a way to to deserialize json to correct type, it means the result can be casted to expected type

Example 1

{
    "type": "my.example.dto.Student",
    "value": {
        "id": 123,
        "name": "Student 1",
                "class": "1A",
               "gender: "nu"
    }
}

Expected result: Object Student (id = 123, name = Studen1)

Example 2

{
    "type": "java.util.List",
    "value": [
        {
            "id": 123,
            "name": "Student 1",
                        "class": "1A",
                        "gender: "nam"
        },
        {
            "id": 124,
            "name": "Student 2",
                        "class": "1A",
                        "gender: "nu"
        }
    ]
}

Expected result: List [Student (id = 123, name = Studen1), Student (id = 124, name = Studen2)]

Example 3

{
   "type":"java.util.Map",
   "value":{
      "1A":{
         "nu":[
            {
               "id":123,
               "name":"Student 1",
               "class":"1A",
               "gender":"nu"
            },
            {
               "id":124,
               "name":"Student 2",
               "class":"1A",
               "gender":"nu"
            }
         ]
      },
      "1B":{
         "nu":[
            {
               "id":125,
               "name":"Student 3",
               "class":"1B",
               "gender":"nu"
            },
            {
               "id":126,
               "name":"Student 4",
               "class":"1B",
               "gender":"nu"
            }
         ]
      }
   }
}

Expected result: Object is Map<String: class, Map<String: gender, Student>>

OzGhost commented 1 week ago

Do we support reference reconstructing? example:

// service impl
boolean mahFunc(List<X> arr) {
    return arr.get(0) == arr.get(1);
}
// input
List<X> arr = new ArrayList<>();
X item = new X();
arr.add(item);
arr.add(item);
// test
assertTrue(mahFunc(arr));