use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class TextWriterTest method testNewLineTypesWithPrettyPrinting.
@Test
public void testNewLineTypesWithPrettyPrinting() {
for (IonTextWriterBuilder.NewLineType nlt : IonTextWriterBuilder.NewLineType.values()) {
String expected = String.format("%s\"Foo\"%<sBar%<s(%<s 1%<s 2%<s 3%<s)%<s[%<s 4,%<s 5,%<s 6%<s]", nlt.getCharSequence());
IonTextWriterBuilder writerBuilder = IonTextWriterBuilder.standard().withInitialIvmHandling(SUPPRESS).withWriteTopLevelValuesOnNewLines(false).withPrettyPrinting().withNewLineType(nlt);
IonDatagram dg = system().newDatagram();
dg.add().newString("Foo");
dg.add().newSymbol("Bar");
dg.add().newSexp(new int[] { 1, 2, 3 });
dg.add().newList(new int[] { 4, 5, 6 });
StringBuilder sb = new StringBuilder();
IonWriter writer = writerBuilder.build(sb);
dg.writeTo(writer);
assertEquals(expected, sb.toString());
}
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method lstAppend.
@Test
public void lstAppend() throws Exception {
writerBuilder = IonBinaryWriterBuilder.standard().withLocalSymbolTableAppendEnabled();
IonReaderBinaryIncremental reader = readerFor(new WriterFunction() {
@Override
public void write(IonWriter writer) throws IOException {
writer.stepIn(IonType.STRUCT);
writer.setFieldName("foo");
writer.addTypeAnnotation("uvw");
writer.writeSymbol("abc");
writer.setFieldName("bar");
writer.setTypeAnnotations("qrs", "xyz");
writer.writeSymbol("def");
writer.stepOut();
writer.flush();
writer.writeSymbol("orange");
}
});
assertEquals(IonType.STRUCT, reader.next());
reader.stepIn();
assertEquals(IonType.SYMBOL, reader.next());
assertEquals("foo", reader.getFieldName());
assertEquals(Collections.singletonList("uvw"), Arrays.asList(reader.getTypeAnnotations()));
assertEquals("abc", reader.stringValue());
assertEquals(IonType.SYMBOL, reader.next());
assertEquals("bar", reader.getFieldName());
assertEquals(Arrays.asList("qrs", "xyz"), Arrays.asList(reader.getTypeAnnotations()));
assertEquals("def", reader.stringValue());
reader.stepOut();
SymbolTable preAppend = reader.getSymbolTable();
assertEquals(IonType.SYMBOL, reader.next());
SymbolTable postAppend = reader.getSymbolTable();
assertEquals("orange", reader.stringValue());
assertNull(preAppend.find("orange"));
assertNotNull(postAppend.find("orange"));
assertNull(reader.next());
reader.close();
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method readerFor.
/**
* Creates an incremental reader over the binary Ion data created by invoking the given WriterFunction.
* @param writerFunction the function used to generate the data.
* @return a new reader.
* @throws Exception if an exception is raised while writing the Ion data.
*/
private IonReaderBinaryIncremental readerFor(WriterFunction writerFunction) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonWriter writer = writerBuilder.build(out);
writerFunction.write(writer);
writer.close();
return new IonReaderBinaryIncremental(readerBuilder, new ByteArrayInputStream(out.toByteArray()));
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonReaderBinaryRawLargeStreamTest method testData.
private byte[] testData(Timestamp timestamp) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonWriter writer = IonBinaryWriterBuilder.standard().build(out);
writer.writeString("foo");
writer.writeDecimal(BigDecimal.TEN);
writer.writeTimestamp(timestamp);
writer.close();
byte[] dataWithIvm = out.toByteArray();
// Strip the IVM, as this needs to be one continuous stream to avoid resetting the reader's internals.
byte[] data = new byte[dataWithIvm.length - BINARY_VERSION_MARKER_1_0.length];
System.arraycopy(dataWithIvm, BINARY_VERSION_MARKER_1_0.length, data, 0, data.length);
return data;
}
use of com.amazon.ion.IonWriter in project ion-java by amzn.
the class IonReaderBinaryIncrementalTest method symbolTableWithImportsThenSymbols.
@Test
public void symbolTableWithImportsThenSymbols() throws Exception {
SimpleCatalog catalog = new SimpleCatalog();
catalog.putTable(SYSTEM.newSharedSymbolTable("foo", 1, Arrays.asList("abc", "def").iterator()));
readerBuilder = IonReaderBuilder.standard().withCatalog(catalog);
writerBuilder = IonBinaryWriterBuilder.standard().withCatalog(catalog);
IonReaderBinaryIncremental reader = readerFor(new WriterFunction() {
@Override
public void write(IonWriter writer) throws IOException {
writer.setTypeAnnotations("$ion_symbol_table");
writer.stepIn(IonType.STRUCT);
writer.setFieldName("imports");
writer.stepIn(IonType.LIST);
writer.stepIn(IonType.STRUCT);
writer.setFieldName("name");
writer.writeString("foo");
writer.setFieldName("version");
writer.writeInt(1);
writer.setFieldName("max_id");
writer.writeInt(2);
writer.stepOut();
writer.stepOut();
writer.setFieldName("symbols");
writer.stepIn(IonType.LIST);
writer.writeString("ghi");
writer.stepOut();
writer.stepOut();
writer.writeSymbol("abc");
writer.writeSymbol("def");
writer.writeSymbol("ghi");
}
});
assertEquals(IonType.SYMBOL, reader.next());
assertEquals("abc", reader.stringValue());
assertEquals(IonType.SYMBOL, reader.next());
assertEquals("def", reader.stringValue());
assertEquals(IonType.SYMBOL, reader.next());
assertEquals("ghi", reader.stringValue());
assertNull(reader.next());
reader.close();
}
Aggregations