Json { @Nonnull public static String toJson(@Nonnull final Object obj) { assert obj != null; return toMapper(OBJECT_MAPPER, obj); } private  Json(); @Nonnull static ObjectMapper getCurrentJsonObjectMapper(); @Nonnull static ObjectMapper getCurrentStringObjectMapper(); @Nonnull static String toJson(@Nonnull final Object obj); @Nullable static T fromJson(@Nonnull final String json, @Nonnull final Class<T> type); @Nonnull static String toStringJson(@Nullable final Object obj);  }
@Test public void testJsonEnumMap() throws Exception { LOG.info("testJsonEnumMap"); final Map<MyEnumType, Integer> x = new EnumMap<>(MyEnumType.class); x.put(MyEnumType.A, 100); x.put(MyEnumType.B, 200); x.put(MyEnumType.C, 300); x.put(MyEnumType.D, 400); final String s = Json.toJson(x); LOG.info("x.toJson = {}", s); Assert.assertEquals( "{\"A\":100,\"B\":200,\"C\":300,\"D\":400}", s); }
@Test public void testJsonArray() throws Exception { LOG.info("testJsonArray"); final int[] x = {1, 2, 3}; final String s1 = Json.toJson(x); LOG.info("x.toJson = {}", s1); Assert.assertEquals("[1,2,3]", s1); final ArrayList<Integer> y = new ArrayList<>(); y.add(10); y.add(20); y.add(30); final String s2 = Json.toJson(y); LOG.info("y.toJson = {}", s2); Assert.assertEquals("[10,20,30]", s2); final List<List<Integer>> z = new ArrayList<>(); final List<Integer> a = new ArrayList<>(); a.add(10); a.add(20); a.add(30); final List<Integer> b = new ArrayList<>(); b.add(100); final List<Integer> c = Collections. EMPTY_LIST; z.add(a); z.add(b); z.add(c); final String s3 = Json.toJson(z); LOG.info("z.toJson = {}", s3); Assert.assertEquals("[[10,20,30],[100],[]]", s3); }
@Test public void testJsonJaxbAwareAbstract() throws Exception { LOG.info("testJsonJaxbAware"); final SomeConcrete x = new SomeConcrete(1, "x"); final String s = Json.toJson(x); LOG.info("x.toJson = {}", s); Assert.assertEquals( "{\"CONCRETE\":{\"value\":1,\"name\":\"x\"}}", s); }
@Test public void testToJsonAbstract() { LOG.info("testToJsonAbstract"); final SomeConcrete x = new SomeConcrete(1, "x"); final String s = Json.toJson(x); LOG.info("x.toJson = {}", s); Assert.assertEquals( "{\"CONCRETE\":{\"value\":1,\"name\":\"x\"}}", s); }
@Test public void testToJsonRegularCases() throws ValidationFailException { LOG.info("testToJsonRegularCases"); ConstructorChecker.validateMethod(Json.class, "toJson", INT_JSON); final Integer i = 1; final String x0 = Json.toJson(i); LOG.info("Json Integer: " + x0); Assert.assertEquals(INT_JSON, x0); final GeoRectangle a = new GeoRectangle(new GeoPoint(1.0, 2.0), new GeoPoint(3.0, 4.0)); final String x1 = Json.toJson(a); LOG.info("Json GeoRectangle: " + x1); Assert.assertEquals(RECT_JSON, x1); final Uid<Integer> uid = new Uid<>("1-2-3-4-5"); final String x2 = Json.toJson(uid); LOG.info("Json Uid: " + x2); Assert.assertEquals( "{\"uuid\":\"00000001-0002-0003-0004-000000000005\"}", x2); final Date d = new Date(Date.UTC(112, 4, 27, 11, 29, 10)); final String x3 = Json.toJson(d); LOG.info("Json Date: " + x3); Assert.assertEquals( "\"2012-05-27T11:29:10.000+00:00\"", x3); final DateTime e = new DateTime(2012, 4, 27, 11, 29, 10, DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/Amsterdam"))); final String x4 = Json.toJson(e); LOG.info("Json DateTime: " + x4); Assert.assertEquals( "\"2012-04-27T11:29:10.000+0200\"", x4); }