Search in sources :

Example 11 with IonDatagram

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

the class TextWriterTest method testWritingJsonLongClobs.

@Test
public void testWritingJsonLongClobs() throws Exception {
    options = IonTextWriterBuilder.json();
    options.setInitialIvmHandling(SUPPRESS);
    options.setLongStringThreshold(4);
    // Cannot call expectRendering as the input to reload() would be Json rather than Ion
    // test 1
    IonDatagram dg = system().newDatagram();
    dg.add().newClob(new byte[] { 'a', '"', '\'', '\n' });
    // expectRendering
    iw = makeWriter();
    dg.writeTo(iw);
    IonDatagram reloaded = loader().load("{{\"a\\\"'\\n\"}}");
    assertEquals(dg, reloaded);
    String actual = outputString();
    assertEquals("\"a\\\"'\\n\"", actual);
    // test 2
    dg.clear();
    dg.add().newClob(new byte[] { 'a', '"', '\'', '\n', 'c', 0x7F });
    // expectRendering
    iw = makeWriter();
    dg.writeTo(iw);
    reloaded = loader().load("{{'''a\"\\'\nc\\x7f'''}}");
    assertEquals(dg, reloaded);
    actual = outputString();
    assertEquals("\"a\\\"'\\nc\\u007f\"", actual);
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) Test(org.junit.Test)

Example 12 with IonDatagram

use of com.amazon.ion.IonDatagram 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 13 with IonDatagram

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

the class ValueWriterTest method testWriteValuesWithSymtabIntoContainer.

// TODO more thorough testing that you can't output from DOM w/ bad text
@Test
public void testWriteValuesWithSymtabIntoContainer() throws Exception {
    myOutputInList = true;
    SymbolTable fredSymtab = Symtabs.register(Symtabs.FRED_NAME, 1, catalog());
    SymbolTable gingerSymtab = Symtabs.register(Symtabs.GINGER_NAME, 1, catalog());
    String gingerSym = gingerSymtab.findKnownSymbol(1);
    // First setup some data to be copied.
    IonDatagram dg = system().newDatagram(gingerSymtab);
    dg.add().newSymbol(gingerSym);
    IonReader r = system().newReader(dg.getBytes());
    // Now copy that data into a non-top-level context
    iw = makeWriter(fredSymtab);
    iw.writeValues(r);
    IonDatagram result = reload();
    IonList l = (IonList) result.get(0);
    assertEquals(1, l.size());
    IonSymbol s = (IonSymbol) l.get(0);
    // Should've assigned a new SID
    checkSymbol(gingerSym, systemMaxId() + Symtabs.FRED_MAX_IDS[1] + 1, s);
}
Also used : IonSymbol(com.amazon.ion.IonSymbol) IonDatagram(com.amazon.ion.IonDatagram) IonList(com.amazon.ion.IonList) IonReader(com.amazon.ion.IonReader) SymbolTable(com.amazon.ion.SymbolTable) Test(org.junit.Test)

Example 14 with IonDatagram

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

the class IonManagedBinaryWriterTest method testManuallyWriteLSTAppendWithSymbolsFirst.

@Test
public void testManuallyWriteLSTAppendWithSymbolsFirst() throws Exception {
    writer.writeSymbol("taco");
    writer.addTypeAnnotation("$ion_symbol_table");
    writer.stepIn(IonType.STRUCT);
    writer.setFieldName("symbols");
    writer.stepIn(IonType.LIST);
    writer.writeString("burrito");
    writer.stepOut();
    writer.setFieldName("imports");
    writer.writeSymbol("$ion_symbol_table");
    writer.stepOut();
    writer.writeSymbol("burrito");
    writer.finish();
    IonReader reader = system().newReader(writer.getBytes());
    reader.next();
    assertEquals(reader.getSymbolTable().findSymbol("taco"), 15);
    assertEquals(reader.getSymbolTable().findSymbol("burrito"), lstAppendMode.isEnabled() ? -1 : 16);
    reader.next();
    assertEquals(reader.getSymbolTable().findSymbol("taco"), 15);
    assertEquals(reader.getSymbolTable().findSymbol("burrito"), 16);
    assertNull(reader.next());
    IonDatagram dg = system().getLoader().load(writer.getBytes());
    assertEquals(2, dg.size());
    assertEquals("taco", ((IonSymbol) dg.get(0)).stringValue());
    assertEquals("burrito", ((IonSymbol) dg.get(1)).stringValue());
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) IonReader(com.amazon.ion.IonReader) Test(org.junit.Test)

Example 15 with IonDatagram

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

the class IonWriterTestCase method testWritingSymtabWithExtraAnnotations.

// TODO amzn/ion-java/issues/15
@Test
// TODO amzn/ion-java/issues/15
@Ignore
public void testWritingSymtabWithExtraAnnotations() throws Exception {
    String[] annotations = new String[] { ION_SYMBOL_TABLE, ION_SYMBOL_TABLE };
    iw = makeWriter();
    iw.setTypeAnnotations(annotations);
    iw.stepIn(IonType.STRUCT);
    iw.stepOut();
    iw.writeSymbol("foo");
    iw.close();
    IonDatagram dg = reload();
    IonStruct v = (IonStruct) dg.systemGet(1);
    Assert.assertArrayEquals(annotations, v.getTypeAnnotations());
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonDatagram(com.amazon.ion.IonDatagram) IonString(com.amazon.ion.IonString) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

IonDatagram (com.amazon.ion.IonDatagram)92 Test (org.junit.Test)77 IonValue (com.amazon.ion.IonValue)20 SymbolTable (com.amazon.ion.SymbolTable)18 IonReader (com.amazon.ion.IonReader)17 IonString (com.amazon.ion.IonString)15 IonStruct (com.amazon.ion.IonStruct)13 IonWriter (com.amazon.ion.IonWriter)11 IonSymbol (com.amazon.ion.IonSymbol)6 IonSystem (com.amazon.ion.IonSystem)6 IonLoader (com.amazon.ion.IonLoader)5 IonType (com.amazon.ion.IonType)5 SimpleCatalog (com.amazon.ion.system.SimpleCatalog)5 BlobTest (com.amazon.ion.BlobTest)4 ClobTest (com.amazon.ion.ClobTest)4 IntTest (com.amazon.ion.IntTest)4 IonList (com.amazon.ion.IonList)4 IonTextWriterBuilder (com.amazon.ion.system.IonTextWriterBuilder)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 BinaryTest (com.amazon.ion.BinaryTest)2