Search in sources :

Example 26 with IonWriter

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

the class Printer method print.

// =========================================================================
// Print methods
public void print(IonValue value, Appendable out) throws IOException {
    // Copy the options so visitor won't see changes made while printing.
    Options options;
    synchronized (// So we don't clone in the midst of changes
    this) {
        options = myOptions.clone();
    }
    if (true) {
        _print(value, makeVisitor(options, out));
    } else {
        // Bridge to the configurable text writer. This is here for
        // testing purposes. It *almost* works except for printing
        // datagram as list.
        boolean dg = value instanceof IonDatagram;
        _Private_IonTextWriterBuilder o = _Private_IonTextWriterBuilder.standard();
        o.setCharset(IonTextWriterBuilder.ASCII);
        if (dg) {
            if (options.skipSystemValues) {
                o.withMinimalSystemData();
            } else if (options.simplifySystemValues) {
                o.setIvmMinimizing(IvmMinimizing.DISTANT);
                o.setLstMinimizing(LstMinimizing.LOCALS);
            }
        }
        o._blob_as_string = options.blobAsString;
        o._clob_as_string = options.clobAsString;
        o._decimal_as_float = options.decimalAsFloat;
        // TODO datagram as list
        o._sexp_as_list = options.sexpAsList;
        o._skip_annotations = options.skipAnnotations;
        o._string_as_json = options.stringAsJson;
        o._symbol_as_string = options.symbolAsString;
        o._timestamp_as_millis = options.timestampAsMillis;
        o._timestamp_as_string = options.timestampAsString;
        o._untyped_nulls = options.untypedNulls;
        IonWriter writer = o.build(out);
        // TODO doesn't work for datagram since it skips system values
        // value.writeTo(writer);
        _Private_IonSystem system = (_Private_IonSystem) value.getSystem();
        IonReader reader = system.newSystemReader(value);
        writer.writeValues(reader);
        writer.finish();
    }
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) IonReader(com.amazon.ion.IonReader) com.amazon.ion.impl._Private_IonSystem(com.amazon.ion.impl._Private_IonSystem) IonWriter(com.amazon.ion.IonWriter) com.amazon.ion.impl._Private_IonTextWriterBuilder(com.amazon.ion.impl._Private_IonTextWriterBuilder)

Example 27 with IonWriter

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

the class _Private_IonWriterFactory method makeSystemWriter.

/**
 * @param container must not be null.
 */
public static IonWriter makeSystemWriter(IonContainer container) {
    IonSystem sys = container.getSystem();
    IonCatalog cat = sys.getCatalog();
    SymbolTable defaultSystemSymtab = sys.getSystemSymbolTable();
    IonWriter writer = new IonWriterSystemTree(defaultSystemSymtab, cat, container, null);
    return writer;
}
Also used : IonSystem(com.amazon.ion.IonSystem) IonCatalog(com.amazon.ion.IonCatalog) SymbolTable(com.amazon.ion.SymbolTable) IonWriter(com.amazon.ion.IonWriter)

Example 28 with IonWriter

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

the class IonLoaderLite method load_helper.

/**
 * This doesn't wrap IOException because some callers need to propagate it.
 *
 * @return a new datagram; not null.
 */
private IonDatagramLite load_helper(IonReader reader) throws IOException {
    IonDatagramLite datagram = new IonDatagramLite(_system, _catalog);
    IonWriter writer = _Private_IonWriterFactory.makeWriter(datagram);
    writer.writeValues(reader);
    return datagram;
}
Also used : IonWriter(com.amazon.ion.IonWriter)

Example 29 with IonWriter

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

the class IonTextWriterBuilderTest method testInitialIvmSuppression.

@Test
public void testInitialIvmSuppression() throws IOException {
    IonTextWriterBuilder b = IonTextWriterBuilder.standard();
    StringBuilder out = new StringBuilder();
    IonWriter writer = b.build(out);
    writer.writeSymbol(ION_1_0);
    writer.writeNull();
    writer.writeSymbol(ION_1_0);
    writer.close();
    assertEquals(ION_1_0 + " null " + ION_1_0, out.toString());
    b.withInitialIvmHandling(SUPPRESS);
    out.setLength(0);
    writer = b.build(out);
    writer.writeSymbol(ION_1_0);
    writer.writeSymbol(ION_1_0);
    writer.writeNull();
    writer.writeSymbol(ION_1_0);
    writer.close();
    assertEquals("null " + ION_1_0, out.toString());
}
Also used : IonWriter(com.amazon.ion.IonWriter) com.amazon.ion.impl._Private_IonWriter(com.amazon.ion.impl._Private_IonWriter) Test(org.junit.Test)

Example 30 with IonWriter

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

the class IonTextWriterBuilderTest method testImports.

// -------------------------------------------------------------------------
@Test
public void testImports() {
    SymbolTable f = Symtabs.CATALOG.getTable("fred", 1);
    SymbolTable g = Symtabs.CATALOG.getTable("ginger", 1);
    SymbolTable[] symtabsF = new SymbolTable[] { f };
    SymbolTable[] symtabsG = new SymbolTable[] { g };
    IonTextWriterBuilder b = IonTextWriterBuilder.standard();
    b.setImports(f);
    StringBuilder out = new StringBuilder();
    IonWriter writer = b.build(out);
    SymbolTable st = writer.getSymbolTable();
    assertArrayEquals(symtabsF, st.getImportedTables());
    // Test with...() on mutable builder
    IonTextWriterBuilder b2 = b.withImports(g);
    assertSame(b, b2);
    assertArrayEquals(symtabsG, b2.getImports());
    // Test with...() on immutable builder
    b2 = b.immutable();
    assertArrayEquals(symtabsG, b2.getImports());
    IonTextWriterBuilder b3 = b2.withImports(f);
    assertNotSame(b2, b3);
    assertArrayEquals(symtabsG, b2.getImports());
    assertArrayEquals(symtabsF, b3.getImports());
    // Test cloning of array
    SymbolTable[] symtabs = new SymbolTable[] { f };
    b3.setImports(symtabs);
    assertNotSame(symtabs, b3.getImports());
    assertArrayEquals(symtabsF, b3.getImports());
    symtabs[0] = g;
    assertArrayEquals(symtabsF, b3.getImports());
    b3.getImports()[0] = g;
    assertArrayEquals(symtabsF, b3.getImports());
}
Also used : SymbolTable(com.amazon.ion.SymbolTable) IonWriter(com.amazon.ion.IonWriter) com.amazon.ion.impl._Private_IonWriter(com.amazon.ion.impl._Private_IonWriter) 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