JSON Print Unit Testing

Is there an easy way

  • to print an annotated JAXB instance
  • in JSON format (since it is Jersey that will print to AS)
  • when testing modules
  • without starting the embedded server?

I can run and see in XML using JAXBContext, Marshaller, etc.

Is there any example for JSON?


I found him. This is similar to JAXB.

With Jersey API

final JSONJAXBContext context = new JSONJAXBContext(...);

final JSONMarshaller marshaller = context.createJSONMarshaller();
marshaller.marshallToJSON(...);

final JSONUnmarshaller unmarshaller = context.createJSONUnmarshaller();
final T unmarshalled = unmarshaller.unmarshalJAXBElementFromJSON(
        ..., T.class).getValue();

for maven

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-json</artifactId>
  <scope>test</scope>
</dependency>
+5
source share
1 answer

Maybe:

...
StringWriter writer = new StringWriter();
marshaller.marshallToJSON(objectToMarshall, writer)
logger.debug(writer.toString());
...

You can use StringWriterto accept the view Stringfor the displayed JSON output.

+2
source

All Articles