Skip to content

Commit 95a0124

Browse files
committed
Work on writer side of #65
1 parent 496cafc commit 95a0124

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,18 @@
1212
public interface ValueWriter {
1313
public void writeValue(JSONWriter context, JsonGenerator g, Object value)
1414
throws IOException;
15+
16+
/*
17+
/**********************************************************************
18+
/* Minimal metadata
19+
/**********************************************************************
20+
*/
21+
22+
/**
23+
* Accessor for non-generic (type-erased) type of values this reader
24+
* produces from input.
25+
*
26+
* @since 2.10
27+
*/
28+
public Class<?> valueType();
1529
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ public class BeanWriter
1010
{
1111
protected final BeanPropertyWriter[] _properties;
1212

13-
public BeanWriter(BeanPropertyWriter[] props) {
13+
protected final Class<?> _valueType;
14+
15+
public BeanWriter(Class<?> type, BeanPropertyWriter[] props) {
16+
_valueType = type;
1417
_properties = props;
1518
}
1619

@@ -21,4 +24,8 @@ public void writeValue(JSONWriter context, JsonGenerator g, Object value)
2124
context.writeBeanValue(_properties, value);
2225
}
2326

27+
@Override
28+
public Class<?> valueType() {
29+
return _valueType;
30+
}
2431
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.fasterxml.jackson.jr.ob.JSON;
1111
import com.fasterxml.jackson.jr.ob.JSONObjectException;
1212
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
13+
import com.fasterxml.jackson.jr.ob.api.ValueWriter;
1314

1415
import static com.fasterxml.jackson.jr.ob.impl.ValueWriterLocator.*;
1516

@@ -268,10 +269,10 @@ public void writeField(String fieldName, Object value, int type) throws IOExcept
268269
}
269270

270271
if (type < 0) { // Bean type!
271-
BeanPropertyWriter[] props = _writerLocator.getPropertyWriters(type);
272-
if (props != null) { // sanity check
272+
ValueWriter writer = _writerLocator.getValueWriter(type);
273+
if (writer != null) { // sanity check
273274
_generator.writeFieldName(fieldName);
274-
writeBeanValue(props, value);
275+
writer.writeValue(this, _generator, value);
275276
return;
276277
}
277278
}
@@ -382,10 +383,10 @@ protected void _writeValue(Object value, int type) throws IOException
382383
return;
383384
}
384385

385-
if (type < 0) { // Bean type!
386-
BeanPropertyWriter[] props = _writerLocator.getPropertyWriters(type);
387-
if (props != null) { // sanity check
388-
writeBeanValue(props, value);
386+
if (type < 0) { // explicit ValueWriter
387+
ValueWriter writer = _writerLocator.getValueWriter(type);
388+
if (writer != null) { // sanity check
389+
writer.writeValue(this, _generator, value);
389390
return;
390391
}
391392
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.fasterxml.jackson.jr.ob.JSON;
99
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
10+
import com.fasterxml.jackson.jr.ob.api.ValueWriter;
1011

1112
/**
1213
* Helper object used for efficient detection of type information
@@ -36,7 +37,7 @@ public class ValueWriterLocator extends ValueLocatorBase
3637
*/
3738
protected final ConcurrentHashMap<ClassKey, Integer> _knownSerTypes;
3839

39-
protected final CopyOnWriteArrayList<BeanPropertyWriter[]> _knownWriters;
40+
protected final CopyOnWriteArrayList<ValueWriter> _knownWriters;
4041

4142
/**
4243
* Provider for custom writers, if any; may be null.
@@ -83,7 +84,7 @@ protected ValueWriterLocator(int features, ReaderWriterProvider rwp)
8384
{
8485
_features = features;
8586
_knownSerTypes = new ConcurrentHashMap<ClassKey, Integer>(20, 0.75f, 2);
86-
_knownWriters = new CopyOnWriteArrayList<BeanPropertyWriter[]>();
87+
_knownWriters = new CopyOnWriteArrayList<ValueWriter>();
8788
_writeContext = null;
8889
_writerProvider = rwp;
8990
}
@@ -121,7 +122,7 @@ public ValueWriterLocator perOperationInstance(JSONWriter w, int features) {
121122
/**********************************************************************
122123
*/
123124

124-
public BeanPropertyWriter[] getPropertyWriters(int index) {
125+
public ValueWriter getValueWriter(int index) {
125126
// for simplicity, let's allow caller to pass negative id as is
126127
if (index < 0) {
127128
index = -(index+1);
@@ -180,7 +181,7 @@ protected int _findPOJOSerializationType(Class<?> raw)
180181
return I.intValue();
181182
}
182183
// otherwise add at the end, use -(index+1) as id
183-
_knownWriters.add(props);
184+
_knownWriters.add(new BeanWriter(raw, props));
184185
int typeId = -_knownWriters.size();
185186
_knownSerTypes.put(k, Integer.valueOf(typeId));
186187
return typeId;

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Project: jackson-jr
77
2.10.0 (not yet released)
88

99
#63: Change default for `JSON.Feature.USE_FIELDS` to `true` (from false) in 2.10
10+
#65: Allow registration of custom readers, writers (to support 3rd party, custom types)
1011
- Add JDK9+ `module-info.class` using Moditect
1112

1213
2.9.9 (16-May-2019)

0 commit comments

Comments
 (0)