Package io.fathom.cloud.protobuf

Source Code of io.fathom.cloud.protobuf.ProtobufJsonWriter

package io.fathom.cloud.protobuf;

import io.fathom.cloud.protobuf.mapper.MessageMapper;
import io.fathom.cloud.protobuf.mapper.ProtobufWriter;

import java.io.IOException;

import com.google.gson.stream.JsonWriter;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Message;

public class ProtobufJsonWriter implements ProtobufWriter {

    private final JsonWriter json;

    public ProtobufJsonWriter(JsonWriter json) {
        this.json = json;
    }

    public static void serialize(Message src, JsonWriter json) throws IOException {
        ProtobufJsonWriter writer = new ProtobufJsonWriter(json);

        Descriptor descriptor = src.getDescriptorForType();
        MessageMapper mapper = MessageMapper.getMessageMapper(descriptor);
        mapper.write(src, writer);
    }

    @Override
    public void beginObject() throws IOException {
        json.beginObject();
    }

    @Override
    public void endObject() throws IOException {
        json.endObject();
    }

    @Override
    public void name(String name) throws IOException {
        json.name(name);
    }

    @Override
    public void beginArray() throws IOException {
        json.beginArray();
    }

    @Override
    public void endArray() throws IOException {
        json.endArray();
    }

    @Override
    public void value(boolean v) throws IOException {
        json.value(v);
    }

    @Override
    public void value(long v) throws IOException {
        json.value(v);
    }

    @Override
    public void value(double v) throws IOException {
        json.value(v);
    }

    @Override
    public void value(String v) throws IOException {
        json.value(v);
    }

}
TOP

Related Classes of io.fathom.cloud.protobuf.ProtobufJsonWriter

TOP
Copyright © 2018 www.massapi.com. 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.