2

I have JSON serialization and deserialization is done using Jackson in java. I have so many JSON fields that to serialize and deserialize I have multiple single-member classes, is there any better way to do this?

I don't have any limitations on using Jackson library, that is the library I have used for most of my cases.

public class Data{
    public String type;
    public int id;
    public Attributes attributes;
    public Relationships relationships;
}

public class Category{
    public Data data;
}

public class Service{
    public Data data;
}

public class Priority{
    public Data data;
}

public class Status{
    public Data data;
}

public class User{
    public Data data;
}

public class Relationships{
    public Category category;
    public Service service;
    public Priority priority;
    public Status status;
    public User user;
}

public class Root{
    public Data data;
}

My sample JSON for which I am serializing looks like below.

{
  "data": {
    "id": 111,
    "type": "op type",
    "attributes": {
      "title": "Some title"
    },
    "relationships": {
      "category": {
        "data": {
          "type": "category",
          "id": 1
        }
      },
      "service": {
        "data": {
          "type": "service",
          "id": 3
        }
      },
      "priority": {
        "data": {
          "type": "priority",
          "id": 1
        }
      },
      "status": {
        "data": {
          "type": "status",
          "id": 3
        }
      },
      "user": {
        "data": {
          "type": "user",
          "id": 3
        }
      }
    }
  }
}
Rathan Naik
  • 993
  • 12
  • 25
  • 1
    see https://stackoverflow.com/questions/1957406/generate-java-class-from-json - don't create classes manually! Autogenerated code is a standart procedure with data classes – Martin Frank Jul 14 '21 at 06:19
  • 1
    @MartinFrank Thanks for the suggestion, i don't create classes manually, I do use jsonschema2pojo, but to import those as dependency would not help me, I need to create objects for serialization. – Rathan Naik Jul 14 '21 at 06:25
  • 2
    You can also use ``JsonNode`` or ``Map`` to deseralise to/from json string – pratap Jul 14 '21 at 06:25
  • @pratap Thanks for the suggestion, but Jackson object mapper seems like a cleaner way of doing it, of course it can be debated, but once I use JSONNode it has to be captured in some local variable, and its not easy to pass around all the JSON fields as local variables. In the end, we come to creating objects and Jackson object mapper is the better one I feel – Rathan Naik Jul 14 '21 at 06:39
  • Yes, I would agree creating object mapper is cleaner approach than JsonNode or Map. But like you said, you need to create many classes. May be you can try creating inner classes. It wont reduce no of classes though. – pratap Jul 14 '21 at 06:57

1 Answers1

1

Because Category, Service and others have the same fields data, if you create class manually, you can just create one common class DataWrapper. But I also see you said you use jsonschema2pojo rather than create class manually.

public class Data{
    public String type;
    public int id;
    public Attributes attributes;
    public Relationships relationships;
}

public class DataWrapper {
    public Data data;
}

public class Relationships{

    public DataWrapper category;
    public DataWrapper service;
    public DataWrapper priority;
    public DataWrapper status;
    public DataWrapper user;
}

public class Root{
    public Data data;
}
yejianfengblue
  • 2,089
  • 1
  • 14
  • 18
  • What's the advantage of creating `DataWrapper` over using `Data` as the type of the fields? – Mustafa Jul 14 '21 at 08:43
  • @Mustafa `DataWrapper` is a wrapper which contains one field of class `Data`. It replaces classes `Category`, `Service`, `Priority`, `Status`, and `User` rather than replaces `Data`. So the number of class is reduced from 5 to 1. – yejianfengblue Jul 14 '21 at 08:49