Package com.github.nmorel.gwtjackson.client.stream

Examples of com.github.nmorel.gwtjackson.client.stream.JsonWriter


        return write( value, new JsonSerializationContext.Builder().build() );
    }

    @Override
    public String write( T value, JsonSerializationContext ctx ) throws JsonSerializationException {
        JsonWriter writer = ctx.newJsonWriter();
        try {
            if ( ctx.isWrapRootValue() ) {
                writer.beginObject();
                writer.name( rootName );
                getSerializer().serialize( writer, value, ctx );
                writer.endObject();
            } else {
                getSerializer().serialize( writer, value, ctx );
            }
            return writer.getOutput();
        } catch ( JsonSerializationException e ) {
            // already logged, we just throw it
            throw e;
        } catch ( RuntimeException e ) {
            throw ctx.traceError( value, e, writer );
View Full Code Here


    public boolean isWriteSingleElemArraysUnwrapped() {
        return writeSingleElemArraysUnwrapped;
    }

    public JsonWriter newJsonWriter() {
        JsonWriter writer = new FastJsonWriter( new StringBuilder() );
        writer.setLenient( true );
        writer.setSerializeNulls( serializeNulls );
        if ( indent ) {
            writer.setIndent( "  " );
        }
        return writer;
    }
View Full Code Here

        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

        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.stream.JsonWriter

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.