@Override public byte[] writeValueAsBytes( Object value, Charset charset ) { return serializerFactory.create().serialize( value ).toString().getBytes( charset ); }
private String toJson() { if (jsonSerializerThreadLocal.get() == null) { jsonSerializerThreadLocal.set(new JsonSerializerFactory().create()); } return jsonSerializerThreadLocal.get().serialize(this).toString(); }
private String toJson() { if (jsonSerializerThreadLocal.get() == null) { jsonSerializerThreadLocal.set(new JsonSerializerFactory().create()); } return jsonSerializerThreadLocal.get().serialize(this).toString(); }
@Override public void writeValue( Writer dest, Object value ) { char [] chars = serializerFactory.create().serialize( value ).toCharArray(); try { dest.write( chars ); } catch ( IOException e ) { Exceptions.handle( e ); } }
@Override public void writeValue( Writer dest, Object value ) { char [] chars = serializerFactory.create().serialize( value ).toCharArray(); try { dest.write( chars ); } catch ( IOException e ) { Exceptions.handle( e ); } }
@Override public void writeValue( OutputStream dest, Object value ) { IO.writeNoClose( dest, serializerFactory.create().serialize( value ).toString() ); }
@Override public void writeValue( File dest, Object value ) { IO.write( IO.path( dest.toString() ), serializerFactory.create().serialize( value ).toString()); }
@Override public void writeValue( OutputStream dest, Object value ) { IO.writeNoClose( dest, serializerFactory.create().serialize( value ).toString() ); }
@Test public void test2() { final JsonSerializerFactory jsonSerializerFactory = new JsonSerializerFactory(); final JsonSerializer jsonSerializer = jsonSerializerFactory.setSerializeMapKeys(true).setEncodeStrings(true).create(); HashMap<String, Object> data = new HashMap<>(); data.put("\"hi\"", Double.parseDouble("1.2312312")); assertEquals("{\"\\\"hi\\\"\":1.2312312}", jsonSerializer.serialize(data).toString()); } }
@Test public void serializeIntArray() { JsonSerializer serializer = new JsonSerializerFactory().create(); serializer.serialize( new int[]{ 0, 1, 2, 3, 4, 5 } ); }
private static JsonSerializer createSerializer() { JsonSerializerFactory factory = new JsonSerializerFactory() .addTypeSerializer(HashCode.class, new HashCodeSerializer() ); return factory.create(); }
public static String toJson(Object obj) { JsonSerializerFactory jsonSerializerFactory = new JsonSerializerFactory() .addTypeSerializer(GStringImpl.class, new AbstractCustomObjectSerializer(GStringImpl.class) { @Override public void serializeObject(JsonSerializerInternal serializer, Object instance, CharBuf builder) { builder.addString(((GString) instance).toString()); } }); JsonSerializer serializer = jsonSerializerFactory.create(); return serializer.serialize(obj).toString(); } }
/** * @author javamonkey79 * https://github.com/boonproject/boon/issues/355 */ @Test public void includeEmptyString() { Cat mittens = new Cat("mittens", "calico", ""); Assert.assertEquals("{\"name\":\"mittens\",\"type\":\"calico\",\"sex\":\"\"}", String.valueOf(new JsonSerializerFactory().setIncludeEmpty(true).create().serialize(mittens))); }
@Test public void test() { JsonSerializerFactory factory = new JsonSerializerFactory(); final JsonSerializer jsonSerializer = factory.includeNulls().create(); final String json = jsonSerializer.serialize( Maps.map("job", "programmer", "age", null, "showSize", 12) ).toString(); ok |= json.contains("\"age\":null") || die(); } }
@Test public void testWithAcii() { Employee aÉddié = new Employee(); aÉddié.name = "Éddié"; String sRick = new JsonSerializerFactory().asciiOnly().create().serialize(aÉddié).toString(); puts(sRick); ok = sRick.equals( "{\"name\":\"\\u00c9ddi\\u00e9\",\"url\":\"http://foo.bar/foo.jpg\",\"dob\":328147200000,\"currency\":\"USD\",\"salary\":100000.00}" ) || die( sRick ); Employee bÉddié = new JsonParserFactory ().create ().parse ( Employee.class, sRick ); bÉddié.name.equals(aÉddié.name); }
@Test public void testWithType() { Employee rick = new Employee(); String sRick = new JsonSerializerFactory().useFieldsFirst().setOutputType( true ).create() .serialize( rick ).toString(); puts (sRick); boolean ok = sRick.equals( "{\"class\":\"org.boon.json.JsonSerializeTest$Employee\",\"name\":\"Rick\",\"url\":\"http://foo.bar/foo.jpg\",\"dob\":328147200000,\"currency\":\"USD\",\"salary\":100000.00}" ) || die( sRick ); }
@Test public void testControlCharEncoding() { Employee rick = new Employee(); rick.setName("\u001d\u001eRick"); String sRick = new JsonSerializerFactory().useFieldsFirst().setOutputType( true ).create() .serialize( rick ).toString(); puts (sRick); ok |= sRick.contains("name\":\"\\u001d\\u001eRick"); }
@Test public void bug3() { JsonSerializer serializer = new JsonSerializerFactory().create (); String sMedium = serializer.serialize( MEDIUM_DATA ).toString(); outputs( sMedium ); Map<String, Object> map = new JsonParserFactory ().create ().parseMap ( sMedium ); map = ( Map<String, Object> ) map.get("photo"); outputs( "url", map.get( "url" ) ); String str = serializer.serialize( COMPLEX_DATA ).toString(); outputs( str ); }
@Test public void testListSimple() { JsonSerializer jsonSerializer = new JsonSerializerFactory().create(); final String json = jsonSerializer.serialize(Lists.list(1, 2, null, 3)).toString(); assertNotNull(json); assertEquals("[1,2,null,3]", json); }
@Test public void testArraySimple() { JsonSerializer jsonSerializer = new JsonSerializerFactory().create(); final String json = jsonSerializer.serialize(Arry.array(1, 2, null, 3)).toString(); assertNotNull(json); assertEquals("[1,2,null,3]", json); } }