Skip to content

Added support for JSON-B - #1385

Merged
joelittlejohn merged 5 commits into
joelittlejohn:masterfrom
TheTrueDentist:jsonb_support
Jun 5, 2022
Merged

joelittlejohn merged 5 commits into
joelittlejohn:masterfrom
TheTrueDentist:jsonb_support

Conversation

@TheTrueDentist

Copy link
Copy Markdown
Contributor

My suggestion for JSON-B-support. I have provided two annotators for JSON-B 1 and 2. They differ only in the package of the annotations though. Alternatively, one could provide a baseclass, too, and let the two extending classes only provide the annotation classes. Thiw, however, would lead to a close coupleing of both versions. Another alternative would be to have the subversion be specified by the configuration of the plugin and provide one class that handles both versions by respecting the configuration. Thought I leave that decision up to you.

@winterkid

Copy link
Copy Markdown

Excuse my impatience. Is there any chance that this pull request will be confirmed/merged soon? I can't wait bringing JSON-B POJO generation into action.

@joelittlejohn

Copy link
Copy Markdown
Owner

Do we actually need to support JSONB1 at this point or is it already effectively obsolete?

If we support the annotator it needs to be changed and supported over time. Makes me think that maybe we should just support JSONB (which means JSONB 2).

@TheTrueDentist

Copy link
Copy Markdown
Contributor Author

I see your point and would wish I could say JSON-B 2 would be enough. Unfortunately, JSON-B 2 seems to be supported by Jakarta EE 9 on only. Jakarta EE 9 in turn is supported by Payara 6 on, which is not even available at this point. So, I don't think we come around JSON-B 1 at this point.
As the only difference in the implementation ist the package name, one could imagine a config parameter to override the package name, but that would seem be rather hacky, I'm afraid.

@joelittlejohn

Copy link
Copy Markdown
Owner

Makes sense @TheTrueDentist, thanks!

import java.util.Iterator;

/**
* Annotates generated Java types using the Jackson 2.x mapping annotations.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a copy/paste from Jackson annotator

@TheTrueDentist TheTrueDentist Apr 6, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I based my work on the Jackson annotator as the annotations and handling are quite similar, although not entirely the same. At the end, i copied JSON-B-annotator v1 for v2. This ways, I forgot to fix the comments and then copied the errors from one version to the other, sorry. I guess the iterator could also be improved by using for-each.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the comments in both files. Iterator is fine in this case IMO.

import jakarta.json.bind.annotation.JsonbTransient;

/**
* Annotates generated Java types using the Jackson 2.x mapping annotations.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like copy/paste from Jackson annotator

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

@joelittlejohn

joelittlejohn commented Apr 6, 2022 •

Copy link
Copy Markdown
Owner

I think there's a couple of integration tests missing here, to actually check whether the way these types are annotated is correct. Do the types we produce work correctly with JSON-B, to bind JSON data that is valid according to the schema?

If you check out the GsonIT and MoshiIT, you can see a round trip test that shows you can parse a JSON file and bind the data to some types, and that you can produce the same JSON using the values. You can use the same two JSON snippets. So the tests would look like:

    @Test
    public void annotationStyleJsonb1MakesTypesThatWorkWithMoshi1() throws ClassNotFoundException, SecurityException, IOException {

        ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/json/examples/", "com.example",
                config("annotationStyle", "jsonb1",
                        "propertyWordDelimiters", "_",
                        "sourceType", "json",
                        "useLongIntegers", true));

        assertJsonRoundTrip(resultsClassLoader, "com.example.Torrent", "/json/examples/torrent.json");
        assertJsonRoundTrip(resultsClassLoader, "com.example.GetUserData", "/json/examples/GetUserData.json");
    }

    @Test
    public void annotationStyleJsonb2MakesTypesThatWorkWithMoshi1() throws ClassNotFoundException, SecurityException, IOException {

        ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/json/examples/", "com.example",
                config("annotationStyle", "jsonb2",
                        "propertyWordDelimiters", "_",
                        "sourceType", "json",
                        "useLongIntegers", true));

        assertJsonRoundTrip(resultsClassLoader, "com.example.Torrent", "/json/examples/torrent.json");
        assertJsonRoundTrip(resultsClassLoader, "com.example.GetUserData", "/json/examples/GetUserData.json");
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    private void assertJsonRoundTrip(ClassLoader resultsClassLoader, String className, String jsonResource) throws ClassNotFoundException, IOException {
        Class generatedType = resultsClassLoader.loadClass(className);

        String expectedJson = IOUtils.toString(getClass().getResource(jsonResource));
        JsonAdapter<Object> jsonAdapter = moshi.adapter(generatedType);
        Object javaInstance = jsonAdapter.fromJson(expectedJson);
        String actualJson = jsonAdapter.toJson(javaInstance);

        assertEqualsJson(expectedJson, actualJson);
    }

@TheTrueDentist

Copy link
Copy Markdown
Contributor Author

I have provided ITs as suggested. Note that I have splitted the test class into two, one per version of JSON-B to decrease confusion. Please also note, that I had to use two different JSON-B implementations. This is because I would have Yasson 1 for JSON-B 1 and Yasson 2 for JSON-B 2 (Yasson 2 is seemingly not backwards compatible). When I tried to set that up, things did not work, wrong version of yasson was used. So I tricked it out and used Johnzon for JSON-B 1.

@winterkid

Copy link
Copy Markdown

So, everythings fine now?

@TheTrueDentist

Copy link
Copy Markdown
Contributor Author

I have noticed that you have published version 1.1.2 of the codegenerator. I have merged master into the branch of this pull request, so everything should be up-to-date again. Please let me know if there is something I can do to speed up things.

@TheTrueDentist

Copy link
Copy Markdown
Contributor Author

There seems to be a status report required about test result on Java 8 under Ubuntu 18.04 before this can be pulled. I have no Ubuntu 18.04 at hand, but under 20.04 with Java 8 tests are all green. Of course, if Ubuntu 18.04 is the reference here, tests should be run there as well. Also, I see that you want to check the results yourself. Still, the setup I have used makes me confident to state: I would not expect any problems under Ubuntu 18.04 with Java 8.

@joelittlejohn

Copy link
Copy Markdown
Owner

@TheTrueDentist

Copy link
Copy Markdown
Contributor Author

You're right, of course. Documentation should be fixed now.

@joelittlejohn
joelittlejohn merged commit 8b013e7 into joelittlejohn:master Jun 5, 2022
@joelittlejohn joelittlejohn added this to the 1.1.3 milestone Jun 5, 2022
andponlin pushed a commit to andponlin/jsonschema2pojo that referenced this pull request Jul 30, 2022
* Added support for JSON-B

* Fxied comments

* Improved on ITs

* Fixed documentation

Co-authored-by: Daniel Kepes <daniel.kepes@ottogroup.com>
@winterkid

Copy link
Copy Markdown

Is milestone 1.1.3 about to be released this year?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants