Search in sources :

Example 21 with IonWriter

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

the class IonMarkupWriterTest method testPrettyCallback.

@Test
public void testPrettyCallback() throws IOException {
    // Write to a StringWriter for testing
    StringWriter out = new StringWriter();
    IonReader ionReader = system().newReader(input);
    _Private_IonTextWriterBuilder builder = (_Private_IonTextWriterBuilder) IonTextWriterBuilder.pretty();
    IonWriter ionWriter = builder.withCallbackBuilder(new TestMarkupCallback.Builder()).build(out);
    write(ionReader, ionWriter);
    assertEquals("Markup callback with pretty printing Ion Writer," + " error with data:\n" + input + "\n", pExpected, out.toString());
}
Also used : StringWriter(java.io.StringWriter) IonReader(com.amazon.ion.IonReader) IonTextWriterBuilder(com.amazon.ion.system.IonTextWriterBuilder) IonWriter(com.amazon.ion.IonWriter) Test(org.junit.Test)

Example 22 with IonWriter

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

the class IonMarkupWriterTest method testEscaping.

@Test
public void testEscaping() throws IOException {
    String input = "These should be escaped <>&";
    String expected = "<><><>&&&<><><>\"These should be escaped &lt;&gt;&amp;\"";
    StringWriter out = new StringWriter();
    _Private_IonTextWriterBuilder builder = (_Private_IonTextWriterBuilder) IonTextWriterBuilder.standard();
    IonWriter ionWriter = builder.withCallbackBuilder(new EscapingCallback.Builder()).build(out);
    ionWriter.writeString(input);
    ionWriter.finish();
    assertEquals("Escaping failed, with data:\n" + input + "\n", expected, out.toString());
}
Also used : StringWriter(java.io.StringWriter) IonTextWriterBuilder(com.amazon.ion.system.IonTextWriterBuilder) IonWriter(com.amazon.ion.IonWriter) Test(org.junit.Test)

Example 23 with IonWriter

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

the class IonMarkupWriterTest method testStandardCallback.

@Test
public void testStandardCallback() throws IOException {
    // Write to a StringWriter for testing
    StringWriter out = new StringWriter();
    IonReader ionReader = system().newReader(input);
    _Private_IonTextWriterBuilder builder = (_Private_IonTextWriterBuilder) IonTextWriterBuilder.standard();
    IonWriter ionWriter = builder.withCallbackBuilder(new TestMarkupCallback.Builder()).build(out);
    write(ionReader, ionWriter);
    assertEquals("Markup callback with standard Ion Writer, " + "error with data:\n" + input + "\n", sExpected, out.toString());
}
Also used : StringWriter(java.io.StringWriter) IonReader(com.amazon.ion.IonReader) IonTextWriterBuilder(com.amazon.ion.system.IonTextWriterBuilder) IonWriter(com.amazon.ion.IonWriter) Test(org.junit.Test)

Example 24 with IonWriter

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

the class IonSystemLite method clone.

@SuppressWarnings("unchecked")
public <T extends IonValue> T clone(T value) throws IonException {
    // Use "fast clone" when the system is the same.
    if (value.getSystem() == this) {
        return (T) value.clone();
    }
    if (value instanceof IonDatagram) {
        IonDatagram datagram = newDatagram();
        IonWriter writer = _Private_IonWriterFactory.makeWriter(datagram);
        IonReader reader = makeSystemReader(value.getSystem(), value);
        try {
            writer.writeValues(reader);
        } catch (IOException e) {
            throw new IonException(e);
        }
        return (T) datagram;
    }
    IonReader reader = newReader(value);
    reader.next();
    return (T) newValue(reader);
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) IonException(com.amazon.ion.IonException) IonReader(com.amazon.ion.IonReader) IonWriter(com.amazon.ion.IonWriter) IOException(java.io.IOException)

Example 25 with IonWriter

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

the class IonValueLite method toString.

public String toString(IonTextWriterBuilder writerBuilder) {
    StringBuilder buf = new StringBuilder(1024);
    try {
        IonWriter writer = writerBuilder.build(buf);
        writeTo(writer);
        writer.finish();
    } catch (IOException e) {
        throw new IonException(e);
    }
    return buf.toString();
}
Also used : IonException(com.amazon.ion.IonException) IonWriter(com.amazon.ion.IonWriter) com.amazon.ion.impl._Private_IonWriter(com.amazon.ion.impl._Private_IonWriter) IOException(java.io.IOException)

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