JLLMNCHR
JLLMNCHR

Reputation: 1591

Modeling JSON in Java

The few times I've worked with Java/Rest/JSon, JSON elements have always been built in camelCase format.

For example:

"someField": {
    "someSonField1": "20191106",
    "someSonField2": "20201119",
    ...
    }

However, in a functional document they have passed me to build a Rest JSon client, they use this notation:

"some_field": {
    "some_son_field_1": "20191106",
    "some_son_field_2": "20201119",
    ...
    }

Is it expressed somewhere that Java has to use the notation 1?.

It seems to me that if it is done this way, everything goes much more smoothly when modeling the objects:

@XmlRootElement(name = "someField")
@XmlType(propOrder = {"someSonField1", "someSonField2"})
public class someField {

    private String someSonField1; 
    private String someSonField2; 

    //...
}

Thanks!

Upvotes: 0

Views: 87

Answers (3)

Deepak Arora
Deepak Arora

Reputation: 61

Or you could use unify-jdocs, a library which I created to read and write JSON documents without using any POJO objects. Rather than defining POJO objects, which we know can be difficult to manage in case of complex documents and changes to the JSON document, just don't use them at all. Directly read and write paths in the JSON document. For example, in your snippet, you could read and write the fields as:

Document d = new JDocument(s); // where s is a JSON string
String s1 = d.getString("$.some_field.some_son_field_1");
String s2 = d.getString("$.some_field.some_son_field_2");

You could use a similar way to write to these paths as so:

d.setString("$.some_field.some_son_field_1", "val1");
d.setString("$.some_field.some_son_field_2", "val2");

This library offers a whole lot of other features which can be used to manipulate JSON documents. Features like model documents which lock the structure of documents to a template, field level validations, comparisons, merging documents etc.

Obviously, you would consider it only if you wanted to work without POJO objects. Alternatively, you could use it to read and write a POJO object using your own method.

Check it out on https://github.com/americanexpress/unify-jdocs.

Upvotes: 1

paulsm4
paulsm4

Reputation: 121871

Q: Is it expressed somewhere that Java has to use the notation?

A: No: it's 100% "convention", not mandatory.

As it happens, the standard convention for both JSON (a creature of Javascript) and Java is camelcase. For example: Java Naming Conventions.

some_son_field_1 is an example of snake case. It's associated with classic "C" programs. It's also common (but NOT universal) with XML. It, too, is a "convention" - not a requirement.

I'm curious why you're choosing XML bindings for JSON data. Have you considered using Jackson?

Finally, this article might be of interest to you:

5 Basic REST API Design Guidelines

Upvotes: 1

madhead
madhead

Reputation: 33511

I see you're using javax.xml.bind package? Have you tried @XmlElement?

@XmlRootElement(name = "someField")
@XmlType(propOrder = {"some_son_field_1", "some_son_field_2"})
public class someField {
    @XmlElement(name="some_son_field_1")
    private String someSonField1;

    @XmlElement(name="some_son_field_2") 
    private String someSonField2; 

    //...
}

Not sure, probably you should try putting them on getters, as your fields are private.

Upvotes: 1

Related Questions