/** * Adds include path query. */ public JsonSerializer include(final String include) { rules.include(new PathQuery(include, true)); return this; }
@Override public boolean accept(final Path value, final PathQuery rule, final boolean include) { return rule.matches(value); } };
@Test void testPathMatchingDifferences() { assertTrue(new PathQuery("one.two", true).matches(Path.parse("one"))); assertTrue(new PathQuery("one.two.three", true).matches(Path.parse("one"))); assertTrue(new PathQuery("one.two", true).matches(Path.parse("one.two"))); assertFalse(new PathQuery("one.two", true).matches(Path.parse("one.two.three"))); assertFalse(new PathQuery("one.two", false).matches(Path.parse("one"))); assertTrue(new PathQuery("one.two", false).matches(Path.parse("one.two"))); assertFalse(new PathQuery("one.two", false).matches(Path.parse("one.two.three"))); }
@Test void testSetIncludes() { JsonSerializer serializer = new JsonSerializer(); serializer.include("people.hobbies", "phones", "home", "people.resume"); assertEquals(4, serializer.rules.totalRules()); assertEquals("[people.hobbies]", serializer.rules.getRule(0).toString()); assertEquals("[phones]", serializer.rules.getRule(1).toString()); assertEquals("[home]", serializer.rules.getRule(2).toString()); assertEquals("[people.resume]", serializer.rules.getRule(3).toString()); }
@Test void testPathMatching() { assertTrue(new PathQuery("hello", true).matches(Path.parse("hello"))); assertFalse(new PathQuery("hello", true).matches(Path.parse("boom"))); assertTrue(new PathQuery("hello.world", true).matches(Path.parse("hello.world"))); assertFalse(new PathQuery("hello", true).matches(new Path())); assertTrue(new PathQuery("hello.*.world", true).matches(Path.parse("hello.jupiter.world"))); assertTrue(new PathQuery("hello.*.world", true).matches(Path.parse("hello.earth.moon.world"))); assertTrue(new PathQuery("*.class", true).matches(Path.parse("foo.class"))); assertTrue(new PathQuery("*.class", true).matches(Path.parse("foo.bar.tzar.class"))); assertFalse(new PathQuery("*.class", true).matches(Path.parse("foo.bar.tzar"))); assertTrue(new PathQuery("*", true).matches(Path.parse("a.b.c.d"))); assertTrue(new PathQuery("*.class.*", true).matches(Path.parse("a.b.class.d"))); assertTrue(new PathQuery("*", true).matches(Path.parse("123.asd.234s.fsdre"))); assertTrue(new PathQuery("*.*", true).matches(Path.parse("billy.bong.class.yeker"))); }
/** * Adds exclude path query. */ public JsonSerializer exclude(final String exclude) { rules.exclude(new PathQuery(exclude, false)); return this; }
/** * Adds excludes with optional parent including. When parents are included, * for each exclude query its parent will be included. * For example, exclude of 'aaa.bb.ccc' would include it's parent: 'aaa.bb'. */ public JsonSerializer exclude(final boolean includeParent, final String... excludes) { for (String exclude : excludes) { if (includeParent) { int dotIndex = exclude.lastIndexOf('.'); if (dotIndex != -1) { PathQuery pathQuery = new PathQuery(exclude.substring(0, dotIndex), true); rules.include(pathQuery); } } PathQuery pathQuery = new PathQuery(exclude, false); rules.exclude(pathQuery); } return this; }