Is it possible to map a JSON object to a string with Moxy?

Suppose I have a data model, for example:

public class MyModel { private String someString; private String someJson; // Data structure owned by client, persisted as a CLOB } 

I serve the model through the REST API (Jersey) for the client. I know that I could marshal / untie this with something like:

 { "someStrong": "foo", "someJson": "{ someClientThing: \"bar\", someOtherClientThing: \"baz\"}" } 

But I'm looking for something cleaner. Is there a way that I can marshal / unmarshal that for JSON like this?

 { someStrong: "foo", someJson: { someClientThing: "bar", someOtherClientThing: "baz" } } 

I do not want the server to be aware of the data model for someJson , since it belonged to the client. I just want the server to handle persistence - so the server will pass it back and forth between the client and the database.

Note. . It is not necessary to directly match a string with an integer if it can display something unstructured (not statically defined on the server), which can be compressed to persistence (and unstructured back to this unstructured object when searching).

+5
source share
3 answers

Maybe if json has no arrays, as in your example:

 { "someClientThing": "bar", "someOtherClientThing": "baz"} 

In such a simple case, the solution is an implementation of the json-string โ†” org.w3c.dom.Document bidirectional conversion example in its own DomHandler. Attach a handler to the field:

 @XmlRootElement @XmlAccessorType(FIELD) public class MyModel { private String someString; @XmlAnyElement(value = SomeJsonHandler.class) private String someJson; // Data structure owned by client, persisted as a CLOB } 

Enjoy.

Unfortunately, there is a big problem with arrays because XML Dom does not support them. After reconverting below json

 { "someClientThing": "bar", "someOtherClientThing": ["baz1","baz2"]} 

you will get something like this

 { "someClientThing": "bar", "someOtherClientThing": {value="baz1"}, "someOtherClientThing": {value="baz2"} } 
+2
source

Try it, maybe it will help you

 public class MyModel { private String someString; private Map<String, Object> someJson; } 
+1
source

The solution to the above is simple. Instead of making SomeJson a String data type. You can use a custom data type that includes data items.

 public class MyModel { private String someString; private SomeJson someJson; } public class SomeJson { private String someClientThing; private String someOtherClientThing; // Data structure owned by client, persisted as a CLOB } 

The above solution will help you.

-1
source

All Articles