Search code examples
javajsongsonpojo

Creating POJOs to match a JSON structure


I have devised a JSON structure to represent a table with header columns plus table rows that looks like the following.

{
  "header": [
    {
      "fieldType": "STRING",
      "readOnly": true,
      "headerValue": "name"
    },
    {
      "fieldType": "STRING",
      "readOnly": true,
      "headerValue": "description"
    }
  ],
  "rows": [
    [
      {
        "fieldValue" : "engine"            
      },
      {
        "fieldValue" : "this is an engine"
      }
    ],
    [
      {
        "fieldValue" : "engine"            
      },
      {
        "fieldValue" : "this is an engine"
      }
    ],
    [
      {
        "fieldValue" : "engine"            
      },
      {
        "fieldValue" : "this is an engine"
      }
    ],
    [
      {
        "fieldValue" : "engine"            
      },
      {
        "fieldValue" : "this is an engine"
      }
    ]
  ]
}

A row is for example

[
  {
    "fieldValue" : "engine"            
  },
  {
    "fieldValue" : "this is an engine"
  }
]

The number of entries in a row matches the number of header columns. So "engine" is the "name" column and "this is an engine" is the "description" column

When I use GSON to turn my POJO's into a JSON String the closest I have got to match this structure is:

{
  "header": [
    {
      "fieldType": "STRING",
      "readOnly": true,
      "headerValue": "name"
    },
    {
      "fieldType": "STRING",
      "readOnly": true,
      "headerValue": "description"
    }
  ],
  "rows": [
    {
      "fieldValues": [
        "engine",
        "this is an engine"
      ]
    },
    {
      "fieldValues": [
        "engine",
        "this is an engine"
      ]
    },
    {
      "fieldValues": [
        "engine",
        "this is an engine"
      ]
    },
    {
      "fieldValues": [
        "engine",
        "this is an engine"
      ]
    }
  ]
}

Here's the code I'm using to test

enum FieldType {

    STRING,
    BOOLEAN,
    NUMBER,
    PHOTO,
    PHOTOLIST;

}

class SurveyFields {

    private List<SurveyColumn> header;  
    private List<SurveyRow> rows;

    public List<SurveyColumn> getHeader() {
        return header;
    }

    public List<SurveyRow> getRows() {
        return rows;
    }

    public void setHeader(List<SurveyColumn> header) {
        this.header = header;
    }

    public void setRows(List<SurveyRow> rows) {
        this.rows = rows;
    }

}

class SurveyColumn {

    private FieldType fieldType;
    private boolean readOnly;
    private String headerValue;

    public static class Builder {
        private FieldType fieldType;
        private boolean readOnly;
        private String headerValue;

        public Builder withFieldType(FieldType fieldType) {
            this.fieldType = fieldType;
            return this;
        }

        public Builder withReadOnly(boolean readOnly) {
            this.readOnly = readOnly;
            return this;
        }

        public Builder withHeaderValue(String headerValue) {
            this.headerValue = headerValue;
            return this;
        }

        public SurveyColumn build() {
            return new SurveyColumn(fieldType, readOnly, headerValue);
        }
    }

    public SurveyColumn(FieldType fieldType, boolean readOnly, String headerValue) {
        this.fieldType = fieldType;
        this.readOnly = readOnly;
        this.headerValue = headerValue;
    }
}

class SurveyRow {

    public static class Builder {
        private String[] fieldValues;

        public Builder withFieldValues(String[] fieldValues) {
            this.fieldValues = fieldValues;
            return this;
        }

        public SurveyRow build() {
            return new SurveyRow(fieldValues);
        }
    }

    private String[] fieldValues;

    public SurveyRow(String[] fieldValues) {
        this.fieldValues = fieldValues;
    }
}

public class TestGson {

    public static void main(String[] args) {

        SurveyFields fields = new SurveyFields();

        fields.setHeader(Arrays.asList(new SurveyColumn[] {
                new SurveyColumn.Builder().withHeaderValue("name").withFieldType(FieldType.STRING).withReadOnly(true)
                        .build(),
                new SurveyColumn.Builder().withHeaderValue("description").withFieldType(FieldType.STRING)
                        .withReadOnly(true).build() }));

        fields.setRows(Arrays.asList(new SurveyRow[] {
                new SurveyRow.Builder().withFieldValues(new String[] { "engine", "this is an engine" }).build(),
                new SurveyRow.Builder().withFieldValues(new String[] { "engine", "this is an engine" }).build(),
                new SurveyRow.Builder().withFieldValues(new String[] { "engine", "this is an engine" }).build(),
                new SurveyRow.Builder().withFieldValues(new String[] { "engine", "this is an engine" }).build()
        }));

        Gson gson = new Gson();
        System.out.println(gson.toJson(fields));

    }

}

How can I structure my POJO's to match the expected JSON output?


Solution

  • If you get the JSON from a third party, this site might help you generate POJO from JSON.