/** * Defines {@link jodd.json.ValueConverter} to use on given path. */ public JsonParser withValueConverter(final String path, final ValueConverter valueConverter) { if (convs == null) { convs = new HashMap<>(); } convs.put(Path.parse(path), valueConverter); return this; }
@Test void testPaths() { Path path = new Path(); assertEquals(0, path.length()); assertEquals("[]", path.toString()); path.push("one"); assertEquals(1, path.length()); assertEquals("[one]", path.toString()); path.push("two"); assertEquals(2, path.length()); assertEquals("[one.two]", path.toString()); path.push("three"); assertEquals(3, path.length()); assertEquals("[one.two.three]", path.toString()); path.push("four"); path.push("five"); path.push("six"); path.push("seven"); path.push("eight"); path.push("nine"); path.push("ten"); assertEquals(10, path.length()); assertEquals("[one.two.three.four.five.six.seven.eight.nine.ten]", path.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"))); }
/** * Replaces type with mapped type for current path. */ protected Class replaceWithMappedTypeForPath(final Class target) { if (mappings == null) { return target; } Class newType; // first try alt paths Path altPath = path.getAltPath(); if (altPath != null) { if (!altPath.equals(path)) { newType = mappings.get(altPath); if (newType != null) { return newType; } } } // now check regular paths newType = mappings.get(path); if (newType != null) { return newType; } return target; }
public JsonContext(final JsonSerializer jsonSerializer, final Appendable appendable) { super(appendable, jsonSerializer.strictStringEncoding); this.jsonSerializer = jsonSerializer; this.bag = new ArrayList<>(); this.path = new Path(); this.excludeNulls = jsonSerializer.excludeNulls; this.excludeEmpty = jsonSerializer.excludeEmpty; this.serializerResolver = jsonSerializer.serializerResolver; }
/** * Pop last element from the path. */ public CharSequence pop(){ if (altPath != null) { altPath.pop(); } return paths[--index]; }
ObjectParser(final JsonParser jsonParser, final Class targetType, final Class keyType, final Class componentType) { this.jsonParser = jsonParser; this.ndx = jsonParser.ndx; this.path = jsonParser.path.clone(); this.targetType = targetType; this.keyType = keyType; this.componentType = componentType; }
/** * Defines custom {@link jodd.json.TypeJsonSerializer} for given path. */ public JsonSerializer withSerializer(final String pathString, final TypeJsonSerializer typeJsonSerializer) { if (pathSerializersMap == null) { pathSerializersMap = new HashMap<>(); } pathSerializersMap.put(Path.parse(pathString), typeJsonSerializer); return this; }
/** * Maps a class to given path. For arrays, append <code>values</code> * to the path to specify component type (if not specified by * generics). */ public JsonParser map(final String path, final Class target) { if (path == null) { rootType = target; return this; } if (mappings == null) { mappings = new HashMap<>(); } mappings.put(Path.parse(path), target); return this; }