Skip to content

Commit 2aee38e

Browse files
committed
Continue work on #70, first feature should work (but lacking tests)
1 parent c73029a commit 2aee38e

3 files changed

Lines changed: 90 additions & 5 deletions

File tree

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,71 @@ public abstract class ReaderWriterModifier
1414
{
1515
// // Reader handling
1616

17+
/**
18+
* Method called after {@link ValueReader} to use has been constructed, but
19+
* before it is to be used for the first time. Method may either return that
20+
* reader as-is, or construct a different {@link ValueReader} and return that
21+
* to be used.
22+
*<p>
23+
* This method is often used to create a new {@link ValueReader} that needs to
24+
* delegate to the original reader for some uses, but not all.
25+
*
26+
* @param readContext Context that may be used to access configuration
27+
* @param type Type of values to read
28+
* @param defaultReader {@link ValueReader} that is to be used
29+
*
30+
* @return either {@code defaultReader} as-is, or an alternate {@link ValueReader} to use.
31+
*/
1732
public ValueReader modifyValueReader(JSONReader readContext,
1833
Class<?> type, ValueReader defaultReader) {
19-
return null;
34+
return defaultReader;
2035
}
2136

2237
// // Writer handling
2338

39+
/**
40+
* Method called after {@link ValueWriter} to use has been constructed, but
41+
* before it is to be used for the first time. Method may either return that
42+
* writer as-is, or construct a different {@link ValueWriter} and return that
43+
* to be used.
44+
*<p>
45+
* Note that this method is NOT called for non-POJO JDK "standard" values that
46+
* jackson-jr supports (such as {@link java.lang.Number}s, {@link java.lang.String}
47+
* and {@link java.net.URL}); for these types, {@link #overrideStandardValueWriter}
48+
* is called instead.
49+
*<p>
50+
* This method is often used to create a new {@link ValueReader} that needs to
51+
* delegate to the original reader for some uses, but not all.
52+
*
53+
* @param writeContext Context that may be used to access configuration
54+
* @param type Type of values to write
55+
* @param defaultWriter {@link ValueReader} that is to be used
56+
*
57+
* @return either {@code defaultReader} as-is, or an alternate {@link ValueWriter} to use;
58+
* must not return {@code null}.
59+
*/
2460
public ValueWriter modifyValueWriter(JSONWriter writeContext,
2561
Class<?> type, ValueWriter defaultWriter) {
62+
return defaultWriter;
63+
}
64+
65+
/**
66+
* Method called instead of {@link #modifyValueWriter} for set of non-POJO
67+
* "standard" JDK types that do not have matching {@link ValueWriter} and are
68+
* normally directly serialized by {@link JSONWriter} itself.
69+
* Handler may either return {@code null} to indicate "no override" or return
70+
* custom {@link ValueWriter} to use.
71+
*
72+
* @param writeContext Context that may be used to access configuration
73+
* @param type Type of values to write
74+
* @param stdTypeId Internal identifier of standard type (not usually useful,
75+
* but could potentially be used for delegating)
76+
*
77+
* @return {@code null} if no override should occur, or {@link ValueWriter}
78+
* to use.
79+
*/
80+
public ValueWriter overrideStandardValueWriter(JSONWriter writeContext,
81+
Class<?> type, int stdTypeId) {
2682
return null;
2783
}
2884
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ public ValueReader findReader(Class<?> raw)
217217
* for {@link java.util.Map}, {@link java.util.Collection}).
218218
*/
219219
protected ValueReader createReader(Class<?> contextType, Class<?> type, Type genericType)
220+
{
221+
ValueReader r = _createReader(contextType, type, genericType);
222+
if (_readerModifier != null) {
223+
r = _readerModifier.modifyValueReader(_readContext, type, r);
224+
if (r == null) { // sanity check
225+
throw new IllegalArgumentException("ReaderWriterModifier.modifyValueReader() returned null");
226+
}
227+
}
228+
return r;
229+
}
230+
231+
protected ValueReader _createReader(Class<?> contextType, Class<?> type, Type genericType)
220232
{
221233
if (type == Object.class) {
222234
return AnyReader.std;

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public final int findSerializationType(Class<?> raw)
158158
if (raw == _prevClass) {
159159
return _prevType;
160160
}
161-
if (raw == String.class) {
161+
if (raw == String.class && (_writerModifier == null)) {
162162
return SER_STRING;
163163
}
164164
ClassKey k = (_key == null) ? new ClassKey(raw, _features) : _key.with(raw, _features);
@@ -202,21 +202,38 @@ protected int _findPOJOSerializationType(Class<?> raw)
202202
if (_writerProvider != null) {
203203
ValueWriter w = _writerProvider.findValueWriter(_writeContext, raw);
204204
if (w != null) {
205-
return _registerWriter(raw, w);
205+
return _modifyAndRegisterWriter(raw, w);
206206
}
207207
}
208-
208+
209209
int type = _findSimpleType(raw, true);
210210
if (type == SER_UNKNOWN) {
211211
if (JSON.Feature.HANDLE_JAVA_BEANS.isEnabled(_features)) {
212212
final BeanPropertyWriter[] props = _resolveBeanForSer(raw,
213213
_resolveBeanDef(raw));
214-
return _registerWriter(raw, new BeanWriter(raw, props));
214+
return _modifyAndRegisterWriter(raw, new BeanWriter(raw, props));
215+
}
216+
} else {
217+
if (_writerModifier != null) {
218+
ValueWriter w = _writerModifier.overrideStandardValueWriter(_writeContext, raw, type);
219+
if (w != null) {
220+
return _registerWriter(raw, w);
221+
}
215222
}
216223
}
217224
return type;
218225
}
219226

227+
private int _modifyAndRegisterWriter(Class<?> rawType, ValueWriter w) {
228+
if (_writerModifier != null) {
229+
w = _writerModifier.modifyValueWriter(_writeContext, rawType, w);
230+
if (w == null) { // sanity check
231+
throw new IllegalArgumentException("ReaderWriterModifier.modifyValueWriter() returned null");
232+
}
233+
}
234+
return _registerWriter(rawType, w);
235+
}
236+
220237
private int _registerWriter(Class<?> rawType, ValueWriter valueWriter) {
221238
// Due to concurrent access, possible that someone might have added it
222239
synchronized (_knownWriters) {

0 commit comments

Comments
 (0)