简体   繁体   中英

How to replicate high json hierarchy into java objects using jackson api?

I want to write a code in JAVA using JACKSON that allows to display a high hierarchy in JSON like the following extract. In addition, the hierarchy must be structered in a way that the resulting json application depends on conditions "validFrom" and "validTo".

In the example below all dates for "validFrom" and "validTo" are the same but in practice the dates could vary, ie each child element could have several different validity periods:

Eg possible dates for "DE":
"validFrom": ["2000-01-31", "1995-01-31"]
"validTo": ["2099-01-31", "2000-01-30"]

So, for the combination of "validFrom"="1995-01-31" and "validTo"="2000-01-30" the child element "DE" hypothetically should belong to the parent "Z9" (not seen in the json example provided below!) but for the combination of "validFrom"="2000-01-31" and "validTo"="2099-01-31" it should belong to the parent "U6" (like in the json example below).

Ie depending on attributes "validFrom" and "validTo" the child element could belong to either one parent or another parent (in the example below "DE" only belongs to parent "U6" but the question is to implement such conditions into java code in a way that there is a differentiation possible). Whithin one hierarchy level a child element should belong only to one parent.

What does the code in java have to look like in order to write this structure in json using jackson?

{
"HierarchicalCode":[
{
    "code": "A1",
    "description": "Welt",
    "validFrom": "2000-01-31",
    "validTo": "2099-01-31",

    "children":[
        {
        "code": "U6",
        "description": "Inland",
        "validFrom": "2000-01-31",
        "validTo": "2099-01-31",

            "children":[
            {
            "code": "DE",
            "description": "Deutschland",
            "validFrom": "2000-01-31",
            "validTo": "2099-01-31"
            }
            ]
        },
        {
        "code": "Z9",
        "description": "Ausland",
        "validFrom": "2000-01-31",
        "validTo": "2099-01-31"

        }
    ]
}



]
}

I didn't quite get that logic based on validFrom and validTo dates.

First, you need to construct your model, second, you have to serialize it using object mapper. Example:

public static void main(String[] args) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    HierarchicalCode hierarchicalCode = objectMapper.readValue(s, HierarchicalCode.class);
    // we constructed our hierarchical code here somehow, now serialize
    String result = objectMapper.writeValueAsString(hierarchicalCode);
    System.out.println(result);
}

private static class HierarchicalCode {

    @JsonProperty("HierarchicalCode")
    private List<HierarchicalElement> hierarchicalCode;

    public List<HierarchicalElement> getHierarchicalCode() {
        return hierarchicalCode;
    }
}

private static class HierarchicalElement {

    private String code;
    private String description;
    private Timestamp validFrom;
    private Timestamp validTo;
    private List<HierarchicalElement> children;

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    public Timestamp getValidFrom() {
        return validFrom;
    }

    public Timestamp getValidTo() {
        return validTo;
    }

    public List<HierarchicalElement> getChildren() {
        return children;
    }
}

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM