Search in sources :

Example 51 with IonWriter

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());
    }
}
Also used : IonTextWriterBuilder(com.amazon.ion.system.IonTextWriterBuilder) IonDatagram(com.amazon.ion.IonDatagram) IonWriter(com.amazon.ion.IonWriter) Test(org.junit.Test)

Example 52 with IonWriter

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();
}
Also used : SymbolTable(com.amazon.ion.SymbolTable) IonWriter(com.amazon.ion.IonWriter) IOException(java.io.IOException) Test(org.junit.Test)

Example 53 with IonWriter

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()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 54 with IonWriter

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;
}
Also used : IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 55 with IonWriter

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();
}
Also used : SimpleCatalog(com.amazon.ion.system.SimpleCatalog) IonWriter(com.amazon.ion.IonWriter) IOException(java.io.IOException) 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