private JsonSerializationContext createSerializationContext() {
return new JsonSerializationContext.Builder().writeSingleElemArraysUnwrapped( true ).build();
}
public void testSerialize() {
JsonSerializationContext context = createSerializationContext();
// Lists:
ArrayList<String> strs = new ArrayList<String>();
strs.add( "xyz" );
JsonWriter writer = context.newJsonWriter();
IterableJsonSerializer.newInstance( StringJsonSerializer.getInstance() ).serialize( writer, strs, context );
assertEquals( ("\"xyz\""), writer.getOutput() );
ArrayList<Integer> ints = new ArrayList<Integer>();
ints.add( 13 );
writer = context.newJsonWriter();
IterableJsonSerializer.newInstance( IntegerJsonSerializer.getInstance() ).serialize( writer, ints, context );
assertEquals( "13", writer.getOutput() );
// other Collections, like Sets:
HashSet<Long> longs = new HashSet<Long>();
longs.add( 42L );
writer = context.newJsonWriter();
IterableJsonSerializer.newInstance( LongJsonSerializer.getInstance() ).serialize( writer, longs, context );
assertEquals( "42", writer.getOutput() );
// [Issue#180]
final String EXP_STRINGS = "{\"values\":\"foo\"}";
assertEquals( EXP_STRINGS, StringListBeanWriter.INSTANCE.write( new StringListBean( Collections
.singletonList( "foo" ) ), createSerializationContext() ) );
final Set<String> SET = new HashSet<String>();
SET.add( "foo" );
assertEquals( EXP_STRINGS, StringListBeanWriter.INSTANCE.write( new StringListBean( SET ), createSerializationContext() ) );
// arrays:
writer = context.newJsonWriter();
PrimitiveBooleanArrayJsonSerializer.getInstance().serialize( writer, new boolean[]{true}, context );
assertEquals( "true", writer.getOutput() );
writer = context.newJsonWriter();
ArrayJsonSerializer.newInstance( BooleanJsonSerializer.getInstance() ).serialize( writer, new Boolean[]{Boolean.TRUE}, context );
assertEquals( "true", writer.getOutput() );
writer = context.newJsonWriter();
PrimitiveIntegerArrayJsonSerializer.getInstance().serialize( writer, new int[]{3}, context );
assertEquals( "3", writer.getOutput() );
writer = context.newJsonWriter();
ArrayJsonSerializer.newInstance( StringJsonSerializer.getInstance() ).serialize( writer, new String[]{"foo"}, context );
assertEquals( "\"foo\"", writer.getOutput() );
}