Skip to content

Commit be15465

Browse files
committed
Find, fix #24
1 parent e6f1d5e commit be15465

File tree

6 files changed

+51
-3
lines changed

6 files changed

+51
-3
lines changed

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/JSON.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -663,13 +663,17 @@ public JSONComposer<OutputStream> composeTo(File f) throws IOException, JSONObje
663663
@SuppressWarnings("resource")
664664
public JSONComposer<String> composeString() throws IOException, JSONObjectException {
665665
SegmentedStringWriter out = new SegmentedStringWriter(_jsonFactory._getBufferRecycler());
666-
return JSONComposer.stringComposer(_features, _jsonFactory.createGenerator(out), out);
666+
JsonGenerator gen = _jsonFactory.createGenerator(out)
667+
.setCodec(asCodec());
668+
return JSONComposer.stringComposer(_features, gen, out);
667669
}
668670

669671
@SuppressWarnings("resource")
670672
public JSONComposer<byte[]> composeBytes() throws IOException, JSONObjectException {
671673
ByteArrayBuilder out = new ByteArrayBuilder(_jsonFactory._getBufferRecycler());
672-
return JSONComposer.bytesComposer(_features, _jsonFactory.createGenerator(out), out);
674+
JsonGenerator gen = _jsonFactory.createGenerator(out)
675+
.setCodec(asCodec());
676+
return JSONComposer.bytesComposer(_features, gen, out);
673677
}
674678

675679
public CollectionComposer<?,List<Object>> composeList() {

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/comp/CollectionComposer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public CollectionComposer<PARENT,C> add(boolean value)
143143
/**
144144
* Method used to add Java Object ("POJO") into sequence being
145145
* composed: this <b>requires</b> that the underlying {@link JsonGenerator}
146-
* has a properly configure {@link com.fasterxml.jackson.core.ObjectCodec}
146+
* has a properly configured {@link com.fasterxml.jackson.core.ObjectCodec}
147147
* to use for serializer object.
148148
*/
149149
public CollectionComposer<PARENT,C> addObject(Object pojo)

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/comp/ObjectComposer.java

+15
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ public ObjectComposer<PARENT> putNull(String fieldName)
118118
return this;
119119
}
120120

121+
/**
122+
* Method used to put a Java Object ("POJO") value into Object being
123+
* composed: this <b>requires</b> that the underlying {@link JsonGenerator}
124+
* has a properly configured {@link com.fasterxml.jackson.core.ObjectCodec}
125+
* to use for serializer object.
126+
*
127+
* @since 2.6
128+
*/
129+
public ObjectComposer<PARENT> putObject(String fieldName, Object value)
130+
throws IOException, JsonProcessingException
131+
{
132+
_generator.writeObjectField(fieldName, value);
133+
return this;
134+
}
135+
121136
public ObjectComposer<PARENT> put(String fieldName, int value)
122137
throws IOException, JsonProcessingException
123138
{

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/ReadSimpleTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public void testSimpleMap() throws Exception
5050
assertEquals(3, ((Map<?,?>) ob).size());
5151
// actually, verify with write...
5252
assertEquals(INPUT, JSON.std.asString(ob));
53+
54+
// or, via explicit Map read
55+
Map<String,Object> stuff = JSON.std.mapFrom(INPUT);
56+
assertEquals(3, stuff.size());
5357
}
5458

5559
public void testSimpleMixed() throws Exception

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/SimpleComposerTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66

77
public class SimpleComposerTest extends TestBase
88
{
9+
public static class NameBean {
10+
String first;
11+
12+
public NameBean(String f) {
13+
first = f;
14+
}
15+
16+
public String getFirst() { return first; }
17+
public void setFirst(String s) { first = s; }
18+
}
19+
920
public void testSimpleNonNestedObject() throws Exception
1021
{
1122
String json = JSON.std.composeString()
@@ -104,4 +115,17 @@ public void testListComposer() throws Exception
104115
.finish();
105116
assertEquals("[-3,{\"a\":1,\"b\":2}]", JSON.std.asString(list));
106117
}
118+
119+
public void testComposerWithPojo() throws Exception
120+
{
121+
String json = JSON.std.composeString()
122+
.startArray()
123+
.addObject(new NameBean("Bob"))
124+
.startObject()
125+
.putObject("name", new NameBean("Bill"))
126+
.end()
127+
.end()
128+
.finish();
129+
assertEquals(aposToQuotes("[{'first':'Bob'},{'name':{'first':'Bill'}}]"), json);
130+
}
107131
}

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Project: jackson-jr
66

77
2.6.0 (not yet released)
88

9+
#24: String/byte[] composers can not write POJOs (ObjectCodec not linked)
910
- Minor performance optimizations, using new jackson-core 2.6 methods
1011

1112
2.5.3 (24-Apr-2015)

0 commit comments

Comments
 (0)