Search in sources :

Example 1 with CountingOutputStream

use of net.morimekta.util.io.CountingOutputStream in project providence by morimekta.

the class JsonSerializer method serialize.

@Override
public <T extends PMessage<T, F>, F extends PField> int serialize(@Nonnull OutputStream output, @Nonnull T message) throws IOException {
    CountingOutputStream counter = new CountingOutputStream(output);
    JsonWriter jsonWriter = prettyPrint ? new PrettyJsonWriter(counter) : new JsonWriter(counter);
    appendMessage(jsonWriter, message);
    jsonWriter.flush();
    counter.flush();
    return counter.getByteCount();
}
Also used : CountingOutputStream(net.morimekta.util.io.CountingOutputStream) PrettyJsonWriter(net.morimekta.util.json.PrettyJsonWriter) PrettyJsonWriter(net.morimekta.util.json.PrettyJsonWriter) JsonWriter(net.morimekta.util.json.JsonWriter)

Example 2 with CountingOutputStream

use of net.morimekta.util.io.CountingOutputStream in project providence by morimekta.

the class JsonSerializer method serialize.

@Override
public <T extends PMessage<T, F>, F extends PField> int serialize(@Nonnull OutputStream output, @Nonnull PServiceCall<T, F> call) throws IOException {
    CountingOutputStream counter = new CountingOutputStream(output);
    JsonWriter jsonWriter = prettyPrint ? new PrettyJsonWriter(counter) : new JsonWriter(counter);
    jsonWriter.array().value(call.getMethod());
    if (enumValueType == IdType.ID) {
        jsonWriter.value(call.getType().asInteger());
    } else {
        jsonWriter.valueUnescaped(call.getType().asString().toLowerCase(Locale.US));
    }
    jsonWriter.value(call.getSequence());
    appendMessage(jsonWriter, call.getMessage());
    jsonWriter.endArray().flush();
    counter.flush();
    return counter.getByteCount();
}
Also used : CountingOutputStream(net.morimekta.util.io.CountingOutputStream) PrettyJsonWriter(net.morimekta.util.json.PrettyJsonWriter) PrettyJsonWriter(net.morimekta.util.json.PrettyJsonWriter) JsonWriter(net.morimekta.util.json.JsonWriter)

Example 3 with CountingOutputStream

use of net.morimekta.util.io.CountingOutputStream in project providence by morimekta.

the class PrettySerializer method serialize.

@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream out, @Nonnull PServiceCall<Message, Field> call) throws IOException {
    CountingOutputStream cout = new CountingOutputStream(out);
    IndentedPrintWriter builder = new IndentedPrintWriter(cout, indent, newline);
    if (call.getSequence() != 0) {
        builder.format("%d: ", call.getSequence());
    }
    builder.format("%s %s", call.getType().asString().toLowerCase(Locale.US), call.getMethod()).begin(indent + indent);
    appendMessage(builder, call.getMessage(), true);
    builder.end().newline().flush();
    return cout.getByteCount();
}
Also used : CountingOutputStream(net.morimekta.util.io.CountingOutputStream) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter)

Example 4 with CountingOutputStream

use of net.morimekta.util.io.CountingOutputStream in project providence by morimekta.

the class TTupleProtocolSerializer method serialize.

@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream output, @Nonnull PServiceCall<Message, Field> call) throws IOException {
    CountingOutputStream wrapper = new CountingOutputStream(output);
    TTransport transport = new TIOStreamTransport(wrapper);
    try {
        TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
        TMessage tm = new TMessage(call.getMethod(), (byte) call.getType().asInteger(), call.getSequence());
        protocol.writeMessageBegin(tm);
        writeMessage(call.getMessage(), protocol);
        protocol.writeMessageEnd();
        transport.flush();
        wrapper.flush();
        return wrapper.getByteCount();
    } catch (TException e) {
        throw new SerializerException(e, e.getMessage());
    }
}
Also used : TException(org.apache.thrift.TException) CountingOutputStream(net.morimekta.util.io.CountingOutputStream) TMessage(org.apache.thrift.protocol.TMessage) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) TTransport(org.apache.thrift.transport.TTransport) SerializerException(net.morimekta.providence.serializer.SerializerException) TTupleProtocol(org.apache.thrift.protocol.TTupleProtocol)

Example 5 with CountingOutputStream

use of net.morimekta.util.io.CountingOutputStream in project providence by morimekta.

the class PrettySerializer method serialize.

public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream out, @Nonnull Message message) {
    CountingOutputStream cout = new CountingOutputStream(out);
    IndentedPrintWriter builder = new IndentedPrintWriter(cout, indent, newline);
    if (prefixWithQualifiedName) {
        builder.append(message.descriptor().getQualifiedName()).append(space);
    }
    appendMessage(builder, message, false);
    builder.flush();
    return cout.getByteCount();
}
Also used : CountingOutputStream(net.morimekta.util.io.CountingOutputStream) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter)

Aggregations

CountingOutputStream (net.morimekta.util.io.CountingOutputStream)8 SerializerException (net.morimekta.providence.serializer.SerializerException)4 TException (org.apache.thrift.TException)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 TTransport (org.apache.thrift.transport.TTransport)4 IndentedPrintWriter (net.morimekta.util.io.IndentedPrintWriter)2 JsonWriter (net.morimekta.util.json.JsonWriter)2 PrettyJsonWriter (net.morimekta.util.json.PrettyJsonWriter)2 TMessage (org.apache.thrift.protocol.TMessage)2 TProtocol (org.apache.thrift.protocol.TProtocol)2 TTupleProtocol (org.apache.thrift.protocol.TTupleProtocol)2