Skip to content

Commit d3e420e

Browse files
committed
Fix #139
1 parent 456f36f commit d3e420e

File tree

7 files changed

+173
-29
lines changed

7 files changed

+173
-29
lines changed

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/JavaPropsFactory.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,17 @@ public boolean canUseSchema(FormatSchema schema) {
150150
* Convenience method to allow feeding a pre-parsed {@link Properties}
151151
* instance as input.
152152
*
153-
* @since 2.9
153+
* @since 2.10
154154
*/
155+
public JavaPropsParser createParser(Map<?,?> content) {
156+
return new JavaPropsParser(_createContext(content, true),
157+
_parserFeatures, content, _objectCodec, content);
158+
}
159+
160+
@Deprecated // since 2.10
155161
public JavaPropsParser createParser(Properties props) {
156-
IOContext ctxt = _createContext(props, true);
157-
return new JavaPropsParser(ctxt,
158-
props, _parserFeatures, _objectCodec, props);
162+
return new JavaPropsParser(_createContext(props, true),
163+
_parserFeatures, props, _objectCodec, props);
159164
}
160165

161166
/**

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/JavaPropsMapper.java

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.dataformat.javaprop;
22

33
import java.io.IOException;
4+
import java.util.Map;
45
import java.util.Properties;
56

67
import com.fasterxml.jackson.core.JsonParser;
@@ -44,9 +45,7 @@ public JavaPropsMapper(JavaPropsFactory f) {
4445
protected JavaPropsMapper(JavaPropsMapper src) {
4546
super(src);
4647
}
47-
4848

49-
@SuppressWarnings("unchecked")
5049
public static Builder builder() {
5150
return new JavaPropsMapper.Builder(new JavaPropsMapper());
5251
}
@@ -73,9 +72,9 @@ public JavaPropsFactory getFactory() {
7372
}
7473

7574
/*
76-
/**********************************************************
77-
/* Extended read methods
78-
/**********************************************************
75+
/**********************************************************************
76+
/* Extended read methods, from Properties objects
77+
/**********************************************************************
7978
*/
8079

8180
/**
@@ -93,7 +92,7 @@ public JavaPropsFactory getFactory() {
9392
@SuppressWarnings("resource")
9493
public <T> T readPropertiesAs(Properties props, JavaPropsSchema schema,
9594
Class<T> valueType) throws IOException {
96-
JsonParser p = getFactory().createParser(props);
95+
JsonParser p = getFactory().createParser((Map<?,?>)props);
9796
p.setSchema(schema);
9897
return (T) readValue(p, valueType);
9998
}
@@ -113,7 +112,7 @@ public <T> T readPropertiesAs(Properties props, JavaPropsSchema schema,
113112
@SuppressWarnings({ "resource", "unchecked" })
114113
public <T> T readPropertiesAs(Properties props, JavaPropsSchema schema,
115114
JavaType valueType) throws IOException {
116-
JsonParser p = getFactory().createParser(props);
115+
JsonParser p = getFactory().createParser((Map<?,?>)props);
117116
p.setSchema(schema);
118117
return (T) readValue(p, valueType);
119118
}
@@ -141,7 +140,83 @@ public <T> T readPropertiesAs(Properties props, Class<T> valueType) throws IOExc
141140
public <T> T readPropertiesAs(Properties props, JavaType valueType) throws IOException {
142141
return readPropertiesAs(props, JavaPropsSchema.emptySchema(), valueType);
143142
}
143+
144+
/*
145+
/**********************************************************************
146+
/* Extended read methods, from Map objects
147+
/**********************************************************************
148+
*/
149+
150+
/**
151+
* Convenience method which uses given `Properties` as the source
152+
* as if they had been read from an external source, processes
153+
* them (splits paths etc), and then binds as given result
154+
* value.
155+
*<p>
156+
* Note that this is NOT identical to calling {@link #convertValue(Object, Class)};
157+
* rather, it would be similar to writing `Properties` out into a File,
158+
* then calling `readValue()` on this mapper to bind contents.
159+
*
160+
* @since 2.10
161+
*/
162+
@SuppressWarnings("resource")
163+
public <T> T readMapAs(Map<String, String> map, JavaPropsSchema schema,
164+
Class<T> valueType) throws IOException {
165+
JsonParser p = getFactory().createParser(map);
166+
p.setSchema(schema);
167+
return (T) readValue(p, valueType);
168+
}
169+
170+
/**
171+
* Convenience method which uses given `Properties` as the source
172+
* as if they had been read from an external source, processes
173+
* them (splits paths etc), and then binds as given result
174+
* value.
175+
*<p>
176+
* Note that this is NOT identical to calling {@link #convertValue(Object, Class)};
177+
* rather, it would be similar to writing `Properties` out into a File,
178+
* then calling `readValue()` on this mapper to bind contents.
179+
*
180+
* @since 2.10
181+
*/
182+
@SuppressWarnings({ "resource", "unchecked" })
183+
public <T> T readMapAs(Map<String, String> map, JavaPropsSchema schema,
184+
JavaType valueType) throws IOException {
185+
JsonParser p = getFactory().createParser(map);
186+
p.setSchema(schema);
187+
return (T) readValue(p, valueType);
188+
}
189+
190+
/**
191+
* Convenience method, functionally equivalent to:
192+
*<pre>
193+
* readPropertiesAs(props, JavaPropsSchema.emptySchema(), valueType);
194+
*</pre>
195+
*
196+
* @since 2.10
197+
*/
198+
public <T> T readMapAs(Map<String, String> map, Class<T> valueType) throws IOException {
199+
return readMapAs(map, JavaPropsSchema.emptySchema(), valueType);
200+
}
201+
202+
/**
203+
* Convenience method, functionally equivalent to:
204+
*<pre>
205+
* readPropertiesAs(props, JavaPropsSchema.emptySchema(), valueType);
206+
*</pre>
207+
*
208+
* @since 2.10
209+
*/
210+
public <T> T readMapAs(Map<String, String> map, JavaType valueType) throws IOException {
211+
return readMapAs(map, JavaPropsSchema.emptySchema(), valueType);
212+
}
144213

214+
/*
215+
/**********************************************************************
216+
/* Extended read methods, from System Properties
217+
/**********************************************************************
218+
*/
219+
145220
/**
146221
* Convenience method, functionally equivalent to:
147222
*<pre>
@@ -168,6 +243,12 @@ public <T> T readSystemPropertiesAs(JavaPropsSchema schema,
168243
return readPropertiesAs(System.getProperties(), schema, valueType);
169244
}
170245

246+
/*
247+
/**********************************************************************
248+
/* Extended read methods, from Env variables
249+
/**********************************************************************
250+
*/
251+
171252
/**
172253
* Convenience method, functionally equivalent to:
173254
*<pre>

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/JavaPropsParser.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import java.math.BigDecimal;
55
import java.math.BigInteger;
6+
import java.util.Map;
67
import java.util.Properties;
78

89
import com.fasterxml.jackson.core.*;
@@ -35,10 +36,11 @@ public class JavaPropsParser extends ParserMinimalBase
3536
protected final Object _inputSource;
3637

3738
/**
38-
* Actual {@link java.util.Properties} that were parsed and handed to us
39+
* Actual {@link java.util.Properties} (or, actually, any {@link java.util.Map}
40+
* with String keys, values) that were parsed and handed to us
3941
* for further processing.
4042
*/
41-
protected final Properties _sourceProperties;
43+
protected final Map<?,?> _sourceContent;
4244

4345
/**
4446
* Schema we use for parsing Properties into structure of some kind.
@@ -71,13 +73,20 @@ public class JavaPropsParser extends ParserMinimalBase
7173
/**********************************************************
7274
*/
7375

76+
@Deprecated // since 2.10
7477
public JavaPropsParser(IOContext ctxt, Object inputSource,
7578
int parserFeatures, ObjectCodec codec, Properties sourceProps)
79+
{
80+
this(ctxt, parserFeatures, inputSource, codec, (Map<?,?>) sourceProps);
81+
}
82+
83+
public JavaPropsParser(IOContext ctxt, int parserFeatures, Object inputSource,
84+
ObjectCodec codec, Map<?,?> sourceMap)
7685
{
7786
super(parserFeatures);
7887
_objectCodec = codec;
7988
_inputSource = inputSource;
80-
_sourceProperties = sourceProps;
89+
_sourceContent = sourceMap;
8190

8291
}
8392

@@ -215,7 +224,7 @@ public JsonToken nextToken() throws IOException {
215224
return null;
216225
}
217226
_closed = true;
218-
JPropNode root = JPropNodeBuilder.build(_schema, _sourceProperties);
227+
JPropNode root = JPropNodeBuilder.build(_sourceContent, _schema);
219228
_readContext = JPropReadContext.create(root);
220229

221230
// 30-Mar-2016, tatu: For debugging can be useful:

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/util/JPropNodeBuilder.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@
77

88
public class JPropNodeBuilder
99
{
10+
/**
11+
* @deprecated Since 2.10
12+
*/
13+
@Deprecated // since 2.10
1014
public static JPropNode build(JavaPropsSchema schema,
1115
Properties props)
16+
{
17+
return build(props, schema);
18+
19+
}
20+
21+
public static JPropNode build(Map<?,?> content, JavaPropsSchema schema)
1222
{
1323
JPropNode root = new JPropNode();
1424
JPropPathSplitter splitter = schema.pathSplitter();
15-
for (Map.Entry<?,?> entry : props.entrySet()) {
25+
for (Map.Entry<?,?> entry : content.entrySet()) {
1626
// these should be Strings; but due to possible "compromised" properties,
1727
// let's play safe, coerce if and as necessary
1828
String key = String.valueOf(entry.getKey());

properties/src/test/java/com/fasterxml/jackson/dataformat/javaprop/PropertiesSupportTest.java

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,41 @@
44

55
/**
66
* Tests for extended functionality to work with JDK `Properties` Object
7+
* (as well as {@code java.util.Map}, since 2.10)
78
*/
89
public class PropertiesSupportTest extends ModuleTestBase
910
{
11+
static class TestObject91 {
12+
Map<String, String> values = new HashMap<>();
13+
public Map<String, String> getValues() {
14+
return values;
15+
}
16+
public void setValues(Map<String, String> values) {
17+
this.values = values;
18+
}
19+
}
20+
1021
private final JavaPropsMapper MAPPER = mapperForProps();
1122

12-
public void testSimpleEmployee() throws Exception
23+
public void testSimpleEmployeeFromProperties() throws Exception
1324
{
1425
Properties props = new Properties();
1526
props.put("a.b", "14");
1627
props.put("x", "foo");
17-
Map<?,?> result = MAPPER.readPropertiesAs(props, Map.class);
28+
_verifySimple(MAPPER.readPropertiesAs(props, Map.class));
29+
}
30+
31+
// for [dataformats-text#139]
32+
public void testSimpleEmployeeFromMap() throws Exception
33+
{
34+
Map<String, String> map = new LinkedHashMap<>();
35+
map.put("a.b", "14");
36+
map.put("x", "foo");
37+
_verifySimple(MAPPER.readMapAs(map, Map.class));
38+
}
39+
40+
private void _verifySimple(Map<?,?> result)
41+
{
1842
assertNotNull(result);
1943
assertEquals(2, result.size());
2044
assertEquals("foo", result.get("x"));
@@ -26,7 +50,7 @@ public void testSimpleEmployee() throws Exception
2650
assertEquals("14", m2.get("b"));
2751
}
2852

29-
public void testWithCustomSchema() throws Exception
53+
public void testWithCustomSchemaFromProperties() throws Exception
3054
{
3155
Properties props = new Properties();
3256
props.put("a/b", "14");
@@ -35,6 +59,24 @@ public void testWithCustomSchema() throws Exception
3559
.withPathSeparator("/");
3660
Map<?,?> result = MAPPER.readPropertiesAs(props, schema,
3761
MAPPER.constructType(Map.class));
62+
_verifyCustom(result);
63+
}
64+
65+
// for [dataformats-text#139]
66+
public void testWithCustomSchemaFromMap() throws Exception
67+
{
68+
Map<String, String> map = new LinkedHashMap<>();
69+
map.put("a/b", "14");
70+
map.put("x.y/z", "foo");
71+
JavaPropsSchema schema = JavaPropsSchema.emptySchema()
72+
.withPathSeparator("/");
73+
Map<?,?> result = MAPPER.readMapAs(map, schema,
74+
MAPPER.constructType(Map.class));
75+
_verifyCustom(result);
76+
}
77+
78+
private void _verifyCustom(Map<?,?> result)
79+
{
3880
assertNotNull(result);
3981
assertEquals(2, result.size());
4082
Object ob = result.get("a");
@@ -52,16 +94,6 @@ public void testWithCustomSchema() throws Exception
5294
assertEquals("foo", m2.get("z"));
5395
}
5496

55-
static class TestObject91 {
56-
Map<String, String> values = new HashMap<>();
57-
public Map<String, String> getValues() {
58-
return values;
59-
}
60-
public void setValues(Map<String, String> values) {
61-
this.values = values;
62-
}
63-
}
64-
6597
// [dataformats-text#91]
6698
public void testEscapingWithReadPropertiesAs() throws Exception
6799
{

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ Stehan Leh (stefanleh@github)
6767
Guillaume Smaha (GuillaumeSmaha@github)
6868
* Contributed fix for #129: (yaml) Convert YAML string issue
6969
(2.10.0)
70+
71+
Filip Hrisafov (filiphr@github)
72+
* Suggested #139: Support for Map<String, String> in `JavaPropsMapper`
73+
(2.10.0)
74+

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Modules:
1010

1111
2.10.0.pr2 (not yet released)
1212

13+
#139: (properties) Support for Map<String, String> in `JavaPropsMapper`
14+
(suggested by Filip H)
1315
#140: (yaml) Implement `JsonGenerator.writeFieldId(...)` for `YAMLGenerator`
1416

1517
2.10.0.pr1 (19-Jul-2019)

0 commit comments

Comments
 (0)