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);
}
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);
}
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));
}
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));
}
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;
}
Aggregations