Search in sources :

Example 26 with IonStruct

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

the class IonReaderTextSystemX method getIonValue.

public IonValue getIonValue(IonSystem sys) {
    if (isNullValue()) {
        switch(_value_type) {
            case NULL:
                return sys.newNull();
            case BOOL:
                return sys.newNullBool();
            case INT:
                return sys.newNullInt();
            case FLOAT:
                return sys.newNullFloat();
            case DECIMAL:
                return sys.newNullDecimal();
            case TIMESTAMP:
                return sys.newNullTimestamp();
            case SYMBOL:
                return sys.newNullSymbol();
            case STRING:
                return sys.newNullString();
            case CLOB:
                return sys.newNullClob();
            case BLOB:
                return sys.newNullBlob();
            case LIST:
                return sys.newNullList();
            case SEXP:
                return sys.newNullSexp();
            case STRUCT:
                return sys.newNullString();
            default:
                throw new IonException("unrecognized type encountered");
        }
    }
    switch(_value_type) {
        case NULL:
            return sys.newNull();
        case BOOL:
            return sys.newBool(booleanValue());
        case INT:
            return sys.newInt(longValue());
        case FLOAT:
            return sys.newFloat(doubleValue());
        case DECIMAL:
            return sys.newDecimal(decimalValue());
        case TIMESTAMP:
            IonTimestamp t = sys.newNullTimestamp();
            Timestamp ti = timestampValue();
            t.setValue(ti);
            return t;
        case SYMBOL:
            return sys.newSymbol(stringValue());
        case STRING:
            return sys.newString(stringValue());
        case CLOB:
            IonClob clob = sys.newNullClob();
            // FIXME inefficient: both newBytes and setBytes copy the data
            clob.setBytes(newBytes());
            return clob;
        case BLOB:
            IonBlob blob = sys.newNullBlob();
            // FIXME inefficient: both newBytes and setBytes copy the data
            blob.setBytes(newBytes());
            return blob;
        case LIST:
            IonList list = sys.newNullList();
            fillContainerList(sys, list);
            return list;
        case SEXP:
            IonSexp sexp = sys.newNullSexp();
            fillContainerList(sys, sexp);
            return sexp;
        case STRUCT:
            IonStruct struct = sys.newNullStruct();
            fillContainerStruct(sys, struct);
            return struct;
        default:
            throw new IonException("unrecognized type encountered");
    }
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonSexp(com.amazon.ion.IonSexp) IonException(com.amazon.ion.IonException) IonTimestamp(com.amazon.ion.IonTimestamp) IonList(com.amazon.ion.IonList) IonClob(com.amazon.ion.IonClob) IonBlob(com.amazon.ion.IonBlob) Timestamp(com.amazon.ion.Timestamp) IonTimestamp(com.amazon.ion.IonTimestamp)

Example 27 with IonStruct

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

the class IonWriterSystemTree method append.

private void append(IonValue value) {
    try {
        super.startValue();
    } catch (IOException e) {
        // Shouldn't happen
        throw new IonException(e);
    }
    if (hasAnnotations()) {
        SymbolToken[] annotations = getTypeAnnotationSymbols();
        // TODO this makes an extra copy of the array
        ((_Private_IonValue) value).setTypeAnnotationSymbols(annotations);
        this.clearAnnotations();
    }
    if (_in_struct) {
        SymbolToken sym = assumeFieldNameSymbol();
        IonStruct struct = (IonStruct) _current_parent;
        struct.add(sym, value);
        this.clearFieldName();
    } else {
        ((IonSequence) _current_parent).add(value);
    }
}
Also used : IonStruct(com.amazon.ion.IonStruct) SymbolToken(com.amazon.ion.SymbolToken) IonException(com.amazon.ion.IonException) IonSequence(com.amazon.ion.IonSequence) IOException(java.io.IOException)

Example 28 with IonStruct

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

the class SimpleCatalogTest method testBenchmark.

@SuppressWarnings("unchecked")
@Test
public void testBenchmark() {
    Map m = new HashMap();
    String s = "hello";
    m.put("This is a test String.", true);
    m.put(s, true);
    m.put("true", true);
    m.put("false", false);
    m.put("Something", null);
    m.put("12242.124598129", 12242.124598129);
    m.put("long", (long) 9326);
    m.put("12", 12);
    m.put("Almost Done.", true);
    m.put("Date", new Date(-10000));
    HashMap<String, String> hmap = new HashMap();
    for (int i = 0; i < 10; i++) {
        hmap.put("Key " + i, "value " + i);
    }
    TreeMap<String, String> tmap = new TreeMap();
    for (int i = 0; i < 10; i++) {
        tmap.put("Key " + i, "value " + i);
    }
    m.put("hmap", hmap);
    m.put("tmap", tmap);
    IonSystem sys = system();
    IonStruct i_tmap, i_hmap, i_map;
    Set<Entry<String, String>> set;
    i_tmap = sys.newEmptyStruct();
    set = tmap.entrySet();
    for (Entry<String, String> e : set) {
        IonString is = sys.newString(e.getValue());
        i_tmap.add(e.getKey(), is);
    }
    i_hmap = sys.newEmptyStruct();
    set = hmap.entrySet();
    for (Entry<String, String> e : set) {
        IonString is = sys.newString(e.getValue());
        i_hmap.add(e.getKey(), is);
    }
    i_map = sys.newEmptyStruct();
    set = tmap.entrySet();
    for (Entry<String, String> e : set) {
        Object val = e.getValue();
        IonValue ival;
        if (val instanceof String) {
            ival = sys.newString((String) val);
        } else if (e.getKey().equals("tmap")) {
            ival = i_tmap;
        } else if (e.getKey().equals("hmap")) {
            ival = i_hmap;
        } else {
            throw new RuntimeException("ACK! there's something in this map I don't understand!");
        }
        i_map.add(e.getKey(), ival);
    }
    IonDatagram dg = sys.newDatagram(i_map);
    byte[] bytes = dg.getBytes();
    IonValue v2 = sys.singleValue(bytes);
    assertNotNull(v2);
}
Also used : IonValue(com.amazon.ion.IonValue) IonSystem(com.amazon.ion.IonSystem) HashMap(java.util.HashMap) IonString(com.amazon.ion.IonString) TreeMap(java.util.TreeMap) Date(java.util.Date) Entry(java.util.Map.Entry) IonStruct(com.amazon.ion.IonStruct) IonString(com.amazon.ion.IonString) IonDatagram(com.amazon.ion.IonDatagram) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Test(org.junit.Test)

Example 29 with IonStruct

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

the class EquivalenceTest method testFieldEquals1.

// TODO amzn/ion-java/issues/58 : Remove the ignore annotation from this test after
// making the required changes to Equivalence.Field.hashCode.
@Ignore
@Test
public void testFieldEquals1() {
    IonValue v1 = oneValue("1");
    IonValue v2 = oneValue("2");
    IonValue v3 = oneValue("3");
    IonStruct struct = system().newEmptyStruct();
    String fieldName = "a";
    struct.add(fieldName, v1);
    struct.add(fieldName, v2);
    struct.add(fieldName, v3);
    assertEquals(3, struct.size());
    Equivalence.Configuration configuration = new Equivalence.Configuration(new Equivalence.Builder().withStrict(true));
    Equivalence.Field f1 = new Equivalence.Field(v1, configuration);
    Equivalence.Field f2 = new Equivalence.Field(v2, configuration);
    Equivalence.Field f3 = new Equivalence.Field(v3, configuration);
    assertFalse(f1.equals(f2));
    assertFalse(f1.equals(f3));
    assertFalse(f2.equals(f3));
    // Simple test that hashCode() is not badly implemented, so that fields with same
    // field names but are not equals() do not result in same hash codes.
    assertTrue(f1.hashCode() != f2.hashCode());
    assertTrue(f1.hashCode() != f3.hashCode());
}
Also used : IonValue(com.amazon.ion.IonValue) IonStruct(com.amazon.ion.IonStruct) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 30 with IonStruct

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

the class PrinterTest method testPrintingStruct.

@Test
public void testPrintingStruct() throws Exception {
    IonStruct value = system().newNullStruct();
    checkNullRendering("null.struct", value);
    value.put("foo", system().newNull());
    checkRendering("{foo:null}", value);
    // TODO this is too strict, order shouldn't matter.
    value.put("123", system().newNull());
    checkRendering("{foo:null,'123':null}", value);
    value.add("foo", int123());
    checkRendering("{foo:null,'123':null,foo:123}", value);
    myPrinter.setPrintSymbolAsString(true);
    checkRendering("{\"foo\":null,\"123\":null,\"foo\":123}", value);
    value = (IonStruct) oneValue("{}");
    checkRendering("{}", value);
    value.addTypeAnnotation("an");
    checkRendering("an::{}", value);
    value = (IonStruct) oneValue("an::{$0:null}");
    value.addTypeAnnotation("\u0007");
    value.put("A\u0000", system().newInt(12));
    checkRendering("an::'\\a'::{\"$0\":null,\"A\\0\":12}", value);
    myPrinter.setPrintStringAsJson(true);
    checkRendering("an::'\\a'::{\"$0\":null,\"A\\u0000\":12}", value);
}
Also used : IonStruct(com.amazon.ion.IonStruct) Test(org.junit.Test) IntTest(com.amazon.ion.IntTest) BlobTest(com.amazon.ion.BlobTest) ClobTest(com.amazon.ion.ClobTest)

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