Search in sources :

Example 36 with IonWriter

use of com.amazon.ion.IonWriter in project ion-java by amzn.

the class RoundTripStreamingTest method makeText.

/**
 * Use IonReader to consume the buffer, and IonTextWriter to print it out.
 */
private byte[] makeText(byte[] buffer, boolean prettyPrint) throws IOException {
    IonReader in = makeIterator(buffer);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IonTextWriterBuilder b = IonTextWriterBuilder.standard();
    if (prettyPrint) {
        b.withPrettyPrinting();
    }
    b.setInitialIvmHandling(SUPPRESS);
    IonWriter tw = b.build(out);
    tw.writeValues(in);
    tw.close();
    in.close();
    // this is utf-8
    byte[] buf = out.toByteArray();
    return buf;
}
Also used : IonTextWriterBuilder(com.amazon.ion.system.IonTextWriterBuilder) IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 37 with IonWriter

use of com.amazon.ion.IonWriter in project jackson-dataformats-binary by FasterXML.

the class DataBindWriteTest method _testIonWriterReuse.

private void _testIonWriterReuse(IonWriterBuilder ionWriterBuilder) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
    IonWriter ionWriter = ionWriterBuilder.build(byteArrayOutputStream);
    IonObjectMapper ionObjectMapper = new IonObjectMapper();
    ionObjectMapper.writeValue(ionWriter, "Animal");
    ionObjectMapper.writeValue(ionWriter, "Vegetable");
    ionObjectMapper.writeValue(ionWriter, "Mineral");
    ionWriter.close();
    byte[] data = byteArrayOutputStream.toByteArray();
    assertNotNull(data);
    IonReader ionReader = IonReaderBuilder.standard().build(data);
    assertEquals(IonType.STRING, ionReader.next());
    assertEquals("Animal", ionReader.stringValue());
    assertEquals(IonType.STRING, ionReader.next());
    assertEquals("Vegetable", ionReader.stringValue());
    assertEquals(IonType.STRING, ionReader.next());
    assertEquals("Mineral", ionReader.stringValue());
}
Also used : IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 38 with IonWriter

use of com.amazon.ion.IonWriter in project jackson-dataformats-binary by FasterXML.

the class IonFactory method _createGenerator.

/*
    /**********************************************************************
    /* Helper methods, generators
    /**********************************************************************
     */
protected IonGenerator _createGenerator(ObjectWriteContext writeCtxt, OutputStream out, JsonEncoding enc, boolean isManaged) {
    IonWriter ion;
    IOContext ioCtxt = _createContext(_createContentReference(out), isManaged);
    // not necessarily same as 'out'...
    Closeable dst;
    // Binary writers are simpler: no alternate encodings
    if (_cfgBinaryWriters) {
        ioCtxt.setEncoding(enc);
        ion = _system.newBinaryWriter(out);
        dst = out;
    } else {
        if (enc != JsonEncoding.UTF8) {
            // not sure if non-UTF-8 encodings would be legal...
            throw _wrapIOFailure(new IOException("Ion only supports UTF-8 encoding, can not use " + enc));
        }
        // In theory Ion package could take some advantage of getting OutputStream.
        // In practice we seem to be better off using Jackson's efficient buffering encoder
        ioCtxt.setEncoding(enc);
        final Writer w = new UTF8Writer(ioCtxt, out);
        ion = _createTextualIonWriter(writeCtxt, w);
        dst = w;
    }
    // `true` for "ionWriterIsManaged" since we created it:
    return _createGenerator(writeCtxt, ioCtxt, ion, true, dst);
}
Also used : UTF8Writer(com.fasterxml.jackson.core.io.UTF8Writer) IOContext(com.fasterxml.jackson.core.io.IOContext) IonWriter(com.amazon.ion.IonWriter) UTF8Writer(com.fasterxml.jackson.core.io.UTF8Writer) IonWriter(com.amazon.ion.IonWriter)

Example 39 with IonWriter

use of com.amazon.ion.IonWriter in project jackson-dataformats-binary by FasterXML.

the class GenerateSexpTest method generatorUsedInStreamingWriteBinary.

@Test
public void generatorUsedInStreamingWriteBinary() throws IOException {
    byte[] expectedBytes = null;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IonWriter writer = ionSystem.newBinaryWriter(baos)) {
        ionSystem.singleValue("(foo 0)").writeTo(writer);
        writer.finish();
        expectedBytes = baos.toByteArray();
    }
    IonObjectMapper binaryMapper = IonObjectMapper.builder(IonFactory.forBinaryWriters()).build();
    Assert.assertArrayEquals(expectedBytes, toBytes(new SexpObject("foo", 0), binaryMapper));
}
Also used : IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 40 with IonWriter

use of com.amazon.ion.IonWriter in project jackson-dataformats-binary by FasterXML.

the class DataBindWriteTest method testSimpleObjectWriteIon.

@Test
public void testSimpleObjectWriteIon() throws Exception {
    IonStruct struct = ion.newEmptyStruct();
    IonWriter writer = ion.newWriter(struct);
    writer.setFieldName("payload");
    new IonObjectMapper().writeValue(writer, new MyBean());
    writer.close();
    IonStruct expectedStruct = ion.newEmptyStruct();
    expectedStruct.put("payload", expectedMyBean.get(0).clone());
    assertEquals(expectedStruct, struct);
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonWriter(com.amazon.ion.IonWriter) Test(org.junit.Test)

Aggregations

IonWriter (com.amazon.ion.IonWriter)71 Test (org.junit.Test)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)35 IonReader (com.amazon.ion.IonReader)19 com.amazon.ion.impl._Private_IonWriter (com.amazon.ion.impl._Private_IonWriter)16 SymbolTable (com.amazon.ion.SymbolTable)12 IonDatagram (com.amazon.ion.IonDatagram)11 IOException (java.io.IOException)11 IonTextWriterBuilder (com.amazon.ion.system.IonTextWriterBuilder)10 com.amazon.ion.impl._Private_IonBinaryWriterBuilder (com.amazon.ion.impl._Private_IonBinaryWriterBuilder)9 IonSystem (com.amazon.ion.IonSystem)8 OutputStream (java.io.OutputStream)8 IonCatalog (com.amazon.ion.IonCatalog)4 IonReaderBuilder (com.amazon.ion.system.IonReaderBuilder)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 IonException (com.amazon.ion.IonException)3 IonStruct (com.amazon.ion.IonStruct)3 StringWriter (java.io.StringWriter)3