Jump to: navigation, search


MOXy support for for Java API for JSON Processing (JSR-353)

Bug 405161

Document History

Date Author Version Description & Notes
25.11.2015 Iaroslav Savytskyi Initial revision


As part of Java™ EE 7 was introduced Java™ API for JSON Processing (JSR-353). It is a standard for generating and processing JSON. Till now MOXy was using it's own JSON processor for this purpose. It's time for changes. Providing support for JSR-353 MOXy allows customers to chose the JSON parser to be used based on their requirements (performance, memory, etc.). By default MOXy will use default implementation from https://jsonp.java.net.


Unmarshal from javax.json.stream.JsonParser

Parsing JSON from stream (StAX) : Note: Originally MOXy was created as JAXB implementation. And later JSON support was added. So unmarshalling JSON from the stream is not an easy task. Currently we are facing some problems with this and working on solution. As workaround we are parsing the whole JSON document and only after that processing it.

   try (
       InputStream inputStream = .....;
       JsonParser parser = Json.createParser(inputStream)
   ) {
       JsonParserSource source = new JsonParserSource(parser);
       Foo foo = context.getUnmarshaller().unmarshal(source, Foo.class);

Unmarshal from javax.json.JsonStructure

Parsing JSON from object (DOM) :

   try (
       InputStream inputStream = .....;
       JsonReader reader = Json.createReader(inputStream)
   ) {
       JsonStructureSource source = new JsonStructureSource(reader.read());
       Foo foo = context.getUnmarshaller().unmarshal(source, Foo.class);


Marshal to javax.json.JsonObjectBuilder/JsonArrayBuilder

   JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
   JsonArrayBuilderResult result = new JsonArrayBuilderResult(arrayBuilder);
   Foo foo = new Foo();
   context.getMarshaller().marshal(foo, result);

For the JsonObjectBuilder:

   JsonObjectBuilderResult result = new JsonObjectBuilderResult();

Marshal to javax.json.stream.JsonGenerator

       OutputStream os = ....;
       JsonGenerator jsonGenerator = Json.createGenerator(os)
   ) {
       JsonGeneratorResult result = new JsonGeneratorResult(jsonGenerator);
       context.getMarshaller().marshal(new Foo(), result);