jdereg / json-io

Convert Java to JSON. Convert JSON to Java. Pretty print JSON. Java JSON serializer. Deep copy Java object graphs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: Replace deep objects

Tawoka opened this issue · comments

Greetings,

I am currently testing json-io as a JSON-parser, since I am very fond of the simplistic setup requirements. Unfortunately I have a "small" requirement, for which I can't seem to find a working solution. Therefore I'd like to ask here directly, if this is possible, and how.

I have following setup. I have a deep object model with multiple layers. As example I will use a simple Table:
Table is the main class, holding a List of Lines.
Line is a second layer, each holding a List of Fields.
Field is the lowest layer, simply holding some attributes like "value"

The standard parsing of this model thus far seems to be working fine. What I would like to have now is the option of a "flat" json. If I parse a "flat table" I want that the JSON holds an array of strings in the "line" property. These strings are the IDs of the original lines.

I know that there is a custom writer option, but in this example I would have to create two custom writers: One for "flat table", and one for "flat line". In the real model this would currently be 17 custom writers, and subsequently 17 custom readers for the other direction. In all 17 cases I have a common interface - let's call it "Element" - where the getId() method is found.

What I was now searching for, was a way to say for each Element in the parser, do not parse the object itself, but parse Element.getId() instead.
For the other direction, do not fill the string but fill the list with results from getElementById()

The names of those "to be replaced" fields are also known at development time, and are all or nothing (so no complex control blocks are required).

Is there a way to accomplish this?

It rebuilds the object as it is. I have not yet been able to test highly complex setups with it, but in a simple test with a couple of mock objects it's reading and writing as I'd expect it.

To make sure I didn't give the wrong signals here, I'd like to emphasize that I had no problem so far with reading or writing a JSON on an "AS-IS" basis. The thing I miss is an ability to enhance both reader and writer (especially writer) with the ability to replace an object with its ID string.
So my question to that extend is still open

I don't fully understand what is being requested. json-io writes out an object id if the object is referenced two (2) or more times. And it only writes the reference for the 2nd and further references. This prevents cycles and ensures that shape of the graph is the same after being written and read.

If you are wanting a special format, you can (as you suggest) add custom reader and writers. If you find that is needs many (as you described above), perhaps you can create a custom reader/writer at a higher level, where that one custom reader/writer takes care of it's world, so-to-speak.

Custom Readers and Writers can be added to read/write objects that do not serialize automatically. In addition, objects that are hard to instantiate (no easy constructor to call by the reader code), you can use the "assign instantiator" method to ensure that your code can be used to instantiate an object that is tough to construct. See the UserGuide, linked to from the main page: https://github.com/jdereg/json-io/blob/master/user-guide.md and look through the "Customization" section for other options.