-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
properties: add properties prefix as filter/append
Properties may contain multiple objects based on prefix, for example: a.b.c.name = a.b.c.id = x.y.z.name = x.y.z.color = To read these formats a prefix should be used as a filter when reading and as append when generating. A new optional JavaPropsSchema::prefix() and JavaPropsSchema::withPrefix() are added to control the behavior. As each path component is an object, use of ObjectReader::withRootName has a different meaning so schema property was used. PR #100
- Loading branch information
1 parent
a027e13
commit 42a93e9
Showing
4 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
properties/src/test/java/com/fasterxml/jackson/dataformat/javaprop/PrefixTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.fasterxml.jackson.dataformat.javaprop; | ||
|
||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class PrefixTest extends ModuleTestBase | ||
{ | ||
private final JavaPropsMapper MAPPER = mapperForProps(); | ||
|
||
public void testPrefixParsing() throws Exception { | ||
final String INPUT = "org.o1.firstName=Bob\n" | ||
+"org.o1.lastName=Palmer\n" | ||
+"org.o2.firstName=Alice\n" | ||
+"org.o2.lastName=Black\n" | ||
+"junk=AQIDBA==\n"; | ||
FiveMinuteUser result1 = _mapFrom(MAPPER.reader(JavaPropsSchema.emptySchema().withPrefix("org.o1")), INPUT, FiveMinuteUser.class, false); | ||
assertEquals("Bob", result1.firstName); | ||
assertEquals("Palmer", result1.lastName); | ||
FiveMinuteUser result2 = _mapFrom(MAPPER.reader(JavaPropsSchema.emptySchema().withPrefix("org.o2")), INPUT, FiveMinuteUser.class, false); | ||
assertEquals("Alice", result2.firstName); | ||
assertEquals("Black", result2.lastName); | ||
} | ||
|
||
public void testPrefixGeneration() throws Exception | ||
{ | ||
FiveMinuteUser input = new FiveMinuteUser("Bob", "Palmer", true, Gender.MALE, | ||
new byte[] { 1, 2, 3, 4 }); | ||
String output = MAPPER.writer(JavaPropsSchema.emptySchema().withPrefix("org.o1")).writeValueAsString(input); | ||
assertEquals("org.o1.firstName=Bob\n" | ||
+"org.o1.lastName=Palmer\n" | ||
+"org.o1.gender=MALE\n" | ||
+"org.o1.verified=true\n" | ||
+"org.o1.userImage=AQIDBA==\n" | ||
,output); | ||
Properties props = MAPPER.writeValueAsProperties(input, JavaPropsSchema.emptySchema().withPrefix("org.o1")); | ||
assertEquals(5, props.size()); | ||
assertEquals("true", props.get("org.o1.verified")); | ||
assertEquals("MALE", props.get("org.o1.gender")); | ||
} | ||
} |