Package com.github.nmorel.gwtjackson.client

Examples of com.github.nmorel.gwtjackson.client.JsonSerializationContext$Builder


    public void testSerializeNullValue() {
        assertSerialization( "null", null );
    }

    protected String serialize( T value ) {
        JsonSerializationContext ctx = new JsonSerializationContext.Builder().build();
        JsonWriter writer = ctx.newJsonWriter();
        createSerializer().serialize( writer, value, ctx );
        return writer.getOutput();
    }
View Full Code Here


    public void testSerializeNullValue() {
        assertSerialization( null, null );
    }

    protected String serialize( T value ) {
        JsonSerializationContext ctx = new JsonSerializationContext.Builder().build();
        return createSerializer().serialize( value, ctx );
    }
View Full Code Here

    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() );
    }
View Full Code Here

TOP

Related Classes of com.github.nmorel.gwtjackson.client.JsonSerializationContext$Builder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.