All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.nmorel.gwtjackson.client.ser.IterableJsonSerializer Maven / Gradle / Ivy

Go to download

gwt-jackson is a GWT JSON serializer/deserializer mechanism based on Jackson annotations

There is a newer version: 0.15.4
Show newest version
package com.github.nmorel.gwtjackson.client.ser;

import javax.annotation.Nonnull;
import java.io.IOException;

import com.github.nmorel.gwtjackson.client.JsonSerializationContext;
import com.github.nmorel.gwtjackson.client.JsonSerializer;
import com.github.nmorel.gwtjackson.client.stream.JsonWriter;

/**
 * Default {@link JsonSerializer} implementation for {@link Iterable}.
 *
 * @param  Type of the elements inside the {@link Iterable}
 *
 * @author Nicolas Morel
 */
public class IterableJsonSerializer, T> extends JsonSerializer {

    /**
     * @param serializer {@link JsonSerializer} used to serialize the objects inside the {@link Iterable}.
     * @param  Type of the elements inside the {@link Iterable}
     *
     * @return a new instance of {@link IterableJsonSerializer}
     */
    public static , T> IterableJsonSerializer newInstance( JsonSerializer serializer ) {
        return new IterableJsonSerializer( serializer );
    }

    protected final JsonSerializer serializer;

    /**
     * @param serializer {@link JsonSerializer} used to serialize the objects inside the {@link Iterable}.
     */
    protected IterableJsonSerializer( JsonSerializer serializer ) {
        if ( null == serializer ) {
            throw new IllegalArgumentException( "serializer cannot be null" );
        }
        this.serializer = serializer;
    }

    @Override
    public void doSerialize( JsonWriter writer, @Nonnull I values, JsonSerializationContext ctx ) throws IOException {
        writer.beginArray();
        for ( T value : values ) {
            serializer.serialize( writer, value, ctx );
        }
        writer.endArray();
    }
}