@Override public byte[] serialize(final T instance) throws IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonIOUtil.writeTo(out, instance, writeSchema, false); return out.toByteArray(); }
/** * Serializes the {@code message} into a byte array * using the given {@code schema}. */ public static <T> byte[] toByteArray(T message, Schema<T> schema, boolean numeric) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { writeTo(baos, message, schema, numeric); } catch (IOException e) { throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); } return baos.toByteArray(); }
/** * Serializes the {@code message} into a byte array * using the given {@code schema}. * * The {@link LinkedBuffer}'s internal byte array will be used as the primary buffer * when writing the message. */ public static <T> byte[] toByteArray(T message, Schema<T> schema, boolean numeric, LinkedBuffer buffer) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { writeTo(baos, message, schema, numeric, buffer); } catch (IOException e) { throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); } return baos.toByteArray(); }
/** * Serializes the {@code message} into a {@link Writer} using * the given {@code schema}. */ public static <T> void writeTo(Writer writer, T message, Schema<T> schema, boolean numeric) throws IOException { final JsonGenerator generator = DEFAULT_JSON_FACTORY.createJsonGenerator(writer); try { writeTo(generator, message, schema, numeric); } finally { generator.close(); } }
/** * Serializes the {@code message} into an {@link OutputStream} * using the given {@code schema}. * * The {@link LinkedBuffer}'s internal byte array will be used as the primary buffer * when writing the message. */ public static <T> void writeTo(OutputStream out, T message, Schema<T> schema, boolean numeric, LinkedBuffer buffer) throws IOException { final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false); final JsonGenerator generator = newJsonGenerator(out, buffer.buffer, 0, false, context); try { writeTo(generator, message, schema, numeric); } finally { generator.close(); } }
/** * Serializes the {@code message} into an {@link OutputStream} * using the given {@code schema}. * * The {@link LinkedBuffer}'s internal byte array will be used as the primary buffer * when writing the message. */ public static <T> void writeTo(OutputStream out, T message, Schema<T> schema, boolean numeric, LinkedBuffer buffer) throws IOException { final IOContext context = new IOContext(DEFAULT_SMILE_FACTORY._getBufferRecycler(), out, false); final SmileGenerator generator = newSmileGenerator(out, buffer.buffer, 0, false, context); //final SmileGenerator generator = DEFAULT_SMILE_FACTORY.createJsonGenerator(out); try { JsonIOUtil.writeTo(generator, message, schema, numeric); } finally { generator.close(); } }
/** * Serializes the {@code message} into an {@link OutputStream} * using the given {@code schema}. */ public static <T> void writeTo(OutputStream out, T message, Schema<T> schema, boolean numeric) throws IOException { final IOContext context = new IOContext(DEFAULT_SMILE_FACTORY._getBufferRecycler(), out, false); final SmileGenerator generator = newSmileGenerator(out, context.allocWriteEncodingBuffer(), 0, true, context); //final SmileGenerator generator = DEFAULT_SMILE_FACTORY.createJsonGenerator(out); try { JsonIOUtil.writeTo(generator, message, schema, numeric); } finally { generator.close(); } }
/** * Serializes the {@code message} into an {@link OutputStream} * using the given {@code schema}. */ public static <T> void writeTo(OutputStream out, T message, Schema<T> schema, boolean numeric) throws IOException { final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false); final JsonGenerator generator = newJsonGenerator(out, context.allocWriteEncodingBuffer(), 0, true, context); /*final JsonGenerator generator = DEFAULT_JSON_FACTORY.createJsonGenerator(out, JsonEncoding.UTF8);*/ try { writeTo(generator, message, schema, numeric); } finally { generator.close(); } }