0

I'm trying to workout how I should be structuring my JSON objects to best comply with http://jsonapi.org/format/

If I used the following classes:

Page (main object)

public class Page {

    @Id
    @ObjectId
    private String id;  //Used for mongodb

    @JsonProperty
    private String type;

    @JsonProperty
    private Attribute attributes;

    @JsonProperty
    private Meta meta;

    public Page() {
        // Jackson deserialization
    }

    // Getters and setters
}

Attributes (nested into page)

public class Attribute {

    @JsonProperty
    private Date created = new Date();

    @JsonProperty
    private String title;

    @JsonProperty
    private String body;

    public Attribute() {
        // Jackson deserialization
    }

    public Attribute(Date created, String title, String body) {
        this.created = created;
        this.title = title;
        this.body = body;
    }

    // Getters and setters

}

Meta (nested into page)

public class Meta {

    @JsonProperty
    private List<String> authors;

    public Meta() {
    }

    public Meta(List<String> authors) {
        this.authors = authors;
    }

    // Getters and setters
}

I can create this object with a post such as:

{
    "type": "page",

    "attributes": {
        "title": "This is the title",
        "body": "<p>This is a long section of html, other stuff</p>"
    }, 

    "meta": {
        "authors": [
            "Steve",
            "John",
            "Sam"
        ]
    }
}

And the resulting JSON object is created:

{  
   id:"56cbed5036f66b05dc2df841",
   type:"page",
   attributes:{  
      created:1456205138886,
      title:"This is the title",
      body:"<p>This is a long section of html, other stuff</p>"
   },
   meta:{  
      authors:[  
         "Steve",
         "John",
         "Sam"
      ]
   }
}

The question(s): Is creating multiple classes the way I have the optimal/correct way of creating nested JSON objects, and should I be trying to wrap this all inside "data:" as per the link above states is a MUST do? If that is the case should I create a single POJO called Data which contains the Page object?

When looking for information around this type of thing all I seem to be able to find is people asking how to deserialize JSON into POJOs, which isn't what I'm looking for.

Really want to find some best practises here for writing APIs.

Chris
  • 3,437
  • 6
  • 40
  • 73
  • In order to serialize nested JSON, you need to define a class for each JSON object. As far as best practice, that's kind of opinionated – OneCricketeer Feb 23 '16 at 05:51

1 Answers1

0

you should start then from what kind of 'behavior' your objects expose and how you want your API to expose. Alhough there is no silver bullet, there is a good amount of literature around which can guide you in the right direction of how to model your API (and in turn your objects)

here are a few links:

Personally, and -in general-, I create POJOs, but like also @cricket_007 mentioned it's kind of opinionated.

HTH.

Community
  • 1
  • 1
Daniele
  • 1,053
  • 10
  • 17