private void tryIndividualsWithConfiguration(JSONConfiguration configuration) throws Exception { final JSONJAXBContext ctx = new JSONJAXBContext(configuration, AnimalList.class, Animal.class, Dog.class, Cat.class); final JSONMarshaller jm = ctx.createJSONMarshaller(); final JSONUnmarshaller ju = ctx.createJSONUnmarshaller(); Animal animalTwo; for (int i = 0; i < one.animals.size(); i++) { final StringWriter sw = new StringWriter(); Animal animalOne = one.animals.get(i); jm.marshallToJSON(animalOne, sw); System.out.println(String.format("Marshalled: %s", sw)); animalTwo = ju.unmarshalFromJSON(new StringReader(sw.toString()), Animal.class); assertEquals(animalOne, animalTwo); System.out.println( String.format( "class one = %s; class two = %s", animalOne.getClass(), animalTwo.getClass())); assertEquals(animalOne.getClass(), animalTwo.getClass()); } }
/** * @param s the JSON representation of the filter * @return the filter * @throws Exception */ public static Filter buildFilter(String s) throws Exception { JSONJAXBContext context = new JSONJAXBContext(JSONConfiguration.natural().build(), FilterModel.class); JSONUnmarshaller unmarshaller = context.createJSONUnmarshaller(); FilterModel model = unmarshaller.unmarshalFromJSON(new StringReader(s), FilterModel.class); return model.build(); }
/** * @param filter the filter * @return the JSON representation of the filter * @throws Exception */ public static String stringifyFilter(final Filter filter) throws Exception { JSONJAXBContext context = new JSONJAXBContext(JSONConfiguration.natural().build(), FilterModel.class); JSONMarshaller marshaller = context.createJSONMarshaller(); StringWriter writer = new StringWriter(); marshaller.marshallToJSON(new FilterModel(filter), writer); return writer.toString(); }
public void testNewLineAdded() throws Exception { final JSONJAXBContext ctx = new JSONJAXBContext( JSONConfiguration.natural().rootUnwrapping(false).humanReadableFormatting(true).build(), User.class); final JSONMarshaller jm = ctx.createJSONMarshaller(); final StringWriter sw = new StringWriter(); final User one = JSONTestHelper.createTestInstance(User.class); jm.marshallToJSON(one, sw); assertTrue(sw.toString().contains("\n")); }