Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.koushikdutta.ion.gson;
 
 import  com.google.gson.Gson;
 import  com.google.gson.reflect.TypeToken;
 import  com.google.gson.stream.JsonReader;
Created by koush on 6/1/13.
public class GsonSerializer<T> implements AsyncParser<T> {
    Gson gson;
    Type type;
    public GsonSerializer(Gson gsonClass<T> clazz) {
        this. = gson;
         = clazz;
    }
    public GsonSerializer(Gson gson, TypeToken<T> token) {
        this. = gson;
         = token.getType();
    }
    @Override
    public Future<T> parse(DataEmitter emitter) {
        return new ByteBufferListParser().parse(emitter)
        .then(new TransformFuture<T, ByteBufferList>() {
            @Override
            protected void transform(ByteBufferList resultthrows Exception {
                ByteBufferListInputStream bin = new ByteBufferListInputStream(result);
                T ret = (T).fromJson(new JsonReader(new InputStreamReader(bin)), );
                setComplete(ret);
            }
        });
    }
    @Override
    public void write(DataSink sink, T pojoCompletedCallback completed) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        OutputStreamWriter out = new OutputStreamWriter(bout);
        .toJson(pojoout);
        try {
            out.flush();
        }
        catch (final Exception e) {
            throw new AssertionError(e);
        }
        Util.writeAll(sinkbout.toByteArray(), completed);
    }
New to GrepCode? Check out our FAQ X