Search in sources :

Example 1 with IonTextWriterBuilder

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

the class RoundTripTest method testTextWriterLongStrings.

@Test
public void testTextWriterLongStrings() throws Exception {
    IonTextWriterBuilder b = IonTextWriterBuilder.standard().withLongStringThreshold(1);
    checkTextOutput(b);
}
Also used : IonTextWriterBuilder(com.amazon.ion.system.IonTextWriterBuilder) Test(org.junit.Test) RoundTripStreamingTest(com.amazon.ion.streaming.RoundTripStreamingTest)

Example 2 with IonTextWriterBuilder

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

the class TextWriterTest method testWritingTopLevelValuesOnNewLinesShouldHaveNoEffectWithPrettyPrint.

@Test
public void testWritingTopLevelValuesOnNewLinesShouldHaveNoEffectWithPrettyPrint() {
    // Setting top-level newlines to false should have no effect when pretty printing.
    IonTextWriterBuilder writerBuilder = IonTextWriterBuilder.standard().withInitialIvmHandling(SUPPRESS).withWriteTopLevelValuesOnNewLines(false).withPrettyPrinting().withNewLineType(IonTextWriterBuilder.NewLineType.LF);
    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("\n\"Foo\"\nBar\n(\n  1\n  2\n  3\n)\n[\n  4,\n  5,\n  6\n]", 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 3 with IonTextWriterBuilder

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

the class TextWriterTest method testWritingTopLevelValuesOnNewLinesWithoutPrettyPrint.

@Test
public void testWritingTopLevelValuesOnNewLinesWithoutPrettyPrint() {
    IonTextWriterBuilder writerBuilder = IonTextWriterBuilder.standard().withInitialIvmHandling(SUPPRESS).withWriteTopLevelValuesOnNewLines(true).withNewLineType(IonTextWriterBuilder.NewLineType.LF);
    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 });
    IonStruct struct = dg.add().newEmptyStruct();
    struct.add("def").newInt(42);
    struct.addTypeAnnotation("abc");
    StringBuilder sb = new StringBuilder();
    IonWriter writer = writerBuilder.build(sb);
    dg.writeTo(writer);
    assertEquals("\"Foo\"\nBar\n(1 2 3)\n[4,5,6]\nabc::{def:42}", sb.toString());
}
Also used : IonTextWriterBuilder(com.amazon.ion.system.IonTextWriterBuilder) IonStruct(com.amazon.ion.IonStruct) IonDatagram(com.amazon.ion.IonDatagram) IonWriter(com.amazon.ion.IonWriter) Test(org.junit.Test)

Example 4 with IonTextWriterBuilder

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

the class TextWriterTest method testNewLineTypesWithStandardPrinting.

@Test
public void testNewLineTypesWithStandardPrinting() {
    for (IonTextWriterBuilder.NewLineType nlt : IonTextWriterBuilder.NewLineType.values()) {
        String expected = String.format("\"Foo\"%sBar%<s(1 2 3)%<s[4,5,6]", nlt.getCharSequence());
        IonTextWriterBuilder writerBuilder = IonTextWriterBuilder.standard().withInitialIvmHandling(SUPPRESS).withWriteTopLevelValuesOnNewLines(true).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 5 with IonTextWriterBuilder

use of com.amazon.ion.system.IonTextWriterBuilder 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)

Aggregations

IonTextWriterBuilder (com.amazon.ion.system.IonTextWriterBuilder)8 IonWriter (com.amazon.ion.IonWriter)6 Test (org.junit.Test)6 IonDatagram (com.amazon.ion.IonDatagram)4 RoundTripStreamingTest (com.amazon.ion.streaming.RoundTripStreamingTest)2 IonReader (com.amazon.ion.IonReader)1 IonStruct (com.amazon.ion.IonStruct)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1