Sha-1
Sha-1

Reputation: 197

Convert JSON to POJO using nested classes

I am receiving some JSON in a String format from a data provider, from my understanding i can convert the JSON into POJO using a class file. The data provider provides @JsonProperty classes which i've managed to use to convert the JSON into POJO's. The question i have is, is it possible to have just one nested java class that contains all the @JsonProperty classes? Instead of having separate classes, of which there are many.

Json String:

{
    "BillingAccount": {
        "AccountType": "Trial",
        "AccountBalance": 999.99,
        "TransactionCost": 999.99,
        "ExtraInformation": {}
    },

    "Request": {
        "RequestGuid": "abcde",
        "PackageId": "abcde",
        "PackageVersion": 2,
        "ResponseVersion": 2,
        "DataKeys": {
            "Vrm": "example"
        }
    }
}

BillingAccount.java

public class BillingAccount
{
    @JsonProperty("AccountType")
    public String accountType;
    @JsonProperty("AccountBalance")
    public double accountBalance;
    @JsonProperty("TransactionCost")
    public double transactionCost;
    @JsonProperty("ExtraInformation")
    public ExtraInformation extraInformation;
}

DataKeys.java

public class DataKeys
{
    @JsonProperty("Vrm")
    public String vrm;
}

Request.java

public class Request
{
    @JsonProperty("RequestGuid")
    public String requestGuid;
    @JsonProperty("PackageId")
    public String packageId;
    @JsonProperty("PackageVersion")
    public int packageVersion;
    @JsonProperty("ResponseVersion")
    public int responseVersion;
    @JsonProperty("DataKeys")
    public DataKeys dataKeys;
}

POJO.java

public class POJO {

    @JsonProperty("BillingAccount")
    public JSONSorter.BillingAccount billingAccount;
    @JsonProperty("Request")
    public JSONSorter.Request request;

}

Ideal Solution:

As you can see there is the JSONSorter class which houses the other classes. So when creating the POJO i would call public JSONSorter.Request request; as opposed to calling just Request. Reason this is an ideal solution is because Request class could have different properties depending on what dataset is being retrieved. So instead of having Request1, Request2, Request3 wouldn't it be better to have JSONSorter1, JSONSorter2, JSONSorter3. Each of which would contain the classes necessary to the data being retrieved.

public class JSONSorter {

    public class Request {
        @JsonProperty("RequestGuid")
        public String requestGuid;
        @JsonProperty("PackageId")
        public String packageId;
        @JsonProperty("PackageVersion")
        public int packageVersion;
        @JsonProperty("ResponseVersion")
        public int responseVersion;
        @JsonProperty("DataKeys")
        public DataKeys dataKeys;
    }

    public class DataKeys {
        @JsonProperty("Vrm")
        public String vrm;
    }

    public class BillingAccount {
        @JsonProperty("AccountType")
        public String accountType;
        @JsonProperty("AccountBalance")
        public double accountBalance;
        @JsonProperty("TransactionCost")
        public double transactionCost;
        @JsonProperty("ExtraInformation")
        public ExtraInformation extraInformation;
    }

}

Upvotes: 1

Views: 2601

Answers (1)

Willis Blackburn
Willis Blackburn

Reputation: 8204

Your ideal solution should work. The only thing that's missing is making the inner classes static. They have to be static so that your JSON deserializer can instantiate them without needing to attach them to an existing instance of JSONSorter.

Upvotes: 2

Related Questions