fotrenc
fotrenc

Reputation: 80

Deserialize Json with inconstant field to POJO in Java

Is there a possibility to create POJO class in Java to which this Json can be deserialized?

{
    "name": "value",
    "random-value-01" : {
        "constant-field-00":"value_00",
        "constant-field-01":"value_01"
    },
    "random-value-02" : {
        "constant-field-00":"value_02",
        "constant-field-01":"value_03"
    },
    ...
    "random-value-XX" : {
        "constant-field-00":"value",
        "constant-field-01":"value"
     },
}

Upvotes: 0

Views: 451

Answers (2)

bazza
bazza

Reputation: 8414

This is not an answer as such, but may pique one's interest on the general topic.

For those interested in a different approach to JSON, one can look at the ITU's ASN.1. Think of it as a bit like Google Protocol Buffers, but with a whole load of different wire formats (including JSON, XML, and a whole load of binary wire formats with varying properties). Basically there's a wire format for every occasion and purpose.

This can be extremely useful sometimes. If you want to ship data around a distributed system, and parts of that system are, say C on a microcontroller at the end of a low bandwidth radio link, whilst other parts are Java on a server, then you can encompass all of your system messaging in a single schema (which acts as a the single point of truth). From this you can (depending on the tools you use) generate plain old objects in C, C++, Java, C#, and even ADA, VHDL. Python is a notable omission (there's Python modules to do code-first ASN1, which kind of misses the point).

It's use of JSON as a wire format is a relatively recent addition to the standard, but some of the commercial tools support it. For those who really need it, it can be a very useful tool.

From the point of view of this particular question, ASN.1 and the tools that support JSON as a wire format is not useful; you cannot take arbitrary JSON and automatically generate a schema that can be compiled to classes. Where it is useful is in a fresh project, where you want to make it easy for other languages / platforms to consume or generate your data.

I have looked for decent C# class generators that consume JSON schemas; unfortunately the best one out there didn't deal with oneof. However the tools I'm using for ASN.1 (which has the equivalent CHOICE) do generate complete classes in C#, Java, C/C++. So I'm in this amusing situation where I have an ASN.1 schema that I compile (in this particular project) to C# and C, and it deals with JSON, XML, and the binary wire formats. The generated classes are smart enough to do their own validation - I don't have to pass the JSON through a JSON schema validator.

JSON schemas and ASN.1 schemas are broadly comparable in terms of the detail one can put into a specification. Similarly ASN.1 schema and XSD XML schema are broadly equivalent (there's even an official standardised translation between the two languages). The benefits I've seen of using ASN.1 schema instead of JSON or XSD schema is that the tools (especially the commercial tools) seem to be significantly more thorough than the class generators typically associated with JSON and XSD schemas (e.g. Microsoft's xsd.exe sucks). This has had a knock-on positive benefit for systems integration, maintenance, being agile with data definitions, etc.

Upvotes: 0

BeUndead
BeUndead

Reputation: 3618

If all of your random-value-x JsonObjects have the same format (i.e. the two constant fields are the same for each), then you could always have something akin to:

class RandomValue {
    private final String constantField00;
    private final String constantField01;
    // ... Constructors, getters, etc.
}

class Pojo {
    private final String name;
    private final Map<String, RandomValue> randomValues;
    // ...
}

If they're ordered (i.e. they're all the same random-value, like property-01, property-02, etc.) then you could also have the Map be a List (or Set, etc.) of your RandomValue elements.


If, on the other hand, the constant-fields are all random keys as well, then you're probably stuck with something more like:

class Pojo {
    private final String name;
    private final Map<String, Map<String, String>> additionalInfo;
    // ...
}

Where the keys to the additionalInfo Map are your random-value-xs, and the values are a Map of String keys (constant-field-0xs) to String values (values).

Upvotes: 1

Related Questions