Skip to content

Commit cf28754

Browse files
authored
Merge pull request #34 from embulk/test-JsonPointerBasedFilter-with-sequence-of-jsons
Test JsonPointerBasedFilter with sequence of JSONs
2 parents 1dd9ad5 + 16e8e8f commit cf28754

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2023 The Embulk project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.embulk.util.json;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNull;
21+
22+
import com.fasterxml.jackson.core.JsonFactory;
23+
import com.fasterxml.jackson.core.JsonPointer;
24+
import com.fasterxml.jackson.core.JsonToken;
25+
import com.fasterxml.jackson.core.filter.FilteringParserDelegate;
26+
import com.fasterxml.jackson.core.filter.JsonPointerBasedFilter;
27+
import com.fasterxml.jackson.core.json.PackageVersion;
28+
import java.io.IOException;
29+
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.Test;
31+
32+
public class TestJacksonFilter {
33+
@Test
34+
public void testFilterSequenceSkippingNonMatch() throws IOException {
35+
// This |parser| returns only {"bar":"baz"} and {"bar":"qux"}, with just skipping {"xxx":{"yyy":"zzz"}}.
36+
final com.fasterxml.jackson.core.JsonParser parser = createFilteredParser(
37+
"{\"foo\":{\"bar\":\"baz\"}}{\"xxx\":{\"yyy\":\"zzz\"}}{\"foo\":{\"bar\":\"quux\"}}",
38+
JsonPointer.compile("/foo"));
39+
assertEquals(JsonToken.START_OBJECT, parser.nextToken());
40+
assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
41+
assertEquals("bar", parser.getValueAsString());
42+
assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
43+
assertEquals("baz", parser.getValueAsString());
44+
assertEquals(JsonToken.END_OBJECT, parser.nextToken());
45+
assertEquals(JsonToken.START_OBJECT, parser.nextToken());
46+
assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
47+
assertEquals("bar", parser.getValueAsString());
48+
assertEquals(JsonToken.VALUE_STRING, parser.nextToken());
49+
assertEquals("quux", parser.getValueAsString());
50+
assertEquals(JsonToken.END_OBJECT, parser.nextToken());
51+
assertNull(parser.nextToken());
52+
}
53+
54+
@BeforeAll
55+
static void printJacksonVersion() {
56+
System.out.println("Tested with Jackson: " + PackageVersion.VERSION.toString());
57+
}
58+
59+
private static com.fasterxml.jackson.core.JsonParser createFilteredParser(
60+
final String json, final JsonPointer jsonPointer) throws IOException {
61+
final JsonFactory factory = new JsonFactory();
62+
factory.enable(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
63+
factory.enable(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS);
64+
65+
return new FilteringParserDelegate(
66+
factory.createParser(json),
67+
new JsonPointerBasedFilter(jsonPointer),
68+
false, // TODO: Use com.fasterxml.jackson.core.filter.TokenFilter.Inclusion since Jackson 2.12.
69+
true // Allow multiple matches
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)