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;
}
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());
}
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);
}
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));
}
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);
}
Aggregations