Search in sources :

Example 56 with IonStruct

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

the class TextWriterTest method testWritingLongStrings.

@Test
public void testWritingLongStrings() throws Exception {
    options = IonTextWriterBuilder.standard();
    options.setInitialIvmHandling(SUPPRESS);
    options.setLongStringThreshold(5);
    // This is tricky because we must avoid triple-quoting multiple
    // long strings in such a way that they'd get concatenated together!
    IonDatagram dg = system().newDatagram();
    dg.add().newNullString();
    dg.add().newString("hello");
    dg.add().newString("hello\nnurse");
    dg.add().newString("goodbye").addTypeAnnotation("a");
    dg.add().newString("what's\nup\ndoc");
    expectRendering("null.string \"hello\" '''hello\nnurse''' a::'''goodbye''' \"what's\\nup\\ndoc\"", dg);
    dg.clear();
    dg.add().newString("looong");
    IonSequence seq = dg.add().newEmptySexp();
    seq.add().newString("looong");
    seq.add().newString("hello");
    seq.add().newString("hello\nnurse");
    seq.add().newString("goodbye").addTypeAnnotation("a");
    seq.add().newString("what's\nup\ndoc");
    expectRendering("'''looong''' ('''looong''' \"hello\" '''hello\nnurse''' a::'''goodbye''' \"what's\\nup\\ndoc\")", dg);
    dg.clear();
    dg.add().newString("looong");
    seq = dg.add().newEmptyList();
    seq.add().newString("looong");
    seq.add().newString("hello");
    seq.add().newString("hello\nnurse");
    seq.add().newString("what's\nup\ndoc");
    expectRendering("'''looong''' ['''looong''',\"hello\",'''hello\n" + "nurse''','''what\\'s\n" + "up\n" + "doc''']", dg);
    options.setLongStringThreshold(0);
    expectRendering("\"looong\" [\"looong\",\"hello\",\"hello\\nnurse\",\"what's\\nup\\ndoc\"]", dg);
    options.setLongStringThreshold(5);
    dg.clear();
    dg.add().newString("looong");
    IonStruct struct = dg.add().newEmptyStruct();
    struct.add("a").newString("looong");
    struct.add("b").newString("hello");
    struct.add("c").newString("hello\nnurse");
    struct.add("d").newString("what's\nup\ndoc");
    expectRendering("'''looong''' {a:'''looong''',b:\"hello\",c:'''hello\n" + "nurse''',d:'''what\\'s\n" + "up\n" + "doc'''}", dg);
    options.withPrettyPrinting();
    expectRendering(// TODO amzn/ion-java/issues/57 determine if these really should be platform independent newlines
    format("%n" + "'''looong'''%n" + "{%n" + "  a:'''looong''',%n" + "  b:\"hello\",%n" + "  c:'''hello\n" + "nurse''',%n" + "  d:'''what\\'s\n" + "up\n" + "doc'''%n" + "}"), dg);
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonDatagram(com.amazon.ion.IonDatagram) IonSequence(com.amazon.ion.IonSequence) Test(org.junit.Test)

Example 57 with IonStruct

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

the class TreeReaderTest method testReadingReadOnly.

@Test
public void testReadingReadOnly() {
    IonDatagram dg = loader().load("{hello:hello}");
    // Make just part of the datagram read-only
    IonStruct s = (IonStruct) dg.get(0);
    s.makeReadOnly();
    IonReader r = system().newReader(s);
    TestUtils.deepRead(r);
    r = system().newReader(dg);
    TestUtils.deepRead(r);
    // Now the whole thing
    dg.makeReadOnly();
    r = system().newReader(dg);
    TestUtils.deepRead(r);
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonDatagram(com.amazon.ion.IonDatagram) IonReader(com.amazon.ion.IonReader) Test(org.junit.Test)

Example 58 with IonStruct

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

the class EquivalenceTest method builderWithEpsilon.

@Test
public void builderWithEpsilon() {
    Equivalence equivalence = new Equivalence.Builder().withEpsilon(1e-6).build();
    IonFloat v1 = ionFloat(3.14);
    IonFloat v2 = ionFloat(3.14 + 1e-7);
    assertTrue(equivalence.ionValueEquals(v1, v2));
    assertTrue(equivalence.ionValueEquals(v2, v1));
    IonStruct struct1 = system().newEmptyStruct();
    struct1.add("foo", v1.clone());
    IonStruct struct2 = system().newEmptyStruct();
    struct2.add("foo", v2.clone());
    assertTrue(equivalence.ionValueEquals(struct1, struct2));
    assertTrue(equivalence.ionValueEquals(struct2, struct1));
    IonList list1 = system().newList(v1.clone());
    IonList list2 = system().newList(v2.clone());
    assertTrue(equivalence.ionValueEquals(list1, list2));
    assertTrue(equivalence.ionValueEquals(list2, list1));
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonList(com.amazon.ion.IonList) IonFloat(com.amazon.ion.IonFloat) Test(org.junit.Test)

Example 59 with IonStruct

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

the class EquivalenceTest method builderWithoutEpsilon.

@Test
public void builderWithoutEpsilon() {
    Equivalence equivalence = new Equivalence.Builder().build();
    IonFloat v1 = ionFloat(3.14);
    IonFloat v2 = ionFloat(3.14 + 1e-7);
    assertFalse(equivalence.ionValueEquals(v1, v2));
    assertFalse(equivalence.ionValueEquals(v2, v1));
    IonStruct struct1 = system().newEmptyStruct();
    struct1.add("foo", v1.clone());
    IonStruct struct2 = system().newEmptyStruct();
    struct2.add("foo", v2.clone());
    assertFalse(equivalence.ionValueEquals(struct1, struct2));
    assertFalse(equivalence.ionValueEquals(struct2, struct1));
    IonList list1 = system().newList(v1.clone());
    IonList list2 = system().newList(v2.clone());
    assertFalse(equivalence.ionValueEquals(list1, list2));
    assertFalse(equivalence.ionValueEquals(list2, list1));
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonList(com.amazon.ion.IonList) IonFloat(com.amazon.ion.IonFloat) Test(org.junit.Test)

Example 60 with IonStruct

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

the class IonStructLite method doClone.

private IonStruct doClone(boolean keep, String... fieldNames) {
    IonStruct clone;
    if (isNullValue()) {
        clone = getSystem().newNullStruct();
    } else {
        Set<String> fields = new HashSet<String>(Arrays.asList(fieldNames));
        if (keep && fields.contains(null)) {
            throw new NullPointerException("Can't retain an unknown field name");
        }
        clone = getSystem().newEmptyStruct();
        for (IonValue value : this) {
            SymbolToken fieldNameSymbol = value.getFieldNameSymbol();
            String fieldName = fieldNameSymbol.getText();
            if (fields.contains(fieldName) == keep) {
                // This ensures that we don't copy an unknown field name.
                fieldName = value.getFieldName();
                clone.add(fieldName, value.clone());
            }
        }
    }
    clone.setTypeAnnotationSymbols(getTypeAnnotationSymbols());
    return clone;
}
Also used : IonValue(com.amazon.ion.IonValue) IonStruct(com.amazon.ion.IonStruct) SymbolToken(com.amazon.ion.SymbolToken) HashSet(java.util.HashSet)

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