Skip to content

Commit bb3c28a

Browse files
committed
More work on Map-backed generator
1 parent d3e420e commit bb3c28a

File tree

7 files changed

+203
-67
lines changed

7 files changed

+203
-67
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,24 @@ public JavaPropsParser createParser(Properties props) {
163163
_parserFeatures, props, _objectCodec, props);
164164
}
165165

166+
@Deprecated // since 2.10
167+
public JavaPropsGenerator createGenerator(Properties props) {
168+
IOContext ctxt = _createContext(props, true);
169+
return new PropertiesBackedGenerator(ctxt,
170+
props, _generatorFeatures, _objectCodec);
171+
}
172+
166173
/**
167-
* Convenience method to allow using a pre-constructed {@link Properties}
174+
* Convenience method to allow using a pre-constructed {@link Map}
168175
* instance as output target, so that serialized property values
169176
* are added.
170177
*
171-
* @since 2.9
178+
* @since 2.10
172179
*/
173-
public JavaPropsGenerator createGenerator(Properties props) {
174-
IOContext ctxt = _createContext(props, true);
180+
public JavaPropsGenerator createGenerator(Map<?,?> target, JavaPropsSchema schema) {
181+
IOContext ctxt = _createContext(target, true);
175182
return new PropertiesBackedGenerator(ctxt,
176-
props, _generatorFeatures, _objectCodec);
183+
target, _generatorFeatures, _objectCodec);
177184
}
178185

179186
/*

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

Lines changed: 60 additions & 20 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.LinkedHashMap;
45
import java.util.Map;
56
import java.util.Properties;
67

@@ -289,39 +290,49 @@ protected Properties _env() {
289290

290291
/**
291292
* Convenience method that "writes" given `value` as properties
292-
* in given {@link Properties} object.
293+
* in given {@link Map} object.
293294
*
294-
* @since 2.9
295+
* @since 2.10
295296
*/
296-
public void writeValue(Properties targetProps, Object value) throws IOException
297+
public void writeValue(Map<?,?> target, Object value) throws IOException
297298
{
298-
if (targetProps == null) {
299-
throw new IllegalArgumentException("Can not pass null Properties as target");
299+
if (target == null) {
300+
throw new IllegalArgumentException("Can not pass `null` target");
301+
}
302+
try (JavaPropsGenerator g = getFactory().createGenerator(target, null)) {
303+
writeValue(g, value);
300304
}
301-
JavaPropsGenerator g = ((JavaPropsFactory) getFactory())
302-
.createGenerator(targetProps);
303-
writeValue(g, value);
304-
g.close();
305305
}
306306

307307
/**
308308
* Convenience method that "writes" given `value` as properties
309-
* in given {@link Properties} object.
309+
* in given {@link Map} object.
310310
*
311-
* @since 2.9
311+
* @since 2.10
312312
*/
313-
public void writeValue(Properties targetProps, Object value, JavaPropsSchema schema)
313+
public void writeValue(Map<?,?> target, Object value, JavaPropsSchema schema)
314314
throws IOException
315315
{
316-
if (targetProps == null) {
317-
throw new IllegalArgumentException("Can not pass null Properties as target");
316+
if (target == null) {
317+
throw new IllegalArgumentException("Can not pass `null` target");
318318
}
319-
JavaPropsGenerator g = getFactory().createGenerator(targetProps);
320-
if (schema != null) {
321-
g.setSchema(schema);
319+
try (JavaPropsGenerator g = getFactory().createGenerator(target, schema)) {
320+
if (schema != null) {
321+
g.setSchema(schema);
322+
}
323+
writeValue(g, value);
322324
}
323-
writeValue(g, value);
324-
g.close();
325+
}
326+
327+
@Deprecated // since 2.10
328+
public void writeValue(Properties targetProps, Object value) throws IOException {
329+
writeValue((Map<?,?>) targetProps, value);
330+
}
331+
332+
@Deprecated // since 2.10
333+
public void writeValue(Properties targetProps, Object value, JavaPropsSchema schema)
334+
throws IOException {
335+
writeValue((Map<?,?>) targetProps, value, schema);
325336
}
326337

327338
/**
@@ -352,7 +363,36 @@ public Properties writeValueAsProperties(Object value, JavaPropsSchema schema)
352363
writeValue(props, value, schema);
353364
return props;
354365
}
355-
366+
367+
/**
368+
* Convenience method that serializes given value but so that results are
369+
* stored in a newly constructed {@link Properties}. Functionally equivalent
370+
* to serializing in a File and reading contents into {@link Properties}.
371+
*
372+
* @since 2.10
373+
*/
374+
public Map<String, String> writeValueAsMap(Object value)
375+
throws IOException
376+
{
377+
final Map<String, String> map = new LinkedHashMap<>();
378+
writeValue(map, value);
379+
return map;
380+
}
381+
382+
/**
383+
* Convenience method that serializes given value but so that results are
384+
* stored in given {@link Properties} instance.
385+
*
386+
* @since 2.10
387+
*/
388+
public Map<String, String> writeValueAsMap(Object value, JavaPropsSchema schema)
389+
throws IOException
390+
{
391+
final Map<String, String> map = new LinkedHashMap<>();
392+
writeValue(map, value, schema);
393+
return map;
394+
}
395+
356396
/*
357397
/**********************************************************
358398
/* Schema support methods?

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/impl/PropertiesBackedGenerator.java

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

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

67
import com.fasterxml.jackson.core.*;
@@ -19,25 +20,37 @@ public class PropertiesBackedGenerator extends JavaPropsGenerator
1920
* Underlying {@link Properties} that we will update with logical
2021
* properties written out.
2122
*/
22-
final protected Properties _props;
23+
final protected Map<String, Object> _content;
2324

2425
/*
2526
/**********************************************************
2627
/* Life-cycle
2728
/**********************************************************
2829
*/
2930

30-
public PropertiesBackedGenerator(IOContext ctxt, Properties props,
31+
@SuppressWarnings("unchecked")
32+
public PropertiesBackedGenerator(IOContext ctxt, Map<?,?> content,
3133
int stdFeatures, ObjectCodec codec)
3234
{
3335
super(ctxt, stdFeatures, codec);
34-
_props = props;
35-
36+
_content = (Map<String, Object>) content;
3637
// Since this is not physically encoding properties, should NOT try
3738
// to attempt writing headers. Easy way is to just fake we already did it
3839
_headerChecked = true;
3940
}
4041

42+
@SuppressWarnings("unchecked")
43+
@Deprecated // since 2.10
44+
public PropertiesBackedGenerator(IOContext ctxt, Properties props,
45+
int stdFeatures, ObjectCodec codec)
46+
{
47+
super(ctxt, stdFeatures, codec);
48+
_content = (Map<String, Object>)(Map<?,?>) props;
49+
// Since this is not physically encoding properties, should NOT try
50+
// to attempt writing headers. Easy way is to just fake we already did it
51+
_headerChecked = true;
52+
}
53+
4154
/*
4255
/**********************************************************
4356
/* Overridden methods, configuration
@@ -46,7 +59,7 @@ public PropertiesBackedGenerator(IOContext ctxt, Properties props,
4659

4760
@Override
4861
public Object getOutputTarget() {
49-
return _props;
62+
return _content;
5063
}
5164

5265
/*
@@ -90,13 +103,13 @@ protected void _writeEscapedEntry(char[] text, int offset, int len) throws IOExc
90103
@Override
91104
protected void _writeEscapedEntry(String value) throws IOException
92105
{
93-
_props.put(_basePath.toString(), value);
106+
_content.put(_basePath.toString(), value);
94107
}
95108

96109
@Override
97110
protected void _writeUnescapedEntry(String value) throws IOException
98111
{
99-
_props.put(_basePath.toString(), value);
112+
_content.put(_basePath.toString(), value);
100113
}
101114

102115
/*

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

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

3+
import java.util.Map;
34
import java.util.Properties;
45

56
import com.fasterxml.jackson.dataformat.javaprop.util.Markers;
@@ -20,10 +21,18 @@ public void testPointListSimple() throws Exception
2021
+"p.3.x=5\n"
2122
+"p.3.y=6\n"
2223
,output);
23-
Properties props = MAPPER.writeValueAsProperties(input);
24-
assertEquals(6, props.size());
25-
assertEquals("6", props.get("p.3.y"));
26-
assertEquals("1", props.get("p.1.x"));
24+
{
25+
Properties props = MAPPER.writeValueAsProperties(input);
26+
assertEquals(6, props.size());
27+
assertEquals("6", props.get("p.3.y"));
28+
assertEquals("1", props.get("p.1.x"));
29+
}
30+
{
31+
Map<String, String> map = MAPPER.writeValueAsMap(input);
32+
assertEquals(6, map.size());
33+
assertEquals("6", map.get("p.3.y"));
34+
assertEquals("1", map.get("p.1.x"));
35+
}
2736
}
2837

2938
public void testPointListWithIndex() throws Exception
@@ -42,10 +51,18 @@ public void testPointListWithIndex() throws Exception
4251
+"p[5].x=5\n"
4352
+"p[5].y=6\n"
4453
,output);
45-
Properties props = MAPPER.writeValueAsProperties(input, schema);
46-
assertEquals(6, props.size());
47-
assertEquals("2", props.get("p[3].y"));
48-
assertEquals("3", props.get("p[4].x"));
54+
{
55+
Properties props = MAPPER.writeValueAsProperties(input, schema);
56+
assertEquals(6, props.size());
57+
assertEquals("2", props.get("p[3].y"));
58+
assertEquals("3", props.get("p[4].x"));
59+
}
60+
{
61+
Map<String, String> map = MAPPER.writeValueAsMap(input, schema);
62+
assertEquals(6, map.size());
63+
assertEquals("2", map.get("p[3].y"));
64+
assertEquals("3", map.get("p[4].x"));
65+
}
4966
}
5067

5168
public void testPointListWithCustomMarkers() throws Exception
@@ -62,9 +79,18 @@ public void testPointListWithCustomMarkers() throws Exception
6279
+"p<<2>>.x=3\n"
6380
+"p<<2>>.y=4\n"
6481
,output);
65-
Properties props = MAPPER.writeValueAsProperties(input, schema);
66-
assertEquals(4, props.size());
67-
assertEquals("1", props.get("p<<1>>.x"));
68-
assertEquals("4", props.get("p<<2>>.y"));
82+
{
83+
Properties props = MAPPER.writeValueAsProperties(input, schema);
84+
assertEquals(4, props.size());
85+
assertEquals("1", props.get("p<<1>>.x"));
86+
assertEquals("4", props.get("p<<2>>.y"));
87+
}
88+
89+
{
90+
Map<String,String> map = MAPPER.writeValueAsMap(input, schema);
91+
assertEquals(4, map.size());
92+
assertEquals("1", map.get("p<<1>>.x"));
93+
assertEquals("4", map.get("p<<2>>.y"));
94+
}
6995
}
7096
}

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertArrayEquals;
44

55
import java.nio.ByteBuffer;
6+
import java.util.Map;
67
import java.util.Properties;
78

89
public class BinaryParsingTest extends ModuleTestBase
@@ -32,15 +33,31 @@ public MyBean(boolean bogus) {
3233
// [dataformats-text#74]
3334
public void testMultipleBinaryFields() throws Exception
3435
{
35-
MyBean src = new MyBean(true);
36-
Properties props = MAPPER.writeValueAsProperties(src);
37-
38-
MyBean result = MAPPER.readPropertiesAs(props, MyBean.class);
39-
assertArrayEquals(src.a, result.a);
40-
assertArrayEquals(src.b, result.b);
41-
ByteBuffer b1 = src.c;
42-
ByteBuffer b2 = result.c;
36+
final MyBean src = new MyBean(true);
37+
38+
{
39+
Properties props = MAPPER.writeValueAsProperties(src);
40+
41+
MyBean result = MAPPER.readPropertiesAs(props, MyBean.class);
42+
assertArrayEquals(src.a, result.a);
43+
assertArrayEquals(src.b, result.b);
44+
ByteBuffer b1 = src.c;
45+
ByteBuffer b2 = result.c;
46+
47+
assertEquals(b1, b2);
48+
}
4349

44-
assertEquals(b1, b2);
50+
{
51+
Map<String, String> map = MAPPER.writeValueAsMap(src);
52+
53+
MyBean result = MAPPER.readMapAs(map, MyBean.class);
54+
assertArrayEquals(src.a, result.a);
55+
assertArrayEquals(src.b, result.b);
56+
ByteBuffer b1 = src.c;
57+
ByteBuffer b2 = result.c;
58+
59+
assertEquals(b1, b2);
60+
}
61+
4562
}
4663
}

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

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

3+
import java.util.Map;
34
import java.util.Properties;
45

56
public class PrefixTest extends ModuleTestBase
@@ -31,9 +32,18 @@ public void testPrefixGeneration() throws Exception
3132
+"org.o1.verified=true\n"
3233
+"org.o1.userImage=AQIDBA==\n"
3334
,output);
34-
Properties props = MAPPER.writeValueAsProperties(input, JavaPropsSchema.emptySchema().withPrefix("org.o1"));
35-
assertEquals(5, props.size());
36-
assertEquals("true", props.get("org.o1.verified"));
37-
assertEquals("MALE", props.get("org.o1.gender"));
35+
{
36+
Properties props = MAPPER.writeValueAsProperties(input, JavaPropsSchema.emptySchema().withPrefix("org.o1"));
37+
assertEquals(5, props.size());
38+
assertEquals("true", props.get("org.o1.verified"));
39+
assertEquals("MALE", props.get("org.o1.gender"));
40+
}
41+
{
42+
Map<String, String> map = MAPPER.writeValueAsMap(input,
43+
JavaPropsSchema.emptySchema().withPrefix("org.o1"));
44+
assertEquals(5, map.size());
45+
assertEquals("true", map.get("org.o1.verified"));
46+
assertEquals("MALE", map.get("org.o1.gender"));
47+
}
3848
}
3949
}

0 commit comments

Comments
 (0)