Skip to content

Commit

Permalink
Fix #139
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 13, 2019
1 parent 456f36f commit d3e420e
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,17 @@ public boolean canUseSchema(FormatSchema schema) {
* Convenience method to allow feeding a pre-parsed {@link Properties}
* instance as input.
*
* @since 2.9
* @since 2.10
*/
public JavaPropsParser createParser(Map<?,?> content) {
return new JavaPropsParser(_createContext(content, true),
_parserFeatures, content, _objectCodec, content);
}

@Deprecated // since 2.10
public JavaPropsParser createParser(Properties props) {
IOContext ctxt = _createContext(props, true);
return new JavaPropsParser(ctxt,
props, _parserFeatures, _objectCodec, props);
return new JavaPropsParser(_createContext(props, true),
_parserFeatures, props, _objectCodec, props);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.dataformat.javaprop;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import com.fasterxml.jackson.core.JsonParser;
Expand Down Expand Up @@ -44,9 +45,7 @@ public JavaPropsMapper(JavaPropsFactory f) {
protected JavaPropsMapper(JavaPropsMapper src) {
super(src);
}


@SuppressWarnings("unchecked")
public static Builder builder() {
return new JavaPropsMapper.Builder(new JavaPropsMapper());
}
Expand All @@ -73,9 +72,9 @@ public JavaPropsFactory getFactory() {
}

/*
/**********************************************************
/* Extended read methods
/**********************************************************
/**********************************************************************
/* Extended read methods, from Properties objects
/**********************************************************************
*/

/**
Expand All @@ -93,7 +92,7 @@ public JavaPropsFactory getFactory() {
@SuppressWarnings("resource")
public <T> T readPropertiesAs(Properties props, JavaPropsSchema schema,
Class<T> valueType) throws IOException {
JsonParser p = getFactory().createParser(props);
JsonParser p = getFactory().createParser((Map<?,?>)props);
p.setSchema(schema);
return (T) readValue(p, valueType);
}
Expand All @@ -113,7 +112,7 @@ public <T> T readPropertiesAs(Properties props, JavaPropsSchema schema,
@SuppressWarnings({ "resource", "unchecked" })
public <T> T readPropertiesAs(Properties props, JavaPropsSchema schema,
JavaType valueType) throws IOException {
JsonParser p = getFactory().createParser(props);
JsonParser p = getFactory().createParser((Map<?,?>)props);
p.setSchema(schema);
return (T) readValue(p, valueType);
}
Expand Down Expand Up @@ -141,7 +140,83 @@ public <T> T readPropertiesAs(Properties props, Class<T> valueType) throws IOExc
public <T> T readPropertiesAs(Properties props, JavaType valueType) throws IOException {
return readPropertiesAs(props, JavaPropsSchema.emptySchema(), valueType);
}

/*
/**********************************************************************
/* Extended read methods, from Map objects
/**********************************************************************
*/

/**
* Convenience method which uses given `Properties` as the source
* as if they had been read from an external source, processes
* them (splits paths etc), and then binds as given result
* value.
*<p>
* Note that this is NOT identical to calling {@link #convertValue(Object, Class)};
* rather, it would be similar to writing `Properties` out into a File,
* then calling `readValue()` on this mapper to bind contents.
*
* @since 2.10
*/
@SuppressWarnings("resource")
public <T> T readMapAs(Map<String, String> map, JavaPropsSchema schema,
Class<T> valueType) throws IOException {
JsonParser p = getFactory().createParser(map);
p.setSchema(schema);
return (T) readValue(p, valueType);
}

/**
* Convenience method which uses given `Properties` as the source
* as if they had been read from an external source, processes
* them (splits paths etc), and then binds as given result
* value.
*<p>
* Note that this is NOT identical to calling {@link #convertValue(Object, Class)};
* rather, it would be similar to writing `Properties` out into a File,
* then calling `readValue()` on this mapper to bind contents.
*
* @since 2.10
*/
@SuppressWarnings({ "resource", "unchecked" })
public <T> T readMapAs(Map<String, String> map, JavaPropsSchema schema,
JavaType valueType) throws IOException {
JsonParser p = getFactory().createParser(map);
p.setSchema(schema);
return (T) readValue(p, valueType);
}

/**
* Convenience method, functionally equivalent to:
*<pre>
* readPropertiesAs(props, JavaPropsSchema.emptySchema(), valueType);
*</pre>
*
* @since 2.10
*/
public <T> T readMapAs(Map<String, String> map, Class<T> valueType) throws IOException {
return readMapAs(map, JavaPropsSchema.emptySchema(), valueType);
}

/**
* Convenience method, functionally equivalent to:
*<pre>
* readPropertiesAs(props, JavaPropsSchema.emptySchema(), valueType);
*</pre>
*
* @since 2.10
*/
public <T> T readMapAs(Map<String, String> map, JavaType valueType) throws IOException {
return readMapAs(map, JavaPropsSchema.emptySchema(), valueType);
}

/*
/**********************************************************************
/* Extended read methods, from System Properties
/**********************************************************************
*/

/**
* Convenience method, functionally equivalent to:
*<pre>
Expand All @@ -168,6 +243,12 @@ public <T> T readSystemPropertiesAs(JavaPropsSchema schema,
return readPropertiesAs(System.getProperties(), schema, valueType);
}

/*
/**********************************************************************
/* Extended read methods, from Env variables
/**********************************************************************
*/

/**
* Convenience method, functionally equivalent to:
*<pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Map;
import java.util.Properties;

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

/**
* Actual {@link java.util.Properties} that were parsed and handed to us
* Actual {@link java.util.Properties} (or, actually, any {@link java.util.Map}
* with String keys, values) that were parsed and handed to us
* for further processing.
*/
protected final Properties _sourceProperties;
protected final Map<?,?> _sourceContent;

/**
* Schema we use for parsing Properties into structure of some kind.
Expand Down Expand Up @@ -71,13 +73,20 @@ public class JavaPropsParser extends ParserMinimalBase
/**********************************************************
*/

@Deprecated // since 2.10
public JavaPropsParser(IOContext ctxt, Object inputSource,
int parserFeatures, ObjectCodec codec, Properties sourceProps)
{
this(ctxt, parserFeatures, inputSource, codec, (Map<?,?>) sourceProps);
}

public JavaPropsParser(IOContext ctxt, int parserFeatures, Object inputSource,
ObjectCodec codec, Map<?,?> sourceMap)
{
super(parserFeatures);
_objectCodec = codec;
_inputSource = inputSource;
_sourceProperties = sourceProps;
_sourceContent = sourceMap;

}

Expand Down Expand Up @@ -215,7 +224,7 @@ public JsonToken nextToken() throws IOException {
return null;
}
_closed = true;
JPropNode root = JPropNodeBuilder.build(_schema, _sourceProperties);
JPropNode root = JPropNodeBuilder.build(_sourceContent, _schema);
_readContext = JPropReadContext.create(root);

// 30-Mar-2016, tatu: For debugging can be useful:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@

public class JPropNodeBuilder
{
/**
* @deprecated Since 2.10
*/
@Deprecated // since 2.10
public static JPropNode build(JavaPropsSchema schema,
Properties props)
{
return build(props, schema);

}

public static JPropNode build(Map<?,?> content, JavaPropsSchema schema)
{
JPropNode root = new JPropNode();
JPropPathSplitter splitter = schema.pathSplitter();
for (Map.Entry<?,?> entry : props.entrySet()) {
for (Map.Entry<?,?> entry : content.entrySet()) {
// these should be Strings; but due to possible "compromised" properties,
// let's play safe, coerce if and as necessary
String key = String.valueOf(entry.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,41 @@

/**
* Tests for extended functionality to work with JDK `Properties` Object
* (as well as {@code java.util.Map}, since 2.10)
*/
public class PropertiesSupportTest extends ModuleTestBase
{
static class TestObject91 {
Map<String, String> values = new HashMap<>();
public Map<String, String> getValues() {
return values;
}
public void setValues(Map<String, String> values) {
this.values = values;
}
}

private final JavaPropsMapper MAPPER = mapperForProps();

public void testSimpleEmployee() throws Exception
public void testSimpleEmployeeFromProperties() throws Exception
{
Properties props = new Properties();
props.put("a.b", "14");
props.put("x", "foo");
Map<?,?> result = MAPPER.readPropertiesAs(props, Map.class);
_verifySimple(MAPPER.readPropertiesAs(props, Map.class));
}

// for [dataformats-text#139]
public void testSimpleEmployeeFromMap() throws Exception
{
Map<String, String> map = new LinkedHashMap<>();
map.put("a.b", "14");
map.put("x", "foo");
_verifySimple(MAPPER.readMapAs(map, Map.class));
}

private void _verifySimple(Map<?,?> result)
{
assertNotNull(result);
assertEquals(2, result.size());
assertEquals("foo", result.get("x"));
Expand All @@ -26,7 +50,7 @@ public void testSimpleEmployee() throws Exception
assertEquals("14", m2.get("b"));
}

public void testWithCustomSchema() throws Exception
public void testWithCustomSchemaFromProperties() throws Exception
{
Properties props = new Properties();
props.put("a/b", "14");
Expand All @@ -35,6 +59,24 @@ public void testWithCustomSchema() throws Exception
.withPathSeparator("/");
Map<?,?> result = MAPPER.readPropertiesAs(props, schema,
MAPPER.constructType(Map.class));
_verifyCustom(result);
}

// for [dataformats-text#139]
public void testWithCustomSchemaFromMap() throws Exception
{
Map<String, String> map = new LinkedHashMap<>();
map.put("a/b", "14");
map.put("x.y/z", "foo");
JavaPropsSchema schema = JavaPropsSchema.emptySchema()
.withPathSeparator("/");
Map<?,?> result = MAPPER.readMapAs(map, schema,
MAPPER.constructType(Map.class));
_verifyCustom(result);
}

private void _verifyCustom(Map<?,?> result)
{
assertNotNull(result);
assertEquals(2, result.size());
Object ob = result.get("a");
Expand All @@ -52,16 +94,6 @@ public void testWithCustomSchema() throws Exception
assertEquals("foo", m2.get("z"));
}

static class TestObject91 {
Map<String, String> values = new HashMap<>();
public Map<String, String> getValues() {
return values;
}
public void setValues(Map<String, String> values) {
this.values = values;
}
}

// [dataformats-text#91]
public void testEscapingWithReadPropertiesAs() throws Exception
{
Expand Down
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ Stehan Leh (stefanleh@github)
Guillaume Smaha (GuillaumeSmaha@github)
* Contributed fix for #129: (yaml) Convert YAML string issue
(2.10.0)

Filip Hrisafov (filiphr@github)
* Suggested #139: Support for Map<String, String> in `JavaPropsMapper`
(2.10.0)

2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Modules:

2.10.0.pr2 (not yet released)

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

2.10.0.pr1 (19-Jul-2019)
Expand Down

0 comments on commit d3e420e

Please sign in to comment.