Example #1
0
  /**
   * Serializes the {@code messages} into the stream using the given schema with the supplied
   * buffer.
   */
  public static <T> void writeListTo(
      OutputStream out, List<T> messages, Schema<T> schema, boolean numeric, LinkedBuffer buffer)
      throws IOException {
    if (buffer.start != buffer.offset)
      throw new IllegalArgumentException("Buffer previously used and had not been reset.");

    if (messages.isEmpty()) {
      System.arraycopy(EMPTY_ARRAY, 0, buffer.buffer, buffer.offset, EMPTY_ARRAY.length);
      buffer.offset += EMPTY_ARRAY.length;
      return;
    }

    final JsonXOutput output = new JsonXOutput(buffer, out, numeric, schema);

    output.writeStartArray();

    boolean first = true;
    for (T m : messages) {
      if (first) {
        first = false;
        output.writeStartObject();
      } else output.writeCommaAndStartObject();

      schema.writeTo(output, m);
      if (output.isLastRepeated()) output.writeEndArray();

      output.writeEndObject().clear(false);
    }

    output.writeEndArray();
    LinkedBuffer.writeTo(out, buffer);
  }
Example #2
0
  /**
   * Serializes the {@code message} into an {@link OutputStream} via {@link JsonXOutput} using the
   * given {@code schema}.
   */
  public static <T> void writeTo(
      OutputStream out, T message, Schema<T> schema, boolean numeric, LinkedBuffer buffer)
      throws IOException {
    if (buffer.start != buffer.offset)
      throw new IllegalArgumentException("Buffer previously used and had not been reset.");

    final JsonXOutput output = new JsonXOutput(buffer, out, numeric, schema);

    output.writeStartObject();

    schema.writeTo(output, message);

    if (output.isLastRepeated()) output.writeEndArray();

    output.writeEndObject();

    LinkedBuffer.writeTo(out, buffer);
  }
Example #3
0
  /**
   * Serializes the {@code message} into a {@link LinkedBuffer} via {@link JsonXOutput} using the
   * given {@code schema} with the supplied buffer.
   */
  public static <T> void writeTo(
      LinkedBuffer buffer, T message, Schema<T> schema, boolean numeric) {
    if (buffer.start != buffer.offset)
      throw new IllegalArgumentException("Buffer previously used and had not been reset.");

    final JsonXOutput output = new JsonXOutput(buffer, numeric, schema);

    try {
      output.writeStartObject();

      schema.writeTo(output, message);

      if (output.isLastRepeated()) output.writeEndArray();

      output.writeEndObject();
    } catch (IOException e) {
      throw new RuntimeException(
          "Serializing to a byte array threw an IOException " + "(should never happen).", e);
    }
  }