Search in sources :

Example 41 with IonWriter

use of com.amazon.ion.IonWriter in project jackson-dataformats-binary by FasterXML.

the class IonObjectMapper method writeValueAsIonValue.

/**
 * Method that can be used to map any Java value to an IonValue.
 */
public IonValue writeValueAsIonValue(Object value) throws IOException {
    // 04-Jan-2017, tatu: Bit of incompatiblity wrt 2.x handling: should this result in
    // Java `null`, or Ion null marker? For now, choose latter
    /*        
        if (value == null) {
            return null;
        }
        */
    IonFactory f = tokenStreamFactory();
    IonDatagram container = f._system.newDatagram();
    try (IonWriter writer = f._system.newWriter(container)) {
        writeValue(writer, value);
        IonValue result = container.get(0);
        result.removeFromContainer();
        return result;
    }
}
Also used : IonValue(com.amazon.ion.IonValue) IonDatagram(com.amazon.ion.IonDatagram) IonWriter(com.amazon.ion.IonWriter)

Example 42 with IonWriter

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

the class SymtabApp method processFiles.

@Override
public void processFiles(String[] filePaths) {
    super.processFiles(filePaths);
    SymbolTable[] importArray = new SymbolTable[myImports.size()];
    myImports.toArray(importArray);
    SymbolTable mySymtab = mySystem.newSharedSymbolTable(mySymtabName, mySymtabVersion, mySymbols.iterator(), importArray);
    IonWriter w = mySystem.newTextWriter((OutputStream) System.out);
    try {
        // TODO ensure IVM is printed
        mySymtab.writeTo(w);
        System.out.println();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : SymbolTable(com.amazon.ion.SymbolTable) IonWriter(com.amazon.ion.IonWriter) IOException(java.io.IOException)

Example 43 with IonWriter

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

the class _Private_IonWriterFactory method makeWriter.

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

Example 44 with IonWriter

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

the class _Private_CommandLine method doPrintVersion.

private static void doPrintVersion() throws IOException {
    IonTextWriterBuilder b = IonTextWriterBuilder.pretty();
    b.setCharset(IonTextWriterBuilder.ASCII);
    IonWriter w = b.build((Appendable) System.out);
    w.stepIn(IonType.STRUCT);
    {
        w.setFieldName("version");
        w.writeString(info.getProjectVersion());
        w.setFieldName("build_time");
        w.writeTimestamp(info.getBuildTime());
    }
    w.stepOut();
    w.finish();
    System.out.println();
}
Also used : IonTextWriterBuilder(com.amazon.ion.system.IonTextWriterBuilder) IonWriter(com.amazon.ion.IonWriter)

Example 45 with IonWriter

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

the class IonReaderLookaheadBufferTest method rewindToValueStartWithLstAppend.

@Test
public void rewindToValueStartWithLstAppend() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IonWriter writer = IonBinaryWriterBuilder.standard().withLocalSymbolTableAppendEnabled().build(out);
    writer.writeSymbol("abc");
    writer.flush();
    writer.writeSymbol("def");
    writer.close();
    InputStream input = new ByteArrayInputStream(out.toByteArray());
    IonReaderLookaheadBuffer lookahead = new IonReaderLookaheadBuffer(builder.build(), input);
    lookahead.fillInput();
    IonReader reader = lookahead.newIonReader(IonReaderBuilder.standard());
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals("abc", reader.stringValue());
    assertTrue(lookahead.moreDataRequired());
    lookahead.fillInput();
    assertFalse(lookahead.moreDataRequired());
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals("def", reader.stringValue());
    lookahead.rewindToValueStart();
    // 2-byte value (0x71 0x0B). No IVM or symbol table.
    assertEquals(2, lookahead.available());
    assertEquals(IonType.SYMBOL, reader.next());
    assertEquals("def", reader.stringValue());
    // Note: if mark() / rewind() is used instead of rewindToValueStart(), the following lines will fail; there
    // will be 12 local symbols and "def" will occur twice in the symbol table. That's because mark() includes
    // the symbol table (in this case an LST append), and rewinding past the symbol table causes the append
    // to be processed a second time by IonReader.next().
    SymbolTable symbolTable = reader.getSymbolTable();
    assertEquals(symbolTable.getSystemSymbolTable().getMaxId() + 2, reader.getSymbolTable().getMaxId());
    List<String> symbols = new ArrayList<String>(2);
    Iterator<String> iterator = symbolTable.iterateDeclaredSymbolNames();
    while (iterator.hasNext()) {
        symbols.add(iterator.next());
    }
    assertEquals(Arrays.asList("abc", "def"), symbols);
    input.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IonReader(com.amazon.ion.IonReader) ArrayList(java.util.ArrayList) SymbolTable(com.amazon.ion.SymbolTable) IonWriter(com.amazon.ion.IonWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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