Search in sources :

Example 36 with IonStruct

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

the class IonStructToMapObjectInspector method getMap.

@Override
public Map<?, ?> getMap(final Object data) {
    if (IonUtil.isIonNull((IonValue) data)) {
        return null;
    }
    final IonStruct struct = (IonStruct) data;
    // sets the initial size of the map to avoid growing as it's immutable while using the default HashMap load
    // factor to maintain the same collision performance
    final int size = (int) Math.ceil(struct.size() / DEFAULT_LOAD_FACTOR);
    final Map<String, IonValue> map = new HashMap<>(size, DEFAULT_LOAD_FACTOR);
    for (IonValue v : struct) {
        map.put(v.getFieldName(), v);
    }
    return map;
}
Also used : IonValue(com.amazon.ion.IonValue) IonStruct(com.amazon.ion.IonStruct) HashMap(java.util.HashMap)

Example 37 with IonStruct

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

the class IonStructToMapObjectInspector method getMapValueElement.

@Override
public Object getMapValueElement(final Object data, final Object key) {
    if (IonUtil.isIonNull((IonValue) data)) {
        return null;
    }
    if (key == null) {
        throw new IllegalArgumentException("key cannot be null");
    }
    final IonStruct struct = (IonStruct) data;
    final IonSymbol symbol = (IonSymbol) key;
    return struct.get(symbol.stringValue());
}
Also used : IonSymbol(com.amazon.ion.IonSymbol) IonStruct(com.amazon.ion.IonStruct)

Example 38 with IonStruct

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

the class DataBindRoundtripTest method testIonRoot.

@SuppressWarnings("rawtypes")
@Test
public void testIonRoot() throws IOException {
    String stringBean = "{a:'test',b:0.25,state:true,data:{{}}, sexp:(foo bar), nestedSexp:([foo2, foo3] (foo4 foo5)), sub:{value:'yellow'}, symbol:testSymbol, enumVal: B}";
    IonSystem system = IonSystemBuilder.standard().build();
    IonValue root = system.newLoader().load(stringBean).iterator().next();
    IonObjectMapper m = new IonObjectMapper();
    Bean bean = m.readValue(root, Bean.class);
    assertNotNull(bean);
    assertEquals(bean.a, "test");
    assertTrue(bean.b == 0.25);
    assertArrayEquals(new byte[0], bean.data);
    assertEquals(bean.state, true);
    assertNotNull(bean.sub);
    assertEquals("yellow", bean.sub.getValue());
    assertEquals("testSymbol", bean.symbol);
    assertEquals(TestEnum.B, bean.enumVal);
    assertEquals("foo", bean.sexp.get(0));
    assertEquals("bar", bean.sexp.get(1));
    assertEquals("foo2", ((List) bean.nestedSexp.get(0)).get(0));
    assertEquals("foo3", ((List) bean.nestedSexp.get(0)).get(1));
    assertEquals("foo4", ((List) bean.nestedSexp.get(1)).get(0));
    assertEquals("foo5", ((List) bean.nestedSexp.get(1)).get(1));
    IonValue subRoot = ((IonStruct) root).get("sub");
    subRoot.removeFromContainer();
    SubBean subbean = m.readValue(subRoot, SubBean.class);
    assertNotNull(subbean);
    assertEquals("yellow", subbean.getValue());
}
Also used : IonValue(com.amazon.ion.IonValue) IonSystem(com.amazon.ion.IonSystem) IonStruct(com.amazon.ion.IonStruct) Test(org.junit.Test)

Example 39 with IonStruct

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

the class DataBindWriteTest method testSimpleObjectWriteIon.

@Test
public void testSimpleObjectWriteIon() throws Exception {
    IonStruct struct = ion.newEmptyStruct();
    IonWriter writer = ion.newWriter(struct);
    writer.setFieldName("payload");
    new IonObjectMapper().writeValue(writer, new MyBean());
    writer.close();
    IonStruct expectedStruct = ion.newEmptyStruct();
    expectedStruct.put("payload", expectedMyBean.get(0).clone());
    assertEquals(expectedStruct, struct);
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonWriter(com.amazon.ion.IonWriter) Test(org.junit.Test)

Example 40 with IonStruct

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

the class IonStructToStructInspector method getStructFieldsDataAsList.

@Override
public List<Object> getStructFieldsDataAsList(final Object data) {
    if (isIonNull((IonValue) data)) {
        return null;
    }
    final IonStruct struct = (IonStruct) data;
    List<Object> list = new ArrayList<>(struct.size());
    for (IonValue v : struct) {
        list.add(v);
    }
    return list;
}
Also used : IonValue(com.amazon.ion.IonValue) IonStruct(com.amazon.ion.IonStruct) ArrayList(java.util.ArrayList)

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