Search in sources :

Example 41 with IonStruct

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

the class IonJavaCli method parseStruct.

private static IonStruct parseStruct(ContainerContext containerContext, CompareContext compareContext, int end, boolean isFirst) {
    Event initEvent = isFirst ? compareContext.getEventStreamFirst().get(containerContext.getIndex()) : compareContext.getEventStreamSecond().get(containerContext.getIndex());
    if (initEvent.getEventType() != EventType.CONTAINER_START || initEvent.getIonType() != IonType.STRUCT) {
        return null;
    }
    IonStruct ionStruct = ION_SYSTEM.newEmptyStruct();
    while (containerContext.increaseIndex() < end) {
        Event event = isFirst ? compareContext.getEventStreamFirst().get(containerContext.getIndex()) : compareContext.getEventStreamSecond().get(containerContext.getIndex());
        EventType eventType = event.getEventType();
        if (eventType == EventType.CONTAINER_START) {
            switch(event.getIonType()) {
                case LIST:
                    ionStruct.add(event.getFieldName(), parseSequence(containerContext, compareContext, end, isFirst, ION_SYSTEM.newEmptyList()));
                    break;
                case STRUCT:
                    ionStruct.add(event.getFieldName(), parseStruct(containerContext, compareContext, end, isFirst));
                    break;
                case SEXP:
                    ionStruct.add(event.getFieldName(), parseSequence(containerContext, compareContext, end, isFirst, ION_SYSTEM.newEmptySexp()));
                    break;
            }
        } else if (eventType == EventType.CONTAINER_END) {
            break;
        } else if (eventType == EventType.STREAM_END) {
            throw new IonException("Invalid struct: eventStream ends without CONTAINER_END");
        } else {
            IonValue cloneValue = event.getValue().clone();
            cloneValue.setTypeAnnotationSymbols(event.getAnnotations());
            ionStruct.add(event.getFieldName(), cloneValue);
        }
    }
    return ionStruct;
}
Also used : IonValue(com.amazon.ion.IonValue) IonStruct(com.amazon.ion.IonStruct) EventType(com.amazon.tools.events.EventType) IonException(com.amazon.ion.IonException) Event(com.amazon.tools.events.Event)

Example 42 with IonStruct

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

the class IonIteratorImpl method readValue.

private IonValue readValue() {
    IonType type = _reader.getType();
    SymbolToken[] annotations = _reader.getTypeAnnotationSymbols();
    IonValue v;
    if (_reader.isNullValue()) {
        v = _valueFactory.newNull(type);
    } else {
        switch(type) {
            case NULL:
                // Handled above
                throw new IllegalStateException();
            case BOOL:
                v = _valueFactory.newBool(_reader.booleanValue());
                break;
            case INT:
                v = _valueFactory.newInt(_reader.bigIntegerValue());
                break;
            case FLOAT:
                v = _valueFactory.newFloat(_reader.doubleValue());
                break;
            case DECIMAL:
                v = _valueFactory.newDecimal(_reader.decimalValue());
                break;
            case TIMESTAMP:
                v = _valueFactory.newTimestamp(_reader.timestampValue());
                break;
            case STRING:
                v = _valueFactory.newString(_reader.stringValue());
                break;
            case SYMBOL:
                // TODO always pass the SID?  Is it correct?
                v = _valueFactory.newSymbol(_reader.symbolValue());
                break;
            case BLOB:
                {
                    IonLob lob = _valueFactory.newNullBlob();
                    lob.setBytes(_reader.newBytes());
                    v = lob;
                    break;
                }
            case CLOB:
                {
                    IonLob lob = _valueFactory.newNullClob();
                    lob.setBytes(_reader.newBytes());
                    v = lob;
                    break;
                }
            case STRUCT:
                {
                    IonStruct struct = _valueFactory.newEmptyStruct();
                    _reader.stepIn();
                    while (_reader.next() != null) {
                        SymbolToken name = _reader.getFieldNameSymbol();
                        IonValue child = readValue();
                        struct.add(name, child);
                    }
                    _reader.stepOut();
                    v = struct;
                    break;
                }
            case LIST:
                {
                    IonSequence seq = _valueFactory.newEmptyList();
                    _reader.stepIn();
                    while (_reader.next() != null) {
                        IonValue child = readValue();
                        seq.add(child);
                    }
                    _reader.stepOut();
                    v = seq;
                    break;
                }
            case SEXP:
                {
                    IonSequence seq = _valueFactory.newEmptySexp();
                    _reader.stepIn();
                    while (_reader.next() != null) {
                        IonValue child = readValue();
                        seq.add(child);
                    }
                    _reader.stepOut();
                    v = seq;
                    break;
                }
            default:
                throw new IllegalStateException();
        }
    }
    // TODO this is too late in the case of system reading
    // when v is a local symtab (it will get itself, not the prior symtab)
    SymbolTable symtab = _reader.getSymbolTable();
    ((_Private_IonValue) v).setSymbolTable(symtab);
    if (annotations.length != 0) {
        ((_Private_IonValue) v).setTypeAnnotationSymbols(annotations);
    }
    return v;
}
Also used : IonValue(com.amazon.ion.IonValue) IonStruct(com.amazon.ion.IonStruct) IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) IonLob(com.amazon.ion.IonLob) IonSequence(com.amazon.ion.IonSequence) SymbolTable(com.amazon.ion.SymbolTable)

Example 43 with IonStruct

use of com.amazon.ion.IonStruct 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 44 with IonStruct

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

the class SymbolTableStructCache method makeIonRepresentation.

/**
 * Create a new IonStruct representation of the symbol table.
 * @param factory the {@link ValueFactory} from which to construct the IonStruct.
 */
private void makeIonRepresentation(ValueFactory factory) {
    image = factory.newEmptyStruct();
    image.addTypeAnnotation(ION_SYMBOL_TABLE);
    if (importedTables.length > 0) {
        // The system symbol table may be the first import. If it is, skip it.
        int i = importedTables[0].isSystemTable() ? 1 : 0;
        if (i < importedTables.length) {
            IonList importsList = factory.newEmptyList();
            while (i < importedTables.length) {
                SymbolTable importedTable = importedTables[i];
                IonStruct importStruct = factory.newEmptyStruct();
                importStruct.add(NAME, factory.newString(importedTable.getName()));
                importStruct.add(VERSION, factory.newInt(importedTable.getVersion()));
                importStruct.add(MAX_ID, factory.newInt(importedTable.getMaxId()));
                importsList.add(importStruct);
                i++;
            }
            image.add(IMPORTS, importsList);
        }
    }
    if (symbolTable.getMaxId() > symbolTable.getImportedMaxId()) {
        Iterator<String> localSymbolIterator = symbolTable.iterateDeclaredSymbolNames();
        int sid = symbolTable.getImportedMaxId() + 1;
        while (localSymbolIterator.hasNext()) {
            addSymbol(localSymbolIterator.next(), sid);
            sid++;
        }
    }
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonList(com.amazon.ion.IonList) SymbolTable(com.amazon.ion.SymbolTable)

Example 45 with IonStruct

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

the class _Private_CurriedValueFactory method newEmptyStruct.

public IonStruct newEmptyStruct() {
    IonStruct v = myFactory.newEmptyStruct();
    handle(v);
    return v;
}
Also used : IonStruct(com.amazon.ion.IonStruct)

Aggregations

IonStruct (com.amazon.ion.IonStruct)68 Test (org.junit.Test)28 IonValue (com.amazon.ion.IonValue)18 IonDatagram (com.amazon.ion.IonDatagram)13 IonList (com.amazon.ion.IonList)13 IonReader (com.amazon.ion.IonReader)13 SymbolTable (com.amazon.ion.SymbolTable)13 IOException (java.io.IOException)8 IonString (com.amazon.ion.IonString)7 IonSystem (com.amazon.ion.IonSystem)6 IonException (com.amazon.ion.IonException)5 IonSequence (com.amazon.ion.IonSequence)5 IonType (com.amazon.ion.IonType)5 IonFloat (com.amazon.ion.IonFloat)4 IonSymbol (com.amazon.ion.IonSymbol)4 IonTimestamp (com.amazon.ion.IonTimestamp)4 IonWriter (com.amazon.ion.IonWriter)4 SymbolToken (com.amazon.ion.SymbolToken)4 ArrayList (java.util.ArrayList)4 IonBlob (com.amazon.ion.IonBlob)3