Unify-jdocs: A New Way of Working With JSON Documents in Java
Unify-jdocs is a Java library that I have created where you can work with JSON documents without using model classes, do away with JSON schema for validation, and more.
Join the DZone community and get the full member experience.
Join For FreeUnify-jdocs is a new way of working with JSON documents without using model classes or JSON schemas. Read more about it on https://github.com/americanexpress/unify-jdocs.
Unify-jdocs is a Java library that I have created as part of working on a new technology platform in American Express. You can work with JSON documents without using model classes. You can also do away with JSON schema for validation, as unify-jdocs provides a much simpler way for structure adherence and content validations. You can read AND write any JSON path including elements of an array nested any levels deep in a single line of code. There are other features like being able to merge documents, reuse model documents, and compare documents.
The background behind writing this library is that we have a large number of JSON document types and many of them are quite complex in terms of structure (many paths and many nested levels). I did not want to create POJO/model classes and write read and write accessor methods for manipulating data in these documents as these can be quite challenging to maintain in the face of changing document structures. I also did not want to use JSON schema validations as they tend to get quite verbose, do not mirror the JSON data document structure, and can be unwieldy to use.
What I did want was the ability to:
- Read and write any JSON path in a document in a single line of code.
- Know metadata information (like the number of elements in an array, index of a specific element etc.) in a single line of code
- Do fast, accurate, and exhaustive impact analysis on the usage of JSON paths over the whole code base so as to be able to refactor quickly in case positions of JSON paths change in the document structure.
- Conform to predefined document structures and specify field validations which would automatically get done in the background.
This library does all this and more.
Before starting work on this library, I did look around to see if something similar existed. I did find some which could be used to read and write simple JSON paths but could not handle the complex scenarios that we wanted to address. I am happy to report that the use of this library in our project has made working with JSON documents an order of magnitude easier, reduced boilerplate lines of code by nearly 90% (primarily due to no POJO/model classes, accessor methods and validations), and made it extremely easy to refactor code in case JSON paths are updated in the document. For reference, we have over 250 JSON document types, more than 3000 JSON paths nested more than 10 levels deep in multiple documents, which we have been successfully able to manage using this library.
I hope this work will help many who have experienced working with POJO/model classes and JSON schema validations to be far too tedious and challenging to maintain in the long run. I look forward to comments and feedback from the community. Read more about it on https://github.com/americanexpress/unify-jdocs.
Please feel free to follow me @DeepakAroraHi on Twitter and let me know what you think.
Disclaimer: #ViewsMyOwn.
Published at DZone with permission of Deepak Arora. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments