Search in sources :

Example 46 with IonDatagram

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

the class SimpleIonWriteTest method ionTextCompare.

private void ionTextCompare(String generatedTextIon) {
    IonLoader loader = ion.newLoader();
    IonDatagram loadedDatagram = loader.load(generatedTextIon);
    // the expected value is always the same {a:"value",b:42}
    assertEquals(expected, loadedDatagram);
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) IonLoader(com.amazon.ion.IonLoader)

Example 47 with IonDatagram

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

the class SimpleIonWriteTest method ionBinaryCompare.

private void ionBinaryCompare(byte[] generatedBinaryIon) {
    IonLoader loader = ion.newLoader();
    IonDatagram loadedDatagram = loader.load(generatedBinaryIon);
    // the expected value is always the same {a:"value",b:42}
    assertEquals(expected, loadedDatagram);
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) IonLoader(com.amazon.ion.IonLoader)

Example 48 with IonDatagram

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

the class IonReaderTreeUserX method next_helper_user.

boolean next_helper_user() {
    if (_eof)
        return false;
    if (_next != null)
        return true;
    clear_system_value_stack();
    // read values from the system
    // reader and if they are system values
    // process them.  Return when we've
    // read all the immediate system values
    IonType next_type;
    for (; ; ) {
        next_type = next_helper_system();
        if (_top == 0 && _parent instanceof IonDatagram) {
            if (IonType.SYMBOL.equals(next_type)) {
                assert (_next instanceof IonSymbol);
                IonSymbol sym = (IonSymbol) _next;
                if (sym.isNullValue()) {
                    // there are no null values we will consume here
                    break;
                }
                int sid = sym.getSymbolId();
                if (sid == UNKNOWN_SYMBOL_ID) {
                    String name = sym.stringValue();
                    if (name != null) {
                        sid = _system_symtab.findSymbol(name);
                    }
                }
                if (sid == ION_1_0_SID && _next.getTypeAnnotationSymbols().length == 0) {
                    // $ion_1_0 is read as an IVM only if it is not annotated
                    SymbolTable symbols = _system_symtab;
                    _symbols = symbols;
                    push_symbol_table(symbols);
                    _next = null;
                    continue;
                }
            } else if (IonType.STRUCT.equals(next_type) && _next.findTypeAnnotation(ION_SYMBOL_TABLE) == 0) {
                assert (_next instanceof IonStruct);
                // read a local symbol table
                IonReader reader = new IonReaderTreeUserX(_next, _catalog, _lstFactory);
                SymbolTable symtab = _lstFactory.newLocalSymtab(_catalog, reader, false);
                _symbols = symtab;
                push_symbol_table(symtab);
                _next = null;
                continue;
            }
        }
        // so this is a value the user gets
        break;
    }
    return (next_type != null);
}
Also used : IonSymbol(com.amazon.ion.IonSymbol) IonStruct(com.amazon.ion.IonStruct) IonType(com.amazon.ion.IonType) IonDatagram(com.amazon.ion.IonDatagram) IonReader(com.amazon.ion.IonReader) SymbolTable(com.amazon.ion.SymbolTable)

Example 49 with IonDatagram

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

the class IonReaderLookaheadBufferTest method skipOversizedValues.

@Test
public void skipOversizedValues() throws Exception {
    if (initialBufferSize == null) {
        // of the buffer is unlimited. Skip.
        return;
    }
    // 14 values ranging in size from 1-14 bytes. There are 14 - x values larger than x bytes.
    TestUtils.BinaryIonAppender appender = new TestUtils.BinaryIonAppender().append(// boolean true
    0x11, // int 0 (overpadded)
    0x21, // int 0 (overpadded)
    0x00, // int 1 (overpadded)
    0x22, // int 1 (overpadded)
    0x00, // int 1 (overpadded)
    0x01, // int -2 (overpadded)
    0x33, // int -2 (overpadded)
    0x00, // int -2 (overpadded)
    0x00, // int -2 (overpadded)
    0x02, // float 0e0
    0x44, // float 0e0
    0x00, // float 0e0
    0x00, // float 0e0
    0x00, // float 0e0
    0x00, // decimal 0d0 (overpadded)
    0x55, // decimal 0d0 (overpadded)
    0x00, // decimal 0d0 (overpadded)
    0x00, // decimal 0d0 (overpadded)
    0x80, // decimal 0d0 (overpadded)
    0x00, // decimal 0d0 (overpadded)
    0x00, // timestamp 0001-01-01T00:00Z
    0x66, // timestamp 0001-01-01T00:00Z
    0x80, // timestamp 0001-01-01T00:00Z
    0x81, // timestamp 0001-01-01T00:00Z
    0x81, // timestamp 0001-01-01T00:00Z
    0x81, // timestamp 0001-01-01T00:00Z
    0x80, // timestamp 0001-01-01T00:00Z
    0x80, // symbol 0 (overpadded length and value)
    0x7E, // symbol 0 (overpadded length and value)
    0x00, // symbol 0 (overpadded length and value)
    0x00, // symbol 0 (overpadded length and value)
    0x00, // symbol 0 (overpadded length and value)
    0x00, // symbol 0 (overpadded length and value)
    0x82, // symbol 0 (overpadded length and value)
    0x00, // symbol 0 (overpadded length and value)
    0x00, // string abcdefgh
    0x88, // string abcdefgh
    'a', // string abcdefgh
    'b', // string abcdefgh
    'c', // string abcdefgh
    'd', // string abcdefgh
    'e', // string abcdefgh
    'f', // string abcdefgh
    'g', // string abcdefgh
    'h', // clob abcdefghi
    0x99, // clob abcdefghi
    'a', // clob abcdefghi
    'b', // clob abcdefghi
    'c', // clob abcdefghi
    'd', // clob abcdefghi
    'e', // clob abcdefghi
    'f', // clob abcdefghi
    'g', // clob abcdefghi
    'h', // clob abcdefghi
    'i', // blob
    0xAA, // blob
    0x01, // blob
    0x02, // blob
    0x03, // blob
    0x04, // blob
    0x05, // blob
    0x06, // blob
    0x07, // blob
    0x08, // blob
    0x09, // blob
    0x0A, // list with ten int 0
    0xBB, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, // sexp with ten decimal 0d0
    0xCC, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, // struct with system symbol field names and int 0 values (some overpadded)
    0xDD, 0x84, 0x20, 0x85, 0x20, 0x86, 0x20, 0x87, 0x21, 0x00, 0x88, 0x22, 0x00, 0x00);
    byte[] data = appender.toByteArray();
    IonDatagram expectedValues = SYSTEM.getLoader().load(data);
    ;
    for (int maxValueSize : Arrays.asList(5, 7, 8, 10, 13)) {
        if (maxValueSize < initialBufferSize) {
            // This would violate the IonBufferConfiguration contract; no need to test here.
            continue;
        }
        ByteArrayInputStream input = new ByteArrayInputStream(data);
        RecordingEventHandler eventHandler = new RecordingEventHandler();
        IonBufferConfiguration.Builder builder = IonBufferConfiguration.Builder.standard().withMaximumBufferSize(maxValueSize).onOversizedValue(eventHandler).onOversizedSymbolTable(eventHandler).onData(eventHandler).withInitialBufferSize(initialBufferSize);
        IonReaderLookaheadBuffer lookahead = new IonReaderLookaheadBuffer(builder.build(), input);
        IonReader reader = null;
        try {
            int valueIndex = 0;
            while (true) {
                lookahead.fillInput();
                if (lookahead.moreDataRequired()) {
                    break;
                }
                if (reader == null) {
                    reader = lookahead.newIonReader(IonReaderBuilder.standard());
                }
                assertNotNull(reader.next());
                assertTrue(Equivalence.ionEquals(expectedValues.get(valueIndex), SYSTEM.newValue(reader)));
                valueIndex++;
            }
            // There is one value per size. For a max size of x, `moreDataRequired` should be false x times.
            assertEquals(valueIndex, maxValueSize);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        int expectedOversizedValues = expectedValues.size() - maxValueSize;
        assertEquals(String.format("Expect %d oversized values for max size %d", expectedOversizedValues, maxValueSize), expectedOversizedValues, eventHandler.oversizedValueCount);
        assertEquals(0, eventHandler.oversizedSymbolTableCount);
        ResizingPipedInputStream buffer = (ResizingPipedInputStream) lookahead.getPipe();
        // The buffer grows in increments of its initial buffer size. Therefore, the final capacity will be at
        // most `initialBufferSize` larger than the lookahead wrapper's configured max value size.
        assertTrue(buffer.capacity() <= maxValueSize + buffer.getInitialBufferSize());
    }
}
Also used : TestUtils(com.amazon.ion.TestUtils) IonBufferConfiguration(com.amazon.ion.IonBufferConfiguration) IonDatagram(com.amazon.ion.IonDatagram) ByteArrayInputStream(java.io.ByteArrayInputStream) IonReader(com.amazon.ion.IonReader) Test(org.junit.Test)

Example 50 with IonDatagram

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

the class OptimizedBinaryWriterLengthPatchingTest method testOptimizedWriteValueBetweenContainers.

/**
 * Test copy optimized writeValue when the IonReader is stepped-in.
 */
@Test
public void testOptimizedWriteValueBetweenContainers() throws Exception {
    String body = // struct
    "{ fred_1: 123, fred_1: fred_1, fred_1: fred_1::null } " + // list
    "[ 123, fred_1, fred_1::null ] " + // sexp
    "( 123 fred_1 fred_1::null ) " + // deeply nested list
    "[[[ 123, fred_1, fred_1::null ]]]" + // list
    "[ 123, fred_1, fred_1::null ]";
    byte[] source = encode(body);
    ir = makeReaderProxy(source);
    iw = makeWriter();
    // Prime the local symtab so it matches the source's symtab
    iw.writeSymbol("fred_1");
    // === Struct ===
    copyContainerElements();
    // === List ===
    copyContainerElements();
    // === Sexp ===
    copyContainerElements();
    // === deeply nested List ===
    ir.next();
    ir.stepIn();
    {
        ir.next();
        ir.stepIn();
        {
            copyContainerElements();
        }
        ir.stepOut();
    }
    ir.stepOut();
    // === copy to a stepped-in-sexp writer ===
    iw.stepIn(IonType.SEXP);
    {
        copyContainerElements();
    }
    iw.stepOut();
    // Create the expected Ion data as text, which will be loaded into
    // a datagram later for equality check.
    StringBuilder sb = new StringBuilder();
    // scalars in each container
    String scalars = "123 fred_1 fred_1::null ";
    // from priming of local symtab
    sb.append("fred_1 ");
    // from struct
    sb.append(scalars);
    // from list
    sb.append(scalars);
    // from sexp
    sb.append(scalars);
    // from deeply nested list
    sb.append(scalars);
    // stepped-in-sexp writer
    sb.append("(" + scalars + ")");
    String expectedIonText = sb.toString();
    IonDatagram expected = loader().load(expectedIonText);
    IonDatagram actual = loader().load(outputByteArray());
    assertIonEquals(expected, actual);
}
Also used : IonDatagram(com.amazon.ion.IonDatagram) 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