Search in sources :

Example 21 with IonStruct

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

the class IonWriterTestCase method testWriteToWithFieldName.

@Test
public void testWriteToWithFieldName() throws Exception {
    IonStruct s = system().newEmptyStruct();
    IonNull n = s.add("f").newNull();
    iw = makeWriter();
    n.writeTo(iw);
    iw.stepIn(IonType.STRUCT);
    {
        n.writeTo(iw);
        iw.setFieldName("g");
        n.writeTo(iw);
    }
    iw.stepOut();
    IonReader reader = reread();
    assertEquals(IonType.NULL, reader.next());
    assertEquals(IonType.STRUCT, reader.next());
    assertIonEquals(oneValue("{f:null, g:null}"), system().newValue(reader));
    assertNull(reader.next());
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonReader(com.amazon.ion.IonReader) IonNull(com.amazon.ion.IonNull) Test(org.junit.Test)

Example 22 with IonStruct

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

the class IonWriterTestCase method testWriteValueCantCopyFieldName.

@Test
public void testWriteValueCantCopyFieldName() throws Exception {
    IonStruct data = struct("{a:{b:10}}");
    IonValue a = data.get("a");
    IonReader ir = system().newReader(a);
    ir.next();
    iw = makeWriter();
    iw.stepIn(IonType.STRUCT);
    // field name not set
    thrown.expect(IllegalStateException.class);
    iw.writeValue(ir);
}
Also used : IonValue(com.amazon.ion.IonValue) IonStruct(com.amazon.ion.IonStruct) IonReader(com.amazon.ion.IonReader) Test(org.junit.Test)

Example 23 with IonStruct

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

the class IonWriterTestCase method testWriteValueCopiesFieldName.

@Test
public void testWriteValueCopiesFieldName() throws Exception {
    IonStruct data = struct("{a:{b:10}}");
    IonReader ir = system().newReader(data);
    ir.next();
    ir.stepIn();
    expectNextField(ir, "a");
    iw = makeWriter();
    iw.stepIn(IonType.STRUCT);
    iw.writeValue(ir);
    iw.stepOut();
    assertEquals(data, reloadSingleValue());
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonReader(com.amazon.ion.IonReader) Test(org.junit.Test)

Example 24 with IonStruct

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

the class IonWriterTestCase method testFlushMidValue.

@Test
public void testFlushMidValue() throws Exception {
    iw = makeWriter();
    iw.addTypeAnnotation("a");
    iw.flush();
    iw.stepIn(IonType.STRUCT);
    iw.flush();
    iw.setFieldName("f");
    iw.flush();
    iw.addTypeAnnotation("a");
    iw.flush();
    iw.writeNull();
    iw.flush();
    iw.stepOut();
    iw.flush();
    IonStruct expected = struct("a::{f:a::null}");
    assertEquals(expected, reload().get(0));
}
Also used : IonStruct(com.amazon.ion.IonStruct) Test(org.junit.Test)

Example 25 with IonStruct

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

the class Equivalence method ionCompareToImpl.

private static int ionCompareToImpl(final IonValue v1, final IonValue v2, final Configuration configuration) {
    int result = 0;
    if (v1 == null || v2 == null) {
        if (v1 != null)
            result = 1;
        if (v2 != null)
            result = -1;
        // otherwise v1 == v2 == null and result == 0
        return result;
    }
    // check type
    IonType ty1 = v1.getType();
    IonType ty2 = v2.getType();
    result = ty1.compareTo(ty2);
    if (result == 0) {
        boolean bo1 = v1.isNullValue();
        boolean bo2 = v2.isNullValue();
        if (bo1 || bo2) {
            // the same type
            if (!bo1)
                result = 1;
            if (!bo2)
                result = -1;
        // othersize they're equal (and null values)
        } else {
            // value compare only if both are not null
            switch(ty1) {
                case NULL:
                    // never visited, precondition is that both are not null
                    break;
                case BOOL:
                    if (((IonBool) v1).booleanValue()) {
                        result = ((IonBool) v2).booleanValue() ? 0 : 1;
                    } else {
                        result = ((IonBool) v2).booleanValue() ? -1 : 0;
                    }
                    break;
                case INT:
                    result = ((IonInt) v1).bigIntegerValue().compareTo(((IonInt) v2).bigIntegerValue());
                    break;
                case FLOAT:
                    double double1 = ((IonFloat) v1).doubleValue();
                    double double2 = ((IonFloat) v2).doubleValue();
                    if (configuration.epsilon != null && (double1 == double2 || Math.abs(double1 - double2) <= configuration.epsilon)) {
                        result = 0;
                    } else {
                        result = Double.compare(double1, double2);
                    }
                    break;
                case DECIMAL:
                    // TODO amzn/ion-java/issues/26
                    assert !PUBLIC_COMPARISON_API;
                    result = Decimal.equals(((IonDecimal) v1).decimalValue(), ((IonDecimal) v2).decimalValue()) ? 0 : 1;
                    break;
                case TIMESTAMP:
                    if (configuration.isStrict) {
                        // TODO amzn/ion-java/issues/26
                        assert !PUBLIC_COMPARISON_API;
                        result = (((IonTimestamp) v1).timestampValue().equals(((IonTimestamp) v2).timestampValue()) ? 0 : 1);
                    } else {
                        // This is kind of lying here, the 'strict' boolean
                        // (if false) denotes ONLY that annotations are not
                        // check for equality. But what this is doing here is
                        // that it is also ignoring IonTimesamps' precision and
                        // local offset.
                        result = ((IonTimestamp) v1).timestampValue().compareTo(((IonTimestamp) v2).timestampValue());
                    }
                    break;
                case STRING:
                    result = (((IonText) v1).stringValue()).compareTo(((IonText) v2).stringValue());
                    break;
                case SYMBOL:
                    result = compareSymbolTokens(((IonSymbol) v1).symbolValue(), ((IonSymbol) v2).symbolValue());
                    break;
                case BLOB:
                case CLOB:
                    result = compareLobContents((IonLob) v1, (IonLob) v2);
                    break;
                case STRUCT:
                    // TODO amzn/ion-java/issues/26
                    assert !PUBLIC_COMPARISON_API;
                    result = compareStructs((IonStruct) v1, (IonStruct) v2, configuration);
                    break;
                case LIST:
                case SEXP:
                case DATAGRAM:
                    result = compareSequences((IonSequence) v1, (IonSequence) v2, configuration);
                    break;
            }
        }
    }
    // comparison, then we check the annotations
    if ((result == 0) && configuration.isStrict) {
        // check tuple equality over the annotations
        // (which are symbol tokens)
        result = compareAnnotations(v1.getTypeAnnotationSymbols(), v2.getTypeAnnotationSymbols());
    }
    return result;
}
Also used : IonType(com.amazon.ion.IonType) IonInt(com.amazon.ion.IonInt) IonSequence(com.amazon.ion.IonSequence) IonBool(com.amazon.ion.IonBool) IonSymbol(com.amazon.ion.IonSymbol) IonText(com.amazon.ion.IonText) IonStruct(com.amazon.ion.IonStruct) IonTimestamp(com.amazon.ion.IonTimestamp) IonLob(com.amazon.ion.IonLob) IonFloat(com.amazon.ion.IonFloat)

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