3
\$\begingroup\$

I'm a front end developer who is brand new to backend development. My task is to model json in a Java object. It's just some mock data for now that my controller returns.

{
"data": {
    "objectId": 25,
    "columnName": [
        "myCategory",
        "myCategoryId"
    ],
    "columnValues": [
        [
            "Category One",
            1
        ],
        [
            "Category Two",
            2
        ],
        [
            "Category Three",
            3
        ],
        [
            "Category Four",
            4
        ],
        [
            "Category Five",
            5
        ]
    ]
}

}

And here's my attempt. The controller returns this json correctly. But isn't this too simple? What I believe should be done is extrapolate the columnName and columnValues arrays into separate classes but I'm not sure how.

package com.category;

import java.util.List;

public class MyObjectData {
    private int objectId;
    private List columnName;
    private List columnValues;

    public int getObjectId() {
        return objectId;
    }
    public void setObjectId(int objectId) {
        this.objectId = objectId;
    }

    public List getColumnName() {
        return columnName;
    }

    public void setColumnName(List colName) {
        this.columnName = colName;
    }

    public List getColumnValues() {
        return columnValues;
    }

    public void setValues(List values) {
        this.columnValues = values;
    }

}

Regarding the columnNames and columnValues, I feel like I should be doing something like this in the model instead:

private List<ColumnNames> columnNames;
    private List<ColumnValues> columnValues;

    public List<ColumnNames> getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(List<ColumnNames> columnNames) {
        this.columnNames = columnNames;
    }

    public List<ColumnValues> getColumnValues() {
        return columnValues;
    }

    public void setColumnValues(List<ColumnValues> columnValues) {
        this.columnValues = columnValues;
    }

And then I'd have two separate classes for them like this:

package com.category;

import java.util.List;

public class ColumnName {

    private String columnName;

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

}

package com.category;

import java.util.List;

public class ColumnValue {

    private String columnValue;
    private int columnValueId;

    public String getColumnValue() {
        return columnValue;
    }

    public void setColumnValue(String columnValue) {
        this.columnValue = columnValue;
    }

    public String getColumnValueId() {
        return columnValueId;
    }

    public void setColumnValueId(int columnValueId) {
        this.columnValueId = columnValueId;
    }

}

I feel like I have all the right pieces but just not sure if this is a better approach than my initial attempt...which works. Just looking for input. Thanks in advance.

\$\endgroup\$
3
  • \$\begingroup\$ Do you have to model the data as it is? If the data and its layout will never change, your approach of defining classes like that should be fine. However, if the data changes even slightly, eventually it would probably be a hassle to keep updating and revising your classes. Instead, it would probably be better to use a JSON library to contain the data so that it can be easily modified. There is one that I've used that's lightweight and reliable; I'll try to find a link to the github page. \$\endgroup\$ Jun 22, 2019 at 15:30
  • \$\begingroup\$ I'm messing around with Jackson now. Thank you for the suggestion. \$\endgroup\$
    – user203355
    Jun 22, 2019 at 17:49
  • 1
    \$\begingroup\$ The data looks like it is a straight representation of a CSV table in JSON. If this is the case, then I would call it "flat json" instead of "hierarchical" and it might be helpful if you modeled your Java objects to represent a CSV data structure. By naming your objects as Table, Header and Row or similar, they would describe the data structures accurately. \$\endgroup\$ Jun 25, 2019 at 8:45

1 Answer 1

1
\$\begingroup\$

You implied Json -> Java object, but have you considered the other way around? If so, check out Java object to Json serialization/deserialization using Gson. Get more at https://en.wikipedia.org/wiki/Gson and https://static.javadoc.io/com.google.code.gson/gson/2.8.5/com/google/gson/Gson.html

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.