Java Code Examples for org.ojai.FieldPath#parseFrom()

The following examples show how to use org.ojai.FieldPath#parseFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestFieldPath.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyFieldPath() {
  FieldSegment root = EMPTY.getRootSegment();
  assertNotNull(root);
  assertTrue(root.isLastPath());
  assertTrue(root.isLeaf());
  assertEquals("", EMPTY.asPathString());

  FieldPath fp = FieldPath.parseFrom("``");
  assertSame(fp, EMPTY);

  fp = FieldPath.parseFrom("\"\"");
  assertSame(fp, EMPTY);

  fp = FieldPath.parseFrom("");
  assertSame(fp, EMPTY);
}
 
Example 2
Source File: TestFieldPath.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testFieldPathEscapedInvalidSequences() {
  String[] validSequences = {"\\`", "\\\"", "\\\\", "\\.", "\\[", "\\]"};
  String sequence = null;
  try {
    for (String validSequence : validSequences) {
      sequence = validSequence;
      FieldPath.parseFrom(validSequence);

      // quoted with double-quote
      sequence = "\"" + validSequence + "\"";
      FieldPath.parseFrom(sequence);

      // quoted with back-tick
      sequence = "`" + validSequence + "`";
      FieldPath.parseFrom(sequence);
    }
  } catch (IllegalArgumentException e) {
    fail("FieldPath parsing failed for sequence: " + hexDump(sequence));
  }
}
 
Example 3
Source File: TestFieldPath.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testFieldPathEscapedWithoutQuote() {
  FieldPath fp = FieldPath.parseFrom("a\\.b\\[4\\]");
  Iterator<FieldSegment> segItr = fp.iterator();

  assertTrue(segItr.hasNext());
  FieldSegment seg = segItr.next();
  assertTrue(seg.isNamed());
  assertEquals("a.b[4]", seg.getNameSegment().getName());
  assertFalse(segItr.hasNext());

  assertEquals("a\\.b\\[4\\]", fp.asPathString());
  assertEquals("a\\.b\\[4\\]", fp.toString());
  assertEquals("a\\.b\\[4\\]", fp.asPathString(false));
  assertEquals("\"a.b[4]\"", fp.asPathString(true));
  assertEquals("\"a\\.b\\[4\\]\"", fp.asJsonString());
}
 
Example 4
Source File: Fields.java    From ojai with Apache License 2.0 5 votes vote down vote up
public static FieldPath[] toFieldPathArray(@Nullable String... fieldPaths) {
  if (fieldPaths == null) {
    return null;
  } else if (fieldPaths.length == 0) {
    return EMPTRY_FIELDPATH_ARRAY;
  } else {
    final FieldPath[] fpArray = new FieldPath[fieldPaths.length];
    for (int i = 0; i < fieldPaths.length; i++) {
      fpArray[i] = FieldPath.parseFrom(fieldPaths[i]);
    }
    return fpArray;
  }
}
 
Example 5
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testSortOrder() {
  FieldPath fp0 = FieldPath.parseFrom("a[0]");
  FieldPath fp1 = FieldPath.parseFrom("a[1]");
  FieldPath fp2 = FieldPath.parseFrom("a.b");
  FieldPath fp3 = FieldPath.parseFrom("a.b.c");
  FieldPath fp4 = FieldPath.parseFrom("a.c");
  FieldPath fp5 = FieldPath.parseFrom("a.c[4]");

  assertTrue(fp0.compareTo(fp1) < 0);
  assertTrue(fp1.compareTo(fp2) < 0);
  assertTrue(fp2.compareTo(fp3) < 0);
  assertTrue(fp3.compareTo(fp4) < 0);
  assertTrue(fp4.compareTo(fp5) < 0);
}
 
Example 6
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testPathWithIlleagalArrayIndex() {
  char[] illegalArrayIndices = {0, 5, 16, 'a', '\u4251'};
  for (char ch : illegalArrayIndices) {
    try {
      FieldPath.parseFrom("a[" + ch + "]");
      fail("Parsing should fail for character with code " + (int) ch);
    } catch (IllegalArgumentException e) { }
  }
}
 
Example 7
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapedPathWithArraysEmptyIndex() {
  FieldPath fp = FieldPath.parseFrom("\"a.b\"[].c");
  assertTrue(fp.getRootSegment().isArray());
  assertTrue(fp.getRootSegment().getChild().isIndexed());
  assertTrue(fp.getRootSegment().getChild().isMap());
  assertTrue(fp.getRootSegment().getChild().getChild().isLeaf());
  assertEquals("\"a.b\"[].\"c\"", fp.asPathString(true));
  assertEquals("\"a.b\"[].c", fp.asPathString());
  assertEquals("\"\\\"a.b\\\"[].c\"", fp.asJsonString());
}
 
Example 8
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimplePathWithArraysEmptyIndex() {
  FieldPath fp = FieldPath.parseFrom("a.b[].c");
  assertTrue(fp.getRootSegment().isMap());
  assertTrue(fp.getRootSegment().getChild().isArray());
  assertTrue(fp.getRootSegment().getChild().getChild().isIndexed());
  assertTrue(fp.getRootSegment().getChild().getChild().isMap());
  assertTrue(fp.getRootSegment().getChild().getChild().getChild().isLeaf());
  assertEquals("\"a\".\"b\"[].\"c\"", fp.asPathString(true));
  assertEquals("a.b[].c", fp.asPathString());
  assertEquals("\"a.b[].c\"", fp.asJsonString());
}
 
Example 9
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimplePathWithNumericNameSegments() {
  FieldPath fp = FieldPath.parseFrom("1.23.4a");
  assertTrue(fp.getRootSegment().isMap());
  assertTrue(fp.getRootSegment().getChild().isMap());
  assertTrue(fp.getRootSegment().getChild().getChild().isNamed());
  assertTrue(fp.getRootSegment().getChild().getChild().isLeaf());
  assertEquals("\"1\".\"23\".\"4a\"", fp.asPathString(true));
  assertEquals("1.23.4a", fp.asPathString());
  assertEquals("\"1.23.4a\"", fp.asJsonString());
}
 
Example 10
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimplePathWithArrays() {
  FieldPath fp = FieldPath.parseFrom("a.b[3].c");
  assertTrue(fp.getRootSegment().isMap());
  assertTrue(fp.getRootSegment().getChild().isArray());
  assertTrue(fp.getRootSegment().getChild().getChild().isIndexed());
  assertTrue(fp.getRootSegment().getChild().getChild().isMap());
  assertTrue(fp.getRootSegment().getChild().getChild().getChild().isLeaf());
  assertEquals("\"a\".\"b\"[3].\"c\"", fp.asPathString(true));
  assertEquals("a.b[3].c", fp.asPathString());
}
 
Example 11
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloneWithNewParent() {
  FieldPath fp1 = FieldPath.parseFrom("a.b.c");
  FieldPath fp2 = fp1.cloneWithNewParent("v");
  assertEquals("v.a.b.c", fp2.asPathString());
  assertEquals(fp1.getRootSegment(), fp2.getRootSegment().getChild());
}
 
Example 12
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testQuotedEscapedPath() {
  FieldPath fp = FieldPath.parseFrom("\"the\\\"quick.brown\\\\fox\"");
  assertTrue(fp.getRootSegment().isLastPath());
  assertEquals("\"the\\\"quick.brown\\\\fox\"", fp.asPathString(true));
  assertEquals("\"the\\\"quick.brown\\\\fox\"", fp.asPathString(false));
  assertEquals("\"the\\\"quick.brown\\\\fox\"", fp.asPathString());
  assertEquals("\"\\\"the\\\\\"quick.brown\\\\fox\\\"\"", fp.asJsonString());
}
 
Example 13
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testQuotedPath() {
  FieldPath fp = FieldPath.parseFrom("\"the quick.brown fox\"");
  assertTrue(fp.getRootSegment().isLastPath());
  assertEquals("\"the quick.brown fox\"", fp.asPathString(true));
  assertEquals("\"the quick.brown fox\"", fp.asPathString());
}
 
Example 14
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testFieldPathUnicodeSequence() {
  // Java literal string "\u0041.\u0042", parsed by FieldPath grammar
  FieldPath fp = FieldPath.parseFrom("\\u0041.\\u0042");
  Iterator<FieldSegment> segItr = fp.iterator();

  assertTrue(segItr.hasNext());
  FieldSegment seg = segItr.next();
  assertTrue(seg.isNamed());
  assertEquals("A", seg.getNameSegment().getName());

  assertTrue(segItr.hasNext());
  seg = segItr.next();
  assertTrue(seg.isNamed());
  assertEquals("B", seg.getNameSegment().getName());

  // Unicode sequence parsed by Java literal String grammar
  fp = FieldPath.parseFrom("\u0041.\u0042");
  segItr = fp.iterator();

  assertTrue(segItr.hasNext());
  seg = segItr.next();
  assertTrue(seg.isNamed());
  assertEquals("A", seg.getNameSegment().getName());

  assertTrue(segItr.hasNext());
  seg = segItr.next();
  assertTrue(seg.isNamed());
  assertEquals("B", seg.getNameSegment().getName());
}
 
Example 15
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testPathWithSpace() {
  FieldPath fp = FieldPath.parseFrom("work phone.cell phone");
  assertFalse(fp.getRootSegment().isLeaf());
  assertTrue(fp.getRootSegment().getChild().isLeaf());
  assertEquals("\"work phone\".\"cell phone\"", fp.asPathString(true));
  assertEquals("work phone.cell phone", fp.asPathString());
  assertEquals("\"work phone.cell phone\"", fp.asJsonString());

  fp = FieldPath.parseFrom("a[ ]");
  assertEquals("\"a\"[]", fp.asPathString(true));
  assertEquals("a[]", fp.asPathString());
  assertEquals("\"a[]\"", fp.asJsonString());
}
 
Example 16
Source File: TestJsonDocumentEquals.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocumentEquals() throws IOException {
  String fieldPath = "`a|b`.`c,''-\\\"d`.` a!\\`#$%&'()*+,-/:;<=>?@`.`[]^_{|}~`";
  FieldPath fp = FieldPath.parseFrom(fieldPath);
  Document d1 = Json.newDocument();
  d1.set(fp, 10);
  Document d2 = Json.newDocument();
  d2.set(fp, 10);
  assertEquals(d1, d2);
}
 
Example 17
Source File: TestFieldPath.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testPathWithUnderscore() {
  FieldPath fp = FieldPath.parseFrom("work_phone");
  assertTrue(fp.getRootSegment().isLeaf());
  assertEquals("\"work_phone\"", fp.asPathString(true));
  assertEquals("work_phone", fp.asPathString());
  assertEquals("\"work_phone\"", fp.asJsonString());
}
 
Example 18
Source File: TypeMappedJsonDocumentReader.java    From ojai with Apache License 2.0 5 votes vote down vote up
private void calculateCurrentFieldPath() {
  StringBuilder sb = new StringBuilder();
  for (Object field : fieldSegmentStack) {
    if (field instanceof Number) {
      sb.append('[').append(field).append(']');
    } else {
      sb.append('`').append(field).append('`').append('.');
    }
  }
  currentFieldPath = FieldPath.parseFrom(sb.append('`').append(currentFieldName).append('`').toString());
}
 
Example 19
Source File: TestFieldPath.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testExceptionWhenBadIndex() {
  FieldPath.parseFrom("a.b.c[a]");
}
 
Example 20
Source File: TestFieldPath.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Test
public void testCanonicalForm() {
  FieldPath fp1 = FieldPath.parseFrom("a.b.\"c\"[4]");
  FieldPath fp2 = FieldPath.parseFrom("a.\"b\".c[4]");
  assertEquals(fp1, fp2);
}