{
String input = "{ \"aKey\" : [ 1, 2 ], \"bKey\" : [ 1.0, 2.0 ] }";
Object o = jsonToObject(input);
// use filterBy(alwaysTrue()) to make sure that FilterIterator.skipToSibling is exercised.
DataIterator it = Builder.create(o, null, IterationOrder.PRE_ORDER).filterBy(alwaysTrue()).dataIterator();
DataElement e;
boolean onlyDataList = false;
while ((e = it.next()) != null)
{
if (onlyDataList == false && e.getValue() instanceof DataList == false)
{
continue;
}
assertTrue(e.getValue() instanceof DataList);
onlyDataList = true;
it.skipToSibling();
}
assertTrue(onlyDataList);
}
// pre-order skipToSibling on an DataList
{
String input = "[ { \"aKey\" : 1 }, { \"bKey\" : 2.0 } ]";
Object o = jsonToObject(input);
DataIterator it = Builder.create(o, null, IterationOrder.PRE_ORDER).dataIterator();
DataElement e;
boolean onlyDataMap = false;
while ((e = it.next()) != null)
{
if (onlyDataMap == false && e.getValue() instanceof DataMap == false)
{
continue;
}
assertTrue(e.getValue() instanceof DataMap);
onlyDataMap = true;
it.skipToSibling();
}
assertTrue(onlyDataMap);
}
// pre-order skipToSibling is no-op, because there is no children
{
String input = "[ 1, 2, 3 ]";
Object o = jsonToObject(input);
DataIterator it = Builder.create(o, null, IterationOrder.PRE_ORDER).dataIterator();
DataElement e;
boolean onlyInteger = false;
while ((e = it.next()) != null)
{
if (onlyInteger == false && e.getValue() instanceof Integer == false)
{
continue;
}
assertTrue(e.getValue() instanceof Integer);
onlyInteger = true;
it.skipToSibling();
}
assertTrue(onlyInteger);
}
// pre-order skipToSibling is no-op, because there is no children
{
String input = "{ \"a\" : 1, \"b\" : 2, \"c\" : 3 }";
Object o = jsonToObject(input);
DataIterator it = Builder.create(o, null, IterationOrder.PRE_ORDER).dataIterator();
DataElement e;
boolean onlyInteger = false;
while ((e = it.next()) != null)
{
if (onlyInteger == false && e.getValue() instanceof Integer == false)
{
continue;
}
assertTrue(e.getValue() instanceof Integer);
onlyInteger = true;
it.skipToSibling();
}
assertTrue(onlyInteger);
}
// post-order skipToSibling
{
String input = "{ \"aKey\" : [ 1, 2 ], \"bKey\" : [ 1.0, 2.0 ] }";
Object o = jsonToObject(input);
DataElement e;
DataIterator it = Builder.create(o, null, IterationOrder.POST_ORDER).dataIterator();
List<String> pathsWithSkip = new ArrayList<String>();
while ((e = it.next()) != null)
{
pathsWithSkip.add(e.pathAsString());
it.skipToSibling();
}
it = Builder.create(o, null, IterationOrder.POST_ORDER).dataIterator();
List<String> pathsWithoutSkip = new ArrayList<String>();
while ((e = it.next()) != null)
{
pathsWithoutSkip.add(e.pathAsString());
}
assertEquals(pathsWithSkip, pathsWithoutSkip);
}
}