Skip to content

Commit 027ea1b

Browse files
committed
Further fixes to make use of ObjectReadContext instead of ObjectCodec, almost there
1 parent be5b5df commit 027ea1b

19 files changed

+132
-216
lines changed

src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.fasterxml.jackson.core.*;
1414
import com.fasterxml.jackson.core.tree.ArrayTreeNode;
1515
import com.fasterxml.jackson.core.tree.ObjectTreeNode;
16+
import com.fasterxml.jackson.core.type.ResolvedType;
17+
import com.fasterxml.jackson.core.type.TypeReference;
1618
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
1719
import com.fasterxml.jackson.databind.deser.*;
1820
import com.fasterxml.jackson.databind.deser.impl.ObjectIdReader;
@@ -320,7 +322,7 @@ public JavaType getContextualType() {
320322

321323
/*
322324
/**********************************************************
323-
/* ObjectReadContext implementation
325+
/* ObjectReadContext impl, config access
324326
/**********************************************************
325327
*/
326328

@@ -339,6 +341,12 @@ public int getFormatReadFeatures(int defaults) {
339341
return _config.getFormatReadFeatures(defaults);
340342
}
341343

344+
/*
345+
/**********************************************************
346+
/* ObjectReadContext impl, Tree creation
347+
/**********************************************************
348+
*/
349+
342350
@Override
343351
public ArrayTreeNode createArrayNode() {
344352
return getNodeFactory().arrayNode();
@@ -349,6 +357,68 @@ public ObjectTreeNode createObjectNode() {
349357
return getNodeFactory().objectNode();
350358
}
351359

360+
/*
361+
/**********************************************************
362+
/* ObjectReadContext impl, databind
363+
/**********************************************************
364+
*/
365+
366+
@SuppressWarnings("unchecked")
367+
@Override
368+
public <T extends TreeNode> T readTree(JsonParser p) throws IOException {
369+
// NOTE: inlined version of `_bindAsTree()` from `ObjectReader`
370+
JsonToken t = p.currentToken();
371+
if (t == null) {
372+
t = p.nextToken();
373+
if (t == null) { // [databind#1406]: expose end-of-input as `null`
374+
return null;
375+
}
376+
}
377+
if (t == JsonToken.VALUE_NULL) {
378+
return (T) getNodeFactory().nullNode();
379+
}
380+
JsonDeserializer<Object> deser = findRootValueDeserializer(ObjectReader.JSON_NODE_TYPE);
381+
return (T) deser.deserialize(p, this);
382+
}
383+
384+
/**
385+
* Convenience method that may be used by composite or container deserializers,
386+
* for reading one-off values contained (for sequences, it is more efficient
387+
* to actually fetch deserializer once for the whole collection).
388+
*<p>
389+
* NOTE: when deserializing values of properties contained in composite types,
390+
* rather use {@link #readPropertyValue(JsonParser, BeanProperty, Class)};
391+
* this method does not allow use of contextual annotations.
392+
*/
393+
@Override
394+
public <T> T readValue(JsonParser p, Class<T> type) throws IOException {
395+
return readValue(p, getTypeFactory().constructType(type));
396+
}
397+
398+
@Override
399+
public <T> T readValue(JsonParser p, TypeReference<?> refType) throws IOException {
400+
return readValue(p, getTypeFactory().constructType(refType));
401+
}
402+
403+
@Override
404+
public <T> T readValue(JsonParser p, ResolvedType type) throws IOException {
405+
if (!(type instanceof JavaType)) {
406+
throw new UnsupportedOperationException(
407+
"Only support `JavaType` implementation of `ResolvedType`, not: "+type.getClass().getName());
408+
}
409+
return readValue(p, (JavaType) type);
410+
}
411+
412+
@SuppressWarnings("unchecked")
413+
public <T> T readValue(JsonParser p, JavaType type) throws IOException {
414+
JsonDeserializer<Object> deser = findRootValueDeserializer(type);
415+
if (deser == null) {
416+
reportBadDefinition(type,
417+
"Could not find JsonDeserializer for type "+type);
418+
}
419+
return (T) deser.deserialize(p, this);
420+
}
421+
352422
/*
353423
/**********************************************************
354424
/* Public API, config setting accessors
@@ -455,9 +525,6 @@ public final JsonNodeFactory getNodeFactory() {
455525
/**
456526
* Method for checking whether we could find a deserializer
457527
* for given type.
458-
*
459-
* @param type
460-
* @since 2.3
461528
*/
462529
public boolean hasValueDeserializerFor(JavaType type, AtomicReference<Throwable> cause) {
463530
try {
@@ -772,50 +839,17 @@ public Calendar constructCalendar(Date d) {
772839
/* Convenience methods for reading parsed values
773840
/**********************************************************
774841
*/
775-
776-
/**
777-
* Convenience method that may be used by composite or container deserializers,
778-
* for reading one-off values contained (for sequences, it is more efficient
779-
* to actually fetch deserializer once for the whole collection).
780-
*<p>
781-
* NOTE: when deserializing values of properties contained in composite types,
782-
* rather use {@link #readPropertyValue(JsonParser, BeanProperty, Class)};
783-
* this method does not allow use of contextual annotations.
784-
*
785-
* @since 2.4
786-
*/
787-
public <T> T readValue(JsonParser p, Class<T> type) throws IOException {
788-
return readValue(p, getTypeFactory().constructType(type));
789-
}
790-
791-
/**
792-
* @since 2.4
793-
*/
794-
@SuppressWarnings("unchecked")
795-
public <T> T readValue(JsonParser p, JavaType type) throws IOException {
796-
JsonDeserializer<Object> deser = findRootValueDeserializer(type);
797-
if (deser == null) {
798-
reportBadDefinition(type,
799-
"Could not find JsonDeserializer for type "+type);
800-
}
801-
return (T) deser.deserialize(p, this);
802-
}
803-
842+
804843
/**
805844
* Convenience method that may be used by composite or container deserializers,
806845
* for reading one-off values for the composite type, taking into account
807846
* annotations that the property (passed to this method -- usually property that
808847
* has custom serializer that called this method) has.
809-
*
810-
* @since 2.4
811848
*/
812849
public <T> T readPropertyValue(JsonParser p, BeanProperty prop, Class<T> type) throws IOException {
813850
return readPropertyValue(p, prop, getTypeFactory().constructType(type));
814851
}
815852

816-
/**
817-
* @since 2.4
818-
*/
819853
@SuppressWarnings("unchecked")
820854
public <T> T readPropertyValue(JsonParser p, BeanProperty prop, JavaType type) throws IOException {
821855
JsonDeserializer<Object> deser = findContextualValueDeserializer(type, prop);

src/main/java/com/fasterxml/jackson/databind/Module.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,15 @@ public static interface SetupContext
119119
* however, instance will always be of that type.
120120
* This is why return value is declared generic, to allow caller to
121121
* specify context to often avoid casting.
122-
*
123-
* @since 2.0
124122
*/
125-
public <C extends ObjectCodec> C getOwner();
123+
public Object getOwner();
126124

127125
/**
128126
* Accessor for finding {@link TypeFactory} that is currently configured
129127
* by the context.
130128
*<p>
131129
* NOTE: since it is possible that other modules might change or replace
132130
* TypeFactory, use of this method adds order-dependency for registrations.
133-
*
134-
* @since 2.0
135131
*/
136132
public TypeFactory getTypeFactory();
137133

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,6 @@
6767
// and find values by, for example, using a {@link com.fasterxml.jackson.core.JsonPointer} expression:
6868
int age = root.at("/personal/age").getValueAsInt();
6969
</pre>
70-
*<p>
71-
* The main conversion API is defined in {@link ObjectCodec}, so that
72-
* implementation details of this class need not be exposed to
73-
* streaming parser and generator classes. Usage via {@link ObjectCodec} is,
74-
* however, usually only for cases where dependency to {@link ObjectMapper} is
75-
* either not possible (from Streaming API), or undesireable (when only relying
76-
* on Streaming API).
7770
*<p>
7871
* Mapper instances are fully thread-safe provided that ALL configuration of the
7972
* instance occurs before ANY read or write calls. If configuration of a mapper instance
@@ -116,7 +109,6 @@
116109
* (using {@link #setDefaultTyping}).
117110
*/
118111
public class ObjectMapper
119-
extends ObjectCodec
120112
implements Versioned,
121113
java.io.Serializable
122114
{
@@ -490,7 +482,6 @@ public ObjectMapper(TokenStreamFactory jf) {
490482
protected ObjectMapper(ObjectMapper src)
491483
{
492484
_jsonFactory = src._jsonFactory.copy();
493-
_jsonFactory.setCodec(this);
494485
_subtypeResolver = src._subtypeResolver;
495486
_typeFactory = src._typeFactory;
496487
_injectableValues = src._injectableValues;
@@ -535,14 +526,9 @@ public ObjectMapper(TokenStreamFactory jf,
535526
// 06-OCt-2017, tatu: Should probably change dependency one of these days...
536527
// but not today.
537528
if (jf == null) {
538-
_jsonFactory = new JsonFactory();
539-
_jsonFactory.setCodec(this);
540-
} else {
541-
_jsonFactory = jf;
542-
if (jf.getCodec() == null) { // as per [JACKSON-741]
543-
_jsonFactory.setCodec(this);
544-
}
529+
jf = new JsonFactory();
545530
}
531+
_jsonFactory = jf;
546532
_subtypeResolver = new StdSubtypeResolver();
547533
RootNameLookup rootNames = new RootNameLookup();
548534
// and default type factory is shared one
@@ -732,9 +718,9 @@ public Version getMapperVersion() {
732718

733719
@SuppressWarnings("unchecked")
734720
@Override
735-
public <C extends ObjectCodec> C getOwner() {
721+
public Object getOwner() {
736722
// why do we need the cast here?!?
737-
return (C) ObjectMapper.this;
723+
return ObjectMapper.this;
738724
}
739725

740726
@Override
@@ -1733,7 +1719,6 @@ public ObjectMapper setConfig(SerializationConfig config) {
17331719
* @return {@link TokenStreamFactory} that this mapper uses when it needs to
17341720
* construct Json parser and generators
17351721
*/
1736-
@Override
17371722
public TokenStreamFactory getFactory() { return _jsonFactory; }
17381723

17391724
/**
@@ -1753,9 +1738,6 @@ public ObjectMapper setDateFormat(DateFormat dateFormat)
17531738
return this;
17541739
}
17551740

1756-
/**
1757-
* @since 2.5
1758-
*/
17591741
public DateFormat getDateFormat() {
17601742
// arbitrary choice but let's do:
17611743
return _serializationConfig.getDateFormat();
@@ -2152,7 +2134,6 @@ public boolean isEnabled(JsonFactory.Feature f) {
21522134
* @throws JsonMappingException if the input JSON structure does not match structure
21532135
* expected for result type (or has other mismatch issues)
21542136
*/
2155-
@Override
21562137
@SuppressWarnings("unchecked")
21572138
public <T> T readValue(JsonParser p, Class<T> valueType)
21582139
throws IOException, JsonParseException, JsonMappingException
@@ -2177,7 +2158,6 @@ public <T> T readValue(JsonParser p, Class<T> valueType)
21772158
* @throws JsonMappingException if the input JSON structure does not match structure
21782159
* expected for result type (or has other mismatch issues)
21792160
*/
2180-
@Override
21812161
@SuppressWarnings("unchecked")
21822162
public <T> T readValue(JsonParser p, TypeReference<?> valueTypeRef)
21832163
throws IOException, JsonParseException, JsonMappingException
@@ -2201,7 +2181,6 @@ public <T> T readValue(JsonParser p, TypeReference<?> valueTypeRef)
22012181
* @throws JsonMappingException if the input JSON structure does not match structure
22022182
* expected for result type (or has other mismatch issues)
22032183
*/
2204-
@Override
22052184
@SuppressWarnings("unchecked")
22062185
public final <T> T readValue(JsonParser p, ResolvedType valueType)
22072186
throws IOException, JsonParseException, JsonMappingException
@@ -2250,7 +2229,6 @@ public <T> T readValue(JsonParser p, JavaType valueType)
22502229
* @throws JsonParseException if underlying input contains invalid content
22512230
* of type {@link JsonParser} supports (JSON for default case)
22522231
*/
2253-
@Override
22542232
public <T extends TreeNode> T readTree(JsonParser p)
22552233
throws IOException, JsonProcessingException
22562234
{
@@ -2296,21 +2274,6 @@ public <T extends TreeNode> T readTree(JsonParser p)
22962274
*<p>
22972275
* Note that {@link ObjectReader} has more complete set of variants.
22982276
*/
2299-
@Override
2300-
public <T> MappingIterator<T> readValues(JsonParser p, ResolvedType valueType)
2301-
throws IOException, JsonProcessingException
2302-
{
2303-
return readValues(p, (JavaType) valueType);
2304-
}
2305-
2306-
/**
2307-
* Convenience method, equivalent in function to:
2308-
*<pre>
2309-
* readerFor(valueType).readValues(p);
2310-
*</pre>
2311-
*<p>
2312-
* Type-safe overload of {@link #readValues(JsonParser, ResolvedType)}.
2313-
*/
23142277
public <T> MappingIterator<T> readValues(JsonParser p, JavaType valueType)
23152278
throws IOException, JsonProcessingException
23162279
{
@@ -2327,25 +2290,14 @@ public <T> MappingIterator<T> readValues(JsonParser p, JavaType valueType)
23272290
* readerFor(valueType).readValues(p);
23282291
*</pre>
23292292
*<p>
2330-
* Type-safe overload of {@link #readValues(JsonParser, ResolvedType)}.
2293+
* Type-safe overload of {@link #readValues(JsonParser, JavaType)}.
23312294
*/
2332-
@Override
23332295
public <T> MappingIterator<T> readValues(JsonParser p, Class<T> valueType)
23342296
throws IOException, JsonProcessingException
23352297
{
23362298
return readValues(p, _typeFactory.constructType(valueType));
23372299
}
23382300

2339-
/**
2340-
* Method for reading sequence of Objects from parser stream.
2341-
*/
2342-
@Override
2343-
public <T> MappingIterator<T> readValues(JsonParser p, TypeReference<?> valueTypeRef)
2344-
throws IOException, JsonProcessingException
2345-
{
2346-
return readValues(p, _typeFactory.constructType(valueTypeRef));
2347-
}
2348-
23492301
/*
23502302
/**********************************************************
23512303
/* Public API not included in ObjectCodec: deserialization
@@ -2550,11 +2502,10 @@ public void writeValue(JsonGenerator g, Object value)
25502502

25512503
/*
25522504
/**********************************************************
2553-
/* Public API (from TreeCodec via ObjectCodec): Tree Model support
2505+
/* Public API: Tree Model support
25542506
/**********************************************************
25552507
*/
25562508

2557-
@Override
25582509
public void writeTree(JsonGenerator g, TreeNode rootNode)
25592510
throws IOException, JsonProcessingException
25602511
{
@@ -2586,7 +2537,6 @@ public void writeTree(JsonGenerator g, JsonNode rootNode)
25862537
* part of core package, whereas impls are part of mapper
25872538
* package)
25882539
*/
2589-
@Override
25902540
public ObjectNode createObjectNode() {
25912541
return _deserializationConfig.getNodeFactory().objectNode();
25922542
}
@@ -2598,7 +2548,6 @@ public ObjectNode createObjectNode() {
25982548
* part of core package, whereas impls are part of mapper
25992549
* package)
26002550
*/
2601-
@Override
26022551
public ArrayNode createArrayNode() {
26032552
return _deserializationConfig.getNodeFactory().arrayNode();
26042553
}
@@ -2609,7 +2558,6 @@ public ArrayNode createArrayNode() {
26092558
*
26102559
* @param n Root node of the tree that resulting parser will read from
26112560
*/
2612-
@Override
26132561
public JsonParser treeAsTokens(TreeNode n) {
26142562
DeserializationContext ctxt = createDeserializationContext();
26152563
return new TreeTraversingParser((JsonNode) n, ctxt);
@@ -2625,7 +2573,6 @@ public JsonParser treeAsTokens(TreeNode n) {
26252573
*</pre>
26262574
*/
26272575
@SuppressWarnings("unchecked")
2628-
@Override
26292576
public <T> T treeToValue(TreeNode n, Class<T> valueType)
26302577
throws JsonProcessingException
26312578
{

0 commit comments

Comments
 (0)