Merge branch '2.8'
diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md
index 0874104..8451799 100644
--- a/.github/ISSUE_TEMPLATE.md
+++ b/.github/ISSUE_TEMPLATE.md
@@ -1,6 +1,7 @@
We appreciate issues as very valuable contributions, but just to make sure here are things that are important to do before filing an issue:
-* Only report issues, and ask Usage Questions on [Jackson-users](https://groups.google.com/forum/#!search/jackson-users) list -- ypu are more likely to get help that way
+* Only report issues (and perhaps request new features, FEATURE):,Usage Questions should be asked on [Jackson-users](https://groups.google.com/forum/#!search/jackson-users) list -- you are more likely to get help that way (and we will promptly close questions-as-issues)
* Check to see if this issue has already been reported (quick glance at existing issues): no deep search necessary, just quick sanity check
* Include version information for Jackson version you use
* (optional but highly recommended) Verify that the problem occurs with the latest patch of same minor version; and even better, if possible, try using the latest stable patch version
+ * For example: if you observe an issue with version `2.4.1`, first upgrade to `2.4.6` to ensure problem has not already been fixed.
diff --git a/.gitignore b/.gitignore
index 1271c27..97155eb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
# use glob syntax.
syntax: glob
+
*.class
*~
*.bak
diff --git a/.travis.yml b/.travis.yml
index d05a18b..c4721ec 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -8,7 +8,9 @@
# Below this line is configuration for deploying to the Sonatype OSS repo
# http://blog.xeiam.com/2013/05/configure-travis-ci-to-deploy-snapshots.html
before_install: "git clone -b travis `git config --get remote.origin.url` target/travis"
-after_success: "mvn source:jar javadoc:jar deploy --settings target/travis/settings.xml"
+after_success:
+ - "mvn source:jar javadoc:jar deploy --settings target/travis/settings.xml"
+ - "mvn -B cobertura:cobertura coveralls:report"
# whitelist
branches:
diff --git a/pom.xml b/pom.xml
index 6965083..c29e287 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,13 +4,13 @@
<parent>
<groupId>com.fasterxml.jackson</groupId>
- <artifactId>jackson-parent</artifactId>
- <version>2.8</version>
+ <artifactId>jackson-bom</artifactId>
+ <version>2.9.0-SNAPSHOT</version>
</parent>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
- <version>2.8.9-SNAPSHOT</version>
+ <version>2.9.0-SNAPSHOT</version>
<name>jackson-databind</name>
<packaging>bundle</packaging>
<description>General data-binding functionality for Jackson: works on core streaming API</description>
@@ -25,9 +25,11 @@
</scm>
<properties>
- <!-- With Jackson 2.8 we will require JDK 7 (except for annotations/streaming),
+ <!-- With Jackson 2.9 we will require JDK 7 (except for annotations/streaming),
and new language features (diamond pattern) may be used.
- Whether JDK dependencies are directly used is an open question as of Feb-2016
+ JDK classes are still loaded dynamically since there isn't much downside
+ (small number of types); this allows use on JDK 6 platforms still (including
+ Android)
-->
<javac.src.version>1.7</javac.src.version>
<javac.target.version>1.7</javac.target.version>
@@ -46,11 +48,16 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
+ <!-- 06-Mar-2017, tatu: Although bom provides for dependencies, some legacy
+ usage seems to benefit from actually specifying version here in case
+ it is dependent on transitively
+ -->
+ <version>${jackson.version.annotations}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
- <version>2.8.8</version>
+ <version>${jackson.version.core}</version>
</dependency>
<!-- and for testing we need a few libraries
@@ -135,6 +142,14 @@
</execution>
</executions>
</plugin>
+
+ <!-- 18-Oct-2016, tatu: Try to make coveralls work -->
+ <plugin>
+ <groupId>org.eluder.coveralls</groupId>
+ <artifactId>coveralls-maven-plugin</artifactId>
+ <version>4.3.0</version>
+ </plugin>
+
</plugins>
</build>
diff --git a/release-notes/CREDITS b/release-notes/CREDITS
index 50583bd..1c2f3dd 100644
--- a/release-notes/CREDITS
+++ b/release-notes/CREDITS
@@ -452,6 +452,8 @@
(2.7.4)
* Reported #1231: `@JsonSerialize(as=superType)` behavior disallowed in 2.7.4
(2.7.5)
+ * Suggested #507: Support for default `@JsonView` for a class
+ (2.9.0)
Tom Mack (tommack@github)
* Reported #1208: treeToValue doesn't handle POJONodes that contain exactly
@@ -466,6 +468,8 @@
Nick Babcock (nickbabcock)
* Reported #1225: `JsonMappingException` should override getProcessor()
(2.7.5)
+ * Suggested #1356: Differentiate between input and code exceptions on deserialization
+ (2.9.0)
Andrew Joseph (apjoseph@github)
* Reported #1248: `Annotated` returns raw type in place of Generic Type in 2.7.x
@@ -617,3 +621,46 @@
Connor Kuhn (ckuhn@github)
* Contributed #1341: FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY
(2.9.0)
+
+Jan Lolling (jlolling@github)
+ * Contributed #1319: Add `ObjectNode.put(String, BigInteger)`
+ (2.9.0)
+
+Michael R Fairhurst (MichaelRFairhurst@github)
+ * Reported #1035: `@JsonAnySetter` assumes key of `String`, does not consider declared type.
+ (2.9.0)
+
+Fabrizio Cucci (fabriziocucci@github)
+ * Reported #1406: `ObjectMapper.readTree()` methods do not return `null` on end-of-input
+ (2.9.0)
+
+Emiliano Clariá (emilianogc@github)
+ * Contributed #1434: Explicitly pass null on invoke calls with no arguments
+ (2.9.0)
+
+Ana Eliza Barbosa (AnaEliza@github)
+ * Contributed #1520: Case insensitive enum deserialization feature.
+ (2.9.0)
+
+Lyor Goldstein (lgoldstein@github)
+ * Reported #1544: `EnumMapDeserializer` assumes a pure `EnumMap` and does not support
+ derived classes
+ (2.9.0)
+
+Harleen Sahni (harleensahni@github)
+ * Reported #403: Make FAIL_ON_NULL_FOR_PRIMITIVES apply to primitive arrays and other
+ types that wrap primitives
+ (2.9.0)
+
+Jared Jacobs (2is10@github)
+ * Requested #1605: Allow serialization of `InetAddress` as simple numeric host address
+ (2.9.0)
+
+Patrick Gunia (pgunia@github)
+ * Reported #1440: Wrong `JsonStreamContext` in `DeserializationProblemHandler` when reading
+ `TokenBuffer` content
+ (2.9.0)
+
+Carsten Wickner (CarstenWickner@github)
+ * Contributed #1522: Global `@JsonInclude(Include.NON_NULL)` for all properties with a specific type
+ (2.9.0)
diff --git a/release-notes/VERSION b/release-notes/VERSION
index 7f50ad0..ca43999 100644
--- a/release-notes/VERSION
+++ b/release-notes/VERSION
@@ -1,8 +1,93 @@
Project: jackson-databind
------------------------------------------------------------------------
-=== Releases ===
+=== Releases ===
------------------------------------------------------------------------
+2.9.0 (not yet released)
+
+#219: SqlDateSerializer does not obey SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS
+ (reported by BrentDouglas@github)
+#265: Add descriptive exception for attempts to use `@JsonWrapped` via Creator parameter
+#291: @JsonTypeInfo with As.EXTERNAL_PROPERTY doesn't work if external type property
+ is referenced more than once
+ (reported by Starkom@github)
+#357: StackOverflowError with contentConverter that returns array type
+ (reported by Florian S)
+#383: Recursive `@JsonUnwrapped` (`child` with same type) fail: "No _valueDeserializer assigned"
+ (reported by tdavis@github)
+#403: Make FAIL_ON_NULL_FOR_PRIMITIVES apply to primitive arrays and other types that wrap primitives
+ (reported by Harleen S)
+#476: Allow "Serialize as POJO" using `@JsonFormat(shape=Shape.OBJECT)` class annotation
+#507: Support for default `@JsonView` for a class
+ (suggested by Mark W)
+#687: Exception deserializing a collection @JsonIdentityInfo and a property based creator
+#865: `JsonFormat.Shape.OBJECT` ignored when class implements `Map.Entry`
+#888: Allow specifying custom exclusion comparator via `@JsonInclude`,
+ using `JsonInclude.Include.CUSTOM`
+#994: `DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS` only works for POJOs, Maps
+#1029: Add a way to define property name aliases
+#1035: `@JsonAnySetter` assumes key of `String`, does not consider declared type.
+ (reported by Michael F)
+#1106: Add `MapperFeature.ALLOW_COERCION_OF_SCALARS` for enabling/disabling coercions
+#1284: Make `StdKeySerializers` use new `JsonGenerator.writeFieldId()` for `int`/`long` keys
+#1320: Add `ObjectNode.put(String, BigInteger)`
+ (proposed by Jan L)
+#1341: `DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY`
+ (contributed by Connor K)
+#1347: Extend `ObjectMapper.configOverrides()` to allow changing visibility rules
+#1356: Differentiate between input and code exceptions on deserialization
+ (suggested by Nick B)
+#1369: Improve `@JsonCreator` detection via `AnnotationIntrospector`
+ by passing `MappingConfig`
+#1371: Add `MapperFeature.INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES` to allow
+ disabling use of `@CreatorProperties` as explicit `@JsonCreator` equivalent
+#1376: Add ability to disable JsonAnySetter/JsonAnyGetter via mixin
+ (suggested by brentryan@github)
+#1399: Add support for `@JsonMerge` to allow "deep update"
+#1402: Use `@JsonSetter(nulls=...)` to specify handling of `null` values during deserialization
+#1406: `ObjectMapper.readTree()` methods do not return `null` on end-of-input
+ (reported by Fabrizio C)
+#1407: `@JsonFormat.pattern` is ignored for `java.sql.Date` valued properties
+ (reported by sangpire@github)
+#1415: Creating CollectionType for non generic collection class broken
+#1428: Allow `@JsonValue` on a field, not just getter
+#1434: Explicitly pass null on invoke calls with no arguments
+ (contributed by Emiliano C)
+#1433: `ObjectMapper.convertValue()` with null does not consider null conversions
+ (`JsonDeserializer.getNullValue()`)
+ (contributed by jdmichal@github)
+#1440: Wrong `JsonStreamContext` in `DeserializationProblemHandler` when reading
+ `TokenBuffer` content
+ (reported by Patrick G)
+#1444: Change `ObjectMapper.setSerializationInclusion()` to apply to content inclusion too
+#1450: `SimpleModule.addKeyDeserializer()' should throw `IllegalArgumentException` if `null`
+ reference of `KeyDeserializer` passed
+ (suggested by PawelJagus@github)
+#1454: Support `@JsonFormat.lenient` for `java.util.Date`, `java.util.Calendar`
+#1474: Replace use of `Class.newInstance()` (deprecated in Java 9) with call via Constructor
+#1480: Add support for serializing `boolean`/`Boolean` as number (0 or 1)
+ (suggested by jwilmoth@github)
+#1520: Case insensitive enum deserialization with `MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS`
+ (contributed by Ana-Eliza B)
+#1522: Global `@JsonInclude(Include.NON_NULL)` for all properties with a specific type
+ (contributed by Carsten W)
+#1544: EnumMapDeserializer assumes a pure EnumMap and does not support EnumMap derived classes
+ (reported by Lyor G)
+#1550: Unexpected behavior with `@JsonInclude(JsonInclude.Include.NON_EMPTY)` and
+ `java.util.Date` serialization
+#1551: `JsonMappingException` with polymorphic type and `JsonIdentityInfo` when basic type is abstract
+ (reported by acm073@github)
+#1552: Map key converted to byte array is not serialized as base64 string
+ (reported by nmatt@github)
+#1554: Support deserialization of `Shape.OBJECT` ("as POJO") for `Map`s (and map-like types)
+#1556: Add `ObjectMapper.updateValue()` method to update instance with given overrides
+ (suggested by syncer@github)
+#1592: Add support for handling primitive/discrepancy problem with type refinements
+#1605: Allow serialization of `InetAddress` as simple numeric host address
+ (requested by Jared J)
+#1616: Extraneous type id mapping added for base type itself
+#1619: By-pass annotation introspection for array types
+
2.8.9 (not yet released)
#1595: `JsonIgnoreProperties.allowSetters` is not working in Jackson 2.8
@@ -103,7 +188,6 @@
2.8.3 (17-Sep-2016)
-#929: `@JsonCreator` not working on a factory with multiple arguments for a enum type
#1351: `@JsonInclude(NON_DEFAULT)` doesn't omit null fields
(reported by Gili T)
#1353: Improve error-handling for `java.net.URL` deserialization
diff --git a/src/main/java/com/fasterxml/jackson/databind/AnnotationIntrospector.java b/src/main/java/com/fasterxml/jackson/databind/AnnotationIntrospector.java
index c1e3655..76ced23 100644
--- a/src/main/java/com/fasterxml/jackson/databind/AnnotationIntrospector.java
+++ b/src/main/java/com/fasterxml/jackson/databind/AnnotationIntrospector.java
@@ -3,13 +3,10 @@
import java.lang.annotation.Annotation;
import java.util.*;
-import com.fasterxml.jackson.annotation.JsonCreator;
-import com.fasterxml.jackson.annotation.JsonFormat;
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.Versioned;
+
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
@@ -20,8 +17,6 @@
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
-import com.fasterxml.jackson.databind.type.MapLikeType;
-import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.Converter;
import com.fasterxml.jackson.databind.util.NameTransformer;
@@ -67,7 +62,7 @@
* {@link com.fasterxml.jackson.annotation.JsonManagedReference}
*/
MANAGED_REFERENCE
-
+
/**
* Reference property that Jackson manages by suppressing it during serialization,
* and reconstructing during deserialization.
@@ -238,56 +233,12 @@
*/
public JsonIgnoreProperties.Value findPropertyIgnorals(Annotated ac)
{
- // 28-Apr-2016, tatu: For backwards compatibility let's delegate to older
- // methods, for Jackson 2.8
- String[] ignorals = findPropertiesToIgnore(ac, true);
- Boolean b = (ac instanceof AnnotatedClass) ?
- findIgnoreUnknownProperties((AnnotatedClass) ac) : null;
- JsonIgnoreProperties.Value v;
- if (ignorals == null) {
- if (b == null) {
- return null;
- }
- v = JsonIgnoreProperties.Value.empty();
- } else {
- v = JsonIgnoreProperties.Value.forIgnoredProperties(ignorals);
- }
- if (b != null) {
- v = b.booleanValue() ? v.withIgnoreUnknown() : v.withoutIgnoreUnknown();
- }
- return v;
+ // 18-Oct-2016, tatu: Used to call deprecated methods for backwards
+ // compatibility in 2.8, but not any more in 2.9
+ return JsonIgnoreProperties.Value.empty();
}
/**
- * @param forSerialization True if requesting properties to ignore for serialization;
- * false if for deserialization
- *
- * @since 2.6
- *
- * @deprecated Since 2.8, use {@link #findPropertyIgnorals} instead
- */
- @Deprecated // since 2.8
- public String[] findPropertiesToIgnore(Annotated ac, boolean forSerialization) {
- return null;
- }
-
- /**
- * @deprecated Since 2.6, use variant that takes second argument.
- */
- @Deprecated // since 2.6
- public String[] findPropertiesToIgnore(Annotated ac) {
- return null;
- }
-
- /**
- * Method for checking whether an annotation indicates that all unknown properties
- *
- * @deprecated Since 2.8, use {@link #findPropertyIgnorals} instead
- */
- @Deprecated // since 2.8
- public Boolean findIgnoreUnknownProperties(AnnotatedClass ac) { return null; }
-
- /**
* Method for checking whether properties that have specified type
* (class, not generics aware) should be completely ignored for
* serialization and deserialization purposes.
@@ -335,6 +286,35 @@
*/
public String findClassDescription(AnnotatedClass ac) { return null; }
+ /**
+ * @param forSerialization True if requesting properties to ignore for serialization;
+ * false if for deserialization
+ *
+ * @since 2.6
+ *
+ * @deprecated Since 2.8, use {@link #findPropertyIgnorals} instead
+ */
+ @Deprecated // since 2.8
+ public String[] findPropertiesToIgnore(Annotated ac, boolean forSerialization) {
+ return null;
+ }
+
+ /**
+ * @deprecated Since 2.6, use variant that takes second argument.
+ */
+ @Deprecated // since 2.6
+ public String[] findPropertiesToIgnore(Annotated ac) {
+ return null;
+ }
+
+ /**
+ * Method for checking whether an annotation indicates that all unknown properties
+ *
+ * @deprecated Since 2.8, use {@link #findPropertyIgnorals} instead
+ */
+ @Deprecated // since 2.8
+ public Boolean findIgnoreUnknownProperties(AnnotatedClass ac) { return null; }
+
/*
/**********************************************************
/* Property auto-detection
@@ -365,7 +345,7 @@
* instantiating resolver builder, but also configuring it based on
* relevant annotations (not including ones checked with a call to
* {@link #findSubtypes}
- *
+ *
* @param config Configuration settings in effect (for serialization or deserialization)
* @param ac Annotated class to check for annotations
* @param baseType Base java type of value for which resolver is to be found
@@ -487,18 +467,26 @@
*
* @return Identifier of value to inject, if any; null if no injection
* indicator is found
+ *
+ * @since 2.9
*/
- public Object findInjectableValueId(AnnotatedMember m) { return null; }
+ public JacksonInject.Value findInjectableValue(AnnotatedMember m) {
+ // 05-Apr-2017, tatu: Just for 2.9, call deprecated method to help
+ // with some cases of overrides for legacy code
+ Object id = findInjectableValueId(m);
+ if (id != null) {
+ return JacksonInject.Value.forId(id);
+ }
+ return null;
+ }
/**
* Method that can be called to check whether this member has
* an annotation that suggests whether value for matching property
* is required or not.
- *
- * @since 2.0
*/
public Boolean hasRequiredMarker(AnnotatedMember m) { return null; }
-
+
/**
* Method for checking if annotated property (represented by a field or
* getter/setter method) has definitions for views it is to be included in.
@@ -507,6 +495,9 @@
* otherwise it will only be included for views included in returned
* array. View matches are checked using class inheritance rules (sub-classes
* inherit inclusions of super-classes)
+ *<p>
+ * Since 2.9 this method may also be called to find "default view(s)" for
+ * {@link AnnotatedClass}
*
* @param a Annotated property (represented by a method, field or ctor parameter)
* @return Array of views (represented by classes) that the property is included in;
@@ -522,7 +513,9 @@
*
* @since 2.1
*/
- public JsonFormat.Value findFormat(Annotated memberOrClass) { return null; }
+ public JsonFormat.Value findFormat(Annotated memberOrClass) {
+ return JsonFormat.Value.empty();
+ }
/**
* Method used to check if specified property has annotation that indicates
@@ -588,6 +581,16 @@
public String findImplicitPropertyName(AnnotatedMember member) { return null; }
/**
+ * Method called to find if given property has alias(es) defined.
+ *
+ * @return `null` if member has no information; otherwise a `List` (possibly
+ * empty) of aliases to use.
+ *
+ * @since 2.9
+ */
+ public List<PropertyName> findPropertyAliases(Annotated ann) { return null; }
+
+ /**
* Method for finding optional access definition for a property, annotated
* on one of its accessors. If a definition for read-only, write-only
* or read-write cases, visibility rules may be modified. Note, however,
@@ -611,6 +614,14 @@
return null;
}
+ /**
+ * @deprecated Since 2.9 Use {@link #findInjectableValue} instead
+ */
+ @Deprecated // since 2.9
+ public Object findInjectableValueId(AnnotatedMember m) {
+ return null;
+ }
+
/*
/**********************************************************
/* Serialization: general annotations
@@ -721,6 +732,18 @@
}
/**
+ * Method for checking inclusion criteria for a type (Class) or property (yes, method
+ * name is bit unfortunate -- not just for properties!).
+ * In case of class, acts as the default for properties POJO contains; for properties
+ * acts as override for class defaults and possible global defaults.
+ *
+ * @since 2.6
+ */
+ public JsonInclude.Value findPropertyInclusion(Annotated a) {
+ return JsonInclude.Value.empty();
+ }
+
+ /**
* Method for checking whether given annotated entity (class, method,
* field) defines which Bean/Map properties are to be included in
* serialization.
@@ -746,9 +769,9 @@
* Method for checking whether content (entries) of a {@link java.util.Map} property
* are to be included during serialization or not.
* NOTE: this is NOT called for POJO properties, or array/Collection elements.
- *
+ *
* @since 2.5
- *
+ *
* @deprecated Since 2.7 Use {@link #findPropertyInclusion} instead
*/
@Deprecated // since 2.7
@@ -756,18 +779,6 @@
return defValue;
}
- /**
- * Method for checking inclusion criteria for a type (Class) or property (yes, method
- * name is bit unfortunate -- not just for properties!).
- * In case of class, acts as the default for properties POJO contains; for properties
- * acts as override for class defaults and possible global defaults.
- *
- * @since 2.6
- */
- public JsonInclude.Value findPropertyInclusion(Annotated a) {
- return JsonInclude.Value.empty();
- }
-
/*
/**********************************************************
/* Serialization: type refinements
@@ -775,14 +786,19 @@
*/
/**
- * Method for accessing annotated type definition that a
- * method/field can have, to be used as the type for serialization
- * instead of the runtime type.
- * Type returned (if any) needs to be widening conversion (super-type).
- * Declared return type of the method is also considered acceptable.
+ * Method called to find out possible type refinements to use
+ * for deserialization, including not just value itself but
+ * key and/or content type, if type has those.
*
- * @return Class to use instead of runtime type
- *
+ * @since 2.7
+ */
+ public JavaType refineSerializationType(final MapperConfig<?> config,
+ final Annotated a, final JavaType baseType) throws JsonMappingException
+ {
+ return baseType;
+ }
+
+ /**
* @deprecated Since 2.7 call {@link #refineSerializationType} instead
*/
@Deprecated // since 2.7
@@ -791,13 +807,6 @@
}
/**
- * Method for finding possible widening type definition that a property
- * value can have, to define less specific key type to use for serialization.
- * It should be only be used with {@link java.util.Map} types.
- *
- * @return Class specifying more general type to use instead of
- * declared type, if annotation found; null if not
- *
* @deprecated Since 2.7 call {@link #refineSerializationType} instead
*/
@Deprecated // since 2.7
@@ -806,138 +815,13 @@
}
/**
- * Method for finding possible widening type definition that a property
- * value can have, to define less specific key type to use for serialization.
- * It should be only used with structured types (arrays, collections, maps).
- *
- * @return Class specifying more general type to use instead of
- * declared type, if annotation found; null if not
- *
* @deprecated Since 2.7 call {@link #refineSerializationType} instead
*/
@Deprecated // since 2.7
public Class<?> findSerializationContentType(Annotated am, JavaType baseType) {
return null;
}
-
- /**
- * Method called to find out possible type refinements to use
- * for deserialization.
- *
- * @since 2.7
- */
- public JavaType refineSerializationType(final MapperConfig<?> config,
- final Annotated a, final JavaType baseType) throws JsonMappingException
- {
- JavaType type = baseType;
- final TypeFactory tf = config.getTypeFactory();
-
- // 10-Oct-2015, tatu: For 2.7, we'll need to delegate back to
- // now-deprecated secondary methods; this because while
- // direct sub-class not yet retrofitted may only override
- // those methods. With 2.8 or later we may consider removal
- // of these methods
-
-
- // Ok: start by refining the main type itself; common to all types
- Class<?> serClass = findSerializationType(a);
- if (serClass != null) {
- if (type.hasRawClass(serClass)) {
- // 30-Nov-2015, tatu: As per [databind#1023], need to allow forcing of
- // static typing this way
- type = type.withStaticTyping();
- } else {
- Class<?> currRaw = type.getRawClass();
- try {
- // 11-Oct-2015, tatu: For deser, we call `TypeFactory.constructSpecializedType()`,
- // may be needed here too in future?
- if (serClass.isAssignableFrom(currRaw)) { // common case
- type = tf.constructGeneralizedType(type, serClass);
- } else if (currRaw.isAssignableFrom(serClass)) { // specialization, ok as well
- type = tf.constructSpecializedType(type, serClass);
- } else {
- throw new JsonMappingException(null,
- String.format("Can not refine serialization type %s into %s; types not related",
- type, serClass.getName()));
- }
- } catch (IllegalArgumentException iae) {
- throw new JsonMappingException(null,
- String.format("Failed to widen type %s with annotation (value %s), from '%s': %s",
- type, serClass.getName(), a.getName(), iae.getMessage()),
- iae);
- }
- }
- }
- // Then further processing for container types
-
- // First, key type (for Maps, Map-like types):
- if (type.isMapLikeType()) {
- JavaType keyType = type.getKeyType();
- Class<?> keyClass = findSerializationKeyType(a, keyType);
- if (keyClass != null) {
- if (keyType.hasRawClass(keyClass)) {
- keyType = keyType.withStaticTyping();
- } else {
- Class<?> currRaw = keyType.getRawClass();
- try {
- // 19-May-2016, tatu: As per [databind#1231], [databind#1178] may need to actually
- // specialize (narrow) type sometimes, even if more commonly opposite
- // is needed.
- if (keyClass.isAssignableFrom(currRaw)) { // common case
- keyType = tf.constructGeneralizedType(keyType, keyClass);
- } else if (currRaw.isAssignableFrom(keyClass)) { // specialization, ok as well
- keyType = tf.constructSpecializedType(keyType, keyClass);
- } else {
- throw new JsonMappingException(null,
- String.format("Can not refine serialization key type %s into %s; types not related",
- keyType, keyClass.getName()));
- }
- } catch (IllegalArgumentException iae) {
- throw new JsonMappingException(null,
- String.format("Failed to widen key type of %s with concrete-type annotation (value %s), from '%s': %s",
- type, keyClass.getName(), a.getName(), iae.getMessage()),
- iae);
- }
- }
- type = ((MapLikeType) type).withKeyType(keyType);
- }
- }
-
- JavaType contentType = type.getContentType();
- if (contentType != null) { // collection[like], map[like], array, reference
- // And then value types for all containers:
- Class<?> contentClass = findSerializationContentType(a, contentType);
- if (contentClass != null) {
- if (contentType.hasRawClass(contentClass)) {
- contentType = contentType.withStaticTyping();
- } else {
- // 03-Apr-2016, tatu: As per [databind#1178], may need to actually
- // specialize (narrow) type sometimes, even if more commonly opposite
- // is needed.
- Class<?> currRaw = contentType.getRawClass();
- try {
- if (contentClass.isAssignableFrom(currRaw)) { // common case
- contentType = tf.constructGeneralizedType(contentType, contentClass);
- } else if (currRaw.isAssignableFrom(contentClass)) { // specialization, ok as well
- contentType = tf.constructSpecializedType(contentType, contentClass);
- } else {
- throw new JsonMappingException(null,
- String.format("Can not refine serialization content type %s into %s; types not related",
- contentType, contentClass.getName()));
- }
- } catch (IllegalArgumentException iae) { // shouldn't really happen
- throw new JsonMappingException(null,
- String.format("Internal error: failed to refine value type of %s with concrete-type annotation (value %s), from '%s': %s",
- type, contentClass.getName(), a.getName(), iae.getMessage()),
- iae);
- }
- }
- type = type.withContentType(contentType);
- }
- }
- return type;
- }
-
+
/*
/**********************************************************
/* Serialization: class annotations
@@ -991,14 +875,6 @@
* @since 2.1
*/
public PropertyName findNameForSerialization(Annotated a) {
- /*
- if (name != null) {
- if (name.length() == 0) { // empty String means 'default'
- return PropertyName.USE_DEFAULT;
- }
- return new PropertyName(name);
- }
- */
return null;
}
@@ -1008,11 +884,71 @@
* should be used as "the value" of the object instance; usually
* serialized as a primitive value such as String or number.
*
- * @return True if such annotation is found (and is not disabled);
- * false if no enabled annotation is found
+ * @return {@link Boolean#TRUE} if such annotation is found and is not disabled;
+ * {@link Boolean#FALSE} if disabled annotation (block) is found (to indicate
+ * accessor is definitely NOT to be used "as value"); or `null` if no
+ * information found.
+ *
+ * @since 2.9
*/
- public boolean hasAsValueAnnotation(AnnotatedMethod am) {
- return false;
+ public Boolean hasAsValue(Annotated a) {
+ // 20-Nov-2016, tatu: Delegate in 2.9; remove redirect from later versions
+ if (a instanceof AnnotatedMethod) {
+ if (hasAsValueAnnotation((AnnotatedMethod) a)) {
+ return true;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Method for checking whether given method has an annotation
+ * that suggests that the method is to serve as "any setter";
+ * method to be used for accessing set of miscellaneous "extra"
+ * properties, often bound with matching "any setter" method.
+ *
+ * @return True if such annotation is found (and is not disabled),
+ * false otherwise
+ *
+ * @since 2.9
+ */
+ public Boolean hasAnyGetter(Annotated a) {
+
+ // 21-Nov-2016, tatu: Delegate in 2.9; remove redirect from later versions
+ if (a instanceof AnnotatedMethod) {
+ if (hasAnyGetterAnnotation((AnnotatedMethod) a)) {
+ return true;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Method for efficiently figuring out which if given set of <code>Enum</code> values
+ * have explicitly defined name. Method will overwrite entries in incoming <code>names</code>
+ * array with explicit names found, if any, leaving other entries unmodified.
+ *<p>
+ * Default implementation will simply delegate to {@link #findEnumValue}, which is close
+ * enough, although unfortunately NOT 100% equivalent (as it will also consider <code>name()</code>
+ * to give explicit value).
+ *
+ * @since 2.7
+ */
+ public String[] findEnumValues(Class<?> enumType, Enum<?>[] enumValues, String[] names) {
+ // 18-Oct-2016, tatu: In 2.8 delegated to deprecated method; not so in 2.9 and beyond
+ return names;
+ }
+
+ /**
+ * Finds the Enum value that should be considered the default value, if possible.
+ *
+ * @param enumCls The Enum class to scan for the default value.
+ * @return null if none found or it's not possible to determine one.
+ *
+ * @since 2.8
+ */
+ public Enum<?> findDefaultEnumValue(Class<Enum<?>> enumCls) {
+ return null;
}
/**
@@ -1033,42 +969,21 @@
}
/**
- * Method for efficiently figuring out which if given set of <code>Enum</code> values
- * have explicitly defined name. Method will overwrite entries in incoming <code>names</code>
- * array with explicit names found, if any, leaving other entries unmodified.
- *<p>
- * Default implementation will simply delegate to {@link #findEnumValue}, which is close
- * enough, although unfortunately NOT 100% equivalent (as it will also consider <code>name()</code>
- * to give explicit value).
- *
- * @since 2.7
+ * @deprecated Since 2.9 Use {@link #hasAsValue(Annotated)} instead.
*/
- public String[] findEnumValues(Class<?> enumType, Enum<?>[] enumValues, String[] names) {
- for (int i = 0, len = enumValues.length; i < len; ++i) {
- /* 12-Mar-2016, tatu: This is quite tricky, considering that we should NOT
- * overwrite values with default `name`... so for now, let's only delegate
- * if no value has been set. Still not optimal but has to do
- */
- // TODO: In 2.8, stop delegation?
- if (names[i] == null) {
- names[i] = findEnumValue(enumValues[i]);
- }
- }
- return names;
+ @Deprecated // since 2.9
+ public boolean hasAsValueAnnotation(AnnotatedMethod am) {
+ return false;
}
/**
- * Finds the Enum value that should be considered the default value, if possible.
- *
- * @param enumCls The Enum class to scan for the default value.
- * @return null if none found or it's not possible to determine one.
- *
- * @since 2.8
+ * @deprecated Since 2.9 Use {@link #hasAnyGetter} instead
*/
- public Enum<?> findDefaultEnumValue(Class<Enum<?>> enumCls) {
- return null;
+ @Deprecated
+ public boolean hasAnyGetterAnnotation(AnnotatedMethod am) {
+ return false;
}
-
+
/*
/**********************************************************
/* Deserialization: general annotations
@@ -1175,65 +1090,9 @@
public JavaType refineDeserializationType(final MapperConfig<?> config,
final Annotated a, final JavaType baseType) throws JsonMappingException
{
- JavaType type = baseType;
- final TypeFactory tf = config.getTypeFactory();
-
- // 10-Oct-2015, tatu: For 2.7, we'll need to delegate back to
- // now-deprecated secondary methods; this because while
- // direct sub-class not yet retrofitted may only override
- // those methods. With 2.8 or later we may consider removal
- // of these methods
-
-
- // Ok: start by refining the main type itself; common to all types
- Class<?> valueClass = findDeserializationType(a, type);
- if ((valueClass != null) && !type.hasRawClass(valueClass)) {
- try {
- type = tf.constructSpecializedType(type, valueClass);
- } catch (IllegalArgumentException iae) {
- throw new JsonMappingException(null,
- String.format("Failed to narrow type %s with annotation (value %s), from '%s': %s",
- type, valueClass.getName(), a.getName(), iae.getMessage()),
- iae);
- }
- }
- // Then further processing for container types
-
- // First, key type (for Maps, Map-like types):
- if (type.isMapLikeType()) {
- JavaType keyType = type.getKeyType();
- Class<?> keyClass = findDeserializationKeyType(a, keyType);
- if (keyClass != null) {
- try {
- keyType = tf.constructSpecializedType(keyType, keyClass);
- type = ((MapLikeType) type).withKeyType(keyType);
- } catch (IllegalArgumentException iae) {
- throw new JsonMappingException(null,
- String.format("Failed to narrow key type of %s with concrete-type annotation (value %s), from '%s': %s",
- type, keyClass.getName(), a.getName(), iae.getMessage()),
- iae);
- }
- }
- }
- JavaType contentType = type.getContentType();
- if (contentType != null) { // collection[like], map[like], array, reference
- // And then value types for all containers:
- Class<?> contentClass = findDeserializationContentType(a, contentType);
- if (contentClass != null) {
- try {
- contentType = tf.constructSpecializedType(contentType, contentClass);
- type = type.withContentType(contentType);
- } catch (IllegalArgumentException iae) {
- throw new JsonMappingException(null,
- String.format("Failed to narrow value type of %s with concrete-type annotation (value %s), from '%s': %s",
- type, contentClass.getName(), a.getName(), iae.getMessage()),
- iae);
- }
- }
- }
- return type;
+ return baseType;
}
-
+
/**
* Method for accessing annotated type definition that a
* property can have, to be used as the type for deserialization
@@ -1325,7 +1184,7 @@
public JsonPOJOBuilder.Value findPOJOBuilderConfig(AnnotatedClass ac) {
return null;
}
-
+
/*
/**********************************************************
/* Deserialization: property annotations
@@ -1347,14 +1206,6 @@
* @since 2.1
*/
public PropertyName findNameForDeserialization(Annotated a) {
- /*
- if (name != null) {
- if (name.length() == 0) { // empty String means 'default'
- return PropertyName.USE_DEFAULT;
- }
- return new PropertyName(name);
- }
- */
return null;
}
@@ -1366,24 +1217,60 @@
*
* @return True if such annotation is found (and is not disabled),
* false otherwise
+ *
+ * @since 2.9
*/
- public boolean hasAnySetterAnnotation(AnnotatedMethod am) {
+ public Boolean hasAnySetter(Annotated a) {
return false;
}
/**
- * Method for checking whether given method has an annotation
- * that suggests that the method is to serve as "any setter";
- * method to be used for accessing set of miscellaneous "extra"
- * properties, often bound with matching "any setter" method.
+ * Method for finding possible settings for property, given annotations
+ * on an accessor.
*
- * @return True if such annotation is found (and is not disabled),
- * false otherwise
+ * @since 2.9
*/
- public boolean hasAnyGetterAnnotation(AnnotatedMethod am) {
- return false;
+ public JsonSetter.Value findSetterInfo(Annotated a) {
+ return JsonSetter.Value.empty();
}
-
+
+ /**
+ * Method for finding merge settings for property, if any.
+ *
+ * @since 2.9
+ */
+ public Boolean findMergeInfo(Annotated a) {
+ return null;
+ }
+
+ /**
+ * Method called to check whether potential Creator (constructor or static factory
+ * method) has explicit annotation to indicate it as actual Creator; and if so,
+ * which {@link com.fasterxml.jackson.annotation.JsonCreator.Mode} to use.
+ *<p>
+ * NOTE: caller needs to consider possibility of both `null` (no annotation found)
+ * and {@link com.fasterxml.jackson.annotation.JsonCreator.Mode#DISABLED} (annotation found,
+ * but disabled); latter is necessary as marker in case multiple introspectors are chained,
+ * as well as possibly as when using mix-in annotations.
+ *
+ * @param config Configuration settings in effect (for serialization or deserialization)
+ * @param a Annotated accessor (usually constructor or static method) to check
+ *
+ * @since 2.9
+ */
+ public JsonCreator.Mode findCreatorAnnotation(MapperConfig<?> config, Annotated a) {
+ // 13-Sep-2016, tatu: for backwards compatibility, implement using delegation
+ /// (remove from version AFTER 2.9)
+ if (hasCreatorAnnotation(a)) {
+ JsonCreator.Mode mode = findCreatorBinding(a);
+ if (mode == null) {
+ mode = JsonCreator.Mode.DEFAULT;
+ }
+ return mode;
+ }
+ return null;
+ }
+
/**
* Method for checking whether given annotated item (method, constructor)
* has an annotation
@@ -1393,7 +1280,10 @@
*
* @return True if such annotation is found (and is not disabled),
* false otherwise
+ *
+ * @deprecated Since 2.9 use {@link #findCreatorAnnotation} instead.
*/
+ @Deprecated
public boolean hasCreatorAnnotation(Annotated a) {
return false;
}
@@ -1405,11 +1295,21 @@
* creator with implicit but no explicit name for the argument).
*
* @since 2.5
+ * @deprecated Since 2.9 use {@link #findCreatorAnnotation} instead.
*/
+ @Deprecated
public JsonCreator.Mode findCreatorBinding(Annotated a) {
return null;
}
-
+
+ /**
+ * @deprecated Since 2.9 use {@link #hasAnySetter} instead.
+ */
+ @Deprecated // since 2.9
+ public boolean hasAnySetterAnnotation(AnnotatedMethod am) {
+ return false;
+ }
+
/*
/**********************************************************
/* Overridable methods: may be used as low-level extension
diff --git a/src/main/java/com/fasterxml/jackson/databind/BeanDescription.java b/src/main/java/com/fasterxml/jackson/databind/BeanDescription.java
index cd21182..24ddfe9 100644
--- a/src/main/java/com/fasterxml/jackson/databind/BeanDescription.java
+++ b/src/main/java/com/fasterxml/jackson/databind/BeanDescription.java
@@ -23,7 +23,7 @@
{
/**
* Bean type information, including raw class and possible
- * * generics information
+ * generics information
*/
protected final JavaType _type;
@@ -114,12 +114,22 @@
*/
public abstract List<BeanPropertyDefinition> findProperties();
+ public abstract Set<String> getIgnoredPropertyNames();
+
/**
* Method for locating all back-reference properties (setters, fields) bean has
+ *
+ * @since 2.9
*/
- public abstract Map<String,AnnotatedMember> findBackReferenceProperties();
+ public abstract List<BeanPropertyDefinition> findBackReferences();
- public abstract Set<String> getIgnoredPropertyNames();
+ /**
+ * Method for locating all back-reference properties (setters, fields) bean has
+ *
+ * @deprecated Since 2.9 use {@link #findBackReferences()} instead
+ */
+ @Deprecated
+ public abstract Map<String,AnnotatedMember> findBackReferenceProperties();
/*
/**********************************************************
@@ -162,39 +172,63 @@
/* Basic API for finding property accessors
/**********************************************************
*/
-
- public abstract AnnotatedMember findAnyGetter();
/**
- * Method used to locate the method of introspected class that
- * implements {@link com.fasterxml.jackson.annotation.JsonAnySetter}. If no such method exists
- * null is returned. If more than one are found, an exception
- * is thrown.
- * Additional checks are also made to see that method signature
- * is acceptable: needs to take 2 arguments, first one String or
- * Object; second any can be any type.
- */
- public abstract AnnotatedMethod findAnySetter();
-
- /**
- * Method used to locate the field of the class that implements
- * {@link com.fasterxml.jackson.annotation.JsonAnySetter} If no such method
- * exists null is returned. If more than one are found, an exception is thrown.
- *
- * @since 2.8
- */
- public abstract AnnotatedMember findAnySetterField();
-
- /**
- * Method for locating the getter method that is annotated with
+ * Method for locating accessor (readable field, or "getter" method)
+ * that has
* {@link com.fasterxml.jackson.annotation.JsonValue} annotation,
* if any. If multiple ones are found,
* an error is reported by throwing {@link IllegalArgumentException}
+ *
+ * @since 2.9
*/
- public abstract AnnotatedMethod findJsonValueMethod();
+ public abstract AnnotatedMember findJsonValueAccessor();
+
+ public abstract AnnotatedMember findAnyGetter();
+
+ /**
+ * Method used to locate a mutator (settable field, or 2-argument set method)
+ * of introspected class that
+ * implements {@link com.fasterxml.jackson.annotation.JsonAnySetter}.
+ * If no such mutator exists null is returned. If more than one are found,
+ * an exception is thrown.
+ * Additional checks are also made to see that method signature
+ * is acceptable: needs to take 2 arguments, first one String or
+ * Object; second any can be any type.
+ *
+ * @since 2.9
+ */
+ public abstract AnnotatedMember findAnySetterAccessor();
public abstract AnnotatedMethod findMethod(String name, Class<?>[] paramTypes);
+
+ @Deprecated // since 2.9
+ public abstract AnnotatedMethod findJsonValueMethod();
+ /**
+ * @deprecated Since 2.9: use {@link #findAnySetterAccessor} instead
+ */
+ @Deprecated
+ public AnnotatedMethod findAnySetter() {
+ AnnotatedMember m = findAnySetterAccessor();
+ if (m instanceof AnnotatedMethod) {
+ return (AnnotatedMethod) m;
+ }
+ return null;
+ }
+
+ /**
+ * @deprecated Since 2.9: use {@link #findAnySetterAccessor} instead
+ */
+ @Deprecated
+ public AnnotatedMember findAnySetterField() {
+ AnnotatedMember m = findAnySetterAccessor();
+ if (m instanceof AnnotatedField) {
+ return m;
+ }
+ return null;
+ }
+
/*
/**********************************************************
/* Basic API, class configuration
@@ -279,4 +313,13 @@
* suitable default constructor was found; null otherwise.
*/
public abstract Object instantiateBean(boolean fixAccess);
+
+ /**
+ * Method for finding out if the POJO specifies default view(s) to
+ * use for properties, considering both per-type annotations and
+ * global default settings.
+ *
+ * @since 2.9
+ */
+ public abstract Class<?>[] findDefaultViews();
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/BeanProperty.java b/src/main/java/com/fasterxml/jackson/databind/BeanProperty.java
index 525aa11..e8a562c 100644
--- a/src/main/java/com/fasterxml/jackson/databind/BeanProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/BeanProperty.java
@@ -1,12 +1,17 @@
package com.fasterxml.jackson.databind;
import java.lang.annotation.Annotation;
+import java.util.Collections;
+import java.util.List;
import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonFormat.Value;
import com.fasterxml.jackson.annotation.JsonInclude;
+
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;
+import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.Annotations;
import com.fasterxml.jackson.databind.util.Named;
@@ -14,7 +19,7 @@
* Bean properties are logical entities that represent data
* that Java objects (POJOs (Plain Old Java Objects), sometimes also called "beans")
* contain; and that are accessed using accessors (methods like getters
- * and setters, fields, constructor parametrers).
+ * and setters, fields, constructor parameters).
* Instances allow access to annotations directly associated
* to property (via field or method), as well as contextual
* annotations (annotations for class that contains properties).
@@ -26,7 +31,7 @@
* {@link com.fasterxml.jackson.databind.deser.ContextualDeserializer}
* resolution occurs (<code>createContextual(...)</code> method is called).
* References may (need to) be retained by serializers and deserializers,
- * especially when further resolving dependant handlers like value
+ * especially when further resolving dependent handlers like value
* serializers/deserializers or structured types.
*/
public interface BeanProperty extends Named
@@ -161,6 +166,16 @@
*/
public JsonInclude.Value findPropertyInclusion(MapperConfig<?> config, Class<?> baseType);
+ /**
+ * Method for accessing set of possible alternate names that are accepted
+ * during deserialization.
+ *
+ * @return List (possibly empty) of alternate names; never null
+ *
+ * @since 2.9
+ */
+ public List<PropertyName> findAliases(MapperConfig<?> config);
+
/*
/**********************************************************
/* Schema/introspection support
@@ -196,8 +211,11 @@
* Simple stand-alone implementation, useful as a placeholder
* or base class for more complex implementations.
*/
- public static class Std implements BeanProperty
+ public static class Std implements BeanProperty,
+ java.io.Serializable // 2.9
{
+ private static final long serialVersionUID = 1L;
+
protected final PropertyName _name;
protected final JavaType _type;
protected final PropertyName _wrapperName;
@@ -211,29 +229,32 @@
*/
protected final AnnotatedMember _member;
- /**
- * Annotations defined in the context class (if any); may be null
- * if no annotations were found
- */
- protected final Annotations _contextAnnotations;
-
public Std(PropertyName name, JavaType type, PropertyName wrapperName,
- Annotations contextAnnotations, AnnotatedMember member,
- PropertyMetadata metadata)
+ AnnotatedMember member, PropertyMetadata metadata)
{
_name = name;
_type = type;
_wrapperName = wrapperName;
_metadata = metadata;
_member = member;
- _contextAnnotations = contextAnnotations;
+ }
+
+ /**
+ * @deprecated Since 2.9
+ */
+ @Deprecated
+ public Std(PropertyName name, JavaType type, PropertyName wrapperName,
+ Annotations contextAnnotations,
+ AnnotatedMember member, PropertyMetadata metadata)
+ {
+ this(name, type, wrapperName, member, metadata);
}
/**
* @since 2.6
*/
public Std(Std base, JavaType newType) {
- this(base._name, newType, base._wrapperName, base._contextAnnotations, base._member, base._metadata);
+ this(base._name, newType, base._wrapperName, base._member, base._metadata);
}
public Std withType(JavaType type) {
@@ -247,7 +268,7 @@
@Override
public <A extends Annotation> A getContextAnnotation(Class<A> acls) {
- return (_contextAnnotations == null) ? null : _contextAnnotations.get(acls);
+ return null;
}
@Override
@@ -279,7 +300,7 @@
@Override
public JsonInclude.Value findPropertyInclusion(MapperConfig<?> config, Class<?> baseType)
{
- JsonInclude.Value v0 = config.getDefaultPropertyInclusion(baseType);
+ JsonInclude.Value v0 = config.getDefaultInclusion(baseType, _type.getRawClass());
AnnotationIntrospector intr = config.getAnnotationIntrospector();
if ((intr == null) || (_member == null)) {
return v0;
@@ -291,6 +312,13 @@
return v0.withOverrides(v);
}
+ @Override
+ public List<PropertyName> findAliases(MapperConfig<?> config) {
+ // 26-Feb-2017, tatu: Do we really need to allow actual definition?
+ // For now, let's not.
+ return Collections.emptyList();
+ }
+
@Override public String getName() { return _name.getSimpleName(); }
@Override public PropertyName getFullName() { return _name; }
@Override public JavaType getType() { return _type; }
@@ -314,4 +342,91 @@
throw new UnsupportedOperationException("Instances of "+getClass().getName()+" should not get visited");
}
}
+
+ /**
+ * Alternative "Null" implementation that can be used in cases where a non-null
+ * {@link BeanProperty} is needed
+ *
+ * @since 2.9
+ */
+ public static class Bogus implements BeanProperty
+ {
+ @Override
+ public String getName() {
+ return "";
+ }
+
+ @Override
+ public PropertyName getFullName() {
+ return PropertyName.NO_NAME;
+ }
+
+ @Override
+ public JavaType getType() {
+ return TypeFactory.unknownType();
+ }
+
+ @Override
+ public PropertyName getWrapperName() {
+ return null;
+ }
+
+ @Override
+ public PropertyMetadata getMetadata() {
+ return PropertyMetadata.STD_REQUIRED_OR_OPTIONAL;
+ }
+
+ @Override
+ public boolean isRequired() {
+ return false;
+ }
+
+ @Override
+ public boolean isVirtual() {
+ return false;
+ }
+
+ @Override
+ public <A extends Annotation> A getAnnotation(Class<A> acls) {
+ return null;
+ }
+
+ @Override
+ public <A extends Annotation> A getContextAnnotation(Class<A> acls) {
+ return null;
+ }
+
+ @Override
+ public AnnotatedMember getMember() {
+ return null;
+ }
+
+ @Override
+ @Deprecated
+ public Value findFormatOverrides(AnnotationIntrospector intr) {
+ return Value.empty();
+ }
+
+ @Override
+ public Value findPropertyFormat(MapperConfig<?> config, Class<?> baseType) {
+ return Value.empty();
+ }
+
+ @Override
+ public com.fasterxml.jackson.annotation.JsonInclude.Value findPropertyInclusion(
+ MapperConfig<?> config, Class<?> baseType)
+ {
+ return null;
+ }
+
+ @Override
+ public List<PropertyName> findAliases(MapperConfig<?> config) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor,
+ SerializerProvider provider) throws JsonMappingException {
+ }
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/DatabindContext.java b/src/main/java/com/fasterxml/jackson/databind/DatabindContext.java
index f42a0bb..e86af75 100644
--- a/src/main/java/com/fasterxml/jackson/databind/DatabindContext.java
+++ b/src/main/java/com/fasterxml/jackson/databind/DatabindContext.java
@@ -25,6 +25,15 @@
*/
public abstract class DatabindContext
{
+ /**
+ * Let's limit length of error messages, for cases where underlying data
+ * may be very large -- no point in spamming logs with megabytes of meaningless
+ * data.
+ *
+ * @since 2.9
+ */
+ private final static int MAX_ERROR_STR_LEN = 500;
+
/*
/**********************************************************
/* Generic config access
@@ -134,7 +143,10 @@
* type (usually {@link java.lang.Class})
*/
public JavaType constructType(Type type) {
- return getTypeFactory().constructType(type);
+ if (type == null) {
+ return null;
+ }
+ return getTypeFactory().constructType(type);
}
/**
@@ -149,6 +161,52 @@
return getConfig().constructSpecializedType(baseType, subclass);
}
+ /**
+ * Lookup method called when code needs to resolve class name from input;
+ * usually simple lookup
+ *
+ * @since 2.9
+ */
+ public JavaType resolveSubType(JavaType baseType, String subClass)
+ throws JsonMappingException
+ {
+ // 30-Jan-2010, tatu: Most ids are basic class names; so let's first
+ // check if any generics info is added; and only then ask factory
+ // to do translation when necessary
+ if (subClass.indexOf('<') > 0) {
+ // note: may want to try combining with specialization (esp for EnumMap)?
+ return getTypeFactory().constructFromCanonical(subClass);
+ }
+ Class<?> cls;
+ try {
+ cls = getTypeFactory().findClass(subClass);
+ } catch (ClassNotFoundException e) { // let caller handle this problem
+ return null;
+ } catch (Exception e) {
+ throw invalidTypeIdException(baseType, subClass, String.format(
+ "problem: (%s) %s",
+ e.getClass().getName(), e.getMessage()));
+ }
+ if (baseType.isTypeOrSuperTypeOf(cls)) {
+ return getTypeFactory().constructSpecializedType(baseType, cls);
+ }
+ throw invalidTypeIdException(baseType, subClass, "Not a subtype");
+ }
+
+ /**
+ * Helper method for constructing exception to indicate that given type id
+ * could not be resolved to a valid subtype of specified base type.
+ * Most commonly called during polymorphic deserialization.
+ *<p>
+ * Note that most of the time this method should NOT be called directly: instead,
+ * method <code>handleUnknownTypeId()</code> should be called which will call this method
+ * if necessary.
+ *
+ * @since 2.9
+ */
+ protected abstract JsonMappingException invalidTypeIdException(JavaType baseType, String typeId,
+ String extraDesc);
+
public abstract TypeFactory getTypeFactory();
/*
@@ -224,4 +282,97 @@
}
return (Converter<Object,Object>) conv;
}
+
+ /*
+ /**********************************************************
+ /* Error reporting
+ /**********************************************************
+ */
+
+ /**
+ * Helper method called to indicate a generic problem that stems from type
+ * definition(s), not input data, or input/output state; typically this
+ * means throwing a {@link com.fasterxml.jackson.databind.exc.InvalidDefinitionException}.
+ *
+ * @since 2.9
+ */
+ public abstract <T> T reportBadDefinition(JavaType type, String msg) throws JsonMappingException;
+
+ /**
+ * @since 2.9
+ */
+ public <T> T reportBadDefinition(Class<?> type, String msg) throws JsonMappingException {
+ return reportBadDefinition(constructType(type), msg);
+ }
+
+ /*
+ /**********************************************************
+ /* Helper methods
+ /**********************************************************
+ */
+
+ /**
+ * @since 2.9
+ */
+ protected final String _format(String msg, Object... msgArgs) {
+ if (msgArgs.length > 0) {
+ return String.format(msg, msgArgs);
+ }
+ return msg;
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected final String _truncate(String desc) {
+ if (desc == null) {
+ return "";
+ }
+ if (desc.length() <= MAX_ERROR_STR_LEN) {
+ return desc;
+ }
+ return desc.substring(0, MAX_ERROR_STR_LEN) + "]...[" + desc.substring(desc.length() - MAX_ERROR_STR_LEN);
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected String _quotedString(String desc) {
+ if (desc == null) {
+ return "[N/A]";
+ }
+ // !!! should we quote it? (in case there are control chars, linefeeds)
+ return String.format("\"%s\"", _truncate(desc));
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected String _colonConcat(String msgBase, String extra) {
+ if (extra == null) {
+ return msgBase;
+ }
+ return msgBase + ": " + extra;
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected String _calcName(Class<?> cls) {
+ if (cls.isArray()) {
+ return _calcName(cls.getComponentType())+"[]";
+ }
+ return cls.getName();
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected String _desc(String desc) {
+ if (desc == null) {
+ return "[N/A]";
+ }
+ // !!! should we quote it? (in case there are control chars, linefeeds)
+ return _truncate(desc);
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/DeserializationConfig.java b/src/main/java/com/fasterxml/jackson/databind/DeserializationConfig.java
index 68556ad..34d2fb4 100644
--- a/src/main/java/com/fasterxml/jackson/databind/DeserializationConfig.java
+++ b/src/main/java/com/fasterxml/jackson/databind/DeserializationConfig.java
@@ -1,10 +1,7 @@
package com.fasterxml.jackson.databind;
-import java.text.DateFormat;
import java.util.*;
-import com.fasterxml.jackson.annotation.*;
-
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.cfg.*;
@@ -12,7 +9,6 @@
import com.fasterxml.jackson.databind.introspect.*;
import com.fasterxml.jackson.databind.jsontype.*;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
-import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.LinkedNode;
import com.fasterxml.jackson.databind.util.RootNameLookup;
@@ -29,8 +25,8 @@
extends MapperConfigBase<DeserializationFeature, DeserializationConfig>
implements java.io.Serializable // since 2.1
{
- // since 2.5
- private static final long serialVersionUID = 1;
+ // since 2.9
+ private static final long serialVersionUID = 2;
/*
/**********************************************************
@@ -93,7 +89,7 @@
/*
/**********************************************************
- /* Life-cycle, constructors
+ /* Life-cycle, primary constructors for new instances
/**********************************************************
*/
@@ -101,8 +97,8 @@
* Constructor used by ObjectMapper to create default configuration object instance.
*/
public DeserializationConfig(BaseSettings base,
- SubtypeResolver str, SimpleMixInResolver mixins,
- RootNameLookup rootNames, ConfigOverrides configOverrides)
+ SubtypeResolver str, SimpleMixInResolver mixins, RootNameLookup rootNames,
+ ConfigOverrides configOverrides)
{
super(base, str, mixins, rootNames, configOverrides);
_deserFeatures = collectFeatureDefaults(DeserializationFeature.class);
@@ -115,14 +111,31 @@
}
/**
- * @deprecated Since 2.8, remove from 2.9 or later
+ * Copy-constructor used for making a copy used by new {@link ObjectMapper}.
+ *
+ * @since 2.9
*/
- @Deprecated
- public DeserializationConfig(BaseSettings base, SubtypeResolver str,
- SimpleMixInResolver mixins, RootNameLookup rootNames) {
- this(base, str, mixins, rootNames, null);
+ protected DeserializationConfig(DeserializationConfig src,
+ SimpleMixInResolver mixins, RootNameLookup rootNames,
+ ConfigOverrides configOverrides)
+ {
+ super(src, mixins, rootNames, configOverrides);
+ _deserFeatures = src._deserFeatures;
+ _problemHandlers = src._problemHandlers;
+ _nodeFactory = src._nodeFactory;
+ _parserFeatures = src._parserFeatures;
+ _parserFeaturesToChange = src._parserFeaturesToChange;
+ _formatReadFeatures = src._formatReadFeatures;
+ _formatReadFeaturesToChange = src._formatReadFeaturesToChange;
}
-
+
+ /*
+ /**********************************************************
+ /* Life-cycle, secondary constructors to support
+ /* "mutant factories", with single property changes
+ /**********************************************************
+ */
+
private DeserializationConfig(DeserializationConfig src,
int mapperFeatures, int deserFeatures,
int parserFeatures, int parserFeatureMask,
@@ -239,109 +252,37 @@
_formatReadFeaturesToChange = src._formatReadFeaturesToChange;
}
- /**
- * Copy-constructor used for making a copy used by new {@link ObjectMapper}.
- *
- * @since 2.8
- */
- protected DeserializationConfig(DeserializationConfig src, SimpleMixInResolver mixins,
- RootNameLookup rootNames, ConfigOverrides configOverrides)
- {
- super(src, mixins, rootNames, configOverrides);
- _deserFeatures = src._deserFeatures;
- _problemHandlers = src._problemHandlers;
- _nodeFactory = src._nodeFactory;
- _parserFeatures = src._parserFeatures;
- _parserFeaturesToChange = src._parserFeaturesToChange;
- _formatReadFeatures = src._formatReadFeatures;
- _formatReadFeaturesToChange = src._formatReadFeaturesToChange;
- }
-
// for unit tests only:
protected BaseSettings getBaseSettings() { return _base; }
/*
/**********************************************************
- /* Life-cycle, factory methods from MapperConfig
+ /* Life-cycle, general factory methods from MapperConfig(Base)
/**********************************************************
*/
-
- @Override
- public DeserializationConfig with(MapperFeature... features)
- {
- int newMapperFlags = _mapperFeatures;
- for (MapperFeature f : features) {
- newMapperFlags |= f.getMask();
- }
- return (newMapperFlags == _mapperFeatures) ? this :
- new DeserializationConfig(this, newMapperFlags, _deserFeatures,
- _parserFeatures, _parserFeaturesToChange,
- _formatReadFeatures, _formatReadFeaturesToChange);
-
+
+ @Override // since 2.9
+ protected final DeserializationConfig _withBase(BaseSettings newBase) {
+ return (_base == newBase) ? this : new DeserializationConfig(this, newBase);
}
- @Override
- public DeserializationConfig without(MapperFeature... features)
- {
- int newMapperFlags = _mapperFeatures;
- for (MapperFeature f : features) {
- newMapperFlags &= ~f.getMask();
- }
- return (newMapperFlags == _mapperFeatures) ? this :
- new DeserializationConfig(this, newMapperFlags, _deserFeatures,
- _parserFeatures, _parserFeaturesToChange,
- _formatReadFeatures, _formatReadFeaturesToChange);
+ @Override // since 2.9
+ protected final DeserializationConfig _withMapperFeatures(int mapperFeatures) {
+ return new DeserializationConfig(this, mapperFeatures, _deserFeatures,
+ _parserFeatures, _parserFeaturesToChange,
+ _formatReadFeatures, _formatReadFeaturesToChange);
}
- @Override
- public DeserializationConfig with(MapperFeature feature, boolean state)
- {
- int newMapperFlags;
- if (state) {
- newMapperFlags = _mapperFeatures | feature.getMask();
- } else {
- newMapperFlags = _mapperFeatures & ~feature.getMask();
- }
- return (newMapperFlags == _mapperFeatures) ? this :
- new DeserializationConfig(this, newMapperFlags, _deserFeatures,
- _parserFeatures, _parserFeaturesToChange,
- _formatReadFeatures, _formatReadFeaturesToChange);
- }
-
- @Override
- public DeserializationConfig with(ClassIntrospector ci) {
- return _withBase(_base.withClassIntrospector(ci));
- }
-
- @Override
- public DeserializationConfig with(AnnotationIntrospector ai) {
- return _withBase(_base.withAnnotationIntrospector(ai));
- }
-
- @Override
- public DeserializationConfig with(VisibilityChecker<?> vc) {
- return _withBase(_base.withVisibilityChecker(vc));
- }
-
- @Override
- public DeserializationConfig withVisibility(PropertyAccessor forMethod, JsonAutoDetect.Visibility visibility) {
- return _withBase( _base.withVisibility(forMethod, visibility));
- }
-
- @Override
- public DeserializationConfig with(TypeResolverBuilder<?> trb) {
- return _withBase(_base.withTypeResolverBuilder(trb));
- }
+ /*
+ /**********************************************************
+ /* Life-cycle, specific factory methods from MapperConfig
+ /**********************************************************
+ */
@Override
public DeserializationConfig with(SubtypeResolver str) {
return (_subtypeResolver == str) ? this : new DeserializationConfig(this, str);
}
-
- @Override
- public DeserializationConfig with(PropertyNamingStrategy pns) {
- return _withBase(_base.withPropertyNamingStrategy(pns));
- }
@Override
public DeserializationConfig withRootName(PropertyName rootName) {
@@ -356,58 +297,14 @@
}
@Override
- public DeserializationConfig with(TypeFactory tf) {
- return _withBase( _base.withTypeFactory(tf));
- }
-
- @Override
- public DeserializationConfig with(DateFormat df) {
- return _withBase(_base.withDateFormat(df));
- }
-
- @Override
- public DeserializationConfig with(HandlerInstantiator hi) {
- return _withBase(_base.withHandlerInstantiator(hi));
- }
-
- @Override
- public DeserializationConfig withInsertedAnnotationIntrospector(AnnotationIntrospector ai) {
- return _withBase(_base.withInsertedAnnotationIntrospector(ai));
- }
-
- @Override
- public DeserializationConfig withAppendedAnnotationIntrospector(AnnotationIntrospector ai) {
- return _withBase(_base.withAppendedAnnotationIntrospector(ai));
- }
-
- @Override
public DeserializationConfig withView(Class<?> view) {
return (_view == view) ? this : new DeserializationConfig(this, view);
}
@Override
- public DeserializationConfig with(Locale l) {
- return _withBase(_base.with(l));
- }
-
- @Override
- public DeserializationConfig with(TimeZone tz) {
- return _withBase(_base.with(tz));
- }
-
- @Override
- public DeserializationConfig with(Base64Variant base64) {
- return _withBase(_base.with(base64));
- }
-
- @Override
public DeserializationConfig with(ContextAttributes attrs) {
return (attrs == _attributes) ? this : new DeserializationConfig(this, attrs);
}
-
- private final DeserializationConfig _withBase(BaseSettings newBase) {
- return (_base == newBase) ? this : new DeserializationConfig(this, newBase);
- }
/*
/**********************************************************
@@ -734,84 +631,6 @@
/*
/**********************************************************
- /* MapperConfig implementation/overrides: introspection
- /**********************************************************
- */
-
- /**
- * Method for getting {@link AnnotationIntrospector} configured
- * to introspect annotation values used for configuration.
- */
- @Override
- public AnnotationIntrospector getAnnotationIntrospector()
- {
- /* 29-Jul-2009, tatu: it's now possible to disable use of
- * annotations; can be done using "no-op" introspector
- */
- if (isEnabled(MapperFeature.USE_ANNOTATIONS)) {
- return super.getAnnotationIntrospector();
- }
- return NopAnnotationIntrospector.instance;
- }
-
- /**
- * Accessor for getting bean description that only contains class
- * annotations: useful if no getter/setter/creator information is needed.
- */
- @Override
- public BeanDescription introspectClassAnnotations(JavaType type) {
- return getClassIntrospector().forClassAnnotations(this, type, this);
- }
-
- /**
- * Accessor for getting bean description that only contains immediate class
- * annotations: ones from the class, and its direct mix-in, if any, but
- * not from super types.
- */
- @Override
- public BeanDescription introspectDirectClassAnnotations(JavaType type) {
- return getClassIntrospector().forDirectClassAnnotations(this, type, this);
- }
-
- /*
- /**********************************************************
- /* Configuration: default settings with per-type overrides
- /**********************************************************
- */
-
- @Override
- public JsonInclude.Value getDefaultPropertyInclusion() {
- return EMPTY_INCLUDE;
- }
-
- @Override
- public JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType) {
- ConfigOverride overrides = findConfigOverride(baseType);
- if (overrides != null) {
- JsonInclude.Value v = overrides.getInclude();
- if (v != null) {
- return v;
- }
- }
- return EMPTY_INCLUDE;
- }
-
- @Override
- public JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType,
- JsonInclude.Value defaultIncl)
- {
- ConfigOverride overrides = findConfigOverride(baseType);
- if (overrides != null) {
- JsonInclude.Value v = overrides.getInclude();
- if (v != null) {
- return v;
- }
- }
- return defaultIncl;
- }
-
- /*
- /**********************************************************
/* MapperConfig implementation/overrides: other
/**********************************************************
*/
diff --git a/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java b/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java
index 0609c1e..3f12f55 100644
--- a/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java
+++ b/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java
@@ -9,12 +9,16 @@
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.annotation.ObjectIdResolver;
+
import com.fasterxml.jackson.core.*;
+
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.impl.ObjectIdReader;
import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId;
import com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
@@ -31,14 +35,14 @@
* Used to allow passing in configuration settings and reusable temporary
* objects (scrap arrays, containers).
*<p>
- * Instance life-cycle is such that an partially configured "blueprint" object
+ * Instance life-cycle is such that a partially configured "blueprint" object
* is registered with {@link ObjectMapper} (and {@link ObjectReader},
- * and when an actual instance is needed for deserialization,
- * a fully configured instance will
- * be created using a method in excented API of sub-class
+ * and when actual instance is needed for deserialization,
+ * a fully configured instance will be created using a method in extended internal
+ * API of sub-class
* ({@link com.fasterxml.jackson.databind.deser.DefaultDeserializationContext#createInstance}).
* Each instance is guaranteed to only be used from single-threaded context;
- * instances may be reused iff no configuration has changed.
+ * instances may be reused if (and only if) no configuration has changed.
*<p>
* Defined as abstract class so that implementations must define methods
* for reconfiguring blueprints and creating instances.
@@ -49,13 +53,6 @@
{
private static final long serialVersionUID = 1L; // 2.6
- /**
- * Let's limit length of error messages, for cases where underlying data
- * may be very large -- no point in spamming logs with megs of meaningless
- * data.
- */
- private final static int MAX_ERROR_STR_LEN = 500;
-
/*
/**********************************************************
/* Configuration, immutable
@@ -160,8 +157,10 @@
throw new IllegalArgumentException("Can not pass null DeserializerFactory");
}
_factory = df;
- _cache = (cache == null) ? new DeserializerCache() : cache;
-
+ if (cache == null) {
+ cache = new DeserializerCache();
+ }
+ _cache = cache;
_featureFlags = 0;
_config = null;
_injectableValues = null;
@@ -374,9 +373,11 @@
public final Object findInjectableValue(Object valueId,
BeanProperty forProperty, Object beanInstance)
+ throws JsonMappingException
{
if (_injectableValues == null) {
- throw new IllegalStateException("No 'injectableValues' configured, can not inject value with id ["+valueId+"]");
+ reportBadDefinition(ClassUtil.classOf(valueId), String.format(
+"No 'injectableValues' configured, can not inject value with id [%s]", valueId));
}
return _injectableValues.findInjectableValue(valueId, this, forProperty, beanInstance);
}
@@ -539,7 +540,7 @@
* </pre>
*/
public final JavaType constructType(Class<?> cls) {
- return _config.constructType(cls);
+ return (cls == null) ? null : _config.constructType(cls);
}
/**
@@ -686,19 +687,6 @@
return deser;
}
- @Deprecated // since 2.5; remove from 2.9
- public JsonDeserializer<?> handlePrimaryContextualization(JsonDeserializer<?> deser, BeanProperty prop) throws JsonMappingException {
- return handlePrimaryContextualization(deser, prop, TypeFactory.unknownType());
- }
-
- @Deprecated // since 2.5; remove from 2.9
- public JsonDeserializer<?> handleSecondaryContextualization(JsonDeserializer<?> deser, BeanProperty prop) throws JsonMappingException {
- if (deser instanceof ContextualDeserializer) {
- deser = ((ContextualDeserializer) deser).createContextual(this, prop);
- }
- return deser;
- }
-
/*
/**********************************************************
/* Parsing methods that may use reusable/-cyclable objects
@@ -765,7 +753,8 @@
public <T> T readValue(JsonParser p, JavaType type) throws IOException {
JsonDeserializer<Object> deser = findRootValueDeserializer(type);
if (deser == null) {
- reportMappingException("Could not find JsonDeserializer for type %s", type);
+ reportBadDefinition(type,
+ "Could not find JsonDeserializer for type "+type);
}
return (T) deser.deserialize(p, this);
}
@@ -789,10 +778,9 @@
public <T> T readPropertyValue(JsonParser p, BeanProperty prop, JavaType type) throws IOException {
JsonDeserializer<Object> deser = findContextualValueDeserializer(type, prop);
if (deser == null) {
- String propName = (prop == null) ? "NULL" : ("'"+prop.getName()+"'");
- reportMappingException(
+ return reportBadDefinition(type, String.format(
"Could not find JsonDeserializer for type %s (via property %s)",
- type, propName);
+ type, ClassUtil.nameOf(prop)));
}
return (T) deser.deserialize(p, this);
}
@@ -859,9 +847,7 @@
throws IOException
{
// but if not handled, just throw exception
- if (msgArgs.length > 0) {
- msg = String.format(msg, msgArgs);
- }
+ msg = _format(msg, msgArgs);
LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
while (h != null) {
// Can bail out if it's handled
@@ -905,9 +891,7 @@
throws IOException
{
// but if not handled, just throw exception
- if (msgArgs.length > 0) {
- msg = String.format(msg, msgArgs);
- }
+ msg = _format(msg, msgArgs);
LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
while (h != null) {
// Can bail out if it's handled
@@ -950,9 +934,7 @@
String msg, Object... msgArgs)
throws IOException
{
- if (msgArgs.length > 0) {
- msg = String.format(msg, msgArgs);
- }
+ msg = _format(msg, msgArgs);
LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
while (h != null) {
// Can bail out if it's handled
@@ -980,36 +962,51 @@
* just skipping it) to keep input state valid
*
* @param instClass Type that was to be instantiated
+ * @param valueInst (optional) Value instantiator to be used, if any; null if type does not
+ * use one for instantiation (custom deserialiers don't; standard POJO deserializer does)
* @param p Parser that points to the JSON value to decode
*
* @return Object that should be constructed, if any; has to be of type <code>instClass</code>
*
- * @since 2.8
+ * @since 2.9 (2.8 had alternate that did not take <code>ValueInstantiator</code>)
*/
- public Object handleMissingInstantiator(Class<?> instClass, JsonParser p,
- String msg, Object... msgArgs)
+ @SuppressWarnings("resource")
+ public Object handleMissingInstantiator(Class<?> instClass, ValueInstantiator valueInst,
+ JsonParser p, String msg, Object... msgArgs)
throws IOException
{
- if (msgArgs.length > 0) {
- msg = String.format(msg, msgArgs);
+ if (p == null) {
+ p = getParser();
}
+ msg = _format(msg, msgArgs);
LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
while (h != null) {
// Can bail out if it's handled
Object instance = h.value().handleMissingInstantiator(this,
- instClass, p, msg);
+ instClass, valueInst, p, msg);
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
// Sanity check for broken handlers, otherwise nasty to debug:
if ((instance == null) || instClass.isInstance(instance)) {
return instance;
}
- throw instantiationException(instClass, String.format(
- "DeserializationProblemHandler.handleMissingInstantiator() for type %s returned value of type %s",
- instClass, instance.getClass()));
+ reportBadDefinition(constructType(instClass), String.format(
+"DeserializationProblemHandler.handleMissingInstantiator() for type %s returned value of type %s",
+ instClass, ClassUtil.classNameOf(instance)));
}
h = h.next();
}
- throw instantiationException(instClass, msg);
+
+ // 16-Oct-2016, tatu: This is either a definition problem (if no applicable creator
+ // exists), or input mismatch problem (otherwise) since none of existing creators
+ // match with token.
+ if ((valueInst != null) && !valueInst.canInstantiate()) {
+ msg = String.format("Can not construct instance of %s (no Creators, like default construct, exist): %s",
+ ClassUtil.nameOf(instClass), msg);
+ return reportBadDefinition(constructType(instClass), msg);
+ }
+ msg = String.format("Can not construct instance of %s (although at least one Creator exists): %s",
+ ClassUtil.nameOf(instClass), msg);
+ return reportInputMismatch(instClass, msg);
}
/**
@@ -1039,19 +1036,17 @@
Object instance = h.value().handleInstantiationProblem(this, instClass, argument, t);
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
// Sanity check for broken handlers, otherwise nasty to debug:
- if ((instance == null) || instClass.isInstance(instance)) {
+ if (instClass.isInstance(instance)) {
return instance;
}
- throw instantiationException(instClass, String.format(
- "DeserializationProblemHandler.handleInstantiationProblem() for type %s returned value of type %s",
- instClass, instance.getClass()));
+ reportBadDefinition(constructType(instClass), String.format(
+"DeserializationProblemHandler.handleInstantiationProblem() for type %s returned value of type %s",
+ instClass, ClassUtil.classNameOf(instance)));
}
h = h.next();
}
// 18-May-2016, tatu: Only wrap if not already a valid type to throw
- if (t instanceof IOException) {
- throw (IOException) t;
- }
+ ClassUtil.throwIfIOE(t);
throw instantiationException(instClass, t);
}
@@ -1074,7 +1069,7 @@
{
return handleUnexpectedToken(instClass, p.getCurrentToken(), p, null);
}
-
+
/**
* Method that deserializers should call if the first token of the value to
* deserialize is of unexpected type (that is, type of token that deserializer
@@ -1083,6 +1078,7 @@
* {@link JsonToken#VALUE_NUMBER_INT} or {@link JsonToken#VALUE_NUMBER_FLOAT}.
*
* @param instClass Type that was to be instantiated
+ * @param t Token encountered that does match expected
* @param p Parser that points to the JSON value to decode
*
* @return Object that should be constructed, if any; has to be of type <code>instClass</code>
@@ -1090,13 +1086,10 @@
* @since 2.8
*/
public Object handleUnexpectedToken(Class<?> instClass, JsonToken t,
- JsonParser p,
- String msg, Object... msgArgs)
+ JsonParser p, String msg, Object... msgArgs)
throws IOException
{
- if (msgArgs.length > 0) {
- msg = String.format(msg, msgArgs);
- }
+ msg = _format(msg, msgArgs);
LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
while (h != null) {
Object instance = h.value().handleUnexpectedToken(this,
@@ -1105,8 +1098,9 @@
if ((instance == null) || instClass.isInstance(instance)) {
return instance;
}
- reportMappingException("DeserializationProblemHandler.handleUnexpectedToken() for type %s returned value of type %s",
- instClass, instance.getClass());
+ reportBadDefinition(constructType(instClass), String.format(
+ "DeserializationProblemHandler.handleUnexpectedToken() for type %s returned value of type %s",
+ instance.getClass()));
}
h = h.next();
}
@@ -1119,7 +1113,7 @@
_calcName(instClass), t);
}
}
- reportMappingException(msg);
+ reportInputMismatch(instClass, msg);
return null; // never gets here
}
@@ -1129,7 +1123,7 @@
* actual type; usually since there is no mapping defined.
* Default implementation will try to call {@link DeserializationProblemHandler#handleUnknownTypeId}
* on configured handlers, if any, to allow for recovery; if recovery does not
- * succeed, will throw exception constructed with {@link #unknownTypeIdException}.
+ * succeed, will throw exception constructed with {@link #invalidTypeIdException}.
*
* @param baseType Base type from which resolution starts
* @param id Type id that could not be converted
@@ -1158,7 +1152,7 @@
if (type.isTypeOrSubTypeOf(baseType.getRawClass())) {
return type;
}
- throw unknownTypeIdException(baseType, id,
+ throw invalidTypeIdException(baseType, id,
"problem handler tried to resolve into non-subtype: "+type);
}
h = h.next();
@@ -1167,13 +1161,44 @@
if (!isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)) {
return null;
}
- throw unknownTypeIdException(baseType, id, extraDesc);
+ throw invalidTypeIdException(baseType, id, extraDesc);
+ }
+
+ /**
+ * @since 2.9
+ */
+ public JavaType handleMissingTypeId(JavaType baseType,
+ TypeIdResolver idResolver, String extraDesc) throws IOException
+ {
+ LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
+ while (h != null) {
+ // Can bail out if it's handled
+ JavaType type = h.value().handleMissingTypeId(this, baseType, idResolver, extraDesc);
+ if (type != null) {
+ if (type.hasRawClass(Void.class)) {
+ return null;
+ }
+ // But ensure there's type compatibility
+ if (type.isTypeOrSubTypeOf(baseType.getRawClass())) {
+ return type;
+ }
+ throw invalidTypeIdException(baseType, null,
+ "problem handler tried to resolve into non-subtype: "+type);
+ }
+ h = h.next();
+ }
+ // 09-Mar-2017, tatu: We may want to consider yet another feature at some
+ // point to allow returning `null`... but that seems bit risky for now
+// if (!isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)) {
+// return null;
+// }
+ throw missingTypeIdException(baseType, extraDesc);
}
/*
/**********************************************************
/* Methods for problem reporting, in cases where recovery
- /* is not considered possible
+ /* is not considered possible: input problem
/**********************************************************
*/
@@ -1185,15 +1210,122 @@
* recovery is attempted (via {@link DeserializationProblemHandler}, as
* problem is considered to be difficult to recover from, in general.
*
+ * @since 2.9
+ */
+ public void reportWrongTokenException(JsonDeserializer<?> deser,
+ JsonToken expToken, String msg, Object... msgArgs)
+ throws JsonMappingException
+ {
+ msg = _format(msg, msgArgs);
+ throw wrongTokenException(getParser(), deser.handledType(), expToken, msg);
+ }
+
+ /**
+ * Method for deserializers to call
+ * when the token encountered was of type different than what <b>should</b>
+ * be seen at that position, usually within a sequence of expected tokens.
+ * Note that this method will throw a {@link JsonMappingException} and no
+ * recovery is attempted (via {@link DeserializationProblemHandler}, as
+ * problem is considered to be difficult to recover from, in general.
+ *
+ * @since 2.9
+ */
+ public void reportWrongTokenException(JavaType targetType,
+ JsonToken expToken, String msg, Object... msgArgs)
+ throws JsonMappingException
+ {
+ msg = _format(msg, msgArgs);
+ throw wrongTokenException(getParser(), targetType, expToken, msg);
+ }
+
+ /**
+ * Method for deserializers to call
+ * when the token encountered was of type different than what <b>should</b>
+ * be seen at that position, usually within a sequence of expected tokens.
+ * Note that this method will throw a {@link JsonMappingException} and no
+ * recovery is attempted (via {@link DeserializationProblemHandler}, as
+ * problem is considered to be difficult to recover from, in general.
+ *
+ * @since 2.9
+ */
+ public void reportWrongTokenException(Class<?> targetType,
+ JsonToken expToken, String msg, Object... msgArgs)
+ throws JsonMappingException
+ {
+ msg = _format(msg, msgArgs);
+ throw wrongTokenException(getParser(), targetType, expToken, msg);
+ }
+
+ /**
* @since 2.8
*/
+ public <T> T reportUnresolvedObjectId(ObjectIdReader oidReader, Object bean)
+ throws JsonMappingException
+ {
+ String msg = String.format("No Object Id found for an instance of %s, to assign to property '%s'",
+ ClassUtil.classNameOf(bean), oidReader.propertyName);
+ return reportInputMismatch(oidReader.idProperty, msg);
+ }
+
+ /**
+ * Helper method used to indicate a problem with input in cases where more
+ * specific <code>reportXxx()</code> method was not available.
+ *
+ * @since 2.9
+ */
+ public <T> T reportInputMismatch(BeanProperty prop,
+ String msg, Object... msgArgs) throws JsonMappingException
+ {
+ msg = _format(msg, msgArgs);
+ JavaType type = (prop == null) ? null : prop.getType();
+ throw MismatchedInputException.from(getParser(), type, msg);
+ }
+
+ /**
+ * Helper method used to indicate a problem with input in cases where more
+ * specific <code>reportXxx()</code> method was not available.
+ *
+ * @since 2.9
+ */
+ public <T> T reportInputMismatch(JsonDeserializer<?> src,
+ String msg, Object... msgArgs) throws JsonMappingException
+ {
+ msg = _format(msg, msgArgs);
+ throw MismatchedInputException.from(getParser(), src.handledType(), msg);
+ }
+
+ /**
+ * Helper method used to indicate a problem with input in cases where more
+ * specific <code>reportXxx()</code> method was not available.
+ *
+ * @since 2.9
+ */
+ public <T> T reportInputMismatch(Class<?> targetType,
+ String msg, Object... msgArgs) throws JsonMappingException
+ {
+ msg = _format(msg, msgArgs);
+ throw MismatchedInputException.from(getParser(), targetType, msg);
+ }
+
+ /**
+ * Helper method used to indicate a problem with input in cases where more
+ * specific <code>reportXxx()</code> method was not available.
+ *
+ * @since 2.9
+ */
+ public <T> T reportInputMismatch(JavaType targetType,
+ String msg, Object... msgArgs) throws JsonMappingException
+ {
+ msg = _format(msg, msgArgs);
+ throw MismatchedInputException.from(getParser(), targetType, msg);
+ }
+
+ @Deprecated // since 2.9
public void reportWrongTokenException(JsonParser p,
JsonToken expToken, String msg, Object... msgArgs)
throws JsonMappingException
{
- if ((msg != null) && (msgArgs.length > 0)) {
- msg = String.format(msg, msgArgs);
- }
+ msg = _format(msg, msgArgs);
throw wrongTokenException(p, expToken, msg);
}
@@ -1213,52 +1345,31 @@
JsonDeserializer<?> deser)
throws JsonMappingException
{
- if (!isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
- return;
+ if (isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
+ // Do we know properties that are expected instead?
+ Collection<Object> propIds = (deser == null) ? null : deser.getKnownPropertyNames();
+ throw UnrecognizedPropertyException.from(_parser,
+ instanceOrClass, fieldName, propIds);
}
- // Do we know properties that are expected instead?
- Collection<Object> propIds = (deser == null) ? null : deser.getKnownPropertyNames();
- throw UnrecognizedPropertyException.from(_parser,
- instanceOrClass, fieldName, propIds);
}
/**
* @since 2.8
+ *
+ * @deprecated Since 2.9: not clear this ever occurs
*/
- public void reportMappingException(String msg, Object... msgArgs)
- throws JsonMappingException
- {
- if (msgArgs.length > 0) {
- msg = String.format(msg, msgArgs);
- }
- throw JsonMappingException.from(getParser(), msg);
+ @Deprecated // since 2.9
+ public void reportMissingContent(String msg, Object... msgArgs) throws JsonMappingException {
+ throw MismatchedInputException.from(getParser(), (JavaType) null, "No content to map due to end-of-input");
}
- /**
- * @since 2.8
+ /*
+ /**********************************************************
+ /* Methods for problem reporting, in cases where recovery
+ /* is not considered possible: POJO definition problems
+ /**********************************************************
*/
- public void reportMissingContent(String msg, Object... msgArgs)
- throws JsonMappingException
- {
- if (msg == null) {
- msg = "No content to map due to end-of-input";
- } else if (msgArgs.length > 0) {
- msg = String.format(msg, msgArgs);
- }
- throw JsonMappingException.from(getParser(), msg);
- }
-
- /**
- * @since 2.8
- */
- public void reportUnresolvedObjectId(ObjectIdReader oidReader, Object bean)
- throws JsonMappingException
- {
- String msg = String.format("No Object Id found for an instance of %s, to assign to property '%s'",
- bean.getClass().getName(), oidReader.propertyName);
- throw JsonMappingException.from(getParser(), msg);
- }
-
+
/**
* Helper method called to indicate problem in POJO (serialization) definitions or settings
* regarding specific Java type, unrelated to actual JSON content to map.
@@ -1267,13 +1378,11 @@
* @since 2.9
*/
public <T> T reportBadTypeDefinition(BeanDescription bean,
- String message, Object... args) throws JsonMappingException {
- if (args != null && args.length > 0) {
- message = String.format(message, args);
- }
- String beanDesc = (bean == null) ? "N/A" : _desc(bean.getType().getGenericSignature());
- throw mappingException("Invalid type definition for type %s: %s",
- beanDesc, message);
+ String msg, Object... msgArgs) throws JsonMappingException {
+ msg = _format(msg, msgArgs);
+ String beanDesc = ClassUtil.nameOf(bean.getBeanClass());
+ msg = String.format("Invalid type definition for type %s: %s", beanDesc, msg);
+ throw InvalidDefinitionException.from(_parser, msg, bean, null);
}
/**
@@ -1284,70 +1393,39 @@
* @since 2.9
*/
public <T> T reportBadPropertyDefinition(BeanDescription bean, BeanPropertyDefinition prop,
- String message, Object... args) throws JsonMappingException {
- if (args != null && args.length > 0) {
- message = String.format(message, args);
+ String msg, Object... msgArgs) throws JsonMappingException {
+ msg = _format(msg, msgArgs);
+ String propName = ClassUtil.nameOf(prop);
+ String beanDesc = ClassUtil.nameOf(bean.getBeanClass());
+ msg = String.format("Invalid definition for property %s (of type %s): %s",
+ propName, beanDesc, msg);
+ throw InvalidDefinitionException.from(_parser, msg, bean, prop);
+ }
+
+ @Override
+ public <T> T reportBadDefinition(JavaType type, String msg) throws JsonMappingException {
+ throw InvalidDefinitionException.from(_parser, msg, type);
+ }
+
+ /**
+ * Method that deserializer may call if it is called to do an update ("merge")
+ * but deserializer operates on a non-mergeable type. Although this should
+ * usually be caught earlier, sometimes it may only be caught during operation
+ * and if so this is the method to call.
+ * Note that if {@link MapperFeature#IGNORE_MERGE_FOR_UNMERGEABLE} is enabled,
+ * this method will simply return null; otherwise {@link InvalidDefinitionException}
+ * will be thrown.
+ *
+ * @since 2.9
+ */
+ public <T> T reportBadMerge(JsonDeserializer<?> deser) throws JsonMappingException
+ {
+ if (isEnabled(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)) {
+ return null;
}
- String propName = (prop == null) ? "N/A" : _quotedString(prop.getName());
- String beanDesc = (bean == null) ? "N/A" : _desc(bean.getType().getGenericSignature());
- throw mappingException("Invalid definition for property %s (of type %s): %s",
- propName, beanDesc, message);
- }
-
- /*
- /**********************************************************
- /* Methods for constructing exceptions, "untyped"
- /**********************************************************
- */
-
- /**
- * Helper method for constructing generic mapping exception with specified
- * message and current location information.
- * Note that application code should almost always call
- * one of <code>handleXxx</code> methods, or {@link #reportMappingException(String, Object...)}
- * instead.
- *
- * @since 2.6
- */
- public JsonMappingException mappingException(String message) {
- return JsonMappingException.from(getParser(), message);
- }
-
- /**
- * Helper method for constructing generic mapping exception with specified
- * message and current location information
- * Note that application code should almost always call
- * one of <code>handleXxx</code> methods, or {@link #reportMappingException(String, Object...)}
- * instead.
- *
- * @since 2.6
- */
- public JsonMappingException mappingException(String msgTemplate, Object... args) {
- if (args != null && args.length > 0) {
- msgTemplate = String.format(msgTemplate, args);
- }
- return JsonMappingException.from(getParser(), msgTemplate);
- }
-
- /**
- * Helper method for constructing generic mapping exception for specified type
- *
- * @deprecated Since 2.8 use {@link #handleUnexpectedToken(Class, JsonParser)} instead
- */
- @Deprecated
- public JsonMappingException mappingException(Class<?> targetClass) {
- return mappingException(targetClass, _parser.getCurrentToken());
- }
-
- /**
- * @deprecated Since 2.8 use {@link #handleUnexpectedToken(Class, JsonParser)} instead
- */
- @Deprecated
- public JsonMappingException mappingException(Class<?> targetClass, JsonToken token) {
- String tokenDesc = (token == null) ? "<end of input>" : String.format("%s token", token);
- return JsonMappingException.from(_parser,
- String.format("Can not deserialize instance of %s out of %s",
- _calcName(targetClass), tokenDesc));
+ JavaType type = constructType(deser.handledType());
+ String msg = String.format("Invalid configuration: values of type %s can not be merged", type);
+ throw InvalidDefinitionException.from(getParser(), msg, type);
}
/*
@@ -1364,16 +1442,32 @@
* Note that most of the time this method should NOT be directly called;
* instead, {@link #reportWrongTokenException} should be called and will
* call this method as necessary.
+ *
+ * @since 2.9
*/
- public JsonMappingException wrongTokenException(JsonParser p, JsonToken expToken,
- String msg0)
+ public JsonMappingException wrongTokenException(JsonParser p, JavaType targetType,
+ JsonToken expToken, String extra)
{
String msg = String.format("Unexpected token (%s), expected %s",
p.getCurrentToken(), expToken);
- if (msg0 != null) {
- msg = msg + ": "+msg0;
- }
- return JsonMappingException.from(p, msg);
+ msg = _colonConcat(msg, extra);
+ return MismatchedInputException.from(p, targetType, msg);
+ }
+
+ public JsonMappingException wrongTokenException(JsonParser p, Class<?> targetType,
+ JsonToken expToken, String extra)
+ {
+ String msg = String.format("Unexpected token (%s), expected %s",
+ p.getCurrentToken(), expToken);
+ msg = _colonConcat(msg, extra);
+ return MismatchedInputException.from(p, targetType, msg);
+ }
+
+ @Deprecated // since 2.9
+ public JsonMappingException wrongTokenException(JsonParser p, JsonToken expToken,
+ String msg)
+ {
+ return wrongTokenException(p, (JavaType) null, expToken, msg);
}
/**
@@ -1388,7 +1482,7 @@
String msg) {
return InvalidFormatException.from(_parser,
String.format("Can not deserialize Map key of type %s from String %s: %s",
- keyClass.getName(), _quotedString(keyValue), msg),
+ ClassUtil.nameOf(keyClass), _quotedString(keyValue), msg),
keyValue, keyClass);
}
@@ -1409,7 +1503,7 @@
String msg) {
return InvalidFormatException.from(_parser,
String.format("Can not deserialize value of type %s from String %s: %s",
- instClass.getName(), _quotedString(value), msg),
+ ClassUtil.nameOf(instClass), _quotedString(value), msg),
value, instClass);
}
@@ -1424,7 +1518,7 @@
String msg) {
return InvalidFormatException.from(_parser,
String.format("Can not deserialize value of type %s from number %s: %s",
- instClass.getName(), String.valueOf(value), msg),
+ ClassUtil.nameOf(instClass), String.valueOf(value), msg),
value, instClass);
}
@@ -1437,10 +1531,14 @@
* {@link #handleInstantiationProblem} should be called which will call this method
* if necessary.
*/
- public JsonMappingException instantiationException(Class<?> instClass, Throwable t) {
- return JsonMappingException.from(_parser,
- String.format("Can not construct instance of %s, problem: %s",
- instClass.getName(), t.getMessage()), t);
+ public JsonMappingException instantiationException(Class<?> instClass, Throwable cause) {
+ // Most likely problem with Creator definition, right?
+ JavaType type = constructType(instClass);
+ String msg = String.format("Can not construct instance of %s, problem: %s",
+ ClassUtil.nameOf(instClass), cause.getMessage());
+ InvalidDefinitionException e = InvalidDefinitionException.from(_parser, msg, type);
+ e.initCause(cause);
+ return e;
}
/**
@@ -1452,29 +1550,30 @@
* {@link #handleMissingInstantiator} should be called which will call this method
* if necessary.
*/
- public JsonMappingException instantiationException(Class<?> instClass, String msg) {
- return JsonMappingException.from(_parser,
- String.format("Can not construct instance of %s: %s",
- instClass.getName(), msg));
+ public JsonMappingException instantiationException(Class<?> instClass, String msg0) {
+ // Most likely problem with Creator definition, right?
+ JavaType type = constructType(instClass);
+ String msg = String.format("Can not construct instance of %s: %s",
+ ClassUtil.nameOf(instClass), msg0);
+ return InvalidDefinitionException.from(_parser, msg, type);
+ }
+
+ @Override
+ public JsonMappingException invalidTypeIdException(JavaType baseType, String typeId,
+ String extraDesc) {
+ String msg = String.format("Could not resolve type id '%s' as a subtype of %s",
+ typeId, baseType);
+ return InvalidTypeIdException.from(_parser, _colonConcat(msg, extraDesc), baseType, typeId);
}
/**
- * Helper method for constructing exception to indicate that given type id
- * could not be resolved to a valid subtype of specified base type, during
- * polymorphic deserialization.
- *<p>
- * Note that most of the time this method should NOT be called; instead,
- * {@link #handleUnknownTypeId} should be called which will call this method
- * if necessary.
+ * @since 2.9
*/
- public JsonMappingException unknownTypeIdException(JavaType baseType, String typeId,
+ public JsonMappingException missingTypeIdException(JavaType baseType,
String extraDesc) {
- String msg = String.format("Could not resolve type id '%s' into a subtype of %s",
- typeId, baseType);
- if (extraDesc != null) {
- msg = msg + ": "+extraDesc;
- }
- return InvalidTypeIdException.from(_parser, msg, baseType, typeId);
+ String msg = String.format("Missing type id when trying to resolve subtype of %s",
+ baseType);
+ return InvalidTypeIdException.from(_parser, _colonConcat(msg, extraDesc), baseType, null);
}
/*
@@ -1490,13 +1589,12 @@
*/
@Deprecated
public JsonMappingException unknownTypeException(JavaType type, String id,
- String extraDesc) {
+ String extraDesc)
+ {
String msg = String.format("Could not resolve type id '%s' into a subtype of %s",
id, type);
- if (extraDesc != null) {
- msg = msg + ": "+extraDesc;
- }
- return JsonMappingException.from(_parser, msg);
+ msg = _colonConcat(msg, extraDesc);
+ return MismatchedInputException.from(_parser, type, msg);
}
/**
@@ -1507,8 +1605,84 @@
*/
@Deprecated
public JsonMappingException endOfInputException(Class<?> instClass) {
- return JsonMappingException.from(_parser, "Unexpected end-of-input when trying to deserialize a "
- +instClass.getName());
+ return MismatchedInputException.from(_parser, instClass,
+ "Unexpected end-of-input when trying to deserialize a "+instClass.getName());
+ }
+
+ /*
+ /**********************************************************
+ /* Deprecated methods for constructing, throwing non-specific
+ /* JsonMappingExceptions: as of 2.9, should use more specific
+ /* ones.
+ /**********************************************************
+ */
+
+ /**
+ * Fallback method that may be called if no other <code>reportXxx</code>
+ * is applicable -- but only in that case.
+ *
+ * @since 2.8
+ *
+ * @deprecated Since 2.9: use a more specific method, or {@link #reportBadDefinition(JavaType, String)},
+ * or {@link #reportInputMismatch} instead
+ */
+ @Deprecated // since 2.9
+ public void reportMappingException(String msg, Object... msgArgs)
+ throws JsonMappingException
+ {
+ throw JsonMappingException.from(getParser(), _format(msg, msgArgs));
+ }
+
+ /**
+ * Helper method for constructing generic mapping exception with specified
+ * message and current location information.
+ * Note that application code should almost always call
+ * one of <code>handleXxx</code> methods, or {@link #reportMappingException(String, Object...)}
+ * instead.
+ *
+ * @since 2.6
+ *
+ * @deprecated Since 2.9 use more specific error reporting methods instead
+ */
+ @Deprecated
+ public JsonMappingException mappingException(String message) {
+ return JsonMappingException.from(getParser(), message);
+ }
+
+ /**
+ * Helper method for constructing generic mapping exception with specified
+ * message and current location information
+ * Note that application code should almost always call
+ * one of <code>handleXxx</code> methods, or {@link #reportMappingException(String, Object...)}
+ * instead.
+ *
+ * @since 2.6
+ *
+ * @deprecated Since 2.9 use more specific error reporting methods instead
+ */
+ @Deprecated
+ public JsonMappingException mappingException(String msg, Object... msgArgs) {
+ return JsonMappingException.from(getParser(), _format(msg, msgArgs));
+ }
+
+ /**
+ * Helper method for constructing generic mapping exception for specified type
+ *
+ * @deprecated Since 2.8 use {@link #handleUnexpectedToken(Class, JsonParser)} instead
+ */
+ @Deprecated
+ public JsonMappingException mappingException(Class<?> targetClass) {
+ return mappingException(targetClass, _parser.getCurrentToken());
+ }
+
+ /**
+ * @deprecated Since 2.8 use {@link #handleUnexpectedToken(Class, JsonParser)} instead
+ */
+ @Deprecated
+ public JsonMappingException mappingException(Class<?> targetClass, JsonToken token) {
+ return JsonMappingException.from(_parser,
+ String.format("Can not deserialize instance of %s out of %s token",
+ _calcName(targetClass), token));
}
/*
@@ -1531,48 +1705,4 @@
_dateFormat = df = (DateFormat) df.clone();
return df;
}
-
- protected String determineClassName(Object instance) {
- return ClassUtil.getClassDescription(instance);
- }
-
- protected String _calcName(Class<?> cls) {
- if (cls.isArray()) {
- return _calcName(cls.getComponentType())+"[]";
- }
- return cls.getName();
- }
-
- protected String _valueDesc() {
- try {
- return _desc(_parser.getText());
- } catch (Exception e) {
- return "[N/A]";
- }
- }
-
- protected String _desc(String desc) {
- if (desc == null) {
- return "[N/A]";
- }
- // !!! should we quote it? (in case there are control chars, linefeeds)
- if (desc.length() > MAX_ERROR_STR_LEN) {
- desc = desc.substring(0, MAX_ERROR_STR_LEN) + "]...[" + desc.substring(desc.length() - MAX_ERROR_STR_LEN);
- }
- return desc;
- }
-
- // @since 2.7
- protected String _quotedString(String desc) {
- if (desc == null) {
- return "[N/A]";
- }
- // !!! should we quote it? (in case there are control chars, linefeeds)
- if (desc.length() > MAX_ERROR_STR_LEN) {
- return String.format("\"%s]...[%s\"",
- desc.substring(0, MAX_ERROR_STR_LEN),
- desc.substring(desc.length() - MAX_ERROR_STR_LEN));
- }
- return "\"" + desc + "\"";
- }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java b/src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java
index 1505419..d41ffd6 100644
--- a/src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java
+++ b/src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java
@@ -92,20 +92,7 @@
* {@link java.util.List}s.
*/
USE_JAVA_ARRAY_FOR_JSON_ARRAY(false),
-
- /**
- * Feature that determines standard deserialization mechanism used for
- * Enum values: if enabled, Enums are assumed to have been serialized using
- * return value of <code>Enum.toString()</code>;
- * if disabled, return value of <code>Enum.name()</code> is assumed to have been used.
- *<p>
- * Note: this feature should usually have same value
- * as {@link SerializationFeature#WRITE_ENUMS_USING_TO_STRING}.
- *<p>
- * Feature is disabled by default.
- */
- READ_ENUMS_USING_TO_STRING(false),
-
+
/*
/******************************************************
* Error handling features
@@ -325,21 +312,27 @@
/**
* Feature that can be enabled to allow JSON empty String
- * value ("") to be bound to POJOs as null.
- * If disabled, standard POJOs can only be bound from JSON null or
+ * value ("") to be bound as `null` for POJOs and other structured
+ * values ({@link java.util.Map}s, {@link java.util.Collection}s).
+ * If disabled, standard POJOs can only be bound from JSON `null` or
* JSON Object (standard meaning that no custom deserializers or
* constructors are defined; both of which can add support for other
* kinds of JSON values); if enabled, empty JSON String can be taken
* to be equivalent of JSON null.
*<p>
+ * NOTE: this does NOT apply to scalar values such as booleans and numbers;
+ * whether they can be coerced depends on
+ * {@link MapperFeature#ALLOW_COERCION_OF_SCALARS}.
+ *<p>
* Feature is disabled by default.
*/
ACCEPT_EMPTY_STRING_AS_NULL_OBJECT(false),
/**
* Feature that can be enabled to allow empty JSON Array
- * value (that is, <code>[ ]</code>) to be bound to POJOs as null.
- * If disabled, standard POJOs can only be bound from JSON null or
+ * value (that is, <code>[ ]</code>) to be bound to POJOs (and
+ * with 2.9, other values too) as `null`.
+ * If disabled, standard POJOs can only be bound from JSON `null` or
* JSON Object (standard meaning that no custom deserializers or
* constructors are defined; both of which can add support for other
* kinds of JSON values); if enabled, empty JSON Array will be taken
@@ -364,7 +357,20 @@
* @since 2.6
*/
ACCEPT_FLOAT_AS_INT(true),
-
+
+ /**
+ * Feature that determines standard deserialization mechanism used for
+ * Enum values: if enabled, Enums are assumed to have been serialized using
+ * return value of <code>Enum.toString()</code>;
+ * if disabled, return value of <code>Enum.name()</code> is assumed to have been used.
+ *<p>
+ * Note: this feature should usually have same value
+ * as {@link SerializationFeature#WRITE_ENUMS_USING_TO_STRING}.
+ *<p>
+ * Feature is disabled by default.
+ */
+ READ_ENUMS_USING_TO_STRING(false),
+
/**
* Feature that allows unknown Enum values to be parsed as null values.
* If disabled, unknown Enum values will throw exceptions.
@@ -420,9 +426,14 @@
* Note that exact behavior depends on date/time types in question; and specifically
* JDK type of {@link java.util.Date} does NOT have in-built timezone information
* so this setting has no effect.
+ * Further, while {@link java.util.Calendar} does have this information basic
+ * JDK {@link java.text.SimpleDateFormat} is unable to retain parsed zone information,
+ * and as a result, {@link java.util.Calendar} will always get context timezone
+ * adjustment regardless of this setting.
*<p>
- * As of Jackson 2.8, this feature is supported only by extension modules for Joda
- * and Java 8 date/tyime datatypes.
+ *<p>
+ * Taking above into account, this feature is supported only by extension modules for
+ * Joda and Java 8 date/tyime datatypes.
*
* @since 2.2
*/
diff --git a/src/main/java/com/fasterxml/jackson/databind/InjectableValues.java b/src/main/java/com/fasterxml/jackson/databind/InjectableValues.java
index a4da378..9f773c3 100644
--- a/src/main/java/com/fasterxml/jackson/databind/InjectableValues.java
+++ b/src/main/java/com/fasterxml/jackson/databind/InjectableValues.java
@@ -2,6 +2,8 @@
import java.util.*;
+import com.fasterxml.jackson.databind.util.ClassUtil;
+
/**
* Abstract class that defines API for objects that provide value to
* "inject" during deserialization. An instance of this object
@@ -23,7 +25,7 @@
* if available; null if bean has not yet been constructed.
*/
public abstract Object findInjectableValue(Object valueId, DeserializationContext ctxt,
- BeanProperty forProperty, Object beanInstance);
+ BeanProperty forProperty, Object beanInstance) throws JsonMappingException;
/*
/**********************************************************
@@ -63,11 +65,13 @@
@Override
public Object findInjectableValue(Object valueId, DeserializationContext ctxt,
- BeanProperty forProperty, Object beanInstance)
+ BeanProperty forProperty, Object beanInstance) throws JsonMappingException
{
if (!(valueId instanceof String)) {
- String type = (valueId == null) ? "[null]" : valueId.getClass().getName();
- throw new IllegalArgumentException("Unrecognized inject value id type ("+type+"), expecting String");
+ ctxt.reportBadDefinition(ClassUtil.classOf(valueId),
+ String.format(
+ "Unrecognized inject value id type (%s), expecting String",
+ ClassUtil.classNameOf(valueId)));
}
String key = (String) valueId;
Object ob = _values.get(key);
diff --git a/src/main/java/com/fasterxml/jackson/databind/JavaType.java b/src/main/java/com/fasterxml/jackson/databind/JavaType.java
index 292cc39..3fdbb2b 100644
--- a/src/main/java/com/fasterxml/jackson/databind/JavaType.java
+++ b/src/main/java/com/fasterxml/jackson/databind/JavaType.java
@@ -211,15 +211,7 @@
if (subclass == _class) { // can still optimize for simple case
return this;
}
- JavaType result = _narrow(subclass);
- // TODO: these checks should NOT actually be needed; above should suffice:
- if (_valueHandler != result.<Object>getValueHandler()) {
- result = result.withValueHandler(_valueHandler);
- }
- if (_typeHandler != result.<Object>getTypeHandler()) {
- result = result.withTypeHandler(_typeHandler);
- }
- return result;
+ return _narrow(subclass);
}
@Deprecated // since 2.7
@@ -257,7 +249,14 @@
* @since 2.6
*/
public final boolean isTypeOrSubTypeOf(Class<?> clz) {
- return (_class == clz) || (clz.isAssignableFrom(_class));
+ return (_class == clz) || clz.isAssignableFrom(_class);
+ }
+
+ /**
+ * @since 2.9
+ */
+ public final boolean isTypeOrSuperTypeOf(Class<?> clz) {
+ return (_class == clz) || _class.isAssignableFrom(clz);
}
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/JsonDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/JsonDeserializer.java
index 1e559a7..d97df33 100644
--- a/src/main/java/com/fasterxml/jackson/databind/JsonDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/JsonDeserializer.java
@@ -4,10 +4,11 @@
import java.util.Collection;
import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.deser.BeanDeserializerFactory;
-import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
+
+import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.impl.ObjectIdReader;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
+import com.fasterxml.jackson.databind.util.AccessPattern;
import com.fasterxml.jackson.databind.util.NameTransformer;
/**
@@ -29,7 +30,7 @@
*<p>
* In addition, to support per-property annotations (to configure aspects
* of deserialization on per-property basis), deserializers may want
- * to implement
+ * to implement
* {@link com.fasterxml.jackson.databind.deser.ContextualDeserializer},
* which allows specialization of deserializers: call to
* {@link com.fasterxml.jackson.databind.deser.ContextualDeserializer#createContextual}
@@ -43,6 +44,7 @@
* contextualization.
*/
public abstract class JsonDeserializer<T>
+ implements NullValueProvider // since 2.9
{
/*
/**********************************************************
@@ -80,8 +82,8 @@
* after the @class. Thus, if you want your method to work correctly
* both with and without polymorphism, you must begin your method with:
* <pre>
- * if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
- * jp.nextToken();
+ * if (p.getCurrentToken() == JsonToken.START_OBJECT) {
+ * p.nextToken();
* }
* </pre>
* This results in the stream pointing to the field name, so that
@@ -121,8 +123,11 @@
* update-existing-value operation (esp. immutable types)
*/
public T deserialize(JsonParser p, DeserializationContext ctxt, T intoValue)
- throws IOException, JsonProcessingException
+ throws IOException
{
+ if (ctxt.isEnabled(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)) {
+ return deserialize(p, ctxt);
+ }
throw new UnsupportedOperationException("Can not update object of type "
+intoValue.getClass().getName()+" (by deserializer of type "+getClass().getName()+")");
}
@@ -251,10 +256,10 @@
/*
/**********************************************************
- /* Other accessors
+ /* Default NullValueProvider implementation
/**********************************************************
*/
-
+
/**
* Method that can be called to determine value to be used for
* representing null values (values deserialized when JSON token
@@ -270,12 +275,44 @@
*
* @since 2.6 Added to replace earlier no-arguments variant
*/
+ @Override
public T getNullValue(DeserializationContext ctxt) throws JsonMappingException {
// Change the direction in 2.7
return getNullValue();
}
/**
+ * Default implementation indicates that "null value" to use for input null
+ * is simply Java `null` for all deserializers, unless overridden by sub-classes.
+ * This information may be used as optimization.
+ */
+ @Override
+ public AccessPattern getNullAccessPattern() {
+ // Default implementation assumes that the null value does not vary, which
+ // is usually the case for most implementations. But it is not necessarily
+ // `null`; so sub-classes may want to refine further.
+ return AccessPattern.CONSTANT;
+ }
+
+ /**
+ * This method may be called in conjunction with calls to
+ * {@link #getEmptyValue(DeserializationContext)}, to check whether it needs
+ * to be called just once (static values), or each time empty value is
+ * needed.
+ *
+ * @since 2.9
+ */
+ public AccessPattern getEmptyAccessPattern() {
+ return AccessPattern.DYNAMIC;
+ }
+
+ /*
+ /**********************************************************
+ /* Other accessors
+ /**********************************************************
+ */
+
+ /**
* Method called to determine value to be used for "empty" values
* (most commonly when deserializing from empty JSON Strings).
* Usually this is same as {@link #getNullValue} (which in turn
@@ -286,16 +323,17 @@
* Since version 2.6 (in which the context argument was added), call is
* expected to be made each and every time an empty value is needed.
*<p>
- * Default implementation simple calls {@link #getNullValue} and
+ * Since version 2.9 does not require return of `T` any more.
+ *<p>
+ * Default implementation simply calls {@link #getNullValue} and
* returns value.
*
* @since 2.6 Added to replace earlier no-arguments variant
*/
- public T getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
- // Change the direction in 2.7
- return getEmptyValue();
+ public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
+ return getNullValue(ctxt);
}
-
+
/**
* Accessor that can be used to check whether this deserializer
* is expecting to possibly get an Object Identifier value instead of full value
@@ -328,6 +366,29 @@
+"': type: value deserializer of type "+getClass().getName()+" does not support them");
}
+ /**
+ * Introspection method that may be called to see whether deserializer supports
+ * update of an existing value (aka "merging") or not. Return value should either
+ * be {@link Boolean#FALSE} if update is not supported at all (immutable values);
+ * {@link Boolean#TRUE} if update should usually work (regular POJOs, for example),
+ * or <code>null</code> if this is either not known, or may sometimes work.
+ *<p>
+ * Information gathered is typically used to either prevent merging update for
+ * property (either by skipping, if based on global defaults; or by exception during
+ * deserialization construction if explicit attempt made) if {@link Boolean#FALSE}
+ * returned, or inclusion if {@link Boolean#TRUE} is specified. If "unknown" case
+ * (<code>null</code> returned) behavior is to exclude property if global defaults
+ * used; or to allow if explicit per-type or property merging is defined.
+ *<p>
+ * Default implementation returns <code>null</code> to allow explicit per-type
+ * or per-property attempts.
+ *
+ * @since 2.9
+ */
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return null;
+ }
+
/*
/**********************************************************
/* Deprecated methods
@@ -344,8 +405,8 @@
* @deprecated Since 2.6 Use overloaded variant that takes context argument
*/
@Deprecated
- public T getEmptyValue() { return getNullValue(); }
-
+ public Object getEmptyValue() { return getNullValue(); }
+
/*
/**********************************************************
/* Helper classes
diff --git a/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java b/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java
index 00896ea..39da02f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java
+++ b/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java
@@ -326,6 +326,9 @@
* Factory method used when "upgrading" an {@link IOException} into
* {@link JsonMappingException}: usually only needed to comply with
* a signature.
+ *<p>
+ * NOTE: since 2.9 should usually NOT be used on input-side (deserialization)
+ * exceptions; instead use method(s) of <code>InputMismatchException</code>
*
* @since 2.1
*/
diff --git a/src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java b/src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java
index 3506cd2..ae455ff 100644
--- a/src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java
@@ -120,7 +120,7 @@
* serializing Objects value contains, if any.
*/
public abstract void serialize(T value, JsonGenerator gen, SerializerProvider serializers)
- throws IOException, JsonProcessingException;
+ throws IOException;
/**
* Method that can be called to ask implementation to serialize
@@ -157,8 +157,9 @@
if (clz == null) {
clz = value.getClass();
}
- serializers.reportMappingProblem("Type id handling not implemented for type %s (by serializer of type %s)",
- clz.getName(), getClass().getName());
+ serializers.reportBadDefinition(clz, String.format(
+ "Type id handling not implemented for type %s (by serializer of type %s)",
+ clz.getName(), getClass().getName()));
}
/*
@@ -188,7 +189,7 @@
* Default implementation will consider only null values to be empty.
*
* @deprecated Since 2.5 Use {@link #isEmpty(SerializerProvider, Object)} instead;
- * will be removed from 2.9
+ * will be removed from 3.0
*/
@Deprecated
public boolean isEmpty(T value) {
@@ -272,7 +273,7 @@
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType type)
throws JsonMappingException
{
- if (visitor != null) visitor.expectAnyFormat(type);
+ visitor.expectAnyFormat(type);
}
/*
diff --git a/src/main/java/com/fasterxml/jackson/databind/KeyDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/KeyDeserializer.java
index 90dd56d..217527d 100644
--- a/src/main/java/com/fasterxml/jackson/databind/KeyDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/KeyDeserializer.java
@@ -2,8 +2,6 @@
import java.io.IOException;
-import com.fasterxml.jackson.core.*;
-
/**
* Abstract class that defines API used for deserializing JSON content
* field names into Java Map keys. These deserializers are only used
@@ -15,7 +13,7 @@
* Method called to deserialize a {@link java.util.Map} key from JSON property name.
*/
public abstract Object deserializeKey(String key, DeserializationContext ctxt)
- throws IOException, JsonProcessingException;
+ throws IOException;
/**
* This marker class is only to be used with annotations, to
diff --git a/src/main/java/com/fasterxml/jackson/databind/MapperFeature.java b/src/main/java/com/fasterxml/jackson/databind/MapperFeature.java
index e163b5f..183c073 100644
--- a/src/main/java/com/fasterxml/jackson/databind/MapperFeature.java
+++ b/src/main/java/com/fasterxml/jackson/databind/MapperFeature.java
@@ -18,7 +18,7 @@
{
/*
/******************************************************
- /* Introspection features
+ /* General introspection features
/******************************************************
*/
@@ -33,6 +33,43 @@
USE_ANNOTATIONS(true),
/**
+ * Feature that determines whether otherwise regular "getter"
+ * methods (but only ones that handle Collections and Maps,
+ * not getters of other type)
+ * can be used for purpose of getting a reference to a Collection
+ * and Map to modify the property, without requiring a setter
+ * method.
+ * This is similar to how JAXB framework sets Collections and
+ * Maps: no setter is involved, just setter.
+ *<p>
+ * Note that such getters-as-setters methods have lower
+ * precedence than setters, so they are only used if no
+ * setter is found for the Map/Collection property.
+ *<p>
+ * Feature is enabled by default.
+ */
+ USE_GETTERS_AS_SETTERS(true),
+
+ /**
+ * Feature that determines how <code>transient</code> modifier for fields
+ * is handled: if disabled, it is only taken to mean exclusion of the field
+ * as accessor; if true, it is taken to imply removal of the whole property.
+ *<p>
+ * Feature is disabled by default, meaning that existence of `transient`
+ * for a field does not necessarily lead to ignoral of getters or setters
+ * but just ignoring the use of field for access.
+ *
+ * @since 2.6
+ */
+ PROPAGATE_TRANSIENT_MARKER(false),
+
+ /*
+ /******************************************************
+ /* Introspection-based property auto-detection
+ /******************************************************
+ */
+
+ /**
* Feature that determines whether "creator" methods are
* automatically detected by consider public constructors,
* and static single argument methods with name "valueOf".
@@ -61,7 +98,7 @@
*<p>
* Feature is enabled by default.
*/
- AUTO_DETECT_FIELDS(true),
+ AUTO_DETECT_FIELDS(true),
/**
* Feature that determines whether regular "getter" methods are
@@ -98,22 +135,22 @@
*/
AUTO_DETECT_IS_GETTERS(true),
- /**
- * Feature that determines whether "setter" methods are
- * automatically detected based on standard Bean naming convention
- * or not. If yes, then all public one-argument methods that
- * start with prefix "set"
- * are considered setters. If disabled, only methods explicitly
- * annotated are considered setters.
- *<p>
- * Note that this feature has lower precedence than per-class
- * annotations, and is only used if there isn't more granular
- * configuration available.
- *<P>
- * Feature is enabled by default.
- */
- AUTO_DETECT_SETTERS(true),
-
+ /**
+ * Feature that determines whether "setter" methods are
+ * automatically detected based on standard Bean naming convention
+ * or not. If yes, then all public one-argument methods that
+ * start with prefix "set"
+ * are considered setters. If disabled, only methods explicitly
+ * annotated are considered setters.
+ *<p>
+ * Note that this feature has lower precedence than per-class
+ * annotations, and is only used if there isn't more granular
+ * configuration available.
+ *<P>
+ * Feature is enabled by default.
+ */
+ AUTO_DETECT_SETTERS(true),
+
/**
* Feature that determines whether getters (getter methods)
* can be auto-detected if there is no matching mutator (setter,
@@ -126,22 +163,61 @@
REQUIRE_SETTERS_FOR_GETTERS(false),
/**
- * Feature that determines whether otherwise regular "getter"
- * methods (but only ones that handle Collections and Maps,
- * not getters of other type)
- * can be used for purpose of getting a reference to a Collection
- * and Map to modify the property, without requiring a setter
- * method.
- * This is similar to how JAXB framework sets Collections and
- * Maps: no setter is involved, just setter.
+ * Feature that determines whether member fields declared as 'final' may
+ * be auto-detected to be used mutators (used to change value of the logical
+ * property) or not. If enabled, 'final' access modifier has no effect, and
+ * such fields may be detected according to usual visibility and inference
+ * rules; if disabled, such fields are NOT used as mutators except if
+ * explicitly annotated for such use.
*<p>
- * Note that such getters-as-setters methods have lower
- * precedence than setters, so they are only used if no
- * setter is found for the Map/Collection property.
+ * Feature is enabled by default, for backwards compatibility reasons.
+ *
+ * @since 2.2
+ */
+ ALLOW_FINAL_FIELDS_AS_MUTATORS(true),
+
+ /**
+ * Feature that determines whether member mutators (fields and
+ * setters) may be "pulled in" even if they are not visible,
+ * as long as there is a visible accessor (getter or field) with same name.
+ * For example: field "value" may be inferred as mutator,
+ * if there is visible or explicitly marked getter "getValue()".
+ * If enabled, inferring is enabled; otherwise (disabled) only visible and
+ * explicitly annotated accessors are ever used.
+ *<p>
+ * Note that 'getters' are never inferred and need to be either visible (including
+ * bean-style naming) or explicitly annotated.
*<p>
* Feature is enabled by default.
+ *
+ * @since 2.2
*/
- USE_GETTERS_AS_SETTERS(true),
+ INFER_PROPERTY_MUTATORS(true),
+
+ /**
+ * Feature that determines handling of <code>java.beans.ConstructorProperties<code>
+ * annotation: when enabled, it is considered as alias of
+ * {@link com.fasterxml.jackson.annotation.JsonCreator}, to mean that constructor
+ * should be considered a property-based Creator; when disabled, only constructor
+ * parameter name information is used, but constructor is NOT considered an explicit
+ * Creator (although may be discovered as one using other annotations or heuristics).
+ *<p>
+ * Feature is mostly used to help interoperability with frameworks like Lombok
+ * that may automatically generate <code>ConstructorProperties</code> annotation
+ * but without necessarily meaning that constructor should be used as Creator
+ * for deserialization.
+ *<p>
+ * Feature is enabled by default.
+ *
+ * @since 2.9
+ */
+ INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES(true),
+
+ /*
+ /******************************************************
+ /* Access modifier handling
+ /******************************************************
+ */
/**
* Feature that determines whether method and field access
@@ -187,51 +263,6 @@
*/
OVERRIDE_PUBLIC_ACCESS_MODIFIERS(true),
- /**
- * Feature that determines whether member mutators (fields and
- * setters) may be "pulled in" even if they are not visible,
- * as long as there is a visible accessor (getter or field) with same name.
- * For example: field "value" may be inferred as mutator,
- * if there is visible or explicitly marked getter "getValue()".
- * If enabled, inferring is enabled; otherwise (disabled) only visible and
- * explicitly annotated accessors are ever used.
- *<p>
- * Note that 'getters' are never inferred and need to be either visible (including
- * bean-style naming) or explicitly annotated.
- *<p>
- * Feature is enabled by default.
- *
- * @since 2.2
- */
- INFER_PROPERTY_MUTATORS(true),
-
- /**
- * Feature that determines whether member fields declared as 'final' may
- * be auto-detected to be used mutators (used to change value of the logical
- * property) or not. If enabled, 'final' access modifier has no effect, and
- * such fields may be detected according to usual visibility and inference
- * rules; if disabled, such fields are NOT used as mutators except if
- * explicitly annotated for such use.
- *<p>
- * Feature is enabled by default, for backwards compatibility reasons.
- *
- * @since 2.2
- */
- ALLOW_FINAL_FIELDS_AS_MUTATORS(true),
-
- /**
- * Feature that determines how <code>transient</code> modifier for fields
- * is handled: if disabled, it is only taken to mean exclusion of the field
- * as accessor; if true, removal of the whole property.
- *<p>
- * Feature is disabled by default, meaning that existence of `transient`
- * for a field does not necessarily lead to ignoral of getters or setters
- * but just ignoring the use of field for access.
- *
- * @since 2.6
- */
- PROPAGATE_TRANSIENT_MARKER(false),
-
/*
/******************************************************
/* Type-handling features
@@ -307,6 +338,7 @@
/* Name-related features
/******************************************************
*/
+
/**
* Feature that will allow for more forgiving deserialization of incoming JSON.
* If enabled, the bean properties will be matched using their lower-case equivalents,
@@ -322,7 +354,20 @@
* @since 2.5
*/
ACCEPT_CASE_INSENSITIVE_PROPERTIES(false),
-
+
+
+ /**
+ * Feature that determines if Enum deserialization should be case sensitive or not.
+ * If enabled, Enum deserialization will ignore case, that is, case of incoming String
+ * value and enum id (dependant on other settings, either `name()`, `toString()`, or
+ * explicit override) do not need to match.
+ * <p>
+ * Feature is disabled by default.
+ *
+ * @since 2.9
+ */
+ ACCEPT_CASE_INSENSITIVE_ENUMS(false),
+
/**
* Feature that can be enabled to make property names be
* overridden by wrapper name (usually detected with annotations
@@ -366,6 +411,34 @@
/*
/******************************************************
+ /* Coercion features
+ /******************************************************
+ */
+
+ /**
+ * Feature that determines whether coercions from secondary representations are allowed
+ * for simple non-textual scalar types: numbers and booleans. This includes `primitive`
+ * types and their wrappers, but excludes `java.lang.String` and date/time types.
+ *<p>
+ * When feature is disabled, only strictly compatible input may be bound: numbers for
+ * numbers, boolean values for booleans. When feature is enabled, conversions from
+ * JSON String are allowed, as long as textual value matches (for example, String
+ * "true" is allowed as equivalent of JSON boolean token `true`; or String "1.0"
+ * for `double`).
+ *<p>
+ * Note that it is possible that other configurability options can override this
+ * in closer scope (like on per-type or per-property basis); this is just the global
+ * default.
+ *<p>
+ * Feature is enabled by default (for backwards compatibility since this was the
+ * default behavior)
+ *
+ * @since 2.9
+ */
+ ALLOW_COERCION_OF_SCALARS(true),
+
+ /*
+ /******************************************************
/* Other features
/******************************************************
*/
@@ -387,8 +460,22 @@
*
* @since 2.5
*/
- IGNORE_DUPLICATE_MODULE_REGISTRATIONS(true)
-
+ IGNORE_DUPLICATE_MODULE_REGISTRATIONS(true),
+
+ /**
+ * Setting that determines what happens if an attempt is made to explicitly
+ * "merge" value of a property, where value does not support merging; either
+ * merging is skipped and new value is created (<code>true</code>) or
+ * an exception is thrown (false).
+ *<p>
+ * Feature is disabled by default since non-mergeable property types are ignored
+ * even if defaults call for merging, and usually explicit per-type or per-property
+ * settings for such types should result in an exception.
+ *
+ * @since 2.9
+ */
+ IGNORE_MERGE_FOR_UNMERGEABLE(true)
+
;
private final boolean _defaultState;
diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
index cb64cee..d635fc3 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
@@ -17,13 +17,9 @@
import com.fasterxml.jackson.core.type.ResolvedType;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.util.*;
-import com.fasterxml.jackson.databind.cfg.BaseSettings;
-import com.fasterxml.jackson.databind.cfg.ContextAttributes;
-import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
-import com.fasterxml.jackson.databind.cfg.MapperConfig;
-import com.fasterxml.jackson.databind.cfg.MutableConfigOverride;
-import com.fasterxml.jackson.databind.cfg.ConfigOverrides;
+import com.fasterxml.jackson.databind.cfg.*;
import com.fasterxml.jackson.databind.deser.*;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.introspect.*;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsontype.*;
@@ -80,7 +76,7 @@
* on Streaming API).
*<p>
* Mapper instances are fully thread-safe provided that ALL configuration of the
- * instance occurs before ANY read or write calls. If configuration of a mapper
+ * instance occurs before ANY read or write calls. If configuration of a mapper instance
* is modified after first usage, changes may or may not take effect, and configuration
* calls themselves may fail.
* If you need to use different configuration, you have two main possibilities:
@@ -112,13 +108,19 @@
* produce differing deserializers), and that the performance impact
* greatest at root level (since it'll essentially cache the full
* graph of deserializers involved).
+ *<p>
+ * Notes on security: use "default typing" feature (see {@link #enableDefaultTyping()})
+ * is a potential security risk, if used with untrusted content (content generated by
+ * untrusted external parties). If so, you may want to construct a custom
+ * {@link TypeResolverBuilder} implementation to limit possible types to instantiate,
+ * (using {@link #setDefaultTyping}).
*/
public class ObjectMapper
extends ObjectCodec
implements Versioned,
java.io.Serializable // as of 2.1
{
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 2L; // as of 2.9
/*
/**********************************************************
@@ -134,11 +136,15 @@
*<p>
* Since 2.4 there are special exceptions for JSON Tree model
* types (sub-types of {@link TreeNode}: default typing is never
- * applied to them
- * (see <a href="https://github.com/FasterXML/jackson-databind/issues/88">databind#88</a> for details)
- *<p>
+ * applied to them.
* Since 2.8(.4) additional checks are made to avoid attempts at default
* typing primitive-valued properties.
+ *<p>
+ * NOTE: use of Default Typing can be a potential security risk if incoming
+ * content comes from untrusted sources, and it is recommended that this
+ * is either not done, or, if enabled, use {@link #setDefaultTyping}
+ * passing a custom {@link TypeResolverBuilder} implementation that white-lists
+ * legal types to use.
*/
public enum DefaultTyping {
/**
@@ -284,8 +290,6 @@
// 16-May-2009, tatu: Ditto ^^^
protected final static AnnotationIntrospector DEFAULT_ANNOTATION_INTROSPECTOR = new JacksonAnnotationIntrospector();
- protected final static VisibilityChecker<?> STD_VISIBILITY_CHECKER = VisibilityChecker.Std.defaultInstance();
-
/**
* Base settings contain defaults used for all {@link ObjectMapper}
* instances.
@@ -293,7 +297,7 @@
protected final static BaseSettings DEFAULT_BASE = new BaseSettings(
null, // can not share global ClassIntrospector any more (2.5+)
DEFAULT_ANNOTATION_INTROSPECTOR,
- STD_VISIBILITY_CHECKER, null, TypeFactory.defaultInstance(),
+ null, TypeFactory.defaultInstance(),
null, StdDateFormat.instance, null,
Locale.getDefault(),
null, // to indicate "use Jackson default TimeZone" (UTC since Jackson 2.7)
@@ -334,9 +338,9 @@
* Currently active per-type configuration overrides, accessed by
* declared type of property.
*
- * @since 2.8
+ * @since 2.9
*/
- protected ConfigOverrides _propertyOverrides;
+ protected final ConfigOverrides _configOverrides;
/*
/**********************************************************
@@ -497,12 +501,14 @@
_subtypeResolver = src._subtypeResolver;
_typeFactory = src._typeFactory;
_injectableValues = src._injectableValues;
- _propertyOverrides = src._propertyOverrides.copy();
+ _configOverrides = src._configOverrides.copy();
_mixIns = src._mixIns.copy();
RootNameLookup rootNames = new RootNameLookup();
- _serializationConfig = new SerializationConfig(src._serializationConfig, _mixIns, rootNames, _propertyOverrides);
- _deserializationConfig = new DeserializationConfig(src._deserializationConfig, _mixIns, rootNames, _propertyOverrides);
+ _serializationConfig = new SerializationConfig(src._serializationConfig,
+ _mixIns, rootNames, _configOverrides);
+ _deserializationConfig = new DeserializationConfig(src._deserializationConfig,
+ _mixIns, rootNames, _configOverrides);
_serializerProvider = src._serializerProvider.copy();
_deserializationContext = src._deserializationContext.copy();
@@ -555,12 +561,11 @@
SimpleMixInResolver mixins = new SimpleMixInResolver(null);
_mixIns = mixins;
BaseSettings base = DEFAULT_BASE.withClassIntrospector(defaultClassIntrospector());
- ConfigOverrides propOverrides = new ConfigOverrides();
- _propertyOverrides = propOverrides;
+ _configOverrides = new ConfigOverrides();
_serializationConfig = new SerializationConfig(base,
- _subtypeResolver, mixins, rootNames, propOverrides);
+ _subtypeResolver, mixins, rootNames, _configOverrides);
_deserializationConfig = new DeserializationConfig(base,
- _subtypeResolver, mixins, rootNames, propOverrides);
+ _subtypeResolver, mixins, rootNames, _configOverrides);
// Some overrides we may need
final boolean needOrder = _jsonFactory.requiresPropertyOrdering();
@@ -618,6 +623,7 @@
protected void _checkInvalidCopy(Class<?> exp)
{
if (getClass() != exp) {
+ // 10-Nov-2016, tatu: could almost use `ClassUtil.verifyMustOverride()` but not quite
throw new IllegalStateException("Failed copy(): "+getClass().getName()
+" (version: "+version()+") does not override copy(); it has to");
}
@@ -741,8 +747,6 @@
throw new IllegalArgumentException("Module without defined version");
}
- final ObjectMapper mapper = this;
-
// And then call registration
module.setupModule(new Module.SetupContext()
{
@@ -757,7 +761,7 @@
@Override
public <C extends ObjectCodec> C getOwner() {
// why do we need the cast here?!?
- return (C) mapper;
+ return (C) ObjectMapper.this;
}
@Override
@@ -767,140 +771,140 @@
@Override
public boolean isEnabled(MapperFeature f) {
- return mapper.isEnabled(f);
+ return ObjectMapper.this.isEnabled(f);
}
@Override
public boolean isEnabled(DeserializationFeature f) {
- return mapper.isEnabled(f);
+ return ObjectMapper.this.isEnabled(f);
}
@Override
public boolean isEnabled(SerializationFeature f) {
- return mapper.isEnabled(f);
+ return ObjectMapper.this.isEnabled(f);
}
@Override
public boolean isEnabled(JsonFactory.Feature f) {
- return mapper.isEnabled(f);
+ return ObjectMapper.this.isEnabled(f);
}
@Override
public boolean isEnabled(JsonParser.Feature f) {
- return mapper.isEnabled(f);
+ return ObjectMapper.this.isEnabled(f);
}
@Override
public boolean isEnabled(JsonGenerator.Feature f) {
- return mapper.isEnabled(f);
+ return ObjectMapper.this.isEnabled(f);
}
// // // Mutant accessors
@Override
public MutableConfigOverride configOverride(Class<?> type) {
- return mapper.configOverride(type);
+ return ObjectMapper.this.configOverride(type);
}
// // // Methods for registering handlers: deserializers
@Override
public void addDeserializers(Deserializers d) {
- DeserializerFactory df = mapper._deserializationContext._factory.withAdditionalDeserializers(d);
- mapper._deserializationContext = mapper._deserializationContext.with(df);
+ DeserializerFactory df = _deserializationContext._factory.withAdditionalDeserializers(d);
+ _deserializationContext = _deserializationContext.with(df);
}
@Override
public void addKeyDeserializers(KeyDeserializers d) {
- DeserializerFactory df = mapper._deserializationContext._factory.withAdditionalKeyDeserializers(d);
- mapper._deserializationContext = mapper._deserializationContext.with(df);
+ DeserializerFactory df = _deserializationContext._factory.withAdditionalKeyDeserializers(d);
+ _deserializationContext = _deserializationContext.with(df);
}
@Override
public void addBeanDeserializerModifier(BeanDeserializerModifier modifier) {
- DeserializerFactory df = mapper._deserializationContext._factory.withDeserializerModifier(modifier);
- mapper._deserializationContext = mapper._deserializationContext.with(df);
+ DeserializerFactory df = _deserializationContext._factory.withDeserializerModifier(modifier);
+ _deserializationContext = _deserializationContext.with(df);
}
// // // Methods for registering handlers: serializers
@Override
public void addSerializers(Serializers s) {
- mapper._serializerFactory = mapper._serializerFactory.withAdditionalSerializers(s);
+ _serializerFactory = _serializerFactory.withAdditionalSerializers(s);
}
@Override
public void addKeySerializers(Serializers s) {
- mapper._serializerFactory = mapper._serializerFactory.withAdditionalKeySerializers(s);
+ _serializerFactory = _serializerFactory.withAdditionalKeySerializers(s);
}
@Override
public void addBeanSerializerModifier(BeanSerializerModifier modifier) {
- mapper._serializerFactory = mapper._serializerFactory.withSerializerModifier(modifier);
+ _serializerFactory = _serializerFactory.withSerializerModifier(modifier);
}
// // // Methods for registering handlers: other
@Override
public void addAbstractTypeResolver(AbstractTypeResolver resolver) {
- DeserializerFactory df = mapper._deserializationContext._factory.withAbstractTypeResolver(resolver);
- mapper._deserializationContext = mapper._deserializationContext.with(df);
+ DeserializerFactory df = _deserializationContext._factory.withAbstractTypeResolver(resolver);
+ _deserializationContext = _deserializationContext.with(df);
}
@Override
public void addTypeModifier(TypeModifier modifier) {
- TypeFactory f = mapper._typeFactory;
+ TypeFactory f = _typeFactory;
f = f.withModifier(modifier);
- mapper.setTypeFactory(f);
+ setTypeFactory(f);
}
@Override
public void addValueInstantiators(ValueInstantiators instantiators) {
- DeserializerFactory df = mapper._deserializationContext._factory.withValueInstantiators(instantiators);
- mapper._deserializationContext = mapper._deserializationContext.with(df);
+ DeserializerFactory df = _deserializationContext._factory.withValueInstantiators(instantiators);
+ _deserializationContext = _deserializationContext.with(df);
}
@Override
public void setClassIntrospector(ClassIntrospector ci) {
- mapper._deserializationConfig = mapper._deserializationConfig.with(ci);
- mapper._serializationConfig = mapper._serializationConfig.with(ci);
+ _deserializationConfig = _deserializationConfig.with(ci);
+ _serializationConfig = _serializationConfig.with(ci);
}
@Override
public void insertAnnotationIntrospector(AnnotationIntrospector ai) {
- mapper._deserializationConfig = mapper._deserializationConfig.withInsertedAnnotationIntrospector(ai);
- mapper._serializationConfig = mapper._serializationConfig.withInsertedAnnotationIntrospector(ai);
+ _deserializationConfig = _deserializationConfig.withInsertedAnnotationIntrospector(ai);
+ _serializationConfig = _serializationConfig.withInsertedAnnotationIntrospector(ai);
}
@Override
public void appendAnnotationIntrospector(AnnotationIntrospector ai) {
- mapper._deserializationConfig = mapper._deserializationConfig.withAppendedAnnotationIntrospector(ai);
- mapper._serializationConfig = mapper._serializationConfig.withAppendedAnnotationIntrospector(ai);
+ _deserializationConfig = _deserializationConfig.withAppendedAnnotationIntrospector(ai);
+ _serializationConfig = _serializationConfig.withAppendedAnnotationIntrospector(ai);
}
@Override
public void registerSubtypes(Class<?>... subtypes) {
- mapper.registerSubtypes(subtypes);
+ ObjectMapper.this.registerSubtypes(subtypes);
}
@Override
public void registerSubtypes(NamedType... subtypes) {
- mapper.registerSubtypes(subtypes);
+ ObjectMapper.this.registerSubtypes(subtypes);
}
@Override
public void setMixInAnnotations(Class<?> target, Class<?> mixinSource) {
- mapper.addMixIn(target, mixinSource);
+ addMixIn(target, mixinSource);
}
@Override
public void addDeserializationProblemHandler(DeserializationProblemHandler handler) {
- mapper.addHandler(handler);
+ addHandler(handler);
}
@Override
public void setNamingStrategy(PropertyNamingStrategy naming) {
- mapper.setPropertyNamingStrategy(naming);
+ setPropertyNamingStrategy(naming);
}
});
return this;
@@ -1199,7 +1203,7 @@
public final void addMixInAnnotations(Class<?> target, Class<?> mixinSource) {
addMixIn(target, mixinSource);
}
-
+
/*
/**********************************************************
/* Configuration, introspection
@@ -1216,28 +1220,20 @@
}
/**
- * @deprecated Since 2.6 use {@link #setVisibility(VisibilityChecker)} instead.
- */
- @Deprecated
- public void setVisibilityChecker(VisibilityChecker<?> vc) {
- setVisibility(vc);
- }
-
- /**
- * Method for setting currently configured {@link VisibilityChecker},
+ * Method for setting currently configured default {@link VisibilityChecker},
* object used for determining whether given property element
* (method, field, constructor) can be auto-detected or not.
- * This default checker is used if no per-class overrides
- * are defined.
+ * This default checker is used as the base visibility:
+ * per-class overrides (both via annotations and per-type config overrides)
+ * can further change these settings.
*
* @since 2.6
*/
public ObjectMapper setVisibility(VisibilityChecker<?> vc) {
- _deserializationConfig = _deserializationConfig.with(vc);
- _serializationConfig = _serializationConfig.with(vc);
+ _configOverrides.setDefaultVisibility(vc);
return this;
}
-
+
/**
* Convenience method that allows changing configuration for
* underlying {@link VisibilityChecker}s, to change details of what kinds of
@@ -1264,11 +1260,12 @@
*/
public ObjectMapper setVisibility(PropertyAccessor forMethod, JsonAutoDetect.Visibility visibility)
{
- _deserializationConfig = _deserializationConfig.withVisibility(forMethod, visibility);
- _serializationConfig = _serializationConfig.withVisibility(forMethod, visibility);
+ VisibilityChecker<?> vc = _configOverrides.getDefaultVisibility();
+ vc = vc.withVisibility(forMethod, visibility);
+ _configOverrides.setDefaultVisibility(vc);
return this;
}
-
+
/**
* Method for accessing subtype resolver in use.
*/
@@ -1342,27 +1339,6 @@
}
/**
- * Convenience method, equivalent to calling:
- *<pre>
- * setPropertyInclusion(JsonInclude.Value.construct(incl, Include.ALWAYS));
- *</pre>
- */
- public ObjectMapper setSerializationInclusion(JsonInclude.Include incl) {
- setPropertyInclusion(JsonInclude.Value.construct(incl, JsonInclude.Include.USE_DEFAULTS));
- return this;
- }
-
- /**
- * Method for setting default POJO property inclusion strategy for serialization.
- *
- * @since 2.7
- */
- public ObjectMapper setPropertyInclusion(JsonInclude.Value incl) {
- _serializationConfig = _serializationConfig.withPropertyInclusion(incl);
- return this;
- }
-
- /**
* Method for specifying {@link PrettyPrinter} to use when "default pretty-printing"
* is enabled (by enabling {@link SerializationFeature#INDENT_OUTPUT})
*
@@ -1377,6 +1353,105 @@
return this;
}
+ /**
+ * @deprecated Since 2.6 use {@link #setVisibility(VisibilityChecker)} instead.
+ */
+ @Deprecated
+ public void setVisibilityChecker(VisibilityChecker<?> vc) {
+ setVisibility(vc);
+ }
+
+ /*
+ /**********************************************************
+ /* Configuration: global-default/per-type override settings
+ /**********************************************************
+ */
+
+ /**
+ * Convenience method, equivalent to calling:
+ *<pre>
+ * setPropertyInclusion(JsonInclude.Value.construct(incl, incl));
+ *</pre>
+ *<p>
+ * NOTE: behavior differs slightly from 2.8, where second argument was
+ * implied to be <code>JsonInclude.Include.ALWAYS</code>.
+ */
+ public ObjectMapper setSerializationInclusion(JsonInclude.Include incl) {
+ setPropertyInclusion(JsonInclude.Value.construct(incl, incl));
+ return this;
+ }
+
+ /**
+ * @since 2.7
+ * @deprecated Since 2.9 use {@link #setDefaultPropertyInclusion}
+ */
+ @Deprecated
+ public ObjectMapper setPropertyInclusion(JsonInclude.Value incl) {
+ return setDefaultPropertyInclusion(incl);
+ }
+
+ /**
+ * Method for setting default POJO property inclusion strategy for serialization,
+ * applied for all properties for which there are no per-type or per-property
+ * overrides (via annotations or config overrides).
+ *
+ * @since 2.9 (basically rename of <code>setPropertyInclusion</code>)
+ */
+ public ObjectMapper setDefaultPropertyInclusion(JsonInclude.Value incl) {
+ _configOverrides.setDefaultInclusion(incl);
+ return this;
+ }
+
+ /**
+ * Short-cut for:
+ *<pre>
+ * setDefaultPropertyInclusion(JsonInclude.Value.construct(incl, incl));
+ *</pre>
+ *
+ * @since 2.9 (basically rename of <code>setPropertyInclusion</code>)
+ */
+ public ObjectMapper setDefaultPropertyInclusion(JsonInclude.Include incl) {
+ _configOverrides.setDefaultInclusion(JsonInclude.Value.construct(incl, incl));
+ return this;
+ }
+
+ /**
+ * Method for setting default Setter configuration, regarding things like
+ * merging, null-handling; used for properties for which there are
+ * no per-type or per-property overrides (via annotations or config overrides).
+ *
+ * @since 2.9
+ */
+ public ObjectMapper setDefaultSetterInfo(JsonSetter.Value v) {
+ _configOverrides.setDefaultSetterInfo(v);
+ return this;
+ }
+
+ /**
+ * Method for setting auto-detection visibility definition
+ * defaults, which are in effect unless overridden by
+ * annotations (like <code>JsonAutoDetect</code>) or per-type
+ * visibility overrides.
+ *
+ * @since 2.9
+ */
+ public ObjectMapper setDefaultVisibility(JsonAutoDetect.Value vis) {
+ _configOverrides.setDefaultVisibility(VisibilityChecker.Std.construct(vis));
+ return this;
+ }
+
+ /**
+ * Method for setting default Setter configuration, regarding things like
+ * merging, null-handling; used for properties for which there are
+ * no per-type or per-property overrides (via annotations or config overrides).
+ *
+ * @since 2.9
+ */
+ public ObjectMapper setDefaultMergeable(Boolean b) {
+ _configOverrides.setDefaultMergeable(b);
+ return this;
+ }
+
/*
/**********************************************************
/* Type information configuration
@@ -1388,6 +1463,12 @@
*<pre>
* enableObjectTyping(DefaultTyping.OBJECT_AND_NON_CONCRETE);
*</pre>
+ *<p>
+ * NOTE: use of Default Typing can be a potential security risk if incoming
+ * content comes from untrusted sources, and it is recommended that this
+ * is either not done, or, if enabled, use {@link #setDefaultTyping}
+ * passing a custom {@link TypeResolverBuilder} implementation that white-lists
+ * legal types to use.
*/
public ObjectMapper enableDefaultTyping() {
return enableDefaultTyping(DefaultTyping.OBJECT_AND_NON_CONCRETE);
@@ -1398,6 +1479,12 @@
*<pre>
* enableObjectTyping(dti, JsonTypeInfo.As.WRAPPER_ARRAY);
*</pre>
+ *<p>
+ * NOTE: use of Default Typing can be a potential security risk if incoming
+ * content comes from untrusted sources, and it is recommended that this
+ * is either not done, or, if enabled, use {@link #setDefaultTyping}
+ * passing a custom {@link TypeResolverBuilder} implementation that white-lists
+ * legal types to use.
*/
public ObjectMapper enableDefaultTyping(DefaultTyping dti) {
return enableDefaultTyping(dti, JsonTypeInfo.As.WRAPPER_ARRAY);
@@ -1411,6 +1498,12 @@
* NOTE: use of <code>JsonTypeInfo.As#EXTERNAL_PROPERTY</code> <b>NOT SUPPORTED</b>;
* and attempts of do so will throw an {@link IllegalArgumentException} to make
* this limitation explicit.
+ *<p>
+ * NOTE: use of Default Typing can be a potential security risk if incoming
+ * content comes from untrusted sources, and it is recommended that this
+ * is either not done, or, if enabled, use {@link #setDefaultTyping}
+ * passing a custom {@link TypeResolverBuilder} implementation that white-lists
+ * legal types to use.
*
* @param applicability Defines kinds of types for which additional type information
* is added; see {@link DefaultTyping} for more information.
@@ -1438,6 +1531,12 @@
* using "As.PROPERTY" inclusion mechanism and specified property name
* to use for inclusion (default being "@class" since default type information
* always uses class name as type identifier)
+ *<p>
+ * NOTE: use of Default Typing can be a potential security risk if incoming
+ * content comes from untrusted sources, and it is recommended that this
+ * is either not done, or, if enabled, use {@link #setDefaultTyping}
+ * passing a custom {@link TypeResolverBuilder} implementation that white-lists
+ * legal types to use.
*/
public ObjectMapper enableDefaultTypingAsProperty(DefaultTyping applicability, String propertyName)
{
@@ -1448,7 +1547,7 @@
typer = typer.typeProperty(propertyName);
return setDefaultTyping(typer);
}
-
+
/**
* Method for disabling automatic inclusion of type information; if so, only
* explicitly annotated types (ones with
@@ -1463,6 +1562,11 @@
* Method for enabling automatic inclusion of type information, using
* specified handler object for determining which types this affects,
* as well as details of how information is embedded.
+ *<p>
+ * NOTE: use of Default Typing can be a potential security risk if incoming
+ * content comes from untrusted sources, so care should be taken to use
+ * a {@link TypeResolverBuilder} that can limit allowed classes to
+ * deserialize.
*
* @param typer Type information inclusion handler
*/
@@ -1519,7 +1623,7 @@
* @since 2.8
*/
public MutableConfigOverride configOverride(Class<?> type) {
- return _propertyOverrides.findOrCreateOverride(type);
+ return _configOverrides.findOrCreateOverride(type);
}
/*
@@ -1549,7 +1653,7 @@
_serializationConfig = _serializationConfig.with(f);
return this;
}
-
+
/**
* Convenience method for constructing {@link JavaType} out of given
* type (typically <code>java.lang.Class</code>), but without explicit
@@ -1558,7 +1662,7 @@
public JavaType constructType(Type t) {
return _typeFactory.constructType(t);
}
-
+
/*
/**********************************************************
/* Configuration, deserialization
@@ -1578,7 +1682,7 @@
public JsonNodeFactory getNodeFactory() {
return _deserializationConfig.getNodeFactory();
}
-
+
/**
* Method for specifying {@link JsonNodeFactory} to use for
* constructing root level tree nodes (via method
@@ -1701,6 +1805,15 @@
* Method that can be used to get hold of {@link JsonFactory} that this
* mapper uses if it needs to construct {@link JsonParser}s
* and/or {@link JsonGenerator}s.
+ *<p>
+ * WARNING: note that all {@link ObjectReader} and {@link ObjectWriter}
+ * instances created by this mapper usually share the same configured
+ * {@link JsonFactory}, so changes to its configuration will "leak".
+ * To avoid such observed changes you should always use "with()" and
+ * "without()" method of {@link ObjectReader} and {@link ObjectWriter}
+ * for changing {@link com.fasterxml.jackson.core.JsonParser.Feature}
+ * and {@link com.fasterxml.jackson.core.JsonGenerator.Feature}
+ * settings to use on per-call basis.
*
* @return {@link JsonFactory} that this mapper uses when it needs to
* construct Json parser and generators
@@ -1789,7 +1902,7 @@
_serializationConfig = _serializationConfig.with(tz);
return this;
}
-
+
/*
/**********************************************************
/* Configuration, simple features: MapperFeature
@@ -1976,6 +2089,10 @@
*<p>
* Note that this is equivalent to directly calling same method
* on {@link #getFactory}.
+ *<p>
+ * WARNING: since this method directly modifies state of underlying {@link JsonFactory},
+ * it will change observed configuration by {@link ObjectReader}s as well -- to avoid
+ * this, use {@link ObjectReader#with(JsonParser.Feature)} instead.
*/
public ObjectMapper configure(JsonParser.Feature f, boolean state) {
_jsonFactory.configure(f, state);
@@ -1987,6 +2104,10 @@
* for parser instances this object mapper creates.
*<p>
* Note that this is equivalent to directly calling same method on {@link #getFactory}.
+ *<p>
+ * WARNING: since this method directly modifies state of underlying {@link JsonFactory},
+ * it will change observed configuration by {@link ObjectReader}s as well -- to avoid
+ * this, use {@link ObjectReader#with(JsonParser.Feature)} instead.
*
* @since 2.5
*/
@@ -2002,6 +2123,10 @@
* for parser instances this object mapper creates.
*<p>
* Note that this is equivalent to directly calling same method on {@link #getFactory}.
+ *<p>
+ * WARNING: since this method directly modifies state of underlying {@link JsonFactory},
+ * it will change observed configuration by {@link ObjectReader}s as well -- to avoid
+ * this, use {@link ObjectReader#without(JsonParser.Feature)} instead.
*
* @since 2.5
*/
@@ -2028,6 +2153,10 @@
*<p>
* Note that this is equivalent to directly calling same method
* on {@link #getFactory}.
+ *<p>
+ * WARNING: since this method directly modifies state of underlying {@link JsonFactory},
+ * it will change observed configuration by {@link ObjectWriter}s as well -- to avoid
+ * this, use {@link ObjectWriter#with(JsonGenerator.Feature)} instead.
*/
public ObjectMapper configure(JsonGenerator.Feature f, boolean state) {
_jsonFactory.configure(f, state);
@@ -2039,6 +2168,10 @@
* for parser instances this object mapper creates.
*<p>
* Note that this is equivalent to directly calling same method on {@link #getFactory}.
+ *<p>
+ * WARNING: since this method directly modifies state of underlying {@link JsonFactory},
+ * it will change observed configuration by {@link ObjectWriter}s as well -- to avoid
+ * this, use {@link ObjectWriter#with(JsonGenerator.Feature)} instead.
*
* @since 2.5
*/
@@ -2054,6 +2187,10 @@
* for parser instances this object mapper creates.
*<p>
* Note that this is equivalent to directly calling same method on {@link #getFactory}.
+ *<p>
+ * WARNING: since this method directly modifies state of underlying {@link JsonFactory},
+ * it will change observed configuration by {@link ObjectWriter}s as well -- to avoid
+ * this, use {@link ObjectWriter#without(JsonGenerator.Feature)} instead.
*
* @since 2.5
*/
@@ -2331,11 +2468,9 @@
* @throws JsonParseException if underlying input contains invalid content
* of type {@link JsonParser} supports (JSON for default case)
*/
- public JsonNode readTree(InputStream in)
- throws IOException, JsonProcessingException
+ public JsonNode readTree(InputStream in) throws IOException
{
- JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(in), JSON_NODE_TYPE);
- return (n == null) ? NullNode.instance : n;
+ return _readTreeAndClose(_jsonFactory.createParser(in));
}
/**
@@ -2361,11 +2496,8 @@
* as a non-null {@link JsonNode} (one that returns <code>true</code>
* for {@link JsonNode#isNull()}
*/
- public JsonNode readTree(Reader r)
- throws IOException, JsonProcessingException
- {
- JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(r), JSON_NODE_TYPE);
- return (n == null) ? NullNode.instance : n;
+ public JsonNode readTree(Reader r) throws IOException {
+ return _readTreeAndClose(_jsonFactory.createParser(r));
}
/**
@@ -2391,11 +2523,8 @@
* @throws JsonParseException if underlying input contains invalid content
* of type {@link JsonParser} supports (JSON for default case)
*/
- public JsonNode readTree(String content)
- throws IOException, JsonProcessingException
- {
- JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(content), JSON_NODE_TYPE);
- return (n == null) ? NullNode.instance : n;
+ public JsonNode readTree(String content) throws IOException {
+ return _readTreeAndClose(_jsonFactory.createParser(content));
}
/**
@@ -2414,11 +2543,8 @@
* @throws JsonParseException if underlying input contains invalid content
* of type {@link JsonParser} supports (JSON for default case)
*/
- public JsonNode readTree(byte[] content)
- throws IOException, JsonProcessingException
- {
- JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(content), JSON_NODE_TYPE);
- return (n == null) ? NullNode.instance : n;
+ public JsonNode readTree(byte[] content) throws IOException {
+ return _readTreeAndClose(_jsonFactory.createParser(content));
}
/**
@@ -2444,8 +2570,7 @@
public JsonNode readTree(File file)
throws IOException, JsonProcessingException
{
- JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(file), JSON_NODE_TYPE);
- return (n == null) ? NullNode.instance : n;
+ return _readTreeAndClose(_jsonFactory.createParser(file));
}
/**
@@ -2468,11 +2593,8 @@
* @throws JsonParseException if underlying input contains invalid content
* of type {@link JsonParser} supports (JSON for default case)
*/
- public JsonNode readTree(URL source)
- throws IOException, JsonProcessingException
- {
- JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(source), JSON_NODE_TYPE);
- return (n == null) ? NullNode.instance : n;
+ public JsonNode readTree(URL source) throws IOException {
+ return _readTreeAndClose(_jsonFactory.createParser(source));
}
/*
@@ -3071,14 +3193,14 @@
SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler());
try {
_configAndWriteValue(_jsonFactory.createGenerator(sw), value);
- } catch (JsonProcessingException e) { // to support [JACKSON-758]
+ } catch (JsonProcessingException e) {
throw e;
} catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
throw JsonMappingException.fromUnexpectedIOE(e);
}
return sw.getAndClear();
}
-
+
/**
* Method that can be used to serialize any Java value as
* a byte array. Functionally equivalent to calling
@@ -3519,8 +3641,6 @@
public <T> T convertValue(Object fromValue, Class<T> toValueType)
throws IllegalArgumentException
{
- // sanity check for null first:
- if (fromValue == null) return null;
return (T) _convert(fromValue, _typeFactory.constructType(toValueType));
}
@@ -3531,7 +3651,7 @@
public <T> T convertValue(Object fromValue, TypeReference<?> toValueTypeRef)
throws IllegalArgumentException
{
- return (T) convertValue(fromValue, _typeFactory.constructType(toValueTypeRef));
+ return (T) _convert(fromValue, _typeFactory.constructType(toValueTypeRef));
}
/**
@@ -3541,8 +3661,6 @@
public <T> T convertValue(Object fromValue, JavaType toValueType)
throws IllegalArgumentException
{
- // sanity check for null first:
- if (fromValue == null) return null;
return (T) _convert(fromValue, toValueType);
}
@@ -3557,17 +3675,20 @@
@SuppressWarnings("resource")
protected Object _convert(Object fromValue, JavaType toValueType)
throws IllegalArgumentException
- {
- // also, as per [databind#11], consider case for simple cast
- /* But with caveats: one is that while everything is Object.class, we don't
- * want to "optimize" that out; and the other is that we also do not want
- * to lose conversions of generic types.
- */
- Class<?> targetType = toValueType.getRawClass();
- if (targetType != Object.class
- && !toValueType.hasGenericTypes()
- && targetType.isAssignableFrom(fromValue.getClass())) {
- return fromValue;
+ {
+ // [databind#1433] Do not shortcut null values.
+ // This defaults primitives and fires deserializer getNullValue hooks.
+ if (fromValue != null) {
+ // also, as per [databind#11], consider case for simple cast
+ // But with caveats: one is that while everything is Object.class, we don't
+ // want to "optimize" that out; and the other is that we also do not want
+ // to lose conversions of generic types.
+ Class<?> targetType = toValueType.getRawClass();
+ if (targetType != Object.class
+ && !toValueType.hasGenericTypes()
+ && targetType.isAssignableFrom(fromValue.getClass())) {
+ return fromValue;
+ }
}
// Then use TokenBuffer, which is a JsonGenerator:
@@ -3587,7 +3708,7 @@
Object result;
// ok to pass in existing feature flags; unwrapping handled by mapper
final DeserializationConfig deserConfig = getDeserializationConfig();
- JsonToken t = _initForReading(p);
+ JsonToken t = _initForReading(p, toValueType);
if (t == JsonToken.VALUE_NULL) {
DeserializationContext ctxt = createDeserializationContext(p, deserConfig);
result = _findRootDeserializer(ctxt, toValueType).getNullValue(ctxt);
@@ -3606,6 +3727,69 @@
}
}
+ /**
+ * Convenience method similar to {@link #convertValue(Object, JavaType)} but one
+ * in which
+ *<p>
+ * Implementation is approximately as follows:
+ *<ol>
+ * <li>Serialize `updateWithValue` into {@link TokenBuffer}</li>
+ * <li>Construct {@link ObjectReader} with `valueToUpdate` (using {@link #readerForUpdating(Object)})
+ * </li>
+ * <li>Construct {@link JsonParser} (using {@link TokenBuffer#asParser()})
+ * </li>
+ * <li>Update using {@link ObjectReader#readValue(JsonParser)}.
+ * </li>
+ * <li>Return `valueToUpdate`
+ * </li>
+ *</ol>
+ *<p>
+ * Note that update is "shallow" in that only first level of properties (or, immediate contents
+ * of container to update) are modified, unless properties themselves indicate that
+ * merging should be applied for contents. Such merging can be specified using
+ * annotations (see <code>JsonMerge</code>) as well as using "config overrides" (see
+ * {@link #configOverride(Class)} and {@link #setDefaultMergeable(Boolean)}).
+ *
+ * @param valueToUpdate Object to update
+ * @param overrides Object to conceptually serialize and merge into value to
+ * update; can be thought of as a provider for overrides to apply.
+ *
+ * @return Either the first argument (`valueToUpdate`), if it is mutable; or a result of
+ * creating new instance that is result of "merging" values (for example, "updating" a
+ * Java array will create a new array)
+ *
+ * @throws JsonMappingException if there are structural incompatibilities that prevent update.
+ *
+ * @since 2.9
+ */
+ @SuppressWarnings("resource")
+ public <T> T updateValue(T valueToUpdate, Object overrides)
+ throws JsonMappingException
+ {
+ T result = valueToUpdate;
+ if ((valueToUpdate != null) && (overrides != null)) {
+ TokenBuffer buf = new TokenBuffer(this, false);
+ if (isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
+ buf = buf.forceUseOfBigDecimal(true);
+ }
+ try {
+ SerializationConfig config = getSerializationConfig().
+ without(SerializationFeature.WRAP_ROOT_VALUE);
+ _serializerProvider(config).serializeValue(buf, overrides);
+ JsonParser p = buf.asParser();
+ result = readerForUpdating(valueToUpdate).readValue(p);
+ p.close();
+ } catch (IOException e) { // should not occur, no real i/o...
+ if (e instanceof JsonMappingException) {
+ throw (JsonMappingException) e;
+ }
+ // 17-Mar-2017, tatu: Really ought not happen...
+ throw JsonMappingException.fromUnexpectedIOE(e);
+ }
+ }
+ return result;
+ }
+
/*
/**********************************************************
/* Extended Public API: JSON Schema generation
@@ -3761,7 +3945,8 @@
/**
* Actual implementation of value reading+binding operation.
*/
- protected Object _readValue(DeserializationConfig cfg, JsonParser p, JavaType valueType)
+ protected Object _readValue(DeserializationConfig cfg, JsonParser p,
+ JavaType valueType)
throws IOException
{
/* First: may need to read the next token, to initialize
@@ -3769,7 +3954,7 @@
* previous token has been cleared)
*/
Object result;
- JsonToken t = _initForReading(p);
+ JsonToken t = _initForReading(p, valueType);
if (t == JsonToken.VALUE_NULL) {
// Ask JsonDeserializer what 'null value' to use:
DeserializationContext ctxt = createDeserializationContext(p, cfg);
@@ -3790,13 +3975,13 @@
p.clearCurrentToken();
return result;
}
-
+
protected Object _readMapAndClose(JsonParser p0, JavaType valueType)
throws IOException
{
try (JsonParser p = p0) {
Object result;
- JsonToken t = _initForReading(p);
+ JsonToken t = _initForReading(p, valueType);
if (t == JsonToken.VALUE_NULL) {
// Ask JsonDeserializer what 'null value' to use:
DeserializationContext ctxt = createDeserializationContext(p,
@@ -3815,13 +4000,51 @@
}
ctxt.checkUnresolvedObjectId();
}
- // Need to consume the token too
- p.clearCurrentToken();
return result;
}
}
/**
+ * Similar to {@link #_readMapAndClose} but specialized for <code>JsonNode</code>
+ * reading.
+ *
+ * @since 2.9
+ */
+ protected JsonNode _readTreeAndClose(JsonParser p0) throws IOException
+ {
+ try (JsonParser p = p0) {
+ final JavaType valueType = JSON_NODE_TYPE;
+
+ // 27-Oct-2016, tatu: Need to inline `_initForReading()` due to
+ // special requirements by tree reading (no fail on eof)
+
+ _deserializationConfig.initialize(p); // since 2.5
+ JsonToken t = p.getCurrentToken();
+ if (t == null) {
+ t = p.nextToken();
+ if (t == null) { // [databind#1406]: expose end-of-input as `null`
+ return null;
+ }
+ }
+ if (t == JsonToken.VALUE_NULL) {
+ return _deserializationConfig.getNodeFactory().nullNode();
+ }
+ DeserializationConfig cfg = getDeserializationConfig();
+ DeserializationContext ctxt = createDeserializationContext(p, cfg);
+ JsonDeserializer<Object> deser = _findRootDeserializer(ctxt, valueType);
+ Object result;
+ if (cfg.useRootWrapping()) {
+ result = _unwrapAndDeserialize(p, ctxt, cfg, valueType, deser);
+ } else {
+ result = deser.deserialize(p, ctxt);
+ }
+ // No ObjectIds so can ignore
+// ctxt.checkUnresolvedObjectId();
+ return (JsonNode) result;
+ }
+ }
+
+ /**
* Method called to ensure that given parser is ready for reading
* content for data binding.
*
@@ -3836,7 +4059,7 @@
* content to map (note: Json "null" value is considered content;
* enf-of-stream not)
*/
- protected JsonToken _initForReading(JsonParser p) throws IOException
+ protected JsonToken _initForReading(JsonParser p, JavaType targetType) throws IOException
{
_deserializationConfig.initialize(p); // since 2.5
@@ -3851,12 +4074,18 @@
if (t == null) {
// Throw mapping exception, since it's failure to map,
// not an actual parsing problem
- throw JsonMappingException.from(p, "No content to map due to end-of-input");
+ throw MismatchedInputException.from(p, targetType,
+ "No content to map due to end-of-input");
}
}
return t;
}
+ @Deprecated // since 2.9, use method that takes JavaType too
+ protected JsonToken _initForReading(JsonParser p) throws IOException {
+ return _initForReading(p, null);
+ }
+
protected Object _unwrapAndDeserialize(JsonParser p, DeserializationContext ctxt,
DeserializationConfig config,
JavaType rootType, JsonDeserializer<Object> deser)
@@ -3866,33 +4095,34 @@
// 12-Jun-2015, tatu: Should try to support namespaces etc but...
String expSimpleName = expRootName.getSimpleName();
if (p.getCurrentToken() != JsonToken.START_OBJECT) {
- ctxt.reportWrongTokenException(p, JsonToken.START_OBJECT,
+ ctxt.reportWrongTokenException(rootType, JsonToken.START_OBJECT,
"Current token not START_OBJECT (needed to unwrap root name '%s'), but %s",
expSimpleName, p.getCurrentToken());
}
if (p.nextToken() != JsonToken.FIELD_NAME) {
- ctxt.reportWrongTokenException(p, JsonToken.FIELD_NAME,
+ ctxt.reportWrongTokenException(rootType, JsonToken.FIELD_NAME,
"Current token not FIELD_NAME (to contain expected root name '"
+expSimpleName+"'), but "+p.getCurrentToken());
}
String actualName = p.getCurrentName();
if (!expSimpleName.equals(actualName)) {
- ctxt.reportMappingException("Root name '%s' does not match expected ('%s') for type %s",
- actualName, expSimpleName, rootType);
+ ctxt.reportInputMismatch(rootType,
+ "Root name '%s' does not match expected ('%s') for type %s",
+ actualName, expSimpleName);
}
// ok, then move to value itself....
p.nextToken();
Object result = deser.deserialize(p, ctxt);
// and last, verify that we now get matching END_OBJECT
if (p.nextToken() != JsonToken.END_OBJECT) {
- ctxt.reportWrongTokenException(p, JsonToken.END_OBJECT,
+ ctxt.reportWrongTokenException(rootType, JsonToken.END_OBJECT,
"Current token not END_OBJECT (to match wrapper object with root name '%s'), but %s",
expSimpleName, p.getCurrentToken());
}
return result;
}
-
+
/*
/**********************************************************
/* Internal methods, other
@@ -3914,7 +4144,7 @@
// Nope: need to ask provider to resolve it
deser = ctxt.findRootValueDeserializer(valueType);
if (deser == null) { // can this happen?
- throw JsonMappingException.from(ctxt,
+ return ctxt.reportBadDefinition(valueType,
"Can not find a deserializer for type "+valueType);
}
_rootDeserializers.put(valueType, deser);
diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java b/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java
index d1ec95f..e413b76 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java
@@ -16,7 +16,6 @@
import com.fasterxml.jackson.databind.deser.DefaultDeserializationContext;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
-import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.TreeTraversingParser;
import com.fasterxml.jackson.databind.type.SimpleType;
import com.fasterxml.jackson.databind.type.TypeFactory;
@@ -32,12 +31,18 @@
* Instances are initially constructed by {@link ObjectMapper} and can be
* reused, shared, cached; both because of thread-safety and because
* instances are relatively light-weight.
+ *<p>
+ * NOTE: this class is NOT meant as sub-classable (with Jackson 2.8 and
+ * above) by users. It is left as non-final mostly to allow frameworks
+ * that require bytecode generation for proxying and similar use cases,
+ * but there is no expecation that functionality should be extended
+ * by sub-classing.
*/
public class ObjectReader
extends ObjectCodec
implements Versioned, java.io.Serializable // since 2.1
{
- private static final long serialVersionUID = 1L; // since 2.5
+ private static final long serialVersionUID = 2L; // since 2.9
private final static JavaType JSON_NODE_TYPE = SimpleType.constructUnsafe(JsonNode.class);
@@ -175,9 +180,6 @@
_parserFactory = mapper._jsonFactory;
_valueType = valueType;
_valueToUpdate = valueToUpdate;
- if (valueToUpdate != null && valueType.isArrayType()) {
- throw new IllegalArgumentException("Can not update an array value");
- }
_schema = schema;
_injectableValues = injectableValues;
_unwrapRoot = config.useRootWrapping();
@@ -204,9 +206,6 @@
_valueType = valueType;
_rootDeserializer = rootDeser;
_valueToUpdate = valueToUpdate;
- if (valueToUpdate != null && valueType.isArrayType()) {
- throw new IllegalArgumentException("Can not update an array value");
- }
_schema = schema;
_injectableValues = injectableValues;
_unwrapRoot = config.useRootWrapping();
@@ -333,8 +332,7 @@
/*
/**********************************************************
- /* Methods sub-classes may choose to override, if customized
- /* initialization is needed.
+ /* Methods for initializing parser instance to use
/**********************************************************
*/
@@ -355,7 +353,8 @@
t = p.nextToken();
if (t == null) {
// Throw mapping exception, since it's failure to map, not an actual parsing problem
- ctxt.reportMissingContent(null); // default msg is fine
+ ctxt.reportInputMismatch(_valueType,
+ "No content to map due to end-of-input");
}
}
return t;
@@ -1010,7 +1009,7 @@
*/
@Override
@SuppressWarnings("unchecked")
- public <T> T readValue(JsonParser p, ResolvedType valueType) throws IOException, JsonProcessingException {
+ public <T> T readValue(JsonParser p, ResolvedType valueType) throws IOException {
return (T) forType((JavaType)valueType).readValue(p);
}
@@ -1159,7 +1158,7 @@
}
@Override
- public void writeTree(JsonGenerator jgen, TreeNode rootNode) {
+ public void writeTree(JsonGenerator g, TreeNode rootNode) {
throw new UnsupportedOperationException();
}
@@ -1176,13 +1175,11 @@
* was specified with {@link #withValueToUpdate(Object)}.
*/
@SuppressWarnings("unchecked")
- public <T> T readValue(InputStream src)
- throws IOException, JsonProcessingException
+ public <T> T readValue(InputStream src) throws IOException
{
if (_dataFormatReaders != null) {
return (T) _detectBindAndClose(_dataFormatReaders.findFormat(src), false);
}
-
return (T) _bindAndClose(_considerFilter(_parserFactory.createParser(src), false));
}
@@ -1193,16 +1190,14 @@
* was specified with {@link #withValueToUpdate(Object)}.
*/
@SuppressWarnings("unchecked")
- public <T> T readValue(Reader src)
- throws IOException, JsonProcessingException
+ public <T> T readValue(Reader src) throws IOException
{
if (_dataFormatReaders != null) {
_reportUndetectableSource(src);
}
-
return (T) _bindAndClose(_considerFilter(_parserFactory.createParser(src), false));
}
-
+
/**
* Method that binds content read from given JSON string,
* using configuration of this reader.
@@ -1210,8 +1205,7 @@
* was specified with {@link #withValueToUpdate(Object)}.
*/
@SuppressWarnings("unchecked")
- public <T> T readValue(String src)
- throws IOException, JsonProcessingException
+ public <T> T readValue(String src) throws IOException
{
if (_dataFormatReaders != null) {
_reportUndetectableSource(src);
@@ -1227,13 +1221,11 @@
* was specified with {@link #withValueToUpdate(Object)}.
*/
@SuppressWarnings("unchecked")
- public <T> T readValue(byte[] src)
- throws IOException, JsonProcessingException
+ public <T> T readValue(byte[] src) throws IOException
{
if (_dataFormatReaders != null) {
return (T) _detectBindAndClose(src, 0, src.length);
}
-
return (T) _bindAndClose(_considerFilter(_parserFactory.createParser(src), false));
}
@@ -1245,19 +1237,18 @@
*/
@SuppressWarnings("unchecked")
public <T> T readValue(byte[] src, int offset, int length)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_dataFormatReaders != null) {
return (T) _detectBindAndClose(src, offset, length);
}
-
return (T) _bindAndClose(_considerFilter(_parserFactory.createParser(src, offset, length),
false));
}
@SuppressWarnings("unchecked")
public <T> T readValue(File src)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_dataFormatReaders != null) {
return (T) _detectBindAndClose(_dataFormatReaders.findFormat(_inputStream(src)), true);
@@ -1274,12 +1265,11 @@
*/
@SuppressWarnings("unchecked")
public <T> T readValue(URL src)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_dataFormatReaders != null) {
return (T) _detectBindAndClose(_dataFormatReaders.findFormat(_inputStream(src)), true);
}
-
return (T) _bindAndClose(_considerFilter(_parserFactory.createParser(src), false));
}
@@ -1292,12 +1282,11 @@
*/
@SuppressWarnings("unchecked")
public <T> T readValue(JsonNode src)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_dataFormatReaders != null) {
_reportUndetectableSource(src);
}
-
return (T) _bindAndClose(_considerFilter(treeAsTokens(src), false));
}
@@ -1322,8 +1311,7 @@
* it will just be ignored; result is always a newly constructed
* {@link JsonNode} instance.
*/
- public JsonNode readTree(InputStream in)
- throws IOException, JsonProcessingException
+ public JsonNode readTree(InputStream in) throws IOException
{
if (_dataFormatReaders != null) {
return _detectBindAndCloseAsTree(in);
@@ -1340,8 +1328,7 @@
* it will just be ignored; result is always a newly constructed
* {@link JsonNode} instance.
*/
- public JsonNode readTree(Reader r)
- throws IOException, JsonProcessingException
+ public JsonNode readTree(Reader r) throws IOException
{
if (_dataFormatReaders != null) {
_reportUndetectableSource(r);
@@ -1358,8 +1345,7 @@
* it will just be ignored; result is always a newly constructed
* {@link JsonNode} instance.
*/
- public JsonNode readTree(String json)
- throws IOException, JsonProcessingException
+ public JsonNode readTree(String json) throws IOException
{
if (_dataFormatReaders != null) {
_reportUndetectableSource(json);
@@ -1393,7 +1379,7 @@
* to the token following it.
*/
public <T> MappingIterator<T> readValues(JsonParser p)
- throws IOException, JsonProcessingException
+ throws IOException
{
DeserializationContext ctxt = createDeserializationContext(p);
// false -> do not close as caller gave parser instance
@@ -1421,7 +1407,7 @@
* <code>START_ARRAY</code> which is part of the first element).
*/
public <T> MappingIterator<T> readValues(InputStream src)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_dataFormatReaders != null) {
return _detectBindAndReadValues(_dataFormatReaders.findFormat(src), false);
@@ -1435,7 +1421,7 @@
*/
@SuppressWarnings("resource")
public <T> MappingIterator<T> readValues(Reader src)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_dataFormatReaders != null) {
_reportUndetectableSource(src);
@@ -1454,7 +1440,7 @@
*/
@SuppressWarnings("resource")
public <T> MappingIterator<T> readValues(String json)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_dataFormatReaders != null) {
_reportUndetectableSource(json);
@@ -1470,7 +1456,7 @@
* Overloaded version of {@link #readValue(InputStream)}.
*/
public <T> MappingIterator<T> readValues(byte[] src, int offset, int length)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_dataFormatReaders != null) {
return _detectBindAndReadValues(_dataFormatReaders.findFormat(src, offset, length), false);
@@ -1483,7 +1469,7 @@
* Overloaded version of {@link #readValue(InputStream)}.
*/
public final <T> MappingIterator<T> readValues(byte[] src)
- throws IOException, JsonProcessingException {
+ throws IOException {
return readValues(src, 0, src.length);
}
@@ -1491,7 +1477,7 @@
* Overloaded version of {@link #readValue(InputStream)}.
*/
public <T> MappingIterator<T> readValues(File src)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_dataFormatReaders != null) {
return _detectBindAndReadValues(
@@ -1506,7 +1492,7 @@
* @param src URL to read to access JSON content to parse.
*/
public <T> MappingIterator<T> readValues(URL src)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_dataFormatReaders != null) {
return _detectBindAndReadValues(
@@ -1540,12 +1526,12 @@
} catch (JsonProcessingException e) {
throw e;
} catch (IOException e) { // should not occur, no real i/o...
- throw new IllegalArgumentException(e.getMessage(), e);
+ throw JsonMappingException.fromUnexpectedIOE(e);
}
}
@Override
- public void writeValue(JsonGenerator gen, Object value) throws IOException, JsonProcessingException {
+ public void writeValue(JsonGenerator gen, Object value) throws IOException {
throw new UnsupportedOperationException("Not implemented for ObjectReader");
}
@@ -1582,8 +1568,9 @@
if (valueToUpdate == null) {
result = deser.deserialize(p, ctxt);
} else {
- deser.deserialize(p, ctxt, valueToUpdate);
- result = valueToUpdate;
+ // 20-Mar-2017, tatu: Important! May be different from `valueToUpdate`
+ // for immutable Objects like Java arrays; logical result
+ result = deser.deserialize(p, ctxt, valueToUpdate);
}
}
}
@@ -1634,32 +1621,43 @@
}
}
- protected JsonNode _bindAndCloseAsTree(JsonParser p0) throws IOException {
+ protected final JsonNode _bindAndCloseAsTree(JsonParser p0) throws IOException {
try (JsonParser p = p0) {
return _bindAsTree(p);
}
}
- protected JsonNode _bindAsTree(JsonParser p) throws IOException
+ protected final JsonNode _bindAsTree(JsonParser p) throws IOException
{
- JsonNode result;
- DeserializationContext ctxt = createDeserializationContext(p);
- JsonToken t = _initForReading(ctxt, p);
- if (t == JsonToken.VALUE_NULL || t == JsonToken.END_ARRAY || t == JsonToken.END_OBJECT) {
- result = NullNode.instance;
- } else {
- JsonDeserializer<Object> deser = _findTreeDeserializer(ctxt);
- if (_unwrapRoot) {
- result = (JsonNode) _unwrapAndDeserialize(p, ctxt, JSON_NODE_TYPE, deser);
- } else {
- result = (JsonNode) deser.deserialize(p, ctxt);
+ // 27-Oct-2016, tatu: Need to inline `_initForReading()` due to
+ // special requirements by tree reading (no fail on eof)
+
+ _config.initialize(p);
+ if (_schema != null) {
+ p.setSchema(_schema);
+ }
+
+ JsonToken t = p.getCurrentToken();
+ if (t == null) {
+ t = p.nextToken();
+ if (t == null) { // [databind#1406]: expose end-of-input as `null`
+ return null;
}
}
- // Need to consume the token too
- p.clearCurrentToken();
- return result;
+ DeserializationContext ctxt = createDeserializationContext(p);
+ if (t == JsonToken.VALUE_NULL) {
+ return ctxt.getNodeFactory().nullNode();
+ }
+ JsonDeserializer<Object> deser = _findTreeDeserializer(ctxt);
+ Object result;
+ if (_unwrapRoot) {
+ result = _unwrapAndDeserialize(p, ctxt, JSON_NODE_TYPE, deser);
+ } else {
+ result = deser.deserialize(p, ctxt);
+ }
+ return (JsonNode) result;
}
-
+
/**
* @since 2.1
*/
@@ -1679,18 +1677,19 @@
String expSimpleName = expRootName.getSimpleName();
if (p.getCurrentToken() != JsonToken.START_OBJECT) {
- ctxt.reportWrongTokenException(p, JsonToken.START_OBJECT,
+ ctxt.reportWrongTokenException(rootType, JsonToken.START_OBJECT,
"Current token not START_OBJECT (needed to unwrap root name '%s'), but %s",
expSimpleName, p.getCurrentToken());
}
if (p.nextToken() != JsonToken.FIELD_NAME) {
- ctxt.reportWrongTokenException(p, JsonToken.FIELD_NAME,
+ ctxt.reportWrongTokenException(rootType, JsonToken.FIELD_NAME,
"Current token not FIELD_NAME (to contain expected root name '%s'), but %s",
expSimpleName, p.getCurrentToken());
}
String actualName = p.getCurrentName();
if (!expSimpleName.equals(actualName)) {
- ctxt.reportMappingException("Root name '%s' does not match expected ('%s') for type %s",
+ ctxt.reportInputMismatch(rootType,
+ "Root name '%s' does not match expected ('%s') for type %s",
actualName, expSimpleName, rootType);
}
// ok, then move to value itself....
@@ -1704,7 +1703,7 @@
}
// and last, verify that we now get matching END_OBJECT
if (p.nextToken() != JsonToken.END_OBJECT) {
- ctxt.reportWrongTokenException(p, JsonToken.END_OBJECT,
+ ctxt.reportWrongTokenException(rootType, JsonToken.END_OBJECT,
"Current token not END_OBJECT (to match wrapper object with root name '%s'), but %s",
expSimpleName, p.getCurrentToken());
}
@@ -1747,7 +1746,7 @@
@SuppressWarnings("resource")
protected <T> MappingIterator<T> _detectBindAndReadValues(DataFormatReaders.Match match, boolean forceClosing)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (!match.hasMatch()) {
_reportUnkownFormat(_dataFormatReaders, match);
@@ -1778,7 +1777,8 @@
* Method called to indicate that format detection failed to detect format
* of given input
*/
- protected void _reportUnkownFormat(DataFormatReaders detector, DataFormatReaders.Match match) throws JsonProcessingException
+ protected void _reportUnkownFormat(DataFormatReaders detector, DataFormatReaders.Match match)
+ throws JsonProcessingException
{
// 17-Aug-2015, tatu: Unfortunately, no parser/generator available so:
throw new JsonParseException(null, "Can not detect format from input, does not look like any of detectable formats "
@@ -1813,13 +1813,6 @@
return _context.createInstance(_config, p, _injectableValues);
}
- protected void _reportUndetectableSource(Object src) throws JsonProcessingException
- {
- // 17-Aug-2015, tatu: Unfortunately, no parser/generator available so:
- throw new JsonParseException(null, "Can not use source of type "
- +src.getClass().getName()+" with format auto-detection: must be byte- not char-based");
- }
-
protected InputStream _inputStream(URL src) throws IOException {
return src.openStream();
}
@@ -1828,6 +1821,13 @@
return new FileInputStream(f);
}
+ protected void _reportUndetectableSource(Object src) throws JsonProcessingException
+ {
+ // 17-Aug-2015, tatu: Unfortunately, no parser/generator available so:
+ throw new JsonParseException(null, "Can not use source of type "
+ +src.getClass().getName()+" with format auto-detection: must be byte- not char-based");
+ }
+
/*
/**********************************************************
/* Helper methods, locating deserializers etc
@@ -1847,9 +1847,9 @@
// Sanity check: must have actual type...
JavaType t = _valueType;
if (t == null) {
- ctxt.reportMappingException("No value type configured for ObjectReader");
+ ctxt.reportBadDefinition((JavaType) null,
+ "No value type configured for ObjectReader");
}
-
// First: have we already seen it?
JsonDeserializer<Object> deser = _rootDeserializers.get(t);
if (deser != null) {
@@ -1858,7 +1858,7 @@
// Nope: need to ask provider to resolve it
deser = ctxt.findRootValueDeserializer(t);
if (deser == null) { // can this happen?
- ctxt.reportMappingException("Can not find a deserializer for type %s", t);
+ ctxt.reportBadDefinition(t, "Can not find a deserializer for type "+t);
}
_rootDeserializers.put(t, deser);
return deser;
@@ -1875,8 +1875,8 @@
// Nope: need to ask provider to resolve it
deser = ctxt.findRootValueDeserializer(JSON_NODE_TYPE);
if (deser == null) { // can this happen?
- ctxt.reportMappingException("Can not find a deserializer for type %s",
- JSON_NODE_TYPE);
+ ctxt.reportBadDefinition(JSON_NODE_TYPE,
+ "Can not find a deserializer for type "+JSON_NODE_TYPE);
}
_rootDeserializers.put(JSON_NODE_TYPE, deser);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java b/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
index 3a6988b..312152e 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
@@ -2,7 +2,9 @@
import java.io.*;
import java.text.*;
-import java.util.*;
+import java.util.Locale;
+import java.util.Map;
+import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.core.*;
@@ -222,6 +224,9 @@
* @since 2.5
*/
protected ObjectWriter _new(ObjectWriter base, SerializationConfig config) {
+ if (config == _config) {
+ return this;
+ }
return new ObjectWriter(base, config);
}
@@ -233,6 +238,9 @@
* @since 2.5
*/
protected ObjectWriter _new(GeneratorSettings genSettings, Prefetch prefetch) {
+ if ((_generatorSettings == genSettings) && (_prefetch == prefetch)) {
+ return this;
+ }
return new ObjectWriter(this, _config, genSettings, prefetch);
}
@@ -264,8 +272,7 @@
* with specified feature enabled.
*/
public ObjectWriter with(SerializationFeature feature) {
- SerializationConfig newConfig = _config.with(feature);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.with(feature));
}
/**
@@ -273,8 +280,7 @@
* with specified features enabled.
*/
public ObjectWriter with(SerializationFeature first, SerializationFeature... other) {
- SerializationConfig newConfig = _config.with(first, other);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.with(first, other));
}
/**
@@ -282,8 +288,7 @@
* with specified features enabled.
*/
public ObjectWriter withFeatures(SerializationFeature... features) {
- SerializationConfig newConfig = _config.withFeatures(features);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withFeatures(features));
}
/**
@@ -291,8 +296,7 @@
* with specified feature enabled.
*/
public ObjectWriter without(SerializationFeature feature) {
- SerializationConfig newConfig = _config.without(feature);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.without(feature));
}
/**
@@ -300,8 +304,7 @@
* with specified features enabled.
*/
public ObjectWriter without(SerializationFeature first, SerializationFeature... other) {
- SerializationConfig newConfig = _config.without(first, other);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.without(first, other));
}
/**
@@ -309,8 +312,7 @@
* with specified features enabled.
*/
public ObjectWriter withoutFeatures(SerializationFeature... features) {
- SerializationConfig newConfig = _config.withoutFeatures(features);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withoutFeatures(features));
}
/*
@@ -323,32 +325,28 @@
* @since 2.5
*/
public ObjectWriter with(JsonGenerator.Feature feature) {
- SerializationConfig newConfig = _config.with(feature);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.with(feature));
}
/**
* @since 2.5
*/
public ObjectWriter withFeatures(JsonGenerator.Feature... features) {
- SerializationConfig newConfig = _config.withFeatures(features);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withFeatures(features));
}
/**
* @since 2.5
*/
public ObjectWriter without(JsonGenerator.Feature feature) {
- SerializationConfig newConfig = _config.without(feature);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.without(feature));
}
/**
* @since 2.5
*/
public ObjectWriter withoutFeatures(JsonGenerator.Feature... features) {
- SerializationConfig newConfig = _config.withoutFeatures(features);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withoutFeatures(features));
}
/*
@@ -361,34 +359,30 @@
* @since 2.7
*/
public ObjectWriter with(FormatFeature feature) {
- SerializationConfig newConfig = _config.with(feature);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.with(feature));
}
/**
* @since 2.7
*/
public ObjectWriter withFeatures(FormatFeature... features) {
- SerializationConfig newConfig = _config.withFeatures(features);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withFeatures(features));
}
/**
* @since 2.7
*/
public ObjectWriter without(FormatFeature feature) {
- SerializationConfig newConfig = _config.without(feature);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.without(feature));
}
/**
* @since 2.7
*/
public ObjectWriter withoutFeatures(FormatFeature... features) {
- SerializationConfig newConfig = _config.withoutFeatures(features);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withoutFeatures(features));
}
-
+
/*
/**********************************************************
/* Life-cycle, fluent factories, type-related
@@ -405,10 +399,8 @@
*
* @since 2.5
*/
- public ObjectWriter forType(JavaType rootType)
- {
- Prefetch pf = _prefetch.forRootType(this, rootType);
- return (pf == _prefetch) ? this : _new(_generatorSettings, pf);
+ public ObjectWriter forType(JavaType rootType) {
+ return _new(_generatorSettings, _prefetch.forRootType(this, rootType));
}
/**
@@ -475,8 +467,7 @@
* rather construct and returns a newly configured instance.
*/
public ObjectWriter with(DateFormat df) {
- SerializationConfig newConfig = _config.with(df);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.with(df));
}
/**
@@ -492,8 +483,10 @@
* provider for resolving filter instances by id.
*/
public ObjectWriter with(FilterProvider filterProvider) {
- return (filterProvider == _config.getFilterProvider()) ? this
- : _new(this, _config.withFilters(filterProvider));
+ if (filterProvider == _config.getFilterProvider()) {
+ return this;
+ }
+ return _new(this, _config.withFilters(filterProvider));
}
/**
@@ -501,11 +494,7 @@
* printer (or, if null, will not do any pretty-printing)
*/
public ObjectWriter with(PrettyPrinter pp) {
- GeneratorSettings genSet = _generatorSettings.with(pp);
- if (genSet == _generatorSettings) {
- return this;
- }
- return _new(genSet, _prefetch);
+ return _new(_generatorSettings.with(pp), _prefetch);
}
/**
@@ -520,16 +509,14 @@
* and empty String ("") for "do NOT add root wrapper"
*/
public ObjectWriter withRootName(String rootName) {
- SerializationConfig newConfig = _config.withRootName(rootName);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withRootName(rootName));
}
/**
* @since 2.6
*/
public ObjectWriter withRootName(PropertyName rootName) {
- SerializationConfig newConfig = _config.withRootName(rootName);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withRootName(rootName));
}
/**
@@ -543,8 +530,7 @@
* @since 2.6
*/
public ObjectWriter withoutRootName() {
- SerializationConfig newConfig = _config.withRootName(PropertyName.NO_NAME);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withRootName(PropertyName.NO_NAME));
}
/**
@@ -555,12 +541,8 @@
* rather construct and returns a newly configured instance.
*/
public ObjectWriter with(FormatSchema schema) {
- GeneratorSettings genSet = _generatorSettings.with(schema);
- if (genSet == _generatorSettings) {
- return this;
- }
_verifySchemaType(schema);
- return _new(genSet, _prefetch);
+ return _new(_generatorSettings.with(schema), _prefetch);
}
/**
@@ -580,18 +562,15 @@
* rather construct and returns a newly configured instance.
*/
public ObjectWriter withView(Class<?> view) {
- SerializationConfig newConfig = _config.withView(view);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withView(view));
}
public ObjectWriter with(Locale l) {
- SerializationConfig newConfig = _config.with(l);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.with(l));
}
public ObjectWriter with(TimeZone tz) {
- SerializationConfig newConfig = _config.with(tz);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.with(tz));
}
/**
@@ -601,19 +580,14 @@
* @since 2.1
*/
public ObjectWriter with(Base64Variant b64variant) {
- SerializationConfig newConfig = _config.with(b64variant);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.with(b64variant));
}
/**
* @since 2.3
*/
public ObjectWriter with(CharacterEscapes escapes) {
- GeneratorSettings genSet = _generatorSettings.with(escapes);
- if (genSet == _generatorSettings) {
- return this;
- }
- return _new(genSet, _prefetch);
+ return _new(_generatorSettings.with(escapes), _prefetch);
}
/**
@@ -627,8 +601,7 @@
* @since 2.3
*/
public ObjectWriter with(ContextAttributes attrs) {
- SerializationConfig newConfig = _config.with(attrs);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.with(attrs));
}
/**
@@ -638,46 +611,35 @@
* @since 2.3
*/
public ObjectWriter withAttributes(Map<?,?> attrs) {
- SerializationConfig newConfig = _config.withAttributes(attrs);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withAttributes(attrs));
}
/**
* @since 2.3
*/
public ObjectWriter withAttribute(Object key, Object value) {
- SerializationConfig newConfig = _config.withAttribute(key, value);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withAttribute(key, value));
}
/**
* @since 2.3
*/
public ObjectWriter withoutAttribute(Object key) {
- SerializationConfig newConfig = _config.withoutAttribute(key);
- return (newConfig == _config) ? this : _new(this, newConfig);
+ return _new(this, _config.withoutAttribute(key));
}
/**
* @since 2.5
*/
public ObjectWriter withRootValueSeparator(String sep) {
- GeneratorSettings genSet = _generatorSettings.withRootValueSeparator(sep);
- if (genSet == _generatorSettings) {
- return this;
- }
- return _new(genSet, _prefetch);
+ return _new(_generatorSettings.withRootValueSeparator(sep), _prefetch);
}
/**
* @since 2.5
*/
public ObjectWriter withRootValueSeparator(SerializableString sep) {
- GeneratorSettings genSet = _generatorSettings.withRootValueSeparator(sep);
- if (genSet == _generatorSettings) {
- return this;
- }
- return _new(genSet, _prefetch);
+ return _new(_generatorSettings.withRootValueSeparator(sep), _prefetch);
}
/*
@@ -869,7 +831,7 @@
}
/**
- * @since 2.8.5
+ * @since 2.9
*/
@Deprecated
public boolean isEnabled(JsonParser.Feature f) {
@@ -877,7 +839,7 @@
}
/**
- * @since 2.8.5
+ * @since 2.9
*/
public boolean isEnabled(JsonGenerator.Feature f) {
return _generatorFactory.isEnabled(f);
@@ -1029,14 +991,14 @@
SegmentedStringWriter sw = new SegmentedStringWriter(_generatorFactory._getBufferRecycler());
try {
_configAndWriteValue(_generatorFactory.createGenerator(sw), value);
- } catch (JsonProcessingException e) { // to support [JACKSON-758]
+ } catch (JsonProcessingException e) {
throw e;
} catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
throw JsonMappingException.fromUnexpectedIOE(e);
}
return sw.getAndClear();
}
-
+
/**
* Method that can be used to serialize any Java value as
* a byte array. Functionally equivalent to calling
@@ -1276,11 +1238,13 @@
if (rootValueSeparator == null) {
return this;
}
- } else if (sep.equals(rootValueSeparator)) {
+ return new GeneratorSettings(prettyPrinter, schema, characterEscapes, null);
+ }
+ if (sep.equals(_rootValueSeparatorAsString())) {
return this;
}
return new GeneratorSettings(prettyPrinter, schema, characterEscapes,
- (sep == null) ? null : new SerializedString(sep));
+ new SerializedString(sep));
}
public GeneratorSettings withRootValueSeparator(SerializableString sep) {
@@ -1288,15 +1252,18 @@
if (rootValueSeparator == null) {
return this;
}
- } else {
- if (rootValueSeparator != null
- && sep.getValue().equals(rootValueSeparator.getValue())) {
- return this;
- }
+ return new GeneratorSettings(prettyPrinter, schema, characterEscapes, null);
+ }
+ if (sep.equals(rootValueSeparator)) {
+ return this;
}
return new GeneratorSettings(prettyPrinter, schema, characterEscapes, sep);
}
+ private final String _rootValueSeparatorAsString() {
+ return (rootValueSeparator == null) ? null : rootValueSeparator.getValue();
+ }
+
/**
* @since 2.6
*/
diff --git a/src/main/java/com/fasterxml/jackson/databind/PropertyMetadata.java b/src/main/java/com/fasterxml/jackson/databind/PropertyMetadata.java
index 52ec5b5..9b57186 100644
--- a/src/main/java/com/fasterxml/jackson/databind/PropertyMetadata.java
+++ b/src/main/java/com/fasterxml/jackson/databind/PropertyMetadata.java
@@ -1,9 +1,12 @@
package com.fasterxml.jackson.databind;
+import com.fasterxml.jackson.annotation.Nulls;
+import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
+
/**
* Simple container class used for storing "additional" metadata about
* properties. Carved out to reduce number of distinct properties that
- * actual property implementations and placeholders need to store;
+ * actual property implementations and place holders need to store;
* since instances are immutable, they can be freely shared.
*
* @since 2.3
@@ -13,12 +16,51 @@
{
private static final long serialVersionUID = -1;
- public final static PropertyMetadata STD_REQUIRED = new PropertyMetadata(Boolean.TRUE, null, null, null);
+ public final static PropertyMetadata STD_REQUIRED = new PropertyMetadata(Boolean.TRUE,
+ null, null, null, null, null, null);
- public final static PropertyMetadata STD_OPTIONAL = new PropertyMetadata(Boolean.FALSE, null, null, null);
+ public final static PropertyMetadata STD_OPTIONAL = new PropertyMetadata(Boolean.FALSE,
+ null, null, null, null, null, null);
- public final static PropertyMetadata STD_REQUIRED_OR_OPTIONAL = new PropertyMetadata(null, null, null, null);
-
+ public final static PropertyMetadata STD_REQUIRED_OR_OPTIONAL = new PropertyMetadata(null,
+ null, null, null, null, null, null);
+
+ /**
+ * Helper class used for containing information about expected merge
+ * information for this property, if merging is expected.
+ *
+ * @since 2.9
+ */
+ public final static class MergeInfo
+ // NOTE: need not be Serializable, not persisted
+ {
+ public final AnnotatedMember getter;
+
+ /**
+ * Flag that is set if the information came from global defaults,
+ * and not from explicit per-property annotations or per-type
+ * config overrides.
+ */
+ public final boolean fromDefaults;
+
+ protected MergeInfo(AnnotatedMember getter, boolean fromDefaults) {
+ this.getter = getter;
+ this.fromDefaults = fromDefaults;
+ }
+
+ public static MergeInfo createForDefaults(AnnotatedMember getter) {
+ return new MergeInfo(getter, true);
+ }
+
+ public static MergeInfo createForTypeOverride(AnnotatedMember getter) {
+ return new MergeInfo(getter, false);
+ }
+
+ public static MergeInfo createForPropertyOverride(AnnotatedMember getter) {
+ return new MergeInfo(getter, false);
+ }
+ }
+
/**
* Three states: required, not required and unknown; unknown represented
* as null.
@@ -38,38 +80,53 @@
protected final Integer _index;
/**
- * Optional default value, as String, for property; not used cor
+ * Optional default value, as String, for property; not used for
* any functionality by core databind, offered as metadata for
* extensions.
*/
protected final String _defaultValue;
-
+
+ /**
+ * Settings regarding merging, if property is determined to possibly
+ * be mergeable (possibly since global settings may be omitted for
+ * non-mergeable types).
+ *<p>
+ * NOTE: transient since it is assumed that this information is only
+ * relevant during initial setup and not needed after full initialization.
+ * May be changed if this proves necessary.
+ *
+ * @since 2.9
+ */
+ protected final transient MergeInfo _mergeInfo;
+
+ /**
+ * Settings regarding handling of incoming `null`s, both for value itself
+ * and, for structured types, content values (array/Collection elements,
+ * Map values).
+ *
+ * @since 2.9
+ */
+ protected Nulls _valueNulls, _contentNulls;
+
/*
/**********************************************************
/* Construction, configuration
/**********************************************************
*/
-
- @Deprecated // since 2.4
- protected PropertyMetadata(Boolean req, String desc) { this(req, desc, null, null); }
/**
- * @since 2.5
+ * @since 2.9
*/
- protected PropertyMetadata(Boolean req, String desc, Integer index, String def)
+ protected PropertyMetadata(Boolean req, String desc, Integer index, String def,
+ MergeInfo mergeInfo, Nulls valueNulls, Nulls contentNulls)
{
_required = req;
_description = desc;
_index = index;
_defaultValue = (def == null || def.isEmpty()) ? null : def;
- }
-
- /**
- * @since 2.4 Use variant that takes more arguments.
- */
- @Deprecated
- public static PropertyMetadata construct(boolean req, String desc) {
- return construct(req, desc, null, null);
+ _mergeInfo = mergeInfo;
+ _valueNulls = valueNulls;
+ _contentNulls = contentNulls;
}
/**
@@ -78,7 +135,8 @@
public static PropertyMetadata construct(Boolean req, String desc, Integer index,
String defaultValue) {
if ((desc != null) || (index != null) || (defaultValue != null)) {
- return new PropertyMetadata(req, desc, index, defaultValue);
+ return new PropertyMetadata(req, desc, index, defaultValue,
+ null, null, null);
}
if (req == null) {
return STD_REQUIRED_OR_OPTIONAL;
@@ -90,7 +148,8 @@
public static PropertyMetadata construct(boolean req, String desc, Integer index,
String defaultValue) {
if (desc != null || index != null || defaultValue != null) {
- return new PropertyMetadata(req, desc, index, defaultValue);
+ return new PropertyMetadata(req, desc, index, defaultValue,
+ null, null, null);
}
return req ? STD_REQUIRED : STD_OPTIONAL;
}
@@ -101,7 +160,9 @@
*/
protected Object readResolve()
{
- if (_description == null && _index == null && _defaultValue == null) {
+ if ((_description == null) && (_index == null) && (_defaultValue == null)
+ && (_mergeInfo == null)
+ && (_valueNulls == null) && (_contentNulls == null)) {
if (_required == null) {
return STD_REQUIRED_OR_OPTIONAL;
}
@@ -111,7 +172,25 @@
}
public PropertyMetadata withDescription(String desc) {
- return new PropertyMetadata(_required, desc, _index, _defaultValue);
+ return new PropertyMetadata(_required, desc, _index, _defaultValue,
+ _mergeInfo, _valueNulls, _contentNulls);
+ }
+
+ /**
+ * @since 2.9
+ */
+ public PropertyMetadata withMergeInfo(MergeInfo mergeInfo) {
+ return new PropertyMetadata(_required, _description, _index, _defaultValue,
+ mergeInfo, _valueNulls, _contentNulls);
+ }
+
+ /**
+ * @since 2.9
+ */
+ public PropertyMetadata withNulls(Nulls valueNulls,
+ Nulls contentNulls) {
+ return new PropertyMetadata(_required, _description, _index, _defaultValue,
+ _mergeInfo, valueNulls, contentNulls);
}
public PropertyMetadata withDefaultValue(String def) {
@@ -120,14 +199,16 @@
return this;
}
def = null;
- } else if (_defaultValue.equals(def)) {
+ } else if (def.equals(_defaultValue)) {
return this;
}
- return new PropertyMetadata(_required, _description, _index, def);
+ return new PropertyMetadata(_required, _description, _index, def,
+ _mergeInfo, _valueNulls, _contentNulls);
}
public PropertyMetadata withIndex(Integer index) {
- return new PropertyMetadata(_required, _description, index, _defaultValue);
+ return new PropertyMetadata(_required, _description, index, _defaultValue,
+ _mergeInfo, _valueNulls, _contentNulls);
}
public PropertyMetadata withRequired(Boolean b) {
@@ -135,14 +216,13 @@
if (_required == null) {
return this;
}
- } else {
- if (_required != null && _required.booleanValue() == b.booleanValue()) {
- return this;
- }
+ } else if (b.equals(_required)) {
+ return this;
}
- return new PropertyMetadata(b, _description, _index, _defaultValue);
+ return new PropertyMetadata(b, _description, _index, _defaultValue,
+ _mergeInfo, _valueNulls, _contentNulls);
}
-
+
/*
/**********************************************************
/* Accessors
@@ -157,21 +237,15 @@
public String getDefaultValue() { return _defaultValue; }
/**
- * @deprecated Since 2.6: typo in name, use {@link #hasDefaultValue()} instead.
- */
- @Deprecated
- public boolean hasDefuaultValue() { return hasDefaultValue(); }
-
- /**
* Accessor for determining whether property has declared "default value",
* which may be used by extension modules.
*
* @since 2.6
*/
public boolean hasDefaultValue() { return (_defaultValue != null); }
-
+
public boolean isRequired() { return (_required != null) && _required.booleanValue(); }
-
+
public Boolean getRequired() { return _required; }
/**
@@ -183,4 +257,19 @@
* @since 2.4
*/
public boolean hasIndex() { return _index != null; }
+
+ /**
+ * @since 2.9
+ */
+ public MergeInfo getMergeInfo() { return _mergeInfo; }
+
+ /**
+ * @since 2.9
+ */
+ public Nulls getValueNulls() { return _valueNulls; }
+
+ /**
+ * @since 2.9
+ */
+ public Nulls getContentNulls() { return _contentNulls; }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/PropertyName.java b/src/main/java/com/fasterxml/jackson/databind/PropertyName.java
index 10c24b1..5fcd044 100644
--- a/src/main/java/com/fasterxml/jackson/databind/PropertyName.java
+++ b/src/main/java/com/fasterxml/jackson/databind/PropertyName.java
@@ -4,6 +4,7 @@
import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.core.util.InternCache;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Simple value class used for containing names of properties as defined
@@ -62,17 +63,23 @@
public PropertyName(String simpleName, String namespace)
{
- _simpleName = (simpleName == null) ? "" : simpleName;
+ _simpleName = ClassUtil.nonNullString(simpleName);
_namespace = namespace;
}
// To support JDK serialization, recovery of Singleton instance
protected Object readResolve() {
- if (_simpleName == null || _USE_DEFAULT.equals(_simpleName)) {
- return USE_DEFAULT;
- }
- if (_simpleName.equals(_NO_NAME) && _namespace == null) {
- return NO_NAME;
+ if (_namespace == null) {
+ if (_simpleName == null || _USE_DEFAULT.equals(_simpleName)) {
+ return USE_DEFAULT;
+ }
+ // 30-Oct-2016, tatu: I don't see how this could ever occur...
+ // or how to distinguish USE_DEFAULT/NO_NAME from serialized
+ /*
+ if (_simpleName.equals(_NO_NAME)) {
+ return NO_NAME;
+ }
+ */
}
return this;
}
@@ -182,10 +189,8 @@
* @since 2.3
*/
public boolean hasSimpleName(String str) {
- if (str == null) {
- return _simpleName == null;
- }
- return str.equals(_simpleName);
+ // _simpleName never null so...
+ return _simpleName.equals(str);
}
public boolean hasNamespace() {
diff --git a/src/main/java/com/fasterxml/jackson/databind/SequenceWriter.java b/src/main/java/com/fasterxml/jackson/databind/SequenceWriter.java
index c6d8bc3..59efb80 100644
--- a/src/main/java/com/fasterxml/jackson/databind/SequenceWriter.java
+++ b/src/main/java/com/fasterxml/jackson/databind/SequenceWriter.java
@@ -172,7 +172,7 @@
if (_cfgCloseCloseable && (value instanceof Closeable)) {
return _writeCloseableValue(value, type);
}
- /* 15-Dec-2014, tatu: I wonder if this could be come problematic. It shouldn't
+ /* 15-Dec-2014, tatu: I wonder if this could become problematic. It shouldn't
* really, since trying to use differently paramterized types in a sequence
* is likely to run into other issues. But who knows; if it does become an
* issue, may need to implement alternative, JavaType-based map.
diff --git a/src/main/java/com/fasterxml/jackson/databind/SerializationConfig.java b/src/main/java/com/fasterxml/jackson/databind/SerializationConfig.java
index f57590f..eece8c6 100644
--- a/src/main/java/com/fasterxml/jackson/databind/SerializationConfig.java
+++ b/src/main/java/com/fasterxml/jackson/databind/SerializationConfig.java
@@ -1,7 +1,6 @@
package com.fasterxml.jackson.databind;
import java.text.DateFormat;
-import java.util.*;
import com.fasterxml.jackson.annotation.*;
@@ -10,14 +9,10 @@
import com.fasterxml.jackson.core.util.Instantiatable;
import com.fasterxml.jackson.databind.cfg.*;
-import com.fasterxml.jackson.databind.introspect.ClassIntrospector;
import com.fasterxml.jackson.databind.introspect.SimpleMixInResolver;
-import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
import com.fasterxml.jackson.databind.jsontype.SubtypeResolver;
-import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
-import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.RootNameLookup;
/**
@@ -41,10 +36,6 @@
// since 2.6
protected final static PrettyPrinter DEFAULT_PRETTY_PRINTER = new DefaultPrettyPrinter();
- // since 2.7
- // Default is "USE_DEFAULTS, USE_DEFAULTS"
- protected final static JsonInclude.Value DEFAULT_INCLUSION = JsonInclude.Value.empty();
-
/*
/**********************************************************
/* Configured helper objects
@@ -104,36 +95,21 @@
* @since 2.7
*/
protected final int _formatWriteFeaturesToChange;
-
- /*
- /**********************************************************
- /* Other configuration
- /**********************************************************
- */
-
- /**
- * Which Bean/Map properties are to be included in serialization?
- * Default settings is to include all regardless of value; can be
- * changed to only include non-null properties, or properties
- * with non-default values.
- *<p>
- * NOTE: type changed in 2.7, to include both value and content
- * inclusion options./
- */
- protected final JsonInclude.Value _serializationInclusion;
/*
/**********************************************************
- /* Life-cycle, constructors
+ /* Life-cycle, primary constructors for new instances
/**********************************************************
*/
/**
* Constructor used by ObjectMapper to create default configuration object instance.
+ *
+ * @since 2.9
*/
public SerializationConfig(BaseSettings base,
- SubtypeResolver str, SimpleMixInResolver mixins,
- RootNameLookup rootNames, ConfigOverrides configOverrides)
+ SubtypeResolver str, SimpleMixInResolver mixins, RootNameLookup rootNames,
+ ConfigOverrides configOverrides)
{
super(base, str, mixins, rootNames, configOverrides);
_serFeatures = collectFeatureDefaults(SerializationFeature.class);
@@ -143,25 +119,38 @@
_generatorFeaturesToChange = 0;
_formatWriteFeatures = 0;
_formatWriteFeaturesToChange = 0;
- _serializationInclusion = DEFAULT_INCLUSION;
}
/**
- * @deprecated Since 2.8, remove from 2.9 or later
+ * Copy-constructor used for making a copy to be used by new {@link ObjectMapper}.
+ *
+ * @since 2.9
*/
- @Deprecated
- public SerializationConfig(BaseSettings base,
- SubtypeResolver str, SimpleMixInResolver mixins,
- RootNameLookup rootNames)
+ protected SerializationConfig(SerializationConfig src,
+ SimpleMixInResolver mixins, RootNameLookup rootNames,
+ ConfigOverrides configOverrides)
{
- this(base, str, mixins, rootNames, null);
+ super(src, mixins, rootNames, configOverrides);
+ _serFeatures = src._serFeatures;
+ _filterProvider = src._filterProvider;
+ _defaultPrettyPrinter = src._defaultPrettyPrinter;
+ _generatorFeatures = src._generatorFeatures;
+ _generatorFeaturesToChange = src._generatorFeaturesToChange;
+ _formatWriteFeatures = src._formatWriteFeatures;
+ _formatWriteFeaturesToChange = src._formatWriteFeaturesToChange;
}
-
+
+ /*
+ /**********************************************************
+ /* Life-cycle, secondary constructors to support
+ /* "mutant factories", with single property changes
+ /**********************************************************
+ */
+
private SerializationConfig(SerializationConfig src, SubtypeResolver str)
{
super(src, str);
_serFeatures = src._serFeatures;
- _serializationInclusion = src._serializationInclusion;
_filterProvider = src._filterProvider;
_defaultPrettyPrinter = src._defaultPrettyPrinter;
_generatorFeatures = src._generatorFeatures;
@@ -177,7 +166,6 @@
{
super(src, mapperFeatures);
_serFeatures = serFeatures;
- _serializationInclusion = src._serializationInclusion;
_filterProvider = src._filterProvider;
_defaultPrettyPrinter = src._defaultPrettyPrinter;
_generatorFeatures = generatorFeatures;
@@ -190,7 +178,6 @@
{
super(src, base);
_serFeatures = src._serFeatures;
- _serializationInclusion = src._serializationInclusion;
_filterProvider = src._filterProvider;
_defaultPrettyPrinter = src._defaultPrettyPrinter;
_generatorFeatures = src._generatorFeatures;
@@ -203,7 +190,6 @@
{
super(src);
_serFeatures = src._serFeatures;
- _serializationInclusion = src._serializationInclusion;
_filterProvider = filters;
_defaultPrettyPrinter = src._defaultPrettyPrinter;
_generatorFeatures = src._generatorFeatures;
@@ -216,20 +202,6 @@
{
super(src, view);
_serFeatures = src._serFeatures;
- _serializationInclusion = src._serializationInclusion;
- _filterProvider = src._filterProvider;
- _defaultPrettyPrinter = src._defaultPrettyPrinter;
- _generatorFeatures = src._generatorFeatures;
- _generatorFeaturesToChange = src._generatorFeaturesToChange;
- _formatWriteFeatures = src._formatWriteFeatures;
- _formatWriteFeaturesToChange = src._formatWriteFeaturesToChange;
- }
-
- private SerializationConfig(SerializationConfig src, JsonInclude.Value incl)
- {
- super(src);
- _serFeatures = src._serFeatures;
- _serializationInclusion = incl;
_filterProvider = src._filterProvider;
_defaultPrettyPrinter = src._defaultPrettyPrinter;
_generatorFeatures = src._generatorFeatures;
@@ -242,7 +214,6 @@
{
super(src, rootName);
_serFeatures = src._serFeatures;
- _serializationInclusion = src._serializationInclusion;
_filterProvider = src._filterProvider;
_defaultPrettyPrinter = src._defaultPrettyPrinter;
_generatorFeatures = src._generatorFeatures;
@@ -258,7 +229,6 @@
{
super(src, attrs);
_serFeatures = src._serFeatures;
- _serializationInclusion = src._serializationInclusion;
_filterProvider = src._filterProvider;
_defaultPrettyPrinter = src._defaultPrettyPrinter;
_generatorFeatures = src._generatorFeatures;
@@ -274,7 +244,6 @@
{
super(src, mixins);
_serFeatures = src._serFeatures;
- _serializationInclusion = src._serializationInclusion;
_filterProvider = src._filterProvider;
_defaultPrettyPrinter = src._defaultPrettyPrinter;
_generatorFeatures = src._generatorFeatures;
@@ -290,7 +259,6 @@
{
super(src);
_serFeatures = src._serFeatures;
- _serializationInclusion = src._serializationInclusion;
_filterProvider = src._filterProvider;
_defaultPrettyPrinter = defaultPP;
_generatorFeatures = src._generatorFeatures;
@@ -299,126 +267,23 @@
_formatWriteFeaturesToChange = src._formatWriteFeaturesToChange;
}
- /**
- * Copy-constructor used for making a copy to be used by new {@link ObjectMapper}.
- *
- * @since 2.8
- */
- protected SerializationConfig(SerializationConfig src, SimpleMixInResolver mixins,
- RootNameLookup rootNames, ConfigOverrides configOverrides)
- {
- super(src, mixins, rootNames, configOverrides);
- _serFeatures = src._serFeatures;
- _serializationInclusion = src._serializationInclusion;
- _filterProvider = src._filterProvider;
- _defaultPrettyPrinter = src._defaultPrettyPrinter;
- _generatorFeatures = src._generatorFeatures;
- _generatorFeaturesToChange = src._generatorFeaturesToChange;
- _formatWriteFeatures = src._formatWriteFeatures;
- _formatWriteFeaturesToChange = src._formatWriteFeaturesToChange;
- }
-
/*
/**********************************************************
- /* Life-cycle, factory methods from MapperConfig
+ /* Life-cycle, factory methods from MapperConfig(Base)
/**********************************************************
*/
- /**
- * Fluent factory method that will construct and return a new configuration
- * object instance with specified features enabled.
- */
- @Override
- public SerializationConfig with(MapperFeature... features)
- {
- int newMapperFlags = _mapperFeatures;
- for (MapperFeature f : features) {
- newMapperFlags |= f.getMask();
- }
- return (newMapperFlags == _mapperFeatures) ? this
- : new SerializationConfig(this, newMapperFlags, _serFeatures,
+ @Override // since 2.9
+ protected final SerializationConfig _withBase(BaseSettings newBase) {
+ return (_base == newBase) ? this : new SerializationConfig(this, newBase);
+ }
+
+ @Override // since 2.9
+ protected final SerializationConfig _withMapperFeatures(int mapperFeatures) {
+ return new SerializationConfig(this, mapperFeatures, _serFeatures,
_generatorFeatures, _generatorFeaturesToChange,
_formatWriteFeatures, _formatWriteFeaturesToChange);
}
-
- /**
- * Fluent factory method that will construct and return a new configuration
- * object instance with specified features disabled.
- */
- @Override
- public SerializationConfig without(MapperFeature... features)
- {
- int newMapperFlags = _mapperFeatures;
- for (MapperFeature f : features) {
- newMapperFlags &= ~f.getMask();
- }
- return (newMapperFlags == _mapperFeatures) ? this
- : new SerializationConfig(this, newMapperFlags, _serFeatures,
- _generatorFeatures, _generatorFeaturesToChange,
- _formatWriteFeatures, _formatWriteFeaturesToChange);
- }
-
- @Override
- public SerializationConfig with(MapperFeature feature, boolean state)
- {
- int newMapperFlags;
- if (state) {
- newMapperFlags = _mapperFeatures | feature.getMask();
- } else {
- newMapperFlags = _mapperFeatures & ~feature.getMask();
- }
- return (newMapperFlags == _mapperFeatures) ? this
- : new SerializationConfig(this, newMapperFlags, _serFeatures,
- _generatorFeatures, _generatorFeaturesToChange,
- _formatWriteFeatures, _formatWriteFeaturesToChange);
- }
-
- @Override
- public SerializationConfig with(AnnotationIntrospector ai) {
- return _withBase(_base.withAnnotationIntrospector(ai));
- }
-
- @Override
- public SerializationConfig withAppendedAnnotationIntrospector(AnnotationIntrospector ai) {
- return _withBase(_base.withAppendedAnnotationIntrospector(ai));
- }
-
- @Override
- public SerializationConfig withInsertedAnnotationIntrospector(AnnotationIntrospector ai) {
- return _withBase(_base.withInsertedAnnotationIntrospector(ai));
- }
-
- @Override
- public SerializationConfig with(ClassIntrospector ci) {
- return _withBase(_base.withClassIntrospector(ci));
- }
-
- /**
- * In addition to constructing instance with specified date format,
- * will enable or disable <code>SerializationFeature.WRITE_DATES_AS_TIMESTAMPS</code>
- * (enable if format set as null; disable if non-null)
- */
- @Override
- public SerializationConfig with(DateFormat df) {
- SerializationConfig cfg = new SerializationConfig(this, _base.withDateFormat(df));
- // Also need to toggle this feature based on existence of date format:
- if (df == null) {
- cfg = cfg.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
- } else {
- cfg = cfg.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
- }
- return cfg;
- }
-
- @Override
- public SerializationConfig with(HandlerInstantiator hi) {
- return _withBase(_base.withHandlerInstantiator(hi));
- }
-
- @Override
- public SerializationConfig with(PropertyNamingStrategy pns) {
- return _withBase(_base.withPropertyNamingStrategy(pns));
- }
@Override
public SerializationConfig withRootName(PropertyName rootName) {
@@ -438,52 +303,34 @@
}
@Override
- public SerializationConfig with(TypeFactory tf) {
- return _withBase(_base.withTypeFactory(tf));
- }
-
- @Override
- public SerializationConfig with(TypeResolverBuilder<?> trb) {
- return _withBase(_base.withTypeResolverBuilder(trb));
- }
-
- @Override
public SerializationConfig withView(Class<?> view) {
return (_view == view) ? this : new SerializationConfig(this, view);
}
-
- @Override
- public SerializationConfig with(VisibilityChecker<?> vc) {
- return _withBase(_base.withVisibilityChecker(vc));
- }
-
- @Override
- public SerializationConfig withVisibility(PropertyAccessor forMethod, JsonAutoDetect.Visibility visibility) {
- return _withBase(_base.withVisibility(forMethod, visibility));
- }
-
- @Override
- public SerializationConfig with(Locale l) {
- return _withBase(_base.with(l));
- }
-
- @Override
- public SerializationConfig with(TimeZone tz) {
- return _withBase(_base.with(tz));
- }
-
- @Override
- public SerializationConfig with(Base64Variant base64) {
- return _withBase(_base.with(base64));
- }
-
+
@Override
public SerializationConfig with(ContextAttributes attrs) {
return (attrs == _attributes) ? this : new SerializationConfig(this, attrs);
}
- private final SerializationConfig _withBase(BaseSettings newBase) {
- return (_base == newBase) ? this : new SerializationConfig(this, newBase);
+ /*
+ /**********************************************************
+ /* Factory method overrides
+ /**********************************************************
+ */
+
+ /**
+ * In addition to constructing instance with specified date format,
+ * will enable or disable <code>SerializationFeature.WRITE_DATES_AS_TIMESTAMPS</code>
+ * (enable if format set as null; disable if non-null)
+ */
+ @Override
+ public SerializationConfig with(DateFormat df) {
+ SerializationConfig cfg = super.with(df);
+ // Also need to toggle this feature based on existence of date format:
+ if (df == null) {
+ return cfg.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+ }
+ return cfg.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
/*
@@ -751,24 +598,20 @@
}
/**
- * @deprecated Since 2.7 use {@link #withPropertyInclusion} instead
+ * Mutant factory method for constructing a new instance with different
+ * default inclusion criteria configuration.
+ *
+ * @since 2.7
+ *
+ * @deprecated Since 2.9; not needed any more
*/
@Deprecated
- public SerializationConfig withSerializationInclusion(JsonInclude.Include incl) {
- return withPropertyInclusion(DEFAULT_INCLUSION.withValueInclusion(incl));
+ public SerializationConfig withPropertyInclusion(JsonInclude.Value incl) {
+ _configOverrides.setDefaultInclusion(incl);
+ return this;
}
/**
- * @since 2.7
- */
- public SerializationConfig withPropertyInclusion(JsonInclude.Value incl) {
- if (_serializationInclusion.equals(incl)) {
- return this;
- }
- return new SerializationConfig(this, incl);
- }
-
- /**
* @since 2.6
*/
public SerializationConfig withDefaultPrettyPrinter(PrettyPrinter pp) {
@@ -834,85 +677,20 @@
/*
/**********************************************************
- /* MapperConfig implementation/overrides: introspection
- /**********************************************************
- */
-
- @Override
- public AnnotationIntrospector getAnnotationIntrospector()
- {
- if (isEnabled(MapperFeature.USE_ANNOTATIONS)) {
- return super.getAnnotationIntrospector();
- }
- return AnnotationIntrospector.nopInstance();
- }
-
- /**
- * Accessor for getting bean description that only contains class
- * annotations: useful if no getter/setter/creator information is needed.
- */
- @Override
- public BeanDescription introspectClassAnnotations(JavaType type) {
- return getClassIntrospector().forClassAnnotations(this, type, this);
- }
-
- /**
- * Accessor for getting bean description that only contains immediate class
- * annotations: ones from the class, and its direct mix-in, if any, but
- * not from super types.
- */
- @Override
- public BeanDescription introspectDirectClassAnnotations(JavaType type) {
- return getClassIntrospector().forDirectClassAnnotations(this, type, this);
- }
-
- /*
- /**********************************************************
/* Configuration: default settings with per-type overrides
/**********************************************************
*/
-
+
/**
* @deprecated Since 2.7 use {@link #getDefaultPropertyInclusion} instead
*/
@Deprecated
public JsonInclude.Include getSerializationInclusion()
{
- JsonInclude.Include incl = _serializationInclusion.getValueInclusion();
+ JsonInclude.Include incl = getDefaultPropertyInclusion().getValueInclusion();
return (incl == JsonInclude.Include.USE_DEFAULTS) ? JsonInclude.Include.ALWAYS : incl;
}
- @Override
- public JsonInclude.Value getDefaultPropertyInclusion() {
- return _serializationInclusion;
- }
-
- @Override
- public JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType) {
- ConfigOverride overrides = findConfigOverride(baseType);
- if (overrides != null) {
- JsonInclude.Value v = overrides.getInclude();
- if (v != null) {
- return v;
- }
- }
- return _serializationInclusion;
- }
-
- @Override
- public JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType,
- JsonInclude.Value defaultIncl)
- {
- ConfigOverride overrides = findConfigOverride(baseType);
- if (overrides != null) {
- JsonInclude.Value v = overrides.getInclude();
- if (v != null) {
- return v;
- }
- }
- return defaultIncl;
- }
-
/*
/**********************************************************
/* Configuration: other
@@ -970,7 +748,7 @@
public FilterProvider getFilterProvider() {
return _filterProvider;
}
-
+
/**
* Accessor for configured blueprint "default" {@link PrettyPrinter} to
* use, if default pretty-printing is enabled.
@@ -999,15 +777,4 @@
public <T extends BeanDescription> T introspect(JavaType type) {
return (T) getClassIntrospector().forSerialization(this, type, this);
}
-
- /*
- /**********************************************************
- /* Debug support
- /**********************************************************
- */
-
- @Override
- public String toString() {
- return "[SerializationConfig: flags=0x"+Integer.toHexString(_serFeatures)+"]";
- }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/SerializationFeature.java b/src/main/java/com/fasterxml/jackson/databind/SerializationFeature.java
index e8d1cb3..2c940d7 100644
--- a/src/main/java/com/fasterxml/jackson/databind/SerializationFeature.java
+++ b/src/main/java/com/fasterxml/jackson/databind/SerializationFeature.java
@@ -275,8 +275,17 @@
* Feature that determines whether Map entries with null values are
* to be serialized (true) or not (false).
*<p>
+ * NOTE: unlike other {@link SerializationFeature}s, this feature <b>can not</b> be
+ * dynamically changed on per-call basis, because its effect is considered during
+ * construction of serializers and property handlers.
+ *<p>
* Feature is enabled by default.
+ *
+ * @deprecated Since 2.9 there are better mechanism for specifying filtering; specifically
+ * using {@link com.fasterxml.jackson.annotation.JsonInclude} or configuration overrides
+ * (see {@link ObjectMapper#configOverride(Class)}}).
*/
+ @Deprecated // since 2.9
WRITE_NULL_MAP_VALUES(true),
/**
diff --git a/src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java b/src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java
index bce67fa..1acf90f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java
+++ b/src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java
@@ -12,6 +12,8 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
+import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
@@ -211,9 +213,6 @@
protected SerializerProvider(SerializerProvider src,
SerializationConfig config, SerializerFactory f)
{
- if (config == null) {
- throw new NullPointerException();
- }
_serializerFactory = f;
_config = config;
@@ -445,6 +444,17 @@
return _config.getFilterProvider();
}
+ /**
+ *<p>
+ * NOTE: current implementation simply returns `null` as generator is not yet
+ * assigned to this provider.
+ *
+ * @since 2.8
+ */
+ public JsonGenerator getGenerator() {
+ return null;
+ }
+
/*
/**********************************************************
/* Access to Object Id aspects
@@ -519,6 +529,8 @@
* full generics-aware type instead of raw class.
* This is necessary for accurate handling of external type information,
* to handle polymorphic types.
+ *<p>
+ * Note: this call will also contextualize serializer before returning it.
*
* @param property When creating secondary serializers, property for which
* serializer is needed: annotations of the property (or bean that contains it)
@@ -528,6 +540,9 @@
public JsonSerializer<Object> findValueSerializer(JavaType valueType, BeanProperty property)
throws JsonMappingException
{
+ if (valueType == null) {
+ reportMappingProblem("Null passed for `valueType` of `findValueSerializer()`");
+ }
// (see comments from above method)
JsonSerializer<Object> ser = _knownSerializers.untypedValueSerializer(valueType);
if (ser == null) {
@@ -913,6 +928,29 @@
Object serDef)
throws JsonMappingException;
+ /**
+ * Method that can be called to construct and configure {@link JsonInclude}
+ * filter instance,
+ * given a {@link Class} to instantiate (with default constructor, by default).
+ *
+ * @param forProperty (optional) If filter is created for a property, that property;
+ * `null` if filter created via defaulting, global or per-type.
+ *
+ * @since 2.9
+ */
+ public abstract Object includeFilterInstance(BeanPropertyDefinition forProperty,
+ Class<?> filterClass)
+ throws JsonMappingException;
+
+ /**
+ * Follow-up method that may be called after calling {@link #includeFilterInstance},
+ * to check handling of `null` values by the filter.
+ *
+ * @since 2.9
+ */
+ public abstract boolean includeFilterSuppressNulls(Object filter)
+ throws JsonMappingException;
+
/*
/**********************************************************
/* Support for contextualization
@@ -1097,34 +1135,6 @@
*/
/**
- * Factory method for constructing a {@link JsonMappingException};
- * usually only indirectly used by calling
- * {@link #reportMappingProblem(String, Object...)}.
- *
- * @since 2.6
- */
- public JsonMappingException mappingException(String message, Object... args) {
- if (args != null && args.length > 0) {
- message = String.format(message, args);
- }
- return JsonMappingException.from(getGenerator(), message);
- }
-
- /**
- * Factory method for constructing a {@link JsonMappingException};
- * usually only indirectly used by calling
- * {@link #reportMappingProblem(Throwable, String, Object...)}
- *
- * @since 2.8
- */
- protected JsonMappingException mappingException(Throwable t, String message, Object... args) {
- if (args != null && args.length > 0) {
- message = String.format(message, args);
- }
- return JsonMappingException.from(getGenerator(), message, t);
- }
-
- /**
* Helper method called to indicate problem; default behavior is to construct and
* throw a {@link JsonMappingException}, but in future may collect more than one
* and only throw after certain number, or at the end of serialization.
@@ -1136,17 +1146,6 @@
}
/**
- * Helper method called to indicate problem; default behavior is to construct and
- * throw a {@link JsonMappingException}, but in future may collect more than one
- * and only throw after certain number, or at the end of serialization.
- *
- * @since 2.8
- */
- public void reportMappingProblem(Throwable t, String message, Object... args) throws JsonMappingException {
- throw mappingException(t, message, args);
- }
-
- /**
* Helper method called to indicate problem in POJO (serialization) definitions or settings
* regarding specific Java type, unrelated to actual JSON content to map.
* Default behavior is to construct and throw a {@link JsonMappingException}.
@@ -1154,13 +1153,14 @@
* @since 2.9
*/
public <T> T reportBadTypeDefinition(BeanDescription bean,
- String message, Object... args) throws JsonMappingException {
- if (args != null && args.length > 0) {
- message = String.format(message, args);
+ String msg, Object... msgArgs) throws JsonMappingException {
+ String beanDesc = "N/A";
+ if (bean != null) {
+ beanDesc = ClassUtil.nameOf(bean.getBeanClass());
}
- String beanDesc = (bean == null) ? "N/A" : _desc(bean.getType().getGenericSignature());
- throw mappingException("Invalid type definition for type %s: %s",
- beanDesc, message);
+ msg = String.format("Invalid type definition for type %s: %s",
+ beanDesc, _format(msg, msgArgs));
+ throw InvalidDefinitionException.from(getGenerator(), msg, bean, null);
}
/**
@@ -1171,21 +1171,98 @@
* @since 2.9
*/
public <T> T reportBadPropertyDefinition(BeanDescription bean, BeanPropertyDefinition prop,
- String message, Object... args) throws JsonMappingException {
- if (args != null && args.length > 0) {
- message = String.format(message, args);
+ String message, Object... msgArgs) throws JsonMappingException {
+ message = _format(message, msgArgs);
+ String propName = "N/A";
+ if (prop != null) {
+ propName = _quotedString(prop.getName());
}
- String propName = (prop == null) ? "N/A" : _quotedString(prop.getName());
- String beanDesc = (bean == null) ? "N/A" : _desc(bean.getType().getGenericSignature());
- throw mappingException("Invalid definition for property %s (of type %s): %s",
+ String beanDesc = "N/A";
+ if (bean != null) {
+ beanDesc = ClassUtil.nameOf(bean.getBeanClass());
+ }
+ message = String.format("Invalid definition for property %s (of type %s): %s",
propName, beanDesc, message);
+ throw InvalidDefinitionException.from(getGenerator(), message, bean, prop);
+ }
+
+ @Override
+ public <T> T reportBadDefinition(JavaType type, String msg) throws JsonMappingException {
+ throw InvalidDefinitionException.from(getGenerator(), msg, type);
}
/**
+ * @since 2.9
+ */
+ public <T> T reportBadDefinition(JavaType type, String msg, Throwable cause)
+ throws JsonMappingException {
+ InvalidDefinitionException e = InvalidDefinitionException.from(getGenerator(), msg, type);
+ e.initCause(cause);
+ throw e;
+ }
+
+ /**
+ * @since 2.9
+ */
+ public <T> T reportBadDefinition(Class<?> raw, String msg, Throwable cause)
+ throws JsonMappingException {
+ InvalidDefinitionException e = InvalidDefinitionException.from(getGenerator(), msg, constructType(raw));
+ e.initCause(cause);
+ throw e;
+ }
+
+ /**
+ * Helper method called to indicate problem; default behavior is to construct and
+ * throw a {@link JsonMappingException}, but in future may collect more than one
+ * and only throw after certain number, or at the end of serialization.
+ *
* @since 2.8
*/
- public JsonGenerator getGenerator() {
- return null;
+ public void reportMappingProblem(Throwable t, String message, Object... msgArgs) throws JsonMappingException {
+ message = _format(message, msgArgs);
+ throw JsonMappingException.from(getGenerator(), message, t);
+ }
+
+ @Override
+ public JsonMappingException invalidTypeIdException(JavaType baseType, String typeId,
+ String extraDesc) {
+ String msg = String.format("Could not resolve type id '%s' as a subtype of %s",
+ typeId, baseType);
+ return InvalidTypeIdException.from(null, _colonConcat(msg, extraDesc), baseType, typeId);
+ }
+
+ /*
+ /********************************************************
+ /* Error reporting, deprecated methods
+ /********************************************************
+ */
+
+ /**
+ * Factory method for constructing a {@link JsonMappingException};
+ * usually only indirectly used by calling
+ * {@link #reportMappingProblem(String, Object...)}.
+ *
+ * @since 2.6
+ *
+ * @deprecated Since 2.9
+ */
+ @Deprecated // since 2.9
+ public JsonMappingException mappingException(String message, Object... msgArgs) {
+ return JsonMappingException.from(getGenerator(), _format(message, msgArgs));
+ }
+
+ /**
+ * Factory method for constructing a {@link JsonMappingException};
+ * usually only indirectly used by calling
+ * {@link #reportMappingProblem(Throwable, String, Object...)}
+ *
+ * @since 2.8
+ *
+ * @deprecated Since 2.9
+ */
+ @Deprecated // since 2.9
+ protected JsonMappingException mappingException(Throwable t, String message, Object... msgArgs) {
+ return JsonMappingException.from(getGenerator(), _format(message, msgArgs), t);
}
/*
@@ -1204,8 +1281,9 @@
return;
}
}
- reportMappingProblem("Incompatible types: declared root type (%s) vs %s",
- rootType, value.getClass().getName());
+ reportBadDefinition(rootType, String.format(
+ "Incompatible types: declared root type (%s) vs %s",
+ rootType, ClassUtil.classNameOf(value)));
}
/**
@@ -1260,8 +1338,8 @@
/* We better only expose checked exceptions, since those
* are what caller is expected to handle
*/
+ ser = null; // doesn't matter but compiler whines otherwise
reportMappingProblem(iae, iae.getMessage());
- return null; // never gets here
}
if (ser != null) {
@@ -1278,11 +1356,10 @@
try {
ser = _createUntypedSerializer(type);
} catch (IllegalArgumentException iae) {
- /* We better only expose checked exceptions, since those
- * are what caller is expected to handle
- */
+ // We better only expose checked exceptions, since those
+ // are what caller is expected to handle
+ ser = null;
reportMappingProblem(iae, iae.getMessage());
- return null; // never gets here
}
if (ser != null) {
@@ -1340,20 +1417,6 @@
/**********************************************************
*/
- protected String _desc(Object value) {
- if (value == null) {
- return "N/A";
- }
- return "'"+value+"'";
- }
-
- protected String _quotedString(Object value) {
- if (value == null) {
- return "N/A";
- }
- return String.valueOf(value);
- }
-
protected final DateFormat _dateFormat()
{
if (_dateFormat != null) {
diff --git a/src/main/java/com/fasterxml/jackson/databind/annotation/JsonPOJOBuilder.java b/src/main/java/com/fasterxml/jackson/databind/annotation/JsonPOJOBuilder.java
index 3bab933..2c6841d 100644
--- a/src/main/java/com/fasterxml/jackson/databind/annotation/JsonPOJOBuilder.java
+++ b/src/main/java/com/fasterxml/jackson/databind/annotation/JsonPOJOBuilder.java
@@ -31,52 +31,66 @@
@com.fasterxml.jackson.annotation.JacksonAnnotation
public @interface JsonPOJOBuilder
{
- /**
- * Property to use for re-defining which zero-argument method
- * is considered the actual "build-method": method called after
- * all data has been bound, and the actual instance needs to
- * be instantiated.
- *<p>
- * Default value is "build".
- */
- public String buildMethodName() default "build";
+ /**
+ * @since 2.9
+ */
+ public final static String DEFAULT_BUILD_METHOD = "build";
- /**
- * Property used for (re)defining name prefix to use for
- * auto-detecting "with-methods": methods that are similar to
- * "set-methods" (in that they take an argument), but that
- * may also return the new builder instance to use
- * (which may be 'this', or a new modified builder instance).
- * Note that in addition to this prefix, it is also possible
- * to use {@link com.fasterxml.jackson.annotation.JsonProperty}
- * annotation to indicate "with-methods" (as well as
- * {@link com.fasterxml.jackson.annotation.JsonSetter}).
- *<p>
- * Default value is "with", so that method named "withValue()"
- * would be used for binding JSON property "value" (using type
- * indicated by the argument; or one defined with annotations.
- */
- public String withPrefix() default "with";
+ /**
+ * @since 2.9
+ */
+ public final static String DEFAULT_WITH_PREFIX = "with";
+
+ /**
+ * Property to use for re-defining which zero-argument method
+ * is considered the actual "build-method": method called after
+ * all data has been bound, and the actual instance needs to
+ * be instantiated.
+ *<p>
+ * Default value is "build".
+ */
+ public String buildMethodName() default DEFAULT_BUILD_METHOD;
+
+ /**
+ * Property used for (re)defining name prefix to use for
+ * auto-detecting "with-methods": methods that are similar to
+ * "set-methods" (in that they take an argument), but that
+ * may also return the new builder instance to use
+ * (which may be 'this', or a new modified builder instance).
+ * Note that in addition to this prefix, it is also possible
+ * to use {@link com.fasterxml.jackson.annotation.JsonProperty}
+ * annotation to indicate "with-methods" (as well as
+ * {@link com.fasterxml.jackson.annotation.JsonSetter}).
+ *<p>
+ * Default value is "with", so that method named "withValue()"
+ * would be used for binding JSON property "value" (using type
+ * indicated by the argument; or one defined with annotations.
+ */
+ public String withPrefix() default DEFAULT_WITH_PREFIX;
/*
/**********************************************************
/* Helper classes
/**********************************************************
*/
-
- /**
- * Simple value container for containing values read from
- * {@link JsonPOJOBuilder} annotation instance.
- */
- public class Value
- {
- public final String buildMethodName;
- public final String withPrefix;
- public Value(JsonPOJOBuilder ann)
- {
- buildMethodName = ann.buildMethodName();
- withPrefix = ann.withPrefix();
- }
- }
+ /**
+ * Simple value container for containing values read from
+ * {@link JsonPOJOBuilder} annotation instance.
+ */
+ public class Value
+ {
+ public final String buildMethodName;
+ public final String withPrefix;
+
+ public Value(JsonPOJOBuilder ann) {
+ this(ann.buildMethodName(), ann.withPrefix());
+ }
+
+ public Value(String buildMethodName, String withPrefix)
+ {
+ this.buildMethodName = buildMethodName;
+ this.withPrefix = withPrefix;
+ }
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/cfg/BaseSettings.java b/src/main/java/com/fasterxml/jackson/databind/cfg/BaseSettings.java
index 3421e87..8c83734 100644
--- a/src/main/java/com/fasterxml/jackson/databind/cfg/BaseSettings.java
+++ b/src/main/java/com/fasterxml/jackson/databind/cfg/BaseSettings.java
@@ -4,13 +4,10 @@
import java.util.Locale;
import java.util.TimeZone;
-import com.fasterxml.jackson.annotation.JsonAutoDetect;
-import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.Base64Variant;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
import com.fasterxml.jackson.databind.introspect.ClassIntrospector;
-import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.StdDateFormat;
@@ -55,17 +52,6 @@
protected final AnnotationIntrospector _annotationIntrospector;
/**
- * Object used for determining whether specific property elements
- * (method, constructors, fields) can be auto-detected based on
- * their visibility (access modifiers). Can be changed to allow
- * different minimum visibility levels for auto-detection. Note
- * that this is the global handler; individual types (classes)
- * can further override active checker used (using
- * {@link JsonAutoDetect} annotation)
- */
- protected final VisibilityChecker<?> _visibilityChecker;
-
- /**
* Custom property naming strategy in use, if any.
*/
protected final PropertyNamingStrategy _propertyNamingStrategy;
@@ -145,13 +131,12 @@
*/
public BaseSettings(ClassIntrospector ci, AnnotationIntrospector ai,
- VisibilityChecker<?> vc, PropertyNamingStrategy pns, TypeFactory tf,
+ PropertyNamingStrategy pns, TypeFactory tf,
TypeResolverBuilder<?> typer, DateFormat dateFormat, HandlerInstantiator hi,
Locale locale, TimeZone tz, Base64Variant defaultBase64)
{
_classIntrospector = ci;
_annotationIntrospector = ai;
- _visibilityChecker = vc;
_propertyNamingStrategy = pns;
_typeFactory = tf;
_typeResolverBuilder = typer;
@@ -172,7 +157,7 @@
if (_classIntrospector == ci) {
return this;
}
- return new BaseSettings(ci, _annotationIntrospector, _visibilityChecker, _propertyNamingStrategy, _typeFactory,
+ return new BaseSettings(ci, _annotationIntrospector, _propertyNamingStrategy, _typeFactory,
_typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
_timeZone, _defaultBase64);
}
@@ -181,7 +166,7 @@
if (_annotationIntrospector == ai) {
return this;
}
- return new BaseSettings(_classIntrospector, ai, _visibilityChecker, _propertyNamingStrategy, _typeFactory,
+ return new BaseSettings(_classIntrospector, ai, _propertyNamingStrategy, _typeFactory,
_typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
_timeZone, _defaultBase64);
}
@@ -193,16 +178,8 @@
public BaseSettings withAppendedAnnotationIntrospector(AnnotationIntrospector ai) {
return withAnnotationIntrospector(AnnotationIntrospectorPair.create(_annotationIntrospector, ai));
}
-
- public BaseSettings withVisibilityChecker(VisibilityChecker<?> vc) {
- if (_visibilityChecker == vc) {
- return this;
- }
- return new BaseSettings(_classIntrospector, _annotationIntrospector, vc, _propertyNamingStrategy, _typeFactory,
- _typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
- _timeZone, _defaultBase64);
- }
+ /*
public BaseSettings withVisibility(PropertyAccessor forMethod, JsonAutoDetect.Visibility visibility) {
return new BaseSettings(_classIntrospector, _annotationIntrospector,
_visibilityChecker.withVisibility(forMethod, visibility),
@@ -210,12 +187,13 @@
_typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
_timeZone, _defaultBase64);
}
+ */
public BaseSettings withPropertyNamingStrategy(PropertyNamingStrategy pns) {
if (_propertyNamingStrategy == pns) {
return this;
}
- return new BaseSettings(_classIntrospector, _annotationIntrospector, _visibilityChecker, pns, _typeFactory,
+ return new BaseSettings(_classIntrospector, _annotationIntrospector, pns, _typeFactory,
_typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
_timeZone, _defaultBase64);
}
@@ -224,7 +202,7 @@
if (_typeFactory == tf) {
return this;
}
- return new BaseSettings(_classIntrospector, _annotationIntrospector, _visibilityChecker, _propertyNamingStrategy, tf,
+ return new BaseSettings(_classIntrospector, _annotationIntrospector, _propertyNamingStrategy, tf,
_typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
_timeZone, _defaultBase64);
}
@@ -233,7 +211,7 @@
if (_typeResolverBuilder == typer) {
return this;
}
- return new BaseSettings(_classIntrospector, _annotationIntrospector, _visibilityChecker, _propertyNamingStrategy, _typeFactory,
+ return new BaseSettings(_classIntrospector, _annotationIntrospector, _propertyNamingStrategy, _typeFactory,
typer, _dateFormat, _handlerInstantiator, _locale,
_timeZone, _defaultBase64);
}
@@ -247,7 +225,7 @@
if ((df != null) && hasExplicitTimeZone()) {
df = _force(df, _timeZone);
}
- return new BaseSettings(_classIntrospector, _annotationIntrospector, _visibilityChecker, _propertyNamingStrategy, _typeFactory,
+ return new BaseSettings(_classIntrospector, _annotationIntrospector, _propertyNamingStrategy, _typeFactory,
_typeResolverBuilder, df, _handlerInstantiator, _locale,
_timeZone, _defaultBase64);
}
@@ -256,7 +234,7 @@
if (_handlerInstantiator == hi) {
return this;
}
- return new BaseSettings(_classIntrospector, _annotationIntrospector, _visibilityChecker, _propertyNamingStrategy, _typeFactory,
+ return new BaseSettings(_classIntrospector, _annotationIntrospector, _propertyNamingStrategy, _typeFactory,
_typeResolverBuilder, _dateFormat, hi, _locale,
_timeZone, _defaultBase64);
}
@@ -265,7 +243,7 @@
if (_locale == l) {
return this;
}
- return new BaseSettings(_classIntrospector, _annotationIntrospector, _visibilityChecker, _propertyNamingStrategy, _typeFactory,
+ return new BaseSettings(_classIntrospector, _annotationIntrospector, _propertyNamingStrategy, _typeFactory,
_typeResolverBuilder, _dateFormat, _handlerInstantiator, l,
_timeZone, _defaultBase64);
}
@@ -286,7 +264,7 @@
DateFormat df = _force(_dateFormat, tz);
return new BaseSettings(_classIntrospector, _annotationIntrospector,
- _visibilityChecker, _propertyNamingStrategy, _typeFactory,
+ _propertyNamingStrategy, _typeFactory,
_typeResolverBuilder, df, _handlerInstantiator, _locale,
tz, _defaultBase64);
}
@@ -299,7 +277,7 @@
return this;
}
return new BaseSettings(_classIntrospector, _annotationIntrospector,
- _visibilityChecker, _propertyNamingStrategy, _typeFactory,
+ _propertyNamingStrategy, _typeFactory,
_typeResolverBuilder, _dateFormat, _handlerInstantiator, _locale,
_timeZone, base64);
}
@@ -318,10 +296,6 @@
return _annotationIntrospector;
}
- public VisibilityChecker<?> getVisibilityChecker() {
- return _visibilityChecker;
- }
-
public PropertyNamingStrategy getPropertyNamingStrategy() {
return _propertyNamingStrategy;
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/cfg/ConfigOverride.java b/src/main/java/com/fasterxml/jackson/databind/cfg/ConfigOverride.java
index 18e6304..3406235 100644
--- a/src/main/java/com/fasterxml/jackson/databind/cfg/ConfigOverride.java
+++ b/src/main/java/com/fasterxml/jackson/databind/cfg/ConfigOverride.java
@@ -1,8 +1,10 @@
package com.fasterxml.jackson.databind.cfg;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonSetter;
/**
* Configuration object that is accessed by databinding functionality
@@ -21,37 +23,113 @@
protected JsonFormat.Value _format;
/**
- * Definitions of inclusion overrides, if any.
+ * Definitions of inclusion defaults to use for properties included in this POJO type.
+ * Overrides global defaults, may be overridden by per-property-type (see
+ * {@link #_includeAsProperty}) and per-property overrides (annotations).
*/
protected JsonInclude.Value _include;
/**
+ * Definitions of inclusion defaults for properties of this specified type (regardless
+ * of POJO in which they are included).
+ * Overrides global defaults, per-POJO inclusion defaults (see {#link {@link #_include}}),
+ * may be overridden by per-property overrides.
+ *
+ * @since 2.9
+ */
+ protected JsonInclude.Value _includeAsProperty;
+
+ /**
* Definitions of property ignoral (whether to serialize, deserialize
* given logical property) overrides, if any.
*/
protected JsonIgnoreProperties.Value _ignorals;
/**
+ * Definitions of setter overrides regarding null handling
+ *
+ * @since 2.9
+ */
+ protected JsonSetter.Value _setterInfo;
+
+ /**
+ * Overrides for auto-detection visibility rules for this type.
+ *
+ * @since 2.9
+ */
+ protected JsonAutoDetect.Value _visibility;
+
+ /**
* Flag that indicates whether "is ignorable type" is specified for this type;
* and if so, is it to be ignored (true) or not ignored (false); `null` is
* used to indicate "not specified", in which case other configuration (class
* annotation) is used.
*/
protected Boolean _isIgnoredType;
+
+ /**
+ * Flag that indicates whether properties of this type default to being merged
+ * or not.
+ */
+ protected Boolean _mergeable;
protected ConfigOverride() { }
protected ConfigOverride(ConfigOverride src) {
_format = src._format;
_include = src._include;
+ _includeAsProperty = src._includeAsProperty;
_ignorals = src._ignorals;
_isIgnoredType = src._isIgnoredType;
+ _mergeable = src._mergeable;
+ }
+
+ /**
+ * Accessor for immutable "empty" instance that has no configuration overrides defined.
+ *
+ * @since 2.9
+ */
+ public static ConfigOverride empty() {
+ return Empty.INSTANCE;
}
public JsonFormat.Value getFormat() { return _format; }
public JsonInclude.Value getInclude() { return _include; }
+
+ /**
+ * @since 2.9
+ */
+ public JsonInclude.Value getIncludeAsProperty() { return _includeAsProperty; }
+
public JsonIgnoreProperties.Value getIgnorals() { return _ignorals; }
public Boolean getIsIgnoredType() {
return _isIgnoredType;
}
+
+ /**
+ * @since 2.9
+ */
+ public JsonSetter.Value getSetterInfo() { return _setterInfo; }
+
+ /**
+ * @since 2.9
+ */
+ public JsonAutoDetect.Value getVisibility() { return _visibility; }
+
+ /**
+ * @since 2.9
+ */
+ public Boolean getMergeable() { return _mergeable; }
+
+ /**
+ * Implementation used solely for "empty" instance; has no mutators
+ * and is not changed by core functionality.
+ *
+ * @since 2.9
+ */
+ final static class Empty extends ConfigOverride {
+ final static Empty INSTANCE = new Empty();
+
+ private Empty() { }
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/cfg/ConfigOverrides.java b/src/main/java/com/fasterxml/jackson/databind/cfg/ConfigOverrides.java
index 9fd944e..49c622c 100644
--- a/src/main/java/com/fasterxml/jackson/databind/cfg/ConfigOverrides.java
+++ b/src/main/java/com/fasterxml/jackson/databind/cfg/ConfigOverrides.java
@@ -2,6 +2,10 @@
import java.util.*;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonSetter;
+import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
+
/**
* Container for individual {@link ConfigOverride} values.
*
@@ -12,28 +16,82 @@
{
private static final long serialVersionUID = 1L;
+ /**
+ * Per-type override definitions
+ */
protected Map<Class<?>, MutableConfigOverride> _overrides;
+ // // // Global defaulting
+
+ /**
+ * @since 2.9
+ */
+ protected JsonInclude.Value _defaultInclusion;
+
+ /**
+ * @since 2.9
+ */
+ protected JsonSetter.Value _defaultSetterInfo;
+
+ /**
+ * @since 2.9
+ */
+ protected VisibilityChecker<?> _visibilityChecker;
+
+ /**
+ * @since 2.9
+ */
+ protected Boolean _defaultMergeable;
+
+ /*
+ /**********************************************************
+ /* Life cycle
+ /**********************************************************
+ */
+
public ConfigOverrides() {
- _overrides = null;
+ this(null,
+ // !!! TODO: change to (ALWAYS, ALWAYS)?
+ JsonInclude.Value.empty(),
+ JsonSetter.Value.empty(),
+ VisibilityChecker.Std.defaultInstance(),
+ null
+ );
}
- protected ConfigOverrides(Map<Class<?>, MutableConfigOverride> overrides) {
+ protected ConfigOverrides(Map<Class<?>, MutableConfigOverride> overrides,
+ JsonInclude.Value defIncl,
+ JsonSetter.Value defSetter,
+ VisibilityChecker<?> defVisibility,
+ Boolean defMergeable) {
_overrides = overrides;
+ _defaultInclusion = defIncl;
+ _defaultSetterInfo = defSetter;
+ _visibilityChecker = defVisibility;
+ _defaultMergeable = defMergeable;
}
public ConfigOverrides copy()
{
+ Map<Class<?>, MutableConfigOverride> newOverrides;
if (_overrides == null) {
- return new ConfigOverrides();
+ newOverrides = null;
+ } else {
+ newOverrides = _newMap();
+ for (Map.Entry<Class<?>, MutableConfigOverride> entry : _overrides.entrySet()) {
+ newOverrides.put(entry.getKey(), entry.getValue().copy());
+ }
}
- Map<Class<?>, MutableConfigOverride> newOverrides = _newMap();
- for (Map.Entry<Class<?>, MutableConfigOverride> entry : _overrides.entrySet()) {
- newOverrides.put(entry.getKey(), entry.getValue().copy());
- }
- return new ConfigOverrides(newOverrides);
+ return new ConfigOverrides(newOverrides,
+ _defaultInclusion, _defaultSetterInfo, _visibilityChecker, _defaultMergeable);
}
+ /*
+ /**********************************************************
+ /* Per-type override access
+ /**********************************************************
+ */
+
public ConfigOverride findOverride(Class<?> type) {
if (_overrides == null) {
return null;
@@ -53,6 +111,65 @@
return override;
}
+ /*
+ /**********************************************************
+ /* Global defaults access
+ /**********************************************************
+ */
+
+ public JsonInclude.Value getDefaultInclusion() {
+ return _defaultInclusion;
+ }
+
+ public JsonSetter.Value getDefaultSetterInfo() {
+ return _defaultSetterInfo;
+ }
+
+ public Boolean getDefaultMergeable() {
+ return _defaultMergeable;
+ }
+
+ /**
+ * @since 2.9
+ */
+ public VisibilityChecker<?> getDefaultVisibility() {
+ return _visibilityChecker;
+ }
+
+ /**
+ * @since 2.9
+ */
+ public void setDefaultInclusion(JsonInclude.Value v) {
+ _defaultInclusion = v;
+ }
+
+ /**
+ * @since 2.9
+ */
+ public void setDefaultSetterInfo(JsonSetter.Value v) {
+ _defaultSetterInfo = v;
+ }
+
+ /**
+ * @since 2.9
+ */
+ public void setDefaultMergeable(Boolean v) {
+ _defaultMergeable = v;
+ }
+
+ /**
+ * @since 2.9
+ */
+ public void setDefaultVisibility(VisibilityChecker<?> v) {
+ _visibilityChecker = v;
+ }
+
+ /*
+ /**********************************************************
+ /* Helper methods
+ /**********************************************************
+ */
+
protected Map<Class<?>, MutableConfigOverride> _newMap() {
return new HashMap<Class<?>, MutableConfigOverride>();
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/cfg/HandlerInstantiator.java b/src/main/java/com/fasterxml/jackson/databind/cfg/HandlerInstantiator.java
index 2106979..757a379 100644
--- a/src/main/java/com/fasterxml/jackson/databind/cfg/HandlerInstantiator.java
+++ b/src/main/java/com/fasterxml/jackson/databind/cfg/HandlerInstantiator.java
@@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.introspect.Annotated;
+import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter;
@@ -160,4 +161,22 @@
Class<?> implClass) {
return null;
}
+
+ /**
+ * Method called to construct a Filter (any Object with implementation of
+ * <code>equals(Object)</code> that determines if given value is to be
+ * excluded (true) or included (false)) to be used based on
+ * {@link com.fasterxml.jackson.annotation.JsonInclude} annotation (or
+ * equivalent).
+ *<p>
+ * Default implementation returns `null` to indicate that default instantiation
+ * (use zero-arg constructor of the <code>filterClass</code>) should be
+ * used.
+ *
+ * @since 2.9
+ */
+ public Object includeFilterInstance(SerializationConfig config,
+ BeanPropertyDefinition forProperty, Class<?> filterClass) {
+ return null;
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfig.java b/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfig.java
index 1ae5732..12fcaa2 100644
--- a/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfig.java
+++ b/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfig.java
@@ -13,6 +13,7 @@
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.ClassIntrospector;
+import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
import com.fasterxml.jackson.databind.jsontype.SubtypeResolver;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
@@ -36,7 +37,7 @@
implements ClassIntrospector.MixInResolver,
java.io.Serializable
{
- private static final long serialVersionUID = 1L; // since 2.5
+ private static final long serialVersionUID = 2L; // since 2.9
/**
* @since 2.7
@@ -57,7 +58,7 @@
* Immutable container object for simple configuration settings.
*/
protected final BaseSettings _base;
-
+
/*
/**********************************************************
/* Life-cycle: constructors
@@ -232,22 +233,12 @@
* Non-final since it is actually overridden by sub-classes (for now?)
*/
public AnnotationIntrospector getAnnotationIntrospector() {
- return _base.getAnnotationIntrospector();
+ if (isEnabled(MapperFeature.USE_ANNOTATIONS)) {
+ return _base.getAnnotationIntrospector();
+ }
+ return NopAnnotationIntrospector.instance;
}
- /**
- * Accessor for object used for determining whether specific property elements
- * (method, constructors, fields) can be auto-detected based on
- * their visibility (access modifiers). Can be changed to allow
- * different minimum visibility levels for auto-detection. Note
- * that this is the global handler; individual types (classes)
- * can further override active checker used (using
- * {@link JsonAutoDetect} annotation)
- */
- public VisibilityChecker<?> getDefaultVisibilityChecker() {
- return _base.getVisibilityChecker();
- }
-
public final PropertyNamingStrategy getPropertyNamingStrategy() {
return _base.getPropertyNamingStrategy();
}
@@ -255,7 +246,7 @@
public final HandlerInstantiator getHandlerInstantiator() {
return _base.getHandlerInstantiator();
}
-
+
/*
/**********************************************************
/* Configuration: type and subtype handling
@@ -319,12 +310,14 @@
public BeanDescription introspectClassAnnotations(Class<?> cls) {
return introspectClassAnnotations(constructType(cls));
}
-
+
/**
* Accessor for getting bean description that only contains class
* annotations: useful if no getter/setter/creator information is needed.
*/
- public abstract BeanDescription introspectClassAnnotations(JavaType type);
+ public BeanDescription introspectClassAnnotations(JavaType type) {
+ return getClassIntrospector().forClassAnnotations(this, type, this);
+ }
/**
* Accessor for getting bean description that only contains immediate class
@@ -334,12 +327,15 @@
public BeanDescription introspectDirectClassAnnotations(Class<?> cls) {
return introspectDirectClassAnnotations(constructType(cls));
}
+
/**
* Accessor for getting bean description that only contains immediate class
* annotations: ones from the class, and its direct mix-in, if any, but
* not from super types.
*/
- public abstract BeanDescription introspectDirectClassAnnotations(JavaType type);
+ public final BeanDescription introspectDirectClassAnnotations(JavaType type) {
+ return getClassIntrospector().forDirectClassAnnotations(this, type, this);
+ }
/*
/**********************************************************
@@ -348,6 +344,33 @@
*/
/**
+ * Accessor for finding {@link ConfigOverride} to use for
+ * properties of given type, if any exist; or return `null` if not.
+ *<p>
+ * Note that only directly associated override
+ * is found; no type hierarchy traversal is performed.
+ *
+ * @since 2.8
+ *
+ * @return Override object to use for the type, if defined; null if none.
+ */
+ public abstract ConfigOverride findConfigOverride(Class<?> type);
+
+ /**
+ * Accessor for finding {@link ConfigOverride} to use for
+ * properties of given type, if any exist; or if none, return an immutable
+ * "empty" instance with no overrides.
+ *<p>
+ * Note that only directly associated override
+ * is found; no type hierarchy traversal is performed.
+ *
+ * @since 2.9
+ *
+ * @return Override object to use for the type, never null (but may be empty)
+ */
+ public abstract ConfigOverride getConfigOverride(Class<?> type);
+
+ /**
* Accessor for default property inclusion to use for serialization,
* used unless overridden by per-type or per-property overrides.
*
@@ -374,8 +397,52 @@
*
* @since 2.8.2
*/
- public abstract JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType,
- JsonInclude.Value defaultIncl);
+ public JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType,
+ JsonInclude.Value defaultIncl)
+ {
+ JsonInclude.Value v = getConfigOverride(baseType).getInclude();
+ if (v != null) {
+ return v;
+ }
+ return defaultIncl;
+ }
+
+ /**
+ * Accessor for default property inclusion to use for serialization,
+ * considering possible per-type override for given base type and
+ * possible per-type override for given property type.<br>
+ * NOTE: if no override found, defaults to value returned by
+ * {@link #getDefaultPropertyInclusion()}.
+ *
+ * @param baseType Type of the instance containing the targeted property.
+ * @param propertyType Type of the property to look up inclusion setting for.
+ *
+ * @since 2.9
+ */
+ public abstract JsonInclude.Value getDefaultInclusion(Class<?> baseType,
+ Class<?> propertyType);
+
+ /**
+ * Accessor for default property inclusion to use for serialization,
+ * considering possible per-type override for given base type and
+ * possible per-type override for given property type; but
+ * if none found, returning given <code>defaultIncl</code>
+ *
+ * @param baseType Type of the instance containing the targeted property.
+ * @param propertyType Type of the property to look up inclusion setting for.
+ * @param defaultIncl Inclusion setting to return if no overrides found.
+ *
+ * @since 2.9
+ */
+ public JsonInclude.Value getDefaultInclusion(Class<?> baseType,
+ Class<?> propertyType, JsonInclude.Value defaultIncl)
+ {
+ JsonInclude.Value baseOverride = getConfigOverride(baseType).getInclude();
+ JsonInclude.Value propOverride = getConfigOverride(propertyType).getIncludeAsProperty();
+
+ JsonInclude.Value result = JsonInclude.Value.mergeAll(defaultIncl, baseOverride, propOverride);
+ return result;
+ }
/**
* Accessor for default format settings to use for serialization (and, to a degree
@@ -406,15 +473,48 @@
AnnotatedClass actualClass);
/**
- * Accessor for finding possible {@link ConfigOverride} to use for
- * properties of given type. Note that only directly associate override
- * is found; no type hierarchy traversal is performed.
- *
- * @since 2.8
- *
- * @return Override object if there is an override for specified type; `null` if not
+ * Accessor for object used for determining whether specific property elements
+ * (method, constructors, fields) can be auto-detected based on
+ * their visibility (access modifiers). Can be changed to allow
+ * different minimum visibility levels for auto-detection. Note
+ * that this is the global handler; individual types (classes)
+ * can further override active checker used (using
+ * {@link JsonAutoDetect} annotation)
*/
- public abstract ConfigOverride findConfigOverride(Class<?> type);
+ public abstract VisibilityChecker<?> getDefaultVisibilityChecker();
+
+ /**
+ * Accessor for object used for determining whether specific property elements
+ * (method, constructors, fields) can be auto-detected based on
+ * their visibility (access modifiers). This is based on global defaults
+ * (as would be returned by {@link #getDefaultVisibilityChecker()}, but
+ * then modified by possible class annotation (see {@link JsonAutoDetect})
+ * and/or per-type config override (see {@link ConfigOverride#getVisibility()}).
+ *
+ * @since 2.9
+ */
+ public abstract VisibilityChecker<?> getDefaultVisibilityChecker(Class<?> baseType,
+ AnnotatedClass actualClass);
+
+ /**
+ * Accessor for the baseline setter info used as the global baseline,
+ * not considering possible per-type overrides.
+ *
+ * @return Global base settings; never null
+ *
+ * @since 2.9
+ */
+ public abstract JsonSetter.Value getDefaultSetterInfo();
+
+ /**
+ * Accessor for the baseline merge info used as the global baseline,
+ * not considering possible per-type overrides.
+ *
+ * @return Global base settings; never null
+ *
+ * @since 2.9
+ */
+ public abstract Boolean getDefaultMergeable();
/*
/**********************************************************
diff --git a/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfigBase.java b/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfigBase.java
index 6f90f9b..920961b 100644
--- a/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfigBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/cfg/MapperConfigBase.java
@@ -6,6 +6,7 @@
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.core.Base64Variant;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.ClassIntrospector;
import com.fasterxml.jackson.databind.introspect.ClassIntrospector.MixInResolver;
@@ -23,8 +24,24 @@
extends MapperConfig<T>
implements java.io.Serializable
{
+ /**
+ * @since 2.9
+ */
+ protected final static ConfigOverride EMPTY_OVERRIDE = ConfigOverride.empty();
+
private final static int DEFAULT_MAPPER_FEATURES = collectFeatureDefaults(MapperFeature.class);
+ /**
+ * @since 2.9
+ */
+ private final static int AUTO_DETECT_MASK =
+ MapperFeature.AUTO_DETECT_FIELDS.getMask()
+ | MapperFeature.AUTO_DETECT_GETTERS.getMask()
+ | MapperFeature.AUTO_DETECT_IS_GETTERS.getMask()
+ | MapperFeature.AUTO_DETECT_SETTERS.getMask()
+ | MapperFeature.AUTO_DETECT_CREATORS.getMask()
+ ;
+
/*
/**********************************************************
/* Immutable config
@@ -102,8 +119,8 @@
* @since 2.8
*/
protected MapperConfigBase(BaseSettings base,
- SubtypeResolver str, SimpleMixInResolver mixins,
- RootNameLookup rootNames, ConfigOverrides configOverrides)
+ SubtypeResolver str, SimpleMixInResolver mixins, RootNameLookup rootNames,
+ ConfigOverrides configOverrides)
{
super(base, DEFAULT_MAPPER_FEATURES);
_mixIns = mixins;
@@ -117,14 +134,20 @@
}
/**
- * @deprecated Since 2.8, remove from 2.9 or later
+ * @since 2.8
*/
- @Deprecated
- protected MapperConfigBase(BaseSettings base,
- SubtypeResolver str, SimpleMixInResolver mixins,
- RootNameLookup rootNames)
+ protected MapperConfigBase(MapperConfigBase<CFG,T> src,
+ SimpleMixInResolver mixins, RootNameLookup rootNames,
+ ConfigOverrides configOverrides)
{
- this(base, str, mixins, rootNames, null);
+ super(src);
+ _mixIns = mixins;
+ _subtypeResolver = src._subtypeResolver;
+ _rootNames = rootNames;
+ _rootName = src._rootName;
+ _view = src._view;
+ _attributes = src._attributes;
+ _configOverrides = configOverrides;
}
/**
@@ -215,7 +238,7 @@
_attributes = src._attributes;
_configOverrides = src._configOverrides;
}
-
+
/**
* @since 2.3
*/
@@ -231,58 +254,86 @@
_configOverrides = src._configOverrides;
}
+ /*
+ /**********************************************************
+ /* Abstract fluent factory methods to be implemented by subtypes
+ /**********************************************************
+ */
+
/**
- * @since 2.8
+ * @since 2.9 (in this case, demoted from sub-classes)
*/
- protected MapperConfigBase(MapperConfigBase<CFG,T> src, SimpleMixInResolver mixins,
- RootNameLookup rootNames, ConfigOverrides configOverrides)
- {
- super(src);
- _mixIns = mixins;
- _subtypeResolver = src._subtypeResolver;
- _rootNames = rootNames;
- _rootName = src._rootName;
- _view = src._view;
- _attributes = src._attributes;
- _configOverrides = configOverrides;
- }
+ protected abstract T _withBase(BaseSettings newBase);
+
+ /**
+ * @since 2.9 (in this case, demoted from sub-classes)
+ */
+ protected abstract T _withMapperFeatures(int mapperFeatures);
/*
/**********************************************************
- /* Overrides
+ /* Additional shared fluent factory methods; features
/**********************************************************
*/
-
- // note: demoted in 2.8 from sub-classes, as there's no difference
+
+ /**
+ * Fluent factory method that will construct and return a new configuration
+ * object instance with specified features enabled.
+ */
+ @SuppressWarnings("unchecked")
@Override
- public VisibilityChecker<?> getDefaultVisibilityChecker()
+ public final T with(MapperFeature... features)
{
- VisibilityChecker<?> vchecker = super.getDefaultVisibilityChecker();
- // then global overrides (disabling)
- if (!isEnabled(MapperFeature.AUTO_DETECT_SETTERS)) {
- vchecker = vchecker.withSetterVisibility(Visibility.NONE);
+ int newMapperFlags = _mapperFeatures;
+ for (MapperFeature f : features) {
+ newMapperFlags |= f.getMask();
}
- if (!isEnabled(MapperFeature.AUTO_DETECT_CREATORS)) {
- vchecker = vchecker.withCreatorVisibility(Visibility.NONE);
+ if (newMapperFlags == _mapperFeatures) {
+ return (T) this;
}
- if (!isEnabled(MapperFeature.AUTO_DETECT_GETTERS)) {
- vchecker = vchecker.withGetterVisibility(Visibility.NONE);
+ return _withMapperFeatures(newMapperFlags);
+ }
+
+ /**
+ * Fluent factory method that will construct and return a new configuration
+ * object instance with specified features disabled.
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public final T without(MapperFeature... features)
+ {
+ int newMapperFlags = _mapperFeatures;
+ for (MapperFeature f : features) {
+ newMapperFlags &= ~f.getMask();
}
- if (!isEnabled(MapperFeature.AUTO_DETECT_IS_GETTERS)) {
- vchecker = vchecker.withIsGetterVisibility(Visibility.NONE);
+ if (newMapperFlags == _mapperFeatures) {
+ return (T) this;
}
- if (!isEnabled(MapperFeature.AUTO_DETECT_FIELDS)) {
- vchecker = vchecker.withFieldVisibility(Visibility.NONE);
+ return _withMapperFeatures(newMapperFlags);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public final T with(MapperFeature feature, boolean state)
+ {
+ int newMapperFlags;
+ if (state) {
+ newMapperFlags = _mapperFeatures | feature.getMask();
+ } else {
+ newMapperFlags = _mapperFeatures & ~feature.getMask();
}
- return vchecker;
+ if (newMapperFlags == _mapperFeatures) {
+ return (T) this;
+ }
+ return _withMapperFeatures(newMapperFlags);
}
/*
/**********************************************************
- /* Addition fluent factory methods, common to all sub-types
+ /* Additional shared fluent factory methods; introspectors
/**********************************************************
*/
-
+
/**
* Method for constructing and returning a new instance with different
* {@link AnnotationIntrospector} to use (replacing old one).
@@ -290,19 +341,25 @@
* NOTE: make sure to register new instance with <code>ObjectMapper</code>
* if directly calling this method.
*/
- public abstract T with(AnnotationIntrospector ai);
+ public final T with(AnnotationIntrospector ai) {
+ return _withBase(_base.withAnnotationIntrospector(ai));
+ }
/**
* Method for constructing and returning a new instance with additional
* {@link AnnotationIntrospector} appended (as the lowest priority one)
*/
- public abstract T withAppendedAnnotationIntrospector(AnnotationIntrospector introspector);
+ public final T withAppendedAnnotationIntrospector(AnnotationIntrospector ai) {
+ return _withBase(_base.withAppendedAnnotationIntrospector(ai));
+ }
/**
* Method for constructing and returning a new instance with additional
* {@link AnnotationIntrospector} inserted (as the highest priority one)
*/
- public abstract T withInsertedAnnotationIntrospector(AnnotationIntrospector introspector);
+ public final T withInsertedAnnotationIntrospector(AnnotationIntrospector ai) {
+ return _withBase(_base.withInsertedAnnotationIntrospector(ai));
+ }
/**
* Method for constructing and returning a new instance with different
@@ -312,122 +369,15 @@
* NOTE: make sure to register new instance with <code>ObjectMapper</code>
* if directly calling this method.
*/
- public abstract T with(ClassIntrospector ci);
-
- /**
- * Method for constructing and returning a new instance with different
- * {@link DateFormat}
- * to use.
- *<p>
- * NOTE: make sure to register new instance with <code>ObjectMapper</code>
- * if directly calling this method.
- */
- public abstract T with(DateFormat df);
-
- /**
- * Method for constructing and returning a new instance with different
- * {@link HandlerInstantiator}
- * to use.
- *<p>
- * NOTE: make sure to register new instance with <code>ObjectMapper</code>
- * if directly calling this method.
- */
- public abstract T with(HandlerInstantiator hi);
-
- /**
- * Method for constructing and returning a new instance with different
- * {@link PropertyNamingStrategy}
- * to use.
- *<p>
- * NOTE: make sure to register new instance with <code>ObjectMapper</code>
- * if directly calling this method.
- */
- public abstract T with(PropertyNamingStrategy strategy);
-
- /**
- * Method for constructing and returning a new instance with different
- * root name to use (none, if null).
- *<p>
- * Note that when a root name is set to a non-Empty String, this will automatically force use
- * of root element wrapping with given name. If empty String passed, will
- * disable root name wrapping; and if null used, will instead use
- * <code>SerializationFeature</code> to determine if to use wrapping, and annotation
- * (or default name) for actual root name to use.
- *
- * @param rootName to use: if null, means "use default" (clear setting);
- * if empty String ("") means that no root name wrapping is used;
- * otherwise defines root name to use.
- *
- * @since 2.6
- */
- public abstract T withRootName(PropertyName rootName);
-
- public T withRootName(String rootName) {
- if (rootName == null) {
- return withRootName((PropertyName) null);
- }
- return withRootName(PropertyName.construct(rootName));
+ public final T with(ClassIntrospector ci) {
+ return _withBase(_base.withClassIntrospector(ci));
}
-
- /**
- * Method for constructing and returning a new instance with different
- * {@link SubtypeResolver}
- * to use.
- *<p>
- * NOTE: make sure to register new instance with <code>ObjectMapper</code>
- * if directly calling this method.
- */
- public abstract T with(SubtypeResolver str);
-
- /**
- * Method for constructing and returning a new instance with different
- * {@link TypeFactory}
- * to use.
- */
- public abstract T with(TypeFactory typeFactory);
- /**
- * Method for constructing and returning a new instance with different
- * {@link TypeResolverBuilder} to use.
+ /*
+ /**********************************************************
+ /* Additional shared fluent factory methods; attributes
+ /**********************************************************
*/
- public abstract T with(TypeResolverBuilder<?> trb);
-
- /**
- * Method for constructing and returning a new instance with different
- * view to use.
- */
- public abstract T withView(Class<?> view);
-
- /**
- * Method for constructing and returning a new instance with different
- * {@link VisibilityChecker}
- * to use.
- */
- public abstract T with(VisibilityChecker<?> vc);
-
- /**
- * Method for constructing and returning a new instance with different
- * minimal visibility level for specified property type
- */
- public abstract T withVisibility(PropertyAccessor forMethod, JsonAutoDetect.Visibility visibility);
-
- /**
- * Method for constructing and returning a new instance with different
- * default {@link java.util.Locale} to use for formatting.
- */
- public abstract T with(Locale l);
-
- /**
- * Method for constructing and returning a new instance with different
- * default {@link java.util.TimeZone} to use for formatting of date values.
- */
- public abstract T with(TimeZone tz);
-
- /**
- * Method for constructing and returning a new instance with different
- * default {@link Base64Variant} to use with base64-encoded binary values.
- */
- public abstract T with(Base64Variant base64);
/**
* Method for constructing an instance that has specified
@@ -466,13 +416,142 @@
public T withoutAttribute(Object key) {
return with(getAttributes().withoutSharedAttribute(key));
}
+
+ /*
+ /**********************************************************
+ /* Additional shared fluent factory methods; factories
+ /**********************************************************
+ */
+
+ /**
+ * Method for constructing and returning a new instance with different
+ * {@link TypeFactory}
+ * to use.
+ */
+ public final T with(TypeFactory tf) {
+ return _withBase( _base.withTypeFactory(tf));
+ }
+
+ /**
+ * Method for constructing and returning a new instance with different
+ * {@link TypeResolverBuilder} to use.
+ */
+ public final T with(TypeResolverBuilder<?> trb) {
+ return _withBase(_base.withTypeResolverBuilder(trb));
+ }
+
+ /**
+ * Method for constructing and returning a new instance with different
+ * {@link PropertyNamingStrategy}
+ * to use.
+ *<p>
+ * NOTE: make sure to register new instance with <code>ObjectMapper</code>
+ * if directly calling this method.
+ */
+ public final T with(PropertyNamingStrategy pns) {
+ return _withBase(_base.withPropertyNamingStrategy(pns));
+ }
+
+ /**
+ * Method for constructing and returning a new instance with different
+ * {@link HandlerInstantiator}
+ * to use.
+ *<p>
+ * NOTE: make sure to register new instance with <code>ObjectMapper</code>
+ * if directly calling this method.
+ */
+ public final T with(HandlerInstantiator hi) {
+ return _withBase(_base.withHandlerInstantiator(hi));
+ }
+
+ /*
+ /**********************************************************
+ /* Additional shared fluent factory methods; other
+ /**********************************************************
+ */
+
+ /**
+ * Method for constructing and returning a new instance with different
+ * default {@link Base64Variant} to use with base64-encoded binary values.
+ */
+ public final T with(Base64Variant base64) {
+ return _withBase(_base.with(base64));
+ }
+
+ /**
+ * Method for constructing and returning a new instance with different
+ * {@link DateFormat}
+ * to use.
+ *<p>
+ * NOTE: non-final since <code>SerializationConfig</code> needs to override this
+ */
+ public T with(DateFormat df) {
+ return _withBase(_base.withDateFormat(df));
+ }
+
+ /**
+ * Method for constructing and returning a new instance with different
+ * default {@link java.util.Locale} to use for formatting.
+ */
+ public final T with(Locale l) {
+ return _withBase(_base.with(l));
+ }
+
+ /**
+ * Method for constructing and returning a new instance with different
+ * default {@link java.util.TimeZone} to use for formatting of date values.
+ */
+ public final T with(TimeZone tz) {
+ return _withBase(_base.with(tz));
+ }
+
+ /**
+ * Method for constructing and returning a new instance with different
+ * root name to use (none, if null).
+ *<p>
+ * Note that when a root name is set to a non-Empty String, this will automatically force use
+ * of root element wrapping with given name. If empty String passed, will
+ * disable root name wrapping; and if null used, will instead use
+ * <code>SerializationFeature</code> to determine if to use wrapping, and annotation
+ * (or default name) for actual root name to use.
+ *
+ * @param rootName to use: if null, means "use default" (clear setting);
+ * if empty String ("") means that no root name wrapping is used;
+ * otherwise defines root name to use.
+ *
+ * @since 2.6
+ */
+ public abstract T withRootName(PropertyName rootName);
+
+ public T withRootName(String rootName) {
+ if (rootName == null) {
+ return withRootName((PropertyName) null);
+ }
+ return withRootName(PropertyName.construct(rootName));
+ }
+ /**
+ * Method for constructing and returning a new instance with different
+ * {@link SubtypeResolver}
+ * to use.
+ *<p>
+ * NOTE: make sure to register new instance with <code>ObjectMapper</code>
+ * if directly calling this method.
+ */
+ public abstract T with(SubtypeResolver str);
+
+ /**
+ * Method for constructing and returning a new instance with different
+ * view to use.
+ */
+ public abstract T withView(Class<?> view);
+
/*
/**********************************************************
/* Simple accessors
/**********************************************************
*/
-
+
/**
* Accessor for object used for finding out all reachable subtypes
* for supertypes; needed when a logical type name is used instead
@@ -510,16 +589,48 @@
/*
/**********************************************************
- /* Property config override access
+ /* Configuration access; default/overrides
/**********************************************************
*/
-
+
+ @Override
+ public final ConfigOverride getConfigOverride(Class<?> type) {
+ ConfigOverride override = _configOverrides.findOverride(type);
+ return (override == null) ? EMPTY_OVERRIDE : override;
+ }
+
@Override
public final ConfigOverride findConfigOverride(Class<?> type) {
return _configOverrides.findOverride(type);
}
@Override
+ public final JsonInclude.Value getDefaultPropertyInclusion() {
+ return _configOverrides.getDefaultInclusion();
+ }
+
+ @Override
+ public final JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType) {
+ JsonInclude.Value v = getConfigOverride(baseType).getInclude();
+ JsonInclude.Value def = getDefaultPropertyInclusion();
+ if (def == null) {
+ return v;
+ }
+ return def.withOverrides(v);
+ }
+
+ @Override
+ public final JsonInclude.Value getDefaultInclusion(Class<?> baseType,
+ Class<?> propertyType) {
+ JsonInclude.Value v = getConfigOverride(propertyType).getIncludeAsProperty();
+ JsonInclude.Value def = getDefaultPropertyInclusion(baseType);
+ if (def == null) {
+ return v;
+ }
+ return def.withOverrides(v);
+ }
+
+ @Override
public final JsonFormat.Value getDefaultPropertyFormat(Class<?> type) {
ConfigOverride overrides = _configOverrides.findOverride(type);
if (overrides != null) {
@@ -556,6 +667,56 @@
return JsonIgnoreProperties.Value.merge(base, overrides);
}
+ @Override
+ public final VisibilityChecker<?> getDefaultVisibilityChecker()
+ {
+ VisibilityChecker<?> vchecker = _configOverrides.getDefaultVisibility();
+ // then global overrides (disabling)
+ if ((_mapperFeatures & AUTO_DETECT_MASK) != 0) {
+ if (!isEnabled(MapperFeature.AUTO_DETECT_FIELDS)) {
+ vchecker = vchecker.withFieldVisibility(Visibility.NONE);
+ }
+ if (!isEnabled(MapperFeature.AUTO_DETECT_GETTERS)) {
+ vchecker = vchecker.withGetterVisibility(Visibility.NONE);
+ }
+ if (!isEnabled(MapperFeature.AUTO_DETECT_IS_GETTERS)) {
+ vchecker = vchecker.withIsGetterVisibility(Visibility.NONE);
+ }
+ if (!isEnabled(MapperFeature.AUTO_DETECT_SETTERS)) {
+ vchecker = vchecker.withSetterVisibility(Visibility.NONE);
+ }
+ if (!isEnabled(MapperFeature.AUTO_DETECT_CREATORS)) {
+ vchecker = vchecker.withCreatorVisibility(Visibility.NONE);
+ }
+ }
+ return vchecker;
+ }
+
+ @Override // since 2.9
+ public final VisibilityChecker<?> getDefaultVisibilityChecker(Class<?> baseType,
+ AnnotatedClass actualClass) {
+ VisibilityChecker<?> vc = getDefaultVisibilityChecker();
+ AnnotationIntrospector intr = getAnnotationIntrospector();
+ if (intr != null) {
+ vc = intr.findAutoDetectVisibility(actualClass, vc);
+ }
+ ConfigOverride overrides = _configOverrides.findOverride(baseType);
+ if (overrides != null) {
+ vc = vc.withOverrides(overrides.getVisibility()); // ok to pass null
+ }
+ return vc;
+ }
+
+ @Override
+ public final JsonSetter.Value getDefaultSetterInfo() {
+ return _configOverrides.getDefaultSetterInfo();
+ }
+
+ @Override
+ public Boolean getDefaultMergeable() {
+ return _configOverrides.getDefaultMergeable();
+ }
+
/*
/**********************************************************
/* Other config access
diff --git a/src/main/java/com/fasterxml/jackson/databind/cfg/MutableConfigOverride.java b/src/main/java/com/fasterxml/jackson/databind/cfg/MutableConfigOverride.java
index 902e18b..b30b99b 100644
--- a/src/main/java/com/fasterxml/jackson/databind/cfg/MutableConfigOverride.java
+++ b/src/main/java/com/fasterxml/jackson/databind/cfg/MutableConfigOverride.java
@@ -1,8 +1,10 @@
package com.fasterxml.jackson.databind.cfg;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonSetter;
/**
* Extension of {@link ConfigOverride} that allows changing of
@@ -24,8 +26,8 @@
protected MutableConfigOverride(MutableConfigOverride src) {
super(src);
}
-
- protected MutableConfigOverride copy() {
+
+ public MutableConfigOverride copy() {
return new MutableConfigOverride(this);
}
@@ -33,19 +35,62 @@
_format = v;
return this;
}
-
+
+ /**
+ * Override inclusion setting for all properties contained in POJOs of the
+ * associated type.
+ *
+ * @param v Inclusion setting to apply contained properties.
+ */
public MutableConfigOverride setInclude(JsonInclude.Value v) {
_include = v;
return this;
}
+ /**
+ * Override inclusion setting for properties of the associated type
+ * regardless of the type of the POJO containing it.
+ *
+ * @param v Inclusion setting to apply for properties of associated type.
+ *
+ * @since 2.9
+ */
+ public MutableConfigOverride setIncludeAsProperty(JsonInclude.Value v) {
+ _includeAsProperty = v;
+ return this;
+ }
+
public MutableConfigOverride setIgnorals(JsonIgnoreProperties.Value v) {
_ignorals = v;
return this;
}
-
+
public MutableConfigOverride setIsIgnoredType(Boolean v) {
_isIgnoredType = v;
return this;
}
+
+ /**
+ * @since 2.9
+ */
+ public MutableConfigOverride setSetterInfo(JsonSetter.Value v) {
+ _setterInfo = v;
+ return this;
+ }
+
+ /**
+ * @since 2.9
+ */
+ public MutableConfigOverride setVisibility(JsonAutoDetect.Value v) {
+ _visibility = v;
+ return this;
+ }
+
+ /**
+ * @since 2.9
+ */
+ public MutableConfigOverride setMergeable(Boolean v) {
+ _mergeable = v;
+ return this;
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/AbstractDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/AbstractDeserializer.java
index 8515376..2ccf2f0 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/AbstractDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/AbstractDeserializer.java
@@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.impl.ObjectIdReader;
+import com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator;
import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.ObjectIdInfo;
@@ -35,6 +36,8 @@
protected final Map<String, SettableBeanProperty> _backRefProperties;
+ protected transient Map<String,SettableBeanProperty> _properties;
+
// support for "native" types, which require special care:
protected final boolean _acceptString;
@@ -48,12 +51,21 @@
/**********************************************************
*/
+ /**
+ * @since 2.9
+ *
+ * @param props Regular properties: currently only needed to support property-annotated
+ * Object Id handling with property inclusion (needed for determining type of Object Id
+ * to bind)
+ */
public AbstractDeserializer(BeanDeserializerBuilder builder,
- BeanDescription beanDesc, Map<String, SettableBeanProperty> backRefProps)
+ BeanDescription beanDesc, Map<String, SettableBeanProperty> backRefProps,
+ Map<String, SettableBeanProperty> props)
{
_baseType = beanDesc.getType();
_objectIdReader = builder.getObjectIdReader();
_backRefProperties = backRefProps;
+ _properties = props;
Class<?> cls = _baseType.getRawClass();
_acceptString = cls.isAssignableFrom(String.class);
_acceptBoolean = (cls == Boolean.TYPE) || cls.isAssignableFrom(Boolean.class);
@@ -61,6 +73,12 @@
_acceptDouble = (cls == Double.TYPE) || cls.isAssignableFrom(Double.class);
}
+ @Deprecated // since 2.9
+ public AbstractDeserializer(BeanDeserializerBuilder builder,
+ BeanDescription beanDesc, Map<String, SettableBeanProperty> backRefProps) {
+ this(builder, beanDesc, backRefProps, null);
+ }
+
protected AbstractDeserializer(BeanDescription beanDesc)
{
_baseType = beanDesc.getType();
@@ -77,7 +95,7 @@
* @since 2.9
*/
protected AbstractDeserializer(AbstractDeserializer base,
- ObjectIdReader objectIdReader)
+ ObjectIdReader objectIdReader, Map<String, SettableBeanProperty> props)
{
_baseType = base._baseType;
_backRefProperties = base._backRefProperties;
@@ -87,8 +105,9 @@
_acceptDouble = base._acceptDouble;
_objectIdReader = objectIdReader;
+ _properties = props;
}
-
+
/**
* Factory method used when constructing instances for non-POJO types, like
* {@link java.util.Map}s.
@@ -103,37 +122,55 @@
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
- // First: may have an override for Object Id:
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
- final AnnotatedMember accessor = (property == null || intr == null)
- ? null : property.getMember();
- if (accessor != null && intr != null) {
- ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(accessor);
- if (objectIdInfo != null) { // some code duplication here as well (from BeanDeserializerFactory)
- // 2.1: allow modifications by "id ref" annotations as well:
- objectIdInfo = intr.findObjectReferenceInfo(accessor, objectIdInfo);
-
- Class<?> implClass = objectIdInfo.getGeneratorType();
- // 02-May-2017, tatu: Alas, properties are NOT available for abstract classes; can not
- // support this particular type
- if (implClass == ObjectIdGenerators.PropertyGenerator.class) {
- ctxt.reportMappingException(
+ if (property != null && intr != null) {
+ final AnnotatedMember accessor = property.getMember();
+ if (accessor != null) {
+ ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(accessor);
+ if (objectIdInfo != null) { // some code duplication here as well (from BeanDeserializerFactory)
+ JavaType idType;
+ ObjectIdGenerator<?> idGen;
+ SettableBeanProperty idProp = null;
+ ObjectIdResolver resolver = ctxt.objectIdResolverInstance(accessor, objectIdInfo);
+
+ // 2.1: allow modifications by "id ref" annotations as well:
+ objectIdInfo = intr.findObjectReferenceInfo(accessor, objectIdInfo);
+ Class<?> implClass = objectIdInfo.getGeneratorType();
+
+ if (implClass == ObjectIdGenerators.PropertyGenerator.class) {
+ PropertyName propName = objectIdInfo.getPropertyName();
+ idProp = (_properties == null) ? null : _properties.get(propName.getSimpleName());
+ if (idProp == null) {
+ ctxt.reportBadDefinition(_baseType, String.format(
+ "Invalid Object Id definition for %s: can not find property with name '%s'",
+ handledType().getName(), propName));
+ }
+ idType = idProp.getType();
+ idGen = new PropertyBasedObjectIdGenerator(objectIdInfo.getScope());
+/*
+ ctxt.reportBadDefinition(_baseType, String.format(
+/
"Invalid Object Id definition for abstract type %s: can not use `PropertyGenerator` on polymorphic types using property annotation",
-handledType().getName());
+handledType().getName()));
+*/
+ } else { // other types simpler
+ resolver = ctxt.objectIdResolverInstance(accessor, objectIdInfo);
+ JavaType type = ctxt.constructType(implClass);
+ idType = ctxt.getTypeFactory().findTypeParameters(type, ObjectIdGenerator.class)[0];
+ idGen = ctxt.objectIdGeneratorInstance(accessor, objectIdInfo);
+ }
+ JsonDeserializer<?> deser = ctxt.findRootValueDeserializer(idType);
+ ObjectIdReader oir = ObjectIdReader.construct(idType, objectIdInfo.getPropertyName(),
+ idGen, deser, idProp, resolver);
+ return new AbstractDeserializer(this, oir, null);
}
- ObjectIdResolver resolver = ctxt.objectIdResolverInstance(accessor, objectIdInfo);
- JavaType type = ctxt.constructType(implClass);
- JavaType idType = ctxt.getTypeFactory().findTypeParameters(type, ObjectIdGenerator.class)[0];
- SettableBeanProperty idProp = null;
- ObjectIdGenerator<?> idGen = ctxt.objectIdGeneratorInstance(accessor, objectIdInfo);
- JsonDeserializer<?> deser = ctxt.findRootValueDeserializer(idType);
- ObjectIdReader oir = ObjectIdReader.construct(idType, objectIdInfo.getPropertyName(),
- idGen, deser, idProp, resolver);
- return new AbstractDeserializer(this, oir);
}
}
- // either way, need to resolve serializer:
- return this;
+ if (_properties == null) {
+ return this;
+ }
+ // Need to ensure properties are dropped at this point, regardless
+ return new AbstractDeserializer(this, _objectIdReader, null);
}
/*
@@ -149,7 +186,17 @@
@Override
public boolean isCachable() { return true; }
-
+
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ /* 23-Oct-2016, tatu: Not exactly sure what to do with this; polymorphic
+ * type handling seems bit risky so for now claim it "may or may not be"
+ * possible, which does allow explicit per-type/per-property merging attempts,
+ * but avoids general-configuration merges
+ */
+ return null;
+ }
+
/**
* Overridden to return true for those instances that are
* handling value for which Object Identity handling is enabled
@@ -197,10 +244,8 @@
&& _objectIdReader.isValidReferencePropertyName(p.getCurrentName(), p)) {
return _deserializeFromObjectId(p, ctxt);
}
-
}
}
-
// First: support "natural" values (which are always serialized without type info!)
Object result = _deserializeIfNatural(p, ctxt);
if (result != null) {
@@ -213,7 +258,11 @@
public Object deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
- return ctxt.handleMissingInstantiator(_baseType.getRawClass(), p,
+ // 16-Oct-2016, tatu: Let's pass non-null value instantiator so that we will
+ // get proper exception type; needed to establish there are no creators
+ // (since without ValueInstantiator this would not be known for certain)
+ ValueInstantiator bogus = new ValueInstantiator.Base(_baseType);
+ return ctxt.handleMissingInstantiator(_baseType.getRawClass(), bogus, p,
"abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information");
}
@@ -222,7 +271,7 @@
/* Internal methods
/**********************************************************
*/
-
+
protected Object _deserializeIfNatural(JsonParser p, DeserializationContext ctxt) throws IOException
{
/* There is a chance we might be "natural" types
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java b/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java
index d5b3639..e5e34ec 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java
@@ -1,10 +1,10 @@
package com.fasterxml.jackson.databind.deser;
-import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
+import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonLocation;
@@ -238,29 +238,28 @@
final DeserializationConfig config = ctxt.getConfig();
ValueInstantiator instantiator = null;
- // [JACKSON-633] Check @JsonValueInstantiator before anything else
+ // Check @JsonValueInstantiator before anything else
AnnotatedClass ac = beanDesc.getClassInfo();
Object instDef = ctxt.getAnnotationIntrospector().findValueInstantiator(ac);
if (instDef != null) {
instantiator = _valueInstantiatorInstance(config, ac, instDef);
}
if (instantiator == null) {
- /* Second: see if some of standard Jackson/JDK types might provide value
- * instantiators.
- */
+ // Second: see if some of standard Jackson/JDK types might provide value
+ // instantiators.
instantiator = _findStdValueInstantiator(config, beanDesc);
if (instantiator == null) {
instantiator = _constructDefaultValueInstantiator(ctxt, beanDesc);
}
}
-
+
// finally: anyone want to modify ValueInstantiator?
if (_factoryConfig.hasValueInstantiators()) {
for (ValueInstantiators insts : _factoryConfig.valueInstantiators()) {
instantiator = insts.findValueInstantiator(config, beanDesc, instantiator);
// let's do sanity check; easier to spot buggy handlers
if (instantiator == null) {
- ctxt.reportMappingException(
+ ctxt.reportBadTypeDefinition(beanDesc,
"Broken registered ValueInstantiators (of type %s): returned null ValueInstantiator",
insts.getClass().getName());
}
@@ -300,8 +299,8 @@
// need to construct suitable visibility checker:
final DeserializationConfig config = ctxt.getConfig();
- VisibilityChecker<?> vchecker = config.getDefaultVisibilityChecker();
- vchecker = intr.findAutoDetectVisibility(beanDesc.getClassInfo(), vchecker);
+ VisibilityChecker<?> vchecker = config.getDefaultVisibilityChecker(beanDesc.getBeanClass(),
+ beanDesc.getClassInfo());
/* 24-Sep-2014, tatu: Tricky part first; need to merge resolved property information
* (which has creator parameters sprinkled around) with actual creator
@@ -401,7 +400,7 @@
// in list of constructors, so needs to be handled separately.
AnnotatedConstructor defaultCtor = beanDesc.findDefaultConstructor();
if (defaultCtor != null) {
- if (!creators.hasDefaultCreator() || intr.hasCreatorAnnotation(defaultCtor)) {
+ if (!creators.hasDefaultCreator() || _hasCreatorAnnotation(ctxt, defaultCtor)) {
creators.setDefaultCreator(defaultCtor);
}
}
@@ -418,21 +417,22 @@
// may need to keep track for [#725]
List<AnnotatedConstructor> implicitCtors = null;
for (AnnotatedConstructor ctor : beanDesc.getConstructors()) {
- final boolean isCreator = intr.hasCreatorAnnotation(ctor);
+ JsonCreator.Mode creatorMode = intr.findCreatorAnnotation(ctxt.getConfig(), ctor);
+ final boolean isCreator = (creatorMode != null) && (creatorMode != JsonCreator.Mode.DISABLED);
BeanPropertyDefinition[] propDefs = creatorParams.get(ctor);
final int argCount = ctor.getParameterCount();
// some single-arg factory methods (String, number) are auto-detected
if (argCount == 1) {
BeanPropertyDefinition argDef = (propDefs == null) ? null : propDefs[0];
- boolean useProps = _checkIfCreatorPropertyBased(intr, ctor, argDef);
+ boolean useProps = _checkIfCreatorPropertyBased(intr, ctor, argDef, creatorMode);
if (useProps) {
SettableBeanProperty[] properties = new SettableBeanProperty[1];
PropertyName name = (argDef == null) ? null : argDef.getFullName();
AnnotatedParameter arg = ctor.getParameter(0);
properties[0] = constructCreatorProperty(ctxt, beanDesc, name, 0, arg,
- intr.findInjectableValueId(arg));
+ intr.findInjectableValue(arg));
creators.addPropertyCreator(ctor, isCreator, properties);
} else {
/*boolean added = */ _handleSingleArgumentConstructor(ctxt, beanDesc, vchecker, intr, creators,
@@ -461,7 +461,7 @@
for (int i = 0; i < argCount; ++i) {
final AnnotatedParameter param = ctor.getParameter(i);
BeanPropertyDefinition propDef = (propDefs == null) ? null : propDefs[i];
- Object injectId = intr.findInjectableValueId(param);
+ JacksonInject.Value injectId = intr.findInjectableValue(param);
final PropertyName name = (propDef == null) ? null : propDef.getFullName();
if (propDef != null && propDef.isExplicitlyNamed()) {
@@ -476,8 +476,11 @@
}
NameTransformer unwrapper = intr.findUnwrappingNameTransformer(param);
if (unwrapper != null) {
+ _reportUnwrappedCreatorProperty(ctxt, beanDesc, param);
+ /*
properties[i] = constructCreatorProperty(ctxt, beanDesc, UNWRAPPED_CREATOR_PARAM_NAME, i, param, null);
++explicitNameCount;
+ */
continue;
}
// One more thing: implicit names are ok iff ctor has creator annotation
@@ -593,36 +596,6 @@
}
}
- protected boolean _checkIfCreatorPropertyBased(AnnotationIntrospector intr,
- AnnotatedWithParams creator, BeanPropertyDefinition propDef)
- {
- JsonCreator.Mode mode = intr.findCreatorBinding(creator);
-
- if (mode == JsonCreator.Mode.PROPERTIES) {
- return true;
- }
- if (mode == JsonCreator.Mode.DELEGATING) {
- return false;
- }
- // If explicit name, or inject id, property-based
- if (((propDef != null) && propDef.isExplicitlyNamed())
- || (intr.findInjectableValueId(creator.getParameter(0)) != null)) {
- return true;
- }
- if (propDef != null) {
- // One more thing: if implicit name matches property with a getter
- // or field, we'll consider it property-based as well
- String implName = propDef.getName();
- if (implName != null && !implName.isEmpty()) {
- if (propDef.couldSerialize()) {
- return true;
- }
- }
- }
- // in absence of everything else, default to delegating
- return false;
- }
-
protected boolean _handleSingleArgumentConstructor(DeserializationContext ctxt,
BeanDescription beanDesc, VisibilityChecker<?> vchecker,
AnnotationIntrospector intr, CreatorCollector creators,
@@ -677,7 +650,8 @@
{
final DeserializationConfig config = ctxt.getConfig();
for (AnnotatedMethod factory : beanDesc.getFactoryMethods()) {
- final boolean isCreator = intr.hasCreatorAnnotation(factory);
+ JsonCreator.Mode creatorMode = intr.findCreatorAnnotation(ctxt.getConfig(), factory);
+ final boolean isCreator = (creatorMode != null) && (creatorMode != JsonCreator.Mode.DISABLED);
final int argCount = factory.getParameterCount();
// zero-arg methods must be annotated; if so, are "default creators" [JACKSON-850]
if (argCount == 0) {
@@ -691,7 +665,7 @@
// some single-arg factory methods (String, number) are auto-detected
if (argCount == 1) {
BeanPropertyDefinition argDef = (propDefs == null) ? null : propDefs[0];
- boolean useProps = _checkIfCreatorPropertyBased(intr, factory, argDef);
+ boolean useProps = _checkIfCreatorPropertyBased(intr, factory, argDef, creatorMode);
if (!useProps) { // not property based but delegating
/*boolean added=*/ _handleSingleArgumentFactory(config, beanDesc, vchecker, intr, creators,
factory, isCreator);
@@ -719,30 +693,33 @@
for (int i = 0; i < argCount; ++i) {
final AnnotatedParameter param = factory.getParameter(i);
BeanPropertyDefinition propDef = (propDefs == null) ? null : propDefs[i];
- Object injectId = intr.findInjectableValueId(param);
+ JacksonInject.Value injectable = intr.findInjectableValue(param);
final PropertyName name = (propDef == null) ? null : propDef.getFullName();
if (propDef != null && propDef.isExplicitlyNamed()) {
++explicitNameCount;
- properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectId);
+ properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectable);
continue;
}
- if (injectId != null) {
+ if (injectable != null) {
++injectCount;
- properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectId);
+ properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectable);
continue;
}
NameTransformer unwrapper = intr.findUnwrappingNameTransformer(param);
if (unwrapper != null) {
+ _reportUnwrappedCreatorProperty(ctxt, beanDesc, param);
+ /*
properties[i] = constructCreatorProperty(ctxt, beanDesc, UNWRAPPED_CREATOR_PARAM_NAME, i, param, null);
++implicitNameCount;
+ */
continue;
}
// One more thing: implicit names are ok iff ctor has creator annotation
if (isCreator) {
if (name != null && !name.isEmpty()) {
++implicitNameCount;
- properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectId);
+ properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectable);
continue;
}
}
@@ -825,6 +802,17 @@
return false;
}
+ // 01-Dec-2016, tatu: As per [databind#265] we can not yet support passing
+ // of unwrapped values through creator properties, so fail fast
+ protected void _reportUnwrappedCreatorProperty(DeserializationContext ctxt,
+ BeanDescription beanDesc, AnnotatedParameter param)
+ throws JsonMappingException
+ {
+ ctxt.reportBadDefinition(beanDesc.getType(), String.format(
+ "Can not define Creator parameter %d as `@JsonUnwrapped`: combination not yet supported",
+ param.getIndex()));
+ }
+
/**
* Method that will construct a property object that represents
* a logical property passed via Creator (constructor or static
@@ -833,7 +821,7 @@
protected SettableBeanProperty constructCreatorProperty(DeserializationContext ctxt,
BeanDescription beanDesc, PropertyName name, int index,
AnnotatedParameter param,
- Object injectableValueId)
+ JacksonInject.Value injectable)
throws JsonMappingException
{
final DeserializationConfig config = ctxt.getConfig();
@@ -852,8 +840,7 @@
}
JavaType type = resolveMemberAndTypeAnnotations(ctxt, param, param.getType());
BeanProperty.Std property = new BeanProperty.Std(name, type,
- intr.findWrapperName(param),
- beanDesc.getClassAnnotations(), param, metadata);
+ intr.findWrapperName(param), param, metadata);
// Type deserializer: either comes from property (and already resolved)
TypeDeserializer typeDeser = (TypeDeserializer) type.getTypeHandler();
// or if not, based on type being referenced:
@@ -862,6 +849,9 @@
}
// Note: contextualization of typeDeser _should_ occur in constructor of CreatorProperty
// so it is not called directly here
+
+ Object injectableValueId = (injectable == null) ? null : injectable.getId();
+
SettableBeanProperty prop = new CreatorProperty(name, type, property.getWrapperName(),
typeDeser, beanDesc.getClassAnnotations(), param, index, injectableValueId,
metadata);
@@ -904,22 +894,32 @@
return null;
}
- @Deprecated // in 2.6, remove from 2.7
- protected PropertyName _findExplicitParamName(AnnotatedParameter param, AnnotationIntrospector intr)
+ protected boolean _checkIfCreatorPropertyBased(AnnotationIntrospector intr,
+ AnnotatedWithParams creator, BeanPropertyDefinition propDef,
+ JsonCreator.Mode creatorMode)
{
- if (param != null && intr != null) {
- return intr.findNameForDeserialization(param);
+ if (creatorMode == JsonCreator.Mode.PROPERTIES) {
+ return true;
}
- return null;
- }
-
- @Deprecated // in 2.6, remove from 2.7
- protected boolean _hasExplicitParamName(AnnotatedParameter param, AnnotationIntrospector intr)
- {
- if (param != null && intr != null) {
- PropertyName n = intr.findNameForDeserialization(param);
- return (n != null) && n.hasSimpleName();
+ if (creatorMode == JsonCreator.Mode.DELEGATING) {
+ return false;
}
+ // If explicit name, or inject id, property-based
+ if (((propDef != null) && propDef.isExplicitlyNamed())
+ || (intr.findInjectableValue(creator.getParameter(0)) != null)) {
+ return true;
+ }
+ if (propDef != null) {
+ // One more thing: if implicit name matches property with a getter
+ // or field, we'll consider it property-based as well
+ String implName = propDef.getName();
+ if (implName != null && !implName.isEmpty()) {
+ if (propDef.couldSerialize()) {
+ return true;
+ }
+ }
+ }
+ // in absence of everything else, default to delegating
return false;
}
@@ -1129,11 +1129,21 @@
// Value handling is identical for all, but EnumMap requires special handling for keys
Class<?> mapClass = type.getRawClass();
if (EnumMap.class.isAssignableFrom(mapClass)) {
+ ValueInstantiator inst;
+
+ // 06-Mar-2017, tatu: Should only need to check ValueInstantiator for
+ // custom sub-classes, see [databind#1544]
+ if (mapClass == EnumMap.class) {
+ inst = null;
+ } else {
+ inst = findValueInstantiator(ctxt, beanDesc);
+ }
Class<?> kt = keyType.getRawClass();
if (kt == null || !kt.isEnum()) {
throw new IllegalArgumentException("Can not construct EnumMap; generic (key) type not available");
}
- deser = new EnumMapDeserializer(type, null, contentDeser, contentTypeDeser);
+ deser = new EnumMapDeserializer(type, inst, null,
+ contentDeser, contentTypeDeser, null);
}
// Otherwise, generic handler works ok.
@@ -1253,7 +1263,7 @@
: valueInstantiator.getFromObjectArguments(ctxt.getConfig());
// May have @JsonCreator for static factory method:
for (AnnotatedMethod factory : beanDesc.getFactoryMethods()) {
- if (ctxt.getAnnotationIntrospector().hasCreatorAnnotation(factory)) {
+ if (_hasCreatorAnnotation(ctxt, factory)) {
if (factory.getParameterCount() == 0) { // [databind#960]
deser = EnumDeserializer.deserializerForNoArgsCreator(config, enumClass, factory);
break;
@@ -1270,7 +1280,8 @@
// Need to consider @JsonValue if one found
if (deser == null) {
deser = new EnumDeserializer(constructEnumResolver(enumClass,
- config, beanDesc.findJsonValueMethod()));
+ config, beanDesc.findJsonValueAccessor()),
+ config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS));
}
}
@@ -1318,8 +1329,19 @@
if (deser == null) {
// Just one referential type as of JDK 1.7 / Java 7: AtomicReference (Java 8 adds Optional)
- if (AtomicReference.class.isAssignableFrom(type.getRawClass())) {
- return new AtomicReferenceDeserializer(type, contentTypeDeser, contentDeser);
+ if (type.isTypeOrSubTypeOf(AtomicReference.class)) {
+ Class<?> rawType = type.getRawClass();
+ ValueInstantiator inst;
+ if (rawType == AtomicReference.class) {
+ inst = null;
+ } else {
+ /* 23-Oct-2016, tatu: Note that subtypes are probably not supportable
+ * without either forcing merging (to avoid having to create instance)
+ * or something else...
+ */
+ inst = findValueInstantiator(ctxt, beanDesc);
+ }
+ return new AtomicReferenceDeserializer(type, inst, contentTypeDeser, contentDeser);
}
}
if (deser != null) {
@@ -1448,11 +1470,10 @@
return StdKeyDeserializers.constructDelegatingKeyDeserializer(config, type, valueDesForKey);
}
}
- EnumResolver enumRes = constructEnumResolver(enumClass, config, beanDesc.findJsonValueMethod());
+ EnumResolver enumRes = constructEnumResolver(enumClass, config, beanDesc.findJsonValueAccessor());
// May have @JsonCreator for static factory method:
- final AnnotationIntrospector ai = config.getAnnotationIntrospector();
for (AnnotatedMethod factory : beanDesc.getFactoryMethods()) {
- if (ai.hasCreatorAnnotation(factory)) {
+ if (_hasCreatorAnnotation(ctxt, factory)) {
int argCount = factory.getParameterCount();
if (argCount == 1) {
Class<?> returnType = factory.getRawReturnType();
@@ -1580,14 +1601,8 @@
}
if (rawType == CLASS_MAP_ENTRY) {
// 28-Apr-2015, tatu: TypeFactory does it all for us already so
- JavaType kt = type.containedType(0);
- if (kt == null) {
- kt = TypeFactory.unknownType();
- }
- JavaType vt = type.containedType(1);
- if (vt == null) {
- vt = TypeFactory.unknownType();
- }
+ JavaType kt = type.containedTypeOrUnknown(0);
+ JavaType vt = type.containedTypeOrUnknown(1);
TypeDeserializer vts = (TypeDeserializer) vt.getTypeHandler();
if (vts == null) {
vts = findTypeDeserializer(ctxt.getConfig(), vt);
@@ -1809,6 +1824,23 @@
}
/**
+ * @since 2.9
+ */
+ protected JsonDeserializer<Object> findContentDeserializerFromAnnotation(DeserializationContext ctxt,
+ Annotated ann)
+ throws JsonMappingException
+ {
+ AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
+ if (intr != null) {
+ Object deserDef = intr.findContentDeserializer(ann);
+ if (deserDef != null) {
+ return ctxt.deserializerInstance(ann, deserDef);
+ }
+ }
+ return null;
+ }
+
+ /**
* Helper method used to resolve additional type-related annotation information
* like type overrides, or handler (serializer, deserializer) overrides,
* so that from declared field, property or constructor parameter type
@@ -1869,20 +1901,34 @@
}
protected EnumResolver constructEnumResolver(Class<?> enumClass,
- DeserializationConfig config, AnnotatedMethod jsonValueMethod)
+ DeserializationConfig config, AnnotatedMember jsonValueAccessor)
{
- if (jsonValueMethod != null) {
- Method accessor = jsonValueMethod.getAnnotated();
+ if (jsonValueAccessor != null) {
if (config.canOverrideAccessModifiers()) {
- ClassUtil.checkAndFixAccess(accessor, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
+ ClassUtil.checkAndFixAccess(jsonValueAccessor.getMember(),
+ config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
}
- return EnumResolver.constructUnsafeUsingMethod(enumClass, accessor, config.getAnnotationIntrospector());
+ return EnumResolver.constructUnsafeUsingMethod(enumClass,
+ jsonValueAccessor, config.getAnnotationIntrospector());
}
// 14-Mar-2016, tatu: We used to check `DeserializationFeature.READ_ENUMS_USING_TO_STRING`
// here, but that won't do: it must be dynamically changeable...
return EnumResolver.constructUnsafe(enumClass, config.getAnnotationIntrospector());
}
+ /**
+ * @since 2.9
+ */
+ protected boolean _hasCreatorAnnotation(DeserializationContext ctxt,
+ Annotated ann) {
+ AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
+ if (intr != null) {
+ JsonCreator.Mode mode = intr.findCreatorAnnotation(ctxt.getConfig(), ann);
+ return (mode != null) && (mode != JsonCreator.Mode.DISABLED);
+ }
+ return false;
+ }
+
/*
/**********************************************************
/* Deprecated helper methods
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializer.java
index 76603d5..7c5e736 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializer.java
@@ -20,7 +20,7 @@
{
/* TODOs for future versions:
*
- * For 2.8?
+ * For 2.9?
*
* - New method in JsonDeserializer (deserializeNext()) to allow use of more
* efficient 'nextXxx()' method `JsonParser` provides.
@@ -35,10 +35,18 @@
* Lazily constructed exception used as root cause if reporting problem
* with creator method that returns <code>null</code> (which is not allowed)
*
- * @since 3.8
+ * @since 2.8
*/
protected transient Exception _nullFromCreator;
-
+
+ /**
+ * State marker we need in order to avoid infinite recursion for some cases
+ * (not very clean, alas, but has to do for now)
+ *
+ * @since 2.9
+ */
+ private volatile transient NameTransformer _currentlyTransforming;
+
/*
/**********************************************************
/* Life-cycle, construction, initialization
@@ -86,19 +94,22 @@
}
@Override
- public JsonDeserializer<Object> unwrappingDeserializer(NameTransformer unwrapper)
+ public JsonDeserializer<Object> unwrappingDeserializer(NameTransformer transformer)
{
- /* bit kludgy but we don't want to accidentally change type; sub-classes
- * MUST override this method to support unwrapped properties...
- */
+ // bit kludgy but we don't want to accidentally change type; sub-classes
+ // MUST override this method to support unwrapped properties...
if (getClass() != BeanDeserializer.class) {
return this;
}
- /* main thing really is to just enforce ignoring of unknown
- * properties; since there may be multiple unwrapped values
- * and properties for all may be interleaved...
- */
- return new BeanDeserializer(this, unwrapper);
+ // 25-Mar-2017, tatu: Not clean at all, but for [databind#383] we do need
+ // to keep track of accidental recursion...
+ if (_currentlyTransforming == transformer) {
+ return this;
+ }
+ _currentlyTransforming = transformer;
+ try {
+ return new BeanDeserializer(this, transformer);
+ } finally { _currentlyTransforming = null; }
}
@Override
@@ -291,7 +302,7 @@
@Override
public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) throws IOException
{
- /* 09-Dec-2014, tatu: As per [#622], we need to allow Object Id references
+ /* 09-Dec-2014, tatu: As per [databind#622], we need to allow Object Id references
* to come in as JSON Objects as well; but for now assume they will
* be simple, single-property references, which means that we can
* recognize them without having to buffer anything.
@@ -381,8 +392,8 @@
{
final PropertyBasedCreator creator = _propertyBasedCreator;
PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, _objectIdReader);
-
TokenBuffer unknown = null;
+ final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
JsonToken t = p.getCurrentToken();
List<BeanReferring> referrings = null;
@@ -397,8 +408,13 @@
SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
if (creatorProp != null) {
// Last creator property to set?
- if (buffer.assignParameter(creatorProp,
- _deserializeWithErrorWrapping(p, ctxt, creatorProp))) {
+ Object value;
+ if ((activeView != null) && !creatorProp.visibleInView(activeView)) {
+ p.skipChildren();
+ continue;
+ }
+ value = _deserializeWithErrorWrapping(p, ctxt, creatorProp);
+ if (buffer.assignParameter(creatorProp, value)) {
p.nextToken(); // to move to following FIELD_NAME/END_OBJECT
Object bean;
try {
@@ -616,7 +632,7 @@
p.nextToken();
SettableBeanProperty prop = _beanProperties.find(propName);
if (prop != null) { // normal case
- if (activeView != null && !prop.visibleInView(activeView)) {
+ if ((activeView != null) && !prop.visibleInView(activeView)) {
p.skipChildren();
continue;
}
@@ -640,21 +656,17 @@
// but... others should be passed to unwrapped property deserializers
tokens.writeFieldName(propName);
tokens.copyCurrentStructure(p);
- } else {
- // Need to copy to a separate buffer first
- TokenBuffer b2 = new TokenBuffer(p, ctxt);
- b2.copyCurrentStructure(p);
- tokens.writeFieldName(propName);
- tokens.append(b2);
- try {
- JsonParser p2 = b2.asParser(p);
- p2.nextToken();
- _anySetter.deserializeAndSet(p2, ctxt, bean, propName);
- } catch (Exception e) {
- wrapAndThrow(e, bean, propName, ctxt);
- }
continue;
}
+ // Need to copy to a separate buffer first
+ TokenBuffer b2 = TokenBuffer.asCopyOfValue(p);
+ tokens.writeFieldName(propName);
+ tokens.append(b2);
+ try {
+ _anySetter.deserializeAndSet(b2.asParserOnFirstToken(), ctxt, bean, propName);
+ } catch (Exception e) {
+ wrapAndThrow(e, bean, propName, ctxt);
+ }
}
tokens.writeEndObject();
_unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
@@ -662,7 +674,8 @@
}
@SuppressWarnings("resource")
- protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext ctxt, Object bean)
+ protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext ctxt,
+ Object bean)
throws IOException
{
JsonToken t = p.getCurrentToken();
@@ -702,14 +715,11 @@
tokens.copyCurrentStructure(p);
} else {
// Need to copy to a separate buffer first
- TokenBuffer b2 = new TokenBuffer(p, ctxt);
- b2.copyCurrentStructure(p);
+ TokenBuffer b2 = TokenBuffer.asCopyOfValue(p);
tokens.writeFieldName(propName);
tokens.append(b2);
try {
- JsonParser p2 = b2.asParser(p);
- p2.nextToken();
- _anySetter.deserializeAndSet(p2, ctxt, bean, propName);
+ _anySetter.deserializeAndSet(b2.asParserOnFirstToken(), ctxt, bean, propName);
} catch (Exception e) {
wrapAndThrow(e, bean, propName, ctxt);
}
@@ -725,6 +735,10 @@
protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p, DeserializationContext ctxt)
throws IOException
{
+ // 01-Dec-2016, tatu: Note: This IS legal to call, but only when unwrapped
+ // value itself is NOT passed via `CreatorProperty` (which isn't supported).
+ // Ok however to pass via setter or field.
+
final PropertyBasedCreator creator = _propertyBasedCreator;
PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, _objectIdReader);
@@ -739,7 +753,8 @@
SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
if (creatorProp != null) {
// Last creator property to set?
- if (buffer.assignParameter(creatorProp, _deserializeWithErrorWrapping(p, ctxt, creatorProp))) {
+ if (buffer.assignParameter(creatorProp,
+ _deserializeWithErrorWrapping(p, ctxt, creatorProp))) {
t = p.nextToken(); // to move to following FIELD_NAME/END_OBJECT
Object bean;
try {
@@ -759,8 +774,8 @@
if (bean.getClass() != _beanType.getRawClass()) {
// !!! 08-Jul-2011, tatu: Could probably support; but for now
// it's too complicated, so bail out
- tokens.close();
- ctxt.reportMappingException("Can not create polymorphic instances with unwrapped values");
+ ctxt.reportInputMismatch(creatorProp,
+ "Can not create polymorphic instances with unwrapped values");
return null;
}
return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
@@ -792,15 +807,12 @@
tokens.copyCurrentStructure(p);
} else {
// Need to copy to a separate buffer first
- TokenBuffer b2 = new TokenBuffer(p, ctxt);
- b2.copyCurrentStructure(p);
+ TokenBuffer b2 = TokenBuffer.asCopyOfValue(p);
tokens.writeFieldName(propName);
tokens.append(b2);
try {
- JsonParser p2 = b2.asParser(p);
- p2.nextToken();
buffer.bufferAnyProperty(_anySetter, propName,
- _anySetter.deserialize(p2, ctxt));
+ _anySetter.deserialize(b2.asParserOnFirstToken(), ctxt));
} catch (Exception e) {
wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
}
@@ -940,8 +952,9 @@
if (bean.getClass() != _beanType.getRawClass()) {
// !!! 08-Jul-2011, tatu: Could theoretically support; but for now
// it's too complicated, so bail out
- ctxt.reportMappingException("Can not create polymorphic instances with external type ids");
- return null;
+ return ctxt.reportBadDefinition(_beanType, String.format(
+ "Can not create polymorphic instances with external type ids (%s -> %s)",
+ _beanType, bean.getClass()));
}
return ext.complete(p, ctxt, bean);
}
@@ -969,7 +982,8 @@
}
// "any property"?
if (_anySetter != null) {
- buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
+ buffer.bufferAnyProperty(_anySetter, propName,
+ _anySetter.deserialize(p, ctxt));
}
}
@@ -1019,7 +1033,7 @@
public void handleResolvedForwardReference(Object id, Object value) throws IOException
{
if (_bean == null) {
- _context.reportMappingException(
+ _context.reportInputMismatch(_prop,
"Can not resolve ObjectId forward reference using property '%s' (of type %s): Bean not yet resolved",
_prop.getName(), _prop.getDeclaringClass().getName());
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java b/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java
index 8fba8b2..262acec 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java
@@ -26,6 +26,7 @@
public abstract class BeanDeserializerBase
extends StdDeserializer<Object>
implements ContextualDeserializer, ResolvableDeserializer,
+ ValueInstantiator.Gettable, // since 2.9
java.io.Serializable // since 2.1
{
private static final long serialVersionUID = 1;
@@ -39,16 +40,6 @@
*/
/**
- * Annotations from the bean class: used for accessing
- * annotations during resolution
- * (see {@link #resolve}) and
- * contextualization (see {@link #createContextual})
- *<p>
- * Transient since annotations only used during construction.
- */
- final private transient Annotations _classAnnotations;
-
- /**
* Declared type of the bean this deserializer handles.
*/
final protected JavaType _beanType;
@@ -57,7 +48,7 @@
* Requested shape from bean class annotations.
*/
final protected JsonFormat.Shape _serializationShape;
-
+
/*
/**********************************************************
/* Configuration for creating value instance
@@ -150,13 +141,13 @@
* on active view used (if any)
*/
final protected boolean _needViewProcesing;
-
+
/**
* We may also have one or more back reference fields (usually
* zero or one).
*/
final protected Map<String, SettableBeanProperty> _backRefs;
-
+
/*
/**********************************************************
/* Related handlers
@@ -208,9 +199,6 @@
boolean hasViews)
{
super(beanDesc.getType());
-
- AnnotatedClass ac = beanDesc.getClassInfo();
- _classAnnotations = ac.getAnnotations();
_beanType = beanDesc.getType();
_valueInstantiator = builder.getValueInstantiator();
@@ -252,7 +240,6 @@
{
super(src._beanType);
- _classAnnotations = src._classAnnotations;
_beanType = src._beanType;
_valueInstantiator = src._valueInstantiator;
@@ -279,7 +266,6 @@
{
super(src._beanType);
- _classAnnotations = src._classAnnotations;
_beanType = src._beanType;
_valueInstantiator = src._valueInstantiator;
@@ -317,8 +303,6 @@
public BeanDeserializerBase(BeanDeserializerBase src, ObjectIdReader oir)
{
super(src._beanType);
-
- _classAnnotations = src._classAnnotations;
_beanType = src._beanType;
_valueInstantiator = src._valueInstantiator;
@@ -356,19 +340,18 @@
public BeanDeserializerBase(BeanDeserializerBase src, Set<String> ignorableProps)
{
super(src._beanType);
- _classAnnotations = src._classAnnotations;
_beanType = src._beanType;
_valueInstantiator = src._valueInstantiator;
_delegateDeserializer = src._delegateDeserializer;
_propertyBasedCreator = src._propertyBasedCreator;
-
+
_backRefs = src._backRefs;
_ignorableProps = ignorableProps;
_ignoreAllUnknown = src._ignoreAllUnknown;
_anySetter = src._anySetter;
_injectables = src._injectables;
-
+
_nonStandardCreation = src._nonStandardCreation;
_unwrappedPropertyHandler = src._unwrappedPropertyHandler;
_needViewProcesing = src._needViewProcesing;
@@ -388,14 +371,12 @@
protected BeanDeserializerBase(BeanDeserializerBase src, BeanPropertyMap beanProps)
{
super(src._beanType);
-
- _classAnnotations = src._classAnnotations;
_beanType = src._beanType;
-
+
_valueInstantiator = src._valueInstantiator;
_delegateDeserializer = src._delegateDeserializer;
_propertyBasedCreator = src._propertyBasedCreator;
-
+
_beanProperties = beanProps;
_backRefs = src._backRefs;
_ignorableProps = src._ignorableProps;
@@ -497,19 +478,30 @@
prop = _resolvedObjectIdProperty(ctxt, prop);
}
// Support unwrapped values (via @JsonUnwrapped)
- SettableBeanProperty u = _resolveUnwrappedProperty(ctxt, prop);
- if (u != null) {
- prop = u;
- if (unwrapped == null) {
- unwrapped = new UnwrappedPropertyHandler();
+ NameTransformer xform = _findPropertyUnwrapper(ctxt, prop);
+ if (xform != null) {
+ JsonDeserializer<Object> orig = prop.getValueDeserializer();
+ JsonDeserializer<Object> unwrapping = orig.unwrappingDeserializer(xform);
+ if (unwrapping != orig && unwrapping != null) {
+ prop = prop.withValueDeserializer(unwrapping);
+ if (unwrapped == null) {
+ unwrapped = new UnwrappedPropertyHandler();
+ }
+ unwrapped.addProperty(prop);
+ // 12-Dec-2014, tatu: As per [databind#647], we will have problems if
+ // the original property is left in place. So let's remove it now.
+ // 25-Mar-2017, tatu: Wonder if this could be problematic wrt creators?
+ // (that is, should be remove it from creator too)
+ _beanProperties.remove(prop);
+ continue;
}
- unwrapped.addProperty(prop);
- // 12-Dec-2014, tatu: As per [databind#647], we will have problems if
- // the original property is left in place. So let's remove it now.
- // 25-Mar-2017, tatu: Wonder if this could be problematic wrt creators?
- _beanProperties.remove(prop);
- continue;
}
+
+ // 26-Oct-2016, tatu: Need to have access to value deserializer to know if
+ // merging needed, and now seems to be reasonable time to do that.
+ final PropertyMetadata md = prop.getMetadata();
+ prop = _resolveMergeAndNullSettings(ctxt, prop, md);
+
// non-static inner classes too:
prop = _resolveInnerClassValuedProperty(ctxt, prop);
if (prop != origProp) {
@@ -522,7 +514,7 @@
TypeDeserializer typeDeser = prop.getValueTypeDeserializer();
if (typeDeser.getTypeInclusion() == JsonTypeInfo.As.EXTERNAL_PROPERTY) {
if (extTypes == null) {
- extTypes = new ExternalTypeHandler.Builder();
+ extTypes = ExternalTypeHandler.builder(_beanType);
}
extTypes.addExternal(prop, typeDeser);
// In fact, remove from list of known properties to simplify later handling
@@ -540,9 +532,9 @@
if (_valueInstantiator.canCreateUsingDelegate()) {
JavaType delegateType = _valueInstantiator.getDelegateType(ctxt.getConfig());
if (delegateType == null) {
- throw new IllegalArgumentException("Invalid delegate-creator definition for "+_beanType
- +": value instantiator ("+_valueInstantiator.getClass().getName()
- +") returned true for 'canCreateUsingDelegate()', but null for 'getDelegateType()'");
+ ctxt.reportBadDefinition(_beanType, String.format(
+"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingDelegate()', but null for 'getDelegateType()'",
+ _beanType, _valueInstantiator.getClass().getName()));
}
_delegateDeserializer = _findDelegateDeserializer(ctxt, delegateType,
_valueInstantiator.getDelegateCreator());
@@ -552,9 +544,9 @@
if (_valueInstantiator.canCreateUsingArrayDelegate()) {
JavaType delegateType = _valueInstantiator.getArrayDelegateType(ctxt.getConfig());
if (delegateType == null) {
- throw new IllegalArgumentException("Invalid array-delegate-creator definition for "+_beanType
- +": value instantiator ("+_valueInstantiator.getClass().getName()
- +") returned true for 'canCreateUsingArrayDelegate()', but null for 'getArrayDelegateType()'");
+ ctxt.reportBadDefinition(_beanType, String.format(
+"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingArrayDelegate()', but null for 'getArrayDelegateType()'",
+ _beanType, _valueInstantiator.getClass().getName()));
}
_arrayDelegateDeserializer = _findDelegateDeserializer(ctxt, delegateType,
_valueInstantiator.getArrayDelegateCreator());
@@ -562,7 +554,8 @@
// And now that we know CreatorProperty instances are also resolved can finally create the creator:
if (creatorProps != null) {
- _propertyBasedCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator, creatorProps);
+ _propertyBasedCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator,
+ creatorProps, _beanProperties);
}
if (extTypes != null) {
@@ -613,7 +606,7 @@
AnnotatedWithParams delegateCreator) throws JsonMappingException {
// Need to create a temporary property to allow contextual deserializers:
BeanProperty.Std property = new BeanProperty.Std(TEMP_PROPERTY_NAME,
- delegateType, null, _classAnnotations, delegateCreator,
+ delegateType, null, delegateCreator,
PropertyMetadata.STD_OPTIONAL);
TypeDeserializer td = delegateType.getTypeHandler();
@@ -673,9 +666,8 @@
// First: may have an override for Object Id:
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
- final AnnotatedMember accessor = (property == null || intr == null)
- ? null : property.getMember();
- if (accessor != null && intr != null) {
+ final AnnotatedMember accessor = _neitherNull(property, intr) ? property.getMember() : null;
+ if (accessor != null) {
ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(accessor);
if (objectIdInfo != null) { // some code duplication here as well (from BeanDeserializerFactory)
// 2.1: allow modifications by "id ref" annotations as well:
@@ -691,12 +683,13 @@
PropertyName propName = objectIdInfo.getPropertyName();
idProp = findProperty(propName);
if (idProp == null) {
- throw new IllegalArgumentException("Invalid Object Id definition for "
- +handledType().getName()+": can not find property with name '"+propName+"'");
+ ctxt.reportBadDefinition(_beanType, String.format(
+ "Invalid Object Id definition for %s: can not find property with name '%s'",
+ handledType().getName(), propName));
}
idType = idProp.getType();
idGen = new PropertyBasedObjectIdGenerator(objectIdInfo.getScope());
- } else { // other types need to be simpler
+ } else { // other types are to be simpler
JavaType type = ctxt.constructType(implClass);
idType = ctxt.getTypeFactory().findTypeParameters(type, ObjectIdGenerator.class)[0];
idProp = null;
@@ -738,7 +731,6 @@
// 16-May-2016, tatu: How about per-property case-insensitivity?
Boolean B = format.getFeature(JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
if (B != null) {
- // !!! TODO
BeanPropertyMap propsOrig = _beanProperties;
BeanPropertyMap props = propsOrig.withCaseInsensitivity(B.booleanValue());
if (props != propsOrig) {
@@ -762,6 +754,7 @@
*/
protected SettableBeanProperty _resolveManagedReferenceProperty(DeserializationContext ctxt,
SettableBeanProperty prop)
+ throws JsonMappingException
{
String refName = prop.getManagedReferenceName();
if (refName == null) {
@@ -770,20 +763,21 @@
JsonDeserializer<?> valueDeser = prop.getValueDeserializer();
SettableBeanProperty backProp = valueDeser.findBackReference(refName);
if (backProp == null) {
- throw new IllegalArgumentException("Can not handle managed/back reference '"+refName+"': no back reference property found from type "
- +prop.getType());
+ ctxt.reportBadDefinition(_beanType, String.format(
+"Can not handle managed/back reference '%s': no back reference property found from type %s",
+ refName, prop.getType()));
}
// also: verify that type is compatible
JavaType referredType = _beanType;
JavaType backRefType = backProp.getType();
boolean isContainer = prop.getType().isContainerType();
if (!backRefType.getRawClass().isAssignableFrom(referredType.getRawClass())) {
- throw new IllegalArgumentException("Can not handle managed/back reference '"+refName+"': back reference type ("
- +backRefType.getRawClass().getName()+") not compatible with managed type ("
- +referredType.getRawClass().getName()+")");
+ ctxt.reportBadDefinition(_beanType, String.format(
+"Can not handle managed/back reference '%s': back reference type (%s) not compatible with managed type (%s)",
+ refName, backRefType.getRawClass().getName(),
+ referredType.getRawClass().getName()));
}
- return new ManagedReferenceProperty(prop, refName, backProp,
- _classAnnotations, isContainer);
+ return new ManagedReferenceProperty(prop, refName, backProp, isContainer);
}
/**
@@ -806,24 +800,27 @@
* Helper method called to see if given property might be so-called unwrapped
* property: these require special handling.
*/
- protected SettableBeanProperty _resolveUnwrappedProperty(DeserializationContext ctxt,
+ protected NameTransformer _findPropertyUnwrapper(DeserializationContext ctxt,
SettableBeanProperty prop)
+ throws JsonMappingException
{
AnnotatedMember am = prop.getMember();
if (am != null) {
NameTransformer unwrapper = ctxt.getAnnotationIntrospector().findUnwrappingNameTransformer(am);
if (unwrapper != null) {
- JsonDeserializer<Object> orig = prop.getValueDeserializer();
- JsonDeserializer<Object> unwrapping = orig.unwrappingDeserializer(unwrapper);
- if (unwrapping != orig && unwrapping != null) {
- // might be cleaner to create new instance; but difficult to do reliably, so:
- return prop.withValueDeserializer(unwrapping);
+ // 01-Dec-2016, tatu: As per [databind#265] we can not yet support passing
+ // of unwrapped values through creator properties, so fail fast
+ if (prop instanceof CreatorProperty) {
+ ctxt.reportBadDefinition(getValueType(), String.format(
+ "Can not define Creator property \"%s\" as `@JsonUnwrapped`: combination not yet supported",
+ prop.getName()));
}
+ return unwrapper;
}
}
return null;
}
-
+
/**
* Helper method that will handle gruesome details of dealing with properties
* that have non-static inner class as value...
@@ -862,20 +859,96 @@
return prop;
}
+ // @since 2.9
+ protected SettableBeanProperty _resolveMergeAndNullSettings(DeserializationContext ctxt,
+ SettableBeanProperty prop, PropertyMetadata propMetadata)
+ throws JsonMappingException
+ {
+ PropertyMetadata.MergeInfo merge = propMetadata.getMergeInfo();
+ // First mergeability
+ if (merge != null) {
+ JsonDeserializer<?> valueDeser = prop.getValueDeserializer();
+ Boolean mayMerge = valueDeser.supportsUpdate(ctxt.getConfig());
+
+ if (mayMerge == null) {
+ // we don't really know if it's ok; so only use if explicitly specified
+ if (merge.fromDefaults) {
+ return prop;
+ }
+ } else if (!mayMerge.booleanValue()) { // prevented
+ if (!merge.fromDefaults) {
+ // If attempts was made via explicit annotation/per-type config override,
+ // should be reported; may or may not result in exception
+ ctxt.reportBadMerge(valueDeser);
+ }
+ return prop;
+ }
+ // Anyway; if we get this far, do enable merging
+ AnnotatedMember accessor = merge.getter;
+ accessor.fixAccess(ctxt.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
+ if (!(prop instanceof SetterlessProperty)) {
+ prop = MergingSettableBeanProperty.construct(prop, accessor);
+ }
+ }
+
+ // And after this, see if we require non-standard null handling
+ NullValueProvider nuller = findValueNullProvider(ctxt, prop, propMetadata);
+ if (nuller != null) {
+ prop = prop.withNullProvider(nuller);
+ }
+ return prop;
+ }
+
/*
/**********************************************************
- /* Public accessors
+ /* Public accessors; null/empty value providers
+ /**********************************************************
+ */
+
+ @Override
+ public AccessPattern getNullAccessPattern() {
+ // POJO types do not have custom `null` values
+ return AccessPattern.ALWAYS_NULL;
+ }
+
+ @Override
+ public AccessPattern getEmptyAccessPattern() {
+ // Empty values can not be shared
+ return AccessPattern.DYNAMIC;
+ }
+
+ @Override // since 2.9
+ public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
+ // alas, need to promote exception, if any:
+ try {
+ return _valueInstantiator.createUsingDefault(ctxt);
+ } catch (IOException e) {
+ return ClassUtil.throwAsMappingException(ctxt, e);
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Public accessors; other
/**********************************************************
*/
@Override
public boolean isCachable() { return true; }
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ // although with possible caveats, yes, values can be updated
+ // 23-Oct-2016, tatu: Perhaps in future could and should verify from
+ // bean settings...
+ return Boolean.TRUE;
+ }
+
@Override
public Class<?> handledType() {
return _beanType.getRawClass();
}
-
+
/**
* Overridden to return true for those instances that are
* handling value for which Object Identity handling is enabled
@@ -966,7 +1039,7 @@
{
SettableBeanProperty prop = (_beanProperties == null) ?
null : _beanProperties.find(propertyName);
- if (prop == null && _propertyBasedCreator != null) {
+ if (_neitherNull(prop, _propertyBasedCreator)) {
prop = _propertyBasedCreator.findCreatorProperty(propertyName);
}
return prop;
@@ -986,7 +1059,7 @@
{
SettableBeanProperty prop = (_beanProperties == null) ?
null : _beanProperties.find(propertyIndex);
- if (prop == null && _propertyBasedCreator != null) {
+ if (_neitherNull(prop, _propertyBasedCreator)) {
prop = _propertyBasedCreator.findCreatorProperty(propertyIndex);
}
return prop;
@@ -1005,6 +1078,7 @@
return _backRefs.get(logicalName);
}
+ @Override // ValueInstantiator.Gettable
public ValueInstantiator getValueInstantiator() {
return _valueInstantiator;
}
@@ -1190,28 +1264,21 @@
if (_propertyBasedCreator != null) {
return _deserializeUsingPropertyBased(p, ctxt);
}
- // should only occur for abstract types...
- if (_beanType.isAbstract()) {
- return ctxt.handleMissingInstantiator(handledType(), p,
- "abstract type (need to add/enable type information?)");
- }
// 25-Jan-2017, tatu: We do not actually support use of Creators for non-static
// inner classes -- with one and only one exception; that of default constructor!
// -- so let's indicate it
Class<?> raw = _beanType.getRawClass();
if (ClassUtil.isNonStaticInnerClass(raw)) {
- return ctxt.handleMissingInstantiator(raw, p,
+ return ctxt.handleMissingInstantiator(raw, null, p,
"can only instantiate non-static inner class by using default, no-argument constructor");
}
- return ctxt.handleMissingInstantiator(raw, p,
-"no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)");
+ return ctxt.handleMissingInstantiator(raw, getValueInstantiator(), p,
+ "can not deserialize from Object value (no delegate- or property-based Creator)");
}
protected abstract Object _deserializeUsingPropertyBased(final JsonParser p,
- final DeserializationContext ctxt)
- throws IOException, JsonProcessingException;
+ final DeserializationContext ctxt) throws IOException;
- @SuppressWarnings("incomplete-switch")
public Object deserializeFromNumber(JsonParser p, DeserializationContext ctxt)
throws IOException
{
@@ -1220,8 +1287,8 @@
return deserializeFromObjectId(p, ctxt);
}
final JsonDeserializer<Object> delegateDeser = _delegateDeserializer();
- switch (p.getNumberType()) {
- case INT:
+ NumberType nt = p.getNumberType();
+ if (nt == NumberType.INT) {
if (delegateDeser != null) {
if (!_valueInstantiator.canCreateFromInt()) {
Object bean = _valueInstantiator.createUsingDelegate(ctxt,
@@ -1233,7 +1300,8 @@
}
}
return _valueInstantiator.createFromInt(ctxt, p.getIntValue());
- case LONG:
+ }
+ if (nt == NumberType.LONG) {
if (delegateDeser != null) {
if (!_valueInstantiator.canCreateFromInt()) {
Object bean = _valueInstantiator.createUsingDelegate(ctxt,
@@ -1255,12 +1323,13 @@
}
return bean;
}
- return ctxt.handleMissingInstantiator(handledType(), p,
+ return ctxt.handleMissingInstantiator(handledType(), getValueInstantiator(), p,
"no suitable creator method found to deserialize from Number value (%s)",
p.getNumberValue());
}
- public Object deserializeFromString(JsonParser p, DeserializationContext ctxt) throws IOException
+ public Object deserializeFromString(JsonParser p, DeserializationContext ctxt)
+ throws IOException
{
// First things first: id Object Id is used, most likely that's it
if (_objectIdReader != null) {
@@ -1311,7 +1380,7 @@
return _valueInstantiator.createUsingDelegate(ctxt,
delegateDeser.deserialize(p, ctxt));
}
- return ctxt.handleMissingInstantiator(handledType(), p,
+ return ctxt.handleMissingInstantiator(handledType(), getValueInstantiator(), p,
"no suitable creator method found to deserialize from Number value (%s)",
p.getNumberValue());
}
@@ -1409,7 +1478,7 @@
injector.inject(ctxt, bean);
}
}
-
+
/**
* Method called to handle set of one or more unknown properties,
* stored in their entirety in given {@link TokenBuffer}
@@ -1493,7 +1562,7 @@
}
p.skipChildren();
}
-
+
/**
* Method called in cases where we may have polymorphic deserialization
* case: that is, type of Creator-constructed bean is not the type
@@ -1536,7 +1605,7 @@
}
return bean;
}
-
+
/**
* Helper method called to (try to) locate deserializer for given sub-type of
* type that this deserializer handles.
@@ -1574,7 +1643,7 @@
}
return subDeser;
}
-
+
/*
/**********************************************************
/* Helper methods for error reporting
@@ -1596,16 +1665,10 @@
public void wrapAndThrow(Throwable t, Object bean, String fieldName, DeserializationContext ctxt)
throws IOException
{
- // [JACKSON-55] Need to add reference information
+ // Need to add reference information
throw JsonMappingException.wrapWithPath(throwOrReturnThrowable(t, ctxt), bean, fieldName);
}
- @Deprecated // since 2.4, not used by core Jackson; only relevant for arrays/Collections
- public void wrapAndThrow(Throwable t, Object bean, int index, DeserializationContext ctxt) throws IOException {
- // [JACKSON-55] Need to add reference information
- throw JsonMappingException.wrapWithPath(throwOrReturnThrowable(t, ctxt), bean, index);
- }
-
private Throwable throwOrReturnThrowable(Throwable t, DeserializationContext ctxt)
throws IOException
{
@@ -1617,9 +1680,7 @@
t = t.getCause();
}
// Errors to be passed as is
- if (t instanceof Error) {
- throw (Error) t;
- }
+ ClassUtil.throwIfError(t);
boolean wrap = (ctxt == null) || ctxt.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS);
// Ditto for IOExceptions; except we may want to wrap JSON exceptions
if (t instanceof IOException) {
@@ -1627,9 +1688,7 @@
throw (IOException) t;
}
} else if (!wrap) { // [JACKSON-407] -- allow disabling wrapping for unchecked exceptions
- if (t instanceof RuntimeException) {
- throw (RuntimeException) t;
- }
+ ClassUtil.throwIfRTE(t);
}
return t;
}
@@ -1641,17 +1700,14 @@
t = t.getCause();
}
// Errors and "plain" IOExceptions to be passed as is
- if (t instanceof Error) {
- throw (Error) t;
- }
- boolean wrap = (ctxt == null) || ctxt.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS);
+ ClassUtil.throwIfError(t);
if (t instanceof IOException) {
// Since we have no more information to add, let's not actually wrap..
throw (IOException) t;
- } else if (!wrap) { // [JACKSON-407] -- allow disabling wrapping for unchecked exceptions
- if (t instanceof RuntimeException) {
- throw (RuntimeException) t;
- }
+ }
+ boolean wrap = (ctxt == null) || ctxt.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS);
+ if (!wrap) { // [JACKSON-407] -- allow disabling wrapping for unchecked exceptions
+ ClassUtil.throwIfRTE(t);
}
return ctxt.handleInstantiationProblem(_beanType.getRawClass(), null, t);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBuilder.java b/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBuilder.java
index 5ca54d5..92cc835 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBuilder.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBuilder.java
@@ -26,6 +26,11 @@
final protected DeserializationConfig _config;
+ /**
+ * @since 2.9
+ */
+ final protected DeserializationContext _context;
+
/*
/**********************************************************
/* General information about POJO
@@ -107,10 +112,11 @@
*/
public BeanDeserializerBuilder(BeanDescription beanDesc,
- DeserializationConfig config)
+ DeserializationContext ctxt)
{
_beanDesc = beanDesc;
- _config = config;
+ _context = ctxt;
+ _config = ctxt.getConfig();
}
/**
@@ -120,6 +126,7 @@
protected BeanDeserializerBuilder(BeanDeserializerBuilder src)
{
_beanDesc = src._beanDesc;
+ _context = src._context;
_config = src._config;
// let's make copy of properties
@@ -146,7 +153,7 @@
private static <T> List<T> _copy(List<T> src) {
return (src == null) ? null : new ArrayList<T>(src);
}
-
+
/*
/**********************************************************
/* Life-cycle: state modification (adders, setters)
@@ -208,8 +215,7 @@
if (fixAccess) {
member.fixAccess(forceAccess);
}
- _injectables.add(new ValueInjector(propName, propType,
- contextAnnotations, member, valueId));
+ _injectables.add(new ValueInjector(propName, propType, member, valueId));
}
/**
@@ -334,7 +340,8 @@
_fixAccess(props);
BeanPropertyMap propertyMap = BeanPropertyMap.construct(props,
- _config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
+ _config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES),
+ _collectAliases(props));
propertyMap.assignIndexes();
// view processing must be enabled if:
@@ -373,22 +380,22 @@
* @since 2.0
*/
public AbstractDeserializer buildAbstract() {
- return new AbstractDeserializer(this, _beanDesc, _backRefProperties);
+ return new AbstractDeserializer(this, _beanDesc, _backRefProperties, _properties);
}
/**
* Method for constructing a specialized deserializer that uses
* additional external Builder object during data binding.
*/
- public JsonDeserializer<?> buildBuilderBased(JavaType valueType,
- String expBuildMethodName)
+ public JsonDeserializer<?> buildBuilderBased(JavaType valueType, String expBuildMethodName)
+ throws JsonMappingException
{
// First: validation; must have build method that returns compatible type
if (_buildMethod == null) {
// as per [databind#777], allow empty name
if (!expBuildMethodName.isEmpty()) {
- throw new IllegalArgumentException(String.format(
- "Builder class %s does not have build method (name: '%s')",
+ _context.reportBadDefinition(_beanDesc.getType(),
+ String.format("Builder class %s does not have build method (name: '%s')",
_beanDesc.getBeanClass().getName(),
expBuildMethodName));
}
@@ -399,16 +406,19 @@
if ((rawBuildType != rawValueType)
&& !rawBuildType.isAssignableFrom(rawValueType)
&& !rawValueType.isAssignableFrom(rawBuildType)) {
- throw new IllegalArgumentException("Build method '"+_buildMethod.getFullName()
- +" has bad return type ("+rawBuildType.getName()
- +"), not compatible with POJO type ("+valueType.getRawClass().getName()+")");
+ _context.reportBadDefinition(_beanDesc.getType(),
+ String.format("Build method '%s' has wrong return type (%s), not compatible with POJO type (%s)",
+ _buildMethod.getFullName(),
+ rawBuildType.getName(),
+ valueType.getRawClass().getName()));
}
}
// And if so, we can try building the deserializer
Collection<SettableBeanProperty> props = _properties.values();
_fixAccess(props);
BeanPropertyMap propertyMap = BeanPropertyMap.construct(props,
- _config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
+ _config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES),
+ _collectAliases(props));
propertyMap.assignIndexes();
boolean anyViews = !_config.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION);
@@ -423,17 +433,15 @@
}
if (_objectIdReader != null) {
- /* 18-Nov-2012, tatu: May or may not have annotations for id property;
- * but no easy access. But hard to see id property being optional,
- * so let's consider required at this point.
- */
+ // May or may not have annotations for id property; but no easy access.
+ // But hard to see id property being optional, so let's consider required at this point.
ObjectIdValueProperty prop = new ObjectIdValueProperty(_objectIdReader,
PropertyMetadata.STD_REQUIRED);
propertyMap = propertyMap.withProperty(prop);
}
return new BuilderBasedDeserializer(this,
- _beanDesc, propertyMap, _backRefProperties, _ignorableProps, _ignoreAllUnknown,
+ _beanDesc, valueType, propertyMap, _backRefProperties, _ignorableProps, _ignoreAllUnknown,
anyViews);
}
@@ -443,7 +451,7 @@
/**********************************************************
*/
- private void _fixAccess(Collection<SettableBeanProperty> mainProps)
+ protected void _fixAccess(Collection<SettableBeanProperty> mainProps)
{
/* 07-Sep-2016, tatu: Ideally we should be able to avoid forcing
* access to properties that are likely ignored, but due to
@@ -482,4 +490,26 @@
_buildMethod.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
}
}
+
+ protected Map<String,List<PropertyName>> _collectAliases(Collection<SettableBeanProperty> props)
+ {
+ Map<String,List<PropertyName>> mapping = null;
+ AnnotationIntrospector intr = _config.getAnnotationIntrospector();
+ if (intr != null) {
+ for (SettableBeanProperty prop : props) {
+ List<PropertyName> aliases = intr.findPropertyAliases(prop.getMember());
+ if ((aliases == null) || aliases.isEmpty()) {
+ continue;
+ }
+ if (mapping == null) {
+ mapping = new HashMap<>();
+ }
+ mapping.put(prop.getName(), aliases);
+ }
+ }
+ if (mapping == null) {
+ return Collections.emptyMap();
+ }
+ return mapping;
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerFactory.java b/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerFactory.java
index 1423f28..9b22130 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerFactory.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerFactory.java
@@ -3,13 +3,12 @@
import java.util.*;
import com.fasterxml.jackson.annotation.*;
-
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.fasterxml.jackson.databind.cfg.DeserializerFactoryConfig;
-import com.fasterxml.jackson.databind.cfg.ConfigOverride;
import com.fasterxml.jackson.databind.deser.impl.*;
import com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.introspect.*;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.util.ClassUtil;
@@ -37,8 +36,6 @@
*/
private final static Class<?>[] INIT_CAUSE_PARAMS = new Class<?>[] { Throwable.class };
- private final static Class<?>[] NO_VIEWS = new Class<?>[0];
-
/**
* Set of well-known "nasty classes", deserialization of which is considered dangerous
* and should (and is) prevented by default.
@@ -103,11 +100,7 @@
* Instead, let's actually just throw an error if this method is called when subtype
* has not properly overridden this method; this to indicate problem as soon as possible.
*/
- if (getClass() != BeanDeserializerFactory.class) {
- throw new IllegalStateException("Subtype of BeanDeserializerFactory ("+getClass().getName()
- +") has not properly overridden method 'withAdditionalDeserializers': can not instantiate subtype with "
- +"additional deserializer definitions");
- }
+ ClassUtil.verifyMustOverride(BeanDeserializerFactory.class, this, "withConfig");
return new BeanDeserializerFactory(config);
}
@@ -174,17 +167,16 @@
}
@Override
- public JsonDeserializer<Object> createBuilderBasedDeserializer(
- DeserializationContext ctxt, JavaType valueType, BeanDescription beanDesc,
- Class<?> builderClass)
- throws JsonMappingException
+ public JsonDeserializer<Object> createBuilderBasedDeserializer(DeserializationContext ctxt,
+ JavaType valueType, BeanDescription beanDesc, Class<?> builderClass)
+ throws JsonMappingException
{
// First: need a BeanDescription for builder class
JavaType builderType = ctxt.constructType(builderClass);
BeanDescription builderDesc = ctxt.getConfig().introspectForBuilder(builderType);
return buildBuilderBasedDeserializer(ctxt, valueType, builderDesc);
}
-
+
/**
* Method called by {@link BeanDeserializerFactory} to see if there might be a standard
* deserializer registered for given type.
@@ -251,6 +243,12 @@
valueInstantiator = findValueInstantiator(ctxt, beanDesc);
} catch (NoClassDefFoundError error) {
return new ErrorThrowingDeserializer(error);
+ } catch (IllegalArgumentException e) {
+ // 05-Apr-2017, tatu: Although it might appear cleaner to require collector
+ // to throw proper exception, it doesn't actually have reference to this
+ // instance so...
+ throw InvalidDefinitionException.from(ctxt.getParser(), e.getMessage(),
+ beanDesc, null);
}
BeanDeserializerBuilder builder = constructBeanDeserializerBuilder(ctxt, beanDesc);
builder.setValueInstantiator(valueInstantiator);
@@ -259,7 +257,7 @@
addObjectIdReader(ctxt, beanDesc, builder);
// managed/back reference fields/setters need special handling... first part
- addReferenceProperties(ctxt, beanDesc, builder);
+ addBackReferenceProperties(ctxt, beanDesc, builder);
addInjectables(ctxt, beanDesc, builder);
final DeserializationConfig config = ctxt.getConfig();
@@ -269,17 +267,13 @@
}
}
JsonDeserializer<?> deserializer;
-
- /* 19-Mar-2012, tatu: This check used to be done earlier; but we have to defer
- * it a bit to collect information on ObjectIdReader, for example.
- */
if (type.isAbstract() && !valueInstantiator.canInstantiate()) {
deserializer = builder.buildAbstract();
} else {
deserializer = builder.build();
}
-
- // [JACKSON-440]: may have modifier(s) that wants to modify or replace serializer we just built:
+ // may have modifier(s) that wants to modify or replace serializer we just built
+ // (note that `resolve()` and `createContextual()` called later on)
if (_factoryConfig.hasDeserializerModifiers()) {
for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) {
deserializer = mod.modifyDeserializer(config, beanDesc, deserializer);
@@ -300,8 +294,19 @@
DeserializationContext ctxt, JavaType valueType, BeanDescription builderDesc)
throws JsonMappingException
{
- // Creators, anyone? (to create builder itself)
- ValueInstantiator valueInstantiator = findValueInstantiator(ctxt, builderDesc);
+ // Creators, anyone? (to create builder itself)
+ ValueInstantiator valueInstantiator;
+ try {
+ valueInstantiator = findValueInstantiator(ctxt, builderDesc);
+ } catch (NoClassDefFoundError error) {
+ return new ErrorThrowingDeserializer(error);
+ } catch (IllegalArgumentException e) {
+ // 05-Apr-2017, tatu: Although it might appear cleaner to require collector
+ // to throw proper exception, it doesn't actually have reference to this
+ // instance so...
+ throw InvalidDefinitionException.from(ctxt.getParser(), e.getMessage(),
+ builderDesc, null);
+ }
final DeserializationConfig config = ctxt.getConfig();
BeanDeserializerBuilder builder = constructBeanDeserializerBuilder(ctxt, builderDesc);
builder.setValueInstantiator(valueInstantiator);
@@ -310,12 +315,12 @@
addObjectIdReader(ctxt, builderDesc, builder);
// managed/back reference fields/setters need special handling... first part
- addReferenceProperties(ctxt, builderDesc, builder);
+ addBackReferenceProperties(ctxt, builderDesc, builder);
addInjectables(ctxt, builderDesc, builder);
JsonPOJOBuilder.Value builderConfig = builderDesc.findPOJOBuilderConfig();
final String buildMethodName = (builderConfig == null) ?
- "build" : builderConfig.buildMethodName;
+ JsonPOJOBuilder.DEFAULT_BUILD_METHOD : builderConfig.buildMethodName;
// and lastly, find build method to use:
AnnotatedMethod buildMethod = builderDesc.findMethod(buildMethodName, null);
@@ -459,7 +464,7 @@
*/
protected BeanDeserializerBuilder constructBeanDeserializerBuilder(DeserializationContext ctxt,
BeanDescription beanDesc) {
- return new BeanDeserializerBuilder(beanDesc, ctxt.getConfig());
+ return new BeanDeserializerBuilder(beanDesc, ctxt);
}
/**
@@ -478,7 +483,7 @@
? builder.getValueInstantiator().getFromObjectArguments(ctxt.getConfig())
: null;
final boolean hasCreatorProps = (creatorProps != null);
-
+
// 01-May-2016, tatu: Which base type to use here gets tricky, since
// it may often make most sense to use general type for overrides,
// but what we have here may be more specific impl type. But for now
@@ -501,20 +506,10 @@
}
// Also, do we have a fallback "any" setter?
- AnnotatedMethod anySetterMethod = beanDesc.findAnySetter();
- AnnotatedMember anySetterField = null;
- if (anySetterMethod != null) {
- builder.setAnySetter(constructAnySetter(ctxt, beanDesc, anySetterMethod));
- }
- else {
- anySetterField = beanDesc.findAnySetterField();
- if(anySetterField != null) {
- builder.setAnySetter(constructAnySetter(ctxt, beanDesc, anySetterField));
- }
- }
- // NOTE: we do NOT add @JsonIgnore'd properties into blocked ones if there's any-setter
- // Implicit ones via @JsonIgnore and equivalent?
- if (anySetterMethod == null && anySetterField == null) {
+ AnnotatedMember anySetter = beanDesc.findAnySetterAccessor();
+ if (anySetter != null) {
+ builder.setAnySetter(constructAnySetter(ctxt, beanDesc, anySetter));
+ } else {
Collection<String> ignored2 = beanDesc.getIgnoredPropertyNames();
if (ignored2 != null) {
for (String propName : ignored2) {
@@ -537,32 +532,42 @@
propDefs = mod.updateProperties(ctxt.getConfig(), beanDesc, propDefs);
}
}
-
+
// At which point we still have all kinds of properties; not all with mutators:
for (BeanPropertyDefinition propDef : propDefs) {
SettableBeanProperty prop = null;
+
/* 18-Oct-2013, tatu: Although constructor parameters have highest precedence,
* we need to do linkage (as per [databind#318]), and so need to start with
* other types, and only then create constructor parameter, if any.
*/
if (propDef.hasSetter()) {
- JavaType propertyType = propDef.getSetter().getParameterType(0);
+ AnnotatedMethod setter = propDef.getSetter();
+ JavaType propertyType = setter.getParameterType(0);
prop = constructSettableProperty(ctxt, beanDesc, propDef, propertyType);
} else if (propDef.hasField()) {
- JavaType propertyType = propDef.getField().getType();
+ AnnotatedField field = propDef.getField();
+ JavaType propertyType = field.getType();
prop = constructSettableProperty(ctxt, beanDesc, propDef, propertyType);
- } else if (useGettersAsSetters && propDef.hasGetter()) {
- /* May also need to consider getters
- * for Map/Collection properties; but with lowest precedence
- */
+ } else {
+ // NOTE: specifically getter, since field was already checked above
AnnotatedMethod getter = propDef.getGetter();
- // should only consider Collections and Maps, for now?
- Class<?> rawPropertyType = getter.getRawType();
- if (Collection.class.isAssignableFrom(rawPropertyType)
- || Map.class.isAssignableFrom(rawPropertyType)) {
- prop = constructSetterlessProperty(ctxt, beanDesc, propDef);
+ if (getter != null) {
+ if (useGettersAsSetters && _isSetterlessType(getter.getRawType())) {
+ prop = constructSetterlessProperty(ctxt, beanDesc, propDef);
+ } else if (!propDef.hasConstructorParameter()) {
+ PropertyMetadata md = propDef.getMetadata();
+ // 25-Oct-2016, tatu: If merging enabled, might not need setter.
+ // We can not quite support this with creator parameters; in theory
+ // possibly, but right not not due to complexities of routing, so
+ // just prevent
+ if (md.getMergeInfo() != null) {
+ prop = constructSetterlessProperty(ctxt, beanDesc, propDef);
+ }
+ }
}
}
+
// 25-Sep-2014, tatu: No point in finding constructor parameters for abstract types
// (since they are never used anyway)
if (hasCreatorProps && propDef.hasConstructorParameter()) {
@@ -594,26 +599,34 @@
if (prop != null) {
cprop.setFallbackSetter(prop);
}
- prop = cprop;
+ Class<?>[] views = propDef.findViews();
+ if (views == null) {
+ views = beanDesc.findDefaultViews();
+ }
+ cprop.setViews(views);
builder.addCreatorProperty(cprop);
continue;
}
-
if (prop != null) {
+ // one more thing before adding to builder: copy any metadata
Class<?>[] views = propDef.findViews();
if (views == null) {
- // one more twist: if default inclusion disabled, need to force empty set of views
- if (!ctxt.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)) {
- views = NO_VIEWS;
- }
+ views = beanDesc.findDefaultViews();
}
- // one more thing before adding to builder: copy any metadata
prop.setViews(views);
builder.addProperty(prop);
}
}
}
-
+
+ private boolean _isSetterlessType(Class<?> rawType) {
+ // May also need to consider getters
+ // for Map/Collection properties; but with lowest precedence
+ // should only consider Collections and Maps, for now?
+ return Collection.class.isAssignableFrom(rawType)
+ || Map.class.isAssignableFrom(rawType);
+ }
+
/**
* Helper method called to filter out explicit ignored properties,
* as well as properties that have "ignorable types".
@@ -636,16 +649,10 @@
continue;
}
if (!property.hasConstructorParameter()) { // never skip constructor params
- Class<?> rawPropertyType = null;
- if (property.hasSetter()) {
- rawPropertyType = property.getSetter().getRawParameterType(0);
- } else if (property.hasField()) {
- rawPropertyType = property.getField().getRawType();
- }
-
+ Class<?> rawPropertyType = property.getRawPrimaryType();
// Some types are declared as ignorable as well
if ((rawPropertyType != null)
- && isIgnorableType(ctxt.getConfig(), beanDesc, rawPropertyType, ignoredTypes)) {
+ && isIgnorableType(ctxt.getConfig(), property, rawPropertyType, ignoredTypes)) {
// important: make ignorable, to avoid errors if value is actually seen
builder.addIgnorable(name);
continue;
@@ -659,17 +666,19 @@
/**
* Method that will find if bean has any managed- or back-reference properties,
* and if so add them to bean, to be linked during resolution phase.
+ *
+ * @since 2.9
*/
- protected void addReferenceProperties(DeserializationContext ctxt,
+ protected void addBackReferenceProperties(DeserializationContext ctxt,
BeanDescription beanDesc, BeanDeserializerBuilder builder)
throws JsonMappingException
{
// and then back references, not necessarily found as regular properties
- Map<String,AnnotatedMember> refs = beanDesc.findBackReferenceProperties();
- if (refs != null) {
- for (Map.Entry<String, AnnotatedMember> en : refs.entrySet()) {
- String name = en.getKey();
- AnnotatedMember m = en.getValue();
+ List<BeanPropertyDefinition> refProps = beanDesc.findBackReferences();
+ if (refProps != null) {
+ for (BeanPropertyDefinition refProp : refProps) {
+ /*
+ AnnotatedMember m = refProp.getMutator();
JavaType type;
if (m instanceof AnnotatedMethod) {
type = ((AnnotatedMethod) m).getParameterType(0);
@@ -679,18 +688,26 @@
// work through constructors; but let's at least indicate the issue for now
if (m instanceof AnnotatedParameter) {
ctxt.reportBadTypeDefinition(beanDesc,
-"Can not bind back references as Creator parameters: type %s (reference '%s', parameter index #%d)",
-beanDesc.getBeanClass().getName(), name, ((AnnotatedParameter) m).getIndex());
+"Can not bind back reference using Creator parameter (reference '%s', parameter index #%d)",
+name, ((AnnotatedParameter) m).getIndex());
}
}
- SimpleBeanPropertyDefinition propDef = SimpleBeanPropertyDefinition.construct(
- ctxt.getConfig(), m, PropertyName.construct(name));
- builder.addBackReferenceProperty(name, constructSettableProperty(ctxt,
- beanDesc, propDef, type));
+ */
+ String refName = refProp.findReferenceName();
+ builder.addBackReferenceProperty(refName, constructSettableProperty(ctxt,
+ beanDesc, refProp, refProp.getPrimaryType()));
}
}
}
+ @Deprecated // since 2.9 (rename)
+ protected void addReferenceProperties(DeserializationContext ctxt,
+ BeanDescription beanDesc, BeanDeserializerBuilder builder)
+ throws JsonMappingException
+ {
+ addBackReferenceProperties(ctxt, beanDesc, builder);
+ }
+
/**
* Method called locate all members used for value injection (if any),
* constructor {@link com.fasterxml.jackson.databind.deser.impl.ValueInjector} instances, and add them to builder.
@@ -724,31 +741,58 @@
throws JsonMappingException
{
//find the java type based on the annotated setter method or setter field
- JavaType type = null;
+ BeanProperty prop;
+ JavaType keyType;
+ JavaType valueType;
+
if (mutator instanceof AnnotatedMethod) {
// we know it's a 2-arg method, second arg is the value
- type = ((AnnotatedMethod) mutator).getParameterType(1);
+ AnnotatedMethod am = (AnnotatedMethod) mutator;
+ keyType = am.getParameterType(0);
+ valueType = am.getParameterType(1);
+ valueType = resolveMemberAndTypeAnnotations(ctxt, mutator, valueType);
+ prop = new BeanProperty.Std(PropertyName.construct(mutator.getName()),
+ valueType, null, mutator,
+ PropertyMetadata.STD_OPTIONAL);
+
} else if (mutator instanceof AnnotatedField) {
+ AnnotatedField af = (AnnotatedField) mutator;
// get the type from the content type of the map object
- type = ((AnnotatedField) mutator).getType().getContentType();
+ JavaType mapType = af.getType();
+ mapType = resolveMemberAndTypeAnnotations(ctxt, mutator, mapType);
+ keyType = mapType.getKeyType();
+ valueType = mapType.getContentType();
+ prop = new BeanProperty.Std(PropertyName.construct(mutator.getName()),
+ mapType, null, mutator, PropertyMetadata.STD_OPTIONAL);
+ } else {
+ return ctxt.reportBadDefinition(beanDesc.getType(), String.format(
+ "Unrecognized mutator type for any setter: %s", mutator.getClass()));
}
- // First: various annotations on type itself, as well as type-overrides
- // on accessor need to be resolved
- type = resolveMemberAndTypeAnnotations(ctxt, mutator, type);
- BeanProperty.Std prop = new BeanProperty.Std(PropertyName.construct(mutator.getName()),
- type, null, beanDesc.getClassAnnotations(), mutator,
- PropertyMetadata.STD_OPTIONAL);
+ // First: see if there are explicitly specified
// and then possible direct deserializer override on accessor
- JsonDeserializer<Object> deser = findDeserializerFromAnnotation(ctxt, mutator);
+ KeyDeserializer keyDeser = findKeyDeserializerFromAnnotation(ctxt, mutator);
+ if (keyDeser == null) {
+ keyDeser = keyType.getValueHandler();
+ }
+ if (keyDeser == null) {
+ keyDeser = ctxt.findKeyDeserializer(keyType, prop);
+ } else {
+ if (keyDeser instanceof ContextualKeyDeserializer) {
+ keyDeser = ((ContextualKeyDeserializer) keyDeser)
+ .createContextual(ctxt, prop);
+ }
+ }
+ JsonDeserializer<Object> deser = findContentDeserializerFromAnnotation(ctxt, mutator);
if (deser == null) {
- deser = type.getValueHandler();
+ deser = valueType.getValueHandler();
}
if (deser != null) {
// As per [databind#462] need to ensure we contextualize deserializer before passing it on
- deser = (JsonDeserializer<Object>) ctxt.handlePrimaryContextualization(deser, prop, type);
+ deser = (JsonDeserializer<Object>) ctxt.handlePrimaryContextualization(deser, prop, valueType);
}
- TypeDeserializer typeDeser = type.getTypeHandler();
- return new SettableAnyProperty(prop, mutator, type, deser, typeDeser);
+ TypeDeserializer typeDeser = valueType.getTypeHandler();
+ return new SettableAnyProperty(prop, mutator, valueType,
+ keyDeser, deser, typeDeser);
}
/**
@@ -864,24 +908,26 @@
* Helper method that will check whether given raw type is marked as always ignorable
* (for purpose of ignoring properties with type)
*/
- protected boolean isIgnorableType(DeserializationConfig config, BeanDescription beanDesc,
+ protected boolean isIgnorableType(DeserializationConfig config, BeanPropertyDefinition propDef,
Class<?> type, Map<Class<?>,Boolean> ignoredTypes)
{
Boolean status = ignoredTypes.get(type);
if (status != null) {
return status.booleanValue();
}
- // 21-Apr-2016, tatu: For 2.8, can specify config overrides
- ConfigOverride override = config.findConfigOverride(type);
- if (override != null) {
- status = override.getIsIgnoredType();
- }
- if (status == null) {
- BeanDescription desc = config.introspectClassAnnotations(type);
- status = config.getAnnotationIntrospector().isIgnorableType(desc.getClassInfo());
- // We default to 'false', i.e. not ignorable
+ // 22-Oct-2016, tatu: Slight check to skip primitives, String
+ if ((type == String.class) || type.isPrimitive()) {
+ status = Boolean.FALSE;
+ } else {
+ // 21-Apr-2016, tatu: For 2.8, can specify config overrides
+ status = config.getConfigOverride(type).getIsIgnoredType();
if (status == null) {
- status = Boolean.FALSE;
+ BeanDescription desc = config.introspectClassAnnotations(type);
+ status = config.getAnnotationIntrospector().isIgnorableType(desc.getClassInfo());
+ // We default to 'false', i.e. not ignorable
+ if (status == null) {
+ status = Boolean.FALSE;
+ }
}
}
ignoredTypes.put(type, status);
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/BuilderBasedDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/BuilderBasedDeserializer.java
index 817e29e..feaca69 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/BuilderBasedDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/BuilderBasedDeserializer.java
@@ -27,6 +27,14 @@
protected final AnnotatedMethod _buildMethod;
+ /**
+ * Type that the builder will produce, target type; as opposed to
+ * `handledType()` which refers to Builder class.
+ *
+ * @since 2.9
+ */
+ protected final JavaType _targetType;
+
/*
/**********************************************************
/* Life-cycle, construction, initialization
@@ -37,13 +45,14 @@
* Constructor used by {@link BeanDeserializerBuilder}.
*/
public BuilderBasedDeserializer(BeanDeserializerBuilder builder,
- BeanDescription beanDesc,
+ BeanDescription beanDesc, JavaType targetType,
BeanPropertyMap properties, Map<String, SettableBeanProperty> backRefs,
Set<String> ignorableProps, boolean ignoreAllUnknown,
boolean hasViews)
{
super(builder, beanDesc, properties, backRefs,
ignorableProps, ignoreAllUnknown, hasViews);
+ _targetType = targetType;
_buildMethod = builder.getBuildMethod();
// 05-Mar-2012, tatu: Can not really make Object Ids work with builders, not yet anyway
if (_objectIdReader != null) {
@@ -53,6 +62,21 @@
}
/**
+ * @deprecated Since 2.9
+ */
+ @Deprecated
+ public BuilderBasedDeserializer(BeanDeserializerBuilder builder,
+ BeanDescription beanDesc,
+ BeanPropertyMap properties, Map<String, SettableBeanProperty> backRefs,
+ Set<String> ignorableProps, boolean ignoreAllUnknown,
+ boolean hasViews)
+ {
+ this(builder, beanDesc,
+ beanDesc.getType(), // Wrong! But got no access via `BeanDeserializerBuilder`
+ properties, backRefs, ignorableProps, ignoreAllUnknown, hasViews);
+ }
+
+ /**
* Copy-constructor that can be used by sub-classes to allow
* copy-on-write styling copying of settings of an existing instance.
*/
@@ -65,26 +89,31 @@
{
super(src, ignoreAllUnknown);
_buildMethod = src._buildMethod;
+ _targetType = src._targetType;
}
protected BuilderBasedDeserializer(BuilderBasedDeserializer src, NameTransformer unwrapper) {
super(src, unwrapper);
_buildMethod = src._buildMethod;
+ _targetType = src._targetType;
}
public BuilderBasedDeserializer(BuilderBasedDeserializer src, ObjectIdReader oir) {
super(src, oir);
_buildMethod = src._buildMethod;
+ _targetType = src._targetType;
}
public BuilderBasedDeserializer(BuilderBasedDeserializer src, Set<String> ignorableProps) {
super(src, ignorableProps);
_buildMethod = src._buildMethod;
+ _targetType = src._targetType;
}
public BuilderBasedDeserializer(BuilderBasedDeserializer src, BeanPropertyMap props) {
super(src, props);
_buildMethod = src._buildMethod;
+ _targetType = src._targetType;
}
@Override
@@ -115,7 +144,7 @@
@Override
protected BeanDeserializerBase asArrayDeserializer() {
SettableBeanProperty[] props = _beanProperties.getPropertiesInInsertionOrder();
- return new BeanAsArrayBuilderDeserializer(this, props, _buildMethod);
+ return new BeanAsArrayBuilderDeserializer(this, _targetType, props, _buildMethod);
}
/*
@@ -124,6 +153,18 @@
/**********************************************************
*/
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ // 26-Oct-2016, tatu: No, we can't merge Builder-based POJOs as of now
+ return Boolean.FALSE;
+ }
+
+ /*
+ /**********************************************************
+ /* JsonDeserializer implementation
+ /**********************************************************
+ */
+
protected final Object finishBuild(DeserializationContext ctxt, Object builder)
throws IOException
{
@@ -132,7 +173,7 @@
return builder;
}
try {
- return _buildMethod.getMember().invoke(builder);
+ return _buildMethod.getMember().invoke(builder, (Object[]) null);
} catch (Exception e) {
return wrapInstantiationProblem(e, ctxt);
}
@@ -145,39 +186,35 @@
public final Object deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
- JsonToken t = p.getCurrentToken();
-
// common case first:
- if (t == JsonToken.START_OBJECT) {
- t = p.nextToken();
+ if (p.isExpectedStartObjectToken()) {
+ JsonToken t = p.nextToken();
if (_vanillaProcessing) {
- return finishBuild(ctxt, vanillaDeserialize(p, ctxt, t));
+ return finishBuild(ctxt, vanillaDeserialize(p, ctxt, t));
}
Object builder = deserializeFromObject(p, ctxt);
return finishBuild(ctxt, builder);
}
// and then others, generally requiring use of @JsonCreator
- if (t != null) {
- switch (t) {
- case VALUE_STRING:
- return finishBuild(ctxt, deserializeFromString(p, ctxt));
- case VALUE_NUMBER_INT:
- return finishBuild(ctxt, deserializeFromNumber(p, ctxt));
- case VALUE_NUMBER_FLOAT:
- return finishBuild(ctxt, deserializeFromDouble(p, ctxt));
- case VALUE_EMBEDDED_OBJECT:
- return p.getEmbeddedObject();
- case VALUE_TRUE:
- case VALUE_FALSE:
- return finishBuild(ctxt, deserializeFromBoolean(p, ctxt));
- case START_ARRAY:
- // these only work if there's a (delegating) creator...
- return finishBuild(ctxt, deserializeFromArray(p, ctxt));
- case FIELD_NAME:
- case END_OBJECT:
- return finishBuild(ctxt, deserializeFromObject(p, ctxt));
- default:
- }
+ switch (p.getCurrentTokenId()) {
+ case JsonTokenId.ID_STRING:
+ return finishBuild(ctxt, deserializeFromString(p, ctxt));
+ case JsonTokenId.ID_NUMBER_INT:
+ return finishBuild(ctxt, deserializeFromNumber(p, ctxt));
+ case JsonTokenId.ID_NUMBER_FLOAT:
+ return finishBuild(ctxt, deserializeFromDouble(p, ctxt));
+ case JsonTokenId.ID_EMBEDDED_OBJECT:
+ return p.getEmbeddedObject();
+ case JsonTokenId.ID_TRUE:
+ case JsonTokenId.ID_FALSE:
+ return finishBuild(ctxt, deserializeFromBoolean(p, ctxt));
+ case JsonTokenId.ID_START_ARRAY:
+ // these only work if there's a (delegating) creator...
+ return finishBuild(ctxt, deserializeFromArray(p, ctxt));
+ case JsonTokenId.ID_FIELD_NAME:
+ case JsonTokenId.ID_END_OBJECT:
+ return finishBuild(ctxt, deserializeFromObject(p, ctxt));
+ default:
}
return ctxt.handleUnexpectedToken(handledType(), p);
}
@@ -189,13 +226,22 @@
*/
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt,
- Object builder)
- throws IOException
+ Object value) throws IOException
{
- /* Important: we call separate method which does NOT call
- * 'finishBuild()', to avoid problems with recursion
- */
- return finishBuild(ctxt, _deserialize(p, ctxt, builder));
+ // 26-Oct-2016, tatu: I can not see any of making this actually
+ // work correctly, so let's indicate problem right away
+ JavaType valueType = _targetType;
+ // Did they try to give us builder?
+ Class<?> builderRawType = handledType();
+ Class<?> instRawType = value.getClass();
+ if (builderRawType.isAssignableFrom(instRawType)) {
+ return ctxt.reportBadDefinition(valueType, String.format(
+ "Deserialization of %s by passing existing Builder (%s) instance not supported",
+ valueType, builderRawType.getName()));
+ }
+ return ctxt.reportBadDefinition(valueType, String.format(
+ "Deserialization of %s by passing existing instance (of %s) not supported",
+ valueType, instRawType.getName()));
}
/*
@@ -204,56 +250,13 @@
/**********************************************************
*/
- protected final Object _deserialize(JsonParser p,
- DeserializationContext ctxt, Object builder)
- throws IOException, JsonProcessingException
- {
- if (_injectables != null) {
- injectValues(ctxt, builder);
- }
- if (_unwrappedPropertyHandler != null) {
- return deserializeWithUnwrapped(p, ctxt, builder);
- }
- if (_externalTypeIdHandler != null) {
- return deserializeWithExternalTypeId(p, ctxt, builder);
- }
- if (_needViewProcesing) {
- Class<?> view = ctxt.getActiveView();
- if (view != null) {
- return deserializeWithView(p, ctxt, builder, view);
- }
- }
- JsonToken t = p.getCurrentToken();
- // 23-Mar-2010, tatu: In some cases, we start with full JSON object too...
- if (t == JsonToken.START_OBJECT) {
- t = p.nextToken();
- }
- for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
- String propName = p.getCurrentName();
- // Skip field name:
- p.nextToken();
- SettableBeanProperty prop = _beanProperties.find(propName);
-
- if (prop != null) { // normal case
- try {
- builder = prop.deserializeSetAndReturn(p, ctxt, builder);
- } catch (Exception e) {
- wrapAndThrow(e, builder, propName, ctxt);
- }
- continue;
- }
- handleUnknownVanilla(p, ctxt, handledType(), propName);
- }
- return builder;
- }
-
/**
* Streamlined version that is only used when no "special"
* features are enabled.
*/
private final Object vanillaDeserialize(JsonParser p,
DeserializationContext ctxt, JsonToken t)
- throws IOException, JsonProcessingException
+ throws IOException
{
Object bean = _valueInstantiator.createUsingDefault(ctxt);
for (; p.getCurrentToken() != JsonToken.END_OBJECT; p.nextToken()) {
@@ -280,7 +283,7 @@
*/
@Override
public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_nonStandardCreation) {
if (_unwrappedPropertyHandler != null) {
@@ -326,15 +329,18 @@
* values for creator method need to be buffered, first; and
* due to non-guaranteed ordering possibly some other properties
* as well.
+ *
+ * @return Builder instance constructed
*/
@Override
@SuppressWarnings("resource")
protected final Object _deserializeUsingPropertyBased(final JsonParser p,
final DeserializationContext ctxt)
- throws IOException, JsonProcessingException
- {
+ throws IOException
+ {
final PropertyBasedCreator creator = _propertyBasedCreator;
PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, _objectIdReader);
+ final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
// 04-Jan-2010, tatu: May need to collect unknown properties for polymorphic cases
TokenBuffer unknown = null;
@@ -346,25 +352,29 @@
// creator property?
SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
if (creatorProp != null) {
+ if ((activeView != null) && !creatorProp.visibleInView(activeView)) {
+ p.skipChildren();
+ continue;
+ }
// Last creator property to set?
if (buffer.assignParameter(creatorProp, creatorProp.deserialize(p, ctxt))) {
p.nextToken(); // to move to following FIELD_NAME/END_OBJECT
- Object bean;
+ Object builder;
try {
- bean = creator.build(ctxt, buffer);
+ builder = creator.build(ctxt, buffer);
} catch (Exception e) {
wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
continue; // never gets here
}
// polymorphic?
- if (bean.getClass() != _beanType.getRawClass()) {
- return handlePolymorphic(p, ctxt, bean, unknown);
+ if (builder.getClass() != _beanType.getRawClass()) {
+ return handlePolymorphic(p, ctxt, builder, unknown);
}
if (unknown != null) { // nope, just extra unknown stuff...
- bean = handleUnknownProperties(ctxt, bean, unknown);
+ builder = handleUnknownProperties(ctxt, builder, unknown);
}
// or just clean?
- return _deserialize(p, ctxt, bean);
+ return _deserialize(p, ctxt, builder);
}
continue;
}
@@ -398,21 +408,69 @@
}
// We hit END_OBJECT, so:
- Object bean;
+ Object builder;
try {
- bean = creator.build(ctxt, buffer);
+ builder = creator.build(ctxt, buffer);
} catch (Exception e) {
- bean = wrapInstantiationProblem(e, ctxt);
+ builder = wrapInstantiationProblem(e, ctxt);
}
if (unknown != null) {
// polymorphic?
- if (bean.getClass() != _beanType.getRawClass()) {
- return handlePolymorphic(null, ctxt, bean, unknown);
+ if (builder.getClass() != _beanType.getRawClass()) {
+ return handlePolymorphic(null, ctxt, builder, unknown);
}
// no, just some extra unknown properties
- return handleUnknownProperties(ctxt, bean, unknown);
+ return handleUnknownProperties(ctxt, builder, unknown);
}
- return bean;
+ return builder;
+ }
+
+ @SuppressWarnings("resource")
+ protected final Object _deserialize(JsonParser p,
+ DeserializationContext ctxt, Object builder) throws IOException
+ {
+ if (_injectables != null) {
+ injectValues(ctxt, builder);
+ }
+ if (_unwrappedPropertyHandler != null) {
+ if (p.hasToken(JsonToken.START_OBJECT)) {
+ p.nextToken();
+ }
+ TokenBuffer tokens = new TokenBuffer(p, ctxt);
+ tokens.writeStartObject();
+ return deserializeWithUnwrapped(p, ctxt, builder, tokens);
+ }
+ if (_externalTypeIdHandler != null) {
+ return deserializeWithExternalTypeId(p, ctxt, builder);
+ }
+ if (_needViewProcesing) {
+ Class<?> view = ctxt.getActiveView();
+ if (view != null) {
+ return deserializeWithView(p, ctxt, builder, view);
+ }
+ }
+ JsonToken t = p.getCurrentToken();
+ // 23-Mar-2010, tatu: In some cases, we start with full JSON object too...
+ if (t == JsonToken.START_OBJECT) {
+ t = p.nextToken();
+ }
+ for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
+ String propName = p.getCurrentName();
+ // Skip field name:
+ p.nextToken();
+ SettableBeanProperty prop = _beanProperties.find(propName);
+
+ if (prop != null) { // normal case
+ try {
+ builder = prop.deserializeSetAndReturn(p, ctxt, builder);
+ } catch (Exception e) {
+ wrapAndThrow(e, builder, propName, ctxt);
+ }
+ continue;
+ }
+ handleUnknownVanilla(p, ctxt, handledType(), propName);
+ }
+ return builder;
}
/*
@@ -423,7 +481,7 @@
protected final Object deserializeWithView(JsonParser p, DeserializationContext ctxt,
Object bean, Class<?> activeView)
- throws IOException, JsonProcessingException
+ throws IOException
{
JsonToken t = p.getCurrentToken();
for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
@@ -460,7 +518,7 @@
*/
@SuppressWarnings("resource")
protected Object deserializeWithUnwrapped(JsonParser p, DeserializationContext ctxt)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_delegateDeserializer != null) {
return _valueInstantiator.createUsingDelegate(ctxt, _delegateDeserializer.deserialize(p, ctxt));
@@ -513,65 +571,20 @@
}
}
tokens.writeEndObject();
- _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
- return bean;
- }
-
- @SuppressWarnings("resource")
- protected Object deserializeWithUnwrapped(JsonParser p,
- DeserializationContext ctxt, Object bean)
- throws IOException, JsonProcessingException
- {
- JsonToken t = p.getCurrentToken();
- if (t == JsonToken.START_OBJECT) {
- t = p.nextToken();
- }
- TokenBuffer tokens = new TokenBuffer(p, ctxt);
- tokens.writeStartObject();
- final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
- for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
- String propName = p.getCurrentName();
- SettableBeanProperty prop = _beanProperties.find(propName);
- p.nextToken();
- if (prop != null) { // normal case
- if (activeView != null && !prop.visibleInView(activeView)) {
- p.skipChildren();
- continue;
- }
- try {
- bean = prop.deserializeSetAndReturn(p, ctxt, bean);
- } catch (Exception e) {
- wrapAndThrow(e, bean, propName, ctxt);
- }
- continue;
- }
- if (_ignorableProps != null && _ignorableProps.contains(propName)) {
- handleIgnoredProperty(p, ctxt, bean, propName);
- continue;
- }
- // but... others should be passed to unwrapped property deserializers
- tokens.writeFieldName(propName);
- tokens.copyCurrentStructure(p);
- // how about any setter? We'll get copies but...
- if (_anySetter != null) {
- _anySetter.deserializeAndSet(p, ctxt, bean, propName);
- }
- }
- tokens.writeEndObject();
- _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
- return bean;
+ return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
}
@SuppressWarnings("resource")
protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p,
DeserializationContext ctxt)
- throws IOException, JsonProcessingException
+ throws IOException
{
final PropertyBasedCreator creator = _propertyBasedCreator;
PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, _objectIdReader);
TokenBuffer tokens = new TokenBuffer(p, ctxt);
tokens.writeStartObject();
+ Object builder = null;
JsonToken t = p.getCurrentToken();
for (; t == JsonToken.FIELD_NAME; t = p.nextToken()) {
@@ -580,7 +593,20 @@
// creator property?
SettableBeanProperty creatorProp = creator.findCreatorProperty(propName);
if (creatorProp != null) {
- buffer.assignParameter(creatorProp, creatorProp.deserialize(p, ctxt));
+ // Last creator property to set?
+ if (buffer.assignParameter(creatorProp, creatorProp.deserialize(p, ctxt))) {
+ t = p.nextToken(); // to move to following FIELD_NAME/END_OBJECT
+ try {
+ builder = creator.build(ctxt, buffer);
+ } catch (Exception e) {
+ wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
+ continue; // never gets here
+ }
+ if (builder.getClass() != _beanType.getRawClass()) {
+ return handlePolymorphic(p, ctxt, builder, tokens);
+ }
+ return deserializeWithUnwrapped(p, ctxt, builder, tokens);
+ }
continue;
}
// Object Id property?
@@ -604,16 +630,52 @@
buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
}
}
-
// We hit END_OBJECT, so:
- Object bean;
- // !!! 15-Feb-2012, tatu: Need to modify creator to use Builder!
- try {
- bean = creator.build(ctxt, buffer);
- } catch (Exception e) {
- return wrapInstantiationProblem(e, ctxt);
+ if (builder == null) {
+ try {
+ builder = creator.build(ctxt, buffer);
+ } catch (Exception e) {
+ return wrapInstantiationProblem(e, ctxt);
+ }
}
- return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
+ return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, builder, tokens);
+ }
+
+ protected Object deserializeWithUnwrapped(JsonParser p,
+ DeserializationContext ctxt, Object builder, TokenBuffer tokens)
+ throws IOException
+ {
+ final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
+ for (JsonToken t = p.getCurrentToken(); t == JsonToken.FIELD_NAME; t = p.nextToken()) {
+ String propName = p.getCurrentName();
+ SettableBeanProperty prop = _beanProperties.find(propName);
+ p.nextToken();
+ if (prop != null) { // normal case
+ if (activeView != null && !prop.visibleInView(activeView)) {
+ p.skipChildren();
+ continue;
+ }
+ try {
+ builder = prop.deserializeSetAndReturn(p, ctxt, builder);
+ } catch (Exception e) {
+ wrapAndThrow(e, builder, propName, ctxt);
+ }
+ continue;
+ }
+ if (_ignorableProps != null && _ignorableProps.contains(propName)) {
+ handleIgnoredProperty(p, ctxt, builder, propName);
+ continue;
+ }
+ // but... others should be passed to unwrapped property deserializers
+ tokens.writeFieldName(propName);
+ tokens.copyCurrentStructure(p);
+ // how about any setter? We'll get copies but...
+ if (_anySetter != null) {
+ _anySetter.deserializeAndSet(p, ctxt, builder, propName);
+ }
+ }
+ tokens.writeEndObject();
+ return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, builder, tokens);
}
/*
@@ -624,7 +686,7 @@
*/
protected Object deserializeWithExternalTypeId(JsonParser p, DeserializationContext ctxt)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (_propertyBasedCreator != null) {
return deserializeUsingPropertyBasedWithExternalTypeId(p, ctxt);
@@ -634,7 +696,7 @@
protected Object deserializeWithExternalTypeId(JsonParser p,
DeserializationContext ctxt, Object bean)
- throws IOException, JsonProcessingException
+ throws IOException
{
final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
final ExternalTypeHandler ext = _externalTypeIdHandler.start();
@@ -644,7 +706,7 @@
t = p.nextToken();
SettableBeanProperty prop = _beanProperties.find(propName);
if (prop != null) { // normal case
- // [JACKSON-831]: may have property AND be used as external type id:
+ // May have property AND be used as external type id:
if (t.isScalarValue()) {
ext.handleTypePropertyValue(p, ctxt, propName, bean);
}
@@ -675,6 +737,7 @@
} catch (Exception e) {
wrapAndThrow(e, bean, propName, ctxt);
}
+ continue;
} else {
// Unknown: let's call handler method
handleUnknownProperty(p, ctxt, bean, propName);
@@ -686,9 +749,12 @@
protected Object deserializeUsingPropertyBasedWithExternalTypeId(JsonParser p,
DeserializationContext ctxt)
- throws IOException, JsonProcessingException
+ throws IOException
{
// !!! 04-Mar-2012, TODO: Need to fix -- will not work as is...
- throw new IllegalStateException("Deserialization with Builder, External type id, @JsonCreator not yet implemented");
+ JavaType t = _targetType;
+ return ctxt.reportBadDefinition(t, String.format(
+ "Deserialization (of %s) with Builder, External type id, @JsonCreator not yet implemented",
+ t));
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/CreatorProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/CreatorProperty.java
index d8e8602..2789cc7 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/CreatorProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/CreatorProperty.java
@@ -6,16 +6,19 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.util.Annotations;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* This concrete sub-class implements property that is passed
* via Creator (constructor or static factory method).
* It is not a full-featured implementation in that its set method
- * should never be called -- instead, value must separately passed.
+ * should usually not be called for primary mutation -- instead, value must separately passed --
+ * but some aspects are still needed (specifically, injection).
*<p>
* Note on injectable values: unlike with other mutators, where
* deserializer and injecting are separate, here we treat the two as related
@@ -96,8 +99,9 @@
_fallbackSetter = src._fallbackSetter;
}
- protected CreatorProperty(CreatorProperty src, JsonDeserializer<?> deser) {
- super(src, deser);
+ protected CreatorProperty(CreatorProperty src, JsonDeserializer<?> deser,
+ NullValueProvider nva) {
+ super(src, deser, nva);
_annotated = src._annotated;
_creatorIndex = src._creatorIndex;
_injectableValueId = src._injectableValueId;
@@ -105,19 +109,24 @@
}
@Override
- public CreatorProperty withName(PropertyName newName) {
+ public SettableBeanProperty withName(PropertyName newName) {
return new CreatorProperty(this, newName);
}
@Override
- public CreatorProperty withValueDeserializer(JsonDeserializer<?> deser) {
+ public SettableBeanProperty withValueDeserializer(JsonDeserializer<?> deser) {
if (_valueDeserializer == deser) {
return this;
}
- return new CreatorProperty(this, deser);
+ return new CreatorProperty(this, deser, _nullProvider);
}
@Override
+ public SettableBeanProperty withNullProvider(NullValueProvider nva) {
+ return new CreatorProperty(this, _valueDeserializer, nva);
+ }
+
+ @Override
public void fixAccess(DeserializationConfig config) {
if (_fallbackSetter != null) {
_fallbackSetter.fixAccess(config);
@@ -139,14 +148,16 @@
* property, if it is configured for this.
*/
public Object findInjectableValue(DeserializationContext context, Object beanInstance)
+ throws JsonMappingException
{
if (_injectableValueId == null) {
- throw new IllegalStateException("Property '"+getName()
- +"' (type "+getClass().getName()+") has no injectable value id configured");
+ context.reportBadDefinition(ClassUtil.classOf(beanInstance),
+ String.format("Property '%s' (type %s) has no injectable value id configured",
+ getName(), getClass().getName()));
}
return context.findInjectableValue(_injectableValueId, this, beanInstance);
}
-
+
/**
* Method to find value to inject, and inject it to this property.
*/
@@ -155,7 +166,7 @@
{
set(beanInstance, findInjectableValue(context, beanInstance));
}
-
+
/*
/**********************************************************
/* BeanProperty impl
@@ -186,36 +197,29 @@
public void deserializeAndSet(JsonParser p, DeserializationContext ctxt,
Object instance) throws IOException
{
- set(instance, deserialize(p, ctxt));
+ _verifySetter();
+ _fallbackSetter.set(instance, deserialize(p, ctxt));
}
@Override
public Object deserializeSetAndReturn(JsonParser p,
DeserializationContext ctxt, Object instance) throws IOException
{
- return setAndReturn(instance, deserialize(p, ctxt));
+ _verifySetter();
+ return _fallbackSetter.setAndReturn(instance, deserialize(p, ctxt));
}
@Override
public void set(Object instance, Object value) throws IOException
{
- /* Hmmmh. Should we return quietly (NOP), or error?
- * Perhaps better to throw an exception, since it's generally an error.
- */
- if (_fallbackSetter == null) {
- throw new IllegalStateException("No fallback setter/field defined: can not use creator property for "
- +getClass().getName());
- }
+ _verifySetter();
_fallbackSetter.set(instance, value);
}
@Override
public Object setAndReturn(Object instance, Object value) throws IOException
{
- if (_fallbackSetter == null) {
- throw new IllegalStateException("No fallback setter/field defined: can not use creator property for "
- +getClass().getName());
- }
+ _verifySetter();
return _fallbackSetter.setAndReturn(instance, value);
}
@@ -226,4 +230,24 @@
@Override
public String toString() { return "[creator property, name '"+getName()+"'; inject id '"+_injectableValueId+"']"; }
+
+ // since 2.9
+ private final void _verifySetter() throws IOException {
+ if (_fallbackSetter == null) {
+ _reportMissingSetter(null, null);
+ }
+ }
+
+ // since 2.9
+ private void _reportMissingSetter(JsonParser p, DeserializationContext ctxt) throws IOException
+ {
+ final String msg = "No fallback setter/field defined for creator property '"+getName()+"'";
+ // Hmmmh. Should we return quietly (NOP), or error?
+ // Perhaps better to throw an exception, since it's generally an error.
+ if (ctxt != null ) {
+ ctxt.reportBadDefinition(getType(), msg);
+ } else {
+ throw InvalidDefinitionException.from(p, msg, getType());
+ }
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/DefaultDeserializationContext.java b/src/main/java/com/fasterxml/jackson/databind/deser/DefaultDeserializationContext.java
index d759ddd..dc45540 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/DefaultDeserializationContext.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/DefaultDeserializationContext.java
@@ -331,16 +331,14 @@
@Override
public DefaultDeserializationContext copy() {
- if (getClass() != Impl.class) {
- return super.copy();
- }
+ ClassUtil.verifyMustOverride(Impl.class, this, "copy");
return new Impl(this);
}
@Override
public DefaultDeserializationContext createInstance(DeserializationConfig config,
- JsonParser jp, InjectableValues values) {
- return new Impl(this, config, jp, values);
+ JsonParser p, InjectableValues values) {
+ return new Impl(this, config, p, values);
}
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/DeserializationProblemHandler.java b/src/main/java/com/fasterxml/jackson/databind/deser/DeserializationProblemHandler.java
index e374867..9245045 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/DeserializationProblemHandler.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/DeserializationProblemHandler.java
@@ -267,15 +267,18 @@
* what to do (and exception may be thrown), or value to use (possibly
* <code>null</code>
*
- * @since 2.8
+ * @since 2.9
*/
public Object handleMissingInstantiator(DeserializationContext ctxt,
- Class<?> instClass, JsonParser p, String msg)
+ Class<?> instClass, ValueInstantiator valueInsta, JsonParser p,
+ String msg)
throws IOException
{
- return NOT_HANDLED;
+ // 16-Oct-2016, tatu: Need to delegate to deprecated method from 2.8;
+ // remove redirect from later versions (post-2.9)
+ return handleMissingInstantiator(ctxt, instClass, p, msg);
}
-
+
/**
* Handler method called if resolution of type id from given String failed
* to produce a subtype; usually because logical id is not mapped to actual
@@ -313,4 +316,58 @@
{
return null;
}
+
+ /**
+ * Handler method called if an expected type id for a polymorphic value is
+ * not found and no "default type" is specified or allowed.
+ * Handler may choose to do one of following things:
+ *<ul>
+ * <li>Indicate it does not know what to do by returning `null`
+ * </li>
+ * <li>Indicate that nothing should be deserialized, by return `Void.class`
+ * </li>
+ * <li>Throw a {@link IOException} to indicate specific fail message (instead of
+ * standard exception caller would throw
+ * </li>
+ * <li>Return actual resolved type to use for this particular case.
+ * </li>
+ * </ul>
+ *
+ * @param ctxt Deserialization context to use for accessing information or
+ * constructing exception to throw
+ * @param baseType Base type to use for resolving subtype id
+ * @param failureMsg Informational message that would be thrown as part of
+ * exception, if resolution still fails
+ *
+ * @return Actual type to use, if resolved; `null` if handler does not know what
+ * to do; or `Void.class` to indicate that nothing should be deserialized for
+ * type with the id (which caller may choose to do... or not)
+ *
+ * @since 2.9
+ */
+ public JavaType handleMissingTypeId(DeserializationContext ctxt,
+ JavaType baseType, TypeIdResolver idResolver,
+ String failureMsg)
+ throws IOException
+ {
+ return null;
+ }
+
+ /*
+ /**********************************************************
+ /* Deprecated
+ /**********************************************************
+ */
+
+ /**
+ * @since 2.8
+ * @deprecated Since 2.9: use variant that takes {@link ValueInstantiator}
+ */
+ @Deprecated
+ public Object handleMissingInstantiator(DeserializationContext ctxt,
+ Class<?> instClass, JsonParser p, String msg)
+ throws IOException
+ {
+ return NOT_HANDLED;
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/DeserializerCache.java b/src/main/java/com/fasterxml/jackson/databind/deser/DeserializerCache.java
index bdb2144..f434033 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/DeserializerCache.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/DeserializerCache.java
@@ -372,11 +372,19 @@
return factory.createArrayDeserializer(ctxt, (ArrayType) type, beanDesc);
}
if (type.isMapLikeType()) {
- MapLikeType mlt = (MapLikeType) type;
- if (mlt.isTrueMapType()) {
- return factory.createMapDeserializer(ctxt,(MapType) mlt, beanDesc);
+ // 11-Mar-2017, tatu: As per [databind#1554], also need to block
+ // handling as Map if overriden with "as POJO" option.
+ // Ideally we'd determine it bit later on (to allow custom handler checks)
+ // but that won't work for other reasons. So do it here.
+ // (read: rewrite for 3.0)
+ JsonFormat.Value format = beanDesc.findExpectedFormat(null);
+ if ((format == null) || format.getShape() != JsonFormat.Shape.OBJECT) {
+ MapLikeType mlt = (MapLikeType) type;
+ if (mlt.isTrueMapType()) {
+ return factory.createMapDeserializer(ctxt,(MapType) mlt, beanDesc);
+ }
+ return factory.createMapLikeDeserializer(ctxt, mlt, beanDesc);
}
- return factory.createMapLikeDeserializer(ctxt, mlt, beanDesc);
}
if (type.isCollectionLikeType()) {
/* 03-Aug-2012, tatu: As per [databind#40], one exception is if shape
@@ -564,25 +572,20 @@
/**********************************************************
*/
- // NOTE: changed 2.6 -> 2.7 to pass context; no way to make backwards compatible
protected JsonDeserializer<Object> _handleUnknownValueDeserializer(DeserializationContext ctxt, JavaType type)
throws JsonMappingException
{
- /* Let's try to figure out the reason, to give better error
- * messages
- */
+ // Let's try to figure out the reason, to give better error messages
Class<?> rawClass = type.getRawClass();
if (!ClassUtil.isConcrete(rawClass)) {
- ctxt.reportMappingException("Can not find a Value deserializer for abstract type %s", type);
+ return ctxt.reportBadDefinition(type, "Can not find a Value deserializer for abstract type "+type);
}
- ctxt.reportMappingException("Can not find a Value deserializer for type %s", type);
- return null;
+ return ctxt.reportBadDefinition(type, "Can not find a Value deserializer for type "+type);
}
protected KeyDeserializer _handleUnknownKeyDeserializer(DeserializationContext ctxt, JavaType type)
throws JsonMappingException
{
- ctxt.reportMappingException("Can not find a (Map) Key deserializer for type %s", type);
- return null;
+ return ctxt.reportBadDefinition(type, "Can not find a (Map) Key deserializer for type "+type);
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/NullValueProvider.java b/src/main/java/com/fasterxml/jackson/databind/deser/NullValueProvider.java
new file mode 100644
index 0000000..b62834d
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/NullValueProvider.java
@@ -0,0 +1,34 @@
+package com.fasterxml.jackson.databind.deser;
+
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.util.AccessPattern;
+
+/**
+ * Helper interface implemented by classes that are to be used as
+ * null providers during deserialization. Most importantly implemented by
+ * {@link com.fasterxml.jackson.databind.JsonDeserializer} (as a mix-in
+ * interface), but also by converters used to support more configurable
+ * null replacement.
+ *
+ * @since 2.9
+ */
+public interface NullValueProvider
+{
+ /**
+ * Method called to possibly convert incoming `null` token (read via
+ * underlying streaming input source) into other value of type accessor
+ * supports. May return `null`, or value compatible with type binding.
+ *<p>
+ * NOTE: if {@link #getNullAccessPattern()} returns `ALWAYS_NULL` or
+ * `CONSTANT`, this method WILL NOT use provided `ctxt` and it may thus
+ * be passed as `null`.
+ */
+ public Object getNullValue(DeserializationContext ctxt) throws JsonMappingException;
+
+ /**
+ * Accessor that may be used to determine if and when provider must be called to
+ * access null replacement value.
+ */
+ public AccessPattern getNullAccessPattern();
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/SettableAnyProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/SettableAnyProperty.java
index 8ee86d4..3e6329b 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/SettableAnyProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/SettableAnyProperty.java
@@ -10,6 +10,7 @@
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Class that represents a "wildcard" set method which can be used
@@ -42,7 +43,12 @@
protected JsonDeserializer<Object> _valueDeserializer;
protected final TypeDeserializer _valueTypeDeserializer;
-
+
+ /**
+ * @since 2.9
+ */
+ protected final KeyDeserializer _keyDeserializer;
+
/*
/**********************************************************
/* Life-cycle
@@ -50,6 +56,7 @@
*/
public SettableAnyProperty(BeanProperty property, AnnotatedMember setter, JavaType type,
+ KeyDeserializer keyDeser,
JsonDeserializer<Object> valueDeser, TypeDeserializer typeDeser)
{
_property = property;
@@ -57,25 +64,20 @@
_type = type;
_valueDeserializer = valueDeser;
_valueTypeDeserializer = typeDeser;
+ _keyDeserializer = keyDeser;
_setterIsField = setter instanceof AnnotatedField;
}
- /**
- * Constructor used for JDK Serialization when reading persisted object
- */
- protected SettableAnyProperty(SettableAnyProperty src)
+ @Deprecated // since 2.9
+ public SettableAnyProperty(BeanProperty property, AnnotatedMember setter, JavaType type,
+ JsonDeserializer<Object> valueDeser, TypeDeserializer typeDeser)
{
- _property = src._property;
- _setter = src._setter;
- _type = src._type;
- _valueDeserializer = src._valueDeserializer;
- _valueTypeDeserializer = src._valueTypeDeserializer;
- _setterIsField = src._setterIsField;
+ this(property, setter, type, null, valueDeser, typeDeser);
}
public SettableAnyProperty withValueDeserializer(JsonDeserializer<Object> deser) {
return new SettableAnyProperty(_property, _setter, _type,
- deser, _valueTypeDeserializer);
+ _keyDeserializer, deser, _valueTypeDeserializer);
}
public void fixAccess(DeserializationConfig config) {
@@ -127,7 +129,9 @@
throws IOException
{
try {
- set(instance, propName, deserialize(p, ctxt));
+ Object key = (_keyDeserializer == null) ? propName
+ : _keyDeserializer.deserializeKey(propName, ctxt);
+ set(instance, key, deserialize(p, ctxt));
} catch (UnresolvedForwardReference reference) {
if (!(_valueDeserializer.getObjectIdReader() != null)) {
throw JsonMappingException.from(p, "Unresolved forward reference but no identity info.", reference);
@@ -151,7 +155,7 @@
}
@SuppressWarnings("unchecked")
- public void set(Object instance, String propName, Object value) throws IOException
+ public void set(Object instance, Object propName, Object value) throws IOException
{
try {
// if annotation in the field (only map is supported now)
@@ -187,11 +191,11 @@
* @param propName Name of property (from Json input) to set
* @param value Value of the property
*/
- protected void _throwAsIOE(Exception e, String propName, Object value)
+ protected void _throwAsIOE(Exception e, Object propName, Object value)
throws IOException
{
if (e instanceof IllegalArgumentException) {
- String actType = (value == null) ? "[NULL]" : value.getClass().getName();
+ String actType = ClassUtil.classNameOf(value);
StringBuilder msg = new StringBuilder("Problem deserializing \"any\" property '").append(propName);
msg.append("' of class "+getClassName()+" (expected type: ").append(_type);
msg.append("; actual type: ").append(actType).append(")");
@@ -203,17 +207,10 @@
}
throw new JsonMappingException(null, msg.toString(), e);
}
- if (e instanceof IOException) {
- throw (IOException) e;
- }
- if (e instanceof RuntimeException) {
- throw (RuntimeException) e;
- }
+ ClassUtil.throwIfIOE(e);
+ ClassUtil.throwIfRTE(e);
// let's wrap the innermost problem
- Throwable t = e;
- while (t.getCause() != null) {
- t = t.getCause();
- }
+ Throwable t = ClassUtil.getRootCause(e);
throw new JsonMappingException(null, t.getMessage(), t);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/SettableBeanProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/SettableBeanProperty.java
index 65be982..ff77c13 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/SettableBeanProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/SettableBeanProperty.java
@@ -7,10 +7,12 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.impl.FailingDeserializer;
+import com.fasterxml.jackson.databind.deser.impl.NullsConstantProvider;
import com.fasterxml.jackson.databind.introspect.*;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.util.Annotations;
+import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.ViewMatcher;
/**
@@ -33,7 +35,7 @@
*/
protected static final JsonDeserializer<Object> MISSING_VALUE_DESERIALIZER = new FailingDeserializer(
"No _valueDeserializer assigned");
-
+
/**
* Logical name of the property (often but not always derived
* from the setter method name)
@@ -49,14 +51,14 @@
* @since 2.2
*/
protected final PropertyName _wrapperName;
-
+
/**
* Class that contains this property (either class that declares
* the property or one of its subclasses), class that is
* deserialized using deserializer that contains this property.
*/
protected final transient Annotations _contextAnnotations;
-
+
/**
* Deserializer used for handling property value.
*<p>
@@ -71,6 +73,15 @@
*/
protected final TypeDeserializer _valueTypeDeserializer;
+ /**
+ * Entity used for possible translation from `null` into non-null
+ * value of type of this property.
+ * Often same as <code>_valueDeserializer</code>, but not always.
+ *
+ * @since 2.9
+ */
+ protected final NullValueProvider _nullProvider;
+
/*
/**********************************************************
/* Configuration that is not yet immutable; generally assigned
@@ -80,9 +91,8 @@
*/
/**
- * If property represents a managed (forward) reference
- * (see [JACKSON-235]), we will need name of reference for
- * later linking.
+ * If property represents a managed (forward) reference, we will need
+ * the name of reference for later linking.
*<p>
* TODO: should try to make immutable.
*/
@@ -103,7 +113,7 @@
* TODO: should try to make immutable.
*/
protected ViewMatcher _viewMatcher;
-
+
/**
* Index of property (within all property of a bean); assigned
* when all properties have been collected. Order of entries
@@ -127,15 +137,6 @@
contextAnnotations, propDef.getMetadata());
}
- @Deprecated // since 2.3
- protected SettableBeanProperty(String propName, JavaType type, PropertyName wrapper,
- TypeDeserializer typeDeser, Annotations contextAnnotations,
- boolean isRequired)
- {
- this(new PropertyName(propName), type, wrapper, typeDeser, contextAnnotations,
- PropertyMetadata.construct(Boolean.valueOf(isRequired), null, null, null));
- }
-
protected SettableBeanProperty(PropertyName propName, JavaType type, PropertyName wrapper,
TypeDeserializer typeDeser, Annotations contextAnnotations,
PropertyMetadata metadata)
@@ -162,6 +163,7 @@
}
_valueTypeDeserializer = typeDeser;
_valueDeserializer = MISSING_VALUE_DESERIALIZER;
+ _nullProvider = MISSING_VALUE_DESERIALIZER;
}
/**
@@ -185,8 +187,10 @@
_viewMatcher = null;
_valueTypeDeserializer = null;
_valueDeserializer = valueDeser;
+ // 29-Jan-2017, tatu: Presumed to be irrelevant for ObjectId values...
+ _nullProvider = valueDeser;
}
-
+
/**
* Basic copy-constructor for sub-classes to use.
*/
@@ -202,13 +206,15 @@
_managedReferenceName = src._managedReferenceName;
_propertyIndex = src._propertyIndex;
_viewMatcher = src._viewMatcher;
+ _nullProvider = src._nullProvider;
}
/**
* Copy-with-deserializer-change constructor for sub-classes to use.
*/
@SuppressWarnings("unchecked")
- protected SettableBeanProperty(SettableBeanProperty src, JsonDeserializer<?> deser)
+ protected SettableBeanProperty(SettableBeanProperty src,
+ JsonDeserializer<?> deser, NullValueProvider nuller)
{
super(src);
_propName = src._propName;
@@ -225,6 +231,11 @@
_valueDeserializer = (JsonDeserializer<Object>) deser;
}
_viewMatcher = src._viewMatcher;
+ // 29-Jan-2017, tatu: Bit messy, but for now has to do...
+ if (nuller == MISSING_VALUE_DESERIALIZER) {
+ nuller = _valueDeserializer;
+ }
+ _nullProvider = nuller;
}
/**
@@ -242,6 +253,7 @@
_managedReferenceName = src._managedReferenceName;
_propertyIndex = src._propertyIndex;
_viewMatcher = src._viewMatcher;
+ _nullProvider = src._nullProvider;
}
/**
@@ -276,11 +288,11 @@
? new PropertyName(simpleName) : _propName.withSimpleName(simpleName);
return (n == _propName) ? this : withName(n);
}
-
- @Deprecated // since 2.3 -- use 'withSimpleName' instead if need be
- public SettableBeanProperty withName(String simpleName) {
- return withName(new PropertyName(simpleName));
- }
+
+ /**
+ * @since 2.9
+ */
+ public abstract SettableBeanProperty withNullProvider(NullValueProvider nva);
public void setManagedReferenceName(String n) {
_managedReferenceName = n;
@@ -297,7 +309,7 @@
_viewMatcher = ViewMatcher.construct(views);
}
}
-
+
/**
* Method used to assign index for property.
*/
@@ -372,7 +384,7 @@
/**********************************************************
*/
- protected final Class<?> getDeclaringClass() {
+ protected Class<?> getDeclaringClass() {
return getMember().getDeclaringClass();
}
@@ -385,7 +397,7 @@
}
public boolean hasValueTypeDeserializer() { return (_valueTypeDeserializer != null); }
-
+
public JsonDeserializer<Object> getValueDeserializer() {
JsonDeserializer<Object> deser = _valueDeserializer;
if (deser == MISSING_VALUE_DESERIALIZER) {
@@ -396,10 +408,15 @@
public TypeDeserializer getValueTypeDeserializer() { return _valueTypeDeserializer; }
+ /**
+ * @since 2.9
+ */
+ public NullValueProvider getNullValueProvider() { return _nullProvider; }
+
public boolean visibleInView(Class<?> activeView) {
return (_viewMatcher == null) || _viewMatcher.isVisibleForView(activeView);
}
-
+
public boolean hasViews() { return _viewMatcher != null; }
/**
@@ -493,10 +510,8 @@
*/
public final Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
- JsonToken t = p.getCurrentToken();
-
- if (t == JsonToken.VALUE_NULL) {
- return _valueDeserializer.getNullValue(ctxt);
+ if (p.hasToken(JsonToken.VALUE_NULL)) {
+ return _nullProvider.getNullValue(ctxt);
}
if (_valueTypeDeserializer != null) {
return _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
@@ -504,6 +519,31 @@
return _valueDeserializer.deserialize(p, ctxt);
}
+ /**
+ * @since 2.9
+ */
+ public final Object deserializeWith(JsonParser p, DeserializationContext ctxt,
+ Object toUpdate) throws IOException
+ {
+ // 20-Oct-2016, tatu: Not 100% sure what to do; probably best to simply return
+ // null value and let caller decide what to do.
+ if (p.hasToken(JsonToken.VALUE_NULL)) {
+ // ... except for "skip nulls" case which should just do that:
+ if (NullsConstantProvider.isSkipper(_nullProvider)) {
+ return toUpdate;
+ }
+ return _nullProvider.getNullValue(ctxt);
+ }
+ // 20-Oct-2016, tatu: Also tricky -- for now, report an error
+ if (_valueTypeDeserializer != null) {
+ ctxt.reportBadDefinition(getType(),
+ String.format("Can not merge polymorphic property '%s'",
+ getName()));
+// return _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
+ }
+ return _valueDeserializer.deserialize(p, ctxt, toUpdate);
+ }
+
/*
/**********************************************************
/* Helper methods
@@ -517,13 +557,17 @@
protected void _throwAsIOE(JsonParser p, Exception e, Object value) throws IOException
{
if (e instanceof IllegalArgumentException) {
- String actType = (value == null) ? "[NULL]" : value.getClass().getName();
- StringBuilder msg = new StringBuilder("Problem deserializing property '").append(getName());
- msg.append("' (expected type: ").append(getType());
- msg.append("; actual type: ").append(actType).append(")");
+ String actType = ClassUtil.classNameOf(value);
+ StringBuilder msg = new StringBuilder("Problem deserializing property '")
+ .append(getName())
+ .append("' (expected type: ")
+ .append(getType())
+ .append("; actual type: ")
+ .append(actType).append(")");
String origMsg = e.getMessage();
if (origMsg != null) {
- msg.append(", problem: ").append(origMsg);
+ msg.append(", problem: ")
+ .append(origMsg);
} else {
msg.append(" (no error message provided)");
}
@@ -537,17 +581,10 @@
*/
protected IOException _throwAsIOE(JsonParser p, Exception e) throws IOException
{
- if (e instanceof IOException) {
- throw (IOException) e;
- }
- if (e instanceof RuntimeException) {
- throw (RuntimeException) e;
- }
+ ClassUtil.throwIfIOE(e);
+ ClassUtil.throwIfRTE(e);
// let's wrap the innermost problem
- Throwable th = e;
- while (th.getCause() != null) {
- th = th.getCause();
- }
+ Throwable th = ClassUtil.getRootCause(e);
throw JsonMappingException.from(p, th.getMessage(), th);
}
@@ -563,4 +600,160 @@
}
@Override public String toString() { return "[property '"+getName()+"']"; }
+
+ /*
+ /**********************************************************
+ /* Helper classes
+ /**********************************************************
+ */
+
+ /**
+ * Helper class that is designed to both make it easier to sub-class
+ * delegating subtypes and to reduce likelihood of breakage when
+ * new methods are added.
+ *<p>
+ * Class was specifically added to help with {@code Afterburner}
+ * module, but its use is not limited to only support it.
+ *
+ * @since 2.9
+ */
+ public static abstract class Delegating
+ extends SettableBeanProperty
+ {
+ protected final SettableBeanProperty delegate;
+
+ protected Delegating(SettableBeanProperty d) {
+ super(d);
+ delegate = d;
+ }
+
+ /**
+ * Method sub-classes must implement, to construct a new instance
+ * with given delegate.
+ */
+ protected abstract SettableBeanProperty withDelegate(SettableBeanProperty d);
+
+ protected SettableBeanProperty _with(SettableBeanProperty newDelegate) {
+ if (newDelegate == delegate) {
+ return this;
+ }
+ return withDelegate(newDelegate);
+ }
+
+ @Override
+ public SettableBeanProperty withValueDeserializer(JsonDeserializer<?> deser) {
+ return _with(delegate.withValueDeserializer(deser));
+ }
+
+ @Override
+ public SettableBeanProperty withName(PropertyName newName) {
+ return _with(delegate.withName(newName));
+ }
+
+ @Override
+ public SettableBeanProperty withNullProvider(NullValueProvider nva) {
+ return _with(delegate.withNullProvider(nva));
+ }
+
+ @Override
+ public void assignIndex(int index) {
+ delegate.assignIndex(index);
+ }
+
+ @Override
+ public void fixAccess(DeserializationConfig config) {
+ delegate.fixAccess(config);
+ }
+
+ /*
+ /**********************************************************
+ /* Accessors
+ /**********************************************************
+ */
+
+ @Override
+ protected Class<?> getDeclaringClass() { return delegate.getDeclaringClass(); }
+
+ @Override
+ public String getManagedReferenceName() { return delegate.getManagedReferenceName(); }
+
+ @Override
+ public ObjectIdInfo getObjectIdInfo() { return delegate.getObjectIdInfo(); }
+
+ @Override
+ public boolean hasValueDeserializer() { return delegate.hasValueDeserializer(); }
+
+ @Override
+ public boolean hasValueTypeDeserializer() { return delegate.hasValueTypeDeserializer(); }
+
+ @Override
+ public JsonDeserializer<Object> getValueDeserializer() { return delegate.getValueDeserializer(); }
+
+ @Override
+ public TypeDeserializer getValueTypeDeserializer() { return delegate.getValueTypeDeserializer(); }
+
+ @Override
+ public boolean visibleInView(Class<?> activeView) { return delegate.visibleInView(activeView); }
+
+ @Override
+ public boolean hasViews() { return delegate.hasViews(); }
+
+ @Override
+ public int getPropertyIndex() { return delegate.getPropertyIndex(); }
+
+ @Override
+ public int getCreatorIndex() { return delegate.getCreatorIndex(); }
+
+ @Override
+ public Object getInjectableValueId() { return delegate.getInjectableValueId(); }
+
+ @Override
+ public AnnotatedMember getMember() {
+ return delegate.getMember();
+ }
+
+ @Override
+ public <A extends Annotation> A getAnnotation(Class<A> acls) {
+ return delegate.getAnnotation(acls);
+ }
+
+ /*
+ /**********************************************************
+ /* Extended API
+ /**********************************************************
+ */
+
+ public SettableBeanProperty getDelegate() {
+ return delegate;
+ }
+
+ /*
+ /**********************************************************
+ /* Actual mutators
+ /**********************************************************
+ */
+
+ @Override
+ public void deserializeAndSet(JsonParser p, DeserializationContext ctxt,
+ Object instance) throws IOException {
+ delegate.deserializeAndSet(p, ctxt, instance);
+ }
+
+ @Override
+ public Object deserializeSetAndReturn(JsonParser p,
+ DeserializationContext ctxt, Object instance) throws IOException
+ {
+ return delegate.deserializeSetAndReturn(p, ctxt, instance);
+ }
+
+ @Override
+ public void set(Object instance, Object value) throws IOException {
+ delegate.set(instance, value);
+ }
+
+ @Override
+ public Object setAndReturn(Object instance, Object value) throws IOException {
+ return delegate.setAndReturn(instance, value);
+ }
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/UnresolvedId.java b/src/main/java/com/fasterxml/jackson/databind/deser/UnresolvedId.java
index c828616..af5772a 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/UnresolvedId.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/UnresolvedId.java
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.databind.deser;
import com.fasterxml.jackson.core.JsonLocation;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Helper class for {@link UnresolvedForwardReference}, to contain information about unresolved ids.
@@ -32,6 +33,6 @@
@Override
public String toString() {
return String.format("Object id [%s] (for %s) at %s", _id,
- (_type == null) ? "NULL" : _type.getName(), _location);
+ ClassUtil.nameOf(_type), _location);
}
-}
\ No newline at end of file
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java b/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java
index dfc62cf..ffb358f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java
@@ -185,7 +185,7 @@
* null or empty List.
*/
public Object createUsingDefault(DeserializationContext ctxt) throws IOException {
- return ctxt.handleMissingInstantiator(getValueClass(), ctxt.getParser(),
+ return ctxt.handleMissingInstantiator(getValueClass(), this, null,
"no default no-arguments constructor found");
}
@@ -199,7 +199,7 @@
*/
public Object createFromObjectWith(DeserializationContext ctxt, Object[] args) throws IOException {
// sanity check; shouldn't really get called if no Creator specified
- return ctxt.handleMissingInstantiator(getValueClass(), ctxt.getParser(),
+ return ctxt.handleMissingInstantiator(getValueClass(), this, null,
"no creator with arguments specified");
}
@@ -233,7 +233,7 @@
* an intermediate "delegate" value to pass to createor method
*/
public Object createUsingDelegate(DeserializationContext ctxt, Object delegate) throws IOException {
- return ctxt.handleMissingInstantiator(getValueClass(), ctxt.getParser(),
+ return ctxt.handleMissingInstantiator(getValueClass(), this, null,
"no delegate creator specified");
}
@@ -242,7 +242,7 @@
* an intermediate "delegate" value to pass to createor method
*/
public Object createUsingArrayDelegate(DeserializationContext ctxt, Object delegate) throws IOException {
- return ctxt.handleMissingInstantiator(getValueClass(), ctxt.getParser(),
+ return ctxt.handleMissingInstantiator(getValueClass(), this, null,
"no array delegate creator specified");
}
@@ -258,25 +258,25 @@
}
public Object createFromInt(DeserializationContext ctxt, int value) throws IOException {
- return ctxt.handleMissingInstantiator(getValueClass(), ctxt.getParser(),
+ return ctxt.handleMissingInstantiator(getValueClass(), this, null,
"no int/Int-argument constructor/factory method to deserialize from Number value (%s)",
value);
}
public Object createFromLong(DeserializationContext ctxt, long value) throws IOException {
- return ctxt.handleMissingInstantiator(getValueClass(), ctxt.getParser(),
+ return ctxt.handleMissingInstantiator(getValueClass(), this, null,
"no long/Long-argument constructor/factory method to deserialize from Number value (%s)",
value);
}
public Object createFromDouble(DeserializationContext ctxt, double value) throws IOException {
- return ctxt.handleMissingInstantiator(getValueClass(), ctxt.getParser(),
+ return ctxt.handleMissingInstantiator(getValueClass(), this, null,
"no double/Double-argument constructor/factory method to deserialize from Number value (%s)",
value);
}
public Object createFromBoolean(DeserializationContext ctxt, boolean value) throws IOException {
- return ctxt.handleMissingInstantiator(getValueClass(), ctxt.getParser(),
+ return ctxt.handleMissingInstantiator(getValueClass(), this, null,
"no boolean/Boolean-argument constructor/factory method to deserialize from boolean value (%s)",
value);
}
@@ -367,13 +367,26 @@
return null;
}
}
- return ctxt.handleMissingInstantiator(getValueClass(), ctxt.getParser(),
+ return ctxt.handleMissingInstantiator(getValueClass(), this, ctxt.getParser(),
"no String-argument constructor/factory method to deserialize from String value ('%s')",
value);
}
/*
/**********************************************************
+ /* Introspection
+ /**********************************************************
+ */
+
+ /**
+ * @since 2.9
+ */
+ public interface Gettable {
+ public ValueInstantiator getValueInstantiator();
+ }
+
+ /*
+ /**********************************************************
/* Standard Base implementation (since 2.8)
/**********************************************************
*/
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanAsArrayBuilderDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanAsArrayBuilderDeserializer.java
index dabe451..4c20cc6 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanAsArrayBuilderDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanAsArrayBuilderDeserializer.java
@@ -25,7 +25,15 @@
final protected SettableBeanProperty[] _orderedProperties;
final protected AnnotatedMethod _buildMethod;
-
+
+ /**
+ * Type that the builder will produce, target type; as opposed to
+ * `handledType()` which refers to Builder class.
+ *
+ * @since 2.9
+ */
+ protected final JavaType _targetType;
+
/*
/**********************************************************
/* Life-cycle, construction, initialization
@@ -36,13 +44,17 @@
* Main constructor used both for creating new instances (by
* {@link BeanDeserializer#asArrayDeserializer}) and for
* creating copies with different delegate.
+ *
+ * @since 2.9
*/
public BeanAsArrayBuilderDeserializer(BeanDeserializerBase delegate,
+ JavaType targetType,
SettableBeanProperty[] ordered,
AnnotatedMethod buildMethod)
{
super(delegate);
_delegate = delegate;
+ _targetType = targetType;
_orderedProperties = ordered;
_buildMethod = buildMethod;
}
@@ -60,19 +72,19 @@
@Override
public BeanDeserializerBase withObjectIdReader(ObjectIdReader oir) {
return new BeanAsArrayBuilderDeserializer(_delegate.withObjectIdReader(oir),
- _orderedProperties, _buildMethod);
+ _targetType, _orderedProperties, _buildMethod);
}
@Override
public BeanDeserializerBase withIgnorableProperties(Set<String> ignorableProps) {
return new BeanAsArrayBuilderDeserializer(_delegate.withIgnorableProperties(ignorableProps),
- _orderedProperties, _buildMethod);
+ _targetType, _orderedProperties, _buildMethod);
}
@Override
public BeanDeserializerBase withBeanProperties(BeanPropertyMap props) {
return new BeanAsArrayBuilderDeserializer(_delegate.withBeanProperties(props),
- _orderedProperties, _buildMethod);
+ _targetType, _orderedProperties, _buildMethod);
}
@Override
@@ -82,6 +94,18 @@
/*
/**********************************************************
+ /* Overrides
+ /**********************************************************
+ */
+
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ // 26-Oct-2016, tatu: No, we can't merge Builder-based POJOs as of now
+ return Boolean.FALSE;
+ }
+
+ /*
+ /**********************************************************
/* JsonDeserializer implementation
/**********************************************************
*/
@@ -90,7 +114,7 @@
throws IOException
{
try {
- return _buildMethod.getMember().invoke(builder);
+ return _buildMethod.getMember().invoke(builder, (Object[]) null);
} catch (Exception e) {
return wrapInstantiationProblem(e, ctxt);
}
@@ -130,9 +154,11 @@
}
++i;
}
- // Ok; extra fields? Let's fail, unless ignoring extra props is fine
- if (!_ignoreAllUnknown) {
- ctxt.reportMappingException("Unexpected JSON values; expected at most %d properties (in JSON Array)",
+ // 09-Nov-2016, tatu: Should call `handleUnknownProperty()` in Context, but it'd give
+ // non-optimal exception message so...
+ if (!_ignoreAllUnknown && ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
+ ctxt.reportInputMismatch(handledType(),
+ "Unexpected JSON values; expected at most %d properties (in JSON Array)",
propCount);
// fall through
}
@@ -144,50 +170,11 @@
}
@Override
- public Object deserialize(JsonParser p, DeserializationContext ctxt, Object builder)
+ public Object deserialize(JsonParser p, DeserializationContext ctxt, Object value)
throws IOException
{
- /* No good way to verify that we have an array... although could I guess
- * check via JsonParser. So let's assume everything is working fine, for now.
- */
- if (_injectables != null) {
- injectValues(ctxt, builder);
- }
- final SettableBeanProperty[] props = _orderedProperties;
- int i = 0;
- final int propCount = props.length;
- while (true) {
- if (p.nextToken() == JsonToken.END_ARRAY) {
- return finishBuild(ctxt, builder);
- }
- if (i == propCount) {
- break;
- }
- SettableBeanProperty prop = props[i];
- if (prop != null) { // normal case
- try {
- builder = prop.deserializeSetAndReturn(p, ctxt, builder);
- } catch (Exception e) {
- wrapAndThrow(e, builder, prop.getName(), ctxt);
- }
- } else { // just skip?
- p.skipChildren();
- }
- ++i;
- }
-
- // Ok; extra fields? Let's fail, unless ignoring extra props is fine
- if (!_ignoreAllUnknown) {
- ctxt.reportWrongTokenException(p, JsonToken.END_ARRAY,
- "Unexpected JSON values; expected at most %d properties (in JSON Array)",
- propCount);
- // never gets here
- }
- // otherwise, skip until end
- do {
- p.skipChildren();
- } while (p.nextToken() != JsonToken.END_ARRAY);
- return finishBuild(ctxt, builder);
+ // 26-Oct-2016, tatu: Will fail, but let the original deserializer provide message
+ return _delegate.deserialize(p, ctxt, value);
}
// needed since 2.1
@@ -247,8 +234,8 @@
p.skipChildren();
}
// Ok; extra fields? Let's fail, unless ignoring extra props is fine
- if (!_ignoreAllUnknown) {
- ctxt.reportWrongTokenException(p, JsonToken.END_ARRAY,
+ if (!_ignoreAllUnknown && ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
+ ctxt.reportWrongTokenException(this, JsonToken.END_ARRAY,
"Unexpected JSON value(s); expected at most %d properties (in JSON Array)",
propCount);
// will never reach here as exception has been thrown
@@ -278,6 +265,7 @@
final SettableBeanProperty[] props = _orderedProperties;
final int propCount = props.length;
+ final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
int i = 0;
Object builder = null;
@@ -287,6 +275,10 @@
p.skipChildren();
continue;
}
+ if ((activeView != null) && !prop.visibleInView(activeView)) {
+ p.skipChildren();
+ continue;
+ }
// if we have already constructed POJO, things are simple:
if (builder != null) {
try {
@@ -314,10 +306,9 @@
* supported (since ordering of elements may not be guaranteed);
* but make explicitly non-supported for now.
*/
- ctxt.reportMappingException("Can not support implicit polymorphic deserialization for POJOs-as-Arrays style: "
- +"nominal type %s, actual type %s",
- _beanType.getRawClass().getName(), builder.getClass().getName());
- return null;
+ return ctxt.reportBadDefinition(_beanType, String.format(
+"Can not support implicit polymorphic deserialization for POJOs-as-Arrays style: nominal type %s, actual type %s",
+ _beanType.getRawClass().getName(), builder.getClass().getName()));
}
}
continue;
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanAsArrayDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanAsArrayDeserializer.java
index 1cff5ca..0e08d09 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanAsArrayDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanAsArrayDeserializer.java
@@ -126,8 +126,8 @@
++i;
}
// Ok; extra fields? Let's fail, unless ignoring extra props is fine
- if (!_ignoreAllUnknown) {
- ctxt.reportWrongTokenException(p, JsonToken.END_ARRAY,
+ if (!_ignoreAllUnknown && ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
+ ctxt.reportWrongTokenException(this, JsonToken.END_ARRAY,
"Unexpected JSON values; expected at most %d properties (in JSON Array)",
propCount);
// never gets here
@@ -145,6 +145,11 @@
{
// [databind#631]: Assign current value, to be accessible by custom serializers
p.setCurrentValue(bean);
+
+ if (!p.isExpectedStartArrayToken()) {
+ return _deserializeFromNonArray(p, ctxt);
+ }
+
/* No good way to verify that we have an array... although could I guess
* check via JsonParser. So let's assume everything is working fine, for now.
*/
@@ -175,8 +180,8 @@
}
// Ok; extra fields? Let's fail, unless ignoring extra props is fine
- if (!_ignoreAllUnknown) {
- ctxt.reportWrongTokenException(p, JsonToken.END_ARRAY,
+ if (!_ignoreAllUnknown && ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
+ ctxt.reportWrongTokenException(this, JsonToken.END_ARRAY,
"Unexpected JSON values; expected at most %d properties (in JSON Array)",
propCount);
// never gets here
@@ -222,6 +227,7 @@
final SettableBeanProperty[] props = _orderedProperties;
int i = 0;
final int propCount = props.length;
+
while (true) {
if (p.nextToken() == JsonToken.END_ARRAY) {
return bean;
@@ -246,7 +252,7 @@
}
// Ok; extra fields? Let's fail, unless ignoring extra props is fine
if (!_ignoreAllUnknown) {
- ctxt.reportWrongTokenException(p, JsonToken.END_ARRAY,
+ ctxt.reportWrongTokenException(this, JsonToken.END_ARRAY,
"Unexpected JSON values; expected at most %d properties (in JSON Array)",
propCount);
// will never reach here as exception has been thrown
@@ -277,13 +283,19 @@
final int propCount = props.length;
int i = 0;
Object bean = null;
-
+ final Class<?> activeView = _needViewProcesing ? ctxt.getActiveView() : null;
+
for (; p.nextToken() != JsonToken.END_ARRAY; ++i) {
SettableBeanProperty prop = (i < propCount) ? props[i] : null;
if (prop == null) { // we get null if there are extra elements; maybe otherwise too?
p.skipChildren();
continue;
}
+ if ((activeView != null) && !prop.visibleInView(activeView)) {
+ p.skipChildren();
+ continue;
+ }
+
// if we have already constructed POJO, things are simple:
if (bean != null) {
try {
@@ -314,9 +326,10 @@
* supported (since ordering of elements may not be guaranteed);
* but make explicitly non-supported for now.
*/
- ctxt.reportMappingException("Can not support implicit polymorphic deserialization for POJOs-as-Arrays style: "
+ ctxt.reportBadDefinition(_beanType, String.format(
+ "Can not support implicit polymorphic deserialization for POJOs-as-Arrays style: "
+"nominal type %s, actual type %s",
- _beanType.getRawClass().getName(), bean.getClass().getName());
+ _beanType.getRawClass().getName(), bean.getClass().getName()));
}
}
continue;
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanPropertyMap.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanPropertyMap.java
index 8031ee0..9308fb7 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanPropertyMap.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/BeanPropertyMap.java
@@ -10,7 +10,9 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
+import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.NameTransformer;
/**
@@ -55,22 +57,54 @@
*/
private SettableBeanProperty[] _propsInOrder;
- public BeanPropertyMap(boolean caseInsensitive, Collection<SettableBeanProperty> props)
+ /**
+ * Configuration of alias mappings, indexed by unmodified property name
+ * to unmodified aliases, if any; entries only included for properties
+ * that do have aliases.
+ * This is is used for constructing actual reverse lookup mapping, if
+ * needed, taking into account possible case-insensitivity, as well
+ * as possibility of name prefixes.
+ *
+ * @since 2.9
+ */
+ private final Map<String,List<PropertyName>> _aliasDefs;
+
+ /**
+ * Mapping from secondary names (aliases) to primary names.
+ *
+ * @since 2.9
+ */
+ private final Map<String,String> _aliasMapping;
+
+ /**
+ * @since 2.9
+ */
+ public BeanPropertyMap(boolean caseInsensitive, Collection<SettableBeanProperty> props,
+ Map<String,List<PropertyName>> aliasDefs)
{
_caseInsensitive = caseInsensitive;
_propsInOrder = props.toArray(new SettableBeanProperty[props.size()]);
+ _aliasDefs = aliasDefs;
+ _aliasMapping = _buildAliasMapping(aliasDefs);
init(props);
}
+ @Deprecated // since 2.8
+ public BeanPropertyMap(boolean caseInsensitive, Collection<SettableBeanProperty> props)
+ {
+ this(caseInsensitive, props, Collections.<String,List<PropertyName>>emptyMap());
+ }
+
/**
* @since 2.8
*/
protected BeanPropertyMap(BeanPropertyMap base, boolean caseInsensitive)
{
_caseInsensitive = caseInsensitive;
+ _aliasDefs = base._aliasDefs;
+ _aliasMapping = base._aliasMapping;
- // 16-May-2016, tatu: Alas, not enough to just change flag, need to re-init
- // as well.
+ // 16-May-2016, tatu: Alas, not enough to just change flag, need to re-init as well.
_propsInOrder = Arrays.copyOf(base._propsInOrder, base._propsInOrder.length);
init(Arrays.asList(_propsInOrder));
}
@@ -107,7 +141,7 @@
if (prop == null) {
continue;
}
-
+
String key = getPropertyName(prop);
int slot = _hashCode(key);
int ix = (slot<<1);
@@ -128,6 +162,8 @@
//System.err.println(" add '"+key+" at #"+(ix>>1)+"/"+size+" (hashed at "+slot+")");
hashed[ix] = key;
hashed[ix+1] = prop;
+
+ // and aliases
}
/*
for (int i = 0; i < hashed.length; i += 2) {
@@ -137,7 +173,7 @@
_hashArea = hashed;
_spillCount = spillCount;
}
-
+
private final static int findSize(int size)
{
if (size <= 5) {
@@ -157,10 +193,17 @@
/**
* @since 2.6
*/
- public static BeanPropertyMap construct(Collection<SettableBeanProperty> props, boolean caseInsensitive) {
- return new BeanPropertyMap(caseInsensitive, props);
+ public static BeanPropertyMap construct(Collection<SettableBeanProperty> props,
+ boolean caseInsensitive, Map<String,List<PropertyName>> aliasMapping) {
+ return new BeanPropertyMap(caseInsensitive, props, aliasMapping);
}
-
+
+ @Deprecated // since 2.9
+ public static BeanPropertyMap construct(Collection<SettableBeanProperty> props, boolean caseInsensitive) {
+ return construct(props, caseInsensitive,
+ Collections.<String,List<PropertyName>>emptyMap());
+ }
+
/**
* Fluent copy method that creates a new instance that is a copy
* of this instance except for one additional property that is
@@ -258,9 +301,16 @@
newProps.add(_rename(prop, transformer));
}
// should we try to re-index? Ordering probably changed but caller probably doesn't want changes...
- return new BeanPropertyMap(_caseInsensitive, newProps);
+ // 26-Feb-2017, tatu: Probably SHOULD handle renaming wrt Aliases?
+ return new BeanPropertyMap(_caseInsensitive, newProps, _aliasDefs);
}
+ /*
+ /**********************************************************
+ /* Public API, mutators
+ /**********************************************************
+ */
+
/**
* Mutant factory method that will use this instance as the base, and
* construct an instance that is otherwise same except for excluding
@@ -288,9 +338,9 @@
}
}
// should we try to re-index? Apparently no need
- return new BeanPropertyMap(_caseInsensitive, newProps);
+ return new BeanPropertyMap(_caseInsensitive, newProps, _aliasDefs);
}
-
+
/**
* Specialized method that can be used to replace an existing entry
* (note: entry MUST exist; otherwise exception is thrown) with
@@ -300,125 +350,15 @@
{
String key = getPropertyName(newProp);
int ix = _findIndexInHash(key);
-
- if (ix >= 0) {
- SettableBeanProperty prop = (SettableBeanProperty) _hashArea[ix];
- _hashArea[ix] = newProp;
- // also, replace in in-order
- _propsInOrder[_findFromOrdered(prop)] = newProp;
- return;
+ if (ix < 0) {
+ throw new NoSuchElementException("No entry '"+key+"' found, can't replace");
}
-
- throw new NoSuchElementException("No entry '"+key+"' found, can't replace");
+ SettableBeanProperty prop = (SettableBeanProperty) _hashArea[ix];
+ _hashArea[ix] = newProp;
+ // also, replace in in-order
+ _propsInOrder[_findFromOrdered(prop)] = newProp;
}
- private List<SettableBeanProperty> properties() {
- ArrayList<SettableBeanProperty> p = new ArrayList<SettableBeanProperty>(_size);
- for (int i = 1, end = _hashArea.length; i < end; i += 2) {
- SettableBeanProperty prop = (SettableBeanProperty) _hashArea[i];
- if (prop != null) {
- p.add(prop);
- }
- }
- return p;
- }
-
- /**
- * Accessor for traversing over all contained properties.
- */
- @Override
- public Iterator<SettableBeanProperty> iterator() {
- return properties().iterator();
- }
-
- /**
- * Method that will re-create initial insertion-ordering of
- * properties contained in this map. Note that if properties
- * have been removed, array may contain nulls; otherwise
- * it should be consecutive.
- *
- * @since 2.1
- */
- public SettableBeanProperty[] getPropertiesInInsertionOrder() {
- return _propsInOrder;
- }
-
- // Confining this case insensitivity to this function (and the find method) in case we want to
- // apply a particular locale to the lower case function. For now, using the default.
- protected final String getPropertyName(SettableBeanProperty prop) {
- return _caseInsensitive ? prop.getName().toLowerCase() : prop.getName();
- }
-
- /**
- * @since 2.3
- */
- public SettableBeanProperty find(int index)
- {
- // note: will scan the whole area, including primary, secondary and
- // possible spill-area
- for (int i = 1, end = _hashArea.length; i < end; i += 2) {
- SettableBeanProperty prop = (SettableBeanProperty) _hashArea[i];
- if ((prop != null) && (index == prop.getPropertyIndex())) {
- return prop;
- }
- }
- return null;
- }
-
- public SettableBeanProperty find(String key)
- {
- if (key == null) {
- throw new IllegalArgumentException("Can not pass null property name");
- }
- if (_caseInsensitive) {
- key = key.toLowerCase();
- }
-
- // inlined `_hashCode(key)`
- int slot = key.hashCode() & _hashMask;
-// int h = key.hashCode();
-// int slot = (h + (h >> 13)) & _hashMask;
-
- int ix = (slot<<1);
- Object match = _hashArea[ix];
- if ((match == key) || key.equals(match)) {
- return (SettableBeanProperty) _hashArea[ix+1];
- }
- return _find2(key, slot, match);
- }
-
- private final SettableBeanProperty _find2(String key, int slot, Object match)
- {
- if (match == null) {
- return null;
- }
- // no? secondary?
- int hashSize = _hashMask+1;
- int ix = hashSize + (slot>>1) << 1;
- match = _hashArea[ix];
- if (key.equals(match)) {
- return (SettableBeanProperty) _hashArea[ix+1];
- }
- if (match != null) { // _findFromSpill(...)
- int i = (hashSize + (hashSize>>1)) << 1;
- for (int end = i + _spillCount; i < end; i += 2) {
- match = _hashArea[i];
- if ((match == key) || key.equals(match)) {
- return (SettableBeanProperty) _hashArea[i+1];
- }
- }
- }
- return null;
- }
-
- /*
- /**********************************************************
- /* Public API
- /**********************************************************
- */
-
- public int size() { return _size; }
-
/**
* Specialized method for removing specified existing entry.
* NOTE: entry MUST exist, otherwise an exception is thrown.
@@ -452,6 +392,181 @@
init(props);
}
+ /*
+ /**********************************************************
+ /* Public API, simple accessors
+ /**********************************************************
+ */
+
+ public int size() { return _size; }
+
+ /**
+ * @since 2.9
+ */
+ public boolean isCaseInsensitive() {
+ return _caseInsensitive;
+ }
+
+ /**
+ * @since 2.9
+ */
+ public boolean hasAliases() {
+ return !_aliasDefs.isEmpty();
+ }
+
+ /**
+ * Accessor for traversing over all contained properties.
+ */
+ @Override
+ public Iterator<SettableBeanProperty> iterator() {
+ return _properties().iterator();
+ }
+
+ private List<SettableBeanProperty> _properties() {
+ ArrayList<SettableBeanProperty> p = new ArrayList<SettableBeanProperty>(_size);
+ for (int i = 1, end = _hashArea.length; i < end; i += 2) {
+ SettableBeanProperty prop = (SettableBeanProperty) _hashArea[i];
+ if (prop != null) {
+ p.add(prop);
+ }
+ }
+ return p;
+ }
+
+ /**
+ * Method that will re-create initial insertion-ordering of
+ * properties contained in this map. Note that if properties
+ * have been removed, array may contain nulls; otherwise
+ * it should be consecutive.
+ *
+ * @since 2.1
+ */
+ public SettableBeanProperty[] getPropertiesInInsertionOrder() {
+ return _propsInOrder;
+ }
+
+ // Confining this case insensitivity to this function (and the find method) in case we want to
+ // apply a particular locale to the lower case function. For now, using the default.
+ protected final String getPropertyName(SettableBeanProperty prop) {
+ return _caseInsensitive ? prop.getName().toLowerCase() : prop.getName();
+ }
+
+ /*
+ /**********************************************************
+ /* Public API, property lookup
+ /**********************************************************
+ */
+
+ /**
+ * @since 2.3
+ */
+ public SettableBeanProperty find(int index)
+ {
+ // note: will scan the whole area, including primary, secondary and
+ // possible spill-area
+ for (int i = 1, end = _hashArea.length; i < end; i += 2) {
+ SettableBeanProperty prop = (SettableBeanProperty) _hashArea[i];
+ if ((prop != null) && (index == prop.getPropertyIndex())) {
+ return prop;
+ }
+ }
+ return null;
+ }
+
+ public SettableBeanProperty find(String key)
+ {
+ if (key == null) {
+ throw new IllegalArgumentException("Can not pass null property name");
+ }
+ if (_caseInsensitive) {
+ key = key.toLowerCase();
+ }
+
+ // inlined `_hashCode(key)`
+ int slot = key.hashCode() & _hashMask;
+// int h = key.hashCode();
+// int slot = (h + (h >> 13)) & _hashMask;
+
+ int ix = (slot<<1);
+ Object match = _hashArea[ix];
+ if ((match == key) || key.equals(match)) {
+ return (SettableBeanProperty) _hashArea[ix+1];
+ }
+ return _find2(key, slot, match);
+ }
+
+ private final SettableBeanProperty _find2(String key, int slot, Object match)
+ {
+ if (match == null) {
+ // 26-Feb-2017, tatu: Need to consider aliases
+ return _findWithAlias(_aliasMapping.get(key));
+ }
+ // no? secondary?
+ int hashSize = _hashMask+1;
+ int ix = hashSize + (slot>>1) << 1;
+ match = _hashArea[ix];
+ if (key.equals(match)) {
+ return (SettableBeanProperty) _hashArea[ix+1];
+ }
+ if (match != null) { // _findFromSpill(...)
+ int i = (hashSize + (hashSize>>1)) << 1;
+ for (int end = i + _spillCount; i < end; i += 2) {
+ match = _hashArea[i];
+ if ((match == key) || key.equals(match)) {
+ return (SettableBeanProperty) _hashArea[i+1];
+ }
+ }
+ }
+ // 26-Feb-2017, tatu: Need to consider aliases
+ return _findWithAlias(_aliasMapping.get(key));
+ }
+
+ private SettableBeanProperty _findWithAlias(String keyFromAlias)
+ {
+ if (keyFromAlias == null) {
+ return null;
+ }
+ // NOTE: need to inline much of handling do avoid cyclic calls via alias
+ // first, inlined main `find(String)`
+ int slot = _hashCode(keyFromAlias);
+ int ix = (slot<<1);
+ Object match = _hashArea[ix];
+ if (keyFromAlias.equals(match)) {
+ return (SettableBeanProperty) _hashArea[ix+1];
+ }
+ if (match == null) {
+ return null;
+ }
+ return _find2ViaAlias(keyFromAlias, slot, match);
+ }
+
+ private SettableBeanProperty _find2ViaAlias(String key, int slot, Object match)
+ {
+ // no? secondary?
+ int hashSize = _hashMask+1;
+ int ix = hashSize + (slot>>1) << 1;
+ match = _hashArea[ix];
+ if (key.equals(match)) {
+ return (SettableBeanProperty) _hashArea[ix+1];
+ }
+ if (match != null) { // _findFromSpill(...)
+ int i = (hashSize + (hashSize>>1)) << 1;
+ for (int end = i + _spillCount; i < end; i += 2) {
+ match = _hashArea[i];
+ if ((match == key) || key.equals(match)) {
+ return (SettableBeanProperty) _hashArea[i+1];
+ }
+ }
+ }
+ return null;
+ }
+
+ /*
+ /**********************************************************
+ /* Public API, deserialization support
+ /**********************************************************
+ */
+
/**
* Convenience method that tries to find property with given name, and
* if it is found, call {@link SettableBeanProperty#deserializeAndSet}
@@ -476,6 +591,12 @@
return true;
}
+ /*
+ /**********************************************************
+ /* Std method overrides
+ /**********************************************************
+ */
+
@Override
public String toString()
{
@@ -495,6 +616,11 @@
sb.append(')');
}
sb.append(']');
+ if (!_aliasDefs.isEmpty()) {
+ sb.append("(aliases: ");
+ sb.append(_aliasDefs);
+ sb.append(")");
+ }
return sb.toString();
}
@@ -531,9 +657,7 @@
t = t.getCause();
}
// Errors to be passed as is
- if (t instanceof Error) {
- throw (Error) t;
- }
+ ClassUtil.throwIfError(t);
// StackOverflowErrors are tricky ones; need to be careful...
boolean wrap = (ctxt == null) || ctxt.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS);
// Ditto for IOExceptions; except we may want to wrap JSON exceptions
@@ -542,9 +666,7 @@
throw (IOException) t;
}
} else if (!wrap) { // allow disabling wrapping for unchecked exceptions
- if (t instanceof RuntimeException) {
- throw (RuntimeException) t;
- }
+ ClassUtil.throwIfRTE(t);
}
throw JsonMappingException.wrapWithPath(t, bean, fieldName);
}
@@ -604,4 +726,27 @@
*/
return key.hashCode() & _hashMask;
}
+
+ // @since 2.9
+ private Map<String,String> _buildAliasMapping(Map<String,List<PropertyName>> defs)
+ {
+ if ((defs == null) || defs.isEmpty()) {
+ return Collections.emptyMap();
+ }
+ Map<String,String> aliases = new HashMap<>();
+ for (Map.Entry<String,List<PropertyName>> entry : defs.entrySet()) {
+ String key = entry.getKey();
+ if (_caseInsensitive) {
+ key = key.toLowerCase();
+ }
+ for (PropertyName pn : entry.getValue()) {
+ String mapped = pn.getSimpleName();
+ if (_caseInsensitive) {
+ mapped = mapped.toLowerCase();
+ }
+ aliases.put(mapped, key);
+ }
+ }
+ return aliases;
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/CreatorCollector.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/CreatorCollector.java
index 2999341..14f0755 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/CreatorCollector.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/CreatorCollector.java
@@ -7,7 +7,6 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
-import com.fasterxml.jackson.databind.deser.CreatorProperty;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator;
@@ -72,8 +71,9 @@
protected AnnotatedParameter _incompleteParameter;
/*
- * /********************************************************** /* Life-cycle
- * /**********************************************************
+ /**********************************************************
+ /* Life-cycle
+ /**********************************************************
*/
public CreatorCollector(BeanDescription beanDesc, MapperConfig<?> config) {
@@ -113,8 +113,9 @@
}
/*
- * /********************************************************** /* Setters
- * /**********************************************************
+ /**********************************************************
+ /* Setters
+ /**********************************************************
*/
/**
@@ -199,48 +200,10 @@
}
}
- // Bunch of methods deprecated in 2.5, to be removed from 2.6 or later
-
- @Deprecated // since 2.5
- public void addStringCreator(AnnotatedWithParams creator) {
- addStringCreator(creator, false);
- }
-
- @Deprecated // since 2.5
- public void addIntCreator(AnnotatedWithParams creator) {
- addBooleanCreator(creator, false);
- }
-
- @Deprecated // since 2.5
- public void addLongCreator(AnnotatedWithParams creator) {
- addBooleanCreator(creator, false);
- }
-
- @Deprecated // since 2.5
- public void addDoubleCreator(AnnotatedWithParams creator) {
- addBooleanCreator(creator, false);
- }
-
- @Deprecated // since 2.5
- public void addBooleanCreator(AnnotatedWithParams creator) {
- addBooleanCreator(creator, false);
- }
-
- @Deprecated // since 2.5
- public void addDelegatingCreator(AnnotatedWithParams creator,
- CreatorProperty[] injectables) {
- addDelegatingCreator(creator, false, injectables);
- }
-
- @Deprecated // since 2.5
- public void addPropertyCreator(AnnotatedWithParams creator,
- CreatorProperty[] properties) {
- addPropertyCreator(creator, false, properties);
- }
-
/*
- * /********************************************************** /* Accessors
- * /**********************************************************
+ /**********************************************************
+ /* Accessors
+ /**********************************************************
*/
/**
@@ -265,8 +228,9 @@
}
/*
- * /********************************************************** /* Helper
- * methods /**********************************************************
+ /**********************************************************
+ /* Helper methods
+ /**********************************************************
*/
private JavaType _computeDelegateType(AnnotatedWithParams creator,
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ExternalTypeHandler.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ExternalTypeHandler.java
index 4069f38..1be53a2 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ExternalTypeHandler.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ExternalTypeHandler.java
@@ -18,16 +18,27 @@
*/
public class ExternalTypeHandler
{
+ private final JavaType _beanType;
+
private final ExtTypedProperty[] _properties;
- private final HashMap<String, Integer> _nameToPropertyIndex;
+
+ /**
+ * Mapping from external property ids to one or more indexes;
+ * in most cases single index as <code>Integer</code>, but
+ * occasionally same name maps to multiple ones: if so,
+ * <code>List<Integer></code>.
+ */
+ private final Map<String, Object> _nameToPropertyIndex;
private final String[] _typeIds;
private final TokenBuffer[] _tokens;
- protected ExternalTypeHandler(ExtTypedProperty[] properties,
- HashMap<String, Integer> nameToPropertyIndex,
+ protected ExternalTypeHandler(JavaType beanType,
+ ExtTypedProperty[] properties,
+ Map<String, Object> nameToPropertyIndex,
String[] typeIds, TokenBuffer[] tokens)
{
+ _beanType = beanType;
_properties = properties;
_nameToPropertyIndex = nameToPropertyIndex;
_typeIds = typeIds;
@@ -36,6 +47,7 @@
protected ExternalTypeHandler(ExternalTypeHandler h)
{
+ _beanType = h._beanType;
_properties = h._properties;
_nameToPropertyIndex = h._nameToPropertyIndex;
int len = _properties.length;
@@ -44,6 +56,13 @@
}
/**
+ * @since 2.9
+ */
+ public static Builder builder(JavaType beanType) {
+ return new Builder(beanType);
+ }
+
+ /**
* Method called to start collection process by creating non-blueprint
* instances.
*/
@@ -54,23 +73,43 @@
/**
* Method called to see if given property/value pair is an external type
* id; and if so handle it. This is <b>only</b> to be called in case
- * containing POJO has similarly named property as the external type id;
+ * containing POJO has similarly named property as the external type id AND
+ * value is of scalar type:
* otherwise {@link #handlePropertyValue} should be called instead.
*/
+ @SuppressWarnings("unchecked")
public boolean handleTypePropertyValue(JsonParser p, DeserializationContext ctxt,
String propName, Object bean)
throws IOException
{
- Integer I = _nameToPropertyIndex.get(propName);
- if (I == null) {
+ Object ob = _nameToPropertyIndex.get(propName);
+ if (ob == null) {
return false;
}
- int index = I.intValue();
+ final String typeId = p.getText();
+ // 28-Nov-2016, tatu: For [databind#291], need separate handling
+ if (ob instanceof List<?>) {
+ boolean result = false;
+ for (Integer index : (List<Integer>) ob) {
+ if (_handleTypePropertyValue(p, ctxt, propName, bean,
+ typeId, index.intValue())) {
+ result = true;
+ }
+ }
+ return result;
+ }
+ return _handleTypePropertyValue(p, ctxt, propName, bean,
+ typeId, ((Integer) ob).intValue());
+ }
+
+ private final boolean _handleTypePropertyValue(JsonParser p, DeserializationContext ctxt,
+ String propName, Object bean, String typeId, int index)
+ throws IOException
+ {
ExtTypedProperty prop = _properties[index];
- if (!prop.hasTypePropertyName(propName)) {
+ if (!prop.hasTypePropertyName(propName)) { // when could/should this ever happen?
return false;
}
- String typeId = p.getText();
// note: can NOT skip child values (should always be String anyway)
boolean canDeserialize = (bean != null) && (_tokens[index] != null);
// Minor optimization: deserialize properties as soon as we have all we need:
@@ -92,14 +131,44 @@
*
* @return True, if the given property was properly handled
*/
+ @SuppressWarnings("unchecked")
public boolean handlePropertyValue(JsonParser p, DeserializationContext ctxt,
String propName, Object bean) throws IOException
{
- Integer I = _nameToPropertyIndex.get(propName);
- if (I == null) {
+ Object ob = _nameToPropertyIndex.get(propName);
+ if (ob == null) {
return false;
}
- int index = I.intValue();
+ // 28-Nov-2016, tatu: For [databind#291], need separate handling
+ if (ob instanceof List<?>) {
+ Iterator<Integer> it = ((List<Integer>) ob).iterator();
+ Integer index = it.next();
+
+ ExtTypedProperty prop = _properties[index];
+ // For now, let's assume it's same type (either type id OR value)
+ // for all mappings, so we'll only check first one
+ if (prop.hasTypePropertyName(propName)) {
+ String typeId = p.getText();
+ p.skipChildren();
+ _typeIds[index] = typeId;
+ while (it.hasNext()) {
+ _typeIds[it.next()] = typeId;
+ }
+ } else {
+ @SuppressWarnings("resource")
+ TokenBuffer tokens = new TokenBuffer(p, ctxt);
+ tokens.copyCurrentStructure(p);
+ _tokens[index] = tokens;
+ while (it.hasNext()) {
+ _tokens[it.next()] = tokens;
+ }
+ }
+ return true;
+ }
+
+ // Otherwise only maps to a single value, in which case we can
+ // handle things in bit more optimal way...
+ int index = ((Integer) ob).intValue();
ExtTypedProperty prop = _properties[index];
boolean canDeserialize;
if (prop.hasTypePropertyName(propName)) {
@@ -113,9 +182,8 @@
_tokens[index] = tokens;
canDeserialize = (bean != null) && (_typeIds[index] != null);
}
- /* Minor optimization: let's deserialize properties as soon as
- * we have all pertinent information:
- */
+ // Minor optimization: let's deserialize properties as soon as
+ // we have all pertinent information:
if (canDeserialize) {
String typeId = _typeIds[index];
// clear stored data, to avoid deserializing+setting twice:
@@ -146,7 +214,7 @@
// [databind#118]: Need to mind natural types, for which no type id
// will be included.
JsonToken t = tokens.firstToken();
- if (t != null && t.isScalarValue()) {
+ if (t.isScalarValue()) { // can't be null as we never store empty buffers
JsonParser buffered = tokens.asParser(p);
buffered.nextToken();
SettableBeanProperty extProp = _properties[i].getProperty();
@@ -157,7 +225,8 @@
}
// 26-Oct-2012, tatu: As per [databind#94], must allow use of 'defaultImpl'
if (!_properties[i].hasDefaultType()) {
- ctxt.reportMappingException("Missing external type id property '%s'",
+ ctxt.reportInputMismatch(bean.getClass(),
+ "Missing external type id property '%s'",
_properties[i].getTypePropertyName());
} else {
typeId = _properties[i].getDefaultTypeId();
@@ -168,7 +237,8 @@
if(prop.isRequired() ||
ctxt.isEnabled(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY)) {
- ctxt.reportMappingException("Missing property '%s' for external type id '%s'",
+ ctxt.reportInputMismatch(bean.getClass(),
+ "Missing property '%s' for external type id '%s'",
prop.getName(), _properties[i].getTypePropertyName());
}
return bean;
@@ -201,14 +271,16 @@
// but not just one
// 26-Oct-2012, tatu: As per [databind#94], must allow use of 'defaultImpl'
if (!extProp.hasDefaultType()) {
- ctxt.reportMappingException("Missing external type id property '%s'",
+ ctxt.reportInputMismatch(_beanType,
+ "Missing external type id property '%s'",
extProp.getTypePropertyName());
} else {
typeId = extProp.getDefaultTypeId();
}
} else if (_tokens[i] == null) {
SettableBeanProperty prop = extProp.getProperty();
- ctxt.reportMappingException("Missing property '%s' for external type id '%s'",
+ ctxt.reportInputMismatch(_beanType,
+ "Missing property '%s' for external type id '%s'",
prop.getName(), _properties[i].getTypePropertyName());
}
values[i] = _deserialize(p, ctxt, i, typeId);
@@ -294,17 +366,39 @@
public static class Builder
{
- private final ArrayList<ExtTypedProperty> _properties = new ArrayList<ExtTypedProperty>();
- private final HashMap<String, Integer> _nameToPropertyIndex = new HashMap<String, Integer>();
+ private final JavaType _beanType;
+
+ private final List<ExtTypedProperty> _properties = new ArrayList<>();
+ private final Map<String, Object> _nameToPropertyIndex = new HashMap<>();
+
+ protected Builder(JavaType t) {
+ _beanType = t;
+ }
public void addExternal(SettableBeanProperty property, TypeDeserializer typeDeser)
{
Integer index = _properties.size();
_properties.add(new ExtTypedProperty(property, typeDeser));
- _nameToPropertyIndex.put(property.getName(), index);
- _nameToPropertyIndex.put(typeDeser.getPropertyName(), index);
+ _addPropertyIndex(property.getName(), index);
+ _addPropertyIndex(typeDeser.getPropertyName(), index);
}
+ private void _addPropertyIndex(String name, Integer index) {
+ Object ob = _nameToPropertyIndex.get(name);
+ if (ob == null) {
+ _nameToPropertyIndex.put(name, index);
+ } else if (ob instanceof List<?>) {
+ @SuppressWarnings("unchecked")
+ List<Object> list = (List<Object>) ob;
+ list.add(index);
+ } else {
+ List<Object> list = new LinkedList<>();
+ list.add(ob);
+ list.add(index);
+ _nameToPropertyIndex.put(name, list);
+ }
+ }
+
/**
* Method called after all external properties have been assigned, to further
* link property with polymorphic value with possible property for type id
@@ -325,13 +419,8 @@
}
extProps[i] = extProp;
}
- return new ExternalTypeHandler(extProps, _nameToPropertyIndex, null, null);
- }
-
- @Deprecated // since 2.8; may be removed as early as 2.9
- public ExternalTypeHandler build() {
- return new ExternalTypeHandler(_properties.toArray(new ExtTypedProperty[_properties.size()]),
- _nameToPropertyIndex, null, null);
+ return new ExternalTypeHandler(_beanType, extProps, _nameToPropertyIndex,
+ null, null);
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/FailingDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/FailingDeserializer.java
index a777bde..9df6742 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/FailingDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/FailingDeserializer.java
@@ -1,5 +1,7 @@
package com.fasterxml.jackson.databind.deser.impl;
+import java.io.IOException;
+
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
@@ -23,8 +25,8 @@
}
@Override
- public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws JsonMappingException{
- ctxt.reportMappingException(_message);
+ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
+ ctxt.reportInputMismatch(this, _message);
return null;
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/FieldProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/FieldProperty.java
index 756dda2..f9f1536 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/FieldProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/FieldProperty.java
@@ -5,7 +5,10 @@
import java.lang.reflect.Field;
import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonToken;
+
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
@@ -32,24 +35,33 @@
*/
final protected transient Field _field;
+ /**
+ * @since 2.9
+ */
+ final protected boolean _skipNulls;
+
public FieldProperty(BeanPropertyDefinition propDef, JavaType type,
TypeDeserializer typeDeser, Annotations contextAnnotations, AnnotatedField field)
{
super(propDef, type, typeDeser, contextAnnotations);
_annotated = field;
_field = field.getAnnotated();
+ _skipNulls = NullsConstantProvider.isSkipper(_nullProvider);
}
- protected FieldProperty(FieldProperty src, JsonDeserializer<?> deser) {
- super(src, deser);
+ protected FieldProperty(FieldProperty src, JsonDeserializer<?> deser,
+ NullValueProvider nva) {
+ super(src, deser, nva);
_annotated = src._annotated;
_field = src._field;
+ _skipNulls = NullsConstantProvider.isSkipper(nva);
}
protected FieldProperty(FieldProperty src, PropertyName newName) {
super(src, newName);
_annotated = src._annotated;
_field = src._field;
+ _skipNulls = src._skipNulls;
}
/**
@@ -64,19 +76,25 @@
throw new IllegalArgumentException("Missing field (broken JDK (de)serialization?)");
}
_field = f;
+ _skipNulls = src._skipNulls;
}
-
+
@Override
- public FieldProperty withName(PropertyName newName) {
+ public SettableBeanProperty withName(PropertyName newName) {
return new FieldProperty(this, newName);
}
-
+
@Override
- public FieldProperty withValueDeserializer(JsonDeserializer<?> deser) {
+ public SettableBeanProperty withValueDeserializer(JsonDeserializer<?> deser) {
if (_valueDeserializer == deser) {
return this;
}
- return new FieldProperty(this, deser);
+ return new FieldProperty(this, deser, _nullProvider);
+ }
+
+ @Override
+ public SettableBeanProperty withNullProvider(NullValueProvider nva) {
+ return new FieldProperty(this, _valueDeserializer, nva);
}
@Override
@@ -108,7 +126,17 @@
public void deserializeAndSet(JsonParser p,
DeserializationContext ctxt, Object instance) throws IOException
{
- Object value = deserialize(p, ctxt);
+ Object value;
+ if (p.hasToken(JsonToken.VALUE_NULL)) {
+ if (_skipNulls) {
+ return;
+ }
+ value = _nullProvider.getNullValue(ctxt);
+ } else if (_valueTypeDeserializer == null) {
+ value = _valueDeserializer.deserialize(p, ctxt);
+ } else {
+ value = _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
+ }
try {
_field.set(instance, value);
} catch (Exception e) {
@@ -120,7 +148,17 @@
public Object deserializeSetAndReturn(JsonParser p,
DeserializationContext ctxt, Object instance) throws IOException
{
- Object value = deserialize(p, ctxt);
+ Object value;
+ if (p.hasToken(JsonToken.VALUE_NULL)) {
+ if (_skipNulls) {
+ return instance;
+ }
+ value = _nullProvider.getNullValue(ctxt);
+ } else if (_valueTypeDeserializer == null) {
+ value = _valueDeserializer.deserialize(p, ctxt);
+ } else {
+ value = _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
+ }
try {
_field.set(instance, value);
} catch (Exception e) {
@@ -128,9 +166,9 @@
}
return instance;
}
-
+
@Override
- public final void set(Object instance, Object value) throws IOException
+ public void set(Object instance, Object value) throws IOException
{
try {
_field.set(instance, value);
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/InnerClassProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/InnerClassProperty.java
index cac8057..e02a72e 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/InnerClassProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/InnerClassProperty.java
@@ -1,10 +1,10 @@
package com.fasterxml.jackson.databind.deser.impl;
import java.io.IOException;
-import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import com.fasterxml.jackson.core.*;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.introspect.*;
@@ -17,16 +17,11 @@
* to regular implementation.
*/
public final class InnerClassProperty
- extends SettableBeanProperty
+ extends SettableBeanProperty.Delegating
{
private static final long serialVersionUID = 1L;
/**
- * Actual property that we use after value construction.
- */
- protected final SettableBeanProperty _delegate;
-
- /**
* Constructor used when deserializing this property.
* Transient since there is no need to persist; only needed during
* construction of objects.
@@ -42,7 +37,6 @@
Constructor<?> ctor)
{
super(delegate);
- _delegate = delegate;
_creator = ctor;
}
@@ -50,66 +44,24 @@
* Constructor used with JDK Serialization; needed to handle transient
* Constructor, wrap/unwrap in/out-of Annotated variant.
*/
- protected InnerClassProperty(InnerClassProperty src, AnnotatedConstructor ann)
+ protected InnerClassProperty(SettableBeanProperty src, AnnotatedConstructor ann)
{
super(src);
- _delegate = src._delegate;
_annotated = ann;
_creator = (_annotated == null) ? null : _annotated.getAnnotated();
if (_creator == null) {
throw new IllegalArgumentException("Missing constructor (broken JDK (de)serialization?)");
}
}
-
- protected InnerClassProperty(InnerClassProperty src, JsonDeserializer<?> deser)
- {
- super(src, deser);
- _delegate = src._delegate.withValueDeserializer(deser);
- _creator = src._creator;
- }
-
- protected InnerClassProperty(InnerClassProperty src, PropertyName newName) {
- super(src, newName);
- _delegate = src._delegate.withName(newName);
- _creator = src._creator;
- }
@Override
- public InnerClassProperty withName(PropertyName newName) {
- return new InnerClassProperty(this, newName);
- }
-
- @Override
- public InnerClassProperty withValueDeserializer(JsonDeserializer<?> deser) {
- if (_valueDeserializer == deser) {
+ protected SettableBeanProperty withDelegate(SettableBeanProperty d) {
+ if (d == this.delegate) {
return this;
}
- return new InnerClassProperty(this, deser);
+ return new InnerClassProperty(d, _creator);
}
- @Override
- public void assignIndex(int index) { _delegate.assignIndex(index); }
-
- @Override
- public int getPropertyIndex() { return _delegate.getPropertyIndex(); }
-
- @Override
- public int getCreatorIndex() { return _delegate.getCreatorIndex(); }
-
- @Override
- public void fixAccess(DeserializationConfig config) {
- _delegate.fixAccess(config);
- }
-
- // // // BeanProperty impl
-
- @Override
- public <A extends Annotation> A getAnnotation(Class<A> acls) {
- return _delegate.getAnnotation(acls);
- }
-
- @Override public AnnotatedMember getMember() { return _delegate.getMember(); }
-
/*
/**********************************************************
/* Deserialization methods
@@ -117,15 +69,15 @@
*/
@Override
- public void deserializeAndSet(JsonParser jp, DeserializationContext ctxt, Object bean)
+ public void deserializeAndSet(JsonParser p, DeserializationContext ctxt, Object bean)
throws IOException
{
- JsonToken t = jp.getCurrentToken();
+ JsonToken t = p.getCurrentToken();
Object value;
if (t == JsonToken.VALUE_NULL) {
value = _valueDeserializer.getNullValue(ctxt);
} else if (_valueTypeDeserializer != null) {
- value = _valueDeserializer.deserializeWithType(jp, ctxt, _valueTypeDeserializer);
+ value = _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
} else { // the usual case
try {
value = _creator.newInstance(bean);
@@ -133,28 +85,21 @@
ClassUtil.unwrapAndThrowAsIAE(e, "Failed to instantiate class "+_creator.getDeclaringClass().getName()+", problem: "+e.getMessage());
value = null;
}
- _valueDeserializer.deserialize(jp, ctxt, value);
+ _valueDeserializer.deserialize(p, ctxt, value);
}
set(bean, value);
}
@Override
- public Object deserializeSetAndReturn(JsonParser jp,
- DeserializationContext ctxt, Object instance)
+ public Object deserializeSetAndReturn(JsonParser p, DeserializationContext ctxt, Object instance)
throws IOException
{
- return setAndReturn(instance, deserialize(jp, ctxt));
- }
-
- @Override
- public final void set(Object instance, Object value) throws IOException {
- _delegate.set(instance, value);
+ return setAndReturn(instance, deserialize(p, ctxt));
}
- @Override
- public Object setAndReturn(Object instance, Object value) throws IOException {
- return _delegate.setAndReturn(instance, value);
- }
+// these are fine with defaults
+// public final void set(Object instance, Object value) throws IOException { }
+// public Object setAndReturn(Object instance, Object value) throws IOException { }
/*
/**********************************************************
@@ -169,9 +114,9 @@
Object writeReplace() {
// need to construct a fake instance to support serialization
- if (_annotated != null) {
- return this;
+ if (_annotated == null) {
+ return new InnerClassProperty(this, new AnnotatedConstructor(null, _creator, null, null));
}
- return new InnerClassProperty(this, new AnnotatedConstructor(null, _creator, null, null));
+ return this;
}
-}
\ No newline at end of file
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ManagedReferenceProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ManagedReferenceProperty.java
index 21b465a..f0cbfda 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ManagedReferenceProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ManagedReferenceProperty.java
@@ -1,15 +1,12 @@
package com.fasterxml.jackson.databind.deser.impl;
import java.io.IOException;
-import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
-import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
-import com.fasterxml.jackson.databind.util.Annotations;
/**
* Wrapper property that is used to handle managed (forward) properties
@@ -17,7 +14,8 @@
* then to back property.
*/
public final class ManagedReferenceProperty
- extends SettableBeanProperty
+ // Changed to extends delegating base class in 2.9
+ extends SettableBeanProperty.Delegating
{
private static final long serialVersionUID = 1L;
@@ -29,73 +27,31 @@
*/
protected final boolean _isContainer;
- protected final SettableBeanProperty _managedProperty;
-
protected final SettableBeanProperty _backProperty;
public ManagedReferenceProperty(SettableBeanProperty forward, String refName,
- SettableBeanProperty backward, Annotations contextAnnotations, boolean isContainer)
+ SettableBeanProperty backward, boolean isContainer)
{
- super(forward.getFullName(), forward.getType(), forward.getWrapperName(),
- forward.getValueTypeDeserializer(), contextAnnotations,
- forward.getMetadata());
+ super(forward);
_referenceName = refName;
- _managedProperty = forward;
_backProperty = backward;
_isContainer = isContainer;
}
- protected ManagedReferenceProperty(ManagedReferenceProperty src, JsonDeserializer<?> deser)
- {
- super(src, deser);
- _referenceName = src._referenceName;
- _isContainer = src._isContainer;
- _managedProperty = src._managedProperty;
- _backProperty = src._backProperty;
- }
-
- protected ManagedReferenceProperty(ManagedReferenceProperty src, PropertyName newName) {
- super(src, newName);
- _referenceName = src._referenceName;
- _isContainer = src._isContainer;
- _managedProperty = src._managedProperty;
- _backProperty = src._backProperty;
- }
-
@Override
- public ManagedReferenceProperty withName(PropertyName newName) {
- return new ManagedReferenceProperty(this, newName);
+ protected SettableBeanProperty withDelegate(SettableBeanProperty d) {
+ throw new IllegalStateException("Should never try to reset delegate");
}
- @Override
- public ManagedReferenceProperty withValueDeserializer(JsonDeserializer<?> deser) {
- if (_valueDeserializer == deser) {
- return this;
- }
- return new ManagedReferenceProperty(this, deser);
- }
-
+ // need to override to ensure both get fixed
@Override
public void fixAccess(DeserializationConfig config) {
- _managedProperty.fixAccess(config);
+ delegate.fixAccess(config);
_backProperty.fixAccess(config);
}
/*
/**********************************************************
- /* BeanProperty impl
- /**********************************************************
- */
-
- @Override
- public <A extends Annotation> A getAnnotation(Class<A> acls) {
- return _managedProperty.getAnnotation(acls);
- }
-
- @Override public AnnotatedMember getMember() { return _managedProperty.getMember(); }
-
- /*
- /**********************************************************
/* Overridden methods
/**********************************************************
*/
@@ -103,7 +59,7 @@
@Override
public void deserializeAndSet(JsonParser p, DeserializationContext ctxt, Object instance)
throws IOException {
- set(instance, _managedProperty.deserialize(p, ctxt));
+ set(instance, delegate.deserialize(p, ctxt));
}
@Override
@@ -146,6 +102,6 @@
}
}
// and then the forward reference itself
- return _managedProperty.setAndReturn(instance, value);
+ return delegate.setAndReturn(instance, value);
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/MergingSettableBeanProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/MergingSettableBeanProperty.java
new file mode 100644
index 0000000..db9fda7
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/MergingSettableBeanProperty.java
@@ -0,0 +1,133 @@
+package com.fasterxml.jackson.databind.deser.impl;
+
+import java.io.IOException;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.deser.*;
+import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
+
+/**
+ * {@link SettableBeanProperty} implementation that will try to access value of
+ * the property first, and if non-null value found, pass that for update
+ * (using {@link com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext, Object)})
+ * instead of constructing a new value. This is necessary to support "merging" properties.
+ *<p>
+ * Note that there are many similarities to {@link SetterlessProperty}, which predates
+ * this variant; and that one is even used in cases where there is no mutator
+ * available.
+ *
+ * @since 2.9
+ */
+public class MergingSettableBeanProperty
+ extends SettableBeanProperty.Delegating
+{
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Member (field, method) used for accessing existing value.
+ */
+ protected final AnnotatedMember _accessor;
+
+ /*
+ /**********************************************************
+ /* Life-cycle
+ /**********************************************************
+ */
+
+ protected MergingSettableBeanProperty(SettableBeanProperty delegate,
+ AnnotatedMember accessor)
+ {
+ super(delegate);
+ _accessor = accessor;
+ }
+
+ protected MergingSettableBeanProperty(MergingSettableBeanProperty src,
+ SettableBeanProperty delegate)
+ {
+ super(delegate);
+ _accessor = src._accessor;
+ }
+
+ public static MergingSettableBeanProperty construct(SettableBeanProperty delegate,
+ AnnotatedMember accessor)
+ {
+ return new MergingSettableBeanProperty(delegate, accessor);
+ }
+
+ @Override
+ protected SettableBeanProperty withDelegate(SettableBeanProperty d) {
+ return new MergingSettableBeanProperty(d, _accessor);
+ }
+
+ /*
+ /**********************************************************
+ /* Deserialization methods
+ /**********************************************************
+ */
+
+ @Override
+ public void deserializeAndSet(JsonParser p, DeserializationContext ctxt,
+ Object instance) throws IOException
+ {
+ Object oldValue = _accessor.getValue(instance);
+ Object newValue;
+ // 20-Oct-2016, tatu: Couple of possibilities of how to proceed; for
+ // now, default to "normal" handling without merging
+ if (oldValue == null) {
+ newValue = delegate.deserialize(p, ctxt);
+ } else {
+ newValue = delegate.deserializeWith(p, ctxt, oldValue);
+ }
+ if (newValue != oldValue) {
+ // 18-Apr-2017, tatu: Null handling should occur within delegate, which may
+ // set/skip/transform it, or throw an exception.
+ delegate.set(instance, newValue);
+ }
+ }
+
+ @Override
+ public Object deserializeSetAndReturn(JsonParser p,
+ DeserializationContext ctxt, Object instance) throws IOException
+ {
+ Object oldValue = _accessor.getValue(instance);
+ Object newValue;
+ // 20-Oct-2016, tatu: Couple of possibilities of how to proceed; for
+ // now, default to "normal" handling without merging
+ if (oldValue == null) {
+ newValue = delegate.deserialize(p, ctxt);
+ } else {
+ newValue = delegate.deserializeWith(p, ctxt, oldValue);
+ }
+ // 23-Oct-2016, tatu: One possible complication here; should we always
+ // try calling setter on builder? Presumably should not be required,
+ // but may need to revise
+ if (newValue != oldValue) {
+ // 31-Oct-2016, tatu: Basically should just ignore as null can't really
+ // contribute to merging.
+ if (newValue != null) {
+ return delegate.setAndReturn(instance, newValue);
+ }
+ }
+ return instance;
+ }
+
+ @Override
+ public void set(Object instance, Object value) throws IOException {
+ // 31-Oct-2016, tatu: Basically should just ignore as null can't really
+ // contribute to merging.
+ if (value != null) {
+ delegate.set(instance, value);
+ }
+ }
+
+ @Override
+ public Object setAndReturn(Object instance, Object value) throws IOException {
+ // 31-Oct-2016, tatu: Basically should just ignore as null can't really
+ // contribute to merging.
+ if (value != null) {
+ return delegate.setAndReturn(instance, value);
+ }
+ return instance;
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/MethodProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/MethodProperty.java
index bf4e0fe..7bf931a 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/MethodProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/MethodProperty.java
@@ -5,8 +5,9 @@
import java.lang.reflect.Method;
import com.fasterxml.jackson.core.JsonParser;
-
+import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.introspect.*;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
@@ -28,6 +29,11 @@
* "regular" method-accessible properties.
*/
protected final transient Method _setter;
+
+ /**
+ * @since 2.9
+ */
+ final protected boolean _skipNulls;
public MethodProperty(BeanPropertyDefinition propDef,
JavaType type, TypeDeserializer typeDeser,
@@ -36,18 +42,22 @@
super(propDef, type, typeDeser, contextAnnotations);
_annotated = method;
_setter = method.getAnnotated();
+ _skipNulls = NullsConstantProvider.isSkipper(_nullProvider);
}
- protected MethodProperty(MethodProperty src, JsonDeserializer<?> deser) {
- super(src, deser);
+ protected MethodProperty(MethodProperty src, JsonDeserializer<?> deser,
+ NullValueProvider nva) {
+ super(src, deser, nva);
_annotated = src._annotated;
_setter = src._setter;
+ _skipNulls = NullsConstantProvider.isSkipper(nva);
}
protected MethodProperty(MethodProperty src, PropertyName newName) {
super(src, newName);
_annotated = src._annotated;
_setter = src._setter;
+ _skipNulls = src._skipNulls;
}
/**
@@ -57,19 +67,25 @@
super(src);
_annotated = src._annotated;
_setter = m;
+ _skipNulls = src._skipNulls;
}
-
+
@Override
- public MethodProperty withName(PropertyName newName) {
+ public SettableBeanProperty withName(PropertyName newName) {
return new MethodProperty(this, newName);
}
@Override
- public MethodProperty withValueDeserializer(JsonDeserializer<?> deser) {
+ public SettableBeanProperty withValueDeserializer(JsonDeserializer<?> deser) {
if (_valueDeserializer == deser) {
return this;
}
- return new MethodProperty(this, deser);
+ return new MethodProperty(this, deser, _nullProvider);
+ }
+
+ @Override
+ public SettableBeanProperty withNullProvider(NullValueProvider nva) {
+ return new MethodProperty(this, _valueDeserializer, nva);
}
@Override
@@ -101,7 +117,17 @@
public void deserializeAndSet(JsonParser p, DeserializationContext ctxt,
Object instance) throws IOException
{
- Object value = deserialize(p, ctxt);
+ Object value;
+ if (p.hasToken(JsonToken.VALUE_NULL)) {
+ if (_skipNulls) {
+ return;
+ }
+ value = _nullProvider.getNullValue(ctxt);
+ } else if (_valueTypeDeserializer == null) {
+ value = _valueDeserializer.deserialize(p, ctxt);
+ } else {
+ value = _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
+ }
try {
_setter.invoke(instance, value);
} catch (Exception e) {
@@ -113,7 +139,17 @@
public Object deserializeSetAndReturn(JsonParser p,
DeserializationContext ctxt, Object instance) throws IOException
{
- Object value = deserialize(p, ctxt);
+ Object value;
+ if (p.hasToken(JsonToken.VALUE_NULL)) {
+ if (_skipNulls) {
+ return instance;
+ }
+ value = _nullProvider.getNullValue(ctxt);
+ } else if (_valueTypeDeserializer == null) {
+ value = _valueDeserializer.deserialize(p, ctxt);
+ } else {
+ value = _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
+ }
try {
Object result = _setter.invoke(instance, value);
return (result == null) ? instance : result;
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/NullsAsEmptyProvider.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/NullsAsEmptyProvider.java
new file mode 100644
index 0000000..51e385f
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/NullsAsEmptyProvider.java
@@ -0,0 +1,33 @@
+package com.fasterxml.jackson.databind.deser.impl;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
+import com.fasterxml.jackson.databind.exc.InvalidNullException;
+import com.fasterxml.jackson.databind.util.AccessPattern;
+
+/**
+ * Simple {@link NullValueProvider} that will always throw a
+ * {@link InvalidNullException} when a null is encountered.
+ */
+public class NullsAsEmptyProvider
+ implements NullValueProvider, java.io.Serializable
+{
+ private static final long serialVersionUID = 1L;
+
+ protected final JsonDeserializer<?> _deserializer;
+
+ public NullsAsEmptyProvider(JsonDeserializer<?> deser) {
+ _deserializer = deser;
+ }
+
+ @Override
+ public AccessPattern getNullAccessPattern() {
+ return AccessPattern.DYNAMIC;
+ }
+
+ @Override
+ public Object getNullValue(DeserializationContext ctxt)
+ throws JsonMappingException {
+ return _deserializer.getEmptyValue(ctxt);
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/NullsConstantProvider.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/NullsConstantProvider.java
new file mode 100644
index 0000000..5995a32
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/NullsConstantProvider.java
@@ -0,0 +1,79 @@
+package com.fasterxml.jackson.databind.deser.impl;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
+import com.fasterxml.jackson.databind.exc.InvalidNullException;
+import com.fasterxml.jackson.databind.util.AccessPattern;
+
+/**
+ * Simple {@link NullValueProvider} that will always throw a
+ * {@link InvalidNullException} when a null is encountered.
+ */
+public class NullsConstantProvider
+ implements NullValueProvider, java.io.Serializable
+{
+ private static final long serialVersionUID = 1L;
+
+ private final static NullsConstantProvider SKIPPER = new NullsConstantProvider(null);
+
+ private final static NullsConstantProvider NULLER = new NullsConstantProvider(null);
+
+ protected final Object _nullValue;
+
+ protected final AccessPattern _access;
+
+ protected NullsConstantProvider(Object nvl) {
+ _nullValue = nvl;
+ _access = (_nullValue == null) ? AccessPattern.ALWAYS_NULL
+ : AccessPattern.CONSTANT;
+ }
+
+ /**
+ * Static accessor for a stateless instance used as marker, to indicate
+ * that all input `null` values should be skipped (ignored), so that
+ * no corresponding property value is set (with POJOs), and no content
+ * values (array/Collection elements, Map entries) are added.
+ */
+ public static NullsConstantProvider skipper() {
+ return SKIPPER;
+ }
+
+ public static NullsConstantProvider nuller() {
+ return NULLER;
+ }
+
+ public static NullsConstantProvider forValue(Object nvl) {
+ if (nvl == null) {
+ return NULLER;
+ }
+ return new NullsConstantProvider(nvl);
+ }
+
+ /**
+ * Utility method that can be used to check if given null value provider
+ * is "skipper", marker provider that means that all input `null`s should
+ * be skipped (ignored), instead of converted
+ */
+ public static boolean isSkipper(NullValueProvider p) {
+ return (p == SKIPPER);
+ }
+
+ /**
+ * Utility method that can be used to check if given null value provider
+ * is "nuller", no-operation provider that will always simply return
+ * Java `null` for any and all input `null`s.
+ */
+ public static boolean isNuller(NullValueProvider p) {
+ return (p == NULLER);
+ }
+
+ @Override
+ public AccessPattern getNullAccessPattern() {
+ return _access;
+ }
+
+ @Override
+ public Object getNullValue(DeserializationContext ctxt) {
+ return _nullValue;
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/NullsFailProvider.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/NullsFailProvider.java
new file mode 100644
index 0000000..ba870c7
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/NullsFailProvider.java
@@ -0,0 +1,44 @@
+package com.fasterxml.jackson.databind.deser.impl;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
+import com.fasterxml.jackson.databind.exc.InvalidNullException;
+import com.fasterxml.jackson.databind.util.AccessPattern;
+
+/**
+ * Simple {@link NullValueProvider} that will always throw a
+ * {@link InvalidNullException} when a null is encountered.
+ */
+public class NullsFailProvider
+ implements NullValueProvider, java.io.Serializable
+{
+ private static final long serialVersionUID = 1L;
+
+ protected final PropertyName _name;
+ protected final JavaType _type;
+
+ protected NullsFailProvider(PropertyName name, JavaType type) {
+ _name = name;
+ _type = type;
+ }
+
+ public static NullsFailProvider constructForProperty(BeanProperty prop) {
+ return new NullsFailProvider(prop.getFullName(), prop.getType());
+ }
+
+ public static NullsFailProvider constructForRootValue(JavaType t) {
+ return new NullsFailProvider(null, t);
+ }
+
+ @Override
+ public AccessPattern getNullAccessPattern() {
+ // Must be called every time to effect the exception...
+ return AccessPattern.DYNAMIC;
+ }
+
+ @Override
+ public Object getNullValue(DeserializationContext ctxt)
+ throws JsonMappingException {
+ throw InvalidNullException.from(ctxt, _name, _type);
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdReader.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdReader.java
index 6e90e35..99b6e75 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdReader.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdReader.java
@@ -4,8 +4,9 @@
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.annotation.ObjectIdResolver;
-import com.fasterxml.jackson.annotation.SimpleObjectIdResolver;
+
import com.fasterxml.jackson.core.JsonParser;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
@@ -55,13 +56,6 @@
idProperty = idProp;
}
- @Deprecated // since 2.4
- protected ObjectIdReader(JavaType t, PropertyName propName, ObjectIdGenerator<?> gen,
- JsonDeserializer<?> deser, SettableBeanProperty idProp)
- {
- this(t,propName, gen, deser, idProp, new SimpleObjectIdResolver());
- }
-
/**
* Factory method called by {@link com.fasterxml.jackson.databind.ser.std.BeanSerializerBase}
* with the initial information based on standard settings for the type
@@ -74,14 +68,6 @@
return new ObjectIdReader(idType, propName, generator, deser, idProp, resolver);
}
- @Deprecated // since 2.4
- public static ObjectIdReader construct(JavaType idType, PropertyName propName,
- ObjectIdGenerator<?> generator, JsonDeserializer<?> deser,
- SettableBeanProperty idProp)
- {
- return construct(idType, propName, generator, deser, idProp, new SimpleObjectIdResolver());
- }
-
/*
/**********************************************************
/* API
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdReferenceProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdReferenceProperty.java
index 2034d91..e436180 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdReferenceProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdReferenceProperty.java
@@ -5,6 +5,7 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.deser.UnresolvedForwardReference;
import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId.Referring;
@@ -24,9 +25,10 @@
_objectIdInfo = objectIdInfo;
}
- public ObjectIdReferenceProperty(ObjectIdReferenceProperty src, JsonDeserializer<?> deser)
+ public ObjectIdReferenceProperty(ObjectIdReferenceProperty src, JsonDeserializer<?> deser,
+ NullValueProvider nva)
{
- super(src, deser);
+ super(src, deser, nva);
_forward = src._forward;
_objectIdInfo = src._objectIdInfo;
}
@@ -39,19 +41,24 @@
}
@Override
- public SettableBeanProperty withValueDeserializer(JsonDeserializer<?> deser) {
- if (_valueDeserializer == deser) {
- return this;
- }
- return new ObjectIdReferenceProperty(this, deser);
- }
-
- @Override
public SettableBeanProperty withName(PropertyName newName) {
return new ObjectIdReferenceProperty(this, newName);
}
@Override
+ public SettableBeanProperty withValueDeserializer(JsonDeserializer<?> deser) {
+ if (_valueDeserializer == deser) {
+ return this;
+ }
+ return new ObjectIdReferenceProperty(this, deser, _nullProvider);
+ }
+
+ @Override
+ public SettableBeanProperty withNullProvider(NullValueProvider nva) {
+ return new ObjectIdReferenceProperty(this, _valueDeserializer, nva);
+ }
+
+ @Override
public void fixAccess(DeserializationConfig config) {
if (_forward != null) {
_forward.fixAccess(config);
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdValueProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdValueProperty.java
index 4787fd0..6316e28 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdValueProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ObjectIdValueProperty.java
@@ -29,9 +29,10 @@
_objectIdReader = objectIdReader;
}
- protected ObjectIdValueProperty(ObjectIdValueProperty src, JsonDeserializer<?> deser)
+ protected ObjectIdValueProperty(ObjectIdValueProperty src, JsonDeserializer<?> deser,
+ NullValueProvider nva)
{
- super(src, deser);
+ super(src, deser, nva);
_objectIdReader = src._objectIdReader;
}
@@ -41,18 +42,23 @@
}
@Override
- public ObjectIdValueProperty withName(PropertyName newName) {
+ public SettableBeanProperty withName(PropertyName newName) {
return new ObjectIdValueProperty(this, newName);
}
@Override
- public ObjectIdValueProperty withValueDeserializer(JsonDeserializer<?> deser) {
+ public SettableBeanProperty withValueDeserializer(JsonDeserializer<?> deser) {
if (_valueDeserializer == deser) {
return this;
}
- return new ObjectIdValueProperty(this, deser);
+ return new ObjectIdValueProperty(this, deser, _nullProvider);
}
-
+
+ @Override
+ public SettableBeanProperty withNullProvider(NullValueProvider nva) {
+ return new ObjectIdValueProperty(this, _valueDeserializer, nva);
+ }
+
// // // BeanProperty impl
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyBasedCreator.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyBasedCreator.java
index f0c4093..4e7bc44 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyBasedCreator.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyBasedCreator.java
@@ -1,14 +1,11 @@
package com.fasterxml.jackson.databind.deser.impl;
import java.io.IOException;
-import java.util.Collection;
-import java.util.HashMap;
+import java.util.*;
import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.JsonMappingException;
-import com.fasterxml.jackson.databind.MapperFeature;
+import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
@@ -53,9 +50,11 @@
/**********************************************************
*/
- protected PropertyBasedCreator(ValueInstantiator valueInstantiator,
+ protected PropertyBasedCreator(DeserializationContext ctxt,
+ ValueInstantiator valueInstantiator,
SettableBeanProperty[] creatorProps,
- boolean caseInsensitive)
+ boolean caseInsensitive,
+ boolean addAliases)
{
_valueInstantiator = valueInstantiator;
if (caseInsensitive) {
@@ -66,6 +65,20 @@
final int len = creatorProps.length;
_propertyCount = len;
_allProperties = new SettableBeanProperty[len];
+
+ // 26-Feb-2017, tatu: Let's start by aliases, so that there is no
+ // possibility of accidental override of primary names
+ if (addAliases) {
+ final DeserializationConfig config = ctxt.getConfig();
+ for (SettableBeanProperty prop : creatorProps) {
+ List<PropertyName> aliases = prop.findAliases(config);
+ if (!aliases.isEmpty()) {
+ for (PropertyName pn : aliases) {
+ _propertyLookup.put(pn.getSimpleName(), prop);
+ }
+ }
+ }
+ }
for (int i = 0; i < len; ++i) {
SettableBeanProperty prop = creatorProps[i];
_allProperties[i] = prop;
@@ -74,23 +87,61 @@
}
/**
- * Factory method used for building actual instances: resolves deserializers
- * and checks for "null values".
+ * Factory method used for building actual instances to be used with POJOS:
+ * resolves deserializers, checks for "null values".
+ *
+ * @since 2.9
*/
public static PropertyBasedCreator construct(DeserializationContext ctxt,
- ValueInstantiator valueInstantiator, SettableBeanProperty[] srcProps)
+ ValueInstantiator valueInstantiator, SettableBeanProperty[] srcCreatorProps,
+ BeanPropertyMap allProperties)
throws JsonMappingException
{
- final int len = srcProps.length;
+ final int len = srcCreatorProps.length;
SettableBeanProperty[] creatorProps = new SettableBeanProperty[len];
for (int i = 0; i < len; ++i) {
- SettableBeanProperty prop = srcProps[i];
+ SettableBeanProperty prop = srcCreatorProps[i];
if (!prop.hasValueDeserializer()) {
prop = prop.withValueDeserializer(ctxt.findContextualValueDeserializer(prop.getType(), prop));
}
creatorProps[i] = prop;
}
- return new PropertyBasedCreator(valueInstantiator, creatorProps,
+ return new PropertyBasedCreator(ctxt, valueInstantiator, creatorProps,
+ allProperties.isCaseInsensitive(),
+ allProperties.hasAliases());
+ }
+
+ /**
+ * Factory method used for building actual instances to be used with types
+ * OTHER than POJOs.
+ * resolves deserializers and checks for "null values".
+ *
+ * @since 2.9
+ */
+ public static PropertyBasedCreator construct(DeserializationContext ctxt,
+ ValueInstantiator valueInstantiator, SettableBeanProperty[] srcCreatorProps,
+ boolean caseInsensitive)
+ throws JsonMappingException
+ {
+ final int len = srcCreatorProps.length;
+ SettableBeanProperty[] creatorProps = new SettableBeanProperty[len];
+ for (int i = 0; i < len; ++i) {
+ SettableBeanProperty prop = srcCreatorProps[i];
+ if (!prop.hasValueDeserializer()) {
+ prop = prop.withValueDeserializer(ctxt.findContextualValueDeserializer(prop.getType(), prop));
+ }
+ creatorProps[i] = prop;
+ }
+ return new PropertyBasedCreator(ctxt, valueInstantiator, creatorProps,
+ caseInsensitive, false);
+ }
+
+ @Deprecated // since 2.9
+ public static PropertyBasedCreator construct(DeserializationContext ctxt,
+ ValueInstantiator valueInstantiator, SettableBeanProperty[] srcCreatorProps)
+ throws JsonMappingException
+ {
+ return construct(ctxt, valueInstantiator, srcCreatorProps,
ctxt.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
}
@@ -158,7 +209,7 @@
/**
* Simple override of standard {@link java.util.HashMap} to support
- * case-insensitive access to creator properties.
+ * case-insensitive access to creator properties
*
* @since 2.8.5
*/
@@ -168,8 +219,7 @@
@Override
public SettableBeanProperty get(Object key0) {
- String key = (String) key0;
- return super.get(key.toLowerCase());
+ return super.get(((String) key0).toLowerCase());
}
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java
index 73238b2..068d08b 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java
@@ -131,9 +131,9 @@
value = _creatorParameters[prop.getCreatorIndex()] = _findMissing(prop);
}
if (value == null && _context.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)) {
- throw _context.mappingException(
+ return _context.reportInputMismatch(prop, String.format(
"Null value for creator property '%s'; DeserializationFeature.FAIL_ON_NULL_FOR_CREATOR_PARAMETERS enabled",
- prop.getName(), prop.getCreatorIndex());
+ prop.getName(), prop.getCreatorIndex()));
}
return value;
}
@@ -169,10 +169,12 @@
if (_context.isEnabled(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)) {
for (int ix = 0; ix < props.length; ++ix) {
- if (_creatorParameters[ix] == null) {
- _context.reportMappingException("Null value for creator property '%s'; DeserializationFeature.FAIL_ON_NULL_FOR_CREATOR_PARAMETERS enabled",
- props[ix].getName(), props[ix].getCreatorIndex());
- }
+ if (_creatorParameters[ix] == null) {
+ SettableBeanProperty prop = props[ix];
+ _context.reportInputMismatch(prop.getType(),
+ "Null value for creator property '%s' (index %d); DeserializationFeature.FAIL_ON_NULL_FOR_CREATOR_PARAMETERS enabled",
+ prop.getName(), props[ix].getCreatorIndex());
+ }
}
}
@@ -189,12 +191,14 @@
}
// Second: required?
if (prop.isRequired()) {
- _context.reportMappingException("Missing required creator property '%s' (index %d)",
- prop.getName(), prop.getCreatorIndex());
+ _context.reportInputMismatch(prop, String.format(
+ "Missing required creator property '%s' (index %d)",
+ prop.getName(), prop.getCreatorIndex()));
}
if (_context.isEnabled(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES)) {
- _context.reportMappingException("Missing creator property '%s' (index %d); DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES enabled",
- prop.getName(), prop.getCreatorIndex());
+ _context.reportInputMismatch(prop, String.format(
+ "Missing creator property '%s' (index %d); DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES enabled",
+ prop.getName(), prop.getCreatorIndex()));
}
// Third: default value
JsonDeserializer<Object> deser = prop.getValueDeserializer();
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/SetterlessProperty.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/SetterlessProperty.java
index 8df84b6..bc73589 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/SetterlessProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/SetterlessProperty.java
@@ -8,6 +8,7 @@
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
@@ -40,8 +41,9 @@
_getter = method.getAnnotated();
}
- protected SetterlessProperty(SetterlessProperty src, JsonDeserializer<?> deser) {
- super(src, deser);
+ protected SetterlessProperty(SetterlessProperty src, JsonDeserializer<?> deser,
+ NullValueProvider nva) {
+ super(src, deser, nva);
_annotated = src._annotated;
_getter = src._getter;
}
@@ -53,16 +55,21 @@
}
@Override
- public SetterlessProperty withName(PropertyName newName) {
+ public SettableBeanProperty withName(PropertyName newName) {
return new SetterlessProperty(this, newName);
}
-
+
@Override
- public SetterlessProperty withValueDeserializer(JsonDeserializer<?> deser) {
+ public SettableBeanProperty withValueDeserializer(JsonDeserializer<?> deser) {
if (_valueDeserializer == deser) {
return this;
}
- return new SetterlessProperty(this, deser);
+ return new SetterlessProperty(this, deser, _nullProvider);
+ }
+
+ @Override
+ public SettableBeanProperty withNullProvider(NullValueProvider nva) {
+ return new SetterlessProperty(this, _valueDeserializer, nva);
}
@Override
@@ -96,36 +103,32 @@
{
JsonToken t = p.getCurrentToken();
if (t == JsonToken.VALUE_NULL) {
- /* Hmmh. Is this a problem? We won't be setting anything, so it's
- * equivalent of empty Collection/Map in this case
- */
+ // Hmmh. Is this a problem? We won't be setting anything, so it's
+ // equivalent of empty Collection/Map in this case
return;
}
-
- // For [#501] fix we need to implement this but:
+ // For [databind#501] fix we need to implement this but:
if (_valueTypeDeserializer != null) {
- ctxt.reportMappingException(
+ ctxt.reportBadDefinition(getType(), String.format(
"Problem deserializing 'setterless' property (\"%s\"): no way to handle typed deser with setterless yet",
- getName());
+ getName()));
// return _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
}
-
// Ok: then, need to fetch Collection/Map to modify:
Object toModify;
try {
- toModify = _getter.invoke(instance);
+ toModify = _getter.invoke(instance, (Object[]) null);
} catch (Exception e) {
_throwAsIOE(p, e);
return; // never gets here
}
- /* Note: null won't work, since we can't then inject anything
- * in. At least that's not good in common case. However,
- * theoretically the case where we get JSON null might
- * be compatible. If so, implementation could be changed.
- */
+ // Note: null won't work, since we can't then inject anything in. At least
+ // that's not good in common case. However, theoretically the case where
+ // we get JSON null might be compatible. If so, implementation could be changed.
if (toModify == null) {
- throw JsonMappingException.from(p,
- "Problem deserializing 'setterless' property '"+getName()+"': get method returned null");
+ ctxt.reportBadDefinition(getType(), String.format(
+ "Problem deserializing 'setterless' property '%s': get method returned null",
+ getName()));
}
_valueDeserializer.deserialize(p, ctxt, toModify);
}
@@ -147,6 +150,6 @@
public Object setAndReturn(Object instance, Object value) throws IOException
{
set(instance, value);
- return null;
+ return instance;
}
}
\ No newline at end of file
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/TypeWrappedDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/TypeWrappedDeserializer.java
index 0f23fd1..8d5039d 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/TypeWrappedDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/TypeWrappedDeserializer.java
@@ -37,6 +37,11 @@
return _deserializer.handledType();
}
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return _deserializer.supportsUpdate(config);
+ }
+
@Override
public JsonDeserializer<?> getDelegatee() {
return _deserializer.getDelegatee();
@@ -58,13 +63,13 @@
}
@Override
- public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException
+ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
- return _deserializer.deserializeWithType(jp, ctxt, _typeDeserializer);
+ return _deserializer.deserializeWithType(p, ctxt, _typeDeserializer);
}
@Override
- public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt,
+ public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer) throws IOException
{
// should never happen? (if it can, could call on that object)
@@ -72,12 +77,12 @@
}
@Override
- public Object deserialize(JsonParser jp, DeserializationContext ctxt,
+ public Object deserialize(JsonParser p, DeserializationContext ctxt,
Object intoValue) throws IOException
{
/* 01-Mar-2013, tatu: Hmmh. Tough call as to what to do... need
* to delegate, but will this work reliably? Let's just hope so:
*/
- return _deserializer.deserialize(jp, ctxt, intoValue);
+ return _deserializer.deserialize(p, ctxt, intoValue);
}
-}
\ No newline at end of file
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ValueInjector.java b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ValueInjector.java
index 46e3bc9..815d51c 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/impl/ValueInjector.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/impl/ValueInjector.java
@@ -2,13 +2,8 @@
import java.io.IOException;
-import com.fasterxml.jackson.databind.BeanProperty;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.JavaType;
-import com.fasterxml.jackson.databind.PropertyMetadata;
-import com.fasterxml.jackson.databind.PropertyName;
+import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
-import com.fasterxml.jackson.databind.util.Annotations;
/**
* Class that encapsulates details of value injection that occurs before
@@ -19,29 +14,22 @@
public class ValueInjector
extends BeanProperty.Std
{
+ private static final long serialVersionUID = 1L;
+
/**
* Identifier used for looking up value to inject
*/
protected final Object _valueId;
public ValueInjector(PropertyName propName, JavaType type,
- Annotations contextAnnotations, AnnotatedMember mutator,
- Object valueId)
+ AnnotatedMember mutator, Object valueId)
{
- super(propName, type, null, contextAnnotations, mutator,
- PropertyMetadata.STD_OPTIONAL);
+ super(propName, type, null, mutator, PropertyMetadata.STD_OPTIONAL);
_valueId = valueId;
}
- @Deprecated // since 2.3
- public ValueInjector(String propName, JavaType type,
- Annotations contextAnnotations, AnnotatedMember mutator,
- Object valueId)
- {
- this(new PropertyName(propName), type, contextAnnotations, mutator, valueId);
- }
-
public Object findValue(DeserializationContext context, Object beanInstance)
+ throws JsonMappingException
{
return context.findInjectableValue(_valueId, this, beanInstance);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/ArrayBlockingQueueDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/ArrayBlockingQueueDeserializer.java
index 0394068..b79c4b5 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/ArrayBlockingQueueDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/ArrayBlockingQueueDeserializer.java
@@ -5,8 +5,8 @@
import java.util.concurrent.ArrayBlockingQueue;
import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
@@ -26,23 +26,24 @@
/**********************************************************
*/
- public ArrayBlockingQueueDeserializer(JavaType collectionType,
+ public ArrayBlockingQueueDeserializer(JavaType containerType,
JsonDeserializer<Object> valueDeser, TypeDeserializer valueTypeDeser,
ValueInstantiator valueInstantiator)
{
- super(collectionType, valueDeser, valueTypeDeser, valueInstantiator);
+ super(containerType, valueDeser, valueTypeDeser, valueInstantiator);
}
/**
* Constructor used when creating contextualized instances.
*/
- protected ArrayBlockingQueueDeserializer(JavaType collectionType,
+ protected ArrayBlockingQueueDeserializer(JavaType containerType,
JsonDeserializer<Object> valueDeser, TypeDeserializer valueTypeDeser,
ValueInstantiator valueInstantiator,
- JsonDeserializer<Object> delegateDeser, Boolean unwrapSingle)
+ JsonDeserializer<Object> delegateDeser,
+ NullValueProvider nuller, Boolean unwrapSingle)
{
- super(collectionType, valueDeser, valueTypeDeser, valueInstantiator,
- delegateDeser, unwrapSingle);
+ super(containerType, valueDeser, valueTypeDeser, valueInstantiator, delegateDeser,
+ nuller, unwrapSingle);
}
/**
@@ -59,16 +60,13 @@
@Override
@SuppressWarnings("unchecked")
protected ArrayBlockingQueueDeserializer withResolved(JsonDeserializer<?> dd,
- JsonDeserializer<?> vd, TypeDeserializer vtd, Boolean unwrapSingle)
+ JsonDeserializer<?> vd, TypeDeserializer vtd,
+ NullValueProvider nuller, Boolean unwrapSingle)
{
- if ((dd == _delegateDeserializer) && (vd == _valueDeserializer) && (vtd == _valueTypeDeserializer)
- && (_unwrapSingle == unwrapSingle)) {
- return this;
- }
- return new ArrayBlockingQueueDeserializer(_collectionType,
+ return new ArrayBlockingQueueDeserializer(_containerType,
(JsonDeserializer<Object>) vd, vtd,
- _valueInstantiator, (JsonDeserializer<Object>) dd, unwrapSingle);
-
+ _valueInstantiator, (JsonDeserializer<Object>) dd,
+ nuller, unwrapSingle);
}
/*
@@ -76,63 +74,35 @@
/* JsonDeserializer API
/**********************************************************
*/
-
- @SuppressWarnings("unchecked")
+
@Override
- public Collection<Object> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException
+ protected Collection<Object> createDefaultInstance(DeserializationContext ctxt)
+ throws IOException
{
- if (_delegateDeserializer != null) {
- return (Collection<Object>) _valueInstantiator.createUsingDelegate(ctxt,
- _delegateDeserializer.deserialize(jp, ctxt));
- }
- if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
- String str = jp.getText();
- if (str.length() == 0) {
- return (Collection<Object>) _valueInstantiator.createFromString(ctxt, str);
- }
- }
- return deserialize(jp, ctxt, null);
+ // 07-Nov-2016, tatu: Important: can not create using default ctor (one
+ // does not exist); and also need to know exact size. Hence, return
+ // null from here
+ return null;
}
@Override
- public Collection<Object> deserialize(JsonParser jp, DeserializationContext ctxt, Collection<Object> result0) throws IOException
+ public Collection<Object> deserialize(JsonParser p, DeserializationContext ctxt,
+ Collection<Object> result0) throws IOException
{
- // Ok: must point to START_ARRAY (or equivalent)
- if (!jp.isExpectedStartArrayToken()) {
- return handleNonArray(jp, ctxt, new ArrayBlockingQueue<Object>(1));
- }
- ArrayList<Object> tmp = new ArrayList<Object>();
-
- JsonDeserializer<Object> valueDes = _valueDeserializer;
- JsonToken t;
- final TypeDeserializer typeDeser = _valueTypeDeserializer;
-
- try {
- while ((t = jp.nextToken()) != JsonToken.END_ARRAY) {
- Object value;
-
- if (t == JsonToken.VALUE_NULL) {
- value = valueDes.getNullValue(ctxt);
- } else if (typeDeser == null) {
- value = valueDes.deserialize(jp, ctxt);
- } else {
- value = valueDes.deserializeWithType(jp, ctxt, typeDeser);
- }
- tmp.add(value);
- }
- } catch (Exception e) {
- throw JsonMappingException.wrapWithPath(e, tmp, tmp.size());
- }
if (result0 != null) {
- result0.addAll(tmp);
- return result0;
+ return super.deserialize(p, ctxt, result0);
}
- return new ArrayBlockingQueue<Object>(tmp.size(), false, tmp);
+ // Ok: must point to START_ARRAY (or equivalent)
+ if (!p.isExpectedStartArrayToken()) {
+ return handleNonArray(p, ctxt, new ArrayBlockingQueue<Object>(1));
+ }
+ result0 = super.deserialize(p, ctxt, new ArrayList<Object>());
+ return new ArrayBlockingQueue<Object>(result0.size(), false, result0);
}
@Override
- public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
+ public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
// In future could check current token... for now this should be enough:
- return typeDeserializer.deserializeTypedFromArray(jp, ctxt);
+ return typeDeserializer.deserializeTypedFromArray(p, ctxt);
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/AtomicReferenceDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/AtomicReferenceDeserializer.java
index 8faa423..916e266 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/AtomicReferenceDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/AtomicReferenceDeserializer.java
@@ -3,6 +3,7 @@
import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
public class AtomicReferenceDeserializer
@@ -16,15 +17,13 @@
/**********************************************************
*/
- @Deprecated // since 2.8
- public AtomicReferenceDeserializer(JavaType fullType) {
- this(fullType, null, null);
- }
-
- public AtomicReferenceDeserializer(JavaType fullType,
+ /**
+ * @since 2.9
+ */
+ public AtomicReferenceDeserializer(JavaType fullType, ValueInstantiator inst,
TypeDeserializer typeDeser, JsonDeserializer<?> deser)
{
- super(fullType, typeDeser, deser);
+ super(fullType, inst, typeDeser, deser);
}
/*
@@ -35,42 +34,38 @@
@Override
public AtomicReferenceDeserializer withResolved(TypeDeserializer typeDeser, JsonDeserializer<?> valueDeser) {
- return new AtomicReferenceDeserializer(_fullType, typeDeser, valueDeser);
+ return new AtomicReferenceDeserializer(_fullType, _valueInstantiator,
+ typeDeser, valueDeser);
}
-
@Override
public AtomicReference<Object> getNullValue(DeserializationContext ctxt) {
return new AtomicReference<Object>();
}
@Override
+ public Object getEmptyValue(DeserializationContext ctxt) {
+ return new AtomicReference<Object>();
+ }
+
+ @Override
public AtomicReference<Object> referenceValue(Object contents) {
return new AtomicReference<Object>(contents);
}
- /*
@Override
- public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
- TypeDeserializer typeDeser) throws IOException
- {
- final JsonToken t = p.getCurrentToken();
- if (t == JsonToken.VALUE_NULL) { // can this actually happen?
- return getNullValue(ctxt);
- }
- // 22-Oct-2015, tatu: This handling is probably not needed (or is wrong), but
- // could be result of older (pre-2.7) Jackson trying to serialize natural types.
- // Because of this, let's allow for now, unless proven problematic
- if ((t != null) && t.isScalarValue()) {
- return deserialize(p, ctxt);
- }
- // 19-Apr-2016, tatu: Alas, due to there not really being anything for AtomicReference
- // itself, need to just ignore `typeDeser`, use TypeDeserializer we do have for contents
- // and it might just work.
-
- if (_valueTypeDeserializer == null) {
- return deserialize(p, ctxt);
- }
- return new AtomicReference<Object>(_valueTypeDeserializer.deserializeTypedFromAny(p, ctxt));
+ public Object getReferenced(AtomicReference<Object> reference) {
+ return reference.get();
}
- */
+
+ @Override // since 2.9
+ public AtomicReference<Object> updateReference(AtomicReference<Object> reference, Object contents) {
+ reference.set(contents);
+ return reference;
+ }
+
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ // yes; regardless of value deserializer reference itself may be updated
+ return Boolean.TRUE;
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/CollectionDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/CollectionDeserializer.java
index c3cb45f..6639d75 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/CollectionDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/CollectionDeserializer.java
@@ -4,15 +4,16 @@
import java.util.*;
import com.fasterxml.jackson.annotation.JsonFormat;
+
import com.fasterxml.jackson.core.*;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
-import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
-import com.fasterxml.jackson.databind.deser.UnresolvedForwardReference;
-import com.fasterxml.jackson.databind.deser.ValueInstantiator;
+import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId.Referring;
import com.fasterxml.jackson.databind.deser.std.ContainerDeserializerBase;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Basic serializer that can take JSON "Array" structure and
@@ -32,8 +33,6 @@
// // Configuration
- protected final JavaType _collectionType;
-
/**
* Value deserializer.
*/
@@ -55,15 +54,6 @@
*/
protected final JsonDeserializer<Object> _delegateDeserializer;
- /**
- * Specific override for this instance (from proper, or global per-type overrides)
- * to indicate whether single value may be taken to mean an unwrapped one-element array
- * or not. If null, left to global defaults.
- *
- * @since 2.7
- */
- protected final Boolean _unwrapSingle;
-
// NOTE: no PropertyBasedCreator, as JSON Arrays have no properties
/*
@@ -80,25 +70,24 @@
JsonDeserializer<Object> valueDeser,
TypeDeserializer valueTypeDeser, ValueInstantiator valueInstantiator)
{
- this(collectionType, valueDeser, valueTypeDeser, valueInstantiator, null, null);
+ this(collectionType, valueDeser, valueTypeDeser, valueInstantiator, null, null, null);
}
/**
* Constructor used when creating contextualized instances.
+ *
+ * @since 2.9
*/
protected CollectionDeserializer(JavaType collectionType,
JsonDeserializer<Object> valueDeser, TypeDeserializer valueTypeDeser,
- ValueInstantiator valueInstantiator,
- JsonDeserializer<Object> delegateDeser,
- Boolean unwrapSingle)
+ ValueInstantiator valueInstantiator, JsonDeserializer<Object> delegateDeser,
+ NullValueProvider nuller, Boolean unwrapSingle)
{
- super(collectionType);
- _collectionType = collectionType;
+ super(collectionType, nuller, unwrapSingle);
_valueDeserializer = valueDeser;
_valueTypeDeserializer = valueTypeDeser;
_valueInstantiator = valueInstantiator;
_delegateDeserializer = delegateDeser;
- _unwrapSingle = unwrapSingle;
}
/**
@@ -107,42 +96,27 @@
*/
protected CollectionDeserializer(CollectionDeserializer src)
{
- super(src._collectionType);
- _collectionType = src._collectionType;
+ super(src);
_valueDeserializer = src._valueDeserializer;
_valueTypeDeserializer = src._valueTypeDeserializer;
_valueInstantiator = src._valueInstantiator;
_delegateDeserializer = src._delegateDeserializer;
- _unwrapSingle = src._unwrapSingle;
}
/**
* Fluent-factory method call to construct contextual instance.
*
- * @since 2.7
+ * @since 2.9
*/
@SuppressWarnings("unchecked")
protected CollectionDeserializer withResolved(JsonDeserializer<?> dd,
JsonDeserializer<?> vd, TypeDeserializer vtd,
- Boolean unwrapSingle)
+ NullValueProvider nuller, Boolean unwrapSingle)
{
- if ((dd == _delegateDeserializer) && (vd == _valueDeserializer) && (vtd == _valueTypeDeserializer)
- && (_unwrapSingle == unwrapSingle)) {
- return this;
- }
- return new CollectionDeserializer(_collectionType,
+ return new CollectionDeserializer(_containerType,
(JsonDeserializer<Object>) vd, vtd,
- _valueInstantiator, (JsonDeserializer<Object>) dd, unwrapSingle);
- }
-
- /**
- * @deprecated Since 2.7 as it does not pass `unwrapSingle`
- */
- @Deprecated // since 2.7 -- will not retain "unwrapSingle" setting
- protected CollectionDeserializer withResolved(JsonDeserializer<?> dd,
- JsonDeserializer<?> vd, TypeDeserializer vtd)
- {
- return withResolved(dd, vd, vtd, _unwrapSingle);
+ _valueInstantiator, (JsonDeserializer<Object>) dd,
+ nuller, unwrapSingle);
}
// Important: do NOT cache if polymorphic values
@@ -176,17 +150,19 @@
if (_valueInstantiator.canCreateUsingDelegate()) {
JavaType delegateType = _valueInstantiator.getDelegateType(ctxt.getConfig());
if (delegateType == null) {
- throw new IllegalArgumentException("Invalid delegate-creator definition for "+_collectionType
- +": value instantiator ("+_valueInstantiator.getClass().getName()
- +") returned true for 'canCreateUsingDelegate()', but null for 'getDelegateType()'");
+ ctxt.reportBadDefinition(_containerType, String.format(
+"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingDelegate()', but null for 'getDelegateType()'",
+_containerType,
+ _valueInstantiator.getClass().getName()));
}
delegateDeser = findDeserializer(ctxt, delegateType, property);
} else if (_valueInstantiator.canCreateUsingArrayDelegate()) {
JavaType delegateType = _valueInstantiator.getArrayDelegateType(ctxt.getConfig());
if (delegateType == null) {
- throw new IllegalArgumentException("Invalid array-delegate-creator definition for "+_collectionType
- +": value instantiator ("+_valueInstantiator.getClass().getName()
- +") returned true for 'canCreateUsingArrayDelegate()', but null for 'getArrayDelegateType()'");
+ ctxt.reportBadDefinition(_containerType, String.format(
+"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingArrayDelegate()', but null for 'getArrayDelegateType()'",
+ _containerType,
+ _valueInstantiator.getClass().getName()));
}
delegateDeser = findDeserializer(ctxt, delegateType, property);
}
@@ -201,7 +177,7 @@
// May have a content converter
valueDeser = findConvertingContentDeserializer(ctxt, property, valueDeser);
- final JavaType vt = _collectionType.getContentType();
+ final JavaType vt = _containerType.getContentType();
if (valueDeser == null) {
valueDeser = ctxt.findContextualValueDeserializer(vt, property);
} else { // if directly assigned, probably not yet contextual, so:
@@ -212,7 +188,17 @@
if (valueTypeDeser != null) {
valueTypeDeser = valueTypeDeser.forProperty(property);
}
- return withResolved(delegateDeser, valueDeser, valueTypeDeser, unwrapSingle);
+ NullValueProvider nuller = findContentNullProvider(ctxt, property, valueDeser);
+ if ( (unwrapSingle != _unwrapSingle)
+ || (nuller != _nullProvider)
+ || (delegateDeser != _delegateDeserializer)
+ || (valueDeser != _valueDeserializer)
+ || (valueTypeDeser != _valueTypeDeserializer)
+ ) {
+ return withResolved(delegateDeser, valueDeser, valueTypeDeser,
+ nuller, unwrapSingle);
+ }
+ return this;
}
/*
@@ -222,21 +208,21 @@
*/
@Override
- public JavaType getContentType() {
- return _collectionType.getContentType();
- }
-
- @Override
public JsonDeserializer<Object> getContentDeserializer() {
return _valueDeserializer;
}
-
+
+ @Override
+ public ValueInstantiator getValueInstantiator() {
+ return _valueInstantiator;
+ }
+
/*
/**********************************************************
/* JsonDeserializer API
/**********************************************************
*/
-
+
@SuppressWarnings("unchecked")
@Override
public Collection<Object> deserialize(JsonParser p, DeserializationContext ctxt)
@@ -246,19 +232,28 @@
return (Collection<Object>) _valueInstantiator.createUsingDelegate(ctxt,
_delegateDeserializer.deserialize(p, ctxt));
}
- /* Empty String may be ok; bit tricky to check, however, since
- * there is also possibility of "auto-wrapping" of single-element arrays.
- * Hence we only accept empty String here.
- */
+ // Empty String may be ok; bit tricky to check, however, since
+ // there is also possibility of "auto-wrapping" of single-element arrays.
+ // Hence we only accept empty String here.
if (p.hasToken(JsonToken.VALUE_STRING)) {
String str = p.getText();
if (str.length() == 0) {
return (Collection<Object>) _valueInstantiator.createFromString(ctxt, str);
}
}
- return deserialize(p, ctxt, (Collection<Object>) _valueInstantiator.createUsingDefault(ctxt));
+ return deserialize(p, ctxt, createDefaultInstance(ctxt));
}
+ /**
+ * @since 2.9
+ */
+ @SuppressWarnings("unchecked")
+ protected Collection<Object> createDefaultInstance(DeserializationContext ctxt)
+ throws IOException
+ {
+ return (Collection<Object>) _valueInstantiator.createUsingDefault(ctxt);
+ }
+
@Override
public Collection<Object> deserialize(JsonParser p, DeserializationContext ctxt,
Collection<Object> result)
@@ -275,14 +270,17 @@
final TypeDeserializer typeDeser = _valueTypeDeserializer;
CollectionReferringAccumulator referringAccumulator =
(valueDes.getObjectIdReader() == null) ? null :
- new CollectionReferringAccumulator(_collectionType.getContentType().getRawClass(), result);
+ new CollectionReferringAccumulator(_containerType.getContentType().getRawClass(), result);
JsonToken t;
while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
try {
Object value;
if (t == JsonToken.VALUE_NULL) {
- value = valueDes.getNullValue(ctxt);
+ if (_skipNullValues) {
+ continue;
+ }
+ value = _nullProvider.getNullValue(ctxt);
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
@@ -302,8 +300,8 @@
reference.getRoid().appendReferring(ref);
} catch (Exception e) {
boolean wrap = (ctxt == null) || ctxt.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS);
- if (!wrap && e instanceof RuntimeException) {
- throw (RuntimeException)e;
+ if (!wrap) {
+ ClassUtil.throwIfRTE(e);
}
throw JsonMappingException.wrapWithPath(e, result, result.size());
}
@@ -335,7 +333,7 @@
((_unwrapSingle == null) &&
ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY));
if (!canWrap) {
- return (Collection<Object>) ctxt.handleUnexpectedToken(_collectionType.getRawClass(), p);
+ return (Collection<Object>) ctxt.handleUnexpectedToken(_containerType.getRawClass(), p);
}
JsonDeserializer<Object> valueDes = _valueDeserializer;
final TypeDeserializer typeDeser = _valueTypeDeserializer;
@@ -345,7 +343,11 @@
try {
if (t == JsonToken.VALUE_NULL) {
- value = valueDes.getNullValue(ctxt);
+ // 03-Feb-2017, tatu: Hmmh. I wonder... let's try skipping here, too
+ if (_skipNullValues) {
+ return result;
+ }
+ value = _nullProvider.getNullValue(ctxt);
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/ContainerDeserializerBase.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/ContainerDeserializerBase.java
index 251a51c..2bf374a 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/ContainerDeserializerBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/ContainerDeserializerBase.java
@@ -3,10 +3,14 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
-import com.fasterxml.jackson.databind.JavaType;
-import com.fasterxml.jackson.databind.JsonDeserializer;
-import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
+import com.fasterxml.jackson.databind.deser.ValueInstantiator;
+import com.fasterxml.jackson.databind.deser.impl.NullsConstantProvider;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+import com.fasterxml.jackson.databind.util.AccessPattern;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Intermediate base deserializer class that adds more shared accessor
@@ -15,9 +19,64 @@
@SuppressWarnings("serial")
public abstract class ContainerDeserializerBase<T>
extends StdDeserializer<T>
+ implements ValueInstantiator.Gettable // since 2.9
{
- protected ContainerDeserializerBase(JavaType selfType) {
+ protected final JavaType _containerType;
+
+ /**
+ * Handler we need for dealing with nulls.
+ *
+ * @since 2.9
+ */
+ protected final NullValueProvider _nullProvider;
+
+ /**
+ * Specific override for this instance (from proper, or global per-type overrides)
+ * to indicate whether single value may be taken to mean an unwrapped one-element array
+ * or not. If null, left to global defaults.
+ *
+ * @since 2.9 (demoted from sub-classes where added in 2.7)
+ */
+ protected final Boolean _unwrapSingle;
+
+ /**
+ * Marker flag set if the <code>_nullProvider</code> indicates that all null
+ * content values should be skipped (instead of being possibly converted).
+ *
+ * @since 2.9
+ */
+ protected final boolean _skipNullValues;
+
+ protected ContainerDeserializerBase(JavaType selfType,
+ NullValueProvider nuller, Boolean unwrapSingle) {
super(selfType);
+ _containerType = selfType;
+ _unwrapSingle = unwrapSingle;
+ _nullProvider = nuller;
+ _skipNullValues = NullsConstantProvider.isSkipper(nuller);
+ }
+
+ protected ContainerDeserializerBase(JavaType selfType) {
+ this(selfType, null, null);
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected ContainerDeserializerBase(ContainerDeserializerBase<?> base) {
+ this(base, base._nullProvider, base._unwrapSingle);
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected ContainerDeserializerBase(ContainerDeserializerBase<?> base,
+ NullValueProvider nuller, Boolean unwrapSingle) {
+ super(base._containerType);
+ _containerType = base._containerType;
+ _nullProvider = nuller;
+ _unwrapSingle = unwrapSingle;
+ _skipNullValues = NullsConstantProvider.isSkipper(nuller);
}
/*
@@ -26,16 +85,25 @@
/**********************************************************
*/
+ @Override // since 2.9
+ public JavaType getValueType() { return _containerType; }
+
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return Boolean.TRUE;
+ }
+
@Override
public SettableBeanProperty findBackReference(String refName) {
JsonDeserializer<Object> valueDeser = getContentDeserializer();
if (valueDeser == null) {
- throw new IllegalArgumentException("Can not handle managed/back reference '"+refName
- +"': type: container deserializer of type "+getClass().getName()+" returned null for 'getContentDeserializer()'");
+ throw new IllegalArgumentException(String.format(
+ "Can not handle managed/back reference '%s': type: container deserializer of type %s returned null for 'getContentDeserializer()'",
+ refName, getClass().getName()));
}
return valueDeser.findBackReference(refName);
}
-
+
/*
/**********************************************************
/* Extended API
@@ -46,13 +114,48 @@
* Accessor for declared type of contained value elements; either exact
* type, or one of its supertypes.
*/
- public abstract JavaType getContentType();
+ public JavaType getContentType() {
+ if (_containerType == null) {
+ return TypeFactory.unknownType(); // should never occur but...
+ }
+ return _containerType.getContentType();
+ }
/**
* Accesor for deserializer use for deserializing content values.
*/
public abstract JsonDeserializer<Object> getContentDeserializer();
+ /**
+ * @since 2.9
+ */
+ @Override
+ public ValueInstantiator getValueInstantiator() {
+ return null;
+ }
+
+ @Override // since 2.9
+ public AccessPattern getEmptyAccessPattern() {
+ // 02-Feb-2017, tatu: Empty containers are usually constructed as needed
+ // and may not be shared; for some deserializers this may be further refined.
+ return AccessPattern.DYNAMIC;
+ }
+
+ @Override // since 2.9
+ public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
+ ValueInstantiator vi = getValueInstantiator();
+ if (vi == null || !vi.canCreateUsingDefault()) {
+ JavaType type = getValueType();
+ ctxt.reportBadDefinition(type,
+ String.format("Can not create empty instance of %s, no default Creator", type));
+ }
+ try {
+ return vi.createUsingDefault(ctxt);
+ } catch (IOException e) {
+ return ClassUtil.throwAsMappingException(ctxt, e);
+ }
+ }
+
/*
/**********************************************************
/* Shared methods for sub-classes
@@ -62,24 +165,20 @@
/**
* Helper method called by various Map(-like) deserializers.
*/
- protected void wrapAndThrow(Throwable t, Object ref, String key) throws IOException
+ protected <BOGUS> BOGUS wrapAndThrow(Throwable t, Object ref, String key) throws IOException
{
// to handle StackOverflow:
while (t instanceof InvocationTargetException && t.getCause() != null) {
t = t.getCause();
}
// Errors and "plain" IOExceptions to be passed as is
- if (t instanceof Error) {
- throw (Error) t;
- }
+ ClassUtil.throwIfError(t);
// ... except for mapping exceptions
if (t instanceof IOException && !(t instanceof JsonMappingException)) {
throw (IOException) t;
}
// for [databind#1141]
- if (key == null) {
- key = "N/A";
- }
- throw JsonMappingException.wrapWithPath(t, ref, key);
+ throw JsonMappingException.wrapWithPath(t, ref,
+ ClassUtil.nonNull(key, "N/A"));
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/DateDeserializers.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/DateDeserializers.java
index 4a840bc..14bddb3 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/DateDeserializers.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/DateDeserializers.java
@@ -1,20 +1,20 @@
package com.fasterxml.jackson.databind.deser.std;
import java.io.IOException;
+import java.lang.reflect.Constructor;
import java.sql.Timestamp;
import java.text.*;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonFormat;
+
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
-import com.fasterxml.jackson.databind.BeanProperty;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.JsonDeserializer;
-import com.fasterxml.jackson.databind.JsonMappingException;
+
+import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
+import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.StdDateFormat;
/**
@@ -59,7 +59,7 @@
}
return null;
}
-
+
/*
/**********************************************************
/* Intermediate class for Date-based ones
@@ -80,7 +80,7 @@
* Let's also keep format String for reference, to use for error messages
*/
protected final String _formatString;
-
+
protected DateBasedDeserializer(Class<?> clz) {
super(clz);
_customFormat = null;
@@ -97,54 +97,87 @@
protected abstract DateBasedDeserializer<T> withDateFormat(DateFormat df, String formatStr);
@Override
- public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
+ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
+ BeanProperty property)
throws JsonMappingException
{
- if (property != null) {
- JsonFormat.Value format = findFormatOverrides(ctxt, property,
- this.handledType());
- if (format != null) {
- TimeZone tz = format.getTimeZone();
- // First: fully custom pattern?
- if (format.hasPattern()) {
- final String pattern = format.getPattern();
+ final JsonFormat.Value format = findFormatOverrides(ctxt, property,
+ handledType());
+
+ if (format != null) {
+ TimeZone tz = format.getTimeZone();
+ final Boolean lenient = format.getLenient();
+
+ // First: fully custom pattern?
+ if (format.hasPattern()) {
+ final String pattern = format.getPattern();
+ final Locale loc = format.hasLocale() ? format.getLocale() : ctxt.getLocale();
+ SimpleDateFormat df = new SimpleDateFormat(pattern, loc);
+ if (tz == null) {
+ tz = ctxt.getTimeZone();
+ }
+ df.setTimeZone(tz);
+ if (lenient != null) {
+ df.setLenient(lenient);
+ }
+ return withDateFormat(df, pattern);
+ }
+ // But if not, can still override timezone
+ if (tz != null) {
+ DateFormat df = ctxt.getConfig().getDateFormat();
+ // one shortcut: with our custom format, can simplify handling a bit
+ if (df.getClass() == StdDateFormat.class) {
final Locale loc = format.hasLocale() ? format.getLocale() : ctxt.getLocale();
- SimpleDateFormat df = new SimpleDateFormat(pattern, loc);
- if (tz == null) {
- tz = ctxt.getTimeZone();
+ StdDateFormat std = (StdDateFormat) df;
+ std = std.withTimeZone(tz);
+ std = std.withLocale(loc);
+ if (lenient != null) {
+ std = std.withLenient(lenient);
}
+ df = std;
+ } else {
+ // otherwise need to clone, re-set timezone:
+ df = (DateFormat) df.clone();
df.setTimeZone(tz);
- return withDateFormat(df, pattern);
- }
- // But if not, can still override timezone
- if (tz != null) {
- DateFormat df = ctxt.getConfig().getDateFormat();
- // one shortcut: with our custom format, can simplify handling a bit
- if (df.getClass() == StdDateFormat.class) {
- final Locale loc = format.hasLocale() ? format.getLocale() : ctxt.getLocale();
- StdDateFormat std = (StdDateFormat) df;
- std = std.withTimeZone(tz);
- std = std.withLocale(loc);
- df = std;
- } else {
- // otherwise need to clone, re-set timezone:
- df = (DateFormat) df.clone();
- df.setTimeZone(tz);
+ if (lenient != null) {
+ df.setLenient(lenient);
}
- return withDateFormat(df, _formatString);
}
+ return withDateFormat(df, _formatString);
+ }
+ // or maybe even just leniency?
+ if (lenient != null) {
+ DateFormat df = ctxt.getConfig().getDateFormat();
+ String pattern = _formatString;
+ // one shortcut: with our custom format, can simplify handling a bit
+ if (df.getClass() == StdDateFormat.class) {
+ StdDateFormat std = (StdDateFormat) df;
+ std = std.withLenient(lenient);
+ df = std;
+ pattern = std.toPattern();
+ } else {
+ // otherwise need to clone,
+ df = (DateFormat) df.clone();
+ df.setLenient(lenient);
+ if (df instanceof SimpleDateFormat) {
+ ((SimpleDateFormat) df).toPattern();
+ }
+ }
+ if (pattern == null) {
+ pattern = "[unknown]";
+ }
+ return withDateFormat(df, pattern);
}
}
return this;
}
-
+
@Override
protected java.util.Date _parseDate(JsonParser p, DeserializationContext ctxt)
throws IOException
{
if (_customFormat != null) {
- JsonToken t = p.getCurrentToken();
- if (t == JsonToken.VALUE_STRING) {
+ if (p.hasToken(JsonToken.VALUE_STRING)) {
String str = p.getText().trim();
if (str.length() == 0) {
return (Date) getEmptyValue(ctxt);
@@ -158,16 +191,6 @@
}
}
}
- // [databind#381]
- if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final Date parsed = _parseDate(p, ctxt);
- t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
- }
}
return super._parseDate(p, ctxt);
}
@@ -185,29 +208,32 @@
/**
* We may know actual expected type; if so, it will be
* used for instantiation.
+ *
+ * @since 2.9
*/
- protected final Class<? extends Calendar> _calendarClass;
-
+ protected final Constructor<Calendar> _defaultCtor;
+
public CalendarDeserializer() {
super(Calendar.class);
- _calendarClass = null;
+ _defaultCtor = null;
}
+ @SuppressWarnings("unchecked")
public CalendarDeserializer(Class<? extends Calendar> cc) {
super(cc);
- _calendarClass = cc;
+ _defaultCtor = (Constructor<Calendar>) ClassUtil.findConstructor(cc, false);
}
public CalendarDeserializer(CalendarDeserializer src, DateFormat df, String formatString) {
super(src, df, formatString);
- _calendarClass = src._calendarClass;
+ _defaultCtor = src._defaultCtor;
}
@Override
protected CalendarDeserializer withDateFormat(DateFormat df, String formatString) {
return new CalendarDeserializer(this, df, formatString);
}
-
+
@Override
public Calendar deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
@@ -215,11 +241,11 @@
if (d == null) {
return null;
}
- if (_calendarClass == null) {
+ if (_defaultCtor == null) {
return ctxt.constructCalendar(d);
}
try {
- Calendar c = _calendarClass.newInstance();
+ Calendar c = _defaultCtor.newInstance();
c.setTimeInMillis(d.getTime());
TimeZone tz = ctxt.getTimeZone();
if (tz != null) {
@@ -227,7 +253,7 @@
}
return c;
} catch (Exception e) {
- return (Calendar) ctxt.handleInstantiationProblem(_calendarClass, d, e);
+ return (Calendar) ctxt.handleInstantiationProblem(handledType(), d, e);
}
}
}
@@ -255,8 +281,8 @@
}
@Override
- public java.util.Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
- return _parseDate(jp, ctxt);
+ public java.util.Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
+ return _parseDate(p, ctxt);
}
}
@@ -278,8 +304,8 @@
}
@Override
- public java.sql.Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
- Date d = _parseDate(jp, ctxt);
+ public java.sql.Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
+ Date d = _parseDate(p, ctxt);
return (d == null) ? null : new java.sql.Date(d.getTime());
}
}
@@ -304,9 +330,9 @@
}
@Override
- public java.sql.Timestamp deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException
+ public java.sql.Timestamp deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
- Date d = _parseDate(jp, ctxt);
+ Date d = _parseDate(p, ctxt);
return (d == null) ? null : new Timestamp(d.getTime());
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/DelegatingDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/DelegatingDeserializer.java
index 0c14ca2..92491e3 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/DelegatingDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/DelegatingDeserializer.java
@@ -4,11 +4,11 @@
import java.util.Collection;
import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.impl.ObjectIdReader;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
+import com.fasterxml.jackson.databind.util.AccessPattern;
/**
* Base class that simplifies implementations of {@link JsonDeserializer}s
@@ -32,22 +32,19 @@
/**********************************************************************
*/
- public DelegatingDeserializer(JsonDeserializer<?> delegatee)
+ public DelegatingDeserializer(JsonDeserializer<?> d)
{
- super(_figureType(delegatee));
- _delegatee = delegatee;
+ super(d.getClass());
+ _delegatee = d;
}
- protected abstract JsonDeserializer<?> newDelegatingInstance(JsonDeserializer<?> newDelegatee);
+ /*
+ /**********************************************************************
+ /* Abstract methods to implement
+ /**********************************************************************
+ */
- private static Class<?> _figureType(JsonDeserializer<?> deser)
- {
- Class<?> cls = deser.handledType();
- if (cls != null) {
- return cls;
- }
- return Object.class;
- }
+ protected abstract JsonDeserializer<?> newDelegatingInstance(JsonDeserializer<?> newDelegatee);
/*
/**********************************************************************
@@ -76,23 +73,13 @@
return newDelegatingInstance(del);
}
- /**
- * @deprecated Since 2.3, use {@link #newDelegatingInstance} instead
- */
- @Deprecated
- protected JsonDeserializer<?> _createContextual(DeserializationContext ctxt,
- BeanProperty property, JsonDeserializer<?> newDelegatee)
+ @Override
+ public JsonDeserializer<?> replaceDelegatee(JsonDeserializer<?> delegatee)
{
- if (newDelegatee == _delegatee) {
+ if (delegatee == _delegatee) {
return this;
}
- return newDelegatingInstance(newDelegatee);
- }
-
- @Override
- public SettableBeanProperty findBackReference(String logicalName) {
- // [Issue#253]: Hope this works....
- return _delegatee.findBackReference(logicalName);
+ return newDelegatingInstance(delegatee);
}
/*
@@ -102,27 +89,27 @@
*/
@Override
- public Object deserialize(JsonParser jp, DeserializationContext ctxt)
- throws IOException, JsonProcessingException
+ public Object deserialize(JsonParser p, DeserializationContext ctxt)
+ throws IOException
{
- return _delegatee.deserialize(jp, ctxt);
+ return _delegatee.deserialize(p, ctxt);
}
@SuppressWarnings("unchecked")
@Override
- public Object deserialize(JsonParser jp, DeserializationContext ctxt,
+ public Object deserialize(JsonParser p, DeserializationContext ctxt,
Object intoValue)
- throws IOException, JsonProcessingException
+ throws IOException
{
- return ((JsonDeserializer<Object>)_delegatee).deserialize(jp, ctxt, intoValue);
+ return ((JsonDeserializer<Object>)_delegatee).deserialize(p, ctxt, intoValue);
}
@Override
- public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt,
+ public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer)
- throws IOException, JsonProcessingException
+ throws IOException
{
- return _delegatee.deserializeWithType(jp, ctxt, typeDeserializer);
+ return _delegatee.deserializeWithType(p, ctxt, typeDeserializer);
}
/*
@@ -132,12 +119,27 @@
*/
@Override
- public JsonDeserializer<?> replaceDelegatee(JsonDeserializer<?> delegatee)
- {
- if (delegatee == _delegatee) {
- return this;
- }
- return newDelegatingInstance(delegatee);
+ public boolean isCachable() { return _delegatee.isCachable(); }
+
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return _delegatee.supportsUpdate(config);
+ }
+
+ @Override
+ public JsonDeserializer<?> getDelegatee() {
+ return _delegatee;
+ }
+
+ @Override
+ public SettableBeanProperty findBackReference(String logicalName) {
+ // [databind#253]: Hope this works....
+ return _delegatee.findBackReference(logicalName);
+ }
+
+ @Override
+ public AccessPattern getNullAccessPattern() {
+ return _delegatee.getNullAccessPattern();
}
@Override
@@ -151,26 +153,8 @@
}
@Override
- @Deprecated // remove in 2.7
- public Object getNullValue() { return _delegatee.getNullValue(); }
-
- // Remove in 2.7
- @Override
- @Deprecated // remove in 2.7
- public Object getEmptyValue() { return _delegatee.getEmptyValue(); }
-
-
- @Override
public Collection<Object> getKnownPropertyNames() { return _delegatee.getKnownPropertyNames(); }
-
- @Override
- public boolean isCachable() { return _delegatee.isCachable(); }
@Override
public ObjectIdReader getObjectIdReader() { return _delegatee.getObjectIdReader(); }
-
- @Override
- public JsonDeserializer<?> getDelegatee() {
- return _delegatee;
- }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java
index c3ad93e..18923cd 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java
@@ -2,9 +2,13 @@
import java.io.IOException;
+import com.fasterxml.jackson.annotation.JsonFormat;
+
import com.fasterxml.jackson.core.*;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
+import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
@@ -19,6 +23,7 @@
@JacksonStdImpl // was missing until 2.6
public class EnumDeserializer
extends StdScalarDeserializer<Object>
+ implements ContextualDeserializer
{
private static final long serialVersionUID = 1L;
@@ -41,16 +46,42 @@
* @since 2.7.3
*/
protected CompactStringObjectMap _lookupByToString;
-
- public EnumDeserializer(EnumResolver byNameResolver)
+
+ protected final Boolean _caseInsensitive;
+
+ /**
+ * @since 2.9
+ */
+ public EnumDeserializer(EnumResolver byNameResolver, Boolean caseInsensitive)
{
super(byNameResolver.getEnumClass());
_lookupByName = byNameResolver.constructLookup();
_enumsByIndex = byNameResolver.getRawEnums();
_enumDefaultValue = byNameResolver.getDefaultValue();
+ _caseInsensitive = caseInsensitive;
}
/**
+ * @since 2.9
+ */
+ protected EnumDeserializer(EnumDeserializer base, Boolean caseInsensitive)
+ {
+ super(base);
+ _lookupByName = base._lookupByName;
+ _enumsByIndex = base._enumsByIndex;
+ _enumDefaultValue = base._enumDefaultValue;
+ _caseInsensitive = caseInsensitive;
+ }
+
+ /**
+ * @deprecated Since 2.9
+ */
+ @Deprecated
+ public EnumDeserializer(EnumResolver byNameResolver) {
+ this(byNameResolver, null);
+ }
+
+ /**
* @deprecated Since 2.8
*/
@Deprecated
@@ -98,6 +129,28 @@
return new FactoryBasedEnumDeserializer(enumClass, factory);
}
+ /**
+ * @since 2.9
+ */
+ public EnumDeserializer withResolved(Boolean caseInsensitive) {
+ if (_caseInsensitive == caseInsensitive) {
+ return this;
+ }
+ return new EnumDeserializer(this, caseInsensitive);
+ }
+
+ @Override // since 2.9
+ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
+ BeanProperty property) throws JsonMappingException
+ {
+ Boolean caseInsensitive = findFormatFeature(ctxt, property, handledType(),
+ JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
+ if (caseInsensitive == null) {
+ caseInsensitive = _caseInsensitive;
+ }
+ return withResolved(caseInsensitive);
+ }
+
/*
/**********************************************************
/* Default JsonDeserializer implementation
@@ -167,17 +220,25 @@
if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
return getEmptyValue(ctxt);
}
- } else if (!ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) {
- // [databind#149]: Allow use of 'String' indexes as well -- unless prohibited (as per above)
- char c = name.charAt(0);
- if (c >= '0' && c <= '9') {
- try {
- int index = Integer.parseInt(name);
- if (index >= 0 && index < _enumsByIndex.length) {
- return _enumsByIndex[index];
+ } else {
+ // [databind#1313]: Case insensitive enum deserialization
+ if (Boolean.TRUE.equals(_caseInsensitive)) {
+ Object match = lookup.findCaseInsensitive(name);
+ if (match != null) {
+ return match;
+ }
+ } else if (!ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) {
+ // [databind#149]: Allow use of 'String' indexes as well -- unless prohibited (as per above)
+ char c = name.charAt(0);
+ if (c >= '0' && c <= '9') {
+ try {
+ int index = Integer.parseInt(name);
+ if (index >= 0 && index < _enumsByIndex.length) {
+ return _enumsByIndex[index];
+ }
+ } catch (NumberFormatException e) {
+ // fine, ignore, was not an integer
}
- } catch (NumberFormatException e) {
- // fine, ignore, was not an integer
}
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumMapDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumMapDeserializer.java
index d227834..03a4c56 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumMapDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumMapDeserializer.java
@@ -6,7 +6,14 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
+import com.fasterxml.jackson.databind.deser.ResolvableDeserializer;
+import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
+import com.fasterxml.jackson.databind.deser.ValueInstantiator;
+import com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator;
+import com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Deserializer for {@link EnumMap} values.
@@ -17,12 +24,10 @@
@SuppressWarnings({ "unchecked", "rawtypes" })
public class EnumMapDeserializer
extends ContainerDeserializerBase<EnumMap<?,?>>
- implements ContextualDeserializer
+ implements ContextualDeserializer, ResolvableDeserializer
{
private static final long serialVersionUID = 1;
- protected final JavaType _mapType;
-
protected final Class<?> _enumClass;
protected KeyDeserializer _keyDeserializer;
@@ -34,31 +39,127 @@
* is the type deserializer that can handle it
*/
protected final TypeDeserializer _valueTypeDeserializer;
+
+ // // Instance construction settings:
+ /**
+ * @since 2.9
+ */
+ protected final ValueInstantiator _valueInstantiator;
+
+ /**
+ * Deserializer that is used iff delegate-based creator is
+ * to be used for deserializing from JSON Object.
+ */
+ protected JsonDeserializer<Object> _delegateDeserializer;
+
+ /**
+ * If the Map is to be instantiated using non-default constructor
+ * or factory method
+ * that takes one or more named properties as argument(s),
+ * this creator is used for instantiation.
+ */
+ protected PropertyBasedCreator _propertyBasedCreator;
+
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
- public EnumMapDeserializer(JavaType mapType, KeyDeserializer keyDeserializer, JsonDeserializer<?> valueDeser, TypeDeserializer valueTypeDeser)
+ /**
+ * @since 2.9
+ */
+ public EnumMapDeserializer(JavaType mapType, ValueInstantiator valueInst,
+ KeyDeserializer keyDeser, JsonDeserializer<?> valueDeser, TypeDeserializer vtd,
+ NullValueProvider nuller)
{
- super(mapType);
- _mapType = mapType;
+ super(mapType, nuller, null);
_enumClass = mapType.getKeyType().getRawClass();
- _keyDeserializer = keyDeserializer;
+ _keyDeserializer = keyDeser;
_valueDeserializer = (JsonDeserializer<Object>) valueDeser;
- _valueTypeDeserializer = valueTypeDeser;
+ _valueTypeDeserializer = vtd;
+ _valueInstantiator = valueInst;
}
- public EnumMapDeserializer withResolved(KeyDeserializer keyDeserializer, JsonDeserializer<?> valueDeserializer, TypeDeserializer valueTypeDeser)
+ /**
+ * @since 2.9
+ */
+ protected EnumMapDeserializer(EnumMapDeserializer base,
+ KeyDeserializer keyDeser, JsonDeserializer<?> valueDeser, TypeDeserializer vtd,
+ NullValueProvider nuller)
{
- if ((keyDeserializer == _keyDeserializer) && (valueDeserializer == _valueDeserializer) && (valueTypeDeser == _valueTypeDeserializer)) {
- return this;
- }
- return new EnumMapDeserializer(_mapType, keyDeserializer, valueDeserializer, _valueTypeDeserializer);
+ super(base, nuller, base._unwrapSingle);
+ _enumClass = base._enumClass;
+ _keyDeserializer = keyDeser;
+ _valueDeserializer = (JsonDeserializer<Object>) valueDeser;
+ _valueTypeDeserializer = vtd;
+
+ _valueInstantiator = base._valueInstantiator;
+ _delegateDeserializer = base._delegateDeserializer;
+ _propertyBasedCreator = base._propertyBasedCreator;
+ }
+
+ @Deprecated // since 2.9
+ public EnumMapDeserializer(JavaType mapType, KeyDeserializer keyDeser,
+ JsonDeserializer<?> valueDeser, TypeDeserializer vtd)
+ {
+ this(mapType, null, keyDeser, valueDeser, vtd, null);
}
+ public EnumMapDeserializer withResolved(KeyDeserializer keyDeserializer,
+ JsonDeserializer<?> valueDeserializer, TypeDeserializer valueTypeDeser,
+ NullValueProvider nuller)
+ {
+ if ((keyDeserializer == _keyDeserializer) && (nuller == _nullProvider)
+ && (valueDeserializer == _valueDeserializer) && (valueTypeDeser == _valueTypeDeserializer)) {
+ return this;
+ }
+ return new EnumMapDeserializer(this,
+ keyDeserializer, valueDeserializer, valueTypeDeser, nuller);
+ }
+
+ /*
+ /**********************************************************
+ /* Validation, post-processing (ResolvableDeserializer)
+ /**********************************************************
+ */
+
+ @Override
+ public void resolve(DeserializationContext ctxt) throws JsonMappingException
+ {
+ // May need to resolve types for delegate- and/or property-based creators:
+ if (_valueInstantiator != null) {
+ if (_valueInstantiator.canCreateUsingDelegate()) {
+ JavaType delegateType = _valueInstantiator.getDelegateType(ctxt.getConfig());
+ if (delegateType == null) {
+ ctxt.reportBadDefinition(_containerType, String.format(
+"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingDelegate()', but null for 'getDelegateType()'",
+ _containerType,
+ _valueInstantiator.getClass().getName()));
+ }
+ /* Theoretically should be able to get CreatorProperty for delegate
+ * parameter to pass; but things get tricky because DelegateCreator
+ * may contain injectable values. So, for now, let's pass nothing.
+ */
+ _delegateDeserializer = findDeserializer(ctxt, delegateType, null);
+ } else if (_valueInstantiator.canCreateUsingArrayDelegate()) {
+ JavaType delegateType = _valueInstantiator.getArrayDelegateType(ctxt.getConfig());
+ if (delegateType == null) {
+ ctxt.reportBadDefinition(_containerType, String.format(
+"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingArrayDelegate()', but null for 'getArrayDelegateType()'",
+ _containerType,
+ _valueInstantiator.getClass().getName()));
+ }
+ _delegateDeserializer = findDeserializer(ctxt, delegateType, null);
+ } else if (_valueInstantiator.canCreateFromObjectWith()) {
+ SettableBeanProperty[] creatorProps = _valueInstantiator.getFromObjectArguments(ctxt.getConfig());
+ _propertyBasedCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator, creatorProps,
+ ctxt.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
+ }
+ }
+ }
+
/**
* Method called to finalize setup of this deserializer,
* when it is known for which property deserializer is needed for.
@@ -69,24 +170,24 @@
// note: instead of finding key deserializer, with enums we actually
// work with regular deserializers (less code duplication; but not
// quite as clean as it ought to be)
- KeyDeserializer kd = _keyDeserializer;
- if (kd == null) {
- kd = ctxt.findKeyDeserializer(_mapType.getKeyType(), property);
+ KeyDeserializer keyDeser = _keyDeserializer;
+ if (keyDeser == null) {
+ keyDeser = ctxt.findKeyDeserializer(_containerType.getKeyType(), property);
}
- JsonDeserializer<?> vd = _valueDeserializer;
- final JavaType vt = _mapType.getContentType();
- if (vd == null) {
- vd = ctxt.findContextualValueDeserializer(vt, property);
+ JsonDeserializer<?> valueDeser = _valueDeserializer;
+ final JavaType vt = _containerType.getContentType();
+ if (valueDeser == null) {
+ valueDeser = ctxt.findContextualValueDeserializer(vt, property);
} else { // if directly assigned, probably not yet contextual, so:
- vd = ctxt.handleSecondaryContextualization(vd, property, vt);
+ valueDeser = ctxt.handleSecondaryContextualization(valueDeser, property, vt);
}
TypeDeserializer vtd = _valueTypeDeserializer;
if (vtd != null) {
vtd = vtd.forProperty(property);
}
- return withResolved(kd, vd, vtd);
+ return withResolved(keyDeser, valueDeser, vtd, findContentNullProvider(ctxt, property, valueDeser));
}
-
+
/**
* Because of costs associated with constructing Enum resolvers,
* let's cache instances by default.
@@ -106,15 +207,16 @@
*/
@Override
- public JavaType getContentType() {
- return _mapType.getContentType();
- }
-
- @Override
public JsonDeserializer<Object> getContentDeserializer() {
return _valueDeserializer;
}
+ // Must override since we do not expose ValueInstantiator
+ @Override // since 2.9
+ public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
+ return constructMap(ctxt);
+ }
+
/*
/**********************************************************
/* Actual deserialization
@@ -125,49 +227,73 @@
public EnumMap<?,?> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
+ if (_propertyBasedCreator != null) {
+ return _deserializeUsingProperties(p, ctxt);
+ }
+ if (_delegateDeserializer != null) {
+ return (EnumMap<?,?>) _valueInstantiator.createUsingDelegate(ctxt,
+ _delegateDeserializer.deserialize(p, ctxt));
+ }
// Ok: must point to START_OBJECT
- if (p.getCurrentToken() != JsonToken.START_OBJECT) {
+ JsonToken t = p.getCurrentToken();
+ if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) {
+ // (empty) String may be ok however; or single-String-arg ctor
+ if (t == JsonToken.VALUE_STRING) {
+ return (EnumMap<?,?>) _valueInstantiator.createFromString(ctxt, p.getText());
+ }
+ // slightly redundant (since String was passed above), but also handles empty array case:
return _deserializeFromEmpty(p, ctxt);
}
- EnumMap result = constructMap();
+ EnumMap result = constructMap(ctxt);
+ return deserialize(p, ctxt, result);
+ }
+
+ @Override
+ public EnumMap<?,?> deserialize(JsonParser p, DeserializationContext ctxt,
+ EnumMap result)
+ throws IOException
+ {
+ // [databind#631]: Assign current value, to be accessible by custom deserializers
+ p.setCurrentValue(result);
+
final JsonDeserializer<Object> valueDes = _valueDeserializer;
final TypeDeserializer typeDeser = _valueTypeDeserializer;
+ String keyName;
- while ((p.nextToken()) == JsonToken.FIELD_NAME) {
- String keyName = p.getCurrentName(); // just for error message
+ while ((keyName = p.nextFieldName()) != null) {
// but we need to let key deserializer handle it separately, nonetheless
Enum<?> key = (Enum<?>) _keyDeserializer.deserializeKey(keyName, ctxt);
if (key == null) {
if (!ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)) {
return (EnumMap<?,?>) ctxt.handleWeirdStringValue(_enumClass, keyName,
"value not one of declared Enum instance names for %s",
- _mapType.getKeyType());
+ _containerType.getKeyType());
}
- /* 24-Mar-2012, tatu: Null won't work as a key anyway, so let's
- * just skip the entry then. But we must skip the value as well, if so.
- */
+ // 24-Mar-2012, tatu: Null won't work as a key anyway, so let's
+ // just skip the entry then. But we must skip the value as well, if so.
p.nextToken();
p.skipChildren();
continue;
}
// And then the value...
JsonToken t = p.nextToken();
- /* note: MUST check for nulls separately: deserializers will
- * not handle them (and maybe fail or return bogus data)
- */
+ // note: MUST check for nulls separately: deserializers will
+ // not handle them (and maybe fail or return bogus data)
Object value;
try {
if (t == JsonToken.VALUE_NULL) {
- value = valueDes.getNullValue(ctxt);
+ if (_skipNullValues) {
+ continue;
+ }
+ value = _nullProvider.getNullValue(ctxt);
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
value = valueDes.deserializeWithType(p, ctxt, typeDeser);
}
} catch (Exception e) {
- wrapAndThrow(e, result, keyName);
- return null;
+ return wrapAndThrow(e, result, keyName);
}
result.put(key, value);
}
@@ -175,15 +301,103 @@
}
@Override
- public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer typeDeserializer)
- throws IOException, JsonProcessingException
+ public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
+ TypeDeserializer typeDeserializer)
+ throws IOException
{
// In future could check current token... for now this should be enough:
- return typeDeserializer.deserializeTypedFromObject(jp, ctxt);
+ return typeDeserializer.deserializeTypedFromObject(p, ctxt);
}
-
- protected EnumMap<?,?> constructMap() {
- return new EnumMap(_enumClass);
+
+ protected EnumMap<?,?> constructMap(DeserializationContext ctxt) throws JsonMappingException {
+ if (_valueInstantiator == null) {
+ return new EnumMap(_enumClass);
+ }
+ try {
+ if (!_valueInstantiator.canCreateUsingDefault()) {
+ return (EnumMap<?,?>) ctxt.handleMissingInstantiator(handledType(),
+ getValueInstantiator(), null,
+ "no default constructor found");
+ }
+ return (EnumMap<?,?>) _valueInstantiator.createUsingDefault(ctxt);
+ } catch (IOException e) {
+ return ClassUtil.throwAsMappingException(ctxt, e);
+ }
+ }
+
+ public EnumMap<?,?> _deserializeUsingProperties(JsonParser p, DeserializationContext ctxt) throws IOException
+ {
+ final PropertyBasedCreator creator = _propertyBasedCreator;
+ // null -> no ObjectIdReader for EnumMaps
+ PropertyValueBuffer buffer = creator.startBuilding(p, ctxt, null);
+
+ String keyName;
+ if (p.isExpectedStartObjectToken()) {
+ keyName = p.nextFieldName();
+ } else if (p.hasToken(JsonToken.FIELD_NAME)) {
+ keyName = p.getCurrentName();
+ } else {
+ keyName = null;
+ }
+
+ for (; keyName != null; keyName = p.nextFieldName()) {
+ JsonToken t = p.nextToken(); // to get to value
+ // creator property?
+ SettableBeanProperty prop = creator.findCreatorProperty(keyName);
+ if (prop != null) {
+ // Last property to set?
+ if (buffer.assignParameter(prop, prop.deserialize(p, ctxt))) {
+ EnumMap<?,?> result;
+ try {
+ result = (EnumMap<?,?>)creator.build(ctxt, buffer);
+ } catch (Exception e) {
+ return wrapAndThrow(e, _containerType.getRawClass(), keyName);
+ }
+ return deserialize(p, ctxt, result);
+ }
+ continue;
+ }
+ // other property? needs buffering
+ // but we need to let key deserializer handle it separately, nonetheless
+ Enum<?> key = (Enum<?>) _keyDeserializer.deserializeKey(keyName, ctxt);
+ if (key == null) {
+ if (!ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)) {
+ return (EnumMap<?,?>) ctxt.handleWeirdStringValue(_enumClass, keyName,
+ "value not one of declared Enum instance names for %s",
+ _containerType.getKeyType());
+ }
+ // 24-Mar-2012, tatu: Null won't work as a key anyway, so let's
+ // just skip the entry then. But we must skip the value as well, if so.
+ p.nextToken();
+ p.skipChildren();
+ continue;
+ }
+ Object value;
+
+ try {
+ if (t == JsonToken.VALUE_NULL) {
+ if (_skipNullValues) {
+ continue;
+ }
+ value = _nullProvider.getNullValue(ctxt);
+ } else if (_valueTypeDeserializer == null) {
+ value = _valueDeserializer.deserialize(p, ctxt);
+ } else {
+ value = _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
+ }
+ } catch (Exception e) {
+ wrapAndThrow(e, _containerType.getRawClass(), keyName);
+ return null;
+ }
+ buffer.bufferMapProperty(key, value);
+ }
+ // end of JSON object?
+ // if so, can just construct and leave...
+ try {
+ return (EnumMap<?,?>)creator.build(ctxt, buffer);
+ } catch (Exception e) {
+ wrapAndThrow(e, _containerType.getRawClass(), keyName);
+ return null;
+ }
}
}
-
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumSetDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumSetDeserializer.java
index 2136210..08ceee8 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumSetDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumSetDeserializer.java
@@ -63,7 +63,7 @@
@SuppressWarnings("unchecked" )
protected EnumSetDeserializer(EnumSetDeserializer base,
JsonDeserializer<?> deser, Boolean unwrapSingle) {
- super(EnumSet.class);
+ super(base);
_enumType = base._enumType;
_enumClass = base._enumClass;
_enumDeserializer = (JsonDeserializer<Enum<?>>) deser;
@@ -96,7 +96,12 @@
}
return true;
}
-
+
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return Boolean.TRUE;
+ }
+
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
@@ -118,15 +123,32 @@
/**********************************************************
*/
- @SuppressWarnings("unchecked")
@Override
public EnumSet<?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
+ EnumSet result = constructSet();
// Ok: must point to START_ARRAY (or equivalent)
if (!p.isExpectedStartArrayToken()) {
- return handleNonArray(p, ctxt);
+ return handleNonArray(p, ctxt, result);
}
- EnumSet result = constructSet();
+ return _deserialize(p, ctxt, result);
+ }
+
+ @Override
+ public EnumSet<?> deserialize(JsonParser p, DeserializationContext ctxt,
+ EnumSet<?> result) throws IOException
+ {
+ // Ok: must point to START_ARRAY (or equivalent)
+ if (!p.isExpectedStartArrayToken()) {
+ return handleNonArray(p, ctxt, result);
+ }
+ return _deserialize(p, ctxt, result);
+ }
+
+ @SuppressWarnings("unchecked")
+ protected final EnumSet<?> _deserialize(JsonParser p, DeserializationContext ctxt,
+ EnumSet result) throws IOException
+ {
JsonToken t;
try {
@@ -164,12 +186,12 @@
@SuppressWarnings("unchecked")
private EnumSet constructSet()
{
- // superbly ugly... but apparently necessary
return EnumSet.noneOf(_enumClass);
}
@SuppressWarnings("unchecked")
- protected EnumSet<?> handleNonArray(JsonParser p, DeserializationContext ctxt)
+ protected EnumSet<?> handleNonArray(JsonParser p, DeserializationContext ctxt,
+ EnumSet result)
throws IOException
{
boolean canWrap = (_unwrapSingle == Boolean.TRUE) ||
@@ -179,8 +201,6 @@
if (!canWrap) {
return (EnumSet<?>) ctxt.handleUnexpectedToken(EnumSet.class, p);
}
-
- EnumSet result = constructSet();
// First: since `null`s not allowed, slightly simpler...
if (p.hasToken(JsonToken.VALUE_NULL)) {
return (EnumSet<?>) ctxt.handleUnexpectedToken(_enumClass, p);
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/FactoryBasedEnumDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/FactoryBasedEnumDeserializer.java
index cf4096b..522855f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/FactoryBasedEnumDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/FactoryBasedEnumDeserializer.java
@@ -1,17 +1,12 @@
package com.fasterxml.jackson.databind.deser.std;
import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
-import com.fasterxml.jackson.databind.BeanProperty;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.JavaType;
-import com.fasterxml.jackson.databind.JsonDeserializer;
-import com.fasterxml.jackson.databind.JsonMappingException;
+
+import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
@@ -99,6 +94,11 @@
return this;
}
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return Boolean.FALSE;
+ }
+
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
@@ -113,7 +113,8 @@
value = p.getText();
} else if ((_creatorProps != null) && p.isExpectedStartObjectToken()) {
if (_propCreator == null) {
- _propCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator, _creatorProps);
+ _propCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator, _creatorProps,
+ ctxt.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
}
p.nextToken();
return deserializeEnumUsingPropertyBased(p, ctxt, _propCreator);
@@ -189,25 +190,18 @@
private Throwable throwOrReturnThrowable(Throwable t, DeserializationContext ctxt) throws IOException
{
- while (t instanceof InvocationTargetException && t.getCause() != null) {
- t = t.getCause();
- }
+ t = ClassUtil.getRootCause(t);
// Errors to be passed as is
- if (t instanceof Error) {
- throw (Error) t;
- }
+ ClassUtil.throwIfError(t);
boolean wrap = (ctxt == null) || ctxt.isEnabled(DeserializationFeature.WRAP_EXCEPTIONS);
- // Ditto for IOExceptions; except we may want to wrap JSON
- // exceptions
- if (t instanceof IOException) {
- if (!wrap || !(t instanceof JsonProcessingException)) {
- throw (IOException) t;
- }
- } else if (!wrap) {
- if (t instanceof RuntimeException) {
- throw (RuntimeException) t;
- }
- }
- return t;
+ // Ditto for IOExceptions; except we may want to wrap JSON exceptions
+ if (t instanceof IOException) {
+ if (!wrap || !(t instanceof JsonProcessingException)) {
+ throw (IOException) t;
+ }
+ } else if (!wrap) {
+ ClassUtil.throwIfRTE(t);
+ }
+ return t;
}
}
\ No newline at end of file
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/FromStringDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/FromStringDeserializer.java
index 0d01e78..7d71257 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/FromStringDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/FromStringDeserializer.java
@@ -14,15 +14,41 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.util.VersionUtil;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.JavaType;
-import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.util.ClassUtil;
/**
- * Base class for simple deserializers that only accept JSON String
- * values as the source.
+ * Base class for simple deserializers that serialize values from String
+ * representation: this includes JSON Strings and other Scalar values that
+ * can be coerced into text, like Numbers and Booleans).
+ * Simple JSON String values are trimmed using {@link java.lang.String#trim}.
+ * Partial deserializer implementation will try to first access current token as
+ * a String, calls {@link #_deserialize(String,DeserializationContext)} and
+ * returns return value.
+ * If this does not work (current token not a simple scalar type), attempts
+ * are made so that:
+ *<ul>
+ * <li>Embedded values ({@link JsonToken#VALUE_EMBEDDED_OBJECT}) are returned as-is
+ * if they are of compatible type
+ * </li>
+ * <li>Arrays may be "unwrapped" if (and only if) {@link DeserializationFeature#UNWRAP_SINGLE_VALUE_ARRAYS}
+ * is enabled, and array contains just a single scalar value that can be deserialized
+ * (for example, JSON Array with single JSON String element).
+ * </li>
+ * </ul>
+ *<p>
+ * Special handling includes:
+ * <ul>
+ * <li>Null values ({@link JsonToken#VALUE_NULL}) are handled by returning value
+ * returned by {@link JsonDeserializer#getNullValue(DeserializationContext)}: default
+ * implementation simply returns Java `null` but this may be overridden.
+ * </li>
+ * <li>Empty String (after trimming) will result in {@link #_deserializeFromEmptyString}
+ * getting called, and return value being returned as deserialization: default implementation
+ * simply returns `null`.
+ * </li>
+ * </ul>
*/
@SuppressWarnings("serial")
public abstract class FromStringDeserializer<T> extends StdScalarDeserializer<T>
@@ -99,16 +125,16 @@
/* Deserializer implementations
/**********************************************************
*/
-
+
@SuppressWarnings("unchecked")
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
- // 22-Sep-2012, tatu: For 2.1, use this new method, may force coercion:
+ // Let's get textual value, possibly via coercion from other scalar types
String text = p.getValueAsString();
if (text != null) { // has String representation
if (text.length() == 0 || (text = text.trim()).length() == 0) {
- // 04-Feb-2013, tatu: Usually should become null; but not always
+ // Usually should become null; but not always
return _deserializeFromEmptyString();
}
Exception cause = null;
@@ -117,10 +143,8 @@
// indicated error; but that seems wrong. Should be able to return
// `null` as value.
return _deserialize(text, ctxt);
- } catch (IllegalArgumentException iae) {
- cause = iae;
- } catch (MalformedURLException me) {
- cause = me;
+ } catch (IllegalArgumentException | MalformedURLException e) {
+ cause = e;
}
String msg = "not a valid textual representation";
if (cause != null) {
@@ -160,7 +184,8 @@
protected T _deserializeEmbedded(Object ob, DeserializationContext ctxt) throws IOException {
// default impl: error out
- ctxt.reportMappingException("Don't know how to convert embedded Object of type %s into %s",
+ ctxt.reportInputMismatch(this,
+ "Don't know how to convert embedded Object of type %s into %s",
ob.getClass().getName(), _valueClass.getName());
return null;
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/JsonNodeDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/JsonNodeDeserializer.java
index 05b0562..ca62033 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/JsonNodeDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/JsonNodeDeserializer.java
@@ -22,7 +22,12 @@
*/
private final static JsonNodeDeserializer instance = new JsonNodeDeserializer();
- protected JsonNodeDeserializer() { super(JsonNode.class); }
+ protected JsonNodeDeserializer() {
+ // `null` means that explicit "merge" is honored and may or may not work, but
+ // that per-type and global defaults do not enable merging. This because
+ // some node types (Object, Array) do support, others don't.
+ super(JsonNode.class, null);
+ }
/**
* Factory method for accessing deserializer for specific node type
@@ -50,12 +55,6 @@
return NullNode.getInstance();
}
- @Override
- @Deprecated // since 2.6, remove from 2.7
- public JsonNode getNullValue() {
- return NullNode.getInstance();
- }
-
/**
* Implementation that will produce types of any JSON nodes; not just one
* deserializer is registered to handle (in case of more specialized handler).
@@ -70,8 +69,8 @@
case JsonTokenId.ID_START_ARRAY:
return deserializeArray(p, ctxt, ctxt.getNodeFactory());
default:
- return deserializeAny(p, ctxt, ctxt.getNodeFactory());
}
+ return deserializeAny(p, ctxt, ctxt.getNodeFactory());
}
/*
@@ -87,16 +86,19 @@
protected final static ObjectDeserializer _instance = new ObjectDeserializer();
- protected ObjectDeserializer() { super(ObjectNode.class); }
+ protected ObjectDeserializer() { super(ObjectNode.class, true); }
public static ObjectDeserializer getInstance() { return _instance; }
-
+
@Override
public ObjectNode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
- if (p.isExpectedStartObjectToken() || p.hasToken(JsonToken.FIELD_NAME)) {
+ if (p.isExpectedStartObjectToken()) {
return deserializeObject(p, ctxt, ctxt.getNodeFactory());
}
+ if (p.hasToken(JsonToken.FIELD_NAME)) {
+ return deserializeObjectAtName(p, ctxt, ctxt.getNodeFactory());
+ }
// 23-Sep-2015, tatu: Ugh. We may also be given END_OBJECT (similar to FIELD_NAME),
// if caller has advanced to the first token of Object, but for empty Object
if (p.hasToken(JsonToken.END_OBJECT)) {
@@ -104,8 +106,23 @@
}
return (ObjectNode) ctxt.handleUnexpectedToken(ObjectNode.class, p);
}
+
+ /**
+ * Variant needed to support both root-level `updateValue()` and merging.
+ *
+ * @since 2.9
+ */
+ @Override
+ public ObjectNode deserialize(JsonParser p, DeserializationContext ctxt,
+ ObjectNode node) throws IOException
+ {
+ if (p.isExpectedStartObjectToken() || p.hasToken(JsonToken.FIELD_NAME)) {
+ return (ObjectNode) updateObject(p, ctxt, (ObjectNode) node);
+ }
+ return (ObjectNode) ctxt.handleUnexpectedToken(ObjectNode.class, p);
+ }
}
-
+
final static class ArrayDeserializer
extends BaseNodeDeserializer<ArrayNode>
{
@@ -113,10 +130,10 @@
protected final static ArrayDeserializer _instance = new ArrayDeserializer();
- protected ArrayDeserializer() { super(ArrayNode.class); }
+ protected ArrayDeserializer() { super(ArrayNode.class, true); }
public static ArrayDeserializer getInstance() { return _instance; }
-
+
@Override
public ArrayNode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
@@ -125,6 +142,21 @@
}
return (ArrayNode) ctxt.handleUnexpectedToken(ArrayNode.class, p);
}
+
+ /**
+ * Variant needed to support both root-level `updateValue()` and merging.
+ *
+ * @since 2.9
+ */
+ @Override
+ public ArrayNode deserialize(JsonParser p, DeserializationContext ctxt,
+ ArrayNode node) throws IOException
+ {
+ if (p.isExpectedStartArrayToken()) {
+ return (ArrayNode) updateArray(p, ctxt, (ArrayNode) node);
+ }
+ return (ArrayNode) ctxt.handleUnexpectedToken(ArrayNode.class, p);
+ }
}
}
@@ -136,8 +168,11 @@
abstract class BaseNodeDeserializer<T extends JsonNode>
extends StdDeserializer<T>
{
- public BaseNodeDeserializer(Class<T> vc) {
+ protected final Boolean _supportsUpdates;
+
+ public BaseNodeDeserializer(Class<T> vc, Boolean supportsUpdates) {
super(vc);
+ _supportsUpdates = supportsUpdates;
}
@Override
@@ -145,9 +180,7 @@
TypeDeserializer typeDeserializer)
throws IOException
{
- /* Output can be as JSON Object, Array or scalar: no way to know
- * a priori. So:
- */
+ // Output can be as JSON Object, Array or scalar: no way to know a priori:
return typeDeserializer.deserializeTypedFromAny(p, ctxt);
}
@@ -158,17 +191,17 @@
@Override
public boolean isCachable() { return true; }
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return _supportsUpdates;
+ }
+
/*
/**********************************************************
/* Overridable methods
/**********************************************************
*/
- @Deprecated // since 2.8
- protected void _reportProblem(JsonParser p, String msg) throws JsonMappingException {
- throw JsonMappingException.from(p, msg);
- }
-
/**
* Method called when there is a duplicate value for a field.
* By default we don't care, and the last value is used.
@@ -190,7 +223,8 @@
{
// [databind#237]: Report an error if asked to do so:
if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)) {
- ctxt.reportMappingException("Duplicate field '%s' for ObjectNode: not allowed when FAIL_ON_READING_DUP_TREE_KEY enabled",
+ ctxt.reportInputMismatch(JsonNode.class,
+ "Duplicate field '%s' for ObjectNode: not allowed when FAIL_ON_READING_DUP_TREE_KEY enabled",
fieldName);
}
}
@@ -201,28 +235,20 @@
/**********************************************************
*/
+ /**
+ * Method called to deserialize Object node instance when there is no existing
+ * node to modify.
+ */
protected final ObjectNode deserializeObject(JsonParser p, DeserializationContext ctxt,
final JsonNodeFactory nodeFactory) throws IOException
{
- ObjectNode node = nodeFactory.objectNode();
- String key;
- if (p.isExpectedStartObjectToken()) {
- key = p.nextFieldName();
- } else {
- JsonToken t = p.getCurrentToken();
- if (t == JsonToken.END_OBJECT) {
- return node;
- }
- if (t != JsonToken.FIELD_NAME) {
- return (ObjectNode) ctxt.handleUnexpectedToken(handledType(), p);
- }
- key = p.getCurrentName();
- }
+ final ObjectNode node = nodeFactory.objectNode();
+ String key = p.nextFieldName();
for (; key != null; key = p.nextFieldName()) {
JsonNode value;
JsonToken t = p.nextToken();
- if (t == null) {
- throw ctxt.mappingException("Unexpected end-of-input when binding data into ObjectNode");
+ if (t == null) { // can this ever occur?
+ t = JsonToken.NOT_AVAILABLE; // can this ever occur?
}
switch (t.id()) {
case JsonTokenId.ID_START_OBJECT:
@@ -261,6 +287,142 @@
return node;
}
+ /**
+ * Alternate deserialization method used when parser already points to first
+ * FIELD_NAME and not START_OBJECT.
+ *
+ * @since 2.9
+ */
+ protected final ObjectNode deserializeObjectAtName(JsonParser p, DeserializationContext ctxt,
+ final JsonNodeFactory nodeFactory) throws IOException
+ {
+ final ObjectNode node = nodeFactory.objectNode();
+ String key = p.getCurrentName();
+ for (; key != null; key = p.nextFieldName()) {
+ JsonNode value;
+ JsonToken t = p.nextToken();
+ if (t == null) { // can this ever occur?
+ t = JsonToken.NOT_AVAILABLE; // can this ever occur?
+ }
+ switch (t.id()) {
+ case JsonTokenId.ID_START_OBJECT:
+ value = deserializeObject(p, ctxt, nodeFactory);
+ break;
+ case JsonTokenId.ID_START_ARRAY:
+ value = deserializeArray(p, ctxt, nodeFactory);
+ break;
+ case JsonTokenId.ID_EMBEDDED_OBJECT:
+ value = _fromEmbedded(p, ctxt, nodeFactory);
+ break;
+ case JsonTokenId.ID_STRING:
+ value = nodeFactory.textNode(p.getText());
+ break;
+ case JsonTokenId.ID_NUMBER_INT:
+ value = _fromInt(p, ctxt, nodeFactory);
+ break;
+ case JsonTokenId.ID_TRUE:
+ value = nodeFactory.booleanNode(true);
+ break;
+ case JsonTokenId.ID_FALSE:
+ value = nodeFactory.booleanNode(false);
+ break;
+ case JsonTokenId.ID_NULL:
+ value = nodeFactory.nullNode();
+ break;
+ default:
+ value = deserializeAny(p, ctxt, nodeFactory);
+ }
+ JsonNode old = node.replace(key, value);
+ if (old != null) {
+ _handleDuplicateField(p, ctxt, nodeFactory,
+ key, node, old, value);
+ }
+ }
+ return node;
+ }
+
+ /**
+ * Alternate deserialization method that is to update existing {@link ObjectNode}
+ * if possible.
+ *
+ * @since 2.9
+ */
+ protected final JsonNode updateObject(JsonParser p, DeserializationContext ctxt,
+ final ObjectNode node) throws IOException
+ {
+ String key;
+ if (p.isExpectedStartObjectToken()) {
+ key = p.nextFieldName();
+ } else {
+ if (!p.hasToken(JsonToken.FIELD_NAME)) {
+ return deserialize(p, ctxt);
+ }
+ key = p.getCurrentName();
+ }
+ for (; key != null; key = p.nextFieldName()) {
+ // If not, fall through to regular handling
+ JsonToken t = p.nextToken();
+
+ // First: see if we can merge things:
+ JsonNode old = node.get(key);
+ if (old != null) {
+ if (old instanceof ObjectNode) {
+ JsonNode newValue = updateObject(p, ctxt, (ObjectNode) old);
+ if (newValue != old) {
+ node.set(key, newValue);
+ }
+ continue;
+ }
+ if (old instanceof ArrayNode) {
+ JsonNode newValue = updateArray(p, ctxt, (ArrayNode) old);
+ if (newValue != old) {
+ node.set(key, newValue);
+ }
+ continue;
+ }
+ }
+ if (t == null) { // can this ever occur?
+ t = JsonToken.NOT_AVAILABLE;
+ }
+ JsonNode value;
+ JsonNodeFactory nodeFactory = ctxt.getNodeFactory();
+ switch (t.id()) {
+ case JsonTokenId.ID_START_OBJECT:
+ value = deserializeObject(p, ctxt, nodeFactory);
+ break;
+ case JsonTokenId.ID_START_ARRAY:
+ value = deserializeArray(p, ctxt, nodeFactory);
+ break;
+ case JsonTokenId.ID_EMBEDDED_OBJECT:
+ value = _fromEmbedded(p, ctxt, nodeFactory);
+ break;
+ case JsonTokenId.ID_STRING:
+ value = nodeFactory.textNode(p.getText());
+ break;
+ case JsonTokenId.ID_NUMBER_INT:
+ value = _fromInt(p, ctxt, nodeFactory);
+ break;
+ case JsonTokenId.ID_TRUE:
+ value = nodeFactory.booleanNode(true);
+ break;
+ case JsonTokenId.ID_FALSE:
+ value = nodeFactory.booleanNode(false);
+ break;
+ case JsonTokenId.ID_NULL:
+ value = nodeFactory.nullNode();
+ break;
+ default:
+ value = deserializeAny(p, ctxt, nodeFactory);
+ }
+ if (old != null) {
+ _handleDuplicateField(p, ctxt, nodeFactory,
+ key, node, old, value);
+ }
+ node.set(key, value);
+ }
+ return node;
+ }
+
protected final ArrayNode deserializeArray(JsonParser p, DeserializationContext ctxt,
final JsonNodeFactory nodeFactory) throws IOException
{
@@ -301,16 +463,60 @@
}
}
+ /**
+ * Alternate deserialization method that is to update existing {@link ObjectNode}
+ * if possible.
+ *
+ * @since 2.9
+ */
+ protected final JsonNode updateArray(JsonParser p, DeserializationContext ctxt,
+ final ArrayNode node) throws IOException
+ {
+ final JsonNodeFactory nodeFactory = ctxt.getNodeFactory();
+ while (true) {
+ JsonToken t = p.nextToken();
+ switch (t.id()) {
+ case JsonTokenId.ID_START_OBJECT:
+ node.add(deserializeObject(p, ctxt, nodeFactory));
+ break;
+ case JsonTokenId.ID_START_ARRAY:
+ node.add(deserializeArray(p, ctxt, nodeFactory));
+ break;
+ case JsonTokenId.ID_END_ARRAY:
+ return node;
+ case JsonTokenId.ID_EMBEDDED_OBJECT:
+ node.add(_fromEmbedded(p, ctxt, nodeFactory));
+ break;
+ case JsonTokenId.ID_STRING:
+ node.add(nodeFactory.textNode(p.getText()));
+ break;
+ case JsonTokenId.ID_NUMBER_INT:
+ node.add(_fromInt(p, ctxt, nodeFactory));
+ break;
+ case JsonTokenId.ID_TRUE:
+ node.add(nodeFactory.booleanNode(true));
+ break;
+ case JsonTokenId.ID_FALSE:
+ node.add(nodeFactory.booleanNode(false));
+ break;
+ case JsonTokenId.ID_NULL:
+ node.add(nodeFactory.nullNode());
+ break;
+ default:
+ node.add(deserializeAny(p, ctxt, nodeFactory));
+ break;
+ }
+ }
+ }
+
protected final JsonNode deserializeAny(JsonParser p, DeserializationContext ctxt,
final JsonNodeFactory nodeFactory) throws IOException
{
switch (p.getCurrentTokenId()) {
- case JsonTokenId.ID_START_OBJECT:
- case JsonTokenId.ID_END_OBJECT: // for empty JSON Objects we may point to this
+ case JsonTokenId.ID_END_OBJECT: // for empty JSON Objects we may point to this?
+ return nodeFactory.objectNode();
case JsonTokenId.ID_FIELD_NAME:
- return deserializeObject(p, ctxt, nodeFactory);
- case JsonTokenId.ID_START_ARRAY:
- return deserializeArray(p, ctxt, nodeFactory);
+ return deserializeObjectAtName(p, ctxt, nodeFactory);
case JsonTokenId.ID_EMBEDDED_OBJECT:
return _fromEmbedded(p, ctxt, nodeFactory);
case JsonTokenId.ID_STRING:
@@ -325,6 +531,14 @@
return nodeFactory.booleanNode(false);
case JsonTokenId.ID_NULL:
return nodeFactory.nullNode();
+
+ /* Caller checks for these, should not get here ever
+ case JsonTokenId.ID_START_OBJECT:
+ return deserializeObject(p, ctxt, nodeFactory);
+ case JsonTokenId.ID_START_ARRAY:
+ return deserializeArray(p, ctxt, nodeFactory);
+ */
+
// These states can not be mapped; input stream is
// off by an event or two
@@ -371,12 +585,8 @@
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
// 20-May-2016, tatu: As per [databind#1028], need to be careful
// (note: JDK 1.8 would have `Double.isFinite()`)
- // 21-Aug-2016, tatu: Not optimal, really, because this may result in
- // value getting parsed twice. But has to do for now, to resolve
- // [databind#1315]
- double d = p.getDoubleValue();
- if (Double.isInfinite(d) || Double.isNaN(d)) {
- return nodeFactory.numberNode(d);
+ if (p.isNaN()) {
+ return nodeFactory.numberNode(p.getDoubleValue());
}
return nodeFactory.numberNode(p.getDecimalValue());
}
@@ -402,7 +612,7 @@
return nodeFactory.rawValueNode((RawValue) ob);
}
if (ob instanceof JsonNode) {
- // [Issue#433]: but could also be a JsonNode hiding in there!
+ // [databind#433]: but could also be a JsonNode hiding in there!
return (JsonNode) ob;
}
// any other special handling needed?
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/MapDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/MapDeserializer.java
index c1d8719..7acc124 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/MapDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/MapDeserializer.java
@@ -4,7 +4,9 @@
import java.util.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+
import com.fasterxml.jackson.core.*;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.deser.*;
@@ -33,8 +35,6 @@
// // Configuration: typing, deserializers
- protected final JavaType _mapType;
-
/**
* Key deserializer to use; either passed via constructor
* (when indicated by annotations), or resolved when
@@ -61,13 +61,11 @@
* is the type deserializer that can handle it
*/
protected final TypeDeserializer _valueTypeDeserializer;
-
+
// // Instance construction settings:
protected final ValueInstantiator _valueInstantiator;
- protected final boolean _hasDefaultCreator;
-
/**
* Deserializer that is used iff delegate-based creator is
* to be used for deserializing from JSON Object.
@@ -82,8 +80,10 @@
*/
protected PropertyBasedCreator _propertyBasedCreator;
+ protected final boolean _hasDefaultCreator;
+
// // Any properties to ignore if seen?
-
+
protected Set<String> _ignorableProperties;
/*
@@ -96,8 +96,7 @@
KeyDeserializer keyDeser, JsonDeserializer<Object> valueDeser,
TypeDeserializer valueTypeDeser)
{
- super(mapType);
- _mapType = mapType;
+ super(mapType, null, null);
_keyDeserializer = keyDeser;
_valueDeserializer = valueDeser;
_valueTypeDeserializer = valueTypeDeser;
@@ -114,8 +113,7 @@
*/
protected MapDeserializer(MapDeserializer src)
{
- super(src._mapType);
- _mapType = src._mapType;
+ super(src);
_keyDeserializer = src._keyDeserializer;
_valueDeserializer = src._valueDeserializer;
_valueTypeDeserializer = src._valueTypeDeserializer;
@@ -132,10 +130,10 @@
protected MapDeserializer(MapDeserializer src,
KeyDeserializer keyDeser, JsonDeserializer<Object> valueDeser,
TypeDeserializer valueTypeDeser,
+ NullValueProvider nuller,
Set<String> ignorable)
{
- super(src._mapType);
- _mapType = src._mapType;
+ super(src, nuller, src._unwrapSingle);
_keyDeserializer = keyDeser;
_valueDeserializer = valueDeser;
_valueTypeDeserializer = valueTypeDeser;
@@ -145,7 +143,7 @@
_hasDefaultCreator = src._hasDefaultCreator;
_ignorableProperties = ignorable;
- _standardStringKey = _isStdKeyDeser(_mapType, keyDeser);
+ _standardStringKey = _isStdKeyDeser(_containerType, keyDeser);
}
/**
@@ -155,15 +153,18 @@
@SuppressWarnings("unchecked")
protected MapDeserializer withResolved(KeyDeserializer keyDeser,
TypeDeserializer valueTypeDeser, JsonDeserializer<?> valueDeser,
+ NullValueProvider nuller,
Set<String> ignorable)
{
if ((_keyDeserializer == keyDeser) && (_valueDeserializer == valueDeser)
- && (_valueTypeDeserializer == valueTypeDeser) && (_ignorableProperties == ignorable)) {
+ && (_valueTypeDeserializer == valueTypeDeser) && (_nullProvider == nuller)
+ && (_ignorableProperties == ignorable)) {
return this;
}
return new MapDeserializer(this,
- keyDeser, (JsonDeserializer<Object>) valueDeser, valueTypeDeser, ignorable);
+ keyDeser, (JsonDeserializer<Object>) valueDeser, valueTypeDeser,
+ nuller, ignorable);
}
/**
@@ -204,34 +205,34 @@
public void resolve(DeserializationContext ctxt) throws JsonMappingException
{
// May need to resolve types for delegate- and/or property-based creators:
- if (_valueInstantiator != null) {
- if (_valueInstantiator.canCreateUsingDelegate()) {
- JavaType delegateType = _valueInstantiator.getDelegateType(ctxt.getConfig());
- if (delegateType == null) {
- throw new IllegalArgumentException("Invalid delegate-creator definition for "+_mapType
- +": value instantiator ("+_valueInstantiator.getClass().getName()
- +") returned true for 'canCreateUsingDelegate()', but null for 'getDelegateType()'");
- }
- /* Theoretically should be able to get CreatorProperty for delegate
- * parameter to pass; but things get tricky because DelegateCreator
- * may contain injectable values. So, for now, let's pass nothing.
- */
- _delegateDeserializer = findDeserializer(ctxt, delegateType, null);
- } else if (_valueInstantiator.canCreateUsingArrayDelegate()) {
- JavaType delegateType = _valueInstantiator.getArrayDelegateType(ctxt.getConfig());
- if (delegateType == null) {
- throw new IllegalArgumentException("Invalid delegate-creator definition for "+_mapType
- +": value instantiator ("+_valueInstantiator.getClass().getName()
- +") returned true for 'canCreateUsingDelegate()', but null for 'getArrayDelegateType()'");
- }
- _delegateDeserializer = findDeserializer(ctxt, delegateType, null);
+ if (_valueInstantiator.canCreateUsingDelegate()) {
+ JavaType delegateType = _valueInstantiator.getDelegateType(ctxt.getConfig());
+ if (delegateType == null) {
+ ctxt.reportBadDefinition(_containerType, String.format(
+"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingDelegate()', but null for 'getDelegateType()'",
+ _containerType,
+ _valueInstantiator.getClass().getName()));
}
+ // Theoretically should be able to get CreatorProperty for delegate
+ // parameter to pass; but things get tricky because DelegateCreator
+ // may contain injectable values. So, for now, let's pass nothing.
+ _delegateDeserializer = findDeserializer(ctxt, delegateType, null);
+ } else if (_valueInstantiator.canCreateUsingArrayDelegate()) {
+ JavaType delegateType = _valueInstantiator.getArrayDelegateType(ctxt.getConfig());
+ if (delegateType == null) {
+ ctxt.reportBadDefinition(_containerType, String.format(
+"Invalid delegate-creator definition for %s: value instantiator (%s) returned true for 'canCreateUsingArrayDelegate()', but null for 'getArrayDelegateType()'",
+ _containerType,
+ _valueInstantiator.getClass().getName()));
+ }
+ _delegateDeserializer = findDeserializer(ctxt, delegateType, null);
}
if (_valueInstantiator.canCreateFromObjectWith()) {
SettableBeanProperty[] creatorProps = _valueInstantiator.getFromObjectArguments(ctxt.getConfig());
- _propertyBasedCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator, creatorProps);
+ _propertyBasedCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator, creatorProps,
+ ctxt.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
}
- _standardStringKey = _isStdKeyDeser(_mapType, _keyDeserializer);
+ _standardStringKey = _isStdKeyDeser(_containerType, _keyDeserializer);
}
/**
@@ -242,25 +243,25 @@
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
- KeyDeserializer kd = _keyDeserializer;
- if (kd == null) {
- kd = ctxt.findKeyDeserializer(_mapType.getKeyType(), property);
+ KeyDeserializer keyDeser = _keyDeserializer;
+ if (keyDeser == null) {
+ keyDeser = ctxt.findKeyDeserializer(_containerType.getKeyType(), property);
} else {
- if (kd instanceof ContextualKeyDeserializer) {
- kd = ((ContextualKeyDeserializer) kd).createContextual(ctxt, property);
+ if (keyDeser instanceof ContextualKeyDeserializer) {
+ keyDeser = ((ContextualKeyDeserializer) keyDeser).createContextual(ctxt, property);
}
}
- JsonDeserializer<?> vd = _valueDeserializer;
+ JsonDeserializer<?> valueDeser = _valueDeserializer;
// [databind#125]: May have a content converter
if (property != null) {
- vd = findConvertingContentDeserializer(ctxt, property, vd);
+ valueDeser = findConvertingContentDeserializer(ctxt, property, valueDeser);
}
- final JavaType vt = _mapType.getContentType();
- if (vd == null) {
- vd = ctxt.findContextualValueDeserializer(vt, property);
+ final JavaType vt = _containerType.getContentType();
+ if (valueDeser == null) {
+ valueDeser = ctxt.findContextualValueDeserializer(vt, property);
} else { // if directly assigned, probably not yet contextual, so:
- vd = ctxt.handleSecondaryContextualization(vd, property, vt);
+ valueDeser = ctxt.handleSecondaryContextualization(valueDeser, property, vt);
}
TypeDeserializer vtd = _valueTypeDeserializer;
if (vtd != null) {
@@ -268,7 +269,7 @@
}
Set<String> ignored = _ignorableProperties;
AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
- if (intr != null && property != null) {
+ if (_neitherNull(intr, property)) {
AnnotatedMember member = property.getMember();
if (member != null) {
JsonIgnoreProperties.Value ignorals = intr.findPropertyIgnorals(member);
@@ -283,7 +284,8 @@
}
}
}
- return withResolved(kd, vtd, vd, ignored);
+ return withResolved(keyDeser, vtd, valueDeser,
+ findContentNullProvider(ctxt, property, valueDeser), ignored);
}
/*
@@ -293,15 +295,15 @@
*/
@Override
- public JavaType getContentType() {
- return _mapType.getContentType();
- }
-
- @Override
public JsonDeserializer<Object> getContentDeserializer() {
return _valueDeserializer;
}
-
+
+ @Override
+ public ValueInstantiator getValueInstantiator() {
+ return _valueInstantiator;
+ }
+
/*
/**********************************************************
/* JsonDeserializer API
@@ -323,9 +325,8 @@
*/
@Override
public boolean isCachable() {
- /* As per [databind#735], existence of value or key deserializer (only passed
- * if annotated to use non-standard one) should also prevent caching.
- */
+ // As per [databind#735], existence of value or key deserializer (only passed
+ // if annotated to use non-standard one) should also prevent caching.
return (_valueDeserializer == null)
&& (_keyDeserializer == null)
&& (_valueTypeDeserializer == null)
@@ -344,17 +345,18 @@
_delegateDeserializer.deserialize(p, ctxt));
}
if (!_hasDefaultCreator) {
- return (Map<Object,Object> ) ctxt.handleMissingInstantiator(getMapClass(), p,
+ return (Map<Object,Object> ) ctxt.handleMissingInstantiator(getMapClass(),
+ getValueInstantiator(), p,
"no default constructor found");
}
// Ok: must point to START_OBJECT, FIELD_NAME or END_OBJECT
JsonToken t = p.getCurrentToken();
if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) {
- // (empty) String may be ok however:
+ // (empty) String may be ok however; or single-String-arg ctor
if (t == JsonToken.VALUE_STRING) {
return (Map<Object,Object>) _valueInstantiator.createFromString(ctxt, p.getText());
}
- // slightly redundant (since String was passed above), but
+ // slightly redundant (since String was passed above), but also handles empty array case:
return _deserializeFromEmpty(p, ctxt);
}
final Map<Object,Object> result = (Map<Object,Object>) _valueInstantiator.createUsingDefault(ctxt);
@@ -372,7 +374,7 @@
Map<Object,Object> result)
throws IOException
{
- // [databind#631]: Assign current value, to be accessible by custom serializers
+ // [databind#631]: Assign current value, to be accessible by custom deserializers
p.setCurrentValue(result);
// Ok: must point to START_OBJECT or FIELD_NAME
@@ -380,23 +382,24 @@
if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME) {
return (Map<Object,Object>) ctxt.handleUnexpectedToken(getMapClass(), p);
}
+ // 21-Apr-2017, tatu: Need separate methods to do proper merging
if (_standardStringKey) {
- _readAndBindStringKeyMap(p, ctxt, result);
+ _readAndUpdateStringKeyMap(p, ctxt, result);
return result;
}
- _readAndBind(p, ctxt, result);
+ _readAndUpdate(p, ctxt, result);
return result;
}
@Override
- public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt,
+ public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer)
- throws IOException, JsonProcessingException
+ throws IOException
{
// In future could check current token... for now this should be enough:
- return typeDeserializer.deserializeTypedFromObject(jp, ctxt);
+ return typeDeserializer.deserializeTypedFromObject(p, ctxt);
}
-
+
/*
/**********************************************************
/* Other public accessors
@@ -404,13 +407,13 @@
*/
@SuppressWarnings("unchecked")
- public final Class<?> getMapClass() { return (Class<Map<Object,Object>>) _mapType.getRawClass(); }
+ public final Class<?> getMapClass() { return (Class<Map<Object,Object>>) _containerType.getRawClass(); }
- @Override public JavaType getValueType() { return _mapType; }
+ @Override public JavaType getValueType() { return _containerType; }
/*
/**********************************************************
- /* Internal methods
+ /* Internal methods, non-merging deserialization
/**********************************************************
*/
@@ -424,7 +427,8 @@
MapReferringAccumulator referringAccumulator = null;
boolean useObjectId = valueDes.getObjectIdReader() != null;
if (useObjectId) {
- referringAccumulator = new MapReferringAccumulator(_mapType.getContentType().getRawClass(), result);
+ referringAccumulator = new MapReferringAccumulator(_containerType.getContentType().getRawClass(),
+ result);
}
String keyStr;
@@ -436,7 +440,7 @@
return;
}
if (t != JsonToken.FIELD_NAME) {
- ctxt.reportWrongTokenException(p, JsonToken.FIELD_NAME, null);
+ ctxt.reportWrongTokenException(this, JsonToken.FIELD_NAME, null);
}
keyStr = p.getCurrentName();
}
@@ -453,7 +457,10 @@
// Note: must handle null explicitly here; value deserializers won't
Object value;
if (t == JsonToken.VALUE_NULL) {
- value = valueDes.getNullValue(ctxt);
+ if (_skipNullValues) {
+ continue;
+ }
+ value = _nullProvider.getNullValue(ctxt);
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
@@ -465,7 +472,7 @@
result.put(key, value);
}
} catch (UnresolvedForwardReference reference) {
- handleUnresolvedReference(p, referringAccumulator, key, reference);
+ handleUnresolvedReference(ctxt, referringAccumulator, key, reference);
} catch (Exception e) {
wrapAndThrow(e, result, keyStr);
}
@@ -485,7 +492,7 @@
MapReferringAccumulator referringAccumulator = null;
boolean useObjectId = (valueDes.getObjectIdReader() != null);
if (useObjectId) {
- referringAccumulator = new MapReferringAccumulator(_mapType.getContentType().getRawClass(), result);
+ referringAccumulator = new MapReferringAccumulator(_containerType.getContentType().getRawClass(), result);
}
String key;
@@ -497,7 +504,7 @@
return;
}
if (t != JsonToken.FIELD_NAME) {
- ctxt.reportWrongTokenException(p, JsonToken.FIELD_NAME, null);
+ ctxt.reportWrongTokenException(this, JsonToken.FIELD_NAME, null);
}
key = p.getCurrentName();
}
@@ -512,7 +519,10 @@
// Note: must handle null explicitly here; value deserializers won't
Object value;
if (t == JsonToken.VALUE_NULL) {
- value = valueDes.getNullValue(ctxt);
+ if (_skipNullValues) {
+ continue;
+ }
+ value = _nullProvider.getNullValue(ctxt);
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
@@ -524,14 +534,14 @@
result.put(key, value);
}
} catch (UnresolvedForwardReference reference) {
- handleUnresolvedReference(p, referringAccumulator, key, reference);
+ handleUnresolvedReference(ctxt, referringAccumulator, key, reference);
} catch (Exception e) {
wrapAndThrow(e, result, key);
}
}
// 23-Mar-2015, tatu: TODO: verify we got END_OBJECT?
}
-
+
@SuppressWarnings("unchecked")
public Map<Object,Object> _deserializeUsingCreator(JsonParser p, DeserializationContext ctxt) throws IOException
{
@@ -567,8 +577,7 @@
try {
result = (Map<Object,Object>)creator.build(ctxt, buffer);
} catch (Exception e) {
- wrapAndThrow(e, _mapType.getRawClass(), key);
- return null;
+ return wrapAndThrow(e, _containerType.getRawClass(), key);
}
_readAndBind(p, ctxt, result);
return result;
@@ -581,14 +590,17 @@
try {
if (t == JsonToken.VALUE_NULL) {
- value = valueDes.getNullValue(ctxt);
+ if (_skipNullValues) {
+ continue;
+ }
+ value = _nullProvider.getNullValue(ctxt);
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
value = valueDes.deserializeWithType(p, ctxt, typeDeser);
}
} catch (Exception e) {
- wrapAndThrow(e, _mapType.getRawClass(), key);
+ wrapAndThrow(e, _containerType.getRawClass(), key);
return null;
}
buffer.bufferMapProperty(actualKey, value);
@@ -598,22 +610,155 @@
try {
return (Map<Object,Object>)creator.build(ctxt, buffer);
} catch (Exception e) {
- wrapAndThrow(e, _mapType.getRawClass(), key);
+ wrapAndThrow(e, _containerType.getRawClass(), key);
return null;
}
}
- @Deprecated // since 2.5
- protected void wrapAndThrow(Throwable t, Object ref) throws IOException {
- wrapAndThrow(t, ref, null);
+ /*
+ /**********************************************************
+ /* Internal methods, non-merging deserialization
+ /**********************************************************
+ */
+
+ /**
+ * @since 2.9
+ */
+ protected final void _readAndUpdate(JsonParser p, DeserializationContext ctxt,
+ Map<Object,Object> result) throws IOException
+ {
+ final KeyDeserializer keyDes = _keyDeserializer;
+ final JsonDeserializer<Object> valueDes = _valueDeserializer;
+ final TypeDeserializer typeDeser = _valueTypeDeserializer;
+
+ // Note: assumption is that Object Id handling can't really work with merging
+ // and thereby we can (and should) just drop that part
+
+ String keyStr;
+ if (p.isExpectedStartObjectToken()) {
+ keyStr = p.nextFieldName();
+ } else {
+ JsonToken t = p.getCurrentToken();
+ if (t == JsonToken.END_OBJECT) {
+ return;
+ }
+ if (t != JsonToken.FIELD_NAME) {
+ ctxt.reportWrongTokenException(this, JsonToken.FIELD_NAME, null);
+ }
+ keyStr = p.getCurrentName();
+ }
+
+ for (; keyStr != null; keyStr = p.nextFieldName()) {
+ Object key = keyDes.deserializeKey(keyStr, ctxt);
+ // And then the value...
+ JsonToken t = p.nextToken();
+ if (_ignorableProperties != null && _ignorableProperties.contains(keyStr)) {
+ p.skipChildren();
+ continue;
+ }
+ try {
+ // Note: must handle null explicitly here, can't merge etc
+ if (t == JsonToken.VALUE_NULL) {
+ if (_skipNullValues) {
+ continue;
+ }
+ result.put(key, _nullProvider.getNullValue(ctxt));
+ continue;
+ }
+ Object value = result.get(key);
+ if (value != null) {
+ valueDes.deserialize(p, ctxt, value);
+ continue;
+ }
+ if (typeDeser == null) {
+ value = valueDes.deserialize(p, ctxt);
+ } else {
+ value = valueDes.deserializeWithType(p, ctxt, typeDeser);
+ }
+ result.put(key, value);
+ } catch (Exception e) {
+ wrapAndThrow(e, result, keyStr);
+ }
+ }
}
- private void handleUnresolvedReference(JsonParser jp, MapReferringAccumulator accumulator,
+ /**
+ * Optimized method used when keys can be deserialized as plain old
+ * {@link java.lang.String}s, and there is no custom deserialized
+ * specified.
+ *
+ * @since 2.9
+ */
+ protected final void _readAndUpdateStringKeyMap(JsonParser p, DeserializationContext ctxt,
+ Map<Object,Object> result) throws IOException
+ {
+ final JsonDeserializer<Object> valueDes = _valueDeserializer;
+ final TypeDeserializer typeDeser = _valueTypeDeserializer;
+
+ // Note: assumption is that Object Id handling can't really work with merging
+ // and thereby we can (and should) just drop that part
+
+ String key;
+ if (p.isExpectedStartObjectToken()) {
+ key = p.nextFieldName();
+ } else {
+ JsonToken t = p.getCurrentToken();
+ if (t == JsonToken.END_OBJECT) {
+ return;
+ }
+ if (t != JsonToken.FIELD_NAME) {
+ ctxt.reportWrongTokenException(this, JsonToken.FIELD_NAME, null);
+ }
+ key = p.getCurrentName();
+ }
+
+ for (; key != null; key = p.nextFieldName()) {
+ JsonToken t = p.nextToken();
+ if (_ignorableProperties != null && _ignorableProperties.contains(key)) {
+ p.skipChildren();
+ continue;
+ }
+ try {
+ // Note: must handle null explicitly here, can't merge etc
+ if (t == JsonToken.VALUE_NULL) {
+ if (_skipNullValues) {
+ continue;
+ }
+ result.put(key, _nullProvider.getNullValue(ctxt));
+ continue;
+ }
+ Object old = result.get(key);
+ Object value;
+ if (old != null) {
+ value = valueDes.deserialize(p, ctxt, old);
+ } else if (typeDeser == null) {
+ value = valueDes.deserialize(p, ctxt);
+ } else {
+ value = valueDes.deserializeWithType(p, ctxt, typeDeser);
+ }
+ if (value != old) {
+ result.put(key, value);
+ }
+ } catch (Exception e) {
+ wrapAndThrow(e, result, key);
+ }
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Internal methods, other
+ /**********************************************************
+ */
+
+ private void handleUnresolvedReference(DeserializationContext ctxt,
+ MapReferringAccumulator accumulator,
Object key, UnresolvedForwardReference reference)
throws JsonMappingException
{
if (accumulator == null) {
- throw JsonMappingException.from(jp, "Unresolved forward reference but no identity info.", reference);
+ ctxt.reportInputMismatch(this,
+ "Unresolved forward reference but no identity info: "+reference);
}
Referring referring = accumulator.handleUnresolvedReference(reference, key);
reference.getRoid().appendReferring(referring);
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/MapEntryDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/MapEntryDeserializer.java
index 7899712..341ac51 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/MapEntryDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/MapEntryDeserializer.java
@@ -27,8 +27,6 @@
// // Configuration: typing, deserializers
- protected final JavaType _type;
-
/**
* Key deserializer to use; either passed via constructor
* (when indicated by annotations), or resolved when
@@ -61,7 +59,6 @@
if (type.containedTypeCount() != 2) { // sanity check
throw new IllegalArgumentException("Missing generic type information for "+type);
}
- _type = type;
_keyDeserializer = keyDeser;
_valueDeserializer = valueDeser;
_valueTypeDeserializer = valueTypeDeser;
@@ -73,8 +70,7 @@
*/
protected MapEntryDeserializer(MapEntryDeserializer src)
{
- super(src._type);
- _type = src._type;
+ super(src);
_keyDeserializer = src._keyDeserializer;
_valueDeserializer = src._valueDeserializer;
_valueTypeDeserializer = src._valueTypeDeserializer;
@@ -84,8 +80,7 @@
KeyDeserializer keyDeser, JsonDeserializer<Object> valueDeser,
TypeDeserializer valueTypeDeser)
{
- super(src._type);
- _type = src._type;
+ super(src);
_keyDeserializer = keyDeser;
_valueDeserializer = valueDeser;
_valueTypeDeserializer = valueTypeDeser;
@@ -124,7 +119,7 @@
{
KeyDeserializer kd = _keyDeserializer;
if (kd == null) {
- kd = ctxt.findKeyDeserializer(_type.containedType(0), property);
+ kd = ctxt.findKeyDeserializer(_containerType.containedType(0), property);
} else {
if (kd instanceof ContextualKeyDeserializer) {
kd = ((ContextualKeyDeserializer) kd).createContextual(ctxt, property);
@@ -132,7 +127,7 @@
}
JsonDeserializer<?> vd = _valueDeserializer;
vd = findConvertingContentDeserializer(ctxt, property, vd);
- JavaType contentType = _type.containedType(1);
+ JavaType contentType = _containerType.containedType(1);
if (vd == null) {
vd = ctxt.findContextualValueDeserializer(contentType, property);
} else { // if directly assigned, probably not yet contextual, so:
@@ -153,7 +148,7 @@
@Override
public JavaType getContentType() {
- return _type.containedType(1);
+ return _containerType.containedType(1);
}
@Override
@@ -183,8 +178,8 @@
}
if (t != JsonToken.FIELD_NAME) {
if (t == JsonToken.END_OBJECT) {
- ctxt.reportMappingException("Can not deserialize a Map.Entry out of empty JSON Object");
- return null;
+ return ctxt.reportInputMismatch(this,
+ "Can not deserialize a Map.Entry out of empty JSON Object");
}
return (Map.Entry<Object,Object>) ctxt.handleUnexpectedToken(handledType(), p);
}
@@ -215,10 +210,13 @@
t = p.nextToken();
if (t != JsonToken.END_OBJECT) {
if (t == JsonToken.FIELD_NAME) { // most likely
- ctxt.reportMappingException("Problem binding JSON into Map.Entry: more than one entry in JSON (second field: '"+p.getCurrentName()+"')");
+ ctxt.reportInputMismatch(this,
+ "Problem binding JSON into Map.Entry: more than one entry in JSON (second field: '%s')",
+ p.getCurrentName());
} else {
// how would this occur?
- ctxt.reportMappingException("Problem binding JSON into Map.Entry: unexpected content after JSON Object entry: "+t);
+ ctxt.reportInputMismatch(this,
+ "Problem binding JSON into Map.Entry: unexpected content after JSON Object entry: "+t);
}
return null;
}
@@ -235,17 +233,9 @@
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer)
- throws IOException, JsonProcessingException
+ throws IOException
{
// In future could check current token... for now this should be enough:
return typeDeserializer.deserializeTypedFromObject(p, ctxt);
}
-
- /*
- /**********************************************************
- /* Other public accessors
- /**********************************************************
- */
-
- @Override public JavaType getValueType() { return _type; }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/NullifyingDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/NullifyingDeserializer.java
index 0453595..bd58ecc 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/NullifyingDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/NullifyingDeserializer.java
@@ -9,7 +9,7 @@
/**
* Bogus deserializer that will simply skip all content there is to map
* and returns Java null reference.
- *
+ *
* @since 2.2
*/
public class NullifyingDeserializer
@@ -26,7 +26,12 @@
/* Deserializer API
/**********************************************************
*/
-
+
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return Boolean.FALSE;
+ }
+
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java
index e545d6b..a5bb52b 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java
@@ -6,9 +6,11 @@
import java.util.HashSet;
import com.fasterxml.jackson.core.*;
+import com.fasterxml.jackson.core.io.NumberInput;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
+import com.fasterxml.jackson.databind.util.AccessPattern;
/**
* Container class for deserializers that handle core JDK primitive
@@ -120,35 +122,47 @@
private static final long serialVersionUID = 1L;
protected final T _nullValue;
+
+ // @since 2.9
+ protected final T _emptyValue;
+
protected final boolean _primitive;
- protected PrimitiveOrWrapperDeserializer(Class<T> vc, T nvl) {
+ protected PrimitiveOrWrapperDeserializer(Class<T> vc, T nvl, T empty) {
super(vc);
_nullValue = nvl;
+ _emptyValue = empty;
_primitive = vc.isPrimitive();
}
@Override
- public final T getNullValue(DeserializationContext ctxt) throws JsonMappingException
- {
+ public AccessPattern getNullAccessPattern() {
+ // 02-Feb-2017, tatu: For primitives we must dynamically check (and possibly throw
+ // exception); for wrappers not.
+ if (_primitive) {
+ return AccessPattern.DYNAMIC;
+ }
+ if (_nullValue == null) {
+ return AccessPattern.ALWAYS_NULL;
+ }
+ return AccessPattern.CONSTANT;
+ }
+
+ @Override
+ public final T getNullValue(DeserializationContext ctxt) throws JsonMappingException {
+ // 01-Mar-2017, tatu: Alas, not all paths lead to `_coerceNull()`, as `SettableBeanProperty`
+ // short-circuits `null` handling. Hence need this check as well.
if (_primitive && ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
- ctxt.reportMappingException(
- "Can not map JSON null into type %s (set DeserializationConfig.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES to 'false' to allow)",
+ ctxt.reportInputMismatch(this,
+ "Can not map `null` into type %s (set DeserializationConfig.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES to 'false' to allow)",
handledType().toString());
}
return _nullValue;
}
@Override
- public T getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
- // [databind#1095]: Should not allow coercion from into null from Empty String
- // either, if `null` not allowed
- if (_primitive && ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
- ctxt.reportMappingException(
- "Can not map Empty String as null into type %s (set DeserializationConfig.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES to 'false' to allow)",
- handledType().toString());
- }
- return _nullValue;
+ public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
+ return _emptyValue;
}
}
@@ -169,13 +183,20 @@
public BooleanDeserializer(Class<Boolean> cls, Boolean nvl)
{
- super(cls, nvl);
+ super(cls, nvl, Boolean.FALSE);
}
@Override
- public Boolean deserialize(JsonParser j, DeserializationContext ctxt) throws IOException
+ public Boolean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
- return _parseBoolean(j, ctxt);
+ JsonToken t = p.getCurrentToken();
+ if (t == JsonToken.VALUE_TRUE) {
+ return Boolean.TRUE;
+ }
+ if (t == JsonToken.VALUE_FALSE) {
+ return Boolean.FALSE;
+ }
+ return _parseBoolean(p, ctxt);
}
// Since we can never have type info ("natural type"; String, Boolean, Integer, Double):
@@ -185,8 +206,61 @@
TypeDeserializer typeDeserializer)
throws IOException
{
+ JsonToken t = p.getCurrentToken();
+ if (t == JsonToken.VALUE_TRUE) {
+ return Boolean.TRUE;
+ }
+ if (t == JsonToken.VALUE_FALSE) {
+ return Boolean.FALSE;
+ }
return _parseBoolean(p, ctxt);
}
+
+ protected final Boolean _parseBoolean(JsonParser p, DeserializationContext ctxt)
+ throws IOException
+ {
+ JsonToken t = p.getCurrentToken();
+ if (t == JsonToken.VALUE_NULL) {
+ return (Boolean) _coerceNullToken(ctxt, _primitive);
+ }
+ if (t == JsonToken.START_ARRAY) { // unwrapping?
+ return _deserializeFromArray(p, ctxt);
+ }
+ // should accept ints too, (0 == false, otherwise true)
+ if (t == JsonToken.VALUE_NUMBER_INT) {
+ return Boolean.valueOf(_parseBooleanFromInt(p, ctxt));
+ }
+ // And finally, let's allow Strings to be converted too
+ if (t == JsonToken.VALUE_STRING) {
+ String text = p.getText().trim();
+ // [databind#422]: Allow aliases
+ if ("true".equals(text) || "True".equals(text)) {
+ _verifyStringForScalarCoercion(ctxt, text);
+ return Boolean.TRUE;
+ }
+ if ("false".equals(text) || "False".equals(text)) {
+ _verifyStringForScalarCoercion(ctxt, text);
+ return Boolean.FALSE;
+ }
+ if (text.length() == 0) {
+ return (Boolean) _coerceEmptyString(ctxt, _primitive);
+ }
+ if (_hasTextualNull(text)) {
+ return (Boolean) _coerceTextualNull(ctxt, _primitive);
+ }
+ return (Boolean) ctxt.handleWeirdStringValue(_valueClass, text,
+ "only \"true\" or \"false\" recognized");
+ }
+ // usually caller should have handled but:
+ if (t == JsonToken.VALUE_TRUE) {
+ return Boolean.TRUE;
+ }
+ if (t == JsonToken.VALUE_FALSE) {
+ return Boolean.FALSE;
+ }
+ // Otherwise, no can do:
+ return (Boolean) ctxt.handleUnexpectedToken(_valueClass, p);
+ }
}
@JacksonStdImpl
@@ -200,14 +274,65 @@
public ByteDeserializer(Class<Byte> cls, Byte nvl)
{
- super(cls, nvl);
+ super(cls, nvl, (byte) 0);
}
@Override
public Byte deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
+ if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) {
+ return p.getByteValue();
+ }
return _parseByte(p, ctxt);
}
+
+ protected Byte _parseByte(JsonParser p, DeserializationContext ctxt) throws IOException
+ {
+ JsonToken t = p.getCurrentToken();
+ if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
+ String text = p.getText().trim();
+ if (_hasTextualNull(text)) {
+ return (Byte) _coerceTextualNull(ctxt, _primitive);
+ }
+ int len = text.length();
+ if (len == 0) {
+ return (Byte) _coerceEmptyString(ctxt, _primitive);
+ }
+ _verifyStringForScalarCoercion(ctxt, text);
+ int value;
+ try {
+ value = NumberInput.parseInt(text);
+ } catch (IllegalArgumentException iae) {
+ return (Byte) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid Byte value");
+ }
+ // So far so good: but does it fit?
+ // as per [JACKSON-804], allow range up to 255, inclusive
+ if (_byteOverflow(value)) {
+ return (Byte) ctxt.handleWeirdStringValue(_valueClass, text,
+ "overflow, value can not be represented as 8-bit value");
+ // fall-through for deferred fails
+ }
+ return Byte.valueOf((byte) value);
+ }
+ if (t == JsonToken.VALUE_NUMBER_FLOAT) {
+ if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
+ _failDoubleToIntCoercion(p, ctxt, "Byte");
+ }
+ return p.getByteValue();
+ }
+ if (t == JsonToken.VALUE_NULL) {
+ return (Byte) _coerceNullToken(ctxt, _primitive);
+ }
+ // [databind#381]
+ if (t == JsonToken.START_ARRAY) {
+ return _deserializeFromArray(p, ctxt);
+ }
+ if (t == JsonToken.VALUE_NUMBER_INT) { // shouldn't usually be called with it but
+ return p.getByteValue();
+ }
+ return (Byte) ctxt.handleUnexpectedToken(_valueClass, p);
+ }
}
@JacksonStdImpl
@@ -221,14 +346,59 @@
public ShortDeserializer(Class<Short> cls, Short nvl)
{
- super(cls, nvl);
+ super(cls, nvl, (short)0);
}
@Override
- public Short deserialize(JsonParser jp, DeserializationContext ctxt)
+ public Short deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
- return _parseShort(jp, ctxt);
+ return _parseShort(p, ctxt);
+ }
+
+ protected Short _parseShort(JsonParser p, DeserializationContext ctxt) throws IOException
+ {
+ JsonToken t = p.getCurrentToken();
+ if (t == JsonToken.VALUE_NUMBER_INT) {
+ return p.getShortValue();
+ }
+ if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
+ String text = p.getText().trim();
+ int len = text.length();
+ if (len == 0) {
+ return (Short) _coerceEmptyString(ctxt, _primitive);
+ }
+ if (_hasTextualNull(text)) {
+ return (Short) _coerceTextualNull(ctxt, _primitive);
+ }
+ _verifyStringForScalarCoercion(ctxt, text);
+ int value;
+ try {
+ value = NumberInput.parseInt(text);
+ } catch (IllegalArgumentException iae) {
+ return (Short) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid Short value");
+ }
+ // So far so good: but does it fit?
+ if (_shortOverflow(value)) {
+ return (Short) ctxt.handleWeirdStringValue(_valueClass, text,
+ "overflow, value can not be represented as 16-bit value");
+ }
+ return Short.valueOf((short) value);
+ }
+ if (t == JsonToken.VALUE_NUMBER_FLOAT) {
+ if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
+ _failDoubleToIntCoercion(p, ctxt, "Short");
+ }
+ return p.getShortValue();
+ }
+ if (t == JsonToken.VALUE_NULL) {
+ return (Short) _coerceNullToken(ctxt, _primitive);
+ }
+ if (t == JsonToken.START_ARRAY) {
+ return _deserializeFromArray(p, ctxt);
+ }
+ return (Short) ctxt.handleUnexpectedToken(_valueClass, p);
}
}
@@ -243,7 +413,7 @@
public CharacterDeserializer(Class<Character> cls, Character nvl)
{
- super(cls, nvl);
+ super(cls, nvl, '\0');
}
@Override
@@ -252,6 +422,7 @@
{
switch (p.getCurrentTokenId()) {
case JsonTokenId.ID_NUMBER_INT: // ok iff ascii value
+ _verifyNumberForScalarCoercion(ctxt, p);
int value = p.getIntValue();
if (value >= 0 && value <= 0xFFFF) {
return Character.valueOf((char) value);
@@ -265,9 +436,11 @@
}
// actually, empty should become null?
if (text.length() == 0) {
- return (Character) getEmptyValue(ctxt);
- }
+ return (Character) _coerceEmptyString(ctxt, _primitive);
+ }
break;
+ case JsonTokenId.ID_NULL:
+ return (Character) _coerceNullToken(ctxt, _primitive);
case JsonTokenId.ID_START_ARRAY:
return _deserializeFromArray(p, ctxt);
default:
@@ -282,11 +455,11 @@
{
private static final long serialVersionUID = 1L;
- final static IntegerDeserializer primitiveInstance = new IntegerDeserializer(Integer.TYPE, Integer.valueOf(0));
+ final static IntegerDeserializer primitiveInstance = new IntegerDeserializer(Integer.TYPE, 0);
final static IntegerDeserializer wrapperInstance = new IntegerDeserializer(Integer.class, null);
public IntegerDeserializer(Class<Integer> cls, Integer nvl) {
- super(cls, nvl);
+ super(cls, nvl, 0);
}
// since 2.6, slightly faster lookups for this very common type
@@ -312,6 +485,51 @@
}
return _parseInteger(p, ctxt);
}
+
+ protected final Integer _parseInteger(JsonParser p, DeserializationContext ctxt) throws IOException
+ {
+ switch (p.getCurrentTokenId()) {
+ // NOTE: caller assumed to usually check VALUE_NUMBER_INT in fast path
+ case JsonTokenId.ID_NUMBER_INT:
+ return Integer.valueOf(p.getIntValue());
+ case JsonTokenId.ID_NUMBER_FLOAT: // coercing may work too
+ if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
+ _failDoubleToIntCoercion(p, ctxt, "Integer");
+ }
+ return Integer.valueOf(p.getValueAsInt());
+ case JsonTokenId.ID_STRING: // let's do implicit re-parse
+ String text = p.getText().trim();
+ int len = text.length();
+ if (len == 0) {
+ return (Integer) _coerceEmptyString(ctxt, _primitive);
+ }
+ if (_hasTextualNull(text)) {
+ return (Integer) _coerceTextualNull(ctxt, _primitive);
+ }
+ _verifyStringForScalarCoercion(ctxt, text);
+ try {
+ if (len > 9) {
+ long l = Long.parseLong(text);
+ if (_intOverflow(l)) {
+ return (Integer) ctxt.handleWeirdStringValue(_valueClass, text, String.format(
+ "Overflow: numeric value (%s) out of range of Integer (%d - %d)",
+ text, Integer.MIN_VALUE, Integer.MAX_VALUE));
+ }
+ return Integer.valueOf((int) l);
+ }
+ return Integer.valueOf(NumberInput.parseInt(text));
+ } catch (IllegalArgumentException iae) {
+ return (Integer) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid Integer value");
+ }
+ case JsonTokenId.ID_NULL:
+ return (Integer) _coerceNullToken(ctxt, _primitive);
+ case JsonTokenId.ID_START_ARRAY:
+ return _deserializeFromArray(p, ctxt);
+ }
+ // Otherwise, no can do:
+ return (Integer) ctxt.handleUnexpectedToken(_valueClass, p);
+ }
}
@JacksonStdImpl
@@ -320,11 +538,11 @@
{
private static final long serialVersionUID = 1L;
- final static LongDeserializer primitiveInstance = new LongDeserializer(Long.TYPE, Long.valueOf(0L));
+ final static LongDeserializer primitiveInstance = new LongDeserializer(Long.TYPE, 0L);
final static LongDeserializer wrapperInstance = new LongDeserializer(Long.class, null);
public LongDeserializer(Class<Long> cls, Long nvl) {
- super(cls, nvl);
+ super(cls, nvl, 0L);
}
// since 2.6, slightly faster lookups for this very common type
@@ -338,6 +556,42 @@
}
return _parseLong(p, ctxt);
}
+
+ protected final Long _parseLong(JsonParser p, DeserializationContext ctxt) throws IOException
+ {
+ switch (p.getCurrentTokenId()) {
+ // NOTE: caller assumed to usually check VALUE_NUMBER_INT in fast path
+ case JsonTokenId.ID_NUMBER_INT:
+ return p.getLongValue();
+ case JsonTokenId.ID_NUMBER_FLOAT:
+ if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
+ _failDoubleToIntCoercion(p, ctxt, "Long");
+ }
+ return p.getValueAsLong();
+ case JsonTokenId.ID_STRING:
+ String text = p.getText().trim();
+ if (text.length() == 0) {
+ return (Long) _coerceEmptyString(ctxt, _primitive);
+ }
+ if (_hasTextualNull(text)) {
+ return (Long) _coerceTextualNull(ctxt, _primitive);
+ }
+ _verifyStringForScalarCoercion(ctxt, text);
+ // let's allow Strings to be converted too
+ try {
+ return Long.valueOf(NumberInput.parseLong(text));
+ } catch (IllegalArgumentException iae) { }
+ return (Long) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid Long value");
+ // fall-through
+ case JsonTokenId.ID_NULL:
+ return (Long) _coerceNullToken(ctxt, _primitive);
+ case JsonTokenId.ID_START_ARRAY:
+ return _deserializeFromArray(p, ctxt);
+ }
+ // Otherwise, no can do:
+ return (Long) ctxt.handleUnexpectedToken(_valueClass, p);
+ }
}
@JacksonStdImpl
@@ -350,7 +604,7 @@
final static FloatDeserializer wrapperInstance = new FloatDeserializer(Float.class, null);
public FloatDeserializer(Class<Float> cls, Float nvl) {
- super(cls, nvl);
+ super(cls, nvl, 0.f);
}
@Override
@@ -358,6 +612,58 @@
{
return _parseFloat(p, ctxt);
}
+
+ protected final Float _parseFloat(JsonParser p, DeserializationContext ctxt)
+ throws IOException
+ {
+ // We accept couple of different types; obvious ones first:
+ JsonToken t = p.getCurrentToken();
+
+ if (t == JsonToken.VALUE_NUMBER_FLOAT || t == JsonToken.VALUE_NUMBER_INT) { // coercing should work too
+ return p.getFloatValue();
+ }
+ // And finally, let's allow Strings to be converted too
+ if (t == JsonToken.VALUE_STRING) {
+ String text = p.getText().trim();
+ if ((text.length() == 0)) {
+ return (Float) _coerceEmptyString(ctxt, _primitive);
+ }
+ if (_hasTextualNull(text)) {
+ return (Float) _coerceTextualNull(ctxt, _primitive);
+ }
+ switch (text.charAt(0)) {
+ case 'I':
+ if (_isPosInf(text)) {
+ return Float.POSITIVE_INFINITY;
+ }
+ break;
+ case 'N':
+ if (_isNaN(text)) {
+ return Float.NaN;
+ }
+ break;
+ case '-':
+ if (_isNegInf(text)) {
+ return Float.NEGATIVE_INFINITY;
+ }
+ break;
+ }
+ _verifyStringForScalarCoercion(ctxt, text);
+ try {
+ return Float.parseFloat(text);
+ } catch (IllegalArgumentException iae) { }
+ return (Float) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid Float value");
+ }
+ if (t == JsonToken.VALUE_NULL) {
+ return (Float) _coerceNullToken(ctxt, _primitive);
+ }
+ if (t == JsonToken.START_ARRAY) {
+ return _deserializeFromArray(p, ctxt);
+ }
+ // Otherwise, no can do:
+ return (Float) ctxt.handleUnexpectedToken(_valueClass, p);
+ }
}
@JacksonStdImpl
@@ -370,21 +676,69 @@
final static DoubleDeserializer wrapperInstance = new DoubleDeserializer(Double.class, null);
public DoubleDeserializer(Class<Double> cls, Double nvl) {
- super(cls, nvl);
+ super(cls, nvl, 0.d);
}
@Override
- public Double deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
- return _parseDouble(jp, ctxt);
+ public Double deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
+ return _parseDouble(p, ctxt);
}
// Since we can never have type info ("natural type"; String, Boolean, Integer, Double):
// (is it an error to even call this version?)
@Override
- public Double deserializeWithType(JsonParser jp, DeserializationContext ctxt,
+ public Double deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer) throws IOException
{
- return _parseDouble(jp, ctxt);
+ return _parseDouble(p, ctxt);
+ }
+
+ protected final Double _parseDouble(JsonParser p, DeserializationContext ctxt) throws IOException
+ {
+ JsonToken t = p.getCurrentToken();
+ if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
+ return p.getDoubleValue();
+ }
+ if (t == JsonToken.VALUE_STRING) {
+ String text = p.getText().trim();
+ if ((text.length() == 0)) {
+ return (Double) _coerceEmptyString(ctxt, _primitive);
+ }
+ if (_hasTextualNull(text)) {
+ return (Double) _coerceTextualNull(ctxt, _primitive);
+ }
+ switch (text.charAt(0)) {
+ case 'I':
+ if (_isPosInf(text)) {
+ return Double.POSITIVE_INFINITY;
+ }
+ break;
+ case 'N':
+ if (_isNaN(text)) {
+ return Double.NaN;
+ }
+ break;
+ case '-':
+ if (_isNegInf(text)) {
+ return Double.NEGATIVE_INFINITY;
+ }
+ break;
+ }
+ _verifyStringForScalarCoercion(ctxt, text);
+ try {
+ return parseDouble(text);
+ } catch (IllegalArgumentException iae) { }
+ return (Double) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid Double value");
+ }
+ if (t == JsonToken.VALUE_NULL) {
+ return (Double) _coerceNullToken(ctxt, _primitive);
+ }
+ if (t == JsonToken.START_ARRAY) {
+ return _deserializeFromArray(p, ctxt);
+ }
+ // Otherwise, no can do:
+ return (Double) ctxt.handleUnexpectedToken(_valueClass, p);
}
}
@@ -421,7 +775,10 @@
case JsonTokenId.ID_NUMBER_FLOAT:
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
- return p.getDecimalValue();
+ // 10-Mar-2017, tatu: NaN and BigDecimal won't mix...
+ if (!p.isNaN()) {
+ return p.getDecimalValue();
+ }
}
return p.getNumberValue();
@@ -430,10 +787,12 @@
* out 'minimal' type to use
*/
String text = p.getText().trim();
- if (text.length() == 0) {
- return getEmptyValue(ctxt);
+ if ((text.length() == 0)) {
+ // note: no need to call `coerce` as this is never primitive
+ return getNullValue(ctxt);
}
if (_hasTextualNull(text)) {
+ // note: no need to call `coerce` as this is never primitive
return getNullValue(ctxt);
}
if (_isPosInf(text)) {
@@ -445,12 +804,13 @@
if (_isNaN(text)) {
return Double.NaN;
}
+ _verifyStringForScalarCoercion(ctxt, text);
try {
if (!_isIntNumber(text)) {
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
return new BigDecimal(text);
}
- return new Double(text);
+ return Double.valueOf(text);
}
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS)) {
return new BigInteger(text);
@@ -480,18 +840,18 @@
* calling type deserializer.
*/
@Override
- public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt,
- TypeDeserializer typeDeserializer)
+ public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
+ TypeDeserializer typeDeserializer)
throws IOException
{
- switch (jp.getCurrentTokenId()) {
+ switch (p.getCurrentTokenId()) {
case JsonTokenId.ID_NUMBER_INT:
case JsonTokenId.ID_NUMBER_FLOAT:
case JsonTokenId.ID_STRING:
// can not point to type information: hence must be non-typed (int/double)
- return deserialize(jp, ctxt);
+ return deserialize(p, ctxt);
}
- return typeDeserializer.deserializeTypedFromScalar(jp, ctxt);
+ return typeDeserializer.deserializeTypedFromScalar(p, ctxt);
}
}
@@ -515,6 +875,11 @@
public BigIntegerDeserializer() { super(BigInteger.class); }
+ @Override
+ public Object getEmptyValue(DeserializationContext ctxt) {
+ return BigInteger.ZERO;
+ }
+
@SuppressWarnings("incomplete-switch")
@Override
public BigInteger deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
@@ -537,15 +902,17 @@
return _deserializeFromArray(p, ctxt);
case JsonTokenId.ID_STRING: // let's do implicit re-parse
String text = p.getText().trim();
- if (text.length() == 0) {
- return null;
+ // note: no need to call `coerce` as this is never primitive
+ if (_isEmptyOrTextualNull(text)) {
+ _verifyNullForScalarCoercion(ctxt, text);
+ return getNullValue(ctxt);
}
+ _verifyStringForScalarCoercion(ctxt, text);
try {
return new BigInteger(text);
- } catch (IllegalArgumentException iae) {
- return (BigInteger) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid representation");
- }
+ } catch (IllegalArgumentException iae) { }
+ return (BigInteger) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid representation");
}
// String is ok too, can easily convert; otherwise, no can do:
return (BigInteger) ctxt.handleUnexpectedToken(_valueClass, p);
@@ -562,6 +929,11 @@
public BigDecimalDeserializer() { super(BigDecimal.class); }
@Override
+ public Object getEmptyValue(DeserializationContext ctxt) {
+ return BigDecimal.ZERO;
+ }
+
+ @Override
public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
@@ -571,15 +943,17 @@
return p.getDecimalValue();
case JsonTokenId.ID_STRING:
String text = p.getText().trim();
- if (text.length() == 0) {
- return null;
+ // note: no need to call `coerce` as this is never primitive
+ if (_isEmptyOrTextualNull(text)) {
+ _verifyNullForScalarCoercion(ctxt, text);
+ return getNullValue(ctxt);
}
+ _verifyStringForScalarCoercion(ctxt, text);
try {
return new BigDecimal(text);
- } catch (IllegalArgumentException iae) {
- return (BigDecimal) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid representation");
- }
+ } catch (IllegalArgumentException iae) { }
+ return (BigDecimal) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid representation");
case JsonTokenId.ID_START_ARRAY:
return _deserializeFromArray(p, ctxt);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/ObjectArrayDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/ObjectArrayDeserializer.java
index 101217b..017317d 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/ObjectArrayDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/ObjectArrayDeserializer.java
@@ -10,8 +10,9 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
-import com.fasterxml.jackson.databind.type.ArrayType;
+import com.fasterxml.jackson.databind.util.AccessPattern;
import com.fasterxml.jackson.databind.util.ObjectBuffer;
/**
@@ -24,14 +25,11 @@
{
private static final long serialVersionUID = 1L;
+ protected final static Object[] NO_OBJECTS = new Object[0];
+
// // Configuration
/**
- * Full generic type of the array being deserialized
- */
- protected final ArrayType _arrayType;
-
- /**
* Flag that indicates whether the component type is Object or not.
* Used for minor optimization when constructing result.
*/
@@ -54,54 +52,42 @@
*/
protected final TypeDeserializer _elementTypeDeserializer;
- /**
- * Specific override for this instance (from proper, or global per-type overrides)
- * to indicate whether single value may be taken to mean an unwrapped one-element array
- * or not. If null, left to global defaults.
- *
- * @since 2.7
- */
- protected final Boolean _unwrapSingle;
-
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
- public ObjectArrayDeserializer(ArrayType arrayType,
+ public ObjectArrayDeserializer(JavaType arrayType,
JsonDeserializer<Object> elemDeser, TypeDeserializer elemTypeDeser)
{
- super(arrayType);
- _arrayType = arrayType;
+ super(arrayType, null, null);
_elementClass = arrayType.getContentType().getRawClass();
_untyped = (_elementClass == Object.class);
_elementDeserializer = elemDeser;
_elementTypeDeserializer = elemTypeDeser;
- _unwrapSingle = null;
}
protected ObjectArrayDeserializer(ObjectArrayDeserializer base,
JsonDeserializer<Object> elemDeser, TypeDeserializer elemTypeDeser,
- Boolean unwrapSingle)
+ NullValueProvider nuller, Boolean unwrapSingle)
{
- super(base._arrayType);
- _arrayType = base._arrayType;
+ super(base, nuller, unwrapSingle);
_elementClass = base._elementClass;
_untyped = base._untyped;
_elementDeserializer = elemDeser;
_elementTypeDeserializer = elemTypeDeser;
- _unwrapSingle = unwrapSingle;
}
-
+
/**
* Overridable fluent-factory method used to create contextual instances
*/
public ObjectArrayDeserializer withDeserializer(TypeDeserializer elemTypeDeser,
JsonDeserializer<?> elemDeser)
{
- return withResolved(elemTypeDeser, elemDeser, _unwrapSingle);
+ return withResolved(elemTypeDeser, elemDeser,
+ _nullProvider, _unwrapSingle);
}
/**
@@ -109,43 +95,46 @@
*/
@SuppressWarnings("unchecked")
public ObjectArrayDeserializer withResolved(TypeDeserializer elemTypeDeser,
- JsonDeserializer<?> elemDeser, Boolean unwrapSingle)
+ JsonDeserializer<?> elemDeser, NullValueProvider nuller, Boolean unwrapSingle)
{
- if ((unwrapSingle == _unwrapSingle)
+ if ((unwrapSingle == _unwrapSingle) && (nuller == _nullProvider)
&& (elemDeser == _elementDeserializer)
&& (elemTypeDeser == _elementTypeDeserializer)) {
return this;
}
return new ObjectArrayDeserializer(this,
- (JsonDeserializer<Object>) elemDeser, elemTypeDeser, unwrapSingle);
+ (JsonDeserializer<Object>) elemDeser, elemTypeDeser,
+ nuller, unwrapSingle);
+ }
+
+ @Override // since 2.5
+ public boolean isCachable() {
+ // Important: do NOT cache if polymorphic values, or if there are annotation-based
+ // custom deserializers
+ return (_elementDeserializer == null) && (_elementTypeDeserializer == null);
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
- JsonDeserializer<?> deser = _elementDeserializer;
- Boolean unwrapSingle = findFormatFeature(ctxt, property, _arrayType.getRawClass(),
+ JsonDeserializer<?> valueDeser = _elementDeserializer;
+ Boolean unwrapSingle = findFormatFeature(ctxt, property, _containerType.getRawClass(),
JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
// May have a content converter
- deser = findConvertingContentDeserializer(ctxt, property, deser);
- final JavaType vt = _arrayType.getContentType();
- if (deser == null) {
- deser = ctxt.findContextualValueDeserializer(vt, property);
+ valueDeser = findConvertingContentDeserializer(ctxt, property, valueDeser);
+ final JavaType vt = _containerType.getContentType();
+ if (valueDeser == null) {
+ valueDeser = ctxt.findContextualValueDeserializer(vt, property);
} else { // if directly assigned, probably not yet contextual, so:
- deser = ctxt.handleSecondaryContextualization(deser, property, vt);
+ valueDeser = ctxt.handleSecondaryContextualization(valueDeser, property, vt);
}
TypeDeserializer elemTypeDeser = _elementTypeDeserializer;
if (elemTypeDeser != null) {
elemTypeDeser = elemTypeDeser.forProperty(property);
}
- return withResolved(elemTypeDeser, deser, unwrapSingle);
- }
-
- @Override // since 2.5
- public boolean isCachable() {
- // Important: do NOT cache if polymorphic values, or ones with custom deserializer
- return (_elementDeserializer == null) && (_elementTypeDeserializer == null);
+ NullValueProvider nuller = findContentNullProvider(ctxt, property, valueDeser);
+ return withResolved(elemTypeDeser, valueDeser, nuller, unwrapSingle);
}
/*
@@ -155,15 +144,22 @@
*/
@Override
- public JavaType getContentType() {
- return _arrayType.getContentType();
- }
-
- @Override
public JsonDeserializer<Object> getContentDeserializer() {
return _elementDeserializer;
}
-
+
+ @Override // since 2.9
+ public AccessPattern getEmptyAccessPattern() {
+ // immutable, shareable so:
+ return AccessPattern.CONSTANT;
+ }
+
+ // need to override as we can't expose ValueInstantiator
+ @Override // since 2.9
+ public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
+ return NO_OBJECTS;
+ }
+
/*
/**********************************************************
/* JsonDeserializer API
@@ -191,7 +187,10 @@
Object value;
if (t == JsonToken.VALUE_NULL) {
- value = _elementDeserializer.getNullValue(ctxt);
+ if (_skipNullValues) {
+ continue;
+ }
+ value = _nullProvider.getNullValue(ctxt);
} else if (typeDeser == null) {
value = _elementDeserializer.deserialize(p, ctxt);
} else {
@@ -223,12 +222,68 @@
TypeDeserializer typeDeserializer)
throws IOException
{
- /* Should there be separate handling for base64 stuff?
- * for now this should be enough:
- */
+ // Should there be separate handling for base64 stuff?
+ // for now this should be enough:
return (Object[]) typeDeserializer.deserializeTypedFromArray(p, ctxt);
}
-
+
+ @Override // since 2.9
+ public Object[] deserialize(JsonParser p, DeserializationContext ctxt,
+ Object[] intoValue) throws IOException
+ {
+ if (!p.isExpectedStartArrayToken()) {
+ Object[] arr = handleNonArray(p, ctxt);
+ if (arr == null) {
+ return intoValue;
+ }
+ final int offset = intoValue.length;
+ Object[] result = new Object[offset + arr.length];
+ System.arraycopy(intoValue, 0, result, 0, offset);
+ System.arraycopy(arr, 0, result, offset, arr.length);
+ return result;
+ }
+
+ final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
+ int ix = intoValue.length;
+ Object[] chunk = buffer.resetAndStart(intoValue, ix);
+ JsonToken t;
+ final TypeDeserializer typeDeser = _elementTypeDeserializer;
+
+ try {
+ while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
+ Object value;
+
+ if (t == JsonToken.VALUE_NULL) {
+ if (_skipNullValues) {
+ continue;
+ }
+ value = _nullProvider.getNullValue(ctxt);
+ } else if (typeDeser == null) {
+ value = _elementDeserializer.deserialize(p, ctxt);
+ } else {
+ value = _elementDeserializer.deserializeWithType(p, ctxt, typeDeser);
+ }
+ if (ix >= chunk.length) {
+ chunk = buffer.appendCompletedChunk(chunk);
+ ix = 0;
+ }
+ chunk[ix++] = value;
+ }
+ } catch (Exception e) {
+ throw JsonMappingException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
+ }
+
+ Object[] result;
+
+ if (_untyped) {
+ result = buffer.completeAndClearBuffer(chunk, ix);
+ } else {
+ result = buffer.completeAndClearBuffer(chunk, ix, _elementClass);
+ }
+ ctxt.returnObjectBuffer(buffer);
+ return result;
+ }
+
/*
/**********************************************************
/* Internal methods
@@ -259,7 +314,7 @@
return null;
}
}
-
+
// Can we do implicit coercion to a single-element array still?
boolean canWrap = (_unwrapSingle == Boolean.TRUE) ||
((_unwrapSingle == null) &&
@@ -272,13 +327,17 @@
&& _elementClass == Byte.class) {
return deserializeFromBase64(p, ctxt);
}
- return (Object[]) ctxt.handleUnexpectedToken(_arrayType.getRawClass(), p);
+ return (Object[]) ctxt.handleUnexpectedToken(_containerType.getRawClass(), p);
}
JsonToken t = p.getCurrentToken();
Object value;
if (t == JsonToken.VALUE_NULL) {
- value = _elementDeserializer.getNullValue(ctxt);
+ // 03-Feb-2017, tatu: Should this be skipped or not?
+ if (_skipNullValues) {
+ return NO_OBJECTS;
+ }
+ value = _nullProvider.getNullValue(ctxt);
} else if (_elementTypeDeserializer == null) {
value = _elementDeserializer.deserialize(p, ctxt);
} else {
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/PrimitiveArrayDeserializers.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/PrimitiveArrayDeserializers.java
index 4221b30..099e849 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/PrimitiveArrayDeserializers.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/PrimitiveArrayDeserializers.java
@@ -1,13 +1,21 @@
package com.fasterxml.jackson.databind.deser.std;
import java.io.IOException;
+import java.lang.reflect.Array;
+import java.util.Arrays;
import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
+import com.fasterxml.jackson.databind.deser.impl.NullsConstantProvider;
+import com.fasterxml.jackson.databind.deser.impl.NullsFailProvider;
+import com.fasterxml.jackson.databind.exc.InvalidNullException;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
+import com.fasterxml.jackson.databind.util.AccessPattern;
import com.fasterxml.jackson.databind.util.ArrayBuilders;
/**
@@ -27,18 +35,35 @@
*/
protected final Boolean _unwrapSingle;
+ // since 2.9
+ private transient Object _emptyValue;
+
+ /**
+ * Flag that indicates need for special handling; either failing
+ * (throw exception) or skipping
+ */
+ protected final NullValueProvider _nuller;
+
+ /*
+ /********************************************************
+ /* Life-cycle
+ /********************************************************
+ */
+
protected PrimitiveArrayDeserializers(Class<T> cls) {
super(cls);
_unwrapSingle = null;
+ _nuller = null;
}
/**
* @since 2.7
*/
protected PrimitiveArrayDeserializers(PrimitiveArrayDeserializers<?> base,
- Boolean unwrapSingle) {
+ NullValueProvider nuller, Boolean unwrapSingle) {
super(base._valueClass);
_unwrapSingle = unwrapSingle;
+ _nuller = nuller;
}
public static JsonDeserializer<?> forType(Class<?> rawType)
@@ -72,37 +97,134 @@
throw new IllegalStateException();
}
- /**
- * @since 2.7
- */
- protected abstract PrimitiveArrayDeserializers<?> withResolved(Boolean unwrapSingle);
-
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
Boolean unwrapSingle = findFormatFeature(ctxt, property, _valueClass,
JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
- if (unwrapSingle == _unwrapSingle) {
+ NullValueProvider nuller = null;
+
+ Nulls nullStyle = findContentNullStyle(ctxt, property);
+ if (nullStyle == Nulls.SKIP) {
+ nuller = NullsConstantProvider.skipper();
+ } else if (nullStyle == Nulls.FAIL) {
+ if (property == null) {
+ nuller = NullsFailProvider.constructForRootValue(ctxt.constructType(_valueClass));
+ } else {
+ nuller = NullsFailProvider.constructForProperty(property);
+ }
+ }
+ if ((unwrapSingle == _unwrapSingle) && (nuller == _nuller)) {
return this;
}
- return withResolved(unwrapSingle);
+ return withResolved(nuller, unwrapSingle);
+ }
+
+ /*
+ /********************************************************
+ /* Abstract methods for sub-classes to implement
+ /********************************************************
+ */
+
+ /**
+ * @since 2.9
+ */
+ protected abstract T _concat(T oldValue, T newValue);
+
+ protected abstract T handleSingleElementUnwrapped(JsonParser p,
+ DeserializationContext ctxt) throws IOException;
+
+ /**
+ * @since 2.9
+ */
+ protected abstract PrimitiveArrayDeserializers<?> withResolved(NullValueProvider nuller,
+ Boolean unwrapSingle);
+
+ // since 2.9
+ protected abstract T _constructEmpty();
+
+ /*
+ /********************************************************
+ /* Default implementations
+ /********************************************************
+ */
+
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return Boolean.TRUE;
+ }
+
+ @Override
+ public AccessPattern getEmptyAccessPattern() {
+ // Empty values shareable freely
+ return AccessPattern.CONSTANT;
+ }
+
+ @Override // since 2.9
+ public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
+ Object empty = _emptyValue;
+ if (empty == null) {
+ _emptyValue = empty = _constructEmpty();
+ }
+ return empty;
}
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer) throws IOException
{
- /* Should there be separate handling for base64 stuff?
- * for now this should be enough:
- */
+ // Should there be separate handling for base64 stuff?
+ // for now this should be enough:
return typeDeserializer.deserializeTypedFromArray(p, ctxt);
}
+ @Override
+ public T deserialize(JsonParser p, DeserializationContext ctxt, T existing) throws IOException
+ {
+ T newValue = deserialize(p, ctxt);
+ if (existing == null) {
+ return newValue;
+ }
+ int len = Array.getLength(existing);
+ if (len == 0) {
+ return newValue;
+ }
+ return _concat(existing, newValue);
+ }
+
+ /*
+ /********************************************************
+ /* Helper methods for sub-classes
+ /********************************************************
+ */
+
+ /*
+ * Convenience method that constructs a concatenation of two arrays,
+ * with the type they have.
+ *
+ * @since 2.9
+ @SuppressWarnings("unchecked")
+ public static <T> T concatArrays(T array1, T array2)
+ {
+ int len1 = Array.getLength(array1);
+ if (len1 == 0) {
+ return array2;
+ }
+ int len2 = Array.getLength(array2);
+ if (len2 == 0) {
+ return array1;
+ }
+ Object result = Arrays.copyOf((Object[]) array1, len1 + len2);
+ System.arraycopy(array2, 0, result, len1, len2);
+ return (T) result;
+ }
+ */
+
@SuppressWarnings("unchecked")
protected T handleNonArray(JsonParser p, DeserializationContext ctxt) throws IOException
{
- // [JACKSON-620] Empty String can become null...
+ // Empty String can become null...
if (p.hasToken(JsonToken.VALUE_STRING)
&& ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
if (p.getText().length() == 0) {
@@ -118,8 +240,10 @@
return (T) ctxt.handleUnexpectedToken(_valueClass, p);
}
- protected abstract T handleSingleElementUnwrapped(JsonParser p,
- DeserializationContext ctxt) throws IOException;
+ protected void _failOnNull(DeserializationContext ctxt) throws IOException
+ {
+ throw InvalidNullException.from(ctxt, null, ctxt.constructType(_valueClass));
+ }
/*
/********************************************************
@@ -134,16 +258,22 @@
private static final long serialVersionUID = 1L;
public CharDeser() { super(char[].class); }
- protected CharDeser(CharDeser base, Boolean unwrapSingle) {
- super(base, unwrapSingle);
+ protected CharDeser(CharDeser base, NullValueProvider nuller, Boolean unwrapSingle) {
+ super(base, nuller, unwrapSingle);
}
@Override
- protected PrimitiveArrayDeserializers<?> withResolved(Boolean unwrapSingle) {
+ protected PrimitiveArrayDeserializers<?> withResolved(NullValueProvider nuller,
+ Boolean unwrapSingle) {
// 11-Dec-2015, tatu: Not sure how re-wrapping would work; omit
return this;
}
-
+
+ @Override
+ protected char[] _constructEmpty() {
+ return new char[0];
+ }
+
@Override
public char[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
@@ -169,13 +299,20 @@
String str;
if (t == JsonToken.VALUE_STRING) {
str = p.getText();
+ } else if (t == JsonToken.VALUE_NULL) {
+ if (_nuller != null) {
+ _nuller.getNullValue(ctxt);
+ continue;
+ }
+ _verifyNullForPrimitive(ctxt);
+ str = "\0";
} else {
CharSequence cs = (CharSequence) ctxt.handleUnexpectedToken(Character.TYPE, p);
str = cs.toString();
}
if (str.length() != 1) {
- ctxt.reportMappingException("Can not convert a JSON String of length %d into a char element of char array",
- str.length());
+ ctxt.reportInputMismatch(this,
+"Can not convert a JSON String of length %d into a char element of char array", str.length());
}
sb.append(str.charAt(0));
}
@@ -206,6 +343,15 @@
// not sure how this should work...
return (char[]) ctxt.handleUnexpectedToken(_valueClass, p);
}
+
+ @Override
+ protected char[] _concat(char[] oldValue, char[] newValue) {
+ int len1 = oldValue.length;
+ int len2 = newValue.length;
+ char[] result = Arrays.copyOf(oldValue, len1+len2);
+ System.arraycopy(newValue, 0, result, len1, len2);
+ return result;
+ }
}
/*
@@ -221,18 +367,24 @@
private static final long serialVersionUID = 1L;
public BooleanDeser() { super(boolean[].class); }
- protected BooleanDeser(BooleanDeser base, Boolean unwrapSingle) {
- super(base, unwrapSingle);
+ protected BooleanDeser(BooleanDeser base, NullValueProvider nuller, Boolean unwrapSingle) {
+ super(base, nuller, unwrapSingle);
}
@Override
- protected PrimitiveArrayDeserializers<?> withResolved(Boolean unwrapSingle) {
- return new BooleanDeser(this, unwrapSingle);
+ protected PrimitiveArrayDeserializers<?> withResolved(NullValueProvider nuller,
+ Boolean unwrapSingle) {
+ return new BooleanDeser(this, nuller, unwrapSingle);
+ }
+
+ @Override
+ protected boolean[] _constructEmpty() {
+ return new boolean[0];
}
@Override
public boolean[] deserialize(JsonParser p, DeserializationContext ctxt)
- throws IOException, JsonProcessingException
+ throws IOException
{
if (!p.isExpectedStartArrayToken()) {
return handleNonArray(p, ctxt);
@@ -242,9 +394,23 @@
int ix = 0;
try {
- while (p.nextToken() != JsonToken.END_ARRAY) {
- // whether we should allow truncating conversions?
- boolean value = _parseBooleanPrimitive(p, ctxt);
+ JsonToken t;
+ while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
+ boolean value;
+ if (t == JsonToken.VALUE_TRUE) {
+ value = true;
+ } else if (t == JsonToken.VALUE_FALSE) {
+ value = false;
+ } else if (t == JsonToken.VALUE_NULL) {
+ if (_nuller != null) {
+ _nuller.getNullValue(ctxt);
+ continue;
+ }
+ _verifyNullForPrimitive(ctxt);
+ value = false;
+ } else {
+ value = _parseBooleanPrimitive(p, ctxt);
+ }
if (ix >= chunk.length) {
chunk = builder.appendCompletedChunk(chunk, ix);
ix = 0;
@@ -262,6 +428,15 @@
DeserializationContext ctxt) throws IOException {
return new boolean[] { _parseBooleanPrimitive(p, ctxt) };
}
+
+ @Override
+ protected boolean[] _concat(boolean[] oldValue, boolean[] newValue) {
+ int len1 = oldValue.length;
+ int len2 = newValue.length;
+ boolean[] result = Arrays.copyOf(oldValue, len1+len2);
+ System.arraycopy(newValue, 0, result, len1, len2);
+ return result;
+ }
}
/**
@@ -275,13 +450,19 @@
private static final long serialVersionUID = 1L;
public ByteDeser() { super(byte[].class); }
- protected ByteDeser(ByteDeser base, Boolean unwrapSingle) {
- super(base, unwrapSingle);
+ protected ByteDeser(ByteDeser base, NullValueProvider nuller,Boolean unwrapSingle) {
+ super(base, nuller, unwrapSingle);
}
@Override
- protected PrimitiveArrayDeserializers<?> withResolved(Boolean unwrapSingle) {
- return new ByteDeser(this, unwrapSingle);
+ protected PrimitiveArrayDeserializers<?> withResolved(NullValueProvider nuller,
+ Boolean unwrapSingle) {
+ return new ByteDeser(this, nuller, unwrapSingle);
+ }
+
+ @Override
+ protected byte[] _constructEmpty() {
+ return new byte[0];
}
@Override
@@ -291,7 +472,19 @@
// Most likely case: base64 encoded String?
if (t == JsonToken.VALUE_STRING) {
- return p.getBinaryValue(ctxt.getBase64Variant());
+ try {
+ return p.getBinaryValue(ctxt.getBase64Variant());
+ } catch (JsonParseException e) {
+ // 25-Nov-2016, tatu: related to [databind#1425], try to convert
+ // to a more usable one, as it's not really a JSON-level parse
+ // exception, but rather binding from JSON String into base64 decoded
+ // binary data
+ String msg = e.getOriginalMessage();
+ if (msg.contains("base64")) {
+ return (byte[]) ctxt.handleWeirdStringValue(byte[].class,
+ p.getText(), msg);
+ }
+ }
}
// 31-Dec-2009, tatu: Also may be hidden as embedded Object
if (t == JsonToken.VALUE_EMBEDDED_OBJECT) {
@@ -318,10 +511,14 @@
} else {
// should probably accept nulls as 0
if (t == JsonToken.VALUE_NULL) {
+ if (_nuller != null) {
+ _nuller.getNullValue(ctxt);
+ continue;
+ }
+ _verifyNullForPrimitive(ctxt);
value = (byte) 0;
} else {
- Number n = (Number) ctxt.handleUnexpectedToken(_valueClass.getComponentType(), p);
- value = n.byteValue();
+ value = _parseBytePrimitive(p, ctxt);
}
}
if (ix >= chunk.length) {
@@ -348,6 +545,11 @@
} else {
// should probably accept nulls as 'false'
if (t == JsonToken.VALUE_NULL) {
+ if (_nuller != null) {
+ _nuller.getNullValue(ctxt);
+ return (byte[]) getEmptyValue(ctxt);
+ }
+ _verifyNullForPrimitive(ctxt);
return null;
}
Number n = (Number) ctxt.handleUnexpectedToken(_valueClass.getComponentType(), p);
@@ -355,6 +557,15 @@
}
return new byte[] { value };
}
+
+ @Override
+ protected byte[] _concat(byte[] oldValue, byte[] newValue) {
+ int len1 = oldValue.length;
+ int len2 = newValue.length;
+ byte[] result = Arrays.copyOf(oldValue, len1+len2);
+ System.arraycopy(newValue, 0, result, len1, len2);
+ return result;
+ }
}
@JacksonStdImpl
@@ -364,15 +575,21 @@
private static final long serialVersionUID = 1L;
public ShortDeser() { super(short[].class); }
- protected ShortDeser(ShortDeser base, Boolean unwrapSingle) {
- super(base, unwrapSingle);
+ protected ShortDeser(ShortDeser base, NullValueProvider nuller, Boolean unwrapSingle) {
+ super(base, nuller, unwrapSingle);
}
@Override
- protected PrimitiveArrayDeserializers<?> withResolved(Boolean unwrapSingle) {
- return new ShortDeser(this, unwrapSingle);
+ protected PrimitiveArrayDeserializers<?> withResolved(NullValueProvider nuller,
+ Boolean unwrapSingle) {
+ return new ShortDeser(this, nuller, unwrapSingle);
}
-
+
+ @Override
+ protected short[] _constructEmpty() {
+ return new short[0];
+ }
+
@Override
public short[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
@@ -384,8 +601,19 @@
int ix = 0;
try {
- while (p.nextToken() != JsonToken.END_ARRAY) {
- short value = _parseShortPrimitive(p, ctxt);
+ JsonToken t;
+ while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
+ short value;
+ if (t == JsonToken.VALUE_NULL) {
+ if (_nuller != null) {
+ _nuller.getNullValue(ctxt);
+ continue;
+ }
+ _verifyNullForPrimitive(ctxt);
+ value = (short) 0;
+ } else {
+ value = _parseShortPrimitive(p, ctxt);
+ }
if (ix >= chunk.length) {
chunk = builder.appendCompletedChunk(chunk, ix);
ix = 0;
@@ -403,6 +631,15 @@
DeserializationContext ctxt) throws IOException {
return new short[] { _parseShortPrimitive(p, ctxt) };
}
+
+ @Override
+ protected short[] _concat(short[] oldValue, short[] newValue) {
+ int len1 = oldValue.length;
+ int len2 = newValue.length;
+ short[] result = Arrays.copyOf(oldValue, len1+len2);
+ System.arraycopy(newValue, 0, result, len1, len2);
+ return result;
+ }
}
@JacksonStdImpl
@@ -414,13 +651,19 @@
public final static IntDeser instance = new IntDeser();
public IntDeser() { super(int[].class); }
- protected IntDeser(IntDeser base, Boolean unwrapSingle) {
- super(base, unwrapSingle);
+ protected IntDeser(IntDeser base, NullValueProvider nuller, Boolean unwrapSingle) {
+ super(base, nuller, unwrapSingle);
}
@Override
- protected PrimitiveArrayDeserializers<?> withResolved(Boolean unwrapSingle) {
- return new IntDeser(this, unwrapSingle);
+ protected PrimitiveArrayDeserializers<?> withResolved(NullValueProvider nuller,
+ Boolean unwrapSingle) {
+ return new IntDeser(this, nuller, unwrapSingle);
+ }
+
+ @Override
+ protected int[] _constructEmpty() {
+ return new int[0];
}
@Override
@@ -434,9 +677,21 @@
int ix = 0;
try {
- while (p.nextToken() != JsonToken.END_ARRAY) {
- // whether we should allow truncating conversions?
- int value = _parseIntPrimitive(p, ctxt);
+ JsonToken t;
+ while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
+ int value;
+ if (t == JsonToken.VALUE_NUMBER_INT) {
+ value = p.getIntValue();
+ } else if (t == JsonToken.VALUE_NULL) {
+ if (_nuller != null) {
+ _nuller.getNullValue(ctxt);
+ continue;
+ }
+ _verifyNullForPrimitive(ctxt);
+ value = 0;
+ } else {
+ value = _parseIntPrimitive(p, ctxt);
+ }
if (ix >= chunk.length) {
chunk = builder.appendCompletedChunk(chunk, ix);
ix = 0;
@@ -454,6 +709,15 @@
DeserializationContext ctxt) throws IOException {
return new int[] { _parseIntPrimitive(p, ctxt) };
}
+
+ @Override
+ protected int[] _concat(int[] oldValue, int[] newValue) {
+ int len1 = oldValue.length;
+ int len2 = newValue.length;
+ int[] result = Arrays.copyOf(oldValue, len1+len2);
+ System.arraycopy(newValue, 0, result, len1, len2);
+ return result;
+ }
}
@JacksonStdImpl
@@ -465,13 +729,19 @@
public final static LongDeser instance = new LongDeser();
public LongDeser() { super(long[].class); }
- protected LongDeser(LongDeser base, Boolean unwrapSingle) {
- super(base, unwrapSingle);
+ protected LongDeser(LongDeser base, NullValueProvider nuller, Boolean unwrapSingle) {
+ super(base, nuller, unwrapSingle);
}
@Override
- protected PrimitiveArrayDeserializers<?> withResolved(Boolean unwrapSingle) {
- return new LongDeser(this, unwrapSingle);
+ protected PrimitiveArrayDeserializers<?> withResolved(NullValueProvider nuller,
+ Boolean unwrapSingle) {
+ return new LongDeser(this, nuller, unwrapSingle);
+ }
+
+ @Override
+ protected long[] _constructEmpty() {
+ return new long[0];
}
@Override
@@ -485,8 +755,21 @@
int ix = 0;
try {
- while (p.nextToken() != JsonToken.END_ARRAY) {
- long value = _parseLongPrimitive(p, ctxt);
+ JsonToken t;
+ while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
+ long value;
+ if (t == JsonToken.VALUE_NUMBER_INT) {
+ value = p.getLongValue();
+ } else if (t == JsonToken.VALUE_NULL) {
+ if (_nuller != null) {
+ _nuller.getNullValue(ctxt);
+ continue;
+ }
+ _verifyNullForPrimitive(ctxt);
+ value = 0L;
+ } else {
+ value = _parseLongPrimitive(p, ctxt);
+ }
if (ix >= chunk.length) {
chunk = builder.appendCompletedChunk(chunk, ix);
ix = 0;
@@ -504,6 +787,15 @@
DeserializationContext ctxt) throws IOException {
return new long[] { _parseLongPrimitive(p, ctxt) };
}
+
+ @Override
+ protected long[] _concat(long[] oldValue, long[] newValue) {
+ int len1 = oldValue.length;
+ int len2 = newValue.length;
+ long[] result = Arrays.copyOf(oldValue, len1+len2);
+ System.arraycopy(newValue, 0, result, len1, len2);
+ return result;
+ }
}
@JacksonStdImpl
@@ -513,18 +805,23 @@
private static final long serialVersionUID = 1L;
public FloatDeser() { super(float[].class); }
- protected FloatDeser(FloatDeser base, Boolean unwrapSingle) {
- super(base, unwrapSingle);
+ protected FloatDeser(FloatDeser base, NullValueProvider nuller, Boolean unwrapSingle) {
+ super(base, nuller, unwrapSingle);
}
@Override
- protected PrimitiveArrayDeserializers<?> withResolved(Boolean unwrapSingle) {
- return new FloatDeser(this, unwrapSingle);
+ protected PrimitiveArrayDeserializers<?> withResolved(NullValueProvider nuller,
+ Boolean unwrapSingle) {
+ return new FloatDeser(this, nuller, unwrapSingle);
}
@Override
- public float[] deserialize(JsonParser p, DeserializationContext ctxt)
- throws IOException, JsonProcessingException
+ protected float[] _constructEmpty() {
+ return new float[0];
+ }
+
+ @Override
+ public float[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
if (!p.isExpectedStartArrayToken()) {
return handleNonArray(p, ctxt);
@@ -534,8 +831,15 @@
int ix = 0;
try {
- while (p.nextToken() != JsonToken.END_ARRAY) {
+ JsonToken t;
+ while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
// whether we should allow truncating conversions?
+ if (t == JsonToken.VALUE_NULL) {
+ if (_nuller != null) {
+ _nuller.getNullValue(ctxt);
+ continue;
+ }
+ }
float value = _parseFloatPrimitive(p, ctxt);
if (ix >= chunk.length) {
chunk = builder.appendCompletedChunk(chunk, ix);
@@ -554,6 +858,15 @@
DeserializationContext ctxt) throws IOException {
return new float[] { _parseFloatPrimitive(p, ctxt) };
}
+
+ @Override
+ protected float[] _concat(float[] oldValue, float[] newValue) {
+ int len1 = oldValue.length;
+ int len2 = newValue.length;
+ float[] result = Arrays.copyOf(oldValue, len1+len2);
+ System.arraycopy(newValue, 0, result, len1, len2);
+ return result;
+ }
}
@JacksonStdImpl
@@ -563,13 +876,19 @@
private static final long serialVersionUID = 1L;
public DoubleDeser() { super(double[].class); }
- protected DoubleDeser(DoubleDeser base, Boolean unwrapSingle) {
- super(base, unwrapSingle);
+ protected DoubleDeser(DoubleDeser base, NullValueProvider nuller, Boolean unwrapSingle) {
+ super(base, nuller, unwrapSingle);
}
@Override
- protected PrimitiveArrayDeserializers<?> withResolved(Boolean unwrapSingle) {
- return new DoubleDeser(this, unwrapSingle);
+ protected PrimitiveArrayDeserializers<?> withResolved(NullValueProvider nuller,
+ Boolean unwrapSingle) {
+ return new DoubleDeser(this, nuller, unwrapSingle);
+ }
+
+ @Override
+ protected double[] _constructEmpty() {
+ return new double[0];
}
@Override
@@ -583,7 +902,14 @@
int ix = 0;
try {
- while (p.nextToken() != JsonToken.END_ARRAY) {
+ JsonToken t;
+ while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
+ if (t == JsonToken.VALUE_NULL) {
+ if (_nuller != null) {
+ _nuller.getNullValue(ctxt);
+ continue;
+ }
+ }
double value = _parseDoublePrimitive(p, ctxt);
if (ix >= chunk.length) {
chunk = builder.appendCompletedChunk(chunk, ix);
@@ -602,5 +928,14 @@
DeserializationContext ctxt) throws IOException {
return new double[] { _parseDoublePrimitive(p, ctxt) };
}
+
+ @Override
+ protected double[] _concat(double[] oldValue, double[] newValue) {
+ int len1 = oldValue.length;
+ int len2 = newValue.length;
+ double[] result = Arrays.copyOf(oldValue, len1+len2);
+ System.arraycopy(newValue, 0, result, len1, len2);
+ return result;
+ }
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/ReferenceTypeDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/ReferenceTypeDeserializer.java
index 65876df..56bf081 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/ReferenceTypeDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/ReferenceTypeDeserializer.java
@@ -10,6 +10,7 @@
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.type.ReferenceType;
+import com.fasterxml.jackson.databind.util.AccessPattern;
/**
* Base deserializer implementation for properties {@link ReferenceType} values.
@@ -22,17 +23,18 @@
extends StdDeserializer<T>
implements ContextualDeserializer
{
- private static final long serialVersionUID = 1L;
-
+ private static final long serialVersionUID = 2L; // 2.9
+
/**
* Full type of property (or root value) for which this deserializer
* has been constructed and contextualized.
*/
protected final JavaType _fullType;
-
+
+ protected final ValueInstantiator _valueInstantiator;
+
protected final TypeDeserializer _valueTypeDeserializer;
-
- protected final JsonDeserializer<?> _valueDeserializer;
+ protected final JsonDeserializer<Object> _valueDeserializer;
/*
/**********************************************************
@@ -40,23 +42,27 @@
/**********************************************************
*/
- public ReferenceTypeDeserializer(JavaType fullType,
+ @SuppressWarnings("unchecked")
+ public ReferenceTypeDeserializer(JavaType fullType, ValueInstantiator vi,
TypeDeserializer typeDeser, JsonDeserializer<?> deser)
{
super(fullType);
+ _valueInstantiator = vi;
_fullType = fullType;
- _valueDeserializer = deser;
+ _valueDeserializer = (JsonDeserializer<Object>) deser;
_valueTypeDeserializer = typeDeser;
}
- // NOTE: for forwards-compatibility; added in 2.8.5 since 2.9.0 has it
- public ReferenceTypeDeserializer(JavaType fullType, ValueInstantiator inst,
- TypeDeserializer typeDeser, JsonDeserializer<?> deser) {
- this(fullType, typeDeser, deser);
+ @Deprecated // since 2.9
+ public ReferenceTypeDeserializer(JavaType fullType,
+ TypeDeserializer typeDeser, JsonDeserializer<?> deser)
+ {
+ this(fullType, null, typeDeser, deser);
}
@Override
- public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException
+ public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
+ throws JsonMappingException
{
JsonDeserializer<?> deser = _valueDeserializer;
if (deser == null) {
@@ -68,6 +74,7 @@
if (typeDeser != null) {
typeDeser = typeDeser.forProperty(property);
}
+ // !!! 23-Oct-2016, tatu: TODO: full support for configurable ValueInstantiators?
if ((deser == _valueDeserializer) && (typeDeser == _valueTypeDeserializer)) {
return this;
}
@@ -76,16 +83,68 @@
/*
/**********************************************************
- /* Abstract methods for sub-classes to implement
+ /* Partial NullValueProvider impl
/**********************************************************
*/
- protected abstract ReferenceTypeDeserializer<T> withResolved(TypeDeserializer typeDeser, JsonDeserializer<?> valueDeser);
+ /**
+ * Null value varies dynamically (unlike with scalar types),
+ * so let's indicate this.
+ */
+ @Override
+ public AccessPattern getNullAccessPattern() {
+ return AccessPattern.DYNAMIC;
+ }
+
+ @Override
+ public AccessPattern getEmptyAccessPattern() {
+ return AccessPattern.DYNAMIC;
+ }
+
+ /*
+ /**********************************************************
+ /* Abstract methods for sub-classes to implement
+ /**********************************************************
+ */
+
+ /**
+ * Mutant factory method called when changes are needed; should construct
+ * newly configured instance with new values as indicated.
+ *<p>
+ * NOTE: caller has verified that there are changes, so implementations
+ * need NOT check if a new instance is needed.
+ */
+ protected abstract ReferenceTypeDeserializer<T> withResolved(TypeDeserializer typeDeser,
+ JsonDeserializer<?> valueDeser);
@Override
public abstract T getNullValue(DeserializationContext ctxt);
+ @Override
+ public Object getEmptyValue(DeserializationContext ctxt) {
+ return getNullValue(ctxt);
+ }
+
public abstract T referenceValue(Object contents);
+
+ /**
+ * Method called in case of "merging update", in which we should try
+ * update reference instead of creating a new one. If this does not
+ * succeed, should just create a new instance.
+ *
+ * @since 2.9
+ */
+ public abstract T updateReference(T reference, Object contents);
+
+ /**
+ * Method that may be called to find contents of specified reference,
+ * if any; or `null` if none. Note that method should never fail, so
+ * for types that use concept of "absence" vs "presence", `null` is
+ * to be returned for both "absent" and "reference to `null`" cases.
+ *
+ * @since 2.9
+ */
+ public abstract Object getReferenced(T reference);
/*
/**********************************************************
@@ -96,14 +155,32 @@
@Override
public JavaType getValueType() { return _fullType; }
+ /**
+ * By default we assume that updateability mostly relies on value
+ * deserializer; if it supports updates, typically that's what
+ * matters. So let's just delegate.
+ */
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return (_valueDeserializer == null) ? null
+ : _valueDeserializer.supportsUpdate(config);
+ }
+
/*
/**********************************************************
/* Deserialization
/**********************************************************
*/
-
+
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
+ // 23-Oct-2016, tatu: ValueInstantiator only defined for non-vanilla instances,
+ // but do check... might work
+ if (_valueInstantiator != null) {
+ @SuppressWarnings("unchecked")
+ T value = (T) _valueInstantiator.createUsingDefault(ctxt);
+ return deserialize(p, ctxt, value);
+ }
Object contents = (_valueTypeDeserializer == null)
? _valueDeserializer.deserialize(p, ctxt)
: _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
@@ -111,6 +188,33 @@
}
@Override
+ public T deserialize(JsonParser p, DeserializationContext ctxt, T reference) throws IOException
+ {
+ Object contents;
+ // 26-Oct-2016, tatu: first things first; see if we should be able to merge:
+ Boolean B = _valueDeserializer.supportsUpdate(ctxt.getConfig());
+ // if explicitly stated that merge won't work...
+ if (B.equals(Boolean.FALSE) || (_valueTypeDeserializer != null)) {
+ contents = (_valueTypeDeserializer == null)
+ ? _valueDeserializer.deserialize(p, ctxt)
+ : _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
+ } else {
+ // Otherwise, see if we can merge the value
+ contents = getReferenced(reference);
+ // Whether to error or not... for now, just go back to default then
+ if (contents == null) {
+ contents = (_valueTypeDeserializer == null)
+ ? _valueDeserializer.deserialize(p, ctxt)
+ : _valueDeserializer.deserializeWithType(p, ctxt, _valueTypeDeserializer);
+ return referenceValue(contents);
+ } else {
+ contents = _valueDeserializer.deserialize(p, ctxt, contents);
+ }
+ }
+ return updateReference(reference, contents);
+ }
+
+ @Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer) throws IOException
{
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StackTraceElementDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StackTraceElementDeserializer.java
index 61572c6..3c69efa 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StackTraceElementDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StackTraceElementDeserializer.java
@@ -24,6 +24,7 @@
String className = "", methodName = "", fileName = "";
// Java 9 adds couple more things
String moduleName = null, moduleVersion = null;
+ String classLoaderName = null;
int lineNumber = -1;
while ((t = p.nextValue()) != JsonToken.END_OBJECT) {
@@ -31,6 +32,8 @@
// TODO: with Java 8, convert to switch
if ("className".equals(propName)) {
className = p.getText();
+ } else if ("classLoaderName".equals(propName)) {
+ classLoaderName = p.getText();
} else if ("fileName".equals(propName)) {
fileName = p.getText();
} else if ("lineNumber".equals(propName)) {
@@ -53,7 +56,7 @@
}
}
return constructValue(ctxt, className, methodName, fileName, lineNumber,
- moduleName, moduleVersion);
+ moduleName, moduleVersion, classLoaderName);
} else if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
p.nextToken();
final StackTraceElement value = deserialize(p, ctxt);
@@ -65,6 +68,14 @@
return (StackTraceElement) ctxt.handleUnexpectedToken(_valueClass, p);
}
+ @Deprecated // since 2.9
+ protected StackTraceElement constructValue(DeserializationContext ctxt,
+ String className, String methodName, String fileName, int lineNumber,
+ String moduleName, String moduleVersion) {
+ return constructValue(ctxt, className, methodName, fileName, lineNumber,
+ moduleName, moduleVersion, null);
+ }
+
/**
* Overridable factory method used for constructing {@link StackTraceElement}s.
*
@@ -72,7 +83,7 @@
*/
protected StackTraceElement constructValue(DeserializationContext ctxt,
String className, String methodName, String fileName, int lineNumber,
- String moduleName, String moduleVersion)
+ String moduleName, String moduleVersion, String classLoaderName)
{
// 21-May-2016, tatu: With Java 9, need to use different constructor, probably
// via different module, and throw exception here if extra args passed
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDelegatingDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDelegatingDeserializer.java
index 9876124..4ac7850 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDelegatingDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDelegatingDeserializer.java
@@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.deser.ResolvableDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
+import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.Converter;
/**
@@ -37,6 +38,9 @@
{
private static final long serialVersionUID = 1L;
+ /**
+ * Converter that was used for creating {@link #_delegateDeserializer}.
+ */
protected final Converter<Object,T> _converter;
/**
@@ -92,9 +96,7 @@
protected StdDelegatingDeserializer<T> withDelegate(Converter<Object,T> converter,
JavaType delegateType, JsonDeserializer<?> delegateDeserializer)
{
- if (getClass() != StdDelegatingDeserializer.class) {
- throw new IllegalStateException("Sub-class "+getClass().getName()+" must override 'withDelegate'");
- }
+ ClassUtil.verifyMustOverride(StdDelegatingDeserializer.class, this, "withDelegate");
return new StdDelegatingDeserializer<T>(converter, delegateType, delegateDeserializer);
}
@@ -150,6 +152,11 @@
return _delegateDeserializer.handledType();
}
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return _delegateDeserializer.supportsUpdate(config);
+ }
+
/*
/**********************************************************
/* Serialization
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java
index 70d7417..9d7dcf2 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java
@@ -4,12 +4,21 @@
import java.util.*;
import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.NumberInput;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
+import com.fasterxml.jackson.databind.deser.BeanDeserializerBase;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
+import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
+import com.fasterxml.jackson.databind.deser.ValueInstantiator;
+import com.fasterxml.jackson.databind.deser.impl.NullsAsEmptyProvider;
+import com.fasterxml.jackson.databind.deser.impl.NullsConstantProvider;
+import com.fasterxml.jackson.databind.deser.impl.NullsFailProvider;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
+import com.fasterxml.jackson.databind.util.AccessPattern;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.Converter;
@@ -34,6 +43,12 @@
protected final static int F_MASK_INT_COERCIONS =
DeserializationFeature.USE_BIG_INTEGER_FOR_INTS.getMask()
| DeserializationFeature.USE_LONG_FOR_INTS.getMask();
+
+ // @since 2.9
+ protected final static int F_MASK_ACCEPT_ARRAYS =
+ DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS.getMask() |
+ DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT.getMask();
+
/**
* Type of values this deserializer handles: sometimes
@@ -48,7 +63,7 @@
}
protected StdDeserializer(JavaType valueType) {
- _valueClass = (valueType == null) ? null : valueType.getRawClass();
+ _valueClass = valueType.getRawClass();
}
/**
@@ -83,7 +98,7 @@
public final Class<?> getValueClass() { return _valueClass; }
/**
- * Exact structured type deserializer handles, if known.
+ * Exact structured type this deserializer handles, if known.
*<p>
* Default implementation just returns null.
*/
@@ -119,7 +134,7 @@
TypeDeserializer typeDeserializer) throws IOException {
return typeDeserializer.deserializeTypedFromAny(p, ctxt);
}
-
+
/*
/**********************************************************
/* Helper methods for sub-classes, parsing: while mostly
@@ -133,7 +148,10 @@
JsonToken t = p.getCurrentToken();
if (t == JsonToken.VALUE_TRUE) return true;
if (t == JsonToken.VALUE_FALSE) return false;
- if (t == JsonToken.VALUE_NULL) return false;
+ if (t == JsonToken.VALUE_NULL) {
+ _verifyNullForPrimitive(ctxt);
+ return false;
+ }
// should accept ints too, (0 == false, otherwise true)
if (t == JsonToken.VALUE_NUMBER_INT) {
@@ -146,80 +164,28 @@
if ("true".equals(text) || "True".equals(text)) {
return true;
}
- if ("false".equals(text) || "False".equals(text) || text.length() == 0) {
+ if ("false".equals(text) || "False".equals(text)) {
return false;
}
- if (_hasTextualNull(text)) {
+ if (_isEmptyOrTextualNull(text)) {
+ _verifyNullForPrimitiveCoercion(ctxt, text);
return false;
}
Boolean b = (Boolean) ctxt.handleWeirdStringValue(_valueClass, text,
"only \"true\" or \"false\" recognized");
- return (b == null) ? false : b.booleanValue();
+ return Boolean.TRUE.equals(b);
}
// [databind#381]
if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
p.nextToken();
final boolean parsed = _parseBooleanPrimitive(p, ctxt);
- t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
+ _verifyEndArrayForSingle(p, ctxt);
return parsed;
}
// Otherwise, no can do:
return ((Boolean) ctxt.handleUnexpectedToken(_valueClass, p)).booleanValue();
}
- protected final Boolean _parseBoolean(JsonParser p, DeserializationContext ctxt)
- throws IOException
- {
- JsonToken t = p.getCurrentToken();
- if (t == JsonToken.VALUE_TRUE) {
- return Boolean.TRUE;
- }
- if (t == JsonToken.VALUE_FALSE) {
- return Boolean.FALSE;
- }
- // should accept ints too, (0 == false, otherwise true)
- if (t == JsonToken.VALUE_NUMBER_INT) {
- return Boolean.valueOf(_parseBooleanFromInt(p, ctxt));
- }
- if (t == JsonToken.VALUE_NULL) {
- return (Boolean) getNullValue(ctxt);
- }
- // And finally, let's allow Strings to be converted too
- if (t == JsonToken.VALUE_STRING) {
- String text = p.getText().trim();
- // [databind#422]: Allow aliases
- if ("true".equals(text) || "True".equals(text)) {
- return Boolean.TRUE;
- }
- if ("false".equals(text) || "False".equals(text)) {
- return Boolean.FALSE;
- }
- if (text.length() == 0) {
- return (Boolean) getEmptyValue(ctxt);
- }
- if (_hasTextualNull(text)) {
- return (Boolean) getNullValue(ctxt);
- }
- return (Boolean) ctxt.handleWeirdStringValue(_valueClass, text,
- "only \"true\" or \"false\" recognized");
- }
- // [databind#381]
- if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final Boolean parsed = _parseBoolean(p, ctxt);
- t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
- }
- // Otherwise, no can do:
- return (Boolean) ctxt.handleUnexpectedToken(_valueClass, p);
- }
-
protected boolean _parseBooleanFromInt(JsonParser p, DeserializationContext ctxt)
throws IOException
{
@@ -227,123 +193,23 @@
// degenerate case of huge integers, legal in JSON.
// ... this is, on the other hand, probably wrong/sub-optimal for non-JSON
// input. For now, no rea
-
+ _verifyNumberForScalarCoercion(ctxt, p);
// Anyway, note that since we know it's valid (JSON) integer, it can't have
// extra whitespace to trim.
return !"0".equals(p.getText());
}
- @Deprecated // since 2.8.4
- protected boolean _parseBooleanFromOther(JsonParser p, DeserializationContext ctxt)
+ protected final byte _parseBytePrimitive(JsonParser p, DeserializationContext ctxt)
throws IOException
{
- return _parseBooleanFromInt(p, ctxt);
- }
-
- protected Byte _parseByte(JsonParser p, DeserializationContext ctxt)
- throws IOException
- {
- JsonToken t = p.getCurrentToken();
- if (t == JsonToken.VALUE_NUMBER_INT) {
- return p.getByteValue();
+ int value = _parseIntPrimitive(p, ctxt);
+ // So far so good: but does it fit?
+ if (_byteOverflow(value)) {
+ Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, String.valueOf(value),
+ "overflow, value can not be represented as 8-bit value");
+ return _nonNullNumber(v).byteValue();
}
- if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
- String text = p.getText().trim();
- if (_hasTextualNull(text)) {
- return (Byte) getNullValue(ctxt);
- }
- int value;
- try {
- int len = text.length();
- if (len == 0) {
- return (Byte) getEmptyValue(ctxt);
- }
- value = NumberInput.parseInt(text);
- } catch (IllegalArgumentException iae) {
- return (Byte) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid Byte value");
- }
- // So far so good: but does it fit?
- // as per [JACKSON-804], allow range up to 255, inclusive
- if (value < Byte.MIN_VALUE || value > 255) {
- return (Byte) ctxt.handleWeirdStringValue(_valueClass, text,
- "overflow, value can not be represented as 8-bit value");
- // fall-through for deferred fails
- }
- return Byte.valueOf((byte) value);
- }
- if (t == JsonToken.VALUE_NUMBER_FLOAT) {
- if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
- _failDoubleToIntCoercion(p, ctxt, "Byte");
- }
- return p.getByteValue();
- }
- if (t == JsonToken.VALUE_NULL) {
- return (Byte) getNullValue(ctxt);
- }
- // [databind#381]
- if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final Byte parsed = _parseByte(p, ctxt);
- t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
- }
- return (Byte) ctxt.handleUnexpectedToken(_valueClass, p);
- }
-
- protected Short _parseShort(JsonParser p, DeserializationContext ctxt)
- throws IOException
- {
- JsonToken t = p.getCurrentToken();
- if (t == JsonToken.VALUE_NUMBER_INT) {
- return p.getShortValue();
- }
- if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
- String text = p.getText().trim();
- int value;
- try {
- int len = text.length();
- if (len == 0) {
- return (Short) getEmptyValue(ctxt);
- }
- if (_hasTextualNull(text)) {
- return (Short) getNullValue(ctxt);
- }
- value = NumberInput.parseInt(text);
- } catch (IllegalArgumentException iae) {
- return (Short) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid Short value");
- }
- // So far so good: but does it fit?
- if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
- return (Short) ctxt.handleWeirdStringValue(_valueClass, text,
- "overflow, value can not be represented as 16-bit value");
- }
- return Short.valueOf((short) value);
- }
- if (t == JsonToken.VALUE_NUMBER_FLOAT) {
- if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
- _failDoubleToIntCoercion(p, ctxt, "Short");
- }
- return p.getShortValue();
- }
- if (t == JsonToken.VALUE_NULL) {
- return (Short) getNullValue(ctxt);
- }
- // [databind#381]
- if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final Short parsed = _parseShort(p, ctxt);
- t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
- }
- return (Short) ctxt.handleUnexpectedToken(_valueClass, p);
+ return (byte) value;
}
protected final short _parseShortPrimitive(JsonParser p, DeserializationContext ctxt)
@@ -351,10 +217,10 @@
{
int value = _parseIntPrimitive(p, ctxt);
// So far so good: but does it fit?
- if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
+ if (_shortOverflow(value)) {
Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, String.valueOf(value),
"overflow, value can not be represented as 16-bit value");
- return (v == null) ? (short) 0 : v.shortValue();
+ return _nonNullNumber(v).shortValue();
}
return (short) value;
}
@@ -365,189 +231,87 @@
if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) {
return p.getIntValue();
}
- JsonToken t = p.getCurrentToken();
- if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
+ switch (p.getCurrentTokenId()) {
+ case JsonTokenId.ID_STRING:
String text = p.getText().trim();
- if (_hasTextualNull(text)) {
+ if (_isEmptyOrTextualNull(text)) {
+ _verifyNullForPrimitiveCoercion(ctxt, text);
return 0;
}
- try {
- int len = text.length();
- if (len > 9) {
- long l = Long.parseLong(text);
- if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
- Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, text,
- "Overflow: numeric value (%s) out of range of int (%d -%d)",
- text, Integer.MIN_VALUE, Integer.MAX_VALUE);
- return (v == null) ? 0 : v.intValue();
- }
- return (int) l;
- }
- if (len == 0) {
- return 0;
- }
- return NumberInput.parseInt(text);
- } catch (IllegalArgumentException iae) {
- Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid int value");
- return (v == null) ? 0 : v.intValue();
- }
- }
- if (t == JsonToken.VALUE_NUMBER_FLOAT) {
+ return _parseIntPrimitive(ctxt, text);
+ case JsonTokenId.ID_NUMBER_FLOAT:
if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
_failDoubleToIntCoercion(p, ctxt, "int");
}
return p.getValueAsInt();
- }
- if (t == JsonToken.VALUE_NULL) {
+ case JsonTokenId.ID_NULL:
+ _verifyNullForPrimitive(ctxt);
return 0;
- }
- if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final int parsed = _parseIntPrimitive(p, ctxt);
- t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
+ case JsonTokenId.ID_START_ARRAY:
+ if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
+ p.nextToken();
+ final int parsed = _parseIntPrimitive(p, ctxt);
+ _verifyEndArrayForSingle(p, ctxt);
+ return parsed;
+ }
+ break;
+ default:
}
// Otherwise, no can do:
return ((Number) ctxt.handleUnexpectedToken(_valueClass, p)).intValue();
}
- protected final Integer _parseInteger(JsonParser p, DeserializationContext ctxt)
- throws IOException
+ /**
+ * @since 2.9
+ */
+ protected final int _parseIntPrimitive(DeserializationContext ctxt, String text) throws IOException
{
- switch (p.getCurrentTokenId()) {
- // NOTE: caller assumed to usually check VALUE_NUMBER_INT in fast path
- case JsonTokenId.ID_NUMBER_INT:
- return Integer.valueOf(p.getIntValue());
- case JsonTokenId.ID_NUMBER_FLOAT: // coercing may work too
- if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
- _failDoubleToIntCoercion(p, ctxt, "Integer");
- }
- return Integer.valueOf(p.getValueAsInt());
- case JsonTokenId.ID_STRING: // let's do implicit re-parse
- String text = p.getText().trim();
- try {
- int len = text.length();
- if (_hasTextualNull(text)) {
- return (Integer) getNullValue(ctxt);
+ try {
+ if (text.length() > 9) {
+ long l = Long.parseLong(text);
+ if (_intOverflow(l)) {
+ Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, text,
+ "Overflow: numeric value (%s) out of range of int (%d -%d)",
+ text, Integer.MIN_VALUE, Integer.MAX_VALUE);
+ return _nonNullNumber(v).intValue();
}
- if (len > 9) {
- long l = Long.parseLong(text);
- if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
- return (Integer) ctxt.handleWeirdStringValue(_valueClass, text,
- "Overflow: numeric value ("+text+") out of range of Integer ("+Integer.MIN_VALUE+" - "+Integer.MAX_VALUE+")");
- // fall-through
- }
- return Integer.valueOf((int) l);
- }
- if (len == 0) {
- return (Integer) getEmptyValue(ctxt);
- }
- return Integer.valueOf(NumberInput.parseInt(text));
- } catch (IllegalArgumentException iae) {
- return (Integer) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid Integer value");
+ return (int) l;
}
- // fall-through
- case JsonTokenId.ID_NULL:
- return (Integer) getNullValue(ctxt);
- case JsonTokenId.ID_START_ARRAY:
- if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final Integer parsed = _parseInteger(p, ctxt);
- if (p.nextToken() != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
- }
- break;
+ return NumberInput.parseInt(text);
+ } catch (IllegalArgumentException iae) {
+ Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid int value");
+ return _nonNullNumber(v).intValue();
}
- // Otherwise, no can do:
- return (Integer) ctxt.handleUnexpectedToken(_valueClass, p);
}
-
- protected final Long _parseLong(JsonParser p, DeserializationContext ctxt) throws IOException
- {
- switch (p.getCurrentTokenId()) {
- // NOTE: caller assumed to usually check VALUE_NUMBER_INT in fast path
- case JsonTokenId.ID_NUMBER_INT:
- return p.getLongValue();
- case JsonTokenId.ID_NUMBER_FLOAT:
- if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
- _failDoubleToIntCoercion(p, ctxt, "Long");
- }
- return p.getValueAsLong();
- case JsonTokenId.ID_STRING:
- // let's allow Strings to be converted too
- // !!! 05-Jan-2009, tatu: Should we try to limit value space, JDK is too lenient?
- String text = p.getText().trim();
- if (text.length() == 0) {
- return (Long) getEmptyValue(ctxt);
- }
- if (_hasTextualNull(text)) {
- return (Long) getNullValue(ctxt);
- }
- try {
- return Long.valueOf(NumberInput.parseLong(text));
- } catch (IllegalArgumentException iae) { }
- return (Long) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid Long value");
- // fall-through
- case JsonTokenId.ID_NULL:
- return (Long) getNullValue(ctxt);
- case JsonTokenId.ID_START_ARRAY:
- if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final Long parsed = _parseLong(p, ctxt);
- JsonToken t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
- }
- break;
- }
- // Otherwise, no can do:
- return (Long) ctxt.handleUnexpectedToken(_valueClass, p);
- }
-
+
protected final long _parseLongPrimitive(JsonParser p, DeserializationContext ctxt)
throws IOException
{
- switch (p.getCurrentTokenId()) {
- case JsonTokenId.ID_NUMBER_INT:
+ if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) {
return p.getLongValue();
+ }
+ switch (p.getCurrentTokenId()) {
+ case JsonTokenId.ID_STRING:
+ String text = p.getText().trim();
+ if (_isEmptyOrTextualNull(text)) {
+ _verifyNullForPrimitiveCoercion(ctxt, text);
+ return 0L;
+ }
+ return _parseLongPrimitive(ctxt, text);
case JsonTokenId.ID_NUMBER_FLOAT:
if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
_failDoubleToIntCoercion(p, ctxt, "long");
}
return p.getValueAsLong();
- case JsonTokenId.ID_STRING:
- String text = p.getText().trim();
- if (text.length() == 0 || _hasTextualNull(text)) {
- return 0L;
- }
- try {
- return NumberInput.parseLong(text);
- } catch (IllegalArgumentException iae) { }
- {
- Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid long value");
- return (v == null) ? 0 : v.longValue();
- }
case JsonTokenId.ID_NULL:
+ _verifyNullForPrimitive(ctxt);
return 0L;
case JsonTokenId.ID_START_ARRAY:
if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
p.nextToken();
final long parsed = _parseLongPrimitive(p, ctxt);
- JsonToken t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
+ _verifyEndArrayForSingle(p, ctxt);
return parsed;
}
break;
@@ -555,249 +319,182 @@
return ((Number) ctxt.handleUnexpectedToken(_valueClass, p)).longValue();
}
- protected final Float _parseFloat(JsonParser p, DeserializationContext ctxt)
- throws IOException
+ /**
+ * @since 2.9
+ */
+ protected final long _parseLongPrimitive(DeserializationContext ctxt, String text) throws IOException
{
- // We accept couple of different types; obvious ones first:
- JsonToken t = p.getCurrentToken();
-
- if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
- return p.getFloatValue();
+ try {
+ return NumberInput.parseLong(text);
+ } catch (IllegalArgumentException iae) { }
+ {
+ Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid long value");
+ return _nonNullNumber(v).longValue();
}
- // And finally, let's allow Strings to be converted too
- if (t == JsonToken.VALUE_STRING) {
- String text = p.getText().trim();
- if (text.length() == 0) {
- return (Float) getEmptyValue(ctxt);
- }
- if (_hasTextualNull(text)) {
- return (Float) getNullValue(ctxt);
- }
- switch (text.charAt(0)) {
- case 'I':
- if (_isPosInf(text)) {
- return Float.POSITIVE_INFINITY;
- }
- break;
- case 'N':
- if (_isNaN(text)) {
- return Float.NaN;
- }
- break;
- case '-':
- if (_isNegInf(text)) {
- return Float.NEGATIVE_INFINITY;
- }
- break;
- }
- try {
- return Float.parseFloat(text);
- } catch (IllegalArgumentException iae) { }
- return (Float) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid Float value");
- }
- if (t == JsonToken.VALUE_NULL) {
- return (Float) getNullValue(ctxt);
- }
- if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final Float parsed = _parseFloat(p, ctxt);
- t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
- }
- // Otherwise, no can do:
- return (Float) ctxt.handleUnexpectedToken(_valueClass, p);
}
protected final float _parseFloatPrimitive(JsonParser p, DeserializationContext ctxt)
throws IOException
{
- JsonToken t = p.getCurrentToken();
-
- if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
+ if (p.hasToken(JsonToken.VALUE_NUMBER_FLOAT)) {
return p.getFloatValue();
}
- if (t == JsonToken.VALUE_STRING) {
+ switch (p.getCurrentTokenId()) {
+ case JsonTokenId.ID_STRING:
String text = p.getText().trim();
- if (text.length() == 0 || _hasTextualNull(text)) {
+ if (_isEmptyOrTextualNull(text)) {
+ _verifyNullForPrimitiveCoercion(ctxt, text);
return 0.0f;
}
- switch (text.charAt(0)) {
- case 'I':
- if (_isPosInf(text)) {
- return Float.POSITIVE_INFINITY;
- }
- break;
- case 'N':
- if (_isNaN(text)) { return Float.NaN; }
- break;
- case '-':
- if (_isNegInf(text)) {
- return Float.NEGATIVE_INFINITY;
- }
- break;
- }
- try {
- return Float.parseFloat(text);
- } catch (IllegalArgumentException iae) { }
- Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid float value");
- return (v == null) ? 0 : v.floatValue();
- }
- if (t == JsonToken.VALUE_NULL) {
+ return _parseFloatPrimitive(ctxt, text);
+ case JsonTokenId.ID_NUMBER_INT:
+ return p.getFloatValue();
+ case JsonTokenId.ID_NULL:
+ _verifyNullForPrimitive(ctxt);
return 0.0f;
- }
- if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final float parsed = _parseFloatPrimitive(p, ctxt);
- t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
+ case JsonTokenId.ID_START_ARRAY:
+ if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
+ p.nextToken();
+ final float parsed = _parseFloatPrimitive(p, ctxt);
+ _verifyEndArrayForSingle(p, ctxt);
+ return parsed;
+ }
+ break;
}
// Otherwise, no can do:
return ((Number) ctxt.handleUnexpectedToken(_valueClass, p)).floatValue();
}
- protected final Double _parseDouble(JsonParser p, DeserializationContext ctxt)
+ /**
+ * @since 2.9
+ */
+ protected final float _parseFloatPrimitive(DeserializationContext ctxt, String text)
throws IOException
{
- JsonToken t = p.getCurrentToken();
-
- if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
- return p.getDoubleValue();
- }
- if (t == JsonToken.VALUE_STRING) {
- String text = p.getText().trim();
- if (text.length() == 0) {
- return (Double) getEmptyValue(ctxt);
+ switch (text.charAt(0)) {
+ case 'I':
+ if (_isPosInf(text)) {
+ return Float.POSITIVE_INFINITY;
}
- if (_hasTextualNull(text)) {
- return (Double) getNullValue(ctxt);
+ break;
+ case 'N':
+ if (_isNaN(text)) { return Float.NaN; }
+ break;
+ case '-':
+ if (_isNegInf(text)) {
+ return Float.NEGATIVE_INFINITY;
}
- switch (text.charAt(0)) {
- case 'I':
- if (_isPosInf(text)) {
- return Double.POSITIVE_INFINITY;
- }
- break;
- case 'N':
- if (_isNaN(text)) {
- return Double.NaN;
- }
- break;
- case '-':
- if (_isNegInf(text)) {
- return Double.NEGATIVE_INFINITY;
- }
- break;
- }
- try {
- return parseDouble(text);
- } catch (IllegalArgumentException iae) { }
- return (Double) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid Double value");
+ break;
}
- if (t == JsonToken.VALUE_NULL) {
- return (Double) getNullValue(ctxt);
- }
- if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final Double parsed = _parseDouble(p, ctxt);
- t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
- }
- // Otherwise, no can do:
- return (Double) ctxt.handleUnexpectedToken(_valueClass, p);
+ try {
+ return Float.parseFloat(text);
+ } catch (IllegalArgumentException iae) { }
+ Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid float value");
+ return _nonNullNumber(v).floatValue();
}
protected final double _parseDoublePrimitive(JsonParser p, DeserializationContext ctxt)
throws IOException
{
- // We accept couple of different types; obvious ones first:
- JsonToken t = p.getCurrentToken();
-
- if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
+ if (p.hasToken(JsonToken.VALUE_NUMBER_FLOAT)) {
return p.getDoubleValue();
}
- // And finally, let's allow Strings to be converted too
- if (t == JsonToken.VALUE_STRING) {
+ switch (p.getCurrentTokenId()) {
+ case JsonTokenId.ID_STRING:
String text = p.getText().trim();
- if (text.length() == 0 || _hasTextualNull(text)) {
+ if (_isEmptyOrTextualNull(text)) {
+ _verifyNullForPrimitiveCoercion(ctxt, text);
return 0.0;
}
- switch (text.charAt(0)) {
- case 'I':
- if (_isPosInf(text)) {
- return Double.POSITIVE_INFINITY;
- }
- break;
- case 'N':
- if (_isNaN(text)) {
- return Double.NaN;
- }
- break;
- case '-':
- if (_isNegInf(text)) {
- return Double.NEGATIVE_INFINITY;
- }
- break;
- }
- try {
- return parseDouble(text);
- } catch (IllegalArgumentException iae) { }
- Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, text,
- "not a valid double value");
- return (v == null) ? 0 : v.doubleValue();
- }
- if (t == JsonToken.VALUE_NULL) {
+ return _parseDoublePrimitive(ctxt, text);
+ case JsonTokenId.ID_NUMBER_INT:
+ return p.getDoubleValue();
+ case JsonTokenId.ID_NULL:
+ _verifyNullForPrimitive(ctxt);
return 0.0;
- }
- // [databind#381]
- if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final double parsed = _parseDoublePrimitive(p, ctxt);
- t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
+ case JsonTokenId.ID_START_ARRAY:
+ if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
+ p.nextToken();
+ final double parsed = _parseDoublePrimitive(p, ctxt);
+ _verifyEndArrayForSingle(p, ctxt);
+ return parsed;
+ }
+ break;
}
// Otherwise, no can do:
return ((Number) ctxt.handleUnexpectedToken(_valueClass, p)).doubleValue();
}
+ /**
+ * @since 2.9
+ */
+ protected final double _parseDoublePrimitive(DeserializationContext ctxt, String text)
+ throws IOException
+ {
+ switch (text.charAt(0)) {
+ case 'I':
+ if (_isPosInf(text)) {
+ return Double.POSITIVE_INFINITY;
+ }
+ break;
+ case 'N':
+ if (_isNaN(text)) {
+ return Double.NaN;
+ }
+ break;
+ case '-':
+ if (_isNegInf(text)) {
+ return Double.NEGATIVE_INFINITY;
+ }
+ break;
+ }
+ try {
+ return parseDouble(text);
+ } catch (IllegalArgumentException iae) { }
+ Number v = (Number) ctxt.handleWeirdStringValue(_valueClass, text,
+ "not a valid double value");
+ return _nonNullNumber(v).doubleValue();
+ }
+
protected java.util.Date _parseDate(JsonParser p, DeserializationContext ctxt)
throws IOException
{
- JsonToken t = p.getCurrentToken();
- if (t == JsonToken.VALUE_NUMBER_INT) {
- return new java.util.Date(p.getLongValue());
- }
- if (t == JsonToken.VALUE_NULL) {
- return (java.util.Date) getNullValue(ctxt);
- }
- if (t == JsonToken.VALUE_STRING) {
+ switch (p.getCurrentTokenId()) {
+ case JsonTokenId.ID_STRING:
return _parseDate(p.getText().trim(), ctxt);
+ case JsonTokenId.ID_NUMBER_INT:
+ return new java.util.Date(p.getLongValue());
+ case JsonTokenId.ID_NULL:
+ return (java.util.Date) getNullValue(ctxt);
+ case JsonTokenId.ID_START_ARRAY:
+ return _parseDateFromArray(p, ctxt);
}
- // [databind#381]
- if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- p.nextToken();
- final Date parsed = _parseDate(p, ctxt);
+ return (java.util.Date) ctxt.handleUnexpectedToken(_valueClass, p);
+ }
+
+ // @since 2.9
+ protected java.util.Date _parseDateFromArray(JsonParser p, DeserializationContext ctxt)
+ throws IOException
+ {
+ JsonToken t;
+ if (ctxt.hasSomeOfFeatures(F_MASK_ACCEPT_ARRAYS)) {
t = p.nextToken();
- if (t != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
+ if (t == JsonToken.END_ARRAY) {
+ if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)) {
+ return (java.util.Date) getNullValue(ctxt);
+ }
+ }
+ if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
+ final Date parsed = _parseDate(p, ctxt);
+ _verifyEndArrayForSingle(p, ctxt);
+ return parsed;
+ }
+ } else {
+ t = p.getCurrentToken();
}
- return (java.util.Date) ctxt.handleUnexpectedToken(_valueClass, p);
+ return (java.util.Date) ctxt.handleUnexpectedToken(_valueClass, t, p, null);
}
/**
@@ -808,10 +505,7 @@
{
try {
// Take empty Strings to mean 'empty' Value, usually 'null':
- if (value.length() == 0) {
- return (Date) getEmptyValue(ctxt);
- }
- if (_hasTextualNull(value)) {
+ if (_isEmptyOrTextualNull(value)) {
return (java.util.Date) getNullValue(ctxt);
}
return ctxt.parseDate(value);
@@ -846,15 +540,17 @@
if (t == JsonToken.VALUE_STRING) {
return p.getText();
}
+ // 07-Nov-2016, tatu: Caller should take care of unwrapping and there shouldn't
+ // be need for extra pass here...
+ /*
// [databind#381]
if ((t == JsonToken.START_ARRAY) && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
p.nextToken();
final String parsed = _parseString(p, ctxt);
- if (p.nextToken() != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
+ _verifyEndArrayForSingle(p, ctxt);
return parsed;
}
+ */
String value = p.getValueAsString();
if (value != null) {
return value;
@@ -903,6 +599,13 @@
return "null".equals(value);
}
+ /**
+ * @since 2.9
+ */
+ protected boolean _isEmptyOrTextualNull(String value) {
+ return value.isEmpty() || "null".equals(value);
+ }
+
protected final boolean _isNegInf(String text) {
return "-Infinity".equals(text) || "-INF".equals(text);
}
@@ -914,11 +617,91 @@
protected final boolean _isNaN(String text) { return "NaN".equals(text); }
/*
+ /**********************************************************
+ /* Helper methods for sub-classes regarding decoding from
+ /* alternate representations
+ /**********************************************************
+ */
+
+ /**
+ * Helper method that allows easy support for array-related {@link DeserializationFeature}s
+ * `ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT` and `UNWRAP_SINGLE_VALUE_ARRAYS`: checks for either
+ * empty array, or single-value array-wrapped value (respectively), and either reports
+ * an exception (if no match, or feature(s) not enabled), or returns appropriate
+ * result value.
+ *<p>
+ * This method should NOT be called if Array representation is explicitly supported
+ * for type: it should only be called in case it is otherwise unrecognized.
+ *<p>
+ * NOTE: in case of unwrapped single element, will handle actual decoding
+ * by calling {@link #_deserializeWrappedValue}, which by default calls
+ * {@link #deserialize(JsonParser, DeserializationContext)}.
+ *
+ * @since 2.9
+ */
+ protected T _deserializeFromArray(JsonParser p, DeserializationContext ctxt) throws IOException
+ {
+ JsonToken t;
+ if (ctxt.hasSomeOfFeatures(F_MASK_ACCEPT_ARRAYS)) {
+ t = p.nextToken();
+ if (t == JsonToken.END_ARRAY) {
+ if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)) {
+ return getNullValue(ctxt);
+ }
+ }
+ if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
+ final T parsed = deserialize(p, ctxt);
+ if (p.nextToken() != JsonToken.END_ARRAY) {
+ handleMissingEndArrayForSingle(p, ctxt);
+ }
+ return parsed;
+ }
+ } else {
+ t = p.getCurrentToken();
+ }
+ @SuppressWarnings("unchecked")
+ T result = (T) ctxt.handleUnexpectedToken(_valueClass, t, p, null);
+ return result;
+ }
+
+ /**
+ * Helper called to support {@link DeserializationFeature#UNWRAP_SINGLE_VALUE_ARRAYS}:
+ * default implementation simply calls
+ * {@link #deserialize(JsonParser, DeserializationContext)},
+ * but handling may be overridden.
+ *
+ * @since 2.9
+ */
+ protected T _deserializeWrappedValue(JsonParser p, DeserializationContext ctxt) throws IOException
+ {
+ // 23-Mar-2017, tatu: Let's specifically block recursive resolution to avoid
+ // either supporting nested arrays, or to cause infinite looping.
+ if (p.hasToken(JsonToken.START_ARRAY)) {
+ String msg = String.format(
+"Can not deserialize instance of %s out of %s token: nested Arrays not allowed with %s",
+ ClassUtil.nameOf(_valueClass), JsonToken.START_ARRAY,
+ "DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS");
+ @SuppressWarnings("unchecked")
+ T result = (T) ctxt.handleUnexpectedToken(_valueClass, p.getCurrentToken(), p, msg);
+ return result;
+ }
+ return (T) deserialize(p, ctxt);
+ }
+
+ /*
/****************************************************
/* Helper methods for sub-classes, coercions
/****************************************************
*/
+ protected void _failDoubleToIntCoercion(JsonParser p, DeserializationContext ctxt,
+ String type) throws IOException
+ {
+ ctxt.reportInputMismatch(handledType(),
+ "Can not coerce a floating-point value ('%s') into %s (enable `DeserializationFeature.ACCEPT_FLOAT_AS_INT` to allow)",
+ p.getValueAsString(), type);
+ }
+
/**
* Helper method called in case where an integral number is encountered, but
* config settings suggest that a coercion may be needed to "upgrade"
@@ -941,7 +724,165 @@
}
return p.getBigIntegerValue(); // should be optimal, whatever it is
}
+
+ /**
+ * Method to call when JSON `null` token is encountered. Note: only called when
+ * this deserializer encounters it but NOT when reached via property
+ *
+ * @since 2.9
+ */
+ protected Object _coerceNullToken(DeserializationContext ctxt, boolean isPrimitive) throws JsonMappingException
+ {
+ if (isPrimitive) {
+ _verifyNullForPrimitive(ctxt);
+ }
+ return getNullValue(ctxt);
+ }
+
+ /**
+ * Method called when JSON String with value "null" is encountered.
+ *
+ * @since 2.9
+ */
+ protected Object _coerceTextualNull(DeserializationContext ctxt, boolean isPrimitive) throws JsonMappingException
+ {
+ Enum<?> feat;
+ boolean enable;
+
+ if (!ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)) {
+ feat = MapperFeature.ALLOW_COERCION_OF_SCALARS;
+ enable = true;
+ } else if (isPrimitive && ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
+ feat = DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES;
+ enable = false;
+ } else {
+ return getNullValue(ctxt);
+ }
+ _reportFailedNullCoerce(ctxt, enable, feat, "String \"null\"");
+ return null;
+ }
+
+ /**
+ * Method called when JSON String with value "" (that is, zero length) is encountered.
+ *
+ * @since 2.9
+ */
+ protected Object _coerceEmptyString(DeserializationContext ctxt, boolean isPrimitive) throws JsonMappingException
+ {
+ Enum<?> feat;
+ boolean enable;
+
+ if (!ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)) {
+ feat = MapperFeature.ALLOW_COERCION_OF_SCALARS;
+ enable = true;
+ } else if (isPrimitive && ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
+ feat = DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES;
+ enable = false;
+ } else {
+ return getNullValue(ctxt);
+ }
+ _reportFailedNullCoerce(ctxt, enable, feat, "empty String (\"\")");
+ return null;
+ }
+
+ // @since 2.9
+ protected final void _verifyNullForPrimitive(DeserializationContext ctxt) throws JsonMappingException
+ {
+ if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
+ ctxt.reportInputMismatch(this,
+ "Can not coerce `null` %s (disable `DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES` to allow)",
+ _coercedTypeDesc());
+ }
+ }
+
+ // NOTE: only for primitive Scalars
+ // @since 2.9
+ protected final void _verifyNullForPrimitiveCoercion(DeserializationContext ctxt, String str) throws JsonMappingException
+ {
+ Enum<?> feat;
+ boolean enable;
+
+ if (!ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)) {
+ feat = MapperFeature.ALLOW_COERCION_OF_SCALARS;
+ enable = true;
+ } else if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
+ feat = DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES;
+ enable = false;
+ } else {
+ return;
+ }
+ String strDesc = str.isEmpty() ? "empty String (\"\")" : String.format("String \"%s\"", str);
+ _reportFailedNullCoerce(ctxt, enable, feat, strDesc);
+ }
+
+ // NOTE: for non-primitive Scalars
+ // @since 2.9
+ protected final void _verifyNullForScalarCoercion(DeserializationContext ctxt, String str) throws JsonMappingException
+ {
+ if (!ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)) {
+ String strDesc = str.isEmpty() ? "empty String (\"\")" : String.format("String \"%s\"", str);
+ _reportFailedNullCoerce(ctxt, true, MapperFeature.ALLOW_COERCION_OF_SCALARS, strDesc);
+ }
+ }
+
+ // @since 2.9
+ protected void _verifyStringForScalarCoercion(DeserializationContext ctxt, String str) throws JsonMappingException
+ {
+ MapperFeature feat = MapperFeature.ALLOW_COERCION_OF_SCALARS;
+ if (!ctxt.isEnabled(feat)) {
+ ctxt.reportInputMismatch(this, "Can not coerce String \"%s\" %s (enable `%s.%s` to allow)",
+ str, _coercedTypeDesc(), feat.getClass().getSimpleName(), feat.name());
+ }
+ }
+
+ // @since 2.9
+ protected void _verifyNumberForScalarCoercion(DeserializationContext ctxt, JsonParser p) throws IOException
+ {
+ MapperFeature feat = MapperFeature.ALLOW_COERCION_OF_SCALARS;
+ if (!ctxt.isEnabled(feat)) {
+ // 31-Mar-2017, tatu: Since we don't know (or this deep, care) about exact type,
+ // access as a String: may require re-encoding by parser which should be fine
+ String valueDesc = p.getText();
+ ctxt.reportInputMismatch(this, "Can not coerce Number (%s) %s (enable `%s.%s` to allow)",
+ valueDesc, _coercedTypeDesc(), feat.getClass().getSimpleName(), feat.name());
+ }
+ }
+ protected void _reportFailedNullCoerce(DeserializationContext ctxt, boolean state, Enum<?> feature,
+ String inputDesc) throws JsonMappingException
+ {
+ String enableDesc = state ? "enable" : "disable";
+ ctxt.reportInputMismatch(this, "Can not coerce %s to Null value %s (%s `%s.%s` to allow)",
+ inputDesc, _coercedTypeDesc(), enableDesc, feature.getClass().getSimpleName(), feature.name());
+ }
+
+ /**
+ * Helper method called to get a description of type into which a scalar value coercion
+ * is (most likely) being applied, to be used for constructing exception messages
+ * on coerce failure.
+ *
+ * @since 2.9
+ */
+ protected String _coercedTypeDesc() {
+ boolean structured;
+ String typeDesc;
+
+ JavaType t = getValueType();
+ if (t != null) {
+ structured = (t.isContainerType() || t.isReferenceType());
+ typeDesc = t.toString();
+ } else {
+ Class<?> cls = handledType();
+ structured = cls.isArray() || Collection.class.isAssignableFrom(cls)
+ || Map.class.isAssignableFrom(cls);
+ typeDesc = ClassUtil.nameOf(cls);
+ }
+ if (structured) {
+ return String.format("as content of type `%s`", typeDesc);
+ }
+ return String.format("for type `%s`", typeDesc);
+ }
+
/*
/****************************************************
/* Helper methods for sub-classes, resolving dependencies
@@ -988,10 +929,10 @@
/*
/**********************************************************
- /* Helper methods for sub-classes, deserializer construction
+ /* Helper methods for: deserializer construction
/**********************************************************
*/
-
+
/**
* Helper method that can be used to see if specified property has annotation
* indicating that a converter is to be used for contained values (contents
@@ -1007,7 +948,7 @@
throws JsonMappingException
{
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
- if (intr != null && prop != null) {
+ if (_neitherNull(intr, prop)) {
AnnotatedMember member = prop.getMember();
if (member != null) {
Object convDef = intr.findDeserializationContentConverter(member);
@@ -1024,6 +965,12 @@
return existingDeserializer;
}
+ /*
+ /**********************************************************
+ /* Helper methods for: accessing contextual config settings
+ /**********************************************************
+ */
+
/**
* Helper method that may be used to find if this deserializer has specific
* {@link JsonFormat} settings, either via property, or through type-specific
@@ -1063,6 +1010,103 @@
return null;
}
+ /**
+ * Method called to find {@link NullValueProvider} for a primary property, using
+ * "value nulls" setting. If no provider found (not defined, or is "skip"),
+ * will return `null`.
+ *
+ * @since 2.9
+ */
+ protected final NullValueProvider findValueNullProvider(DeserializationContext ctxt,
+ SettableBeanProperty prop, PropertyMetadata propMetadata)
+ throws JsonMappingException
+ {
+ if (prop != null) {
+ return _findNullProvider(ctxt, prop, propMetadata.getValueNulls(),
+ prop.getValueDeserializer());
+ }
+ return null;
+ }
+
+ /**
+ * Method called to find {@link NullValueProvider} for a contents of a structured
+ * primary property (Collection, Map, array), using
+ * "content nulls" setting. If no provider found (not defined),
+ * will return given value deserializer (which is a null value provider itself).
+ *
+ * @since 2.9
+ */
+ protected NullValueProvider findContentNullProvider(DeserializationContext ctxt,
+ BeanProperty prop, JsonDeserializer<?> valueDeser)
+ throws JsonMappingException
+ {
+ final Nulls nulls = findContentNullStyle(ctxt, prop);
+ if (nulls == Nulls.SKIP) {
+ return NullsConstantProvider.skipper();
+ }
+ NullValueProvider prov = _findNullProvider(ctxt, prop, nulls, valueDeser);
+ if (prov != null) {
+ return prov;
+ }
+ return valueDeser;
+ }
+
+ protected Nulls findContentNullStyle(DeserializationContext ctxt, BeanProperty prop)
+ throws JsonMappingException
+ {
+ if (prop != null) {
+ return prop.getMetadata().getContentNulls();
+ }
+ return null;
+ }
+
+ // @since 2.9
+ protected final NullValueProvider _findNullProvider(DeserializationContext ctxt,
+ BeanProperty prop, Nulls nulls, JsonDeserializer<?> valueDeser)
+ throws JsonMappingException
+ {
+ if (nulls == Nulls.FAIL) {
+ if (prop == null) {
+ return NullsFailProvider.constructForRootValue(ctxt.constructType(valueDeser.handledType()));
+ }
+ return NullsFailProvider.constructForProperty(prop);
+ }
+ if (nulls == Nulls.AS_EMPTY) {
+ // can not deal with empty values if there is no value deserializer that
+ // can indicate what "empty value" is:
+ if (valueDeser == null) {
+ return null;
+ }
+
+ // Let's first do some sanity checking...
+ // NOTE: although we could use `ValueInstantiator.Gettable` in general,
+ // let's not since that would prevent being able to use custom impls:
+ if (valueDeser instanceof BeanDeserializerBase) {
+ ValueInstantiator vi = ((BeanDeserializerBase) valueDeser).getValueInstantiator();
+ if (!vi.canCreateUsingDefault()) {
+ final JavaType type = prop.getType();
+ ctxt.reportBadDefinition(type,
+ String.format("Can not create empty instance of %s, no default Creator", type));
+ }
+ }
+ // Second: can with pre-fetch value?
+ {
+ AccessPattern access = valueDeser.getEmptyAccessPattern();
+ if (access == AccessPattern.ALWAYS_NULL) {
+ return NullsConstantProvider.nuller();
+ }
+ if (access == AccessPattern.CONSTANT) {
+ return NullsConstantProvider.forValue(valueDeser.getEmptyValue(ctxt));
+ }
+ }
+ return new NullsAsEmptyProvider(valueDeser);
+ }
+ if (nulls == Nulls.SKIP) {
+ return NullsConstantProvider.skipper();
+ }
+ return null;
+ }
+
/*
/**********************************************************
/* Helper methods for sub-classes, problem reporting
@@ -1083,7 +1127,8 @@
* If null, will assume type is what {@link #getValueClass} returns.
* @param propName Name of the property that can not be mapped
*/
- protected void handleUnknownProperty(JsonParser p, DeserializationContext ctxt, Object instanceOrClass, String propName)
+ protected void handleUnknownProperty(JsonParser p, DeserializationContext ctxt,
+ Object instanceOrClass, String propName)
throws IOException
{
if (instanceOrClass == null) {
@@ -1102,17 +1147,64 @@
protected void handleMissingEndArrayForSingle(JsonParser p, DeserializationContext ctxt)
throws IOException
{
- ctxt.reportWrongTokenException(p, JsonToken.END_ARRAY,
-"Attempted to unwrap single value array for single '%s' value but there was more than a single value in the array",
+ ctxt.reportWrongTokenException(this, JsonToken.END_ARRAY,
+"Attempted to unwrap '%s' value from an array (with `DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS`) but it contains more than one value",
handledType().getName());
// 05-May-2016, tatu: Should recover somehow (maybe skip until END_ARRAY);
// but for now just fall through
}
- protected void _failDoubleToIntCoercion(JsonParser p, DeserializationContext ctxt,
- String type) throws IOException
+ protected void _verifyEndArrayForSingle(JsonParser p, DeserializationContext ctxt) throws IOException
{
- ctxt.reportMappingException("Can not coerce a floating-point value ('%s') into %s; enable `DeserializationFeature.ACCEPT_FLOAT_AS_INT` to allow",
- p.getValueAsString(), type);
+ JsonToken t = p.nextToken();
+ if (t != JsonToken.END_ARRAY) {
+ handleMissingEndArrayForSingle(p, ctxt);
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Helper methods, other
+ /**********************************************************
+ */
+
+ /**
+ * @since 2.9
+ */
+ protected final static boolean _neitherNull(Object a, Object b) {
+ return (a != null) && (b != null);
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected final boolean _byteOverflow(int value) {
+ // 07-nov-2016, tatu: We support "unsigned byte" as well
+ // as Java signed range since that's relatively common usage
+ return (value < Byte.MIN_VALUE || value > 255);
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected final boolean _shortOverflow(int value) {
+ return (value < Short.MIN_VALUE || value > Short.MAX_VALUE);
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected final boolean _intOverflow(long value) {
+ return (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE);
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected Number _nonNullNumber(Number n) {
+ if (n == null) {
+ n = Integer.valueOf(0);
+ }
+ return n;
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdKeyDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdKeyDeserializer.java
index de6b23b..64ee48f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdKeyDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdKeyDeserializer.java
@@ -47,6 +47,7 @@
public final static int TYPE_URL = 14;
public final static int TYPE_CLASS = 15;
public final static int TYPE_CURRENCY = 16;
+ public final static int TYPE_BYTE_ARRAY = 17; // since 2.9
final protected int _kind;
final protected Class<?> _keyClass;
@@ -108,6 +109,8 @@
} else if (raw == Currency.class) {
FromStringDeserializer<?> deser = FromStringDeserializer.findDeserializer(Currency.class);
return new StdKeyDeserializer(TYPE_CURRENCY, raw, deser);
+ } else if (raw == byte[].class) {
+ kind = TYPE_BYTE_ARRAY;
} else {
return null;
}
@@ -186,37 +189,36 @@
case TYPE_LOCALE:
try {
return _deser._deserialize(key, ctxt);
- } catch (IOException e) {
- return ctxt.handleWeirdKey(_keyClass, key, "unable to parse key as locale");
+ } catch (IllegalArgumentException e) {
+ return _weirdKey(ctxt, key, e);
}
case TYPE_CURRENCY:
try {
return _deser._deserialize(key, ctxt);
- } catch (IOException e) {
- return ctxt.handleWeirdKey(_keyClass, key, "unable to parse key as currency");
+ } catch (IllegalArgumentException e) {
+ return _weirdKey(ctxt, key, e);
}
case TYPE_DATE:
return ctxt.parseDate(key);
case TYPE_CALENDAR:
- java.util.Date date = ctxt.parseDate(key);
- return (date == null) ? null : ctxt.constructCalendar(date);
+ return ctxt.constructCalendar(ctxt.parseDate(key));
case TYPE_UUID:
try {
return UUID.fromString(key);
} catch (Exception e) {
- return ctxt.handleWeirdKey(_keyClass, key, "problem: %s", e.getMessage());
+ return _weirdKey(ctxt, key, e);
}
case TYPE_URI:
try {
return URI.create(key);
} catch (Exception e) {
- return ctxt.handleWeirdKey(_keyClass, key, "problem: %s", e.getMessage());
+ return _weirdKey(ctxt, key, e);
}
case TYPE_URL:
try {
return new URL(key);
} catch (MalformedURLException e) {
- return ctxt.handleWeirdKey(_keyClass, key, "problem: %s", e.getMessage());
+ return _weirdKey(ctxt, key, e);
}
case TYPE_CLASS:
try {
@@ -224,6 +226,12 @@
} catch (Exception e) {
return ctxt.handleWeirdKey(_keyClass, key, "unable to parse key as Class");
}
+ case TYPE_BYTE_ARRAY:
+ try {
+ return ctxt.getConfig().getBase64Variant().decode(key);
+ } catch (IllegalArgumentException e) {
+ return _weirdKey(ctxt, key, e);
+ }
default:
throw new IllegalStateException("Internal error: unknown key type "+_keyClass);
}
@@ -247,6 +255,11 @@
return NumberInput.parseDouble(key);
}
+ // @since 2.9
+ protected Object _weirdKey(DeserializationContext ctxt, String key, Exception e) throws IOException {
+ return ctxt.handleWeirdKey(_keyClass, key, "problem: %s", e.getMessage());
+ }
+
/*
/**********************************************************
/* First: the standard "String as String" deserializer
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdScalarDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdScalarDeserializer.java
index f75d101..8a91bff 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdScalarDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdScalarDeserializer.java
@@ -3,10 +3,9 @@
import java.io.IOException;
import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
+import com.fasterxml.jackson.databind.util.AccessPattern;
/**
* Base class for deserializers that handle types that are serialized
@@ -14,11 +13,6 @@
*/
public abstract class StdScalarDeserializer<T> extends StdDeserializer<T>
{
- // @since 2.8.8
- protected final static int FEATURES_ACCEPT_ARRAYS =
- DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS.getMask() |
- DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT.getMask();
-
private static final long serialVersionUID = 1L;
protected StdScalarDeserializer(Class<?> vc) { super(vc); }
@@ -32,28 +26,37 @@
return typeDeserializer.deserializeTypedFromScalar(p, ctxt);
}
- protected T _deserializeFromArray(JsonParser p, DeserializationContext ctxt) throws IOException
- {
- JsonToken t;
- if (ctxt.hasSomeOfFeatures(FEATURES_ACCEPT_ARRAYS)) {
- t = p.nextToken();
- if (t == JsonToken.END_ARRAY) {
- if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)) {
- return getNullValue(ctxt);
- }
- }
- if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- final T parsed = deserialize(p, ctxt);
- if (p.nextToken() != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
- }
- } else {
- t = p.getCurrentToken();
- }
- @SuppressWarnings("unchecked")
- T result = (T) ctxt.handleUnexpectedToken(_valueClass, t, p, null);
- return result;
+ /**
+ * Overridden to simply call <code>deserialize()</code> method that does not take value
+ * to update, since scalar values are usually non-mergeable.
+ */
+ @Override // since 2.9
+ public T deserialize(JsonParser p, DeserializationContext ctxt, T intoValue) throws IOException {
+ // 25-Oct-2016, tatu: And if attempt is made, see if we are to complain...
+ ctxt.reportBadMerge(this);
+ // except that it is possible to suppress this; and if so...
+ return deserialize(p, ctxt);
+ }
+
+ /**
+ * By default assumption is that scalar types can not be updated: many are immutable
+ * values (such as primitives and wrappers)
+ */
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return Boolean.FALSE;
+ }
+
+ // Typically Scalar values have default setting of "nulls as nulls"
+ @Override
+ public AccessPattern getNullAccessPattern() {
+ return AccessPattern.ALWAYS_NULL;
+ }
+
+ // While some scalar types have non-null empty values (hence can't say "ALWAYS_NULL")
+ // they are mostly immutable, shareable and so constant.
+ @Override // since 2.9
+ public AccessPattern getEmptyAccessPattern() {
+ return AccessPattern.CONSTANT;
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java
index 94b5831..1495a99 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java
@@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Default {@link ValueInstantiator} implementation, which supports
@@ -79,7 +80,7 @@
*/
@Deprecated
public StdValueInstantiator(DeserializationConfig config, Class<?> valueType) {
- _valueTypeDesc = (valueType == null) ? "UNKNOWN TYPE" : valueType.getName();
+ _valueTypeDesc = ClassUtil.nameOf(valueType);
_valueClass = (valueType == null) ? Object.class : valueType;
}
@@ -258,9 +259,8 @@
}
try {
return _defaultCreator.call();
- } catch (Throwable t) {
- return ctxt.handleInstantiationProblem(_defaultCreator.getDeclaringClass(),
- null, rewrapCtorProblem(ctxt, t));
+ } catch (Exception e) { // 19-Apr-2017, tatu: Let's not catch Errors, just Exceptions
+ return ctxt.handleInstantiationProblem(_valueClass, null, rewrapCtorProblem(ctxt, e));
}
}
@@ -272,9 +272,8 @@
}
try {
return _withArgsCreator.call(args);
- } catch (Throwable t) {
- return ctxt.handleInstantiationProblem(_withArgsCreator.getDeclaringClass(),
- args, rewrapCtorProblem(ctxt, t));
+ } catch (Exception e) { // 19-Apr-2017, tatu: Let's not catch Errors, just Exceptions
+ return ctxt.handleInstantiationProblem(_valueClass, args, rewrapCtorProblem(ctxt, e));
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StringArrayDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StringArrayDeserializer.java
index 38f319f..a348a40 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StringArrayDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StringArrayDeserializer.java
@@ -7,7 +7,10 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
+import com.fasterxml.jackson.databind.deser.impl.NullsConstantProvider;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
+import com.fasterxml.jackson.databind.util.AccessPattern;
import com.fasterxml.jackson.databind.util.ObjectBuffer;
/**
@@ -17,11 +20,16 @@
*/
@JacksonStdImpl
public final class StringArrayDeserializer
+// 28-Oct-2016, tatu: Should do this:
+// extends ContainerDeserializerBase<String[]>
+// but for now won't:
extends StdDeserializer<String[]>
implements ContextualDeserializer
{
private static final long serialVersionUID = 2L;
+ private final static String[] NO_STRINGS = new String[0];
+
public final static StringArrayDeserializer instance = new StringArrayDeserializer();
/**
@@ -30,6 +38,13 @@
protected JsonDeserializer<String> _elementDeserializer;
/**
+ * Handler we need for dealing with nulls.
+ *
+ * @since 2.9
+ */
+ protected final NullValueProvider _nullProvider;
+
+ /**
* Specific override for this instance (from proper, or global per-type overrides)
* to indicate whether single value may be taken to mean an unwrapped one-element array
* or not. If null, left to global defaults.
@@ -38,15 +53,42 @@
*/
protected final Boolean _unwrapSingle;
+ /**
+ * Marker flag set if the <code>_nullProvider</code> indicates that all null
+ * content values should be skipped (instead of being possibly converted).
+ *
+ * @since 2.9
+ */
+ protected final boolean _skipNullValues;
+
public StringArrayDeserializer() {
- this(null, null);
+ this(null, null, null);
}
@SuppressWarnings("unchecked")
- protected StringArrayDeserializer(JsonDeserializer<?> deser, Boolean unwrapSingle) {
+ protected StringArrayDeserializer(JsonDeserializer<?> deser,
+ NullValueProvider nuller, Boolean unwrapSingle) {
super(String[].class);
_elementDeserializer = (JsonDeserializer<String>) deser;
+ _nullProvider = nuller;
_unwrapSingle = unwrapSingle;
+ _skipNullValues = NullsConstantProvider.isSkipper(nuller);
+ }
+
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ return Boolean.TRUE;
+ }
+
+ @Override // since 2.9
+ public AccessPattern getEmptyAccessPattern() {
+ // immutable, shareable so:
+ return AccessPattern.CONSTANT;
+ }
+
+ @Override // since 2.9
+ public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
+ return NO_STRINGS;
}
/**
@@ -54,7 +96,8 @@
* of String values, or if we have to use separate value deserializer.
*/
@Override
- public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException
+ public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
+ throws JsonMappingException
{
JsonDeserializer<?> deser = _elementDeserializer;
// May have a content converter
@@ -68,14 +111,17 @@
// One more thing: allow unwrapping?
Boolean unwrapSingle = findFormatFeature(ctxt, property, String[].class,
JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
+ NullValueProvider nuller = findContentNullProvider(ctxt, property, deser);
// Ok ok: if all we got is the default String deserializer, can just forget about it
if ((deser != null) && isDefaultDeserializer(deser)) {
deser = null;
}
- if ((_elementDeserializer == deser) && (_unwrapSingle == unwrapSingle)) {
+ if ((_elementDeserializer == deser)
+ && (_unwrapSingle == unwrapSingle)
+ && (_nullProvider == nuller)) {
return this;
}
- return new StringArrayDeserializer(deser, unwrapSingle);
+ return new StringArrayDeserializer(deser, nuller, unwrapSingle);
}
@Override
@@ -86,7 +132,7 @@
return handleNonArray(p, ctxt);
}
if (_elementDeserializer != null) {
- return _deserializeCustom(p, ctxt);
+ return _deserializeCustom(p, ctxt, null);
}
final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
@@ -102,7 +148,12 @@
if (t == JsonToken.END_ARRAY) {
break;
}
- if (t != JsonToken.VALUE_NULL) {
+ if (t == JsonToken.VALUE_NULL) {
+ if (_skipNullValues) {
+ continue;
+ }
+ value = (String) _nullProvider.getNullValue(ctxt);
+ } else {
value = _parseString(p, ctxt);
}
}
@@ -123,13 +174,22 @@
/**
* Offlined version used when we do not use the default deserialization method.
*/
- protected final String[] _deserializeCustom(JsonParser p, DeserializationContext ctxt) throws IOException
+ protected final String[] _deserializeCustom(JsonParser p, DeserializationContext ctxt,
+ String[] old) throws IOException
{
final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
- Object[] chunk = buffer.resetAndStart();
- final JsonDeserializer<String> deser = _elementDeserializer;
+ int ix;
+ Object[] chunk;
+
+ if (old == null) {
+ ix = 0;
+ chunk = buffer.resetAndStart();
+ } else {
+ ix = old.length;
+ chunk = buffer.resetAndStart(old, ix);
+ }
- int ix = 0;
+ final JsonDeserializer<String> deser = _elementDeserializer;
try {
while (true) {
@@ -145,7 +205,14 @@
break;
}
// Ok: no need to convert Strings, but must recognize nulls
- value = (t == JsonToken.VALUE_NULL) ? deser.getNullValue(ctxt) : deser.deserialize(p, ctxt);
+ if (t == JsonToken.VALUE_NULL) {
+ if (_skipNullValues) {
+ continue;
+ }
+ value = (String) _nullProvider.getNullValue(ctxt);
+ } else {
+ value = deser.deserialize(p, ctxt);
+ }
} else {
value = deser.deserialize(p, ctxt);
}
@@ -163,12 +230,68 @@
ctxt.returnObjectBuffer(buffer);
return result;
}
-
+
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
return typeDeserializer.deserializeTypedFromArray(p, ctxt);
}
+ @Override
+ public String[] deserialize(JsonParser p, DeserializationContext ctxt,
+ String[] intoValue) throws IOException
+ {
+ // Ok: must point to START_ARRAY (or equivalent)
+ if (!p.isExpectedStartArrayToken()) {
+ String[] arr = handleNonArray(p, ctxt);
+ if (arr == null) {
+ return intoValue;
+ }
+ final int offset = intoValue.length;
+ String[] result = new String[offset + arr.length];
+ System.arraycopy(intoValue, 0, result, 0, offset);
+ System.arraycopy(arr, 0, result, offset, arr.length);
+ return result;
+ }
+
+ if (_elementDeserializer != null) {
+ return _deserializeCustom(p, ctxt, intoValue);
+ }
+ final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
+ int ix = intoValue.length;
+ Object[] chunk = buffer.resetAndStart(intoValue, ix);
+
+ try {
+ while (true) {
+ String value = p.nextTextValue();
+ if (value == null) {
+ JsonToken t = p.getCurrentToken();
+ if (t == JsonToken.END_ARRAY) {
+ break;
+ }
+ if (t == JsonToken.VALUE_NULL) {
+ // 03-Feb-2017, tatu: Should we skip null here or not?
+ if (_skipNullValues) {
+ return NO_STRINGS;
+ }
+ value = (String) _nullProvider.getNullValue(ctxt);
+ } else {
+ value = _parseString(p, ctxt);
+ }
+ }
+ if (ix >= chunk.length) {
+ chunk = buffer.appendCompletedChunk(chunk);
+ ix = 0;
+ }
+ chunk[ix++] = value;
+ }
+ } catch (Exception e) {
+ throw JsonMappingException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
+ }
+ String[] result = buffer.completeAndClearBuffer(chunk, ix, String.class);
+ ctxt.returnObjectBuffer(buffer);
+ return result;
+ }
+
private final String[] handleNonArray(JsonParser p, DeserializationContext ctxt) throws IOException
{
// implicit arrays from single values?
@@ -176,8 +299,12 @@
((_unwrapSingle == null) &&
ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY));
if (canWrap) {
- return new String[] { p.hasToken(JsonToken.VALUE_NULL) ? null : _parseString(p, ctxt) };
- } else if (p.hasToken(JsonToken.VALUE_STRING)
+ String value = p.hasToken(JsonToken.VALUE_NULL)
+ ? (String) _nullProvider.getNullValue(ctxt)
+ : _parseString(p, ctxt);
+ return new String[] { value };
+ }
+ if (p.hasToken(JsonToken.VALUE_STRING)
&& ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
String str = p.getText();
if (str.length() == 0) {
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StringCollectionDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StringCollectionDeserializer.java
index 0ef9714..321df6f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StringCollectionDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StringCollectionDeserializer.java
@@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
+import com.fasterxml.jackson.databind.deser.NullValueProvider;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
@@ -26,8 +27,6 @@
// // Configuration
- protected final JavaType _collectionType;
-
/**
* Value deserializer to use, if NOT the standard one
* (if it is, will be null).
@@ -47,15 +46,6 @@
*/
protected final JsonDeserializer<Object> _delegateDeserializer;
- /**
- * Specific override for this instance (from proper, or global per-type overrides)
- * to indicate whether single value may be taken to mean an unwrapped one-element array
- * or not. If null, left to global defaults.
- *
- * @since 2.7
- */
- protected final Boolean _unwrapSingle;
-
// NOTE: no PropertyBasedCreator, as JSON Arrays have no properties
/*
@@ -67,36 +57,37 @@
public StringCollectionDeserializer(JavaType collectionType,
JsonDeserializer<?> valueDeser, ValueInstantiator valueInstantiator)
{
- this(collectionType, valueInstantiator, null, valueDeser, null);
+ this(collectionType, valueInstantiator, null, valueDeser, valueDeser, null);
}
@SuppressWarnings("unchecked")
protected StringCollectionDeserializer(JavaType collectionType,
ValueInstantiator valueInstantiator, JsonDeserializer<?> delegateDeser,
- JsonDeserializer<?> valueDeser, Boolean unwrapSingle)
+ JsonDeserializer<?> valueDeser,
+ NullValueProvider nuller, Boolean unwrapSingle)
{
- super(collectionType);
- _collectionType = collectionType;
+ super(collectionType, nuller, unwrapSingle);
_valueDeserializer = (JsonDeserializer<String>) valueDeser;
_valueInstantiator = valueInstantiator;
_delegateDeserializer = (JsonDeserializer<Object>) delegateDeser;
- _unwrapSingle = unwrapSingle;
}
protected StringCollectionDeserializer withResolved(JsonDeserializer<?> delegateDeser,
- JsonDeserializer<?> valueDeser, Boolean unwrapSingle)
+ JsonDeserializer<?> valueDeser,
+ NullValueProvider nuller, Boolean unwrapSingle)
{
- if ((_unwrapSingle == unwrapSingle)
+ if ((_unwrapSingle == unwrapSingle) && (_nullProvider == nuller)
&& (_valueDeserializer == valueDeser) && (_delegateDeserializer == delegateDeser)) {
return this;
}
- return new StringCollectionDeserializer(_collectionType,
- _valueInstantiator, delegateDeser, valueDeser, unwrapSingle);
+ return new StringCollectionDeserializer(_containerType, _valueInstantiator,
+ delegateDeser, valueDeser, nuller, unwrapSingle);
}
@Override // since 2.5
public boolean isCachable() {
- // 26-Mar-2015, tatu: Important: prevent caching if custom deserializers are involved
+ // 26-Mar-2015, tatu: Important: prevent caching if custom deserializers via annotations
+ // are involved
return (_valueDeserializer == null) && (_delegateDeserializer == null);
}
@@ -119,7 +110,7 @@
}
}
JsonDeserializer<?> valueDeser = _valueDeserializer;
- final JavaType valueType = _collectionType.getContentType();
+ final JavaType valueType = _containerType.getContentType();
if (valueDeser == null) {
// [databind#125]: May have a content converter
valueDeser = findConvertingContentDeserializer(ctxt, property, valueDeser);
@@ -134,10 +125,11 @@
// comes down to "List vs Collection" I suppose... for now, pass Collection
Boolean unwrapSingle = findFormatFeature(ctxt, property, Collection.class,
JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
+ NullValueProvider nuller = findContentNullProvider(ctxt, property, valueDeser);
if (isDefaultDeserializer(valueDeser)) {
valueDeser = null;
}
- return withResolved(delegate, valueDeser, unwrapSingle);
+ return withResolved(delegate, valueDeser, nuller, unwrapSingle);
}
/*
@@ -146,18 +138,18 @@
/**********************************************************
*/
- @Override
- public JavaType getContentType() {
- return _collectionType.getContentType();
- }
-
@SuppressWarnings("unchecked")
@Override
public JsonDeserializer<Object> getContentDeserializer() {
JsonDeserializer<?> deser = _valueDeserializer;
return (JsonDeserializer<Object>) deser;
}
-
+
+ @Override
+ public ValueInstantiator getValueInstantiator() {
+ return _valueInstantiator;
+ }
+
/*
/**********************************************************
/* JsonDeserializer API
@@ -202,7 +194,12 @@
if (t == JsonToken.END_ARRAY) {
break;
}
- if (t != JsonToken.VALUE_NULL) {
+ if (t == JsonToken.VALUE_NULL) {
+ if (_skipNullValues) {
+ continue;
+ }
+ value = (String) _nullProvider.getNullValue(ctxt);
+ } else {
value = _parseString(p, ctxt);
}
result.add(value);
@@ -229,7 +226,14 @@
break;
}
// Ok: no need to convert Strings, but must recognize nulls
- value = (t == JsonToken.VALUE_NULL) ? deser.getNullValue(ctxt) : deser.deserialize(p, ctxt);
+ if (t == JsonToken.VALUE_NULL) {
+ if (_skipNullValues) {
+ continue;
+ }
+ value = (String) _nullProvider.getNullValue(ctxt);
+ } else {
+ value = deser.deserialize(p, ctxt);
+ }
} else {
value = deser.deserialize(p, ctxt);
}
@@ -239,7 +243,8 @@
}
@Override
- public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
+ public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
+ TypeDeserializer typeDeserializer) throws IOException {
// In future could check current token... for now this should be enough:
return typeDeserializer.deserializeTypedFromArray(p, ctxt);
}
@@ -250,14 +255,15 @@
* array, depending on configuration.
*/
@SuppressWarnings("unchecked")
- private final Collection<String> handleNonArray(JsonParser p, DeserializationContext ctxt, Collection<String> result) throws IOException
+ private final Collection<String> handleNonArray(JsonParser p, DeserializationContext ctxt,
+ Collection<String> result) throws IOException
{
// implicit arrays from single values?
boolean canWrap = (_unwrapSingle == Boolean.TRUE) ||
((_unwrapSingle == null) &&
ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY));
if (!canWrap) {
- return (Collection<String>) ctxt.handleUnexpectedToken(_collectionType.getRawClass(), p);
+ return (Collection<String>) ctxt.handleUnexpectedToken(_containerType.getRawClass(), p);
}
// Strings are one of "native" (intrinsic) types, so there's never type deserializer involved
JsonDeserializer<String> valueDes = _valueDeserializer;
@@ -266,7 +272,11 @@
String value;
if (t == JsonToken.VALUE_NULL) {
- value = (valueDes == null) ? null : valueDes.getNullValue(ctxt);
+ // 03-Feb-2017, tatu: Does this work?
+ if (_skipNullValues) {
+ return result;
+ }
+ value = (String) _nullProvider.getNullValue(ctxt);
} else {
value = (valueDes == null) ? _parseString(p, ctxt) : valueDes.deserialize(p, ctxt);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StringDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StringDeserializer.java
index 5082b74..42fed4e 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StringDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StringDeserializer.java
@@ -3,32 +3,31 @@
import java.io.IOException;
import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
@JacksonStdImpl
-public final class StringDeserializer extends StdScalarDeserializer<String>
+public class StringDeserializer extends StdScalarDeserializer<String> // non-final since 2.9
{
private static final long serialVersionUID = 1L;
- // @since 2.8.8
- protected final static int FEATURES_ACCEPT_ARRAYS =
- DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS.getMask() |
- DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT.getMask();
-
/**
* @since 2.2
*/
public final static StringDeserializer instance = new StringDeserializer();
-
+
public StringDeserializer() { super(String.class); }
// since 2.6, slightly faster lookups for this very common type
@Override
public boolean isCachable() { return true; }
+ @Override // since 2.9
+ public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
+ return "";
+ }
+
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
@@ -63,31 +62,8 @@
// Since we can never have type info ("natural type"; String, Boolean, Integer, Double):
// (is it an error to even call this version?)
@Override
- public String deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
+ public String deserializeWithType(JsonParser p, DeserializationContext ctxt,
+ TypeDeserializer typeDeserializer) throws IOException {
return deserialize(p, ctxt);
}
-
- // @since 2.8.8
- protected String _deserializeFromArray(JsonParser p, DeserializationContext ctxt) throws IOException
- {
- JsonToken t;
- if (ctxt.hasSomeOfFeatures(FEATURES_ACCEPT_ARRAYS)) {
- t = p.nextToken();
- if (t == JsonToken.END_ARRAY) {
- if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)) {
- return getNullValue(ctxt);
- }
- }
- if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
- final String parsed = _parseString(p, ctxt);
- if (p.nextToken() != JsonToken.END_ARRAY) {
- handleMissingEndArrayForSingle(p, ctxt);
- }
- return parsed;
- }
- } else {
- t = p.getCurrentToken();
- }
- return (String) ctxt.handleUnexpectedToken(_valueClass, t, p, null);
- }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/ThrowableDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/ThrowableDeserializer.java
index 0906ad5..c164c1c 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/ThrowableDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/ThrowableDeserializer.java
@@ -69,14 +69,14 @@
_delegateDeserializer.deserialize(p, ctxt));
}
if (_beanType.isAbstract()) { // for good measure, check this too
- return ctxt.handleMissingInstantiator(handledType(), p,
+ return ctxt.handleMissingInstantiator(handledType(), getValueInstantiator(), p,
"abstract type (need to add/enable type information?)");
}
boolean hasStringCreator = _valueInstantiator.canCreateFromString();
boolean hasDefaultCtor = _valueInstantiator.canCreateUsingDefault();
// and finally, verify we do have single-String arg constructor (if no @JsonCreator)
if (!hasStringCreator && !hasDefaultCtor) {
- return ctxt.handleMissingInstantiator(handledType(), p,
+ return ctxt.handleMissingInstantiator(handledType(), getValueInstantiator(), p,
"Throwable needs a default contructor, a single-String-arg constructor; or explicit @JsonCreator");
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/UntypedObjectDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/UntypedObjectDeserializer.java
index 7f4e220..a4aa05c 100644
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/UntypedObjectDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/UntypedObjectDeserializer.java
@@ -5,6 +5,7 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.BeanProperty;
+import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
@@ -37,12 +38,6 @@
protected final static Object[] NO_OBJECTS = new Object[0];
- /**
- * @deprecated Since 2.3, construct a new instance, needs to be resolved
- */
- @Deprecated
- public final static UntypedObjectDeserializer instance = new UntypedObjectDeserializer(null, null);
-
/*
/**********************************************************
/* Possible custom deserializer overrides we need to use
@@ -184,13 +179,6 @@
return this;
}
- protected JsonDeserializer<?> _withResolved(JsonDeserializer<?> mapDeser,
- JsonDeserializer<?> listDeser,
- JsonDeserializer<?> stringDeser, JsonDeserializer<?> numberDeser) {
- return new UntypedObjectDeserializer(this,
- mapDeser, listDeser, stringDeser, numberDeser);
- }
-
/*
/**********************************************************
/* Deserializer API
@@ -210,6 +198,12 @@
return true;
}
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ // 21-Apr-2017, tatu: Bit tricky... some values, yes. So let's say "dunno"
+ return null;
+ }
+
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
@@ -267,7 +261,7 @@
case JsonTokenId.ID_FALSE:
return Boolean.FALSE;
- case JsonTokenId.ID_NULL: // should not get this but...
+ case JsonTokenId.ID_NULL: // 08-Nov-2016, tatu: yes, occurs
return null;
// case JsonTokenId.ID_END_ARRAY: // invalid
@@ -284,9 +278,7 @@
case JsonTokenId.ID_START_ARRAY:
case JsonTokenId.ID_START_OBJECT:
case JsonTokenId.ID_FIELD_NAME:
- /* Output can be as JSON Object, Array or scalar: no way to know
- * a this point:
- */
+ // Output can be as JSON Object, Array or scalar: no way to know at this point:
return typeDeserializer.deserializeTypedFromAny(p, ctxt);
case JsonTokenId.ID_EMBEDDED_OBJECT:
@@ -332,6 +324,74 @@
return ctxt.handleUnexpectedToken(Object.class, p);
}
+ @SuppressWarnings("unchecked")
+ @Override // since 2.9 (to support deep merge)
+ public Object deserialize(JsonParser p, DeserializationContext ctxt, Object intoValue)
+ throws IOException
+ {
+ switch (p.getCurrentTokenId()) {
+ case JsonTokenId.ID_START_OBJECT:
+ case JsonTokenId.ID_FIELD_NAME:
+ // 28-Oct-2015, tatu: [databind#989] We may also be given END_OBJECT (similar to FIELD_NAME),
+ // if caller has advanced to the first token of Object, but for empty Object
+ case JsonTokenId.ID_END_OBJECT:
+ if (_mapDeserializer != null) {
+ return _mapDeserializer.deserialize(p, ctxt, intoValue);
+ }
+ if (intoValue instanceof Map<?,?>) {
+ return mapObject(p, ctxt, (Map<Object,Object>) intoValue);
+ }
+ return mapObject(p, ctxt);
+ case JsonTokenId.ID_START_ARRAY:
+ if (_listDeserializer != null) {
+ return _listDeserializer.deserialize(p, ctxt, intoValue);
+ }
+ if (intoValue instanceof Collection<?>) {
+ return mapArray(p, ctxt, (Collection<Object>) intoValue);
+ }
+ if (ctxt.isEnabled(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)) {
+ return mapArrayToArray(p, ctxt);
+ }
+ return mapArray(p, ctxt);
+ case JsonTokenId.ID_EMBEDDED_OBJECT:
+ return p.getEmbeddedObject();
+ case JsonTokenId.ID_STRING:
+ if (_stringDeserializer != null) {
+ return _stringDeserializer.deserialize(p, ctxt, intoValue);
+ }
+ return p.getText();
+
+ case JsonTokenId.ID_NUMBER_INT:
+ if (_numberDeserializer != null) {
+ return _numberDeserializer.deserialize(p, ctxt, intoValue);
+ }
+ if (ctxt.hasSomeOfFeatures(F_MASK_INT_COERCIONS)) {
+ return _coerceIntegral(p, ctxt);
+ }
+ return p.getNumberValue();
+
+ case JsonTokenId.ID_NUMBER_FLOAT:
+ if (_numberDeserializer != null) {
+ return _numberDeserializer.deserialize(p, ctxt, intoValue);
+ }
+ if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
+ return p.getDecimalValue();
+ }
+ return p.getNumberValue();
+ case JsonTokenId.ID_TRUE:
+ return Boolean.TRUE;
+ case JsonTokenId.ID_FALSE:
+ return Boolean.FALSE;
+
+ case JsonTokenId.ID_NULL:
+ // 21-Apr-2017, tatu: May need to consider "skip nulls" at some point but...
+ return null;
+ default:
+ }
+ // easiest to just delegate to "dumb" version for the rest?
+ return deserialize(p, ctxt);
+ }
+
/*
/**********************************************************
/* Internal methods
@@ -381,6 +441,17 @@
return result;
}
+ protected Object mapArray(JsonParser p, DeserializationContext ctxt,
+ Collection<Object> result) throws IOException
+ {
+ // we start by pointing to START_ARRAY. Also, no real merging; array/Collection
+ // just appends always
+ while (p.nextToken() != JsonToken.END_ARRAY) {
+ result.add(deserialize(p, ctxt));
+ }
+ return result;
+ }
+
/**
* Method called to map a JSON Object into a Java value.
*/
@@ -463,6 +534,36 @@
return buffer.completeAndClearBuffer(values, ptr);
}
+ protected Object mapObject(JsonParser p, DeserializationContext ctxt,
+ Map<Object,Object> m) throws IOException
+ {
+ JsonToken t = p.getCurrentToken();
+ if (t == JsonToken.START_OBJECT) {
+ t = p.nextToken();
+ }
+ if (t == JsonToken.END_OBJECT) {
+ return m;
+ }
+ // NOTE: we are guaranteed to point to FIELD_NAME
+ String key = p.getCurrentName();
+ do {
+ p.nextToken();
+ // and possibly recursive merge here
+ Object old = m.get(key);
+ Object newV;
+
+ if (old != null) {
+ newV = deserialize(p, ctxt, old);
+ } else {
+ newV = deserialize(p, ctxt);
+ }
+ if (newV != old) {
+ m.put(key, newV);
+ }
+ } while ((key = p.nextFieldName()) != null);
+ return m;
+ }
+
/*
/**********************************************************
/* Separate "vanilla" implementation for common case of
@@ -480,6 +581,12 @@
public Vanilla() { super(Object.class); }
+ @Override // since 2.9
+ public Boolean supportsUpdate(DeserializationConfig config) {
+ // 21-Apr-2017, tatu: Bit tricky... some values, yes. So let's say "dunno"
+ return null;
+ }
+
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
@@ -529,14 +636,14 @@
case JsonTokenId.ID_FALSE:
return Boolean.FALSE;
- case JsonTokenId.ID_NULL: // should not get this but...
- return null;
-
case JsonTokenId.ID_END_OBJECT:
// 28-Oct-2015, tatu: [databind#989] We may also be given END_OBJECT (similar to FIELD_NAME),
// if caller has advanced to the first token of Object, but for empty Object
return new LinkedHashMap<String,Object>(2);
+ case JsonTokenId.ID_NULL: // 08-Nov-2016, tatu: yes, occurs
+ return null;
+
//case JsonTokenId.ID_END_ARRAY: // invalid
default:
}
@@ -581,6 +688,68 @@
return ctxt.handleUnexpectedToken(Object.class, p);
}
+ @SuppressWarnings("unchecked")
+ @Override // since 2.9 (to support deep merge)
+ public Object deserialize(JsonParser p, DeserializationContext ctxt, Object intoValue)
+ throws IOException
+ {
+ switch (p.getCurrentTokenId()) {
+ case JsonTokenId.ID_END_OBJECT:
+ case JsonTokenId.ID_END_ARRAY:
+ return intoValue;
+ case JsonTokenId.ID_START_OBJECT:
+ {
+ JsonToken t = p.nextToken(); // to get to FIELD_NAME or END_OBJECT
+ if (t == JsonToken.END_OBJECT) {
+ return intoValue;
+ }
+ }
+ case JsonTokenId.ID_FIELD_NAME:
+ if (intoValue instanceof Map<?,?>) {
+ Map<Object,Object> m = (Map<Object,Object>) intoValue;
+ // NOTE: we are guaranteed to point to FIELD_NAME
+ String key = p.getCurrentName();
+ do {
+ p.nextToken();
+ // and possibly recursive merge here
+ Object old = m.get(key);
+ Object newV;
+ if (old != null) {
+ newV = deserialize(p, ctxt, old);
+ } else {
+ newV = deserialize(p, ctxt);
+ }
+ if (newV != old) {
+ m.put(key, newV);
+ }
+ } while ((key = p.nextFieldName()) != null);
+ return intoValue;
+ }
+ break;
+ case JsonTokenId.ID_START_ARRAY:
+ {
+ JsonToken t = p.nextToken(); // to get to FIELD_NAME or END_OBJECT
+ if (t == JsonToken.END_ARRAY) {
+ return intoValue;
+ }
+ }
+
+ if (intoValue instanceof Collection<?>) {
+ Collection<Object> c = (Collection<Object>) intoValue;
+ // NOTE: merge for arrays/Collections means append, can't merge contents
+ do {
+ c.add(deserialize(p, ctxt));
+ } while (p.nextToken() != JsonToken.END_ARRAY);
+ return intoValue;
+ }
+ // 21-Apr-2017, tatu: Should we try to support merging of Object[] values too?
+ // ... maybe future improvement
+ break;
+ }
+ // Easiest handling for the rest, delegate. Only (?) question: how about nulls?
+ return deserialize(p, ctxt);
+ }
+
protected Object mapArray(JsonParser p, DeserializationContext ctxt) throws IOException
{
Object value = deserialize(p, ctxt);
@@ -618,6 +787,24 @@
}
/**
+ * Method called to map a JSON Array into a Java Object array (Object[]).
+ */
+ protected Object[] mapArrayToArray(JsonParser p, DeserializationContext ctxt) throws IOException {
+ ObjectBuffer buffer = ctxt.leaseObjectBuffer();
+ Object[] values = buffer.resetAndStart();
+ int ptr = 0;
+ do {
+ Object value = deserialize(p, ctxt);
+ if (ptr >= values.length) {
+ values = buffer.appendCompletedChunk(values);
+ ptr = 0;
+ }
+ values[ptr++] = value;
+ } while (p.nextToken() != JsonToken.END_ARRAY);
+ return buffer.completeAndClearBuffer(values, ptr);
+ }
+
+ /**
* Method called to map a JSON Object into a Java value.
*/
protected Object mapObject(JsonParser p, DeserializationContext ctxt) throws IOException
@@ -653,23 +840,5 @@
} while ((key = p.nextFieldName()) != null);
return result;
}
-
- /**
- * Method called to map a JSON Array into a Java Object array (Object[]).
- */
- protected Object[] mapArrayToArray(JsonParser p, DeserializationContext ctxt) throws IOException {
- ObjectBuffer buffer = ctxt.leaseObjectBuffer();
- Object[] values = buffer.resetAndStart();
- int ptr = 0;
- do {
- Object value = deserialize(p, ctxt);
- if (ptr >= values.length) {
- values = buffer.appendCompletedChunk(values);
- ptr = 0;
- }
- values[ptr++] = value;
- } while (p.nextToken() != JsonToken.END_ARRAY);
- return buffer.completeAndClearBuffer(values, ptr);
- }
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/UntypedObjectDeserializer.java.orig b/src/main/java/com/fasterxml/jackson/databind/deser/std/UntypedObjectDeserializer.java.orig
deleted file mode 100644
index 38637e6..0000000
--- a/src/main/java/com/fasterxml/jackson/databind/deser/std/UntypedObjectDeserializer.java.orig
+++ /dev/null
@@ -1,676 +0,0 @@
-package com.fasterxml.jackson.databind.deser.std;
-
-import java.io.IOException;
-import java.util.*;
-
-import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.BeanProperty;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.JavaType;
-import com.fasterxml.jackson.databind.JsonDeserializer;
-import com.fasterxml.jackson.databind.JsonMappingException;
-import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
-import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
-import com.fasterxml.jackson.databind.deser.ResolvableDeserializer;
-import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
-import com.fasterxml.jackson.databind.type.TypeFactory;
-import com.fasterxml.jackson.databind.util.ClassUtil;
-import com.fasterxml.jackson.databind.util.ObjectBuffer;
-
-/**
- * Deserializer implementation that is used if it is necessary to bind content of
- * "unknown" type; something declared as basic {@link java.lang.Object}
- * (either explicitly, or due to type erasure).
- * If so, "natural" mapping is used to convert JSON values to their natural
- * Java object matches: JSON arrays to Java {@link java.util.List}s (or, if configured,
- * Object[]), JSON objects to {@link java.util.Map}s, numbers to
- * {@link java.lang.Number}s, booleans to {@link java.lang.Boolean}s and
- * strings to {@link java.lang.String} (and nulls to nulls).
- */
-@JacksonStdImpl
-public class UntypedObjectDeserializer
- extends StdDeserializer<Object>
- implements ResolvableDeserializer, ContextualDeserializer
-{
- private static final long serialVersionUID = 1L;
-
- protected final static Object[] NO_OBJECTS = new Object[0];
-
- /**
- * @deprecated Since 2.3, construct a new instance, needs to be resolved
- */
- @Deprecated
- public final static UntypedObjectDeserializer instance = new UntypedObjectDeserializer(null, null);
-
- /*
- /**********************************************************
- /* Possible custom deserializer overrides we need to use
- /**********************************************************
- */
-
- protected JsonDeserializer<Object> _mapDeserializer;
-
- protected JsonDeserializer<Object> _listDeserializer;
-
- protected JsonDeserializer<Object> _stringDeserializer;
-
- protected JsonDeserializer<Object> _numberDeserializer;
-
- /**
- * If {@link java.util.List} has been mapped to non-default implementation,
- * we'll store type here
- *
- * @since 2.6
- */
- protected JavaType _listType;
-
- /**
- * If {@link java.util.Map} has been mapped to non-default implementation,
- * we'll store type here
- *
- * @since 2.6
- */
- protected JavaType _mapType;
-
- /**
- * @deprecated Since 2.6 use variant takes type arguments
- */
- @Deprecated
- public UntypedObjectDeserializer() {
- this(null, null);
- }
-
- public UntypedObjectDeserializer(JavaType listType, JavaType mapType) {
- super(Object.class);
- _listType = listType;
- _mapType = mapType;
- }
-
- @SuppressWarnings("unchecked")
- public UntypedObjectDeserializer(UntypedObjectDeserializer base,
- JsonDeserializer<?> mapDeser, JsonDeserializer<?> listDeser,
- JsonDeserializer<?> stringDeser, JsonDeserializer<?> numberDeser)
- {
- super(Object.class);
- _mapDeserializer = (JsonDeserializer<Object>) mapDeser;
- _listDeserializer = (JsonDeserializer<Object>) listDeser;
- _stringDeserializer = (JsonDeserializer<Object>) stringDeser;
- _numberDeserializer = (JsonDeserializer<Object>) numberDeser;
- _listType = base._listType;
- _mapType = base._mapType;
- }
-
- /*
- /**********************************************************
- /* Initialization
- /**********************************************************
- */
-
- /**
- * We need to implement this method to properly find things to delegate
- * to: it can not be done earlier since delegated deserializers almost
- * certainly require access to this instance (at least "List" and "Map" ones)
- */
- @SuppressWarnings("unchecked")
- @Override
- public void resolve(DeserializationContext ctxt) throws JsonMappingException
- {
- JavaType obType = ctxt.constructType(Object.class);
- JavaType stringType = ctxt.constructType(String.class);
- TypeFactory tf = ctxt.getTypeFactory();
-
- /* 26-Nov-2014, tatu: This is highly unusual, as in general contextualization
- * should always be called separately, from within "createContextual()".
- * But this is a very singular deserializer since it operates on `Object`
- * (and often for `?` type parameter), and as a result, easily and commonly
- * results in cycles, being value deserializer for various Maps and Collections.
- * Because of this, we must somehow break the cycles. This is done here by
- * forcing pseudo-contextualization with null property.
- */
-
- // So: first find possible custom instances
- if (_listType == null) {
- _listDeserializer = _clearIfStdImpl(_findCustomDeser(ctxt, tf.constructCollectionType(List.class, obType)));
- } else {
- // NOTE: if non-default List type, always consider to be non-standard deser
- _listDeserializer = _findCustomDeser(ctxt, _listType);
- }
- if (_mapType == null) {
- _mapDeserializer = _clearIfStdImpl(_findCustomDeser(ctxt, tf.constructMapType(Map.class, stringType, obType)));
- } else {
- // NOTE: if non-default Map type, always consider to be non-standard deser
- _mapDeserializer = _findCustomDeser(ctxt, _mapType);
- }
- _stringDeserializer = _clearIfStdImpl(_findCustomDeser(ctxt, stringType));
- _numberDeserializer = _clearIfStdImpl(_findCustomDeser(ctxt, tf.constructType(Number.class)));
-
- // and then do bogus contextualization, in case custom ones need to resolve dependencies of
- // their own
- JavaType unknown = TypeFactory.unknownType();
- _mapDeserializer = (JsonDeserializer<Object>) ctxt.handleSecondaryContextualization(_mapDeserializer, null, unknown);
- _listDeserializer = (JsonDeserializer<Object>) ctxt.handleSecondaryContextualization(_listDeserializer, null, unknown);
- _stringDeserializer = (JsonDeserializer<Object>) ctxt.handleSecondaryContextualization(_stringDeserializer, null, unknown);
- _numberDeserializer = (JsonDeserializer<Object>) ctxt.handleSecondaryContextualization(_numberDeserializer, null, unknown);
- }
-
- protected JsonDeserializer<Object> _findCustomDeser(DeserializationContext ctxt, JavaType type)
- throws JsonMappingException
- {
- // Since we are calling from `resolve`, we should NOT try to contextualize yet;
- // contextualization will only occur at a later point
- return ctxt.findNonContextualValueDeserializer(type);
- }
-
- protected JsonDeserializer<Object> _clearIfStdImpl(JsonDeserializer<Object> deser) {
- return ClassUtil.isJacksonStdImpl(deser) ? null : deser;
- }
-
- /**
- * We only use contextualization for optimizing the case where no customization
- * occurred; if so, can slip in a more streamlined version.
- */
- @Override
- public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
- BeanProperty property) throws JsonMappingException
- {
- // 20-Apr-2014, tatu: If nothing custom, let's use "vanilla" instance,
- // simpler and can avoid some of delegation
- if ((_stringDeserializer == null) && (_numberDeserializer == null)
- && (_mapDeserializer == null) && (_listDeserializer == null)
- && getClass() == UntypedObjectDeserializer.class) {
- return Vanilla.std;
- }
- return this;
- }
-
- protected JsonDeserializer<?> _withResolved(JsonDeserializer<?> mapDeser,
- JsonDeserializer<?> listDeser,
- JsonDeserializer<?> stringDeser, JsonDeserializer<?> numberDeser) {
- return new UntypedObjectDeserializer(this,
- mapDeser, listDeser, stringDeser, numberDeser);
- }
-
- /*
- /**********************************************************
- /* Deserializer API
- /**********************************************************
- */
-
- /* 07-Nov-2014, tatu: When investigating [databind#604], realized that it makes
- * sense to also mark this is cachable, since lookup not exactly free, and
- * since it's not uncommon to "read anything"
- */
- @Override
- public boolean isCachable() {
- /* 26-Mar-2015, tatu: With respect to [databind#735], there are concerns over
- * cachability. It seems like we SHOULD be safe here; but just in case there
- * are problems with false sharing, this may need to be revisited.
- */
- return true;
- }
-
- @Override
- public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
- {
- switch (p.getCurrentTokenId()) {
- case JsonTokenId.ID_START_OBJECT:
- case JsonTokenId.ID_FIELD_NAME:
- // 28-Oct-2015, tatu: [databind#989] We may also be given END_OBJECT (similar to FIELD_NAME),
- // if caller has advanced to the first token of Object, but for empty Object
- case JsonTokenId.ID_END_OBJECT:
- if (_mapDeserializer != null) {
- return _mapDeserializer.deserialize(p, ctxt);
- }
- return mapObject(p, ctxt);
- case JsonTokenId.ID_START_ARRAY:
- if (ctxt.isEnabled(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)) {
- return mapArrayToArray(p, ctxt);
- }
- if (_listDeserializer != null) {
- return _listDeserializer.deserialize(p, ctxt);
- }
- return mapArray(p, ctxt);
- case JsonTokenId.ID_EMBEDDED_OBJECT:
- return p.getEmbeddedObject();
- case JsonTokenId.ID_STRING:
- if (_stringDeserializer != null) {
- return _stringDeserializer.deserialize(p, ctxt);
- }
- return p.getText();
-
- case JsonTokenId.ID_NUMBER_INT:
- if (_numberDeserializer != null) {
- return _numberDeserializer.deserialize(p, ctxt);
- }
- /* Caller may want to get all integral values returned as {@link java.math.BigInteger},
- * or {@link java.lang.Long} for consistency
- */
- if (ctxt.hasSomeOfFeatures(F_MASK_INT_COERCIONS)) {
- return _coerceIntegral(p, ctxt);
- }
- return p.getNumberValue(); // should be optimal, whatever it is
-
- case JsonTokenId.ID_NUMBER_FLOAT:
- if (_numberDeserializer != null) {
- return _numberDeserializer.deserialize(p, ctxt);
- }
- /* [JACKSON-72]: need to allow overriding the behavior regarding
- * which type to use
- */
- if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
- return p.getDecimalValue();
- }
- return p.getDoubleValue();
-
- case JsonTokenId.ID_TRUE:
- return Boolean.TRUE;
- case JsonTokenId.ID_FALSE:
- return Boolean.FALSE;
-
- case JsonTokenId.ID_NULL: // should not get this but...
- return null;
-
-// case JsonTokenId.ID_END_ARRAY: // invalid
- default:
- }
- return ctxt.handleUnexpectedToken(Object.class, p);
- }
-
- @Override
- public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException
- {
- switch (p.getCurrentTokenId()) {
- // First: does it look like we had type id wrapping of some kind?
- case JsonTokenId.ID_START_ARRAY:
- case JsonTokenId.ID_START_OBJECT:
- case JsonTokenId.ID_FIELD_NAME:
- /* Output can be as JSON Object, Array or scalar: no way to know
- * a this point:
- */
- return typeDeserializer.deserializeTypedFromAny(p, ctxt);
-
- case JsonTokenId.ID_EMBEDDED_OBJECT:
- return p.getEmbeddedObject();
-
- /* Otherwise we probably got a "native" type (ones that map
- * naturally and thus do not need or use type ids)
- */
- case JsonTokenId.ID_STRING:
- if (_stringDeserializer != null) {
- return _stringDeserializer.deserialize(p, ctxt);
- }
- return p.getText();
-
- case JsonTokenId.ID_NUMBER_INT:
- if (_numberDeserializer != null) {
- return _numberDeserializer.deserialize(p, ctxt);
- }
- // May need coercion to "bigger" types:
- if (ctxt.hasSomeOfFeatures(F_MASK_INT_COERCIONS)) {
- return _coerceIntegral(p, ctxt);
- }
- return p.getNumberValue(); // should be optimal, whatever it is
-
- case JsonTokenId.ID_NUMBER_FLOAT:
- if (_numberDeserializer != null) {
- return _numberDeserializer.deserialize(p, ctxt);
- }
- if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
- return p.getDecimalValue();
- }
- return Double.valueOf(p.getDoubleValue());
-
- case JsonTokenId.ID_TRUE:
- return Boolean.TRUE;
- case JsonTokenId.ID_FALSE:
- return Boolean.FALSE;
-
- case JsonTokenId.ID_NULL: // should not get this far really but...
- return null;
- default:
- }
- return ctxt.handleUnexpectedToken(Object.class, p);
- }
-
- /*
- /**********************************************************
- /* Internal methods
- /**********************************************************
- */
-
- /**
- * Method called to map a JSON Array into a Java value.
- */
- protected Object mapArray(JsonParser p, DeserializationContext ctxt) throws IOException
- {
- // Minor optimization to handle small lists (default size for ArrayList is 10)
- if (p.nextToken() == JsonToken.END_ARRAY) {
- return new ArrayList<Object>(2);
- }
- Object value = deserialize(p, ctxt);
- if (p.nextToken() == JsonToken.END_ARRAY) {
- ArrayList<Object> l = new ArrayList<Object>(2);
- l.add(value);
- return l;
- }
- Object value2 = deserialize(p, ctxt);
- if (p.nextToken() == JsonToken.END_ARRAY) {
- ArrayList<Object> l = new ArrayList<Object>(2);
- l.add(value);
- l.add(value2);
- return l;
- }
- ObjectBuffer buffer = ctxt.leaseObjectBuffer();
- Object[] values = buffer.resetAndStart();
- int ptr = 0;
- values[ptr++] = value;
- values[ptr++] = value2;
- int totalSize = ptr;
- do {
- value = deserialize(p, ctxt);
- ++totalSize;
- if (ptr >= values.length) {
- values = buffer.appendCompletedChunk(values);
- ptr = 0;
- }
- values[ptr++] = value;
- } while (p.nextToken() != JsonToken.END_ARRAY);
- // let's create full array then
- ArrayList<Object> result = new ArrayList<Object>(totalSize);
- buffer.completeAndClearBuffer(values, ptr, result);
- return result;
- }
-
- /**
- * Method called to map a JSON Object into a Java value.
- */
- protected Object mapObject(JsonParser p, DeserializationContext ctxt) throws IOException
- {
- String key1;
-
- JsonToken t = p.getCurrentToken();
-
- if (t == JsonToken.START_OBJECT) {
- key1 = p.nextFieldName();
- } else if (t == JsonToken.FIELD_NAME) {
- key1 = p.getCurrentName();
- } else {
- if (t != JsonToken.END_OBJECT) {
- return ctxt.handleUnexpectedToken(handledType(), p);
- }
- key1 = null;
- }
- if (key1 == null) {
- // empty map might work; but caller may want to modify... so better just give small modifiable
- return new LinkedHashMap<String,Object>(2);
- }
- // minor optimization; let's handle 1 and 2 entry cases separately
- // 24-Mar-2015, tatu: Ideally, could use one of 'nextXxx()' methods, but for
- // that we'd need new method(s) in JsonDeserializer. So not quite yet.
- p.nextToken();
- Object value1 = deserialize(p, ctxt);
-
- String key2 = p.nextFieldName();
- if (key2 == null) { // has to be END_OBJECT, then
- // single entry; but we want modifiable
- LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>(2);
- result.put(key1, value1);
- return result;
- }
- p.nextToken();
- Object value2 = deserialize(p, ctxt);
-
- String key = p.nextFieldName();
-
- if (key == null) {
- LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>(4);
- result.put(key1, value1);
- result.put(key2, value2);
- return result;
- }
- // And then the general case; default map size is 16
- LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
- result.put(key1, value1);
- result.put(key2, value2);
-
- do {
- p.nextToken();
- result.put(key, deserialize(p, ctxt));
- } while ((key = p.nextFieldName()) != null);
- return result;
- }
-
- /**
- * Method called to map a JSON Array into a Java Object array (Object[]).
- */
- protected Object[] mapArrayToArray(JsonParser p, DeserializationContext ctxt) throws IOException
- {
- // Minor optimization to handle small lists (default size for ArrayList is 10)
- if (p.nextToken() == JsonToken.END_ARRAY) {
- return NO_OBJECTS;
- }
- ObjectBuffer buffer = ctxt.leaseObjectBuffer();
- Object[] values = buffer.resetAndStart();
- int ptr = 0;
- do {
- Object value = deserialize(p, ctxt);
- if (ptr >= values.length) {
- values = buffer.appendCompletedChunk(values);
- ptr = 0;
- }
- values[ptr++] = value;
- } while (p.nextToken() != JsonToken.END_ARRAY);
- return buffer.completeAndClearBuffer(values, ptr);
- }
-
- /*
- /**********************************************************
- /* Separate "vanilla" implementation for common case of
- /* no custom deserializer overrides
- /**********************************************************
- */
-
- @JacksonStdImpl
- public static class Vanilla
- extends StdDeserializer<Object>
- {
- private static final long serialVersionUID = 1L;
-
- public final static Vanilla std = new Vanilla();
-
- public Vanilla() { super(Object.class); }
-
- @Override
- public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
- {
- switch (p.getCurrentTokenId()) {
- case JsonTokenId.ID_START_OBJECT:
- {
- JsonToken t = p.nextToken();
- if (t == JsonToken.END_OBJECT) {
- return new LinkedHashMap<String,Object>(2);
- }
- }
- case JsonTokenId.ID_FIELD_NAME:
- return mapObject(p, ctxt);
- case JsonTokenId.ID_START_ARRAY:
- {
- JsonToken t = p.nextToken();
- if (t == JsonToken.END_ARRAY) { // and empty one too
- if (ctxt.isEnabled(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)) {
- return NO_OBJECTS;
- }
- return new ArrayList<Object>(2);
- }
- }
- if (ctxt.isEnabled(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)) {
- return mapArrayToArray(p, ctxt);
- }
- return mapArray(p, ctxt);
- case JsonTokenId.ID_EMBEDDED_OBJECT:
- return p.getEmbeddedObject();
- case JsonTokenId.ID_STRING:
- return p.getText();
-
- case JsonTokenId.ID_NUMBER_INT:
- if (ctxt.hasSomeOfFeatures(F_MASK_INT_COERCIONS)) {
- return _coerceIntegral(p, ctxt);
- }
- return p.getNumberValue(); // should be optimal, whatever it is
-
- case JsonTokenId.ID_NUMBER_FLOAT:
- if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
- return p.getDecimalValue();
- }
- return Double.valueOf(p.getDoubleValue());
-
- case JsonTokenId.ID_TRUE:
- return Boolean.TRUE;
- case JsonTokenId.ID_FALSE:
- return Boolean.FALSE;
-
- case JsonTokenId.ID_NULL: // should not get this but...
- return null;
-
- case JsonTokenId.ID_END_OBJECT:
- // 28-Oct-2015, tatu: [databind#989] We may also be given END_OBJECT (similar to FIELD_NAME),
- // if caller has advanced to the first token of Object, but for empty Object
- return new LinkedHashMap<String,Object>(2);
-
- //case JsonTokenId.ID_END_ARRAY: // invalid
- default:
- }
- return ctxt.handleUnexpectedToken(Object.class, p);
- }
-
- @Override
- public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException
- {
- switch (p.getCurrentTokenId()) {
- case JsonTokenId.ID_START_ARRAY:
- case JsonTokenId.ID_START_OBJECT:
- case JsonTokenId.ID_FIELD_NAME:
- return typeDeserializer.deserializeTypedFromAny(p, ctxt);
-
- case JsonTokenId.ID_STRING:
- return p.getText();
-
- case JsonTokenId.ID_NUMBER_INT:
- if (ctxt.isEnabled(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS)) {
- return p.getBigIntegerValue();
- }
- return p.getNumberValue();
-
- case JsonTokenId.ID_NUMBER_FLOAT:
- if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
- return p.getDecimalValue();
- }
- return Double.valueOf(p.getDoubleValue());
-
- case JsonTokenId.ID_TRUE:
- return Boolean.TRUE;
- case JsonTokenId.ID_FALSE:
- return Boolean.FALSE;
- case JsonTokenId.ID_EMBEDDED_OBJECT:
- return p.getEmbeddedObject();
-
- case JsonTokenId.ID_NULL: // should not get this far really but...
- return null;
- default:
- }
- return ctxt.handleUnexpectedToken(Object.class, p);
- }
-
- protected Object mapArray(JsonParser p, DeserializationContext ctxt) throws IOException
- {
- Object value = deserialize(p, ctxt);
- if (p.nextToken() == JsonToken.END_ARRAY) {
- ArrayList<Object> l = new ArrayList<Object>(2);
- l.add(value);
- return l;
- }
- Object value2 = deserialize(p, ctxt);
- if (p.nextToken() == JsonToken.END_ARRAY) {
- ArrayList<Object> l = new ArrayList<Object>(2);
- l.add(value);
- l.add(value2);
- return l;
- }
- ObjectBuffer buffer = ctxt.leaseObjectBuffer();
- Object[] values = buffer.resetAndStart();
- int ptr = 0;
- values[ptr++] = value;
- values[ptr++] = value2;
- int totalSize = ptr;
- do {
- value = deserialize(p, ctxt);
- ++totalSize;
- if (ptr >= values.length) {
- values = buffer.appendCompletedChunk(values);
- ptr = 0;
- }
- values[ptr++] = value;
- } while (p.nextToken() != JsonToken.END_ARRAY);
- // let's create full array then
- ArrayList<Object> result = new ArrayList<Object>(totalSize);
- buffer.completeAndClearBuffer(values, ptr, result);
- return result;
- }
-
- /**
- * Method called to map a JSON Object into a Java value.
- */
- protected Object mapObject(JsonParser p, DeserializationContext ctxt) throws IOException
- {
- // will point to FIELD_NAME at this point, guaranteed
- String key1 = p.getText();
- p.nextToken();
- Object value1 = deserialize(p, ctxt);
-
- String key2 = p.nextFieldName();
- if (key2 == null) { // single entry; but we want modifiable
- LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>(2);
- result.put(key1, value1);
- return result;
- }
- p.nextToken();
- Object value2 = deserialize(p, ctxt);
-
- String key = p.nextFieldName();
- if (key == null) {
- LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>(4);
- result.put(key1, value1);
- result.put(key2, value2);
- return result;
- }
- // And then the general case; default map size is 16
- LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
- result.put(key1, value1);
- result.put(key2, value2);
- do {
- p.nextToken();
- result.put(key, deserialize(p, ctxt));
- } while ((key = p.nextFieldName()) != null);
- return result;
- }
-
- /**
- * Method called to map a JSON Array into a Java Object array (Object[]).
- */
- protected Object[] mapArrayToArray(JsonParser p, DeserializationContext ctxt) throws IOException {
- ObjectBuffer buffer = ctxt.leaseObjectBuffer();
- Object[] values = buffer.resetAndStart();
- int ptr = 0;
- do {
- Object value = deserialize(p, ctxt);
- if (ptr >= values.length) {
- values = buffer.appendCompletedChunk(values);
- ptr = 0;
- }
- values[ptr++] = value;
- } while (p.nextToken() != JsonToken.END_ARRAY);
- return buffer.completeAndClearBuffer(values, ptr);
- }
- }
-}
diff --git a/src/main/java/com/fasterxml/jackson/databind/exc/IgnoredPropertyException.java b/src/main/java/com/fasterxml/jackson/databind/exc/IgnoredPropertyException.java
index c26d951..01b000e 100644
--- a/src/main/java/com/fasterxml/jackson/databind/exc/IgnoredPropertyException.java
+++ b/src/main/java/com/fasterxml/jackson/databind/exc/IgnoredPropertyException.java
@@ -39,7 +39,6 @@
super(msg, loc, referringClass, propName, propertyIds);
}
-
/**
* Factory method used for constructing instances of this exception type.
*
@@ -54,17 +53,14 @@
Object fromObjectOrClass, String propertyName,
Collection<Object> propertyIds)
{
- if (fromObjectOrClass == null) {
- throw new IllegalArgumentException();
- }
Class<?> ref;
if (fromObjectOrClass instanceof Class<?>) {
ref = (Class<?>) fromObjectOrClass;
- } else {
+ } else { // also acts as null check:
ref = fromObjectOrClass.getClass();
}
- String msg = "Ignored field \""+propertyName+"\" (class "+ref.getName()
- +") encountered; mapper configured not to allow this";
+ String msg = String.format("Ignored field \"%s\" (class %s) encountered; mapper configured not to allow this",
+ propertyName, ref.getName());
IgnoredPropertyException e = new IgnoredPropertyException(p, msg,
p.getCurrentLocation(), ref, propertyName, propertyIds);
// but let's also ensure path includes this last (missing) segment
diff --git a/src/main/java/com/fasterxml/jackson/databind/exc/InvalidDefinitionException.java b/src/main/java/com/fasterxml/jackson/databind/exc/InvalidDefinitionException.java
new file mode 100644
index 0000000..444e6d9
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/exc/InvalidDefinitionException.java
@@ -0,0 +1,104 @@
+package com.fasterxml.jackson.databind.exc;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.BeanDescription;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
+
+/**
+ * Intermediate exception type used as the base class for all {@link JsonMappingException}s
+ * that are due to problems with target type definition; usually a problem with
+ * annotations used on a class or its properties.
+ * This is in contrast to {@link MismatchedInputException} which
+ * signals a problem with input to map.
+ *
+ * @since 2.9
+ */
+@SuppressWarnings("serial")
+public class InvalidDefinitionException
+ extends JsonMappingException
+{
+ protected final JavaType _type;
+
+ protected transient BeanDescription _beanDesc;
+ protected transient BeanPropertyDefinition _property;
+
+ protected InvalidDefinitionException(JsonParser p, String msg,
+ JavaType type) {
+ super(p, msg);
+ _type = type;
+ _beanDesc = null;
+ _property = null;
+ }
+
+ protected InvalidDefinitionException(JsonGenerator g, String msg,
+ JavaType type) {
+ super(g, msg);
+ _type = type;
+ _beanDesc = null;
+ _property = null;
+ }
+
+ protected InvalidDefinitionException(JsonParser p, String msg,
+ BeanDescription bean, BeanPropertyDefinition prop) {
+ super(p, msg);
+ _type = (bean == null) ? null : bean.getType();
+ _beanDesc = bean;
+ _property = prop;
+ }
+
+ protected InvalidDefinitionException(JsonGenerator g, String msg,
+ BeanDescription bean, BeanPropertyDefinition prop) {
+ super(g, msg);
+ _type = (bean == null) ? null : bean.getType();
+ _beanDesc = bean;
+ _property = prop;
+ }
+
+ public static InvalidDefinitionException from(JsonParser p, String msg,
+ BeanDescription bean, BeanPropertyDefinition prop) {
+ return new InvalidDefinitionException(p, msg, bean, prop);
+ }
+
+ public static InvalidDefinitionException from(JsonParser p, String msg,
+ JavaType type) {
+ return new InvalidDefinitionException(p, msg, type);
+ }
+
+ public static InvalidDefinitionException from(JsonGenerator g, String msg,
+ BeanDescription bean, BeanPropertyDefinition prop) {
+ return new InvalidDefinitionException(g, msg, bean, prop);
+ }
+
+ public static InvalidDefinitionException from(JsonGenerator g, String msg,
+ JavaType type) {
+ return new InvalidDefinitionException(g, msg, type);
+ }
+
+ /**
+ * Accessor for type fully resolved type that had the problem; this should always
+ * known and available, never <code>null</code>
+ */
+ public JavaType getType() {
+ return _type;
+ }
+
+ /**
+ * Accessor for type definition (class) that had the definition problem, if any; may sometimes
+ * be undefined or unknown; if so, returns <code>null</code>.
+ */
+ public BeanDescription getBeanDescription() {
+ return _beanDesc;
+ }
+
+ /**
+ * Accessor for property that had the definition problem if any
+ * (none, for example if the problem relates to type in general),
+ * if known. If not known (or relevant), returns <code>null</code>.
+ */
+ public BeanPropertyDefinition getProperty() {
+ return _property;
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/exc/InvalidFormatException.java b/src/main/java/com/fasterxml/jackson/databind/exc/InvalidFormatException.java
index e7593d1..b1b8b10 100644
--- a/src/main/java/com/fasterxml/jackson/databind/exc/InvalidFormatException.java
+++ b/src/main/java/com/fasterxml/jackson/databind/exc/InvalidFormatException.java
@@ -2,16 +2,16 @@
import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.databind.JsonMappingException;
/**
- * Specialized sub-class of {@link JsonMappingException}
+ * Specialized sub-class of {@link MismatchedInputException}
* that is used when the underlying problem appears to be that
* of bad formatting of a value to deserialize.
*
* @since 2.1
*/
-public class InvalidFormatException extends JsonMappingException
+public class InvalidFormatException
+ extends MismatchedInputException // since 2.9
{
private static final long serialVersionUID = 1L; // silly Eclipse, warnings
@@ -21,12 +21,6 @@
*/
protected final Object _value;
- /**
- * Intended target type (type-erased class) that value could not
- * be deserialized into, if known.
- */
- protected final Class<?> _targetType;
-
/*
/**********************************************************
/* Life-cycle
@@ -63,9 +57,8 @@
public InvalidFormatException(JsonParser p,
String msg, Object value, Class<?> targetType)
{
- super(p, msg);
+ super(p, msg, targetType);
_value = value;
- _targetType = targetType;
}
public static InvalidFormatException from(JsonParser p, String msg,
@@ -89,14 +82,4 @@
public Object getValue() {
return _value;
}
-
- /**
- * Accessor for checking target type of value ({@link #getValue} that failed
- * to deserialize.
- * Note that type may not be available, depending on who throws the exception
- * and when.
- */
- public Class<?> getTargetType() {
- return _targetType;
- }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/exc/InvalidNullException.java b/src/main/java/com/fasterxml/jackson/databind/exc/InvalidNullException.java
new file mode 100644
index 0000000..daa0eeb
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/exc/InvalidNullException.java
@@ -0,0 +1,52 @@
+package com.fasterxml.jackson.databind.exc;
+
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.PropertyName;
+import com.fasterxml.jackson.databind.util.ClassUtil;
+
+/**
+ * Exception thrown if a `null` value is being encountered for a property
+ * designed as "fail on null" property (see {@link com.fasterxml.jackson.annotation.JsonSetter}).
+ *
+ * @since 2.9
+ */
+public class InvalidNullException
+ extends MismatchedInputException // since 2.9
+{
+ private static final long serialVersionUID = 1L; // silly Eclipse, warnings
+
+ /**
+ * Name of property, if known, for which null was encountered.
+ */
+ protected final PropertyName _propertyName;
+
+ /*
+ /**********************************************************
+ /* Life-cycle
+ /**********************************************************
+ */
+
+ protected InvalidNullException(DeserializationContext ctxt, String msg,
+ PropertyName pname)
+ {
+ super(ctxt.getParser(), msg);
+ _propertyName = pname;
+ }
+
+ public static InvalidNullException from(DeserializationContext ctxt,
+ PropertyName name, JavaType type)
+ {
+ String msg = String.format("Invalid `null` value encountered for property %s",
+ ClassUtil.quotedOr(name, "<UNKNOWN>"));
+ InvalidNullException exc = new InvalidNullException(ctxt, msg, name);
+ if (type != null) {
+ exc.setTargetType(type);
+ }
+ return exc;
+ }
+
+ public PropertyName getPropertyName() {
+ return _propertyName;
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/exc/InvalidTypeIdException.java b/src/main/java/com/fasterxml/jackson/databind/exc/InvalidTypeIdException.java
index 8b05215..d405a43 100644
--- a/src/main/java/com/fasterxml/jackson/databind/exc/InvalidTypeIdException.java
+++ b/src/main/java/com/fasterxml/jackson/databind/exc/InvalidTypeIdException.java
@@ -2,14 +2,14 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JavaType;
-import com.fasterxml.jackson.databind.JsonMappingException;
/**
* Exception thrown when resolution of a type id fails.
*
* @since 2.8
*/
-public class InvalidTypeIdException extends JsonMappingException
+public class InvalidTypeIdException
+ extends MismatchedInputException // since 2.9
{
private static final long serialVersionUID = 1L; // silly Eclipse, warnings
@@ -19,7 +19,8 @@
protected final JavaType _baseType;
/**
- * Type id that failed to be resolved to a subtype
+ * Type id that failed to be resolved to a subtype; `null` in cases
+ * where no type id was located (since 2.9).
*/
protected final String _typeId;
diff --git a/src/main/java/com/fasterxml/jackson/databind/exc/MismatchedInputException.java b/src/main/java/com/fasterxml/jackson/databind/exc/MismatchedInputException.java
new file mode 100644
index 0000000..0479422
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/exc/MismatchedInputException.java
@@ -0,0 +1,77 @@
+package com.fasterxml.jackson.databind.exc;
+
+import com.fasterxml.jackson.core.JsonLocation;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.JsonMappingException;
+
+/**
+ * General exception type used as the base class for all {@link JsonMappingException}s
+ * that are due to input not mapping to target definition; these are typically
+ * considered "client errors" since target type definition itself is not the root cause
+ * but mismatching input. This is in contrast to {@link InvalidDefinitionException} which
+ * signals a problem with target type definition and not input.
+ *<p>
+ * This type is used as-is for some input problems, but in most cases there should be
+ * more explicit subtypes to use.
+ *<p>
+ * NOTE: name chosen to differ from `java.util.InputMismatchException` since while that
+ * would have been better name, use of same overlapping name causes nasty issues
+ * with IDE auto-completion, so slightly less optimal chosen.
+ *
+ * @since 2.9
+ */
+@SuppressWarnings("serial")
+public class MismatchedInputException
+ extends JsonMappingException
+{
+ /**
+ * Type of value that was to be deserialized
+ */
+ protected Class<?> _targetType;
+
+ protected MismatchedInputException(JsonParser p, String msg) {
+ this(p, msg, (JavaType) null);
+ }
+
+ protected MismatchedInputException(JsonParser p, String msg, JsonLocation loc) {
+ super(p, msg, loc);
+ }
+
+ protected MismatchedInputException(JsonParser p, String msg, Class<?> targetType) {
+ super(p, msg);
+ _targetType = targetType;
+ }
+
+ protected MismatchedInputException(JsonParser p, String msg, JavaType targetType) {
+ super(p, msg);
+ _targetType = (targetType == null) ? null : targetType.getRawClass();
+ }
+
+ // Only to prevent super-class static method from getting called
+ @Deprecated // as of 2.9
+ public static MismatchedInputException from(JsonParser p, String msg) {
+ return from(p, (Class<?>) null, msg);
+ }
+
+ public static MismatchedInputException from(JsonParser p, JavaType targetType, String msg) {
+ return new MismatchedInputException(p, msg, targetType);
+ }
+
+ public static MismatchedInputException from(JsonParser p, Class<?> targetType, String msg) {
+ return new MismatchedInputException(p, msg, targetType);
+ }
+
+ public MismatchedInputException setTargetType(JavaType t) {
+ _targetType = t.getRawClass();
+ return this;
+ }
+
+ /**
+ * Accessor for getting intended target type, with which input did not match,
+ * if known; `null` if not known for some reason.
+ */
+ public Class<?> getTargetType() {
+ return _targetType;
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/exc/PropertyBindingException.java b/src/main/java/com/fasterxml/jackson/databind/exc/PropertyBindingException.java
index 2dcfe8a..7e5c630 100644
--- a/src/main/java/com/fasterxml/jackson/databind/exc/PropertyBindingException.java
+++ b/src/main/java/com/fasterxml/jackson/databind/exc/PropertyBindingException.java
@@ -16,7 +16,7 @@
*/
@SuppressWarnings("serial")
public abstract class PropertyBindingException
- extends JsonMappingException
+ extends MismatchedInputException // since 2.9
{
/**
* Class that does not contain mapping for the unrecognized property.
diff --git a/src/main/java/com/fasterxml/jackson/databind/exc/UnrecognizedPropertyException.java b/src/main/java/com/fasterxml/jackson/databind/exc/UnrecognizedPropertyException.java
index d913079..6f72800 100644
--- a/src/main/java/com/fasterxml/jackson/databind/exc/UnrecognizedPropertyException.java
+++ b/src/main/java/com/fasterxml/jackson/databind/exc/UnrecognizedPropertyException.java
@@ -49,16 +49,14 @@
Object fromObjectOrClass, String propertyName,
Collection<Object> propertyIds)
{
- if (fromObjectOrClass == null) {
- throw new IllegalArgumentException();
- }
Class<?> ref;
if (fromObjectOrClass instanceof Class<?>) {
ref = (Class<?>) fromObjectOrClass;
} else {
ref = fromObjectOrClass.getClass();
}
- String msg = "Unrecognized field \""+propertyName+"\" (class "+ref.getName()+"), not marked as ignorable";
+ String msg = String.format("Unrecognized field \"%s\" (class %s), not marked as ignorable",
+ propertyName, ref.getName());
UnrecognizedPropertyException e = new UnrecognizedPropertyException(p, msg,
p.getCurrentLocation(), ref, propertyName, propertyIds);
// but let's also ensure path includes this last (missing) segment
diff --git a/src/main/java/com/fasterxml/jackson/databind/ext/Java7Support.java b/src/main/java/com/fasterxml/jackson/databind/ext/Java7Support.java
index 5edf8a0..051f570 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ext/Java7Support.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ext/Java7Support.java
@@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* To support Java7-incomplete platforms, we will offer support for JDK 7
@@ -21,7 +22,7 @@
Java7Support impl = null;
try {
Class<?> cls = Class.forName("com.fasterxml.jackson.databind.ext.Java7SupportImpl");
- impl = (Java7Support) cls.newInstance();
+ impl = (Java7Support) ClassUtil.createInstance(cls, false);
} catch (Throwable t) {
// 24-Nov-2015, tatu: Should we log or not?
java.util.logging.Logger.getLogger(Java7Support.class.getName())
diff --git a/src/main/java/com/fasterxml/jackson/databind/ext/OptionalHandlerFactory.java b/src/main/java/com/fasterxml/jackson/databind/ext/OptionalHandlerFactory.java
index b3d9e58..5fe03ff 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ext/OptionalHandlerFactory.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ext/OptionalHandlerFactory.java
@@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.Deserializers;
import com.fasterxml.jackson.databind.ser.Serializers;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Helper class used for isolating details of handling optional+external types
@@ -149,7 +150,7 @@
private Object instantiate(String className)
{
try {
- return Class.forName(className).newInstance();
+ return ClassUtil.createInstance(Class.forName(className), false);
} catch (LinkageError e) { }
// too many different kinds to enumerate here:
catch (Exception e) { }
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/Annotated.java b/src/main/java/com/fasterxml/jackson/databind/introspect/Annotated.java
index 2d7c064..c0e2b5b 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/Annotated.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/Annotated.java
@@ -24,20 +24,6 @@
* @since 2.7
*/
public abstract boolean hasOneOf(Class<? extends Annotation>[] annoClasses);
-
- /**
- * Fluent factory method that will construct a new instance that uses specified
- * instance annotations instead of currently configured ones.
- */
- public abstract Annotated withAnnotations(AnnotationMap fallback);
-
- /**
- * Fluent factory method that will construct a new instance that uses
- * annotations from specified {@link Annotated} as fallback annotations
- */
- public final Annotated withFallBackAnnotationsFrom(Annotated annotated) {
- return withAnnotations(AnnotationMap.merge(getAllAnnotations(), annotated.getAllAnnotations()));
- }
/**
* Method that can be used to find actual JDK element that this instance
@@ -48,7 +34,7 @@
protected abstract int getModifiers();
- public final boolean isPublic() {
+ public boolean isPublic() {
return Modifier.isPublic(getModifiers());
}
@@ -90,20 +76,6 @@
*/
public abstract Class<?> getRawType();
- /**
- * Accessor that can be used to iterate over all the annotations
- * associated with annotated component.
- *
- * @since 2.3
- */
- public abstract Iterable<Annotation> annotations();
-
- /**
- * Internal helper method used to access annotation information;
- * not exposed to developers since instances are mutable.
- */
- protected abstract AnnotationMap getAllAnnotations();
-
// Also: ensure we can use #equals, #hashCode
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClass.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClass.java
index 109c4c7..1275e3d 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClass.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClass.java
@@ -1,8 +1,6 @@
package com.fasterxml.jackson.databind.introspect;
import java.lang.annotation.Annotation;
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
import java.lang.reflect.*;
import java.util.*;
@@ -19,8 +17,6 @@
extends Annotated
implements TypeResolutionContext
{
- private final static AnnotationMap[] NO_ANNOTATION_MAPS = new AnnotationMap[0];
-
/*
/**********************************************************
/* Configuration
@@ -48,8 +44,6 @@
/**
* Ordered set of super classes and interfaces of the
* class itself: included in order of precedence
- *<p>
- * NOTE: changed in 2.7 from List of <code>Class</code>es to List of {@link JavaType}s.
*/
final protected List<JavaType> _superTypes;
@@ -88,29 +82,12 @@
* Combined list of Jackson annotations that the class has,
* including inheritable ones from super classes and interfaces
*/
- final protected AnnotationMap _classAnnotations;
+ final protected Annotations _classAnnotations;
/**
- * Flag to indicate whether creator information has been resolved
- * or not.
+ * @since 2.9
*/
- protected boolean _creatorsResolved = false;
-
- /**
- * Default constructor of the annotated class, if it has one.
- */
- protected AnnotatedConstructor _defaultConstructor;
-
- /**
- * Single argument constructors the class has, if any.
- */
- protected List<AnnotatedConstructor> _constructors;
-
- /**
- * Single argument static methods that might be usable
- * as factory methods
- */
- protected List<AnnotatedMethod> _creatorMethods;
+ protected Creators _creators;
/**
* Member methods of interest; for now ones with 0 or 1 arguments
@@ -142,97 +119,60 @@
* Constructor will not do any initializations, to allow for
* configuring instances differently depending on use cases
*/
- private AnnotatedClass(JavaType type, Class<?> rawType, TypeBindings bindings,
- List<JavaType> superTypes,
+ AnnotatedClass(JavaType type, Class<?> rawType, List<JavaType> superTypes,
+ Class<?> primaryMixIn, Annotations classAnnotations, TypeBindings bindings,
AnnotationIntrospector aintr, MixInResolver mir, TypeFactory tf)
{
_type = type;
_class = rawType;
+ _classAnnotations = classAnnotations;
_bindings = bindings;
_superTypes = superTypes;
_annotationIntrospector = aintr;
_typeFactory = tf;
_mixInResolver = mir;
- _primaryMixIn = (_mixInResolver == null) ? null
- : _mixInResolver.findMixInClassFor(_class);
- _classAnnotations = _resolveClassAnnotations();
- }
-
- private AnnotatedClass(AnnotatedClass base, AnnotationMap clsAnn) {
- _type = base._type;
- _class = base._class;
- _bindings = base._bindings;
- _superTypes = base._superTypes;
- _annotationIntrospector = base._annotationIntrospector;
- _typeFactory = base._typeFactory;
- _mixInResolver = base._mixInResolver;
- _primaryMixIn = base._primaryMixIn;
- _classAnnotations = clsAnn;
- }
-
- @Override
- public AnnotatedClass withAnnotations(AnnotationMap ann) {
- return new AnnotatedClass(this, ann);
+ _primaryMixIn = primaryMixIn;
}
/**
- * Factory method that instantiates an instance. Returned instance
- * will only be initialized with class annotations, but not with
- * any method information.
- *
- * @since 2.7
+ * @deprecated Since 2.9, use methods in {@link AnnotatedClassResolver} instead.
*/
+ @Deprecated
public static AnnotatedClass construct(JavaType type, MapperConfig<?> config) {
- AnnotationIntrospector intr = config.isAnnotationProcessingEnabled()
- ? config.getAnnotationIntrospector() : null;
- Class<?> raw = type.getRawClass();
- return new AnnotatedClass(type, raw, type.getBindings(),
- ClassUtil.findSuperTypes(type, null, false), intr,
- (MixInResolver) config, config.getTypeFactory());
+ return construct(type, config, (MixInResolver) config);
}
/**
- * @since 2.7
+ * @deprecated Since 2.9, use methods in {@link AnnotatedClassResolver} instead.
*/
+ @Deprecated
public static AnnotatedClass construct(JavaType type, MapperConfig<?> config,
MixInResolver mir)
{
- AnnotationIntrospector intr = config.isAnnotationProcessingEnabled()
- ? config.getAnnotationIntrospector() : null;
- Class<?> raw = type.getRawClass();
- return new AnnotatedClass(type, raw, type.getBindings(),
- ClassUtil.findSuperTypes(type, null, false),
- intr, mir, config.getTypeFactory());
+ return AnnotatedClassResolver.resolve(config, type, mir);
}
-
+
/**
* Method similar to {@link #construct}, but that will NOT include
* information from supertypes; only class itself and any direct
* mix-ins it may have.
*/
- public static AnnotatedClass constructWithoutSuperTypes(Class<?> cls, MapperConfig<?> config)
- {
- if (config == null) {
- return new AnnotatedClass(null, cls, TypeBindings.emptyBindings(),
- Collections.<JavaType>emptyList(), null, null, null);
- }
- AnnotationIntrospector intr = config.isAnnotationProcessingEnabled()
- ? config.getAnnotationIntrospector() : null;
- return new AnnotatedClass(null, cls, TypeBindings.emptyBindings(),
- Collections.<JavaType>emptyList(), intr, (MixInResolver) config, config.getTypeFactory());
+ /**
+ * @deprecated Since 2.9, use methods in {@link AnnotatedClassResolver} instead.
+ */
+ @Deprecated
+ public static AnnotatedClass constructWithoutSuperTypes(Class<?> raw, MapperConfig<?> config) {
+ return constructWithoutSuperTypes(raw, config, config);
}
- public static AnnotatedClass constructWithoutSuperTypes(Class<?> cls, MapperConfig<?> config,
+ /**
+ * @deprecated Since 2.9, use methods in {@link AnnotatedClassResolver} instead.
+ */
+ @Deprecated
+ public static AnnotatedClass constructWithoutSuperTypes(Class<?> raw, MapperConfig<?> config,
MixInResolver mir)
{
- if (config == null) {
- return new AnnotatedClass(null, cls, TypeBindings.emptyBindings(),
- Collections.<JavaType>emptyList(), null, null, null);
- }
- AnnotationIntrospector intr = config.isAnnotationProcessingEnabled()
- ? config.getAnnotationIntrospector() : null;
- return new AnnotatedClass(null, cls, TypeBindings.emptyBindings(),
- Collections.<JavaType>emptyList(), intr, mir, config.getTypeFactory());
+ return AnnotatedClassResolver.resolveWithoutSuperTypes(config, raw, mir);
}
/*
@@ -282,16 +222,6 @@
}
@Override
- public Iterable<Annotation> annotations() {
- return _classAnnotations.annotations();
- }
-
- @Override
- protected AnnotationMap getAllAnnotations() {
- return _classAnnotations;
- }
-
- @Override
public JavaType getType() {
return _type;
}
@@ -310,69 +240,81 @@
return _classAnnotations.size() > 0;
}
- public AnnotatedConstructor getDefaultConstructor()
- {
- if (!_creatorsResolved) {
- resolveCreators();
- }
- return _defaultConstructor;
+ public AnnotatedConstructor getDefaultConstructor() {
+ return _creators().defaultConstructor;
}
- public List<AnnotatedConstructor> getConstructors()
- {
- if (!_creatorsResolved) {
- resolveCreators();
- }
- return _constructors;
+ public List<AnnotatedConstructor> getConstructors() {
+ return _creators().constructors;
}
- public List<AnnotatedMethod> getStaticMethods()
- {
- if (!_creatorsResolved) {
- resolveCreators();
- }
- return _creatorMethods;
+ /**
+ * @since 2.9
+ */
+ public List<AnnotatedMethod> getFactoryMethods() {
+ return _creators().creatorMethods;
}
- public Iterable<AnnotatedMethod> memberMethods()
- {
- if (_memberMethods == null) {
- resolveMemberMethods();
- }
- return _memberMethods;
+ /**
+ * @deprecated Since 2.9; use {@link #getFactoryMethods} instead.
+ */
+ @Deprecated
+ public List<AnnotatedMethod> getStaticMethods() {
+ return getFactoryMethods();
}
- public int getMemberMethodCount()
- {
- if (_memberMethods == null) {
- resolveMemberMethods();
+ private final Creators _creators() {
+ Creators c = _creators;
+ if (c == null) {
+ _creators = c = AnnotatedCreatorCollector.collectCreators(_annotationIntrospector,
+ this,
+ _type, _primaryMixIn);
}
- return _memberMethods.size();
+ return c;
}
- public AnnotatedMethod findMethod(String name, Class<?>[] paramTypes)
- {
- if (_memberMethods == null) {
- resolveMemberMethods();
+ public Iterable<AnnotatedMethod> memberMethods() {
+ return _methods();
+ }
+
+ public int getMemberMethodCount() {
+ return _methods().size();
+ }
+
+ public AnnotatedMethod findMethod(String name, Class<?>[] paramTypes) {
+ return _methods().find(name, paramTypes);
+ }
+
+ private final AnnotatedMethodMap _methods() {
+ AnnotatedMethodMap m = _memberMethods;
+ if (m == null) {
+ _memberMethods = m = AnnotatedMethodCollector.collectMethods(_annotationIntrospector,
+ this,
+ _mixInResolver, _typeFactory,
+ _type, _superTypes, _primaryMixIn);
}
- return _memberMethods.find(name, paramTypes);
+ return m;
}
public int getFieldCount() {
- if (_fields == null) {
- resolveFields();
- }
- return _fields.size();
+ return _fields().size();
}
- public Iterable<AnnotatedField> fields()
- {
- if (_fields == null) {
- resolveFields();
- }
- return _fields;
+ public Iterable<AnnotatedField> fields() {
+ return _fields();
}
+ private final List<AnnotatedField> _fields() {
+ List<AnnotatedField> f = _fields;
+ if (f == null) {
+ _fields = f = AnnotatedFieldCollector.collectFields(_annotationIntrospector,
+ this,
+ _mixInResolver, _typeFactory,
+ _type);
+ }
+ return f;
+ }
+
/**
* @since 2.9
*/
@@ -387,836 +329,7 @@
/*
/**********************************************************
- /* Public API, main-level resolution methods
- /**********************************************************
- */
-
- /**
- * Initialization method that will recursively collect Jackson
- * annotations for this class and all super classes and
- * interfaces.
- */
- private AnnotationMap _resolveClassAnnotations()
- {
- AnnotationMap ca = new AnnotationMap();
- // Should skip processing if annotation processing disabled
- if (_annotationIntrospector != null) {
- // add mix-in annotations first (overrides)
- if (_primaryMixIn != null) {
- _addClassMixIns(ca, _class, _primaryMixIn);
- }
- // first, annotations from the class itself:
- _addAnnotationsIfNotPresent(ca,
- ClassUtil.findClassAnnotations(_class));
-
- // and then from super types
- for (JavaType type : _superTypes) {
- // and mix mix-in annotations in-between
- _addClassMixIns(ca, type);
- _addAnnotationsIfNotPresent(ca,
- ClassUtil.findClassAnnotations(type.getRawClass()));
- }
- /* and finally... any annotations there might be for plain
- * old Object.class: separate because for all other purposes
- * it is just ignored (not included in super types)
- */
- /* 12-Jul-2009, tatu: Should this be done for interfaces too?
- * For now, yes, seems useful for some cases, and not harmful for any?
- */
- _addClassMixIns(ca, Object.class);
- }
- return ca;
- }
-
- /**
- * Initialization method that will find out all constructors
- * and potential static factory methods the class has.
- */
- private void resolveCreators()
- {
- // Constructor also always members of this class
- TypeResolutionContext typeContext = this;
-
- // 30-Apr-2016, tatu: [databind#1215]: Actually, while true, this does
- // NOT apply to context since sub-class may have type bindings
-// TypeResolutionContext typeContext = new TypeResolutionContext.Basic(_typeFactory, _type.getBindings());
-
- // Then see which constructors we have
- List<AnnotatedConstructor> constructors = null;
-
- // 18-Jun-2016, tatu: Enum constructors will never be useful (unlike
- // possibly static factory methods); but they can be royal PITA
- // due to some oddities by JVM; see:
- // [https://github.com/FasterXML/jackson-module-parameter-names/issues/35]
- // for more. So, let's just skip them.
- if (!_type.isEnumType()) {
- ClassUtil.Ctor[] declaredCtors = ClassUtil.getConstructors(_class);
- for (ClassUtil.Ctor ctor : declaredCtors) {
- if (_isIncludableConstructor(ctor.getConstructor())) {
- if (ctor.getParamCount() == 0) {
- _defaultConstructor = _constructDefaultConstructor(ctor, typeContext);
- } else {
- if (constructors == null) {
- constructors = new ArrayList<AnnotatedConstructor>(Math.max(10, declaredCtors.length));
- }
- constructors.add(_constructNonDefaultConstructor(ctor, typeContext));
- }
- }
- }
- }
- if (constructors == null) {
- _constructors = Collections.emptyList();
- } else {
- _constructors = constructors;
- }
- // and if need be, augment with mix-ins
- if (_primaryMixIn != null) {
- if (_defaultConstructor != null || !_constructors.isEmpty()) {
- _addConstructorMixIns(_primaryMixIn);
- }
- }
-
- /* And then... let's remove all constructors that are deemed
- * ignorable after all annotations have been properly collapsed.
- */
- // AnnotationIntrospector is null if annotations not enabled; if so, can skip:
- if (_annotationIntrospector != null) {
- if (_defaultConstructor != null) {
- if (_annotationIntrospector.hasIgnoreMarker(_defaultConstructor)) {
- _defaultConstructor = null;
- }
- }
- if (_constructors != null) {
- // count down to allow safe removal
- for (int i = _constructors.size(); --i >= 0; ) {
- if (_annotationIntrospector.hasIgnoreMarker(_constructors.get(i))) {
- _constructors.remove(i);
- }
- }
- }
- }
- List<AnnotatedMethod> creatorMethods = null;
-
- // Then static methods which are potential factory methods
- for (Method m : _findClassMethods(_class)) {
- if (!Modifier.isStatic(m.getModifiers())) {
- continue;
- }
- // all factory methods are fine:
- //int argCount = m.getParameterTypes().length;
- if (creatorMethods == null) {
- creatorMethods = new ArrayList<AnnotatedMethod>(8);
- }
- creatorMethods.add(_constructCreatorMethod(m, typeContext));
- }
- if (creatorMethods == null) {
- _creatorMethods = Collections.emptyList();
- } else {
- _creatorMethods = creatorMethods;
- // mix-ins to mix in?
- if (_primaryMixIn != null) {
- _addFactoryMixIns(_primaryMixIn);
- }
- // anything to ignore at this point?
- if (_annotationIntrospector != null) {
- // count down to allow safe removal
- for (int i = _creatorMethods.size(); --i >= 0; ) {
- if (_annotationIntrospector.hasIgnoreMarker(_creatorMethods.get(i))) {
- _creatorMethods.remove(i);
- }
- }
- }
- }
- _creatorsResolved = true;
- }
-
- /**
- * Method for resolving member method information: aggregating all non-static methods
- * and combining annotations (to implement method-annotation inheritance)
- *
- * @param methodFilter Filter used to determine which methods to include
- */
- private void resolveMemberMethods()
- {
- _memberMethods = _resolveMemberMethods();
- }
-
- private AnnotatedMethodMap _resolveMemberMethods()
- {
- AnnotatedMethodMap memberMethods = new AnnotatedMethodMap();
- AnnotatedMethodMap mixins = new AnnotatedMethodMap();
- // first: methods from the class itself
- _addMemberMethods(_class, this, memberMethods, _primaryMixIn, mixins);
-
- // and then augment these with annotations from super-types:
- for (JavaType type : _superTypes) {
- Class<?> mixin = (_mixInResolver == null) ? null : _mixInResolver.findMixInClassFor(type.getRawClass());
- _addMemberMethods(type.getRawClass(),
- new TypeResolutionContext.Basic(_typeFactory, type.getBindings()),
- memberMethods, mixin, mixins);
- }
- // Special case: mix-ins for Object.class? (to apply to ALL classes)
- if (_mixInResolver != null) {
- Class<?> mixin = _mixInResolver.findMixInClassFor(Object.class);
- if (mixin != null) {
- _addMethodMixIns(_class, memberMethods, mixin, mixins);
- }
- }
-
- /* Any unmatched mix-ins? Most likely error cases (not matching
- * any method); but there is one possible real use case:
- * exposing Object#hashCode (alas, Object#getClass can NOT be
- * exposed)
- */
- // 14-Feb-2011, tatu: AnnotationIntrospector is null if annotations not enabled; if so, can skip:
- if (_annotationIntrospector != null) {
- if (!mixins.isEmpty()) {
- Iterator<AnnotatedMethod> it = mixins.iterator();
- while (it.hasNext()) {
- AnnotatedMethod mixIn = it.next();
- try {
- Method m = Object.class.getDeclaredMethod(mixIn.getName(), mixIn.getRawParameterTypes());
- if (m != null) {
- // Since it's from java.lang.Object, no generics, no need for real type context:
- AnnotatedMethod am = _constructMethod(m, this);
- _addMixOvers(mixIn.getAnnotated(), am, false);
- memberMethods.add(am);
- }
- } catch (Exception e) { }
- }
- }
- }
- return memberMethods;
- }
-
- /**
- * Method that will collect all member (non-static) fields
- * that are either public, or have at least a single annotation
- * associated with them.
- */
- private void resolveFields()
- {
- Map<String,AnnotatedField> foundFields = _findFields(_type, this, null);
- List<AnnotatedField> f;
- if (foundFields == null || foundFields.size() == 0) {
- f = Collections.emptyList();
- } else {
- f = new ArrayList<AnnotatedField>(foundFields.size());
- f.addAll(foundFields.values());
- }
- _fields = f;
- }
-
- /*
- /**********************************************************
- /* Helper methods for resolving class annotations
- /* (resolution consisting of inheritance, overrides,
- /* and injection of mix-ins as necessary)
- /**********************************************************
- */
-
- /**
- * Helper method for adding any mix-in annotations specified
- * class might have.
- */
- protected void _addClassMixIns(AnnotationMap annotations, JavaType target)
- {
- if (_mixInResolver != null) {
- final Class<?> toMask = target.getRawClass();
- _addClassMixIns(annotations, toMask, _mixInResolver.findMixInClassFor(toMask));
- }
- }
-
- protected void _addClassMixIns(AnnotationMap annotations, Class<?> target)
- {
- if (_mixInResolver != null) {
- _addClassMixIns(annotations, target, _mixInResolver.findMixInClassFor(target));
- }
- }
-
- protected void _addClassMixIns(AnnotationMap annotations, Class<?> toMask,
- Class<?> mixin)
- {
- if (mixin == null) {
- return;
- }
- // Ok, first: annotations from mix-in class itself:
- _addAnnotationsIfNotPresent(annotations, ClassUtil.findClassAnnotations(mixin));
-
- /* And then from its supertypes, if any. But note that we will
- * only consider super-types up until reaching the masked
- * class (if found); this because often mix-in class
- * is a sub-class (for convenience reasons). And if so, we
- * absolutely must NOT include super types of masked class,
- * as that would inverse precedence of annotations.
- */
- for (Class<?> parent : ClassUtil.findSuperClasses(mixin, toMask, false)) {
- _addAnnotationsIfNotPresent(annotations, ClassUtil.findClassAnnotations(parent));
- }
- }
-
- /*
- /**********************************************************
- /* Helper methods for populating creator (ctor, factory) information
- /**********************************************************
- */
-
- protected void _addConstructorMixIns(Class<?> mixin)
- {
- MemberKey[] ctorKeys = null;
- int ctorCount = (_constructors == null) ? 0 : _constructors.size();
- for (ClassUtil.Ctor ctor0 : ClassUtil.getConstructors(mixin)) {
- Constructor<?> ctor = ctor0.getConstructor();
- if (ctor.getParameterTypes().length == 0) {
- if (_defaultConstructor != null) {
- _addMixOvers(ctor, _defaultConstructor, false);
- }
- } else {
- if (ctorKeys == null) {
- ctorKeys = new MemberKey[ctorCount];
- for (int i = 0; i < ctorCount; ++i) {
- ctorKeys[i] = new MemberKey(_constructors.get(i).getAnnotated());
- }
- }
- MemberKey key = new MemberKey(ctor);
-
- for (int i = 0; i < ctorCount; ++i) {
- if (!key.equals(ctorKeys[i])) {
- continue;
- }
- _addMixOvers(ctor, _constructors.get(i), true);
- break;
- }
- }
- }
- }
-
- protected void _addFactoryMixIns(Class<?> mixin)
- {
- MemberKey[] methodKeys = null;
- int methodCount = _creatorMethods.size();
-
- for (Method m : ClassUtil.getDeclaredMethods(mixin)) {
- if (!Modifier.isStatic(m.getModifiers())) {
- continue;
- }
- if (m.getParameterTypes().length == 0) {
- continue;
- }
- if (methodKeys == null) {
- methodKeys = new MemberKey[methodCount];
- for (int i = 0; i < methodCount; ++i) {
- methodKeys[i] = new MemberKey(_creatorMethods.get(i).getAnnotated());
- }
- }
- MemberKey key = new MemberKey(m);
- for (int i = 0; i < methodCount; ++i) {
- if (!key.equals(methodKeys[i])) {
- continue;
- }
- _addMixOvers(m, _creatorMethods.get(i), true);
- break;
- }
- }
- }
-
- /*
- /**********************************************************
- /* Helper methods for populating method information
- /**********************************************************
- */
-
- protected void _addMemberMethods(Class<?> cls, TypeResolutionContext typeContext,
- AnnotatedMethodMap methods,
- Class<?> mixInCls, AnnotatedMethodMap mixIns)
- {
- // first, mixIns, since they have higher priority then class methods
- if (mixInCls != null) {
- _addMethodMixIns(cls, methods, mixInCls, mixIns);
- }
- if (cls == null) { // just so caller need not check when passing super-class
- return;
- }
- // then methods from the class itself
- for (Method m : _findClassMethods(cls)) {
- if (!_isIncludableMemberMethod(m)) {
- continue;
- }
- AnnotatedMethod old = methods.find(m);
- if (old == null) {
- AnnotatedMethod newM = _constructMethod(m, typeContext);
- methods.add(newM);
- // Ok, but is there a mix-in to connect now?
- old = mixIns.remove(m);
- if (old != null) {
- _addMixOvers(old.getAnnotated(), newM, false);
- }
- } else {
- /* If sub-class already has the method, we only want to augment
- * annotations with entries that are not masked by sub-class.
- */
- _addMixUnders(m, old);
-
- /* 06-Jan-2010, tatu: [JACKSON-450] Except that if method we saw first is
- * from an interface, and we now find a non-interface definition, we should
- * use this method, but with combination of annotations.
- * This helps (or rather, is essential) with JAXB annotations and
- * may also result in faster method calls (interface calls are slightly
- * costlier than regular method calls)
- */
- if (old.getDeclaringClass().isInterface() && !m.getDeclaringClass().isInterface()) {
- methods.add(old.withMethod(m));
- }
- }
- }
- }
-
- protected void _addMethodMixIns(Class<?> targetClass, AnnotatedMethodMap methods,
- Class<?> mixInCls, AnnotatedMethodMap mixIns)
- {
-// List<Class<?>> parents = ClassUtil.findSuperClasses(mixInCls, targetClass, true);
-
- List<Class<?>> parents = ClassUtil.findRawSuperTypes(mixInCls, targetClass, true);
- for (Class<?> mixin : parents) {
- for (Method m : ClassUtil.getDeclaredMethods(mixin)) {
- if (!_isIncludableMemberMethod(m)) {
- continue;
- }
- AnnotatedMethod am = methods.find(m);
- /* Do we already have a method to augment (from sub-class
- * that will mask this mixIn)? If so, add if visible
- * without masking (no such annotation)
- */
- if (am != null) {
- _addMixUnders(m, am);
- /* Otherwise will have precedence, but must wait
- * until we find the real method (mixIn methods are
- * just placeholder, can't be called)
- */
- } else {
- // Well, or, as per [databind#515], multi-level merge within mixins...
- am = mixIns.find(m);
- if (am != null) {
- _addMixUnders(m, am);
- } else {
- // 03-Nov-2015, tatu: Mix-in method never called, should not need
- // to resolve generic types, so this class is fine as context
- mixIns.add(_constructMethod(m, this));
- }
- }
- }
- }
- }
-
- /*
- /**********************************************************
- /* Helper methods for populating field information
- /**********************************************************
- */
-
- protected Map<String,AnnotatedField> _findFields(JavaType type,
- TypeResolutionContext typeContext, Map<String,AnnotatedField> fields)
- {
- /* First, a quick test: we only care for regular classes (not
- * interfaces, primitive types etc), except for Object.class.
- * A simple check to rule out other cases is to see if there
- * is a super class or not.
- */
- JavaType parent = type.getSuperClass();
- if (parent != null) {
- final Class<?> cls = type.getRawClass();
- // Let's add super-class' fields first, then ours.
- /* 21-Feb-2010, tatu: Need to handle masking: as per [JACKSON-226]
- * we otherwise get into trouble...
- */
- fields = _findFields(parent,
- new TypeResolutionContext.Basic(_typeFactory, parent.getBindings()),
- fields);
- for (Field f : ClassUtil.getDeclaredFields(cls)) {
- // static fields not included (transients are at this point, filtered out later)
- if (!_isIncludableField(f)) {
- continue;
- }
- /* Ok now: we can (and need) not filter out ignorable fields
- * at this point; partly because mix-ins haven't been
- * added, and partly because logic can be done when
- * determining get/settability of the field.
- */
- if (fields == null) {
- fields = new LinkedHashMap<String,AnnotatedField>();
- }
- fields.put(f.getName(), _constructField(f, typeContext));
- }
- // And then... any mix-in overrides?
- if (_mixInResolver != null) {
- Class<?> mixin = _mixInResolver.findMixInClassFor(cls);
- if (mixin != null) {
- _addFieldMixIns(mixin, cls, fields);
- }
- }
- }
- return fields;
- }
-
- /**
- * Method called to add field mix-ins from given mix-in class (and its fields)
- * into already collected actual fields (from introspected classes and their
- * super-classes)
- */
- protected void _addFieldMixIns(Class<?> mixInCls, Class<?> targetClass,
- Map<String,AnnotatedField> fields)
- {
- List<Class<?>> parents = ClassUtil.findSuperClasses(mixInCls, targetClass, true);
- for (Class<?> mixin : parents) {
- for (Field mixinField : ClassUtil.getDeclaredFields(mixin)) {
- // there are some dummy things (static, synthetic); better ignore
- if (!_isIncludableField(mixinField)) {
- continue;
- }
- String name = mixinField.getName();
- // anything to mask? (if not, quietly ignore)
- AnnotatedField maskedField = fields.get(name);
- if (maskedField != null) {
- _addOrOverrideAnnotations(maskedField, mixinField.getDeclaredAnnotations());
- }
- }
- }
- }
-
- /*
- /**********************************************************
- /* Helper methods, constructing value types
- /**********************************************************
- */
-
- protected AnnotatedMethod _constructMethod(Method m, TypeResolutionContext typeContext)
- {
- /* note: parameter annotations not used for regular (getter, setter)
- * methods; only for creator methods (static factory methods)
- * -- at least not yet!
- */
- if (_annotationIntrospector == null) { // when annotation processing is disabled
- return new AnnotatedMethod(typeContext, m, _emptyAnnotationMap(), null);
- }
- return new AnnotatedMethod(typeContext, m, _collectRelevantAnnotations(m.getDeclaredAnnotations()), null);
- }
-
- protected AnnotatedConstructor _constructDefaultConstructor(ClassUtil.Ctor ctor,
- TypeResolutionContext typeContext)
- {
- if (_annotationIntrospector == null) { // when annotation processing is disabled
- return new AnnotatedConstructor(typeContext, ctor.getConstructor(), _emptyAnnotationMap(), NO_ANNOTATION_MAPS);
- }
- return new AnnotatedConstructor(typeContext, ctor.getConstructor(),
- _collectRelevantAnnotations(ctor.getDeclaredAnnotations()), NO_ANNOTATION_MAPS);
- }
-
- protected AnnotatedConstructor _constructNonDefaultConstructor(ClassUtil.Ctor ctor,
- TypeResolutionContext typeContext)
- {
- final int paramCount = ctor.getParamCount();
- if (_annotationIntrospector == null) { // when annotation processing is disabled
- return new AnnotatedConstructor(typeContext, ctor.getConstructor(),
- _emptyAnnotationMap(), _emptyAnnotationMaps(paramCount));
- }
-
- /* Looks like JDK has discrepancy, whereas annotations for implicit 'this'
- * (for non-static inner classes) are NOT included, but type is?
- * Strange, sounds like a bug. Alas, we can't really fix that...
- */
- if (paramCount == 0) { // no-arg default constructors, can simplify slightly
- return new AnnotatedConstructor(typeContext, ctor.getConstructor(),
- _collectRelevantAnnotations(ctor.getDeclaredAnnotations()), NO_ANNOTATION_MAPS);
- }
- // Also: enum value constructors
- AnnotationMap[] resolvedAnnotations;
- Annotation[][] paramAnns = ctor.getParameterAnnotations();
- if (paramCount != paramAnns.length) {
- // Limits of the work-around (to avoid hiding real errors):
- // first, only applicable for member classes and then either:
-
- resolvedAnnotations = null;
- Class<?> dc = ctor.getDeclaringClass();
- // (a) is enum, which have two extra hidden params (name, index)
- if (dc.isEnum() && (paramCount == paramAnns.length + 2)) {
- Annotation[][] old = paramAnns;
- paramAnns = new Annotation[old.length+2][];
- System.arraycopy(old, 0, paramAnns, 2, old.length);
- resolvedAnnotations = _collectRelevantAnnotations(paramAnns);
- } else if (dc.isMemberClass()) {
- // (b) non-static inner classes, get implicit 'this' for parameter, not annotation
- if (paramCount == (paramAnns.length + 1)) {
- // hack attack: prepend a null entry to make things match
- Annotation[][] old = paramAnns;
- paramAnns = new Annotation[old.length+1][];
- System.arraycopy(old, 0, paramAnns, 1, old.length);
- resolvedAnnotations = _collectRelevantAnnotations(paramAnns);
- }
- }
- if (resolvedAnnotations == null) {
- throw new IllegalStateException("Internal error: constructor for "+ctor.getDeclaringClass().getName()
- +" has mismatch: "+paramCount+" parameters; "+paramAnns.length+" sets of annotations");
- }
- } else {
- resolvedAnnotations = _collectRelevantAnnotations(paramAnns);
- }
- return new AnnotatedConstructor(typeContext, ctor.getConstructor(),
- _collectRelevantAnnotations(ctor.getDeclaredAnnotations()), resolvedAnnotations);
- }
-
- protected AnnotatedMethod _constructCreatorMethod(Method m, TypeResolutionContext typeContext)
- {
- final int paramCount = m.getParameterTypes().length;
- if (_annotationIntrospector == null) { // when annotation processing is disabled
- return new AnnotatedMethod(typeContext, m, _emptyAnnotationMap(), _emptyAnnotationMaps(paramCount));
- }
- if (paramCount == 0) { // common enough we can slightly optimize
- return new AnnotatedMethod(typeContext, m, _collectRelevantAnnotations(m.getDeclaredAnnotations()),
- NO_ANNOTATION_MAPS);
- }
- return new AnnotatedMethod(typeContext, m, _collectRelevantAnnotations(m.getDeclaredAnnotations()),
- _collectRelevantAnnotations(m.getParameterAnnotations()));
- }
-
- protected AnnotatedField _constructField(Field f, TypeResolutionContext typeContext)
- {
- if (_annotationIntrospector == null) { // when annotation processing is disabled
- return new AnnotatedField(typeContext, f, _emptyAnnotationMap());
- }
- return new AnnotatedField(typeContext, f, _collectRelevantAnnotations(f.getDeclaredAnnotations()));
- }
-
- private AnnotationMap _emptyAnnotationMap() {
- return new AnnotationMap();
- }
-
- private AnnotationMap[] _emptyAnnotationMaps(int count) {
- if (count == 0) {
- return NO_ANNOTATION_MAPS;
- }
- AnnotationMap[] maps = new AnnotationMap[count];
- for (int i = 0; i < count; ++i) {
- maps[i] = _emptyAnnotationMap();
- }
- return maps;
- }
-
- /*
- /**********************************************************
- /* Helper methods, inclusion filtering
- /**********************************************************
- */
-
- protected boolean _isIncludableMemberMethod(Method m)
- {
- if (Modifier.isStatic(m.getModifiers())) {
- return false;
- }
- /* 07-Apr-2009, tatu: Looks like generics can introduce hidden
- * bridge and/or synthetic methods. I don't think we want to
- * consider those...
- */
- if (m.isSynthetic() || m.isBridge()) {
- return false;
- }
- // also, for now we have no use for methods with 2 or more arguments:
- int pcount = m.getParameterTypes().length;
- return (pcount <= 2);
- }
-
- private boolean _isIncludableField(Field f)
- {
- // Most likely synthetic fields, if any, are to be skipped similar to methods
- if (f.isSynthetic()) {
- return false;
- }
- // Static fields are never included. Transient are (since 2.6), for
- // purpose of propagating removal
- int mods = f.getModifiers();
- if (Modifier.isStatic(mods)) {
- return false;
- }
- return true;
- }
-
- // for [databind#1005]: do not use or expose synthetic constructors
- private boolean _isIncludableConstructor(Constructor<?> c)
- {
- return !c.isSynthetic();
- }
-
- /*
- /**********************************************************
- /* Helper methods, attaching annotations
- /**********************************************************
- */
-
- protected AnnotationMap[] _collectRelevantAnnotations(Annotation[][] anns)
- {
- int len = anns.length;
- AnnotationMap[] result = new AnnotationMap[len];
- for (int i = 0; i < len; ++i) {
- result[i] = _collectRelevantAnnotations(anns[i]);
- }
- return result;
- }
-
- protected AnnotationMap _collectRelevantAnnotations(Annotation[] anns)
- {
- return _addAnnotationsIfNotPresent(new AnnotationMap(), anns);
- }
-
- /* Helper method used to add all applicable annotations from given set.
- * Takes into account possible "annotation bundles" (meta-annotations to
- * include instead of main-level annotation)
- */
- private AnnotationMap _addAnnotationsIfNotPresent(AnnotationMap result, Annotation[] anns)
- {
- if (anns != null) {
- List<Annotation> fromBundles = null;
- for (Annotation ann : anns) { // first: direct annotations
- // note: we will NOT filter out non-Jackson anns any more
- boolean wasNotPresent = result.addIfNotPresent(ann);
- if (wasNotPresent && _isAnnotationBundle(ann)) {
- fromBundles = _addFromBundle(ann, fromBundles);
- }
- }
- if (fromBundles != null) { // and secondarily handle bundles, if any found: precedence important
- _addAnnotationsIfNotPresent(result, fromBundles.toArray(new Annotation[fromBundles.size()]));
- }
- }
- return result;
- }
-
- private List<Annotation> _addFromBundle(Annotation bundle, List<Annotation> result)
- {
- for (Annotation a : ClassUtil.findClassAnnotations(bundle.annotationType())) {
- // minor optimization: by-pass 2 common JDK meta-annotations
- if ((a instanceof Target) || (a instanceof Retention)) {
- continue;
- }
- if (result == null) {
- result = new ArrayList<Annotation>();
- }
- result.add(a);
- }
- return result;
- }
-
- private void _addAnnotationsIfNotPresent(AnnotatedMember target, Annotation[] anns)
- {
- if (anns != null) {
- List<Annotation> fromBundles = null;
- for (Annotation ann : anns) { // first: direct annotations
- boolean wasNotPresent = target.addIfNotPresent(ann);
- if (wasNotPresent && _isAnnotationBundle(ann)) {
- fromBundles = _addFromBundle(ann, fromBundles);
- }
- }
- if (fromBundles != null) { // and secondarily handle bundles, if any found: precedence important
- _addAnnotationsIfNotPresent(target, fromBundles.toArray(new Annotation[fromBundles.size()]));
- }
- }
- }
-
- private void _addOrOverrideAnnotations(AnnotatedMember target, Annotation[] anns)
- {
- if (anns != null) {
- List<Annotation> fromBundles = null;
- for (Annotation ann : anns) { // first: direct annotations
- boolean wasModified = target.addOrOverride(ann);
- if (wasModified && _isAnnotationBundle(ann)) {
- fromBundles = _addFromBundle(ann, fromBundles);
- }
- }
- if (fromBundles != null) { // and then bundles, if any: important for precedence
- _addOrOverrideAnnotations(target, fromBundles.toArray(new Annotation[fromBundles.size()]));
- }
- }
- }
-
- /**
- * @param addParamAnnotations Whether parameter annotations are to be
- * added as well
- */
- protected void _addMixOvers(Constructor<?> mixin, AnnotatedConstructor target,
- boolean addParamAnnotations)
- {
- _addOrOverrideAnnotations(target, mixin.getDeclaredAnnotations());
- if (addParamAnnotations) {
- Annotation[][] pa = mixin.getParameterAnnotations();
- for (int i = 0, len = pa.length; i < len; ++i) {
- for (Annotation a : pa[i]) {
- target.addOrOverrideParam(i, a);
- }
- }
- }
- }
-
- /**
- * @param addParamAnnotations Whether parameter annotations are to be
- * added as well
- */
- protected void _addMixOvers(Method mixin, AnnotatedMethod target,
- boolean addParamAnnotations)
- {
- _addOrOverrideAnnotations(target, mixin.getDeclaredAnnotations());
- if (addParamAnnotations) {
- Annotation[][] pa = mixin.getParameterAnnotations();
- for (int i = 0, len = pa.length; i < len; ++i) {
- for (Annotation a : pa[i]) {
- target.addOrOverrideParam(i, a);
- }
- }
- }
- }
-
- /**
- * Method that will add annotations from specified source method to target method,
- * but only if target does not yet have them.
- */
- protected void _addMixUnders(Method src, AnnotatedMethod target) {
- _addAnnotationsIfNotPresent(target, src.getDeclaredAnnotations());
- }
-
- private final boolean _isAnnotationBundle(Annotation ann) {
- return (_annotationIntrospector != null) && _annotationIntrospector.isAnnotationBundle(ann);
- }
-
- /**
- * Helper method that gets methods declared in given class; usually a simple thing,
- * but sometimes (as per [databind#785]) more complicated, depending on classloader
- * setup.
- *
- * @since 2.4.7
- */
- protected Method[] _findClassMethods(Class<?> cls)
- {
- try {
- return ClassUtil.getDeclaredMethods(cls);
- } catch (final NoClassDefFoundError ex) {
- // One of the methods had a class that was not found in the cls.getClassLoader.
- // Maybe the developer was nice and has a different class loader for this context.
- final ClassLoader loader = Thread.currentThread().getContextClassLoader();
- if (loader == null){
- // Nope... this is going to end poorly
- throw ex;
- }
- final Class<?> contextClass;
- try {
- contextClass = loader.loadClass(cls.getName());
- } catch (ClassNotFoundException e) {
- // !!! TODO: 08-May-2015, tatu: Chain appropriately once we have JDK 1.7/Java7 as baseline
- //ex.addSuppressed(e); Not until Jackson 2.7
- throw ex;
- }
- return contextClass.getDeclaredMethods(); // Cross fingers
- }
- }
-
- /*
- /**********************************************************
- /* Other methods
+ /* Standard method overrides
/**********************************************************
*/
@@ -1233,7 +346,43 @@
@Override
public boolean equals(Object o) {
if (o == this) return true;
- if (o == null || o.getClass() != getClass()) return false;
+ if (!ClassUtil.hasClass(o, getClass())) {
+ return false;
+ }
return ((AnnotatedClass) o)._class == _class;
}
+
+ /*
+ /**********************************************************
+ /* Helper classes
+ /**********************************************************
+ */
+
+ public static final class Creators
+ {
+ /**
+ * Default constructor of the annotated class, if it has one.
+ */
+ public final AnnotatedConstructor defaultConstructor;
+
+ /**
+ * Single argument constructors the class has, if any.
+ */
+ public final List<AnnotatedConstructor> constructors;
+
+ /**
+ * Single argument static methods that might be usable
+ * as factory methods
+ */
+ public final List<AnnotatedMethod> creatorMethods;
+
+ public Creators(AnnotatedConstructor defCtor,
+ List<AnnotatedConstructor> ctors,
+ List<AnnotatedMethod> ctorMethods)
+ {
+ defaultConstructor = defCtor;
+ constructors = ctors;
+ creatorMethods = ctorMethods;
+ }
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClassResolver.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClassResolver.java
new file mode 100644
index 0000000..ec10854
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClassResolver.java
@@ -0,0 +1,237 @@
+package com.fasterxml.jackson.databind.introspect;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.util.Collections;
+import java.util.List;
+
+import com.fasterxml.jackson.databind.AnnotationIntrospector;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.cfg.MapperConfig;
+import com.fasterxml.jackson.databind.introspect.ClassIntrospector.MixInResolver;
+import com.fasterxml.jackson.databind.type.TypeBindings;
+import com.fasterxml.jackson.databind.util.Annotations;
+import com.fasterxml.jackson.databind.util.ClassUtil;
+
+/**
+ * Helper class that contains logic for resolving annotations to construct
+ * {@link AnnotatedClass} instances.
+ *
+ * @since 2.9
+ */
+public class AnnotatedClassResolver
+{
+ private final static Annotations NO_ANNOTATIONS = AnnotationCollector.emptyAnnotations();
+
+ private final MapperConfig<?> _config;
+ private final AnnotationIntrospector _intr;
+ private final MixInResolver _mixInResolver;
+ private final TypeBindings _bindings;
+
+ private final JavaType _type;
+ private final Class<?> _class;
+ private final Class<?> _primaryMixin;
+
+ AnnotatedClassResolver(MapperConfig<?> config, JavaType type, MixInResolver r) {
+ _config = config;
+ _type = type;
+ _class = type.getRawClass();
+ _mixInResolver = r;
+ _bindings = type.getBindings();
+ _intr = config.isAnnotationProcessingEnabled()
+ ? config.getAnnotationIntrospector() : null;
+ _primaryMixin = _config.findMixInClassFor(_class);
+ }
+
+ AnnotatedClassResolver(MapperConfig<?> config, Class<?> cls, MixInResolver r) {
+ _config = config;
+ _type = null;
+ _class = cls;
+ _mixInResolver = r;
+ _bindings = TypeBindings.emptyBindings();
+ if (config == null) {
+ _intr = null;
+ _primaryMixin = null;
+ } else {
+ _intr = config.isAnnotationProcessingEnabled()
+ ? config.getAnnotationIntrospector() : null;
+ _primaryMixin = _config.findMixInClassFor(_class);
+ }
+ }
+
+ public static AnnotatedClass resolve(MapperConfig<?> config, JavaType forType,
+ MixInResolver r)
+ {
+ if (forType.isArrayType() && skippableArray(config, forType.getRawClass())) {
+ return createArrayType(config, forType.getRawClass());
+ }
+ return new AnnotatedClassResolver(config, forType, r).resolveFully();
+ }
+
+ public static AnnotatedClass resolveWithoutSuperTypes(MapperConfig<?> config, Class<?> forType) {
+ return resolveWithoutSuperTypes(config, forType, config);
+ }
+
+ public static AnnotatedClass resolveWithoutSuperTypes(MapperConfig<?> config, JavaType forType,
+ MixInResolver r)
+ {
+ if (forType.isArrayType() && skippableArray(config, forType.getRawClass())) {
+ return createArrayType(config, forType.getRawClass());
+ }
+ return new AnnotatedClassResolver(config, forType, r).resolveWithoutSuperTypes();
+ }
+
+ public static AnnotatedClass resolveWithoutSuperTypes(MapperConfig<?> config, Class<?> forType,
+ MixInResolver r)
+ {
+ if (forType.isArray() && skippableArray(config, forType)) {
+ return createArrayType(config, forType);
+ }
+ return new AnnotatedClassResolver(config, forType, r).resolveWithoutSuperTypes();
+ }
+
+ private static boolean skippableArray(MapperConfig<?> config, Class<?> type) {
+ return (config == null) || (config.findMixInClassFor(type) == null);
+
+ }
+
+ /**
+ * Internal helper method used for resolving a small set of "primordial" types for which
+ * we do not accept any annotation information or overrides.
+ */
+ static AnnotatedClass createPrimordial(Class<?> raw) {
+ List<JavaType> superTypes = Collections.emptyList();
+ return new AnnotatedClass(null, raw, superTypes, null, NO_ANNOTATIONS,
+ TypeBindings.emptyBindings(), null, null, null);
+ }
+
+ /**
+ * Internal helper method used for resolving array types, unless they happen
+ * to have associated mix-in to apply.
+ */
+ static AnnotatedClass createArrayType(MapperConfig<?> config, Class<?> raw) {
+ List<JavaType> superTypes = Collections.emptyList();
+ return new AnnotatedClass(null, raw, superTypes, null, NO_ANNOTATIONS,
+ TypeBindings.emptyBindings(), null, null, null);
+ }
+
+ AnnotatedClass resolveFully() {
+ List<JavaType> superTypes = ClassUtil.findSuperTypes(_type, null, false);
+ return new AnnotatedClass(_type, _class, superTypes, _primaryMixin,
+ resolveClassAnnotations(superTypes),
+ _bindings, _intr, _mixInResolver, _config.getTypeFactory());
+
+ }
+
+ AnnotatedClass resolveWithoutSuperTypes() {
+ List<JavaType> superTypes = Collections.<JavaType>emptyList();
+ return new AnnotatedClass(null, _class, superTypes, _primaryMixin,
+ resolveClassAnnotations(superTypes),
+ _bindings, _intr, _config, _config.getTypeFactory());
+ }
+
+ /*
+ /**********************************************************
+ /* Class annotation resolution
+ /**********************************************************
+ */
+
+ /**
+ * Initialization method that will recursively collect Jackson
+ * annotations for this class and all super classes and
+ * interfaces.
+ */
+ private Annotations resolveClassAnnotations(List<JavaType> superTypes)
+ {
+ // Should skip processing if annotation processing disabled
+ if (_intr == null) {
+ return NO_ANNOTATIONS;
+ }
+ AnnotationCollector resolvedCA = AnnotationCollector.emptyCollector();
+ // add mix-in annotations first (overrides)
+ if (_primaryMixin != null) {
+ resolvedCA = _addClassMixIns(resolvedCA, _class, _primaryMixin);
+ }
+ // then annotations from the class itself:
+ resolvedCA = _addAnnotationsIfNotPresent(resolvedCA,
+ ClassUtil.findClassAnnotations(_class));
+
+ // and then from super types
+ for (JavaType type : superTypes) {
+ // and mix mix-in annotations in-between
+ if (_mixInResolver != null) {
+ Class<?> cls = type.getRawClass();
+ resolvedCA = _addClassMixIns(resolvedCA, cls,
+ _mixInResolver.findMixInClassFor(cls));
+ }
+ resolvedCA = _addAnnotationsIfNotPresent(resolvedCA,
+ ClassUtil.findClassAnnotations(type.getRawClass()));
+ }
+ /* and finally... any annotations there might be for plain
+ * old Object.class: separate because for all other purposes
+ * it is just ignored (not included in super types)
+ */
+ // 12-Jul-2009, tatu: Should this be done for interfaces too?
+ // For now, yes, seems useful for some cases, and not harmful for any?
+ if (_mixInResolver != null) {
+ resolvedCA = _addClassMixIns(resolvedCA, Object.class,
+ _mixInResolver.findMixInClassFor(Object.class));
+ }
+ return resolvedCA.asAnnotations();
+ }
+
+ private AnnotationCollector _addClassMixIns(AnnotationCollector annotations,
+ Class<?> target, Class<?> mixin)
+ {
+ if (mixin != null) {
+ // Ok, first: annotations from mix-in class itself:
+ annotations = _addAnnotationsIfNotPresent(annotations, ClassUtil.findClassAnnotations(mixin));
+
+ // And then from its supertypes, if any. But note that we will only consider
+ // super-types up until reaching the masked class (if found); this because
+ // often mix-in class is a sub-class (for convenience reasons).
+ // And if so, we absolutely must NOT include super types of masked class,
+ // as that would inverse precedence of annotations.
+ for (Class<?> parent : ClassUtil.findSuperClasses(mixin, target, false)) {
+ annotations = _addAnnotationsIfNotPresent(annotations, ClassUtil.findClassAnnotations(parent));
+ }
+ }
+ return annotations;
+ }
+
+ private AnnotationCollector _addAnnotationsIfNotPresent(AnnotationCollector c,
+ Annotation[] anns)
+ {
+ if (anns != null) {
+ for (Annotation ann : anns) { // first: direct annotations
+ // note: we will NOT filter out non-Jackson annotations any more
+ if (!c.isPresent(ann)) {
+ c = c.addOrOverride(ann);
+ if (_intr.isAnnotationBundle(ann)) {
+ c = _addFromBundleIfNotPresent(c, ann);
+ }
+ }
+ }
+ }
+ return c;
+ }
+
+ private AnnotationCollector _addFromBundleIfNotPresent(AnnotationCollector c,
+ Annotation bundle)
+ {
+ for (Annotation ann : ClassUtil.findClassAnnotations(bundle.annotationType())) {
+ // minor optimization: by-pass 2 common JDK meta-annotations
+ if ((ann instanceof Target) || (ann instanceof Retention)) {
+ continue;
+ }
+ if (!c.isPresent(ann)) {
+ c = c.addOrOverride(ann);
+ if (_intr.isAnnotationBundle(ann)) {
+ c = _addFromBundleIfNotPresent(c, ann);
+ }
+ }
+ }
+ return c;
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedConstructor.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedConstructor.java
index a5e81ad..4e17a00 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedConstructor.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedConstructor.java
@@ -176,10 +176,10 @@
@Override
public boolean equals(Object o) {
if (o == this) return true;
- if (o == null || o.getClass() != getClass()) return false;
- return ((AnnotatedConstructor) o)._constructor == _constructor;
+ return ClassUtil.hasClass(o, getClass())
+ && (((AnnotatedConstructor) o)._constructor == _constructor);
}
-
+
/*
/**********************************************************
/* JDK serialization handling
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java
new file mode 100644
index 0000000..0ec6d66
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java
@@ -0,0 +1,358 @@
+package com.fasterxml.jackson.databind.introspect;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import com.fasterxml.jackson.databind.AnnotationIntrospector;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.introspect.AnnotatedClass.Creators;
+import com.fasterxml.jackson.databind.util.ClassUtil;
+
+/**
+ * Helper class used to contain details of how Creators (annotated constructors
+ * and static methods) are discovered to be accessed by and via {@link AnnotatedClass}.
+ *
+ * @since 2.9
+ */
+final class AnnotatedCreatorCollector
+ extends CollectorBase
+{
+ // // // Configuration
+
+ private final TypeResolutionContext _typeContext;
+
+ // // // Collected state
+
+ private AnnotatedConstructor _defaultConstructor;
+
+ AnnotatedCreatorCollector(AnnotationIntrospector intr,
+ TypeResolutionContext tc)
+ {
+ super(intr);
+ _typeContext = tc;
+ }
+
+ public static Creators collectCreators(AnnotationIntrospector intr,
+ TypeResolutionContext tc,
+ JavaType type, Class<?> primaryMixIn)
+ {
+ // Constructor also always members of resolved class, parent == resolution context
+ return new AnnotatedCreatorCollector(intr, tc)
+ .collect(type, primaryMixIn);
+ }
+
+ Creators collect(JavaType type, Class<?> primaryMixIn)
+ {
+ // 30-Apr-2016, tatu: [databind#1215]: Actually, while true, this does
+ // NOT apply to context since sub-class may have type bindings
+// TypeResolutionContext typeContext = new TypeResolutionContext.Basic(_typeFactory, _type.getBindings());
+
+ List<AnnotatedConstructor> constructors = _findPotentialConstructors(type, primaryMixIn);
+ List<AnnotatedMethod> factories = _findPotentialFactories(type, primaryMixIn);
+
+ /* And then... let's remove all constructors that are deemed
+ * ignorable after all annotations have been properly collapsed.
+ */
+ // AnnotationIntrospector is null if annotations not enabled; if so, can skip:
+ if (_intr != null) {
+ if (_defaultConstructor != null) {
+ if (_intr.hasIgnoreMarker(_defaultConstructor)) {
+ _defaultConstructor = null;
+ }
+ }
+ // count down to allow safe removal
+ for (int i = constructors.size(); --i >= 0; ) {
+ if (_intr.hasIgnoreMarker(constructors.get(i))) {
+ constructors.remove(i);
+ }
+ }
+ for (int i = factories.size(); --i >= 0; ) {
+ if (_intr.hasIgnoreMarker(factories.get(i))) {
+ factories.remove(i);
+ }
+ }
+ }
+ return new AnnotatedClass.Creators(_defaultConstructor, constructors, factories);
+ }
+
+ /**
+ * Helper method for locating constructors (and matching mix-in overrides)
+ * we might want to use; this is needed in order to mix information between
+ * the two and construct resulting {@link AnnotatedConstructor}s
+ */
+ private List<AnnotatedConstructor> _findPotentialConstructors(JavaType type,
+ Class<?> primaryMixIn)
+ {
+ ClassUtil.Ctor defaultCtor = null;
+ List<ClassUtil.Ctor> ctors = null;
+
+ // 18-Jun-2016, tatu: Enum constructors will never be useful (unlike
+ // possibly static factory methods); but they can be royal PITA
+ // due to some oddities by JVM; see:
+ // [https://github.com/FasterXML/jackson-module-parameter-names/issues/35]
+ // for more. So, let's just skip them.
+ if (!type.isEnumType()) {
+ ClassUtil.Ctor[] declaredCtors = ClassUtil.getConstructors(type.getRawClass());
+ for (ClassUtil.Ctor ctor : declaredCtors) {
+ if (!isIncludableConstructor(ctor.getConstructor())) {
+ continue;
+ }
+ if (ctor.getParamCount() == 0) {
+ defaultCtor = ctor;
+ } else {
+ if (ctors == null) {
+ ctors = new ArrayList<>();
+ }
+ ctors.add(ctor);
+ }
+ }
+ }
+ List<AnnotatedConstructor> result;
+ int ctorCount;
+ if (ctors == null) {
+ result = Collections.emptyList();
+ // Nothing found? Short-circuit
+ if (defaultCtor == null) {
+ return result;
+ }
+ ctorCount = 0;
+ } else {
+ ctorCount = ctors.size();
+ result = new ArrayList<>(ctorCount);
+ for (int i = 0; i < ctorCount; ++i) {
+ result.add(null);
+ }
+ }
+
+ // so far so good; but do we also need to find mix-ins overrides?
+ if (primaryMixIn != null) {
+ MemberKey[] ctorKeys = null;
+ for (ClassUtil.Ctor mixinCtor : ClassUtil.getConstructors(primaryMixIn)) {
+ if (mixinCtor.getParamCount() == 0) {
+ if (defaultCtor != null) {
+ _defaultConstructor = constructDefaultConstructor(defaultCtor, mixinCtor);
+ defaultCtor = null;
+ }
+ continue;
+ }
+ if (ctors != null) {
+ if (ctorKeys == null) {
+ ctorKeys = new MemberKey[ctorCount];
+ for (int i = 0; i < ctorCount; ++i) {
+ ctorKeys[i] = new MemberKey(ctors.get(i).getConstructor());
+ }
+ }
+ MemberKey key = new MemberKey(mixinCtor.getConstructor());
+
+ for (int i = 0; i < ctorCount; ++i) {
+ if (key.equals(ctorKeys[i])) {
+ result.set(i,
+ constructNonDefaultConstructor(ctors.get(i), mixinCtor));
+ break;
+ }
+ }
+ }
+ }
+ }
+ // Ok: anything within mix-ins has been resolved; anything remaining we must resolve
+ if (defaultCtor != null) {
+ _defaultConstructor = constructDefaultConstructor(defaultCtor, null);
+ }
+ for (int i = 0; i < ctorCount; ++i) {
+ AnnotatedConstructor ctor = result.get(i);
+ if (ctor == null) {
+ result.set(i,
+ constructNonDefaultConstructor(ctors.get(i), null));
+ }
+ }
+ return result;
+ }
+
+ private List<AnnotatedMethod> _findPotentialFactories(JavaType type, Class<?> primaryMixIn)
+ {
+ List<Method> candidates = null;
+
+ // First find all potentially relevant static methods
+ for (Method m : ClassUtil.getClassMethods(type.getRawClass())) {
+ if (!Modifier.isStatic(m.getModifiers())) {
+ continue;
+ }
+ // all factory methods are fine:
+ //int argCount = m.getParameterTypes().length;
+ if (candidates == null) {
+ candidates = new ArrayList<>();
+ }
+ candidates.add(m);
+ }
+ // and then locate mix-ins, if any
+ if (candidates == null) {
+ return Collections.emptyList();
+ }
+ int factoryCount = candidates.size();
+ List<AnnotatedMethod> result = new ArrayList<>(factoryCount);
+ for (int i = 0; i < factoryCount; ++i) {
+ result.add(null);
+ }
+ // so far so good; but do we also need to find mix-ins overrides?
+ if (primaryMixIn != null) {
+ MemberKey[] methodKeys = null;
+ for (Method mixinFactory : ClassUtil.getDeclaredMethods(primaryMixIn)) {
+ if (!Modifier.isStatic(mixinFactory.getModifiers())) {
+ continue;
+ }
+ if (methodKeys == null) {
+ methodKeys = new MemberKey[factoryCount];
+ for (int i = 0; i < factoryCount; ++i) {
+ methodKeys[i] = new MemberKey(candidates.get(i));
+ }
+ }
+ MemberKey key = new MemberKey(mixinFactory);
+ for (int i = 0; i < factoryCount; ++i) {
+ if (key.equals(methodKeys[i])) {
+ result.set(i,
+ constructFactoryCreator(candidates.get(i), mixinFactory));
+ break;
+ }
+ }
+ }
+ }
+ // Ok: anything within mix-ins has been resolved; anything remaining we must resolve
+ for (int i = 0; i < factoryCount; ++i) {
+ AnnotatedMethod factory = result.get(i);
+ if (factory == null) {
+ result.set(i,
+ constructFactoryCreator(candidates.get(i), null));
+ }
+ }
+ return result;
+ }
+
+ protected AnnotatedConstructor constructDefaultConstructor(ClassUtil.Ctor ctor,
+ ClassUtil.Ctor mixin)
+ {
+ if (_intr == null) { // when annotation processing is disabled
+ return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
+ _emptyAnnotationMap(), NO_ANNOTATION_MAPS);
+ }
+ return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
+ collectAnnotations(ctor, mixin),
+ collectAnnotations(ctor.getConstructor().getParameterAnnotations(),
+ (mixin == null) ? null : mixin.getConstructor().getParameterAnnotations()));
+ }
+
+ protected AnnotatedConstructor constructNonDefaultConstructor(ClassUtil.Ctor ctor,
+ ClassUtil.Ctor mixin)
+ {
+ final int paramCount = ctor.getParamCount();
+ if (_intr == null) { // when annotation processing is disabled
+ return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
+ _emptyAnnotationMap(), _emptyAnnotationMaps(paramCount));
+ }
+
+ /* Looks like JDK has discrepancy, whereas annotations for implicit 'this'
+ * (for non-static inner classes) are NOT included, but type is?
+ * Strange, sounds like a bug. Alas, we can't really fix that...
+ */
+ if (paramCount == 0) { // no-arg default constructors, can simplify slightly
+ return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
+ collectAnnotations(ctor, mixin),
+ NO_ANNOTATION_MAPS);
+ }
+ // Also: enum value constructors
+ AnnotationMap[] resolvedAnnotations;
+ Annotation[][] paramAnns = ctor.getParameterAnnotations();
+ if (paramCount != paramAnns.length) {
+ // Limits of the work-around (to avoid hiding real errors):
+ // first, only applicable for member classes and then either:
+
+ resolvedAnnotations = null;
+ Class<?> dc = ctor.getDeclaringClass();
+ // (a) is enum, which have two extra hidden params (name, index)
+ if (dc.isEnum() && (paramCount == paramAnns.length + 2)) {
+ Annotation[][] old = paramAnns;
+ paramAnns = new Annotation[old.length+2][];
+ System.arraycopy(old, 0, paramAnns, 2, old.length);
+ resolvedAnnotations = collectAnnotations(paramAnns, null);
+ } else if (dc.isMemberClass()) {
+ // (b) non-static inner classes, get implicit 'this' for parameter, not annotation
+ if (paramCount == (paramAnns.length + 1)) {
+ // hack attack: prepend a null entry to make things match
+ Annotation[][] old = paramAnns;
+ paramAnns = new Annotation[old.length+1][];
+ System.arraycopy(old, 0, paramAnns, 1, old.length);
+ paramAnns[0] = NO_ANNOTATIONS;
+ resolvedAnnotations = collectAnnotations(paramAnns, null);
+ }
+ }
+ if (resolvedAnnotations == null) {
+ throw new IllegalStateException(String.format(
+"Internal error: constructor for %s has mismatch: %d parameters; %d sets of annotations",
+ctor.getDeclaringClass().getName(), paramCount, paramAnns.length));
+ }
+ } else {
+ resolvedAnnotations = collectAnnotations(paramAnns,
+ (mixin == null) ? null : mixin.getParameterAnnotations());
+ }
+ return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
+ collectAnnotations(ctor, mixin), resolvedAnnotations);
+ }
+
+ protected AnnotatedMethod constructFactoryCreator(Method m, Method mixin)
+ {
+ final int paramCount = m.getParameterTypes().length;
+ if (_intr == null) { // when annotation processing is disabled
+ return new AnnotatedMethod(_typeContext, m, _emptyAnnotationMap(),
+ _emptyAnnotationMaps(paramCount));
+ }
+ if (paramCount == 0) { // common enough we can slightly optimize
+ return new AnnotatedMethod(_typeContext, m, collectAnnotations(m, mixin),
+ NO_ANNOTATION_MAPS);
+ }
+ return new AnnotatedMethod(_typeContext, m, collectAnnotations(m, mixin),
+ collectAnnotations(m.getParameterAnnotations(),
+ (mixin == null) ? null : mixin.getParameterAnnotations()));
+ }
+
+ private AnnotationMap[] collectAnnotations(Annotation[][] mainAnns, Annotation[][] mixinAnns) {
+ final int count = mainAnns.length;
+ AnnotationMap[] result = new AnnotationMap[count];
+ for (int i = 0; i < count; ++i) {
+ AnnotationCollector c = collectAnnotations(AnnotationCollector.emptyCollector(),
+ mainAnns[i]);
+ if (mixinAnns != null) {
+ c = collectAnnotations(c, mixinAnns[i]);
+ }
+ result[i] = c.asAnnotationMap();
+ }
+ return result;
+ }
+
+ // // NOTE: these are only called when we know we have AnnotationIntrospector
+
+ private AnnotationMap collectAnnotations(ClassUtil.Ctor main, ClassUtil.Ctor mixin) {
+ AnnotationCollector c = collectAnnotations(main.getConstructor().getDeclaredAnnotations());
+ if (mixin != null) {
+ c = collectAnnotations(c, mixin.getConstructor().getDeclaredAnnotations());
+ }
+ return c.asAnnotationMap();
+ }
+
+ private final AnnotationMap collectAnnotations(AnnotatedElement main, AnnotatedElement mixin) {
+ AnnotationCollector c = collectAnnotations(main.getDeclaredAnnotations());
+ if (mixin != null) {
+ c = collectAnnotations(c, mixin.getDeclaredAnnotations());
+ }
+ return c.asAnnotationMap();
+ }
+
+ // for [databind#1005]: do not use or expose synthetic constructors
+ private static boolean isIncludableConstructor(Constructor<?> c) {
+ return !c.isSynthetic();
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedField.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedField.java
index a499407..996250e 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedField.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedField.java
@@ -126,10 +126,6 @@
/**********************************************************
*/
- public String getFullName() {
- return getDeclaringClass().getName() + "#" + getName();
- }
-
public int getAnnotationCount() { return _annotations.size(); }
/**
@@ -145,8 +141,8 @@
@Override
public boolean equals(Object o) {
if (o == this) return true;
- if (o == null || o.getClass() != getClass()) return false;
- return ((AnnotatedField) o)._field == _field;
+ return ClassUtil.hasClass(o, getClass())
+ && (((AnnotatedField) o)._field == _field);
}
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedFieldCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedFieldCollector.java
new file mode 100644
index 0000000..0dba88b
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedFieldCollector.java
@@ -0,0 +1,149 @@
+package com.fasterxml.jackson.databind.introspect;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.*;
+
+import com.fasterxml.jackson.databind.AnnotationIntrospector;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.introspect.ClassIntrospector.MixInResolver;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+import com.fasterxml.jackson.databind.util.ClassUtil;
+
+public class AnnotatedFieldCollector
+ extends CollectorBase
+{
+ // // // Configuration
+
+ private final TypeFactory _typeFactory;
+ private final MixInResolver _mixInResolver;
+
+ // // // Collected state
+
+ AnnotatedFieldCollector(AnnotationIntrospector intr,
+ TypeFactory types, MixInResolver mixins)
+ {
+ super(intr);
+ _typeFactory = types;
+ _mixInResolver = (intr == null) ? null : mixins;
+ }
+
+ public static List<AnnotatedField> collectFields(AnnotationIntrospector intr,
+ TypeResolutionContext tc,
+ MixInResolver mixins, TypeFactory types,
+ JavaType type)
+ {
+ return new AnnotatedFieldCollector(intr, types, mixins).collect(tc, type);
+ }
+
+ List<AnnotatedField> collect(TypeResolutionContext tc, JavaType type)
+ {
+ Map<String,FieldBuilder> foundFields = _findFields(tc, type, null);
+ if (foundFields == null) {
+ return Collections.emptyList();
+ }
+ List<AnnotatedField> result = new ArrayList<>(foundFields.size());
+ for (FieldBuilder b : foundFields.values()) {
+ result.add(b.build());
+ }
+ return result;
+ }
+
+ private Map<String,FieldBuilder> _findFields(TypeResolutionContext tc,
+ JavaType type, Map<String,FieldBuilder> fields)
+ {
+ // First, a quick test: we only care for regular classes (not interfaces,
+ //primitive types etc), except for Object.class. A simple check to rule out
+ // other cases is to see if there is a super class or not.
+ JavaType parent = type.getSuperClass();
+ if (parent == null) {
+ return fields;
+ }
+ final Class<?> cls = type.getRawClass();
+ // Let's add super-class' fields first, then ours.
+ fields = _findFields(new TypeResolutionContext.Basic(_typeFactory, parent.getBindings()),
+ parent, fields);
+ for (Field f : ClassUtil.getDeclaredFields(cls)) {
+ // static fields not included (transients are at this point, filtered out later)
+ if (!_isIncludableField(f)) {
+ continue;
+ }
+ // Ok now: we can (and need) not filter out ignorable fields at this point; partly
+ // because mix-ins haven't been added, and partly because logic can be done
+ // when determining get/settability of the field.
+ if (fields == null) {
+ fields = new LinkedHashMap<>();
+ }
+ FieldBuilder b = new FieldBuilder(tc, f);
+ if (_intr != null) {
+ b.annotations = collectAnnotations(b.annotations, f.getDeclaredAnnotations());
+ }
+ fields.put(f.getName(), b);
+ }
+ // And then... any mix-in overrides?
+ if (_mixInResolver != null) {
+ Class<?> mixin = _mixInResolver.findMixInClassFor(cls);
+ if (mixin != null) {
+ _addFieldMixIns(mixin, cls, fields);
+ }
+ }
+ return fields;
+ }
+
+ /**
+ * Method called to add field mix-ins from given mix-in class (and its fields)
+ * into already collected actual fields (from introspected classes and their
+ * super-classes)
+ */
+ private void _addFieldMixIns(Class<?> mixInCls, Class<?> targetClass,
+ Map<String,FieldBuilder> fields)
+ {
+ List<Class<?>> parents = ClassUtil.findSuperClasses(mixInCls, targetClass, true);
+ for (Class<?> mixin : parents) {
+ for (Field mixinField : ClassUtil.getDeclaredFields(mixin)) {
+ // there are some dummy things (static, synthetic); better ignore
+ if (!_isIncludableField(mixinField)) {
+ continue;
+ }
+ String name = mixinField.getName();
+ // anything to mask? (if not, quietly ignore)
+ FieldBuilder b = fields.get(name);
+ if (b != null) {
+ b.annotations = collectAnnotations(b.annotations, mixinField.getDeclaredAnnotations());
+ }
+ }
+ }
+ }
+
+ private boolean _isIncludableField(Field f)
+ {
+ // Most likely synthetic fields, if any, are to be skipped similar to methods
+ if (f.isSynthetic()) {
+ return false;
+ }
+ // Static fields are never included. Transient are (since 2.6), for
+ // purpose of propagating removal
+ int mods = f.getModifiers();
+ if (Modifier.isStatic(mods)) {
+ return false;
+ }
+ return true;
+ }
+
+ private final static class FieldBuilder {
+ public final TypeResolutionContext typeContext;
+ public final Field field;
+
+ public AnnotationCollector annotations;
+
+ public FieldBuilder(TypeResolutionContext tc, Field f) {
+ typeContext = tc;
+ field = f;
+ annotations = AnnotationCollector.emptyCollector();
+ }
+
+ public AnnotatedField build() {
+ return new AnnotatedField(typeContext, field, annotations.asAnnotationMap());
+ }
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMember.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMember.java
index 423f209..40bbe6d 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMember.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMember.java
@@ -2,7 +2,6 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
-import java.util.Collections;
import com.fasterxml.jackson.databind.util.ClassUtil;
@@ -47,7 +46,15 @@
_typeContext = base._typeContext;
_annotations = base._annotations;
}
-
+
+ /**
+ * Fluent factory method that will construct a new instance that uses specified
+ * instance annotations instead of currently configured ones.
+ *
+ * @since 2.9 (promoted from `Annotated`)
+ */
+ public abstract Annotated withAnnotations(AnnotationMap fallback);
+
/**
* Actual physical class in which this memmber was declared.
*/
@@ -55,12 +62,19 @@
public abstract Member getMember();
+ public String getFullName() {
+ return getDeclaringClass().getName() + "#" + getName();
+ }
+
/**
* Accessor for {@link TypeResolutionContext} that is used for resolving
* full generic type of this member.
*
* @since 2.7
+ *
+ * @deprecated Since 2.9
*/
+ @Deprecated
public TypeResolutionContext getTypeContext() {
return _typeContext;
}
@@ -88,39 +102,16 @@
}
return _annotations.hasOneOf(annoClasses);
}
-
- @Override
- public Iterable<Annotation> annotations() {
- if (_annotations == null) {
- return Collections.emptyList();
- }
- return _annotations.annotations();
- }
-
- @Override
- protected AnnotationMap getAllAnnotations() {
+
+ /**
+ *<p>
+ * NOTE: promoted in 2.9 from `Annotated` up
+ */
+ public AnnotationMap getAllAnnotations() { // alas, used by at least one module, hence public
return _annotations;
}
/**
- * Method called to override an annotation, usually due to a mix-in
- * annotation masking or overriding an annotation 'real' constructor
- * has.
- */
- public final boolean addOrOverride(Annotation a) {
- return _annotations.add(a);
- }
-
- /**
- * Method called to augment annotations, by adding specified
- * annotation if and only if it is not yet present in the
- * annotation map we have.
- */
- public final boolean addIfNotPresent(Annotation a) {
- return _annotations.addIfNotPresent(a);
- }
-
- /**
* Method that can be called to modify access rights, by calling
* {@link java.lang.reflect.AccessibleObject#setAccessible} on
* the underlying annotated element.
@@ -140,15 +131,6 @@
}
/**
- * @deprecated Since 2.7 use {@link #fixAccess(boolean)} instead
- */
- @Deprecated
- public final void fixAccess() {
-// fixAccess(false);
- fixAccess(true);
- }
-
- /**
* Optional method that can be used to assign value of
* this member on given object, if this is a supported
* operation for member type.
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethod.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethod.java
index e7d75ac..36d2925 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethod.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethod.java
@@ -51,15 +51,7 @@
_method = null;
_serialization = ser;
}
-
- /**
- * Method that constructs a new instance with settings (annotations, parameter annotations)
- * of this instance, but with different physical {@link Method}.
- */
- public AnnotatedMethod withMethod(Method m) {
- return new AnnotatedMethod(_typeContext, m, _annotations, _paramAnnotations);
- }
-
+
@Override
public AnnotatedMethod withAnnotations(AnnotationMap ann) {
return new AnnotatedMethod(_typeContext, _method, ann, _paramAnnotations);
@@ -123,7 +115,7 @@
}
public final Object callOn(Object pojo) throws Exception {
- return _method.invoke(pojo);
+ return _method.invoke(pojo, (Object[]) null);
}
public final Object callOnWith(Object pojo, Object... args) throws Exception {
@@ -178,10 +170,7 @@
{
try {
_method.invoke(pojo, value);
- } catch (IllegalAccessException e) {
- throw new IllegalArgumentException("Failed to setValue() with method "
- +getFullName()+": "+e.getMessage(), e);
- } catch (InvocationTargetException e) {
+ } catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalArgumentException("Failed to setValue() with method "
+getFullName()+": "+e.getMessage(), e);
}
@@ -191,11 +180,8 @@
public Object getValue(Object pojo) throws IllegalArgumentException
{
try {
- return _method.invoke(pojo);
- } catch (IllegalAccessException e) {
- throw new IllegalArgumentException("Failed to getValue() with method "
- +getFullName()+": "+e.getMessage(), e);
- } catch (InvocationTargetException e) {
+ return _method.invoke(pojo, (Object[]) null);
+ } catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalArgumentException("Failed to getValue() with method "
+getFullName()+": "+e.getMessage(), e);
}
@@ -207,9 +193,9 @@
/*****************************************************
*/
+ @Override
public String getFullName() {
- return getDeclaringClass().getName() + "#" + getName() + "("
- +getParameterCount()+" params)";
+ return String.format("%s(%d params)", super.getFullName(), getParameterCount());
}
public Class<?>[] getRawParameterTypes()
@@ -260,10 +246,10 @@
@Override
public boolean equals(Object o) {
if (o == this) return true;
- if (o == null || o.getClass() != getClass()) return false;
- return ((AnnotatedMethod) o)._method == _method;
+ return ClassUtil.hasClass(o, getClass())
+ && (((AnnotatedMethod) o)._method == _method);
}
-
+
/*
/**********************************************************
/* JDK serialization handling
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java
new file mode 100644
index 0000000..6e8988c
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java
@@ -0,0 +1,201 @@
+package com.fasterxml.jackson.databind.introspect;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.*;
+
+import com.fasterxml.jackson.databind.AnnotationIntrospector;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.introspect.ClassIntrospector.MixInResolver;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+import com.fasterxml.jackson.databind.util.ClassUtil;
+
+public class AnnotatedMethodCollector
+ extends CollectorBase
+{
+ private final MixInResolver _mixInResolver;
+
+ AnnotatedMethodCollector(AnnotationIntrospector intr,
+ MixInResolver mixins)
+ {
+ super(intr);
+ _mixInResolver = (intr == null) ? null : mixins;
+ }
+
+ public static AnnotatedMethodMap collectMethods(AnnotationIntrospector intr,
+ TypeResolutionContext tc,
+ MixInResolver mixins, TypeFactory types,
+ JavaType type, List<JavaType> superTypes, Class<?> primaryMixIn)
+ {
+ // Constructor also always members of resolved class, parent == resolution context
+ return new AnnotatedMethodCollector(intr, mixins)
+ .collect(types, tc, type, superTypes, primaryMixIn);
+ }
+
+ AnnotatedMethodMap collect(TypeFactory typeFactory, TypeResolutionContext tc,
+ JavaType mainType, List<JavaType> superTypes, Class<?> primaryMixIn)
+ {
+ Map<MemberKey,MethodBuilder> methods = new LinkedHashMap<>();
+
+ // first: methods from the class itself
+ _addMemberMethods(tc, mainType.getRawClass(), methods, primaryMixIn);
+
+ // and then augment these with annotations from super-types:
+ for (JavaType type : superTypes) {
+ Class<?> mixin = (_mixInResolver == null) ? null : _mixInResolver.findMixInClassFor(type.getRawClass());
+ _addMemberMethods(
+ new TypeResolutionContext.Basic(typeFactory, type.getBindings()),
+ type.getRawClass(), methods, mixin);
+ }
+ // Special case: mix-ins for Object.class? (to apply to ALL classes)
+ /*
+ if (_mixInResolver != null) {
+ Class<?> mixin = _mixInResolver.findMixInClassFor(Object.class);
+ if (mixin != null) {
+ _addMethodMixIns(tc, mainType.getRawClass(), memberMethods, mixin, mixins);
+ }
+ }
+
+ // Any unmatched mix-ins? Most likely error cases (not matching any method);
+ // but there is one possible real use case: exposing Object#hashCode
+ // (alas, Object#getClass can NOT be exposed)
+ if (_intr != null) {
+ if (!mixins.isEmpty()) {
+ Iterator<AnnotatedMethod> it = mixins.iterator();
+ while (it.hasNext()) {
+ AnnotatedMethod mixIn = it.next();
+ try {
+ Method m = Object.class.getDeclaredMethod(mixIn.getName(), mixIn.getRawParameterTypes());
+ if (m != null) {
+ // Since it's from java.lang.Object, no generics, no need for real type context:
+ AnnotatedMethod am = _constructMethod(tc, m);
+ _addMixOvers(mixIn.getAnnotated(), am, false);
+ memberMethods.add(am);
+ }
+ } catch (Exception e) { }
+ }
+ }
+ }
+ */
+
+ // And then let's
+ if (methods.isEmpty()) {
+ return new AnnotatedMethodMap();
+ }
+ Map<MemberKey,AnnotatedMethod> actual = new LinkedHashMap<>(methods.size());
+ for (Map.Entry<MemberKey,MethodBuilder> entry : methods.entrySet()) {
+ AnnotatedMethod am = entry.getValue().build();
+ if (am != null) {
+ actual.put(entry.getKey(), am);
+ }
+ }
+ return new AnnotatedMethodMap(actual);
+ }
+
+ private void _addMemberMethods(TypeResolutionContext tc,
+ Class<?> cls, Map<MemberKey,MethodBuilder> methods, Class<?> mixInCls)
+ {
+ // first, mixIns, since they have higher priority then class methods
+ if (mixInCls != null) {
+ _addMethodMixIns(tc, cls, methods, mixInCls);
+ }
+ if (cls == null) { // just so caller need not check when passing super-class
+ return;
+ }
+ // then methods from the class itself
+ for (Method m : ClassUtil.getClassMethods(cls)) {
+ if (!_isIncludableMemberMethod(m)) {
+ continue;
+ }
+ final MemberKey key = new MemberKey(m);
+ MethodBuilder b = methods.get(key);
+ if (b == null) {
+ AnnotationCollector c = (_intr == null) ? AnnotationCollector.emptyCollector()
+ : collectAnnotations(m.getDeclaredAnnotations());
+ methods.put(key, new MethodBuilder(tc, m, c));
+ } else {
+ if (_intr != null) {
+ b.annotations = collectDefaultAnnotations(b.annotations, m.getDeclaredAnnotations());
+ }
+ Method old = b.method;
+ if (old == null) { // had "mix-over", replace
+ b.method = m;
+// } else if (old.getDeclaringClass().isInterface() && !m.getDeclaringClass().isInterface()) {
+ } else if (Modifier.isAbstract(old.getModifiers())
+ && !Modifier.isAbstract(m.getModifiers())) {
+ // 06-Jan-2010, tatu: Except that if method we saw first is
+ // from an interface, and we now find a non-interface definition, we should
+ // use this method, but with combination of annotations.
+ // This helps (or rather, is essential) with JAXB annotations and
+ // may also result in faster method calls (interface calls are slightly
+ // costlier than regular method calls)
+ b.method = m;
+ }
+ }
+ }
+ }
+
+ protected void _addMethodMixIns(TypeResolutionContext tc, Class<?> targetClass,
+ Map<MemberKey,MethodBuilder> methods, Class<?> mixInCls)
+ {
+ if (_intr == null) {
+ return;
+ }
+ for (Class<?> mixin : ClassUtil.findRawSuperTypes(mixInCls, targetClass, true)) {
+ for (Method m : ClassUtil.getDeclaredMethods(mixin)) {
+ if (!_isIncludableMemberMethod(m)) {
+ continue;
+ }
+ final MemberKey key = new MemberKey(m);
+ MethodBuilder b = methods.get(key);
+ Annotation[] anns = m.getDeclaredAnnotations();
+ if (b == null) {
+ // nothing yet; add but do NOT specify method -- this marks it
+ // as "mix-over", floating mix-in
+ methods.put(key, new MethodBuilder(tc, null, collectAnnotations(anns)));
+ } else {
+ b.annotations = collectDefaultAnnotations(b.annotations, anns);
+ }
+ }
+ }
+ }
+
+ private boolean _isIncludableMemberMethod(Method m)
+ {
+ if (Modifier.isStatic(m.getModifiers())
+ // Looks like generics can introduce hidden bridge and/or synthetic methods.
+ // I don't think we want to consider those...
+ || m.isSynthetic() || m.isBridge()) {
+ return false;
+ }
+ // also, for now we have no use for methods with more than 2 arguments:
+ // (2 argument methods for "any setter", fwtw)
+ int pcount = m.getParameterTypes().length;
+ return (pcount <= 2);
+ }
+
+ private final static class MethodBuilder {
+ public final TypeResolutionContext typeContext;
+
+ // Method left empty for "floating" mix-in, filled in as need be
+ public Method method;
+ public AnnotationCollector annotations;
+
+ public MethodBuilder(TypeResolutionContext tc, Method m,
+ AnnotationCollector ann) {
+ typeContext = tc;
+ method = m;
+ annotations = ann;
+ }
+
+ public AnnotatedMethod build() {
+ if (method == null) {
+ return null;
+ }
+ // 12-Apr-2017, tatu: Note that parameter annotations are NOT collected -- we could
+ // collect them if that'd make sense but...
+ return new AnnotatedMethod(typeContext, method, annotations.asAnnotationMap(), null);
+ }
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodMap.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodMap.java
index 0cfb387..dcd08a2 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodMap.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodMap.java
@@ -11,40 +11,15 @@
public final class AnnotatedMethodMap
implements Iterable<AnnotatedMethod>
{
- protected LinkedHashMap<MemberKey,AnnotatedMethod> _methods;
+ protected Map<MemberKey,AnnotatedMethod> _methods;
public AnnotatedMethodMap() { }
/**
- * Method called to add specified annotated method in the Map.
+ * @since 2.9
*/
- public void add(AnnotatedMethod am)
- {
- if (_methods == null) {
- _methods = new LinkedHashMap<MemberKey,AnnotatedMethod>();
- }
- _methods.put(new MemberKey(am.getAnnotated()), am);
- }
-
- /**
- * Method called to remove specified method, assuming
- * it exists in the Map
- */
- public AnnotatedMethod remove(AnnotatedMethod am)
- {
- return remove(am.getAnnotated());
- }
-
- public AnnotatedMethod remove(Method m)
- {
- if (_methods != null) {
- return _methods.remove(new MemberKey(m));
- }
- return null;
- }
-
- public boolean isEmpty() {
- return (_methods == null || _methods.size() == 0);
+ public AnnotatedMethodMap(Map<MemberKey,AnnotatedMethod> m) {
+ _methods = m;
}
public int size() {
@@ -76,10 +51,9 @@
@Override
public Iterator<AnnotatedMethod> iterator()
{
- if (_methods != null) {
- return _methods.values().iterator();
+ if (_methods == null) {
+ return Collections.emptyIterator();
}
- List<AnnotatedMethod> empty = Collections.emptyList();
- return empty.iterator();
+ return _methods.values().iterator();
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedParameter.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedParameter.java
index 62ce143..bb10657 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedParameter.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedParameter.java
@@ -3,6 +3,7 @@
import java.lang.reflect.*;
import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Object that represents method parameters, mostly so that associated
@@ -30,7 +31,7 @@
* Index of the parameter within argument list
*/
protected final int _index;
-
+
/*
/**********************************************************
/* Life-cycle
@@ -38,9 +39,10 @@
*/
public AnnotatedParameter(AnnotatedWithParams owner, JavaType type,
+ TypeResolutionContext typeContext,
AnnotationMap annotations, int index)
{
- super((owner == null) ? null : owner.getTypeContext(), annotations);
+ super(typeContext, annotations);
_owner = owner;
_type = type;
_index = index;
@@ -167,7 +169,9 @@
@Override
public boolean equals(Object o) {
if (o == this) return true;
- if (o == null || o.getClass() != getClass()) return false;
+ if (!ClassUtil.hasClass(o, getClass())) {
+ return false;
+ }
AnnotatedParameter other = (AnnotatedParameter) o;
return other._owner.equals(_owner) && (other._index == _index);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedWithParams.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedWithParams.java
index 8cf5a79..4f415a6 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedWithParams.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedWithParams.java
@@ -84,7 +84,7 @@
public final AnnotatedParameter getParameter(int index) {
return new AnnotatedParameter(this, getParameterType(index),
- getParameterAnnotations(index), index);
+ _typeContext, getParameterAnnotations(index), index);
}
public abstract int getParameterCount();
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationCollector.java
new file mode 100644
index 0000000..940d435
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationCollector.java
@@ -0,0 +1,302 @@
+package com.fasterxml.jackson.databind.introspect;
+
+import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import com.fasterxml.jackson.databind.util.Annotations;
+
+/**
+ * Helper class used to collect annotations to be stored as
+ * {@link com.fasterxml.jackson.databind.util.Annotations} (like {@link AnnotationMap}).
+ *
+ * @since 2.9
+ */
+public abstract class AnnotationCollector
+{
+ protected final static Annotations NO_ANNOTATIONS = new NoAnnotations();
+
+ /**
+ * Optional data to carry along
+ */
+ protected final Object _data;
+
+ protected AnnotationCollector(Object d) {
+ _data = d;
+ }
+
+ public static Annotations emptyAnnotations() { return NO_ANNOTATIONS; }
+
+ public static AnnotationCollector emptyCollector() {
+ return EmptyCollector.instance;
+ }
+
+ public static AnnotationCollector emptyCollector(Object data) {
+ return new EmptyCollector(data);
+ }
+
+ public abstract Annotations asAnnotations();
+ public abstract AnnotationMap asAnnotationMap();
+
+ public Object getData() {
+ return _data;
+ }
+
+ /*
+ /**********************************************************
+ /* API
+ /**********************************************************
+ */
+
+ public abstract boolean isPresent(Annotation ann);
+
+ public abstract AnnotationCollector addOrOverride(Annotation ann);
+
+ /*
+ /**********************************************************
+ /* Collector implementations
+ /**********************************************************
+ */
+
+ static class EmptyCollector extends AnnotationCollector
+ {
+ public final static EmptyCollector instance = new EmptyCollector(null);
+
+ EmptyCollector(Object data) { super(data); }
+
+ @Override
+ public Annotations asAnnotations() {
+ return NO_ANNOTATIONS;
+ }
+
+ @Override
+ public AnnotationMap asAnnotationMap() {
+ return new AnnotationMap();
+ }
+
+ @Override
+ public boolean isPresent(Annotation ann) { return false; }
+
+ @Override
+ public AnnotationCollector addOrOverride(Annotation ann) {
+ return new OneCollector(_data, ann.annotationType(), ann);
+ }
+ }
+
+ static class OneCollector extends AnnotationCollector
+ {
+ private Class<?> _type;
+ private Annotation _value;
+
+ public OneCollector(Object data,
+ Class<?> type, Annotation value) {
+ super(data);
+ _type = type;
+ _value = value;
+ }
+
+ @Override
+ public Annotations asAnnotations() {
+ return new OneAnnotation(_type, _value);
+ }
+
+ @Override
+ public AnnotationMap asAnnotationMap() {
+ return AnnotationMap.of(_type, _value);
+ }
+
+ @Override
+ public boolean isPresent(Annotation ann) {
+ return ann.annotationType() == _type;
+ }
+
+ @Override
+ public AnnotationCollector addOrOverride(Annotation ann) {
+ final Class<?> type = ann.annotationType();
+ // true override? Just replace in-place, return
+ if (_type == type) {
+ _value = ann;
+ return this;
+ }
+ return new NCollector(_data, _type, _value, type, ann);
+ }
+ }
+
+ static class NCollector extends AnnotationCollector
+ {
+ protected final HashMap<Class<?>,Annotation> _annotations;
+
+ public NCollector(Object data,
+ Class<?> type1, Annotation value1,
+ Class<?> type2, Annotation value2) {
+ super(data);
+ _annotations = new HashMap<>();
+ _annotations.put(type1, value1);
+ _annotations.put(type2, value2);
+ }
+
+ @Override
+ public Annotations asAnnotations() {
+ if (_annotations.size() == 2) {
+ Iterator<Map.Entry<Class<?>,Annotation>> it = _annotations.entrySet().iterator();
+ Map.Entry<Class<?>,Annotation> en1 = it.next(), en2 = it.next();
+ return new TwoAnnotations(en1.getKey(), en1.getValue(),
+ en2.getKey(), en2.getValue());
+ }
+ return new AnnotationMap(_annotations);
+ }
+
+ @Override
+ public AnnotationMap asAnnotationMap() {
+ AnnotationMap result = new AnnotationMap();
+ for (Annotation ann : _annotations.values()) {
+ result.add(ann);
+ }
+ return result;
+ }
+
+ @Override
+ public boolean isPresent(Annotation ann) {
+ return _annotations.containsKey(ann.annotationType());
+ }
+
+ @Override
+ public AnnotationCollector addOrOverride(Annotation ann) {
+ _annotations.put(ann.annotationType(), ann);
+ return this;
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Annotations implementations
+ /**********************************************************
+ */
+
+ /**
+ * Immutable implementation for case where no annotations are associated with
+ * an annotatable entity.
+ *
+ * @since 2.9
+ */
+ public static class NoAnnotations
+ implements Annotations, java.io.Serializable
+ {
+ private static final long serialVersionUID = 1L;
+
+ NoAnnotations() { }
+
+ @Override
+ public <A extends Annotation> A get(Class<A> cls) {
+ return null;
+ }
+
+ @Override
+ public boolean has(Class<?> cls) {
+ return false;
+ }
+
+ @Override
+ public boolean hasOneOf(Class<? extends Annotation>[] annoClasses) {
+ return false;
+ }
+
+ @Override
+ public int size() {
+ return 0;
+ }
+ }
+
+ public static class OneAnnotation
+ implements Annotations, java.io.Serializable
+ {
+ private static final long serialVersionUID = 1L;
+
+ private final Class<?> _type;
+ private final Annotation _value;
+
+ public OneAnnotation(Class<?> type, Annotation value) {
+ _type = type;
+ _value = value;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <A extends Annotation> A get(Class<A> cls) {
+ if (_type == cls) {
+ return (A) _value;
+ }
+ return null;
+ }
+
+ @Override
+ public boolean has(Class<?> cls) {
+ return (_type == cls);
+ }
+
+ @Override
+ public boolean hasOneOf(Class<? extends Annotation>[] annoClasses) {
+ for (Class<?> cls : annoClasses) {
+ if (cls == _type) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public int size() {
+ return 1;
+ }
+ }
+
+ public static class TwoAnnotations
+ implements Annotations, java.io.Serializable
+ {
+ private static final long serialVersionUID = 1L;
+
+ private final Class<?> _type1, _type2;
+ private final Annotation _value1, _value2;
+
+ public TwoAnnotations(Class<?> type1, Annotation value1,
+ Class<?> type2, Annotation value2) {
+ _type1 = type1;
+ _value1 = value1;
+ _type2 = type2;
+ _value2 = value2;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <A extends Annotation> A get(Class<A> cls) {
+ if (_type1 == cls) {
+ return (A) _value1;
+ }
+ if (_type2 == cls) {
+ return (A) _value2;
+ }
+ return null;
+ }
+
+ @Override
+ public boolean has(Class<?> cls) {
+ return (_type1 == cls) || (_type2 == cls);
+ }
+
+ @Override
+ public boolean hasOneOf(Class<? extends Annotation>[] annoClasses) {
+ for (Class<?> cls : annoClasses) {
+ if ((cls == _type1) || (cls == _type2)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public int size() {
+ return 2;
+ }
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationIntrospectorPair.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationIntrospectorPair.java
index 97e5edc..aa288b9 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationIntrospectorPair.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationIntrospectorPair.java
@@ -5,11 +5,13 @@
import java.util.Collection;
import java.util.List;
+import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
@@ -119,43 +121,9 @@
{
JsonIgnoreProperties.Value v2 = _secondary.findPropertyIgnorals(a);
JsonIgnoreProperties.Value v1 = _primary.findPropertyIgnorals(a);
-
- if (v2 == null) { // shouldn't occur but
- return v1;
- }
- return v2.withOverrides(v1);
+ return (v2 == null) // shouldn't occur but
+ ? v1 : v2.withOverrides(v1);
}
-
- @Override
- @Deprecated // since 2.6
- public String[] findPropertiesToIgnore(Annotated ac) {
- String[] result = _primary.findPropertiesToIgnore(ac);
- if (result == null) {
- result = _secondary.findPropertiesToIgnore(ac);
- }
- return result;
- }
-
- @Override
- @Deprecated // since 2.8
- public String[] findPropertiesToIgnore(Annotated ac, boolean forSerialization) {
- String[] result = _primary.findPropertiesToIgnore(ac, forSerialization);
- if (result == null) {
- result = _secondary.findPropertiesToIgnore(ac, forSerialization);
- }
- return result;
- }
-
- @Override
- @Deprecated // since 2.8
- public Boolean findIgnoreUnknownProperties(AnnotatedClass ac)
- {
- Boolean result = _primary.findIgnoreUnknownProperties(ac);
- if (result == null) {
- result = _secondary.findIgnoreUnknownProperties(ac);
- }
- return result;
- }
@Override
public Boolean isIgnorableType(AnnotatedClass ac)
@@ -196,6 +164,37 @@
return str;
}
+ @Override
+ @Deprecated // since 2.6
+ public String[] findPropertiesToIgnore(Annotated ac) {
+ String[] result = _primary.findPropertiesToIgnore(ac);
+ if (result == null) {
+ result = _secondary.findPropertiesToIgnore(ac);
+ }
+ return result;
+ }
+
+ @Override
+ @Deprecated // since 2.8
+ public String[] findPropertiesToIgnore(Annotated ac, boolean forSerialization) {
+ String[] result = _primary.findPropertiesToIgnore(ac, forSerialization);
+ if (result == null) {
+ result = _secondary.findPropertiesToIgnore(ac, forSerialization);
+ }
+ return result;
+ }
+
+ @Override
+ @Deprecated // since 2.8
+ public Boolean findIgnoreUnknownProperties(AnnotatedClass ac)
+ {
+ Boolean result = _primary.findIgnoreUnknownProperties(ac);
+ if (result == null) {
+ result = _secondary.findIgnoreUnknownProperties(ac);
+ }
+ return result;
+ }
+
/*
/******************************************************
/* Property auto-detection
@@ -217,8 +216,8 @@
/******************************************************
/* Type handling
/******************************************************
- */
-
+ */
+
@Override
public TypeResolverBuilder<?> findTypeResolver(MapperConfig<?> config,
AnnotatedClass ac, JavaType baseType)
@@ -274,8 +273,11 @@
}
return name;
}
-
- // // // General member (field, method/constructor) annotations
+ /*
+ /******************************************************
+ /* General member (field, method/constructor) annotations
+ /******************************************************
+ */
@Override
public ReferenceProperty findReferenceType(AnnotatedMember member) {
@@ -290,50 +292,69 @@
}
@Override
- public Object findInjectableValueId(AnnotatedMember m) {
- Object r = _primary.findInjectableValueId(m);
- return (r == null) ? _secondary.findInjectableValueId(m) : r;
+ public JacksonInject.Value findInjectableValue(AnnotatedMember m) {
+ JacksonInject.Value r = _primary.findInjectableValue(m);
+ return (r == null) ? _secondary.findInjectableValue(m) : r;
}
@Override
public boolean hasIgnoreMarker(AnnotatedMember m) {
return _primary.hasIgnoreMarker(m) || _secondary.hasIgnoreMarker(m);
}
-
+
@Override
public Boolean hasRequiredMarker(AnnotatedMember m) {
Boolean r = _primary.hasRequiredMarker(m);
return (r == null) ? _secondary.hasRequiredMarker(m) : r;
}
-
+
+ @Override
+ @Deprecated // since 2.9
+ public Object findInjectableValueId(AnnotatedMember m) {
+ Object r = _primary.findInjectableValueId(m);
+ return (r == null) ? _secondary.findInjectableValueId(m) : r;
+ }
+
// // // Serialization: general annotations
@Override
public Object findSerializer(Annotated am) {
Object r = _primary.findSerializer(am);
- return _isExplicitClassOrOb(r, JsonSerializer.None.class)
- ? r : _secondary.findSerializer(am);
+ if (_isExplicitClassOrOb(r, JsonSerializer.None.class)) {
+ return r;
+ }
+ return _explicitClassOrOb(_secondary.findSerializer(am),
+ JsonSerializer.None.class);
}
@Override
public Object findKeySerializer(Annotated a) {
Object r = _primary.findKeySerializer(a);
- return _isExplicitClassOrOb(r, JsonSerializer.None.class)
- ? r : _secondary.findKeySerializer(a);
+ if (_isExplicitClassOrOb(r, JsonSerializer.None.class)) {
+ return r;
+ }
+ return _explicitClassOrOb(_secondary.findKeySerializer(a),
+ JsonSerializer.None.class);
}
@Override
public Object findContentSerializer(Annotated a) {
Object r = _primary.findContentSerializer(a);
- return _isExplicitClassOrOb(r, JsonSerializer.None.class)
- ? r : _secondary.findContentSerializer(a);
+ if (_isExplicitClassOrOb(r, JsonSerializer.None.class)) {
+ return r;
+ }
+ return _explicitClassOrOb(_secondary.findContentSerializer(a),
+ JsonSerializer.None.class);
}
@Override
public Object findNullSerializer(Annotated a) {
Object r = _primary.findNullSerializer(a);
- return _isExplicitClassOrOb(r, JsonSerializer.None.class)
- ? r : _secondary.findNullSerializer(a);
+ if (_isExplicitClassOrOb(r, JsonSerializer.None.class)) {
+ return r;
+ }
+ return _explicitClassOrOb(_secondary.findNullSerializer(a),
+ JsonSerializer.None.class);
}
@Deprecated
@@ -464,9 +485,15 @@
}
@Override
- public String findImplicitPropertyName(AnnotatedMember param) {
- String r = _primary.findImplicitPropertyName(param);
- return (r == null) ? _secondary.findImplicitPropertyName(param) : r;
+ public String findImplicitPropertyName(AnnotatedMember ann) {
+ String r = _primary.findImplicitPropertyName(ann);
+ return (r == null) ? _secondary.findImplicitPropertyName(ann) : r;
+ }
+
+ @Override
+ public List<PropertyName> findPropertyAliases(Annotated ann) {
+ List<PropertyName> r = _primary.findPropertyAliases(ann);
+ return (r == null) ? _secondary.findPropertyAliases(ann) : r;
}
@Override
@@ -562,18 +589,24 @@
}
return n;
}
-
+
@Override
- public boolean hasAsValueAnnotation(AnnotatedMethod am) {
- return _primary.hasAsValueAnnotation(am) || _secondary.hasAsValueAnnotation(am);
+ public Boolean hasAsValue(Annotated a) {
+ Boolean b = _primary.hasAsValue(a);
+ if (b == null) {
+ b = _secondary.hasAsValue(a);
+ }
+ return b;
}
@Override
- @Deprecated
- public String findEnumValue(Enum<?> value) {
- String r = _primary.findEnumValue(value);
- return (r == null) ? _secondary.findEnumValue(value) : r;
- }
+ public Boolean hasAnyGetter(Annotated a) {
+ Boolean b = _primary.hasAnyGetter(a);
+ if (b == null) {
+ b = _secondary.hasAnyGetter(a);
+ }
+ return b;
+ }
@Override
public String[] findEnumValues(Class<?> enumType, Enum<?>[] enumValues, String[] names) {
@@ -589,27 +622,56 @@
return (en == null) ? _secondary.findDefaultEnumValue(enumCls) : en;
}
- // // // Deserialization: general annotations
+ @Override
+ @Deprecated // since 2.8
+ public String findEnumValue(Enum<?> value) {
+ String r = _primary.findEnumValue(value);
+ return (r == null) ? _secondary.findEnumValue(value) : r;
+ }
@Override
- public Object findDeserializer(Annotated am) {
- Object r = _primary.findDeserializer(am);
- return _isExplicitClassOrOb(r, JsonDeserializer.None.class)
- ? r : _secondary.findDeserializer(am);
+ @Deprecated // since 2.9
+ public boolean hasAsValueAnnotation(AnnotatedMethod am) {
+ return _primary.hasAsValueAnnotation(am) || _secondary.hasAsValueAnnotation(am);
}
@Override
- public Object findKeyDeserializer(Annotated am) {
- Object r = _primary.findKeyDeserializer(am);
- return _isExplicitClassOrOb(r, KeyDeserializer.None.class)
- ? r : _secondary.findKeyDeserializer(am);
+ @Deprecated // since 2.9
+ public boolean hasAnyGetterAnnotation(AnnotatedMethod am) {
+ return _primary.hasAnyGetterAnnotation(am) || _secondary.hasAnyGetterAnnotation(am);
+ }
+
+ // // // Deserialization: general annotations
+
+ @Override
+ public Object findDeserializer(Annotated a) {
+ Object r = _primary.findDeserializer(a);
+ if (_isExplicitClassOrOb(r, JsonDeserializer.None.class)) {
+ return r;
+ }
+ return _explicitClassOrOb(_secondary.findDeserializer(a),
+ JsonDeserializer.None.class);
+ }
+
+ @Override
+ public Object findKeyDeserializer(Annotated a) {
+ Object r = _primary.findKeyDeserializer(a);
+ if (_isExplicitClassOrOb(r, KeyDeserializer.None.class)) {
+ return r;
+ }
+ return _explicitClassOrOb(_secondary.findKeyDeserializer(a),
+ KeyDeserializer.None.class);
}
@Override
public Object findContentDeserializer(Annotated am) {
Object r = _primary.findContentDeserializer(am);
- return _isExplicitClassOrOb(r, JsonDeserializer.None.class)
- ? r : _secondary.findContentDeserializer(am);
+ if (_isExplicitClassOrOb(r, JsonDeserializer.None.class)) {
+ return r;
+ }
+ return _explicitClassOrOb(_secondary.findContentDeserializer(am),
+ JsonDeserializer.None.class);
+
}
@Override
@@ -675,7 +737,7 @@
JsonPOJOBuilder.Value result = _primary.findPOJOBuilderConfig(ac);
return (result == null) ? _secondary.findPOJOBuilderConfig(ac) : result;
}
-
+
// // // Deserialization: method annotations
@Override
@@ -693,23 +755,41 @@
}
return n;
}
-
+
@Override
- public boolean hasAnySetterAnnotation(AnnotatedMethod am) {
- return _primary.hasAnySetterAnnotation(am) || _secondary.hasAnySetterAnnotation(am);
+ public Boolean hasAnySetter(Annotated a) {
+ Boolean b = _primary.hasAnySetter(a);
+ if (b == null) {
+ b = _secondary.hasAnySetter(a);
+ }
+ return b;
}
@Override
- public boolean hasAnyGetterAnnotation(AnnotatedMethod am) {
- return _primary.hasAnyGetterAnnotation(am) || _secondary.hasAnyGetterAnnotation(am);
+ public JsonSetter.Value findSetterInfo(Annotated a) {
+ JsonSetter.Value v2 = _secondary.findSetterInfo(a);
+ JsonSetter.Value v1 = _primary.findSetterInfo(a);
+ return (v2 == null) // shouldn't occur but
+ ? v1 : v2.withOverrides(v1);
}
-
+
+ @Override // since 2.9
+ public Boolean findMergeInfo(Annotated a) {
+ Boolean b = _primary.findMergeInfo(a);
+ if (b == null) {
+ b = _secondary.findMergeInfo(a);
+ }
+ return b;
+ }
+
@Override
+ @Deprecated // since 2.9
public boolean hasCreatorAnnotation(Annotated a) {
return _primary.hasCreatorAnnotation(a) || _secondary.hasCreatorAnnotation(a);
}
@Override
+ @Deprecated // since 2.9
public JsonCreator.Mode findCreatorBinding(Annotated a) {
JsonCreator.Mode mode = _primary.findCreatorBinding(a);
if (mode != null) {
@@ -717,15 +797,37 @@
}
return _secondary.findCreatorBinding(a);
}
-
+
+ @Override
+ public JsonCreator.Mode findCreatorAnnotation(MapperConfig<?> config, Annotated a) {
+ JsonCreator.Mode mode = _primary.findCreatorAnnotation(config, a);
+ return (mode == null) ? _secondary.findCreatorAnnotation(config, a) : mode;
+ }
+
+ @Override
+ @Deprecated // since 2.9
+ public boolean hasAnySetterAnnotation(AnnotatedMethod am) {
+ return _primary.hasAnySetterAnnotation(am) || _secondary.hasAnySetterAnnotation(am);
+ }
+
protected boolean _isExplicitClassOrOb(Object maybeCls, Class<?> implicit) {
- if (maybeCls == null) {
+ if ((maybeCls == null) || (maybeCls == implicit)) {
return false;
}
- if (!(maybeCls instanceof Class<?>)) {
- return true;
+ if (maybeCls instanceof Class<?>) {
+ return !ClassUtil.isBogusClass((Class<?>) maybeCls);
}
- Class<?> cls = (Class<?>) maybeCls;
- return (cls != implicit && !ClassUtil.isBogusClass(cls));
+ return true;
+ }
+
+ // @since 2.9
+ protected Object _explicitClassOrOb(Object maybeCls, Class<?> implicit) {
+ if ((maybeCls == null) || (maybeCls == implicit)) {
+ return null;
+ }
+ if ((maybeCls instanceof Class<?>) && ClassUtil.isBogusClass((Class<?>) maybeCls)) {
+ return null;
+ }
+ return maybeCls;
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationMap.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationMap.java
index 26b31cd..8ae39b6 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationMap.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotationMap.java
@@ -16,11 +16,23 @@
protected HashMap<Class<?>,Annotation> _annotations;
public AnnotationMap() { }
-
- private AnnotationMap(HashMap<Class<?>,Annotation> a) {
+
+ public static AnnotationMap of(Class<?> type, Annotation value) {
+ HashMap<Class<?>,Annotation> ann = new HashMap<>(4);
+ ann.put(type, value);
+ return new AnnotationMap(ann);
+ }
+
+ AnnotationMap(HashMap<Class<?>,Annotation> a) {
_annotations = a;
}
+ /*
+ /**********************************************************
+ /* Annotations impl
+ /**********************************************************
+ */
+
@SuppressWarnings("unchecked")
@Override
public <A extends Annotation> A get(Class<A> cls)
@@ -31,6 +43,7 @@
return (A) _annotations.get(cls);
}
+ @Override
public boolean has(Class<?> cls)
{
if (_annotations == null) {
@@ -45,6 +58,7 @@
*
* @since 2.7
*/
+ @Override
public boolean hasOneOf(Class<? extends Annotation>[] annoClasses) {
if (_annotations != null) {
for (int i = 0, end = annoClasses.length; i < end; ++i) {
@@ -56,6 +70,12 @@
return false;
}
+ /*
+ /**********************************************************
+ /* Other API
+ /**********************************************************
+ */
+
/**
* @since 2.3
*/
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/BasicBeanDescription.java b/src/main/java/com/fasterxml/jackson/databind/introspect/BasicBeanDescription.java
index fd2f0d4..c039e2a 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/BasicBeanDescription.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/BasicBeanDescription.java
@@ -4,6 +4,7 @@
import java.lang.reflect.Method;
import java.util.*;
+import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -26,6 +27,9 @@
*/
public class BasicBeanDescription extends BeanDescription
{
+ // since 2.9
+ private final static Class<?>[] NO_VIEWS = new Class<?>[0];
+
/*
/**********************************************************
/* General configuration
@@ -42,12 +46,28 @@
final protected MapperConfig<?> _config;
final protected AnnotationIntrospector _annotationIntrospector;
+
+ /*
+ /**********************************************************
+ /* Information about type itself
+ /**********************************************************
+ */
/**
* Information collected about the class introspected.
*/
final protected AnnotatedClass _classInfo;
+ /**
+ * @since 2.9
+ */
+ protected Class<?>[] _defaultViews;
+
+ /**
+ * @since 2.9
+ */
+ protected boolean _defaultViewsResolved;
+
/*
/**********************************************************
/* Member information
@@ -220,11 +240,18 @@
}
@Override
+ @Deprecated // since 2.9
public AnnotatedMethod findJsonValueMethod() {
return (_propCollector == null) ? null
: _propCollector.getJsonValueMethod();
}
+ @Override // since 2.9
+ public AnnotatedMember findJsonValueAccessor() {
+ return (_propCollector == null) ? null
+ : _propCollector.getJsonValueAccessor();
+ }
+
@Override
public Set<String> getIgnoredPropertyNames() {
Set<String> ign = (_propCollector == null) ? null
@@ -266,25 +293,39 @@
}
@Override
- public AnnotatedMethod findAnySetter() throws IllegalArgumentException
+ public AnnotatedMember findAnySetterAccessor() throws IllegalArgumentException
{
- AnnotatedMethod anySetter = (_propCollector == null) ? null
- : _propCollector.getAnySetterMethod();
- if (anySetter != null) {
- /* Also, let's be somewhat strict on how field name is to be
- * passed; String, Object make sense, others not
- * so much.
- */
- /* !!! 18-May-2009, tatu: how about enums? Can add support if
- * requested; easy enough for devs to add support within
- * method.
- */
- Class<?> type = anySetter.getRawParameterType(0);
- if (type != String.class && type != Object.class) {
- throw new IllegalArgumentException("Invalid 'any-setter' annotation on method "+anySetter.getName()+"(): first argument not of type String or Object, but "+type.getName());
+ if (_propCollector != null) {
+ AnnotatedMethod anyMethod = _propCollector.getAnySetterMethod();
+ if (anyMethod != null) {
+ // Also, let's be somewhat strict on how field name is to be
+ // passed; String, Object make sense, others not so much.
+
+ /* !!! 18-May-2009, tatu: how about enums? Can add support if
+ * requested; easy enough for devs to add support within method.
+ */
+ Class<?> type = anyMethod.getRawParameterType(0);
+ if ((type != String.class) && (type != Object.class)) {
+ throw new IllegalArgumentException(String.format(
+"Invalid 'any-setter' annotation on method '%s()': first argument not of type String or Object, but %s",
+anyMethod.getName(), type.getName()));
+ }
+ return anyMethod;
+ }
+ AnnotatedMember anyField = _propCollector.getAnySetterField();
+ if (anyField != null) {
+ // For now let's require a Map; in future can add support for other
+ // types like perhaps Iterable<Map.Entry>?
+ Class<?> type = anyField.getRawType();
+ if (!Map.class.isAssignableFrom(type)) {
+ throw new IllegalArgumentException(String.format(
+"Invalid 'any-setter' annotation on field '%s': type is not instance of java.util.Map",
+anyField.getName()));
+ }
+ return anyField;
}
}
- return anySetter;
+ return null;
}
@Override
@@ -316,8 +357,8 @@
while (t.getCause() != null) {
t = t.getCause();
}
- if (t instanceof Error) throw (Error) t;
- if (t instanceof RuntimeException) throw (RuntimeException) t;
+ ClassUtil.throwIfError(t);
+ ClassUtil.throwIfRTE(t);
throw new IllegalArgumentException("Failed to instantiate bean of type "+_classInfo.getAnnotated().getName()+": ("+t.getClass().getName()+") "+t.getMessage(), t);
}
}
@@ -364,7 +405,25 @@
}
return defValue;
}
-
+
+ @Override // since 2.9
+ public Class<?>[] findDefaultViews()
+ {
+ if (!_defaultViewsResolved) {
+ _defaultViewsResolved = true;
+ Class<?>[] def = (_annotationIntrospector == null) ? null
+ : _annotationIntrospector.findViews(_classInfo);
+ // one more twist: if default inclusion disabled, need to force empty set of views
+ if (def == null) {
+ if (!_config.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)) {
+ def = NO_VIEWS;
+ }
+ }
+ _defaultViews = def;
+ }
+ return _defaultViews;
+ }
+
/*
/**********************************************************
/* Introspection for serialization
@@ -421,54 +480,41 @@
}
@Override
- public AnnotatedMember findAnySetterField() throws IllegalArgumentException {
- AnnotatedMember anySetter = (_propCollector == null) ? null : _propCollector.getAnySetterField();
- if (anySetter != null) {
- /*
- * For now let's require a Map; in future can add support for other
- * types like perhaps Iterable<Map.Entry>?
- */
- Class<?> type = anySetter.getRawType();
- if (!Map.class.isAssignableFrom(type)) {
- throw new IllegalArgumentException("Invalid 'any-setter' annotation on field " + anySetter.getName()
- + "(): type is not instance of java.util.Map");
- }
- }
- return anySetter;
- }
-
- @Override
- public Map<String,AnnotatedMember> findBackReferenceProperties()
+ public List<BeanPropertyDefinition> findBackReferences()
{
- HashMap<String,AnnotatedMember> result = null;
-// boolean hasIgnored = (_ignoredPropertyNames != null);
-
+ List<BeanPropertyDefinition> result = null;
+ HashSet<String> names = null;
for (BeanPropertyDefinition property : _properties()) {
- /* 23-Sep-2014, tatu: As per [databind#426], we _should_ try to avoid
- * calling accessor, as it triggers exception from seeming conflict.
- * But the problem is that _ignoredPropertyNames here only contains
- * ones ignored on per-property annotations, but NOT class annotations...
- * so commented out part does not work, alas
- */
- /*
- if (hasIgnored && _ignoredPropertyNames.contains(property.getName())) {
+ AnnotationIntrospector.ReferenceProperty refDef = property.findReferenceType();
+ if ((refDef == null) || !refDef.isBackReference()) {
continue;
}
- */
- AnnotatedMember am = property.getMutator();
- if (am == null) {
- continue;
- }
- AnnotationIntrospector.ReferenceProperty refDef = _annotationIntrospector.findReferenceType(am);
- if (refDef != null && refDef.isBackReference()) {
- if (result == null) {
- result = new HashMap<String,AnnotatedMember>();
- }
- String refName = refDef.getName();
- if (result.put(refName, am) != null) {
+ final String refName = refDef.getName();
+ if (result == null) {
+ result = new ArrayList<BeanPropertyDefinition>();
+ names = new HashSet<>();
+ names.add(refName);
+ } else {
+ if (!names.add(refName)) {
throw new IllegalArgumentException("Multiple back-reference properties with name '"+refName+"'");
}
}
+ result.add(property);
+ }
+ return result;
+ }
+
+ @Deprecated // since 2.9
+ @Override
+ public Map<String,AnnotatedMember> findBackReferenceProperties()
+ {
+ List<BeanPropertyDefinition> props = findBackReferences();
+ if (props == null) {
+ return null;
+ }
+ Map<String,AnnotatedMember> result = new HashMap<>();
+ for (BeanPropertyDefinition prop : props) {
+ result.put(prop.getName(), prop.getMutator());
}
return result;
}
@@ -483,7 +529,7 @@
public List<AnnotatedMethod> getFactoryMethods()
{
// must filter out anything that clearly is not a factory method
- List<AnnotatedMethod> candidates = _classInfo.getStaticMethods();
+ List<AnnotatedMethod> candidates = _classInfo.getFactoryMethods();
if (candidates.isEmpty()) {
return candidates;
}
@@ -520,7 +566,7 @@
public Method findFactoryMethod(Class<?>... expArgTypes)
{
// So, of all single-arg static methods:
- for (AnnotatedMethod am : _classInfo.getStaticMethods()) {
+ for (AnnotatedMethod am : _classInfo.getFactoryMethods()) {
// 24-Oct-2016, tatu: Better ensure it only takes 1 arg, no matter what
if (isFactoryMethod(am) && am.getParameterCount() == 1) {
// And must take one of expected arg types (or supertype)
@@ -538,9 +584,8 @@
protected boolean isFactoryMethod(AnnotatedMethod am)
{
- /* First: return type must be compatible with the introspected class
- * (i.e. allowed to be sub-class, although usually is the same class)
- */
+ // First: return type must be compatible with the introspected class
+ // (i.e. allowed to be sub-class, although usually is the same class)
Class<?> rt = am.getRawReturnType();
if (!getBeanClass().isAssignableFrom(rt)) {
return false;
@@ -549,7 +594,8 @@
* (a) marked with @JsonCreator annotation, or
* (b) "valueOf" (at this point, need not be public)
*/
- if (_annotationIntrospector.hasCreatorAnnotation(am)) {
+ JsonCreator.Mode mode = _annotationIntrospector.findCreatorAnnotation(this._config, am);
+ if ((mode != null) && (mode != JsonCreator.Mode.DISABLED)) {
return true;
}
final String name = am.getName();
@@ -666,7 +712,7 @@
*/
@SuppressWarnings("unchecked")
- public Converter<Object,Object> _createConverter(Object converterDef)
+ protected Converter<Object,Object> _createConverter(Object converterDef)
{
if (converterDef == null) {
return null;
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/BasicClassIntrospector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/BasicClassIntrospector.java
index 7d83043..e523adf 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/BasicClassIntrospector.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/BasicClassIntrospector.java
@@ -24,29 +24,28 @@
* This is strictly performance optimization to reduce what is
* usually one-time cost, but seems useful for some cases considering
* simplicity.
- *
+ *
* @since 2.4
*/
-
protected final static BasicBeanDescription STRING_DESC;
static {
- AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(String.class, null);
- STRING_DESC = BasicBeanDescription.forOtherUse(null, SimpleType.constructUnsafe(String.class), ac);
+ STRING_DESC = BasicBeanDescription.forOtherUse(null, SimpleType.constructUnsafe(String.class),
+ AnnotatedClassResolver.createPrimordial(String.class));
}
protected final static BasicBeanDescription BOOLEAN_DESC;
static {
- AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(Boolean.TYPE, null);
- BOOLEAN_DESC = BasicBeanDescription.forOtherUse(null, SimpleType.constructUnsafe(Boolean.TYPE), ac);
+ BOOLEAN_DESC = BasicBeanDescription.forOtherUse(null, SimpleType.constructUnsafe(Boolean.TYPE),
+ AnnotatedClassResolver.createPrimordial(Boolean.TYPE));
}
protected final static BasicBeanDescription INT_DESC;
static {
- AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(Integer.TYPE, null);
- INT_DESC = BasicBeanDescription.forOtherUse(null, SimpleType.constructUnsafe(Integer.TYPE), ac);
+ INT_DESC = BasicBeanDescription.forOtherUse(null, SimpleType.constructUnsafe(Integer.TYPE),
+ AnnotatedClassResolver.createPrimordial(Integer.TYPE));
}
protected final static BasicBeanDescription LONG_DESC;
static {
- AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(Long.TYPE, null);
- LONG_DESC = BasicBeanDescription.forOtherUse(null, SimpleType.constructUnsafe(Long.TYPE), ac);
+ LONG_DESC = BasicBeanDescription.forOtherUse(null, SimpleType.constructUnsafe(Long.TYPE),
+ AnnotatedClassResolver.createPrimordial(Long.TYPE));
}
/*
@@ -55,9 +54,6 @@
/**********************************************************
*/
- @Deprecated // since 2.5: construct instance directly
- public final static BasicClassIntrospector instance = new BasicClassIntrospector();
-
/**
* Looks like 'forClassAnnotations()' gets called so frequently that we
* should consider caching to avoid some of the lookups.
@@ -84,7 +80,7 @@
// minor optimization: for some JDK types do minimal introspection
BasicBeanDescription desc = _findStdTypeDesc(type);
if (desc == null) {
- // As per [Databind#550], skip full introspection for some of standard
+ // As per [databind#550], skip full introspection for some of standard
// structured types as well
desc = _findStdJdkCollectionDesc(cfg, type);
if (desc == null) {
@@ -157,8 +153,8 @@
if (desc == null) {
desc = _cachedFCA.get(type);
if (desc == null) {
- AnnotatedClass ac = AnnotatedClass.construct(type, config, r);
- desc = BasicBeanDescription.forOtherUse(config, type, ac);
+ desc = BasicBeanDescription.forOtherUse(config, type,
+ _resolveAnnotatedClass(config, type, r));
_cachedFCA.put(type, desc);
}
}
@@ -171,12 +167,12 @@
{
BasicBeanDescription desc = _findStdTypeDesc(type);
if (desc == null) {
- AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(type.getRawClass(), config, r);
- desc = BasicBeanDescription.forOtherUse(config, type, ac);
+ desc = BasicBeanDescription.forOtherUse(config, type,
+ _resolveAnnotatedWithoutSuperTypes(config, type, r));
}
return desc;
}
-
+
/*
/**********************************************************
/* Overridable helper methods
@@ -187,18 +183,18 @@
JavaType type, MixInResolver r, boolean forSerialization,
String mutatorPrefix)
{
- AnnotatedClass ac = AnnotatedClass.construct(type, config, r);
- return constructPropertyCollector(config, ac, type, forSerialization, mutatorPrefix);
+ return constructPropertyCollector(config,
+ _resolveAnnotatedClass(config, type, r),
+ type, forSerialization, mutatorPrefix);
}
-
+
protected POJOPropertiesCollector collectPropertiesWithBuilder(MapperConfig<?> config,
JavaType type, MixInResolver r, boolean forSerialization)
{
- boolean useAnnotations = config.isAnnotationProcessingEnabled();
- AnnotationIntrospector ai = useAnnotations ? config.getAnnotationIntrospector() : null;
- AnnotatedClass ac = AnnotatedClass.construct(type, config, r);
+ AnnotatedClass ac = _resolveAnnotatedClass(config, type, r);
+ AnnotationIntrospector ai = config.isAnnotationProcessingEnabled() ? config.getAnnotationIntrospector() : null;
JsonPOJOBuilder.Value builderConfig = (ai == null) ? null : ai.findPOJOBuilderConfig(ac);
- String mutatorPrefix = (builderConfig == null) ? "with" : builderConfig.withPrefix;
+ String mutatorPrefix = (builderConfig == null) ? JsonPOJOBuilder.DEFAULT_WITH_PREFIX : builderConfig.withPrefix;
return constructPropertyCollector(config, ac, type, forSerialization, mutatorPrefix);
}
@@ -211,7 +207,7 @@
{
return new POJOPropertiesCollector(config, forSerialization, type, ac, mutatorPrefix);
}
-
+
/**
* Method called to see if type is one of core JDK types
* that we have cached for efficiency.
@@ -267,9 +263,25 @@
protected BasicBeanDescription _findStdJdkCollectionDesc(MapperConfig<?> cfg, JavaType type)
{
if (_isStdJDKCollection(type)) {
- AnnotatedClass ac = AnnotatedClass.construct(type, cfg);
- return BasicBeanDescription.forOtherUse(cfg, type, ac);
+ return BasicBeanDescription.forOtherUse(cfg, type,
+ _resolveAnnotatedClass(cfg, type, cfg));
}
return null;
}
+
+ /**
+ * @since 2.9
+ */
+ protected AnnotatedClass _resolveAnnotatedClass(MapperConfig<?> config,
+ JavaType type, MixInResolver r) {
+ return AnnotatedClassResolver.resolve(config, type, r);
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected AnnotatedClass _resolveAnnotatedWithoutSuperTypes(MapperConfig<?> config,
+ JavaType type, MixInResolver r) {
+ return AnnotatedClassResolver.resolveWithoutSuperTypes(config, type, r);
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/BeanPropertyDefinition.java b/src/main/java/com/fasterxml/jackson/databind/introspect/BeanPropertyDefinition.java
index 4039230..caa2663 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/BeanPropertyDefinition.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/BeanPropertyDefinition.java
@@ -48,7 +48,7 @@
/*
/**********************************************************
- /* Basic property information, name, type
+ /* Property name information
/**********************************************************
*/
@@ -83,15 +83,6 @@
public abstract PropertyName getWrapperName();
/**
- * Method for accessing additional metadata.
- * NOTE: will never return null, so de-referencing return value
- * is safe.
- *
- * @since 2.3
- */
- public abstract PropertyMetadata getMetadata();
-
- /**
* Accessor that can be called to check whether property was included
* due to an explicit marker (usually annotation), or just by naming
* convention.
@@ -116,7 +107,42 @@
public boolean isExplicitlyNamed() {
return isExplicitlyIncluded();
}
+
+ /*
+ /**********************************************************
+ /* Basic property metadata
+ /**********************************************************
+ */
+
+ /**
+ * @since 2.9
+ */
+ public abstract JavaType getPrimaryType();
+
+ /**
+ * @since 2.9
+ */
+ public abstract Class<?> getRawPrimaryType();
+ /**
+ * Method for accessing additional metadata.
+ * NOTE: will never return null, so de-referencing return value
+ * is safe.
+ *
+ * @since 2.3
+ */
+ public abstract PropertyMetadata getMetadata();
+
+ /**
+ * Method used to check if this property is expected to have a value;
+ * and if none found, should either be considered invalid (and most likely
+ * fail deserialization), or handled by other means (by providing default
+ * value)
+ */
+ public boolean isRequired() {
+ return getMetadata().isRequired();
+ }
+
/*
/**********************************************************
/* Capabilities
@@ -157,20 +183,42 @@
* value of the property.
* Null if no such member exists.
*/
- public abstract AnnotatedMember getAccessor();
+ public AnnotatedMember getAccessor()
+ {
+ AnnotatedMember m = getGetter();
+ if (m == null) {
+ m = getField();
+ }
+ return m;
+ }
/**
* Method used to find mutator (constructor parameter, setter, field) to use for
* changing value of the property.
* Null if no such member exists.
*/
- public abstract AnnotatedMember getMutator();
+ public AnnotatedMember getMutator() {
+ AnnotatedMember acc = getConstructorParameter();
+ if (acc == null) {
+ acc = getSetter();
+ if (acc == null) {
+ acc = getField();
+ }
+ }
+ return acc;
+ }
/**
* @since 2.3
*/
- public abstract AnnotatedMember getNonConstructorMutator();
-
+ public AnnotatedMember getNonConstructorMutator() {
+ AnnotatedMember m = getSetter();
+ if (m == null) {
+ m = getField();
+ }
+ return m;
+ }
+
/**
* Method used to find the property member (getter, setter, field) that has
* the highest precedence in current context (getter method when serializing,
@@ -185,12 +233,12 @@
/*
/**********************************************************
/* More refined access to configuration features
- /* (usually based on annotations)
+ /* (usually based on annotations and/or config overrides)
/* Since most trivial implementations do not support
/* these methods, they are implemented as no-ops.
/**********************************************************
*/
-
+
/**
* Method used to find View-inclusion definitions for the property.
*/
@@ -203,6 +251,14 @@
public AnnotationIntrospector.ReferenceProperty findReferenceType() { return null; }
/**
+ * @since 2.9
+ */
+ public String findReferenceName() {
+ AnnotationIntrospector.ReferenceProperty ref = findReferenceType();
+ return (ref == null) ? null : ref.getName();
+ }
+
+ /**
* Method used to check whether this logical property has a marker
* to indicate it should be used as the type id for polymorphic type
* handling.
@@ -215,17 +271,6 @@
* (or, when multiple references exist, all but first AS Object Identifier).
*/
public ObjectIdInfo findObjectIdInfo() { return null; }
-
- /**
- * Method used to check if this property is expected to have a value;
- * and if none found, should either be considered invalid (and most likely
- * fail deserialization), or handled by other means (by providing default
- * value)
- */
- public boolean isRequired() {
- PropertyMetadata md = getMetadata();
- return (md != null) && md.isRequired();
- }
/**
* Method used to check if this property has specific inclusion override
@@ -235,7 +280,5 @@
*
* @since 2.5
*/
- public JsonInclude.Value findInclusion() {
- return EMPTY_INCLUDE;
- }
+ public abstract JsonInclude.Value findInclusion();
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/ClassIntrospector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/ClassIntrospector.java
index 9bd3f29..c91a5bf 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/ClassIntrospector.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/ClassIntrospector.java
@@ -46,13 +46,13 @@
}
protected ClassIntrospector() { }
-
+
/*
/**********************************************************
/* Public API: factory methods
/**********************************************************
*/
-
+
/**
* Factory method that constructs an introspector that has all
* information needed for serialization purposes.
@@ -100,4 +100,3 @@
public abstract BeanDescription forDirectClassAnnotations(MapperConfig<?> cfg, JavaType type,
MixInResolver r);
}
-
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/CollectorBase.java b/src/main/java/com/fasterxml/jackson/databind/introspect/CollectorBase.java
new file mode 100644
index 0000000..c8608ae
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/CollectorBase.java
@@ -0,0 +1,121 @@
+package com.fasterxml.jackson.databind.introspect;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import com.fasterxml.jackson.databind.AnnotationIntrospector;
+import com.fasterxml.jackson.databind.util.ClassUtil;
+
+// @since 2.9
+class CollectorBase
+{
+ protected final static AnnotationMap[] NO_ANNOTATION_MAPS = new AnnotationMap[0];
+ protected final static Annotation[] NO_ANNOTATIONS = new Annotation[0];
+
+ protected final AnnotationIntrospector _intr;
+
+ protected CollectorBase(AnnotationIntrospector intr) {
+ _intr = intr;
+ }
+
+ // // // Annotation overrides ("mix over")
+
+ protected final AnnotationCollector collectAnnotations(Annotation[] anns) {
+ AnnotationCollector c = AnnotationCollector.emptyCollector();
+ for (int i = 0, end = anns.length; i < end; ++i) {
+ Annotation ann = anns[i];
+ c = c.addOrOverride(ann);
+ if (_intr.isAnnotationBundle(ann)) {
+ c = collectFromBundle(c, ann);
+ }
+ }
+ return c;
+ }
+
+ protected final AnnotationCollector collectAnnotations(AnnotationCollector c, Annotation[] anns) {
+ for (int i = 0, end = anns.length; i < end; ++i) {
+ Annotation ann = anns[i];
+ c = c.addOrOverride(ann);
+ if (_intr.isAnnotationBundle(ann)) {
+ c = collectFromBundle(c, ann);
+ }
+ }
+ return c;
+ }
+
+ protected final AnnotationCollector collectFromBundle(AnnotationCollector c, Annotation bundle) {
+ Annotation[] anns = ClassUtil.findClassAnnotations(bundle.annotationType());
+ for (int i = 0, end = anns.length; i < end; ++i) {
+ Annotation ann = anns[i];
+ // minor optimization: by-pass 2 common JDK meta-annotations
+ if (_ignorableAnnotation(ann)) {
+ continue;
+ }
+ if (_intr.isAnnotationBundle(ann)) {
+ // 11-Apr-2017, tatu: Also must guard against recursive definitions...
+ if (!c.isPresent(ann)) {
+ c = c.addOrOverride(ann);
+ c = collectFromBundle(c, ann);
+ }
+ } else {
+ c = c.addOrOverride(ann);
+ }
+ }
+ return c;
+ }
+
+ // // // Defaulting ("mix under")
+
+ // Variant that only adds annotations that are missing
+ protected final AnnotationCollector collectDefaultAnnotations(AnnotationCollector c, Annotation[] anns) {
+ for (int i = 0, end = anns.length; i < end; ++i) {
+ Annotation ann = anns[i];
+ if (!c.isPresent(ann)) {
+ c = c.addOrOverride(ann);
+ if (_intr.isAnnotationBundle(ann)) {
+ c = collectDefaultFromBundle(c, ann);
+ }
+ }
+ }
+ return c;
+ }
+
+ protected final AnnotationCollector collectDefaultFromBundle(AnnotationCollector c, Annotation bundle) {
+ Annotation[] anns = ClassUtil.findClassAnnotations(bundle.annotationType());
+ for (int i = 0, end = anns.length; i < end; ++i) {
+ Annotation ann = anns[i];
+ // minor optimization: by-pass 2 common JDK meta-annotations
+ if (_ignorableAnnotation(ann)) {
+ continue;
+ }
+ // also only defaulting, not overrides:
+ if (!c.isPresent(ann)) {
+ c = c.addOrOverride(ann);
+ if (_intr.isAnnotationBundle(ann)) {
+ c = collectFromBundle(c, ann);
+ }
+ }
+ }
+ return c;
+ }
+
+ protected final static boolean _ignorableAnnotation(Annotation a) {
+ return (a instanceof Target) || (a instanceof Retention);
+ }
+
+ static AnnotationMap _emptyAnnotationMap() {
+ return new AnnotationMap();
+ }
+
+ static AnnotationMap[] _emptyAnnotationMaps(int count) {
+ if (count == 0) {
+ return NO_ANNOTATION_MAPS;
+ }
+ AnnotationMap[] maps = new AnnotationMap[count];
+ for (int i = 0; i < count; ++i) {
+ maps[i] = _emptyAnnotationMap();
+ }
+ return maps;
+ }
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/ConcreteBeanPropertyBase.java b/src/main/java/com/fasterxml/jackson/databind/introspect/ConcreteBeanPropertyBase.java
index 7ea742b..7d2bc48 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/ConcreteBeanPropertyBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/ConcreteBeanPropertyBase.java
@@ -1,10 +1,14 @@
package com.fasterxml.jackson.databind.introspect;
+import java.util.Collections;
+import java.util.List;
+
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.PropertyMetadata;
+import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
/**
@@ -24,7 +28,7 @@
* @since 2.3
*/
protected final PropertyMetadata _metadata;
-
+
/**
* Lazily accessed value for per-property format override definition.
*
@@ -32,6 +36,11 @@
*/
protected transient JsonFormat.Value _propertyFormat;
+ /**
+ * @since 2.9
+ */
+ protected transient List<PropertyName> _aliases;
+
protected ConcreteBeanPropertyBase(PropertyMetadata md) {
_metadata = (md == null) ? PropertyMetadata.STD_REQUIRED_OR_OPTIONAL : md;
}
@@ -95,16 +104,37 @@
@Override
public JsonInclude.Value findPropertyInclusion(MapperConfig<?> config, Class<?> baseType)
{
- JsonInclude.Value v0 = config.getDefaultPropertyInclusion(baseType);
AnnotationIntrospector intr = config.getAnnotationIntrospector();
AnnotatedMember member = getMember();
- if ((intr == null) || (member == null)) {
+ if (member == null) {
+ JsonInclude.Value def = config.getDefaultPropertyInclusion(baseType);
+ return def;
+ }
+ JsonInclude.Value v0 = config.getDefaultInclusion(baseType, member.getRawType());
+ if (intr == null) {
return v0;
}
JsonInclude.Value v = intr.findPropertyInclusion(member);
- if (v == null) {
- return v0;
+ if (v0 == null) {
+ return v;
}
return v0.withOverrides(v);
}
+
+ @Override
+ public List<PropertyName> findAliases(MapperConfig<?> config)
+ {
+ List<PropertyName> aliases = _aliases;
+ if (aliases == null) {
+ AnnotationIntrospector intr = config.getAnnotationIntrospector();
+ if (intr != null) {
+ aliases = intr.findPropertyAliases(getMember());
+ }
+ if (aliases == null) {
+ aliases = Collections.emptyList();
+ }
+ _aliases = aliases;
+ }
+ return aliases;
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java
index 8417aa3..19f52c6 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java
@@ -19,6 +19,8 @@
import com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.AttributePropertyWriter;
import com.fasterxml.jackson.databind.ser.std.RawSerializer;
+import com.fasterxml.jackson.databind.type.MapLikeType;
+import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.*;
/**
@@ -53,7 +55,8 @@
JsonTypeInfo.class,
JsonUnwrapped.class,
JsonBackReference.class,
- JsonManagedReference.class
+ JsonManagedReference.class,
+ JsonMerge.class // since 2.9
};
// NOTE: loading of Java7 dependencies is encapsulated by handlers in Java7Support,
@@ -172,7 +175,7 @@
* explicit serialized name
*/
@Override
- @Deprecated
+ @Deprecated // since 2.8
public String findEnumValue(Enum<?> value)
{
// 11-Jun-2015, tatu: As per [databind#677], need to allow explicit naming.
@@ -270,39 +273,10 @@
{
JsonIgnoreProperties v = _findAnnotation(a, JsonIgnoreProperties.class);
if (v == null) {
- // could alternatively return `Value.empty()`?
- return null;
+ return JsonIgnoreProperties.Value.empty();
}
return JsonIgnoreProperties.Value.from(v);
}
-
- @Override // since 2.6
- @Deprecated // since 2.8
- public String[] findPropertiesToIgnore(Annotated a, boolean forSerialization) {
- JsonIgnoreProperties.Value v = findPropertyIgnorals(a);
- if (v == null) {
- return null;
- }
- // 13-May-2015, tatu: As per [databind#95], allow read-only/write-only props
- if (forSerialization) {
- if (v.getAllowGetters()) {
- return null;
- }
- } else {
- if (v.getAllowSetters()) {
- return null;
- }
- }
- Set<String> ignored = v.getIgnored();
- return ignored.toArray(new String[ignored.size()]);
- }
-
- @Override
- @Deprecated // since 2.8
- public Boolean findIgnoreUnknownProperties(AnnotatedClass a) {
- JsonIgnoreProperties.Value v = findPropertyIgnorals(a);
- return (v == null) ? null : v.getIgnoreUnknown();
- }
@Override
public Boolean isIgnorableType(AnnotatedClass ac) {
@@ -361,7 +335,25 @@
PropertyName n = _findConstructorName(m);
return (n == null) ? null : n.getSimpleName();
}
-
+
+ @Override
+ public List<PropertyName> findPropertyAliases(Annotated m) {
+ JsonAlias ann = _findAnnotation(m, JsonAlias.class);
+ if (ann == null) {
+ return null;
+ }
+ String[] strs = ann.value();
+ final int len = strs.length;
+ if (len == 0) {
+ return Collections.emptyList();
+ }
+ List<PropertyName> result = new ArrayList<>(len);
+ for (int i = 0; i < len; ++i) {
+ result.add(PropertyName.construct(strs[i]));
+ }
+ return result;
+ }
+
@Override
public boolean hasIgnoreMarker(AnnotatedMember m) {
return _isIgnorable(m);
@@ -449,29 +441,37 @@
return NameTransformer.simpleTransformer(prefix, suffix);
}
- @Override
- public Object findInjectableValueId(AnnotatedMember m)
- {
+ @Override // since 2.9
+ public JacksonInject.Value findInjectableValue(AnnotatedMember m) {
JacksonInject ann = _findAnnotation(m, JacksonInject.class);
if (ann == null) {
return null;
}
- /* Empty String means that we should use name of declared
- * value class.
- */
- String id = ann.value();
- if (id.length() == 0) {
+ // Empty String means that we should use name of declared value class.
+ JacksonInject.Value v = JacksonInject.Value.from(ann);
+ if (!v.hasId()) {
+ Object id;
// slight complication; for setters, type
if (!(m instanceof AnnotatedMethod)) {
- return m.getRawType().getName();
+ id = m.getRawType().getName();
+ } else {
+ AnnotatedMethod am = (AnnotatedMethod) m;
+ if (am.getParameterCount() == 0) { // getter
+ id = m.getRawType().getName();
+ } else { // setter
+ id = am.getRawParameterType(0).getName();
+ }
}
- AnnotatedMethod am = (AnnotatedMethod) m;
- if (am.getParameterCount() == 0) {
- return m.getRawType().getName();
- }
- return am.getRawParameterType(0).getName();
+ v = v.withId(id);
}
- return id;
+ return v;
+ }
+
+ @Override
+ @Deprecated // since 2.9
+ public Object findInjectableValueId(AnnotatedMember m) {
+ JacksonInject.Value v = findInjectableValue(m);
+ return (v == null) ? null : v.getId();
}
@Override
@@ -678,106 +678,39 @@
}
@Override
- @SuppressWarnings("deprecation")
- public JsonInclude.Include findSerializationInclusion(Annotated a, JsonInclude.Include defValue)
- {
- JsonInclude inc = _findAnnotation(a, JsonInclude.class);
- if (inc != null) {
- JsonInclude.Include v = inc.value();
- if (v != JsonInclude.Include.USE_DEFAULTS) {
- return v;
- }
- }
- JsonSerialize ann = _findAnnotation(a, JsonSerialize.class);
- if (ann != null) {
- JsonSerialize.Inclusion i2 = ann.include();
- switch (i2) {
- case ALWAYS:
- return JsonInclude.Include.ALWAYS;
- case NON_NULL:
- return JsonInclude.Include.NON_NULL;
- case NON_DEFAULT:
- return JsonInclude.Include.NON_DEFAULT;
- case NON_EMPTY:
- return JsonInclude.Include.NON_EMPTY;
- case DEFAULT_INCLUSION: // since 2.3 -- fall through, use default
- break;
- }
- }
- return defValue;
- }
-
- @Override
- @Deprecated
- public JsonInclude.Include findSerializationInclusionForContent(Annotated a, JsonInclude.Include defValue)
- {
- JsonInclude inc = _findAnnotation(a, JsonInclude.class);
- if (inc != null) {
- JsonInclude.Include incl = inc.content();
- if (incl != JsonInclude.Include.USE_DEFAULTS) {
- return incl;
- }
- }
- return defValue;
- }
-
- @Override
- @SuppressWarnings("deprecation")
public JsonInclude.Value findPropertyInclusion(Annotated a)
{
JsonInclude inc = _findAnnotation(a, JsonInclude.class);
- JsonInclude.Include valueIncl = (inc == null) ? JsonInclude.Include.USE_DEFAULTS : inc.value();
- if (valueIncl == JsonInclude.Include.USE_DEFAULTS) {
- JsonSerialize ann = _findAnnotation(a, JsonSerialize.class);
- if (ann != null) {
- JsonSerialize.Inclusion i2 = ann.include();
- switch (i2) {
- case ALWAYS:
- valueIncl = JsonInclude.Include.ALWAYS;
- break;
- case NON_NULL:
- valueIncl = JsonInclude.Include.NON_NULL;
- break;
- case NON_DEFAULT:
- valueIncl = JsonInclude.Include.NON_DEFAULT;
- break;
- case NON_EMPTY:
- valueIncl = JsonInclude.Include.NON_EMPTY;
- break;
- case DEFAULT_INCLUSION:
- default:
- }
+ JsonInclude.Value value = (inc == null) ? JsonInclude.Value.empty() : JsonInclude.Value.from(inc);
+
+ // only consider deprecated variant if we didn't have non-deprecated one:
+ if (value.getValueInclusion() == JsonInclude.Include.USE_DEFAULTS) {
+ value = _refinePropertyInclusion(a, value);
+ }
+ return value;
+ }
+
+ @SuppressWarnings("deprecation")
+ private JsonInclude.Value _refinePropertyInclusion(Annotated a, JsonInclude.Value value) {
+ JsonSerialize ann = _findAnnotation(a, JsonSerialize.class);
+ if (ann != null) {
+ switch (ann.include()) {
+ case ALWAYS:
+ return value.withValueInclusion(JsonInclude.Include.ALWAYS);
+ case NON_NULL:
+ return value.withValueInclusion(JsonInclude.Include.NON_NULL);
+ case NON_DEFAULT:
+ return value.withValueInclusion(JsonInclude.Include.NON_DEFAULT);
+ case NON_EMPTY:
+ return value.withValueInclusion(JsonInclude.Include.NON_EMPTY);
+ case DEFAULT_INCLUSION:
+ default:
}
}
- JsonInclude.Include contentIncl = (inc == null) ? JsonInclude.Include.USE_DEFAULTS : inc.content();
- return JsonInclude.Value.construct(valueIncl, contentIncl);
+ return value;
}
@Override
- @Deprecated
- public Class<?> findSerializationType(Annotated am)
- {
- JsonSerialize ann = _findAnnotation(am, JsonSerialize.class);
- return (ann == null) ? null : _classIfExplicit(ann.as());
- }
-
- @Override
- @Deprecated
- public Class<?> findSerializationKeyType(Annotated am, JavaType baseType)
- {
- JsonSerialize ann = _findAnnotation(am, JsonSerialize.class);
- return (ann == null) ? null : _classIfExplicit(ann.keyAs());
- }
-
- @Override
- @Deprecated
- public Class<?> findSerializationContentType(Annotated am, JavaType baseType)
- {
- JsonSerialize ann = _findAnnotation(am, JsonSerialize.class);
- return (ann == null) ? null : _classIfExplicit(ann.contentAs());
- }
-
- @Override
public JsonSerialize.Typing findSerializationTyping(Annotated a)
{
JsonSerialize ann = _findAnnotation(a, JsonSerialize.class);
@@ -798,6 +731,148 @@
/*
/**********************************************************
+ /* Serialization: type refinements
+ /**********************************************************
+ */
+
+ @Override
+ public JavaType refineSerializationType(final MapperConfig<?> config,
+ final Annotated a, final JavaType baseType) throws JsonMappingException
+ {
+ JavaType type = baseType;
+ final TypeFactory tf = config.getTypeFactory();
+
+ final JsonSerialize jsonSer = _findAnnotation(a, JsonSerialize.class);
+
+ // Ok: start by refining the main type itself; common to all types
+
+ final Class<?> serClass = (jsonSer == null) ? null : _classIfExplicit(jsonSer.as());
+ if (serClass != null) {
+ if (type.hasRawClass(serClass)) {
+ // 30-Nov-2015, tatu: As per [databind#1023], need to allow forcing of
+ // static typing this way
+ type = type.withStaticTyping();
+ } else {
+ Class<?> currRaw = type.getRawClass();
+ try {
+ // 11-Oct-2015, tatu: For deser, we call `TypeFactory.constructSpecializedType()`,
+ // may be needed here too in future?
+ if (serClass.isAssignableFrom(currRaw)) { // common case
+ type = tf.constructGeneralizedType(type, serClass);
+ } else if (currRaw.isAssignableFrom(serClass)) { // specialization, ok as well
+ type = tf.constructSpecializedType(type, serClass);
+ } else if (_primitiveAndWrapper(currRaw, serClass)) {
+ // 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements
+ type = type.withStaticTyping();
+ } else {
+ throw new JsonMappingException(null,
+ String.format("Can not refine serialization type %s into %s; types not related",
+ type, serClass.getName()));
+ }
+ } catch (IllegalArgumentException iae) {
+ throw new JsonMappingException(null,
+ String.format("Failed to widen type %s with annotation (value %s), from '%s': %s",
+ type, serClass.getName(), a.getName(), iae.getMessage()),
+ iae);
+ }
+ }
+ }
+ // Then further processing for container types
+
+ // First, key type (for Maps, Map-like types):
+ if (type.isMapLikeType()) {
+ JavaType keyType = type.getKeyType();
+ final Class<?> keyClass = (jsonSer == null) ? null : _classIfExplicit(jsonSer.keyAs());
+ if (keyClass != null) {
+ if (keyType.hasRawClass(keyClass)) {
+ keyType = keyType.withStaticTyping();
+ } else {
+ Class<?> currRaw = keyType.getRawClass();
+ try {
+ // 19-May-2016, tatu: As per [databind#1231], [databind#1178] may need to actually
+ // specialize (narrow) type sometimes, even if more commonly opposite
+ // is needed.
+ if (keyClass.isAssignableFrom(currRaw)) { // common case
+ keyType = tf.constructGeneralizedType(keyType, keyClass);
+ } else if (currRaw.isAssignableFrom(keyClass)) { // specialization, ok as well
+ keyType = tf.constructSpecializedType(keyType, keyClass);
+ } else if (_primitiveAndWrapper(currRaw, keyClass)) {
+ // 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements
+ keyType = keyType.withStaticTyping();
+ } else {
+ throw new JsonMappingException(null,
+ String.format("Can not refine serialization key type %s into %s; types not related",
+ keyType, keyClass.getName()));
+ }
+ } catch (IllegalArgumentException iae) {
+ throw new JsonMappingException(null,
+ String.format("Failed to widen key type of %s with concrete-type annotation (value %s), from '%s': %s",
+ type, keyClass.getName(), a.getName(), iae.getMessage()),
+ iae);
+ }
+ }
+ type = ((MapLikeType) type).withKeyType(keyType);
+ }
+ }
+
+ JavaType contentType = type.getContentType();
+ if (contentType != null) { // collection[like], map[like], array, reference
+ // And then value types for all containers:
+ final Class<?> contentClass = (jsonSer == null) ? null : _classIfExplicit(jsonSer.contentAs());
+ if (contentClass != null) {
+ if (contentType.hasRawClass(contentClass)) {
+ contentType = contentType.withStaticTyping();
+ } else {
+ // 03-Apr-2016, tatu: As per [databind#1178], may need to actually
+ // specialize (narrow) type sometimes, even if more commonly opposite
+ // is needed.
+ Class<?> currRaw = contentType.getRawClass();
+ try {
+ if (contentClass.isAssignableFrom(currRaw)) { // common case
+ contentType = tf.constructGeneralizedType(contentType, contentClass);
+ } else if (currRaw.isAssignableFrom(contentClass)) { // specialization, ok as well
+ contentType = tf.constructSpecializedType(contentType, contentClass);
+ } else if (_primitiveAndWrapper(currRaw, contentClass)) {
+ // 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements
+ contentType = contentType.withStaticTyping();
+ } else {
+ throw new JsonMappingException(null,
+ String.format("Can not refine serialization content type %s into %s; types not related",
+ contentType, contentClass.getName()));
+ }
+ } catch (IllegalArgumentException iae) { // shouldn't really happen
+ throw new JsonMappingException(null,
+ String.format("Internal error: failed to refine value type of %s with concrete-type annotation (value %s), from '%s': %s",
+ type, contentClass.getName(), a.getName(), iae.getMessage()),
+ iae);
+ }
+ }
+ type = type.withContentType(contentType);
+ }
+ }
+ return type;
+ }
+
+ @Override
+ @Deprecated // since 2.7
+ public Class<?> findSerializationType(Annotated am) {
+ return null;
+ }
+
+ @Override
+ @Deprecated // since 2.7
+ public Class<?> findSerializationKeyType(Annotated am, JavaType baseType) {
+ return null;
+ }
+
+ @Override
+ @Deprecated // since 2.7
+ public Class<?> findSerializationContentType(Annotated am, JavaType baseType) {
+ return null;
+ }
+
+ /*
+ /**********************************************************
/* Serialization: class annotations
/**********************************************************
*/
@@ -815,9 +890,8 @@
private final Boolean _findSortAlpha(Annotated ann) {
JsonPropertyOrder order = _findAnnotation(ann, JsonPropertyOrder.class);
- /* 23-Jun-2015, tatu: as per [databind#840], let's only consider
- * `true` to have any significance.
- */
+ // 23-Jun-2015, tatu: as per [databind#840], let's only consider
+ // `true` to have any significance.
if ((order != null) && order.alphabetic()) {
return Boolean.TRUE;
}
@@ -937,11 +1011,37 @@
return null;
}
+ @Override // since 2.9
+ public Boolean hasAsValue(Annotated a) {
+ JsonValue ann = _findAnnotation(a, JsonValue.class);
+ if (ann == null) {
+ return null;
+ }
+ return ann.value();
+ }
+
+ @Override // since 2.9
+ public Boolean hasAnyGetter(Annotated a) {
+ JsonAnyGetter ann = _findAnnotation(a, JsonAnyGetter.class);
+ if (ann == null) {
+ return null;
+ }
+ return ann.enabled();
+ }
+
@Override
+ @Deprecated // since 2.9
+ public boolean hasAnyGetterAnnotation(AnnotatedMethod am) {
+ // No dedicated disabling; regular @JsonIgnore used if needs to be ignored (handled separately)
+ return _hasAnnotation(am, JsonAnyGetter.class);
+ }
+
+ @Override
+ @Deprecated // since 2.9
public boolean hasAsValueAnnotation(AnnotatedMethod am) {
JsonValue ann = _findAnnotation(am, JsonValue.class);
// value of 'false' means disabled...
- return (ann != null && ann.value());
+ return (ann != null) && ann.value();
}
/*
@@ -1012,33 +1112,90 @@
*/
@Override
- @Deprecated
- public Class<?> findDeserializationContentType(Annotated am, JavaType baseContentType)
+ public JavaType refineDeserializationType(final MapperConfig<?> config,
+ final Annotated a, final JavaType baseType) throws JsonMappingException
{
- JsonDeserialize ann = _findAnnotation(am, JsonDeserialize.class);
- return (ann == null) ? null : _classIfExplicit(ann.contentAs());
- }
-
- @Deprecated
- @Override
- public Class<?> findDeserializationType(Annotated am, JavaType baseType) {
- JsonDeserialize ann = _findAnnotation(am, JsonDeserialize.class);
- return (ann == null) ? null : _classIfExplicit(ann.as());
+ JavaType type = baseType;
+ final TypeFactory tf = config.getTypeFactory();
+
+ final JsonDeserialize jsonDeser = _findAnnotation(a, JsonDeserialize.class);
+
+ // Ok: start by refining the main type itself; common to all types
+ final Class<?> valueClass = (jsonDeser == null) ? null : _classIfExplicit(jsonDeser.as());
+ if ((valueClass != null) && !type.hasRawClass(valueClass)
+ && !_primitiveAndWrapper(type, valueClass)) {
+ try {
+ type = tf.constructSpecializedType(type, valueClass);
+ } catch (IllegalArgumentException iae) {
+ throw new JsonMappingException(null,
+ String.format("Failed to narrow type %s with annotation (value %s), from '%s': %s",
+ type, valueClass.getName(), a.getName(), iae.getMessage()),
+ iae);
+ }
+ }
+ // Then further processing for container types
+
+ // First, key type (for Maps, Map-like types):
+ if (type.isMapLikeType()) {
+ JavaType keyType = type.getKeyType();
+ final Class<?> keyClass = (jsonDeser == null) ? null : _classIfExplicit(jsonDeser.keyAs());
+ if ((keyClass != null)
+ && !_primitiveAndWrapper(keyType, keyClass)) {
+ try {
+ keyType = tf.constructSpecializedType(keyType, keyClass);
+ type = ((MapLikeType) type).withKeyType(keyType);
+ } catch (IllegalArgumentException iae) {
+ throw new JsonMappingException(null,
+ String.format("Failed to narrow key type of %s with concrete-type annotation (value %s), from '%s': %s",
+ type, keyClass.getName(), a.getName(), iae.getMessage()),
+ iae);
+ }
+ }
+ }
+ JavaType contentType = type.getContentType();
+ if (contentType != null) { // collection[like], map[like], array, reference
+ // And then value types for all containers:
+ final Class<?> contentClass = (jsonDeser == null) ? null : _classIfExplicit(jsonDeser.contentAs());
+ if ((contentClass != null)
+ && !_primitiveAndWrapper(contentType, contentClass)) {
+ try {
+ contentType = tf.constructSpecializedType(contentType, contentClass);
+ type = type.withContentType(contentType);
+ } catch (IllegalArgumentException iae) {
+ throw new JsonMappingException(null,
+ String.format("Failed to narrow value type of %s with concrete-type annotation (value %s), from '%s': %s",
+ type, contentClass.getName(), a.getName(), iae.getMessage()),
+ iae);
+ }
+ }
+ }
+ return type;
}
@Override
- @Deprecated
- public Class<?> findDeserializationKeyType(Annotated am, JavaType baseKeyType) {
- JsonDeserialize ann = _findAnnotation(am, JsonDeserialize.class);
- return (ann == null) ? null : _classIfExplicit(ann.keyAs());
+ @Deprecated // since 2.7
+ public Class<?> findDeserializationContentType(Annotated am, JavaType baseContentType) {
+ return null;
}
-
+
+ @Override
+ @Deprecated // since 2.7
+ public Class<?> findDeserializationType(Annotated am, JavaType baseType) {
+ return null;
+ }
+
+ @Override
+ @Deprecated // since 2.7
+ public Class<?> findDeserializationKeyType(Annotated am, JavaType baseKeyType) {
+ return null;
+ }
+
/*
/**********************************************************
/* Deserialization: Class annotations
/**********************************************************
*/
-
+
@Override
public Object findValueInstantiator(AnnotatedClass ac)
{
@@ -1087,25 +1244,30 @@
}
@Override
- public boolean hasAnySetterAnnotation(AnnotatedMethod am)
- {
- /* No dedicated disabling; regular @JsonIgnore used
- * if needs to be ignored (and if so, is handled prior
- * to this method getting called)
- */
+ public Boolean hasAnySetter(Annotated a) {
+ JsonAnySetter ann = _findAnnotation(a, JsonAnySetter.class);
+ return (ann == null) ? null : ann.enabled();
+ }
+
+ @Override
+ public JsonSetter.Value findSetterInfo(Annotated a) {
+ return JsonSetter.Value.from(_findAnnotation(a, JsonSetter.class));
+ }
+
+ @Override // since 2.9
+ public Boolean findMergeInfo(Annotated a) {
+ JsonMerge ann = _findAnnotation(a, JsonMerge.class);
+ return (ann == null) ? null : ann.value().asBoolean();
+ }
+
+ @Override
+ @Deprecated // since 2.9
+ public boolean hasAnySetterAnnotation(AnnotatedMethod am) {
return _hasAnnotation(am, JsonAnySetter.class);
}
@Override
- public boolean hasAnyGetterAnnotation(AnnotatedMethod am)
- {
- /* No dedicated disabling; regular @JsonIgnore used
- * if needs to be ignored (handled separately
- */
- return _hasAnnotation(am, JsonAnyGetter.class);
- }
-
- @Override
+ @Deprecated // since 2.9
public boolean hasCreatorAnnotation(Annotated a)
{
/* No dedicated disabling; regular @JsonIgnore used if needs to be
@@ -1131,11 +1293,36 @@
}
@Override
+ @Deprecated // since 2.9
public JsonCreator.Mode findCreatorBinding(Annotated a) {
JsonCreator ann = _findAnnotation(a, JsonCreator.class);
return (ann == null) ? null : ann.mode();
}
+ @Override
+ public JsonCreator.Mode findCreatorAnnotation(MapperConfig<?> config, Annotated a) {
+ JsonCreator ann = _findAnnotation(a, JsonCreator.class);
+ if (ann != null) {
+ return ann.mode();
+ }
+
+ if (_cfgConstructorPropertiesImpliesCreator
+ && config.isEnabled(MapperFeature.INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES)
+ ) {
+ if (a instanceof AnnotatedConstructor) {
+ if (_java7Helper != null) {
+ Boolean b = _java7Helper.hasCreatorAnnotation(a);
+ if ((b != null) && b.booleanValue()) {
+ // 13-Sep-2016, tatu: Judgment call, but I don't think JDK ever implies
+ // use of delegate; assumes as-properties implicitly
+ return JsonCreator.Mode.PROPERTIES;
+ }
+ }
+ }
+ }
+ return null;
+ }
+
/*
/**********************************************************
/* Helper methods
@@ -1276,9 +1463,25 @@
return StdTypeResolverBuilder.noTypeInfoBuilder();
}
- /*
- /**********************************************************
- /* Helper classes
- /**********************************************************
- */
+ private boolean _primitiveAndWrapper(Class<?> baseType, Class<?> refinement)
+ {
+ if (baseType.isPrimitive()) {
+ return baseType == ClassUtil.primitiveType(refinement);
+ }
+ if (refinement.isPrimitive()) {
+ return refinement == ClassUtil.primitiveType(baseType);
+ }
+ return false;
+ }
+
+ private boolean _primitiveAndWrapper(JavaType baseType, Class<?> refinement)
+ {
+ if (baseType.isPrimitive()) {
+ return baseType.hasRawClass(ClassUtil.primitiveType(refinement));
+ }
+ if (refinement.isPrimitive()) {
+ return refinement == ClassUtil.primitiveType(baseType.getRawClass());
+ }
+ return false;
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/ObjectIdInfo.java b/src/main/java/com/fasterxml/jackson/databind/introspect/ObjectIdInfo.java
index 049ab35..0605830 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/ObjectIdInfo.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/ObjectIdInfo.java
@@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.ObjectIdResolver;
import com.fasterxml.jackson.annotation.SimpleObjectIdResolver;
import com.fasterxml.jackson.databind.PropertyName;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Container object that encapsulates information usually
@@ -30,17 +31,6 @@
this(name, scope, gen, false, resolver);
}
- @Deprecated // since 2.4
- public ObjectIdInfo(PropertyName name, Class<?> scope, Class<? extends ObjectIdGenerator<?>> gen)
- {
- this(name, scope, gen, false);
- }
-
- @Deprecated // since 2.3
- public ObjectIdInfo(String name, Class<?> scope, Class<? extends ObjectIdGenerator<?>> gen) {
- this(new PropertyName(name), scope, gen, false);
- }
-
protected ObjectIdInfo(PropertyName prop, Class<?> scope, Class<? extends ObjectIdGenerator<?>> gen,
boolean alwaysAsId)
{
@@ -81,8 +71,8 @@
@Override
public String toString() {
return "ObjectIdInfo: propName="+_propertyName
- +", scope="+(_scope == null ? "null" : _scope.getName())
- +", generatorType="+(_generator == null ? "null" : _generator.getName())
+ +", scope="+ClassUtil.nameOf(_scope)
+ +", generatorType="+ClassUtil.nameOf(_generator)
+", alwaysAsId="+_alwaysAsId;
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java
index fd6a613..65ad2d2 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java
@@ -3,9 +3,12 @@
import java.lang.reflect.Modifier;
import java.util.*;
-import com.fasterxml.jackson.annotation.JsonAnySetter;
-import com.fasterxml.jackson.annotation.JsonProperty.Access;
+import com.fasterxml.jackson.annotation.JacksonInject;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
import com.fasterxml.jackson.databind.*;
+
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.util.BeanUtil;
@@ -54,6 +57,11 @@
protected final AnnotationIntrospector _annotationIntrospector;
/**
+ * @since 2.9
+ */
+ protected final boolean _useAnnotations;
+
+ /**
* Prefix used by auto-detected mutators ("setters"): usually "set",
* but differs for builder objects ("with" by default).
*/
@@ -80,7 +88,7 @@
*/
protected LinkedHashMap<String, POJOPropertyBuilder> _properties;
- protected LinkedList<POJOPropertyBuilder> _creatorProperties ;
+ protected LinkedList<POJOPropertyBuilder> _creatorProperties;
protected LinkedList<AnnotatedMember> _anyGetters;
@@ -90,8 +98,10 @@
/**
* Method(s) marked with 'JsonValue' annotation
+ *<p>
+ * NOTE: before 2.9, was `AnnotatedMethod`; with 2.9 allows fields too
*/
- protected LinkedList<AnnotatedMethod> _jsonValueGetters;
+ protected LinkedList<AnnotatedMember> _jsonValueAccessors;
/**
* Lazily collected list of properties that can be implicitly
@@ -122,14 +132,15 @@
_type = type;
_classDef = classDef;
_mutatorPrefix = (mutatorPrefix == null) ? "set" : mutatorPrefix;
- _annotationIntrospector = config.isAnnotationProcessingEnabled() ?
- _config.getAnnotationIntrospector() : null;
- if (_annotationIntrospector == null) {
- _visibilityChecker = _config.getDefaultVisibilityChecker();
+ if (config.isAnnotationProcessingEnabled()) {
+ _useAnnotations = true;
+ _annotationIntrospector = _config.getAnnotationIntrospector();
} else {
- _visibilityChecker = _annotationIntrospector.findAutoDetectVisibility(classDef,
- _config.getDefaultVisibilityChecker());
+ _useAnnotations = false;
+ _annotationIntrospector = AnnotationIntrospector.nopInstance();
}
+ _visibilityChecker = _config.getDefaultVisibilityChecker(type.getRawClass(),
+ classDef);
}
/*
@@ -166,20 +177,33 @@
}
return _injectables;
}
-
- public AnnotatedMethod getJsonValueMethod()
+
+ @Deprecated // since 2.9
+ public AnnotatedMethod getJsonValueMethod() {
+ AnnotatedMember m = getJsonValueAccessor();
+ if (m instanceof AnnotatedMethod) {
+ return (AnnotatedMethod) m;
+ }
+ return null;
+ }
+
+ /**
+ * @since 2.9
+ */
+ public AnnotatedMember getJsonValueAccessor()
{
if (!_collected) {
collectAll();
}
// If @JsonValue defined, must have a single one
- if (_jsonValueGetters != null) {
- if (_jsonValueGetters.size() > 1) {
- reportProblem("Multiple value properties defined ("+_jsonValueGetters.get(0)+" vs "
- +_jsonValueGetters.get(1)+")");
+ if (_jsonValueAccessors != null) {
+ if (_jsonValueAccessors.size() > 1) {
+ reportProblem("Multiple 'as-value' properties defined (%s vs %s)",
+ _jsonValueAccessors.get(0),
+ _jsonValueAccessors.get(1));
}
// otherwise we won't greatly care
- return _jsonValueGetters.get(0);
+ return _jsonValueAccessors.get(0);
}
return null;
}
@@ -191,14 +215,14 @@
}
if (_anyGetters != null) {
if (_anyGetters.size() > 1) {
- reportProblem("Multiple 'any-getters' defined ("+_anyGetters.get(0)+" vs "
- +_anyGetters.get(1)+")");
+ reportProblem("Multiple 'any-getters' defined (%s vs %s)",
+ _anyGetters.get(0), _anyGetters.get(1));
}
return _anyGetters.getFirst();
}
return null;
}
-
+
public AnnotatedMember getAnySetterField()
{
if (!_collected) {
@@ -206,8 +230,8 @@
}
if (_anySetterField != null) {
if (_anySetterField.size() > 1) {
- reportProblem("Multiple 'any-Setters' defined ("+_anySetters.get(0)+" vs "
- +_anySetterField.get(1)+")");
+ reportProblem("Multiple 'any-setter' fields defined (%s vs %s)",
+ _anySetterField.get(0), _anySetterField.get(1));
}
return _anySetterField.getFirst();
}
@@ -221,8 +245,8 @@
}
if (_anySetters != null) {
if (_anySetters.size() > 1) {
- reportProblem("Multiple 'any-setters' defined ("+_anySetters.get(0)+" vs "
- +_anySetters.get(1)+")");
+ reportProblem("Multiple 'any-setter' methods defined (%s vs %s)",
+ _anySetters.get(0), _anySetters.get(1));
}
return _anySetters.getFirst();
}
@@ -243,9 +267,6 @@
*/
public ObjectIdInfo getObjectIdInfo()
{
- if (_annotationIntrospector == null) {
- return null;
- }
ObjectIdInfo info = _annotationIntrospector.findObjectIdInfo(_classDef);
if (info != null) { // 2.1: may also have different defaults for refs:
info = _annotationIntrospector.findObjectReferenceInfo(_classDef, info);
@@ -256,8 +277,7 @@
/**
* Method for finding Class to use as POJO builder, if any.
*/
- public Class<?> findPOJOBuilderClass()
- {
+ public Class<?> findPOJOBuilderClass() {
return _annotationIntrospector.findPOJOBuilder(_classDef);
}
@@ -276,20 +296,6 @@
*/
/**
- * Method that orchestrates collection activities, and needs to be called
- * after creating the instance.
- *<p>
- * Since 2.6 has become a no-op and actual collection is done more lazily
- * at point where properties are actually needed.
- *
- * @deprecated Since 2.6; no need to call
- */
- @Deprecated
- public POJOPropertiesCollector collect() {
- return this;
- }
-
- /**
* Internal method that will collect actual property information.
*
* @since 2.6
@@ -369,16 +375,29 @@
final boolean transientAsIgnoral = _config.isEnabled(MapperFeature.PROPAGATE_TRANSIENT_MARKER);
for (AnnotatedField f : _classDef.fields()) {
- String implName = (ai == null) ? null : ai.findImplicitPropertyName(f);
+ String implName = ai.findImplicitPropertyName(f);
+ // @JsonValue?
+ if (Boolean.TRUE.equals(ai.hasAsValue(f))) {
+ if (_jsonValueAccessors == null) {
+ _jsonValueAccessors = new LinkedList<>();
+ }
+ _jsonValueAccessors.add(f);
+ continue;
+ }
+ // @JsonAnySetter?
+ if (Boolean.TRUE.equals(ai.hasAnySetter(f))) {
+ if (_anySetterField == null) {
+ _anySetterField = new LinkedList<AnnotatedMember>();
+ }
+ _anySetterField.add(f);
+ continue;
+ }
if (implName == null) {
implName = f.getName();
}
-
PropertyName pn;
- if (ai == null) {
- pn = null;
- } else if (_forSerialization) {
+ if (_forSerialization) {
/* 18-Aug-2011, tatu: As per existing unit tests, we should only
* use serialization annotation (@JsonSerialize) when serializing
* fields, and similarly for deserialize-only annotations... so
@@ -401,7 +420,7 @@
visible = _visibilityChecker.isFieldVisible(f);
}
// and finally, may also have explicit ignoral
- boolean ignored = (ai != null) && ai.hasIgnoreMarker(f);
+ boolean ignored = ai.hasIgnoreMarker(f);
// 13-May-2015, tatu: Moved from earlier place (AnnotatedClass) in 2.6
if (f.isTransient()) {
@@ -419,17 +438,10 @@
* Also: if 'ignored' is set, need to included until a later point, to
* avoid losing ignoral information.
*/
- if (pruneFinalFields && (pn == null) && !ignored && Modifier.isFinal(f.getModifiers())) {
+ if (pruneFinalFields && (pn == null) && !ignored
+ && Modifier.isFinal(f.getModifiers())) {
continue;
}
-
- //if field has annotation @JsonAnySetter
- if(f.hasAnnotation(JsonAnySetter.class)) {
- if (_anySetterField == null) {
- _anySetterField = new LinkedList<AnnotatedMember>();
- }
- _anySetterField.add(f);
- }
_property(props, implName).addField(f, pn, nameExplicit, visible, ignored);
}
}
@@ -440,7 +452,7 @@
protected void _addCreators(Map<String, POJOPropertyBuilder> props)
{
// can be null if annotation processing is disabled...
- if (_annotationIntrospector == null) {
+ if (!_useAnnotations) {
return;
}
for (AnnotatedConstructor ctor : _classDef.getConstructors()) {
@@ -451,7 +463,7 @@
_addCreatorParam(props, ctor.getParameter(i));
}
}
- for (AnnotatedMethod factory : _classDef.getStaticMethods()) {
+ for (AnnotatedMethod factory : _classDef.getFactoryMethods()) {
if (_creatorProperties == null) {
_creatorProperties = new LinkedList<POJOPropertyBuilder>();
}
@@ -476,14 +488,14 @@
boolean expl = (pn != null && !pn.isEmpty());
if (!expl) {
if (impl.isEmpty()) {
- /* Important: if neither implicit nor explicit name, can not make use
- * of this creator parameter -- may or may not be a problem, verified
- * at a later point.
- */
+ // Important: if neither implicit nor explicit name, can not make use of
+ // this creator parameter -- may or may not be a problem, verified at a later point.
return;
}
// Also: if this occurs, there MUST be explicit annotation on creator itself
- if (!_annotationIntrospector.hasCreatorAnnotation(param.getOwner())) {
+ JsonCreator.Mode creatorMode = _annotationIntrospector.findCreatorAnnotation(_config,
+ param.getOwner());
+ if ((creatorMode == null) || (creatorMode == JsonCreator.Mode.DISABLED)) {
return;
}
pn = PropertyName.construct(impl);
@@ -501,14 +513,13 @@
prop.addCtor(param, pn, expl, true, false);
_creatorProperties.add(prop);
}
-
+
/**
* Method for collecting basic information on all fields found
*/
protected void _addMethods(Map<String, POJOPropertyBuilder> props)
{
final AnnotationIntrospector ai = _annotationIntrospector;
-
for (AnnotatedMethod m : _classDef.memberMethods()) {
/* For methods, handling differs between getters and setters; and
* we will also only consider entries that either follow the bean
@@ -521,11 +532,13 @@
} else if (argCount == 1) { // setters
_addSetterMethod(props, m, ai);
} else if (argCount == 2) { // any getter?
- if (ai != null && ai.hasAnySetterAnnotation(m)) {
- if (_anySetters == null) {
- _anySetters = new LinkedList<AnnotatedMethod>();
+ if (ai != null) {
+ if (Boolean.TRUE.equals(ai.hasAnySetter(m))) {
+ if (_anySetters == null) {
+ _anySetters = new LinkedList<AnnotatedMethod>();
+ }
+ _anySetters.add(m);
}
- _anySetters.add(m);
}
}
}
@@ -540,31 +553,30 @@
}
// any getter?
- if (ai != null) {
- if (ai.hasAnyGetterAnnotation(m)) {
- if (_anyGetters == null) {
- _anyGetters = new LinkedList<AnnotatedMember>();
- }
- _anyGetters.add(m);
- return;
+ // @JsonAnyGetter?
+ if (Boolean.TRUE.equals(ai.hasAnyGetter(m))) {
+ if (_anyGetters == null) {
+ _anyGetters = new LinkedList<AnnotatedMember>();
}
- // @JsonValue?
- if (ai.hasAsValueAnnotation(m)) {
- if (_jsonValueGetters == null) {
- _jsonValueGetters = new LinkedList<AnnotatedMethod>();
- }
- _jsonValueGetters.add(m);
- return;
+ _anyGetters.add(m);
+ return;
+ }
+ // @JsonValue?
+ if (Boolean.TRUE.equals(ai.hasAsValue(m))) {
+ if (_jsonValueAccessors == null) {
+ _jsonValueAccessors = new LinkedList<>();
}
+ _jsonValueAccessors.add(m);
+ return;
}
String implName; // from naming convention
boolean visible;
- PropertyName pn = (ai == null) ? null : ai.findNameForSerialization(m);
+ PropertyName pn = ai.findNameForSerialization(m);
boolean nameExplicit = (pn != null);
if (!nameExplicit) { // no explicit name; must consider implicit
- implName = (ai == null) ? null : ai.findImplicitPropertyName(m);
+ implName = ai.findImplicitPropertyName(m);
if (implName == null) {
implName = BeanUtil.okNameForRegularGetter(m, m.getName(), _stdBeanNaming);
}
@@ -579,7 +591,7 @@
}
} else { // explicit indication of inclusion, but may be empty
// we still need implicit name to link with other pieces
- implName = (ai == null) ? null : ai.findImplicitPropertyName(m);
+ implName = ai.findImplicitPropertyName(m);
if (implName == null) {
implName = BeanUtil.okNameForGetter(m, _stdBeanNaming);
}
@@ -594,7 +606,7 @@
}
visible = true;
}
- boolean ignore = (ai == null) ? false : ai.hasIgnoreMarker(m);
+ boolean ignore = ai.hasIgnoreMarker(m);
_property(props, implName).addGetter(m, pn, nameExplicit, visible, ignore);
}
@@ -634,43 +646,41 @@
boolean ignore = (ai == null) ? false : ai.hasIgnoreMarker(m);
_property(props, implName).addSetter(m, pn, nameExplicit, visible, ignore);
}
-
+
protected void _addInjectables(Map<String, POJOPropertyBuilder> props)
{
final AnnotationIntrospector ai = _annotationIntrospector;
- if (ai == null) {
- return;
- }
-
- // first fields, then methods
+ // first fields, then methods, to allow overriding
for (AnnotatedField f : _classDef.fields()) {
- _doAddInjectable(ai.findInjectableValueId(f), f);
+ _doAddInjectable(ai.findInjectableValue(f), f);
}
for (AnnotatedMethod m : _classDef.memberMethods()) {
- /* for now, only allow injection of a single arg
- * (to be changed in future)
- */
+ // for now, only allow injection of a single arg (to be changed in future?)
if (m.getParameterCount() != 1) {
continue;
}
- _doAddInjectable(ai.findInjectableValueId(m), m);
+ _doAddInjectable(ai.findInjectableValue(m), m);
}
}
- protected void _doAddInjectable(Object id, AnnotatedMember m)
+ protected void _doAddInjectable(JacksonInject.Value injectable, AnnotatedMember m)
{
- if (id == null) {
+ if (injectable == null) {
return;
}
+ Object id = injectable.getId();
if (_injectables == null) {
_injectables = new LinkedHashMap<Object, AnnotatedMember>();
}
AnnotatedMember prev = _injectables.put(id, m);
if (prev != null) {
- String type = id.getClass().getName();
- throw new IllegalArgumentException("Duplicate injectable value with id '"
- +String.valueOf(id)+"' (of type "+type+")");
+ // 12-Apr-2017, tatu: Let's allow masking of Field by Method
+ if (prev.getClass() == m.getClass()) {
+ String type = id.getClass().getName();
+ throw new IllegalArgumentException("Duplicate injectable value with id '"
+ +String.valueOf(id)+"' (of type "+type+")");
+ }
}
}
@@ -729,8 +739,8 @@
while (it.hasNext()) {
POJOPropertyBuilder prop = it.next();
// 26-Jan-2017, tatu: [databind#935]: need to denote removal of
- Access acc = prop.removeNonVisible(inferMutators);
- if (!_forSerialization && (acc == Access.READ_ONLY)) {
+ JsonProperty.Access acc = prop.removeNonVisible(inferMutators);
+ if (!_forSerialization && (acc == JsonProperty.Access.READ_ONLY)) {
_collectIgnorals(prop.getName());
}
}
@@ -869,9 +879,8 @@
protected void _renameWithWrappers(Map<String, POJOPropertyBuilder> props)
{
- /* 11-Sep-2012, tatu: To support 'MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME',
- * need another round of renaming...
- */
+ // 11-Sep-2012, tatu: To support 'MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME',
+ // need another round of renaming...
Iterator<Map.Entry<String,POJOPropertyBuilder>> it = props.entrySet().iterator();
LinkedList<POJOPropertyBuilder> renamed = null;
while (it.hasNext()) {
@@ -882,7 +891,7 @@
continue;
}
PropertyName wrapperName = _annotationIntrospector.findWrapperName(member);
- // One trickier part (wrt [Issue#24] of JAXB annotations: wrapper that
+ // One trickier part (wrt [#24] of JAXB annotations: wrapper that
// indicates use of actual property... But hopefully has been taken care
// of previously
if (wrapperName == null || !wrapperName.hasSimpleName()) {
@@ -924,7 +933,7 @@
{
// Then how about explicit ordering?
AnnotationIntrospector intr = _annotationIntrospector;
- Boolean alpha = (intr == null) ? null : intr.findSerializationSortAlphabetically((Annotated) _classDef);
+ Boolean alpha = intr.findSerializationSortAlphabetically((Annotated) _classDef);
boolean sort;
if (alpha == null) {
@@ -932,7 +941,7 @@
} else {
sort = alpha.booleanValue();
}
- String[] propertyOrder = (intr == null) ? null : intr.findSerializationPropertyOrder(_classDef);
+ String[] propertyOrder = intr.findSerializationPropertyOrder(_classDef);
// no sorting? no need to shuffle, then
if (!sort && (_creatorProperties == null) && (propertyOrder == null)) {
@@ -999,7 +1008,6 @@
}
// And finally whatever is left (trying to put again will not change ordering)
ordered.putAll(all);
-
props.clear();
props.putAll(ordered);
}
@@ -1010,13 +1018,23 @@
/**********************************************************
*/
- protected void reportProblem(String msg) {
+ protected void reportProblem(String msg, Object... args) {
+ if (args.length > 0) {
+ msg = String.format(msg, args);
+ }
throw new IllegalArgumentException("Problem with definition of "+_classDef+": "+msg);
}
protected POJOPropertyBuilder _property(Map<String, POJOPropertyBuilder> props,
PropertyName name) {
- return _property(props, name.getSimpleName());
+ String simpleName = name.getSimpleName();
+ POJOPropertyBuilder prop = props.get(simpleName);
+ if (prop == null) {
+ prop = new POJOPropertyBuilder(_config, _annotationIntrospector,
+ _forSerialization, name);
+ props.put(simpleName, prop);
+ }
+ return prop;
}
// !!! TODO: deprecate, require use of PropertyName
@@ -1034,8 +1052,7 @@
private PropertyNamingStrategy _findNamingStrategy()
{
- Object namingDef = (_annotationIntrospector == null)? null
- : _annotationIntrospector.findNamingStrategy(_classDef);
+ Object namingDef = _annotationIntrospector.findNamingStrategy(_classDef);
if (namingDef == null) {
return _config.getPropertyNamingStrategy();
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertyBuilder.java b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertyBuilder.java
index 7cee780..602e88d 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertyBuilder.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertyBuilder.java
@@ -4,8 +4,12 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonSetter;
+import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.cfg.ConfigOverride;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
+import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.ClassUtil;
/**
@@ -17,6 +21,15 @@
implements Comparable<POJOPropertyBuilder>
{
/**
+ * Marker value used to denote that no reference-property information found for
+ * this property
+ *
+ * @since 2.9
+ */
+ private final static AnnotationIntrospector.ReferenceProperty NOT_REFEFERENCE_PROP =
+ AnnotationIntrospector.ReferenceProperty.managed("");
+
+ /**
* Whether property is being composed for serialization
* (true) or deserialization (false)
*/
@@ -47,6 +60,16 @@
protected Linked<AnnotatedMethod> _setters;
+ protected transient PropertyMetadata _metadata;
+
+ /**
+ * Lazily accessed information about this property iff it is a forward or
+ * back reference.
+ *
+ * @since 2.9
+ */
+ protected transient AnnotationIntrospector.ReferenceProperty _referenceInfo;
+
public POJOPropertyBuilder(MapperConfig<?> config, AnnotationIntrospector ai,
boolean forSerialization, PropertyName internalName) {
this(config, ai, forSerialization, internalName, internalName);
@@ -62,7 +85,8 @@
_forSerialization = forSerialization;
}
- public POJOPropertyBuilder(POJOPropertyBuilder src, PropertyName newName)
+ // protected since 2.9 (was public before)
+ protected POJOPropertyBuilder(POJOPropertyBuilder src, PropertyName newName)
{
_config = src._config;
_annotationIntrospector = src._annotationIntrospector;
@@ -74,10 +98,10 @@
_setters = src._setters;
_forSerialization = src._forSerialization;
}
-
+
/*
/**********************************************************
- /* Fluent factory methods
+ /* Mutant factory methods
/**********************************************************
*/
@@ -92,7 +116,7 @@
PropertyName newName = _name.withSimpleName(newSimpleName);
return (newName == _name) ? this : new POJOPropertyBuilder(this, newName);
}
-
+
/*
/**********************************************************
/* Comparable implementation: sort alphabetically, except
@@ -183,7 +207,159 @@
|| _anyExplicitNames(_ctorParameters)
;
}
-
+
+ /*
+ /**********************************************************
+ /* Simple metadata
+ /**********************************************************
+ */
+
+ @Override
+ public PropertyMetadata getMetadata() {
+ if (_metadata == null) {
+ final Boolean b = _findRequired();
+ final String desc = _findDescription();
+ final Integer idx = _findIndex();
+ final String def = _findDefaultValue();
+ if (b == null && idx == null && def == null) {
+ _metadata = (desc == null) ? PropertyMetadata.STD_REQUIRED_OR_OPTIONAL
+ : PropertyMetadata.STD_REQUIRED_OR_OPTIONAL.withDescription(desc);
+ } else {
+ _metadata = PropertyMetadata.construct(b, desc, idx, def);
+ }
+ if (!_forSerialization) {
+ _metadata = _getSetterInfo(_metadata);
+ }
+ }
+ return _metadata;
+ }
+
+ /**
+ * Helper method that contains logic for accessing and merging all setter
+ * information that we needed, regarding things like possible merging
+ * of property value, and handling of incoming nulls.
+ */
+ protected PropertyMetadata _getSetterInfo(PropertyMetadata metadata)
+ {
+ boolean needMerge = true;
+ Nulls valueNulls = null;
+ Nulls contentNulls = null;
+
+ // Slightly confusing: first, annotations should be accessed via primary member
+ // (mutator); but accessor is needed for actual merge operation. So:
+ AnnotatedMember prim = getPrimaryMember();
+ AnnotatedMember acc = getAccessor();
+
+ if (prim != null) {
+ // Ok, first: does property itself have something to say?
+ if (_annotationIntrospector != null) {
+ if (acc != null) {
+ Boolean b = _annotationIntrospector.findMergeInfo(prim);
+ if (b != null) {
+ needMerge = false;
+ if (b.booleanValue()) {
+ metadata = metadata.withMergeInfo(PropertyMetadata.MergeInfo.createForPropertyOverride(acc));
+ }
+ }
+ }
+ JsonSetter.Value setterInfo = _annotationIntrospector.findSetterInfo(prim);
+ if (setterInfo != null) {
+ valueNulls = setterInfo.nonDefaultValueNulls();
+ contentNulls = setterInfo.nonDefaultContentNulls();
+ }
+ }
+ // If not, config override?
+ // 25-Oct-2016, tatu: Either this, or type of accessor...
+ if (needMerge || (valueNulls == null) || (contentNulls == null)) {
+ Class<?> rawType = getRawPrimaryType();
+ ConfigOverride co = _config.getConfigOverride(rawType);
+ JsonSetter.Value setterInfo = co.getSetterInfo();
+ if (setterInfo != null) {
+ if (valueNulls == null) {
+ valueNulls = setterInfo.nonDefaultValueNulls();
+ }
+ if (contentNulls == null) {
+ contentNulls = setterInfo.nonDefaultContentNulls();
+ }
+ }
+ if (needMerge && (acc != null)) {
+ Boolean b = co.getMergeable();
+ if (b != null) {
+ needMerge = false;
+ if (b.booleanValue()) {
+ metadata = metadata.withMergeInfo(PropertyMetadata.MergeInfo.createForTypeOverride(acc));
+ }
+ }
+ }
+ }
+ }
+ if (needMerge || (valueNulls == null) || (contentNulls == null)) {
+ JsonSetter.Value setterInfo = _config.getDefaultSetterInfo();
+ if (valueNulls == null) {
+ valueNulls = setterInfo.nonDefaultValueNulls();
+ }
+ if (contentNulls == null) {
+ contentNulls = setterInfo.nonDefaultContentNulls();
+ }
+ if (needMerge) {
+ Boolean b = _config.getDefaultMergeable();
+ if ((acc != null) && (b != null)) {
+ if (b.booleanValue()) {
+ metadata = metadata.withMergeInfo(PropertyMetadata.MergeInfo.createForDefaults(acc));
+ }
+ }
+ }
+ }
+ if ((valueNulls != null) || (contentNulls != null)) {
+ metadata = metadata.withNulls(valueNulls, contentNulls);
+ }
+ return metadata;
+ }
+
+ /**
+ * Type determined from the primary member for the property being built,
+ * considering precedence according to whether we are processing serialization
+ * or deserialization.
+ */
+ @Override
+ public JavaType getPrimaryType() {
+ if (_forSerialization) {
+ AnnotatedMember m = getGetter();
+ if (m == null) {
+ m = getField();
+ if (m == null) {
+ // 09-Feb-2017, tatu: Not sure if this or `null` but...
+ return TypeFactory.unknownType();
+ }
+ return m.getType();
+ }
+ return m.getType();
+ }
+ AnnotatedMember m = getConstructorParameter();
+ if (m == null) {
+ m = getSetter();
+ // Important: can't try direct type access for setter; what we need is
+ // type of the first parameter
+ if (m != null) {
+ return ((AnnotatedMethod) m).getParameterType(0);
+ }
+ m = getField();
+ }
+ // for setterless properties, however, can further try getter
+ if (m == null) {
+ m = getGetter();
+ if (m == null) {
+ return TypeFactory.unknownType();
+ }
+ }
+ return m.getType();
+ }
+
+ @Override
+ public Class<?> getRawPrimaryType() {
+ return getPrimaryType().getRawClass();
+ }
+
/*
/**********************************************************
/* BeanPropertyDefinition implementation, accessor access
@@ -388,45 +564,18 @@
}
return new MemberIterator<AnnotatedParameter>(_ctorParameters);
}
-
- @Override
- public AnnotatedMember getAccessor()
- {
- AnnotatedMember m = getGetter();
- if (m == null) {
- m = getField();
- }
- return m;
- }
-
- @Override
- public AnnotatedMember getMutator()
- {
- AnnotatedMember m = getConstructorParameter();
- if (m == null) {
- m = getSetter();
- if (m == null) {
- m = getField();
- }
- }
- return m;
- }
-
- @Override
- public AnnotatedMember getNonConstructorMutator() {
- AnnotatedMember m = getSetter();
- if (m == null) {
- m = getField();
- }
- return m;
- }
@Override
public AnnotatedMember getPrimaryMember() {
if (_forSerialization) {
return getAccessor();
}
- return getMutator();
+ AnnotatedMember m = getMutator();
+ // for setterless properties, however...
+ if (m == null) {
+ m = getAccessor();
+ }
+ return m;
}
protected int _getterPriority(AnnotatedMethod m)
@@ -471,12 +620,23 @@
@Override
public AnnotationIntrospector.ReferenceProperty findReferenceType() {
- return fromMemberAnnotations(new WithMember<AnnotationIntrospector.ReferenceProperty>() {
+ // 30-Mar-2017, tatu: Access lazily but retain information since it needs
+ // to be accessed multiple times during processing.
+ AnnotationIntrospector.ReferenceProperty result = _referenceInfo;
+ if (result != null) {
+ if (result == NOT_REFEFERENCE_PROP) {
+ return null;
+ }
+ return result;
+ }
+ result = fromMemberAnnotations(new WithMember<AnnotationIntrospector.ReferenceProperty>() {
@Override
public AnnotationIntrospector.ReferenceProperty withMember(AnnotatedMember member) {
return _annotationIntrospector.findReferenceType(member);
}
});
+ _referenceInfo = (result == null) ? NOT_REFEFERENCE_PROP : result;
+ return result;
}
@Override
@@ -490,19 +650,6 @@
return (b != null) && b.booleanValue();
}
- @Override
- public PropertyMetadata getMetadata() {
- final Boolean b = _findRequired();
- final String desc = _findDescription();
- final Integer idx = _findIndex();
- final String def = _findDefaultValue();
- if (b == null && idx == null && def == null) {
- return (desc == null) ? PropertyMetadata.STD_REQUIRED_OR_OPTIONAL
- : PropertyMetadata.STD_REQUIRED_OR_OPTIONAL.withDescription(desc);
- }
- return PropertyMetadata.construct(b, desc, idx, def);
- }
-
protected Boolean _findRequired() {
return fromMemberAnnotations(new WithMember<Boolean>() {
@Override
@@ -573,7 +720,7 @@
}
}, JsonProperty.Access.AUTO);
}
-
+
/*
/**********************************************************
/* Data aggregation
@@ -736,7 +883,7 @@
AnnotationMap ann = _getAllAnnotations(nodes[index]);
while (++index < nodes.length) {
if (nodes[index] != null) {
- return AnnotationMap.merge(ann, _mergeAnnotations(index, nodes));
+ return AnnotationMap.merge(ann, _mergeAnnotations(index, nodes));
}
}
return ann;
@@ -1073,7 +1220,7 @@
}
return null;
}
-
+
/*
/**********************************************************
/* Helper classes
@@ -1228,8 +1375,8 @@
@Override
public String toString() {
- String msg = value.toString()+"[visible="+isVisible+",ignore="+isMarkedIgnored
- +",explicitName="+isNameExplicit+"]";
+ String msg = String.format("%s[visible=%b,ignore=%b,explicitName=%b]",
+ value.toString(), isVisible, isMarkedIgnored, isNameExplicit);
if (next != null) {
msg = msg + ", "+next.toString();
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/VirtualAnnotatedMember.java b/src/main/java/com/fasterxml/jackson/databind/introspect/VirtualAnnotatedMember.java
index 13038f0..763605b 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/VirtualAnnotatedMember.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/VirtualAnnotatedMember.java
@@ -3,6 +3,7 @@
import java.lang.reflect.*;
import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Placeholder used by virtual properties as placeholder for
@@ -39,15 +40,6 @@
_name = name;
}
- /**
- * @deprecated Since 2.8
- */
- @Deprecated
- public VirtualAnnotatedMember(TypeResolutionContext typeContext, Class<?> declaringClass,
- String name, Class<?> rawType) {
- this(typeContext, declaringClass, name, typeContext.resolveType(rawType));
- }
-
@Override
public Annotated withAnnotations(AnnotationMap fallback) {
return this;
@@ -106,10 +98,6 @@
/**********************************************************
*/
- public String getFullName() {
- return getDeclaringClass().getName() + "#" + getName();
- }
-
public int getAnnotationCount() { return 0; }
@Override
@@ -120,7 +108,9 @@
@Override
public boolean equals(Object o) {
if (o == this) return true;
- if (o == null || o.getClass() != getClass()) return false;
+ if (!ClassUtil.hasClass(o, getClass())) {
+ return false;
+ }
VirtualAnnotatedMember other = (VirtualAnnotatedMember) o;
return (other._declaringClass == _declaringClass)
&& other._name.equals(_name);
@@ -128,6 +118,6 @@
@Override
public String toString() {
- return "[field "+getFullName()+"]";
+ return "[virtual "+getFullName()+"]";
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/VisibilityChecker.java b/src/main/java/com/fasterxml/jackson/databind/introspect/VisibilityChecker.java
index 0e2d0ad..0cb7766 100644
--- a/src/main/java/com/fasterxml/jackson/databind/introspect/VisibilityChecker.java
+++ b/src/main/java/com/fasterxml/jackson/databind/introspect/VisibilityChecker.java
@@ -29,6 +29,16 @@
public T with(JsonAutoDetect ann);
/**
+ * Method that can be used for merging default values from `this`
+ * instance with specified overrides; and either return `this`
+ * if overrides had no effect (that is, result would be equal),
+ * or a new instance with merged visibility settings.
+ *
+ * @since 2.9
+ */
+ public T withOverrides(JsonAutoDetect.Value vis);
+
+ /**
* Builder method that will create and return an instance that has specified
* {@link Visibility} value to use for all property elements.
* Typical usage would be something like:
@@ -140,23 +150,7 @@
* methods. As a result, type is declared is funky recursive generic
* type, to allow for sub-classing of build methods with property type
* co-variance.
- *<p>
- * Note on <code>JsonAutoDetect</code> annotation: it is used to
- * access default minimum visibility access definitions.
*/
- @JsonAutoDetect(
- getterVisibility = Visibility.PUBLIC_ONLY,
- isGetterVisibility = Visibility.PUBLIC_ONLY,
- setterVisibility = Visibility.ANY,
- /**
- * By default, all matching single-arg constructed are found,
- * regardless of visibility. Does not apply to factory methods,
- * they can not be auto-detected; ditto for multiple-argument
- * constructors.
- */
- creatorVisibility = Visibility.ANY,
- fieldVisibility = Visibility.PUBLIC_ONLY
- )
public static class Std
implements VisibilityChecker<Std>,
java.io.Serializable
@@ -167,8 +161,14 @@
* This is the canonical base instance, configured with default
* visibility values
*/
- protected final static Std DEFAULT = new Std(Std.class.getAnnotation(JsonAutoDetect.class));
-
+ protected final static Std DEFAULT = new Std(
+ Visibility.PUBLIC_ONLY, // getter
+ Visibility.PUBLIC_ONLY, // is-getter
+ Visibility.ANY, // setter
+ Visibility.ANY, // creator -- legacy, to support single-arg ctors
+ Visibility.PUBLIC_ONLY // field
+ );
+
protected final Visibility _getterMinLevel;
protected final Visibility _isGetterMinLevel;
protected final Visibility _setterMinLevel;
@@ -185,7 +185,7 @@
*/
public Std(JsonAutoDetect ann)
{
- // let's combine checks for enabled/disabled, with minimimum level checks:
+ // let's combine checks for enabled/disabled, with minimum level checks:
_getterMinLevel = ann.getterVisibility();
_isGetterMinLevel = ann.isGetterVisibility();
_setterMinLevel = ann.setterVisibility();
@@ -196,7 +196,8 @@
/**
* Constructor that allows directly specifying minimum visibility levels to use
*/
- public Std(Visibility getter, Visibility isGetter, Visibility setter, Visibility creator, Visibility field)
+ public Std(Visibility getter, Visibility isGetter, Visibility setter,
+ Visibility creator, Visibility field)
{
_getterMinLevel = getter;
_isGetterMinLevel = isGetter;
@@ -229,6 +230,13 @@
}
}
+ /**
+ * @since 2.9
+ */
+ public static Std construct(JsonAutoDetect.Value vis) {
+ return DEFAULT.withOverrides(vis);
+ }
+
/*
/********************************************************
/* Builder/fluent methods for instantiating configured
@@ -236,20 +244,58 @@
/********************************************************
*/
+ protected Std _with(Visibility g, Visibility isG, Visibility s,
+ Visibility cr, Visibility f) {
+ if ((g == _getterMinLevel)
+ && (isG == _isGetterMinLevel)
+ && (s == _setterMinLevel)
+ && (cr == _creatorMinLevel)
+ && (f == _fieldMinLevel)
+ ) {
+ return this;
+ }
+ return new Std(g, isG, s, cr, f);
+ }
+
@Override
public Std with(JsonAutoDetect ann)
{
Std curr = this;
if (ann != null) {
- curr = curr.withGetterVisibility(ann.getterVisibility());
- curr = curr.withIsGetterVisibility(ann.isGetterVisibility());
- curr = curr.withSetterVisibility(ann.setterVisibility());
- curr = curr.withCreatorVisibility(ann.creatorVisibility());
- curr = curr.withFieldVisibility(ann.fieldVisibility());
+ return _with(
+ _defaultOrOverride(_getterMinLevel, ann.getterVisibility()),
+ _defaultOrOverride(_isGetterMinLevel, ann.isGetterVisibility()),
+ _defaultOrOverride(_setterMinLevel, ann.setterVisibility()),
+ _defaultOrOverride(_creatorMinLevel, ann.creatorVisibility()),
+ _defaultOrOverride(_fieldMinLevel, ann.fieldVisibility())
+ );
}
return curr;
}
+ @Override // since 2.9
+ public Std withOverrides(JsonAutoDetect.Value vis)
+ {
+ Std curr = this;
+ if (vis != null) {
+ return _with(
+ _defaultOrOverride(_getterMinLevel, vis.getGetterVisibility()),
+ _defaultOrOverride(_isGetterMinLevel, vis.getIsGetterVisibility()),
+ _defaultOrOverride(_setterMinLevel, vis.getSetterVisibility()),
+ _defaultOrOverride(_creatorMinLevel, vis.getCreatorVisibility()),
+ _defaultOrOverride(_fieldMinLevel, vis.getFieldVisibility())
+ );
+ }
+ return curr;
+ }
+
+ private Visibility _defaultOrOverride(Visibility defaults, Visibility override) {
+ if (override == Visibility.DEFAULT) {
+ return defaults;
+ }
+ return override;
+ }
+
@Override
public Std with(Visibility v)
{
@@ -258,7 +304,7 @@
}
return new Std(v);
}
-
+
@Override
public Std withVisibility(PropertyAccessor method, Visibility v)
{
@@ -284,39 +330,39 @@
@Override
public Std withGetterVisibility(Visibility v) {
- if (v == Visibility.DEFAULT) v = DEFAULT._getterMinLevel;
+ if (v == Visibility.DEFAULT) v = DEFAULT._getterMinLevel;
if (_getterMinLevel == v) return this;
return new Std(v, _isGetterMinLevel, _setterMinLevel, _creatorMinLevel, _fieldMinLevel);
}
@Override
public Std withIsGetterVisibility(Visibility v) {
- if (v == Visibility.DEFAULT) v = DEFAULT._isGetterMinLevel;
+ if (v == Visibility.DEFAULT) v = DEFAULT._isGetterMinLevel;
if (_isGetterMinLevel == v) return this;
return new Std(_getterMinLevel, v, _setterMinLevel, _creatorMinLevel, _fieldMinLevel);
}
@Override
public Std withSetterVisibility(Visibility v) {
- if (v == Visibility.DEFAULT) v = DEFAULT._setterMinLevel;
+ if (v == Visibility.DEFAULT) v = DEFAULT._setterMinLevel;
if (_setterMinLevel == v) return this;
return new Std(_getterMinLevel, _isGetterMinLevel, v, _creatorMinLevel, _fieldMinLevel);
}
-
+
@Override
public Std withCreatorVisibility(Visibility v) {
- if (v == Visibility.DEFAULT) v = DEFAULT._creatorMinLevel;
+ if (v == Visibility.DEFAULT) v = DEFAULT._creatorMinLevel;
if (_creatorMinLevel == v) return this;
return new Std(_getterMinLevel, _isGetterMinLevel, _setterMinLevel, v, _fieldMinLevel);
}
-
+
@Override
public Std withFieldVisibility(Visibility v) {
if (v == Visibility.DEFAULT) v = DEFAULT._fieldMinLevel;
if (_fieldMinLevel == v) return this;
return new Std(_getterMinLevel, _isGetterMinLevel, _setterMinLevel, _creatorMinLevel, v);
}
-
+
/*
/********************************************************
/* Public API impl
@@ -327,7 +373,7 @@
public boolean isCreatorVisible(Member m) {
return _creatorMinLevel.isVisible(m);
}
-
+
@Override
public boolean isCreatorVisible(AnnotatedMember m) {
return isCreatorVisible(m.getMember());
@@ -337,17 +383,17 @@
public boolean isFieldVisible(Field f) {
return _fieldMinLevel.isVisible(f);
}
-
+
@Override
public boolean isFieldVisible(AnnotatedField f) {
return isFieldVisible(f.getAnnotated());
}
-
+
@Override
public boolean isGetterVisible(Method m) {
return _getterMinLevel.isVisible(m);
}
-
+
@Override
public boolean isGetterVisible(AnnotatedMethod m) {
return isGetterVisible(m.getAnnotated());
@@ -381,13 +427,8 @@
@Override
public String toString() {
- return new StringBuilder("[Visibility:")
- .append(" getter: ").append(_getterMinLevel)
- .append(", isGetter: ").append(_isGetterMinLevel)
- .append(", setter: ").append(_setterMinLevel)
- .append(", creator: ").append(_creatorMinLevel)
- .append(", field: ").append(_fieldMinLevel)
- .append("]").toString();
+ return String.format("[Visibility: getter=%s,isGetter=%s,setter=%s,creator=%s,field=%s]",
+ _getterMinLevel, _isGetterMinLevel, _setterMinLevel, _creatorMinLevel, _fieldMinLevel);
}
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsonFormatVisitors/JsonObjectFormatVisitor.java b/src/main/java/com/fasterxml/jackson/databind/jsonFormatVisitors/JsonObjectFormatVisitor.java
index 02ba141..72dbbe1 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsonFormatVisitors/JsonObjectFormatVisitor.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsonFormatVisitors/JsonObjectFormatVisitor.java
@@ -56,8 +56,7 @@
JavaType propertyTypeHint) throws JsonMappingException { }
@Override
- public void optionalProperty(BeanProperty prop)
- throws JsonMappingException { }
+ public void optionalProperty(BeanProperty prop) throws JsonMappingException { }
@Override
public void optionalProperty(String name, JsonFormatVisitable handler,
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/NamedType.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/NamedType.java
index e64b7f2..226668e 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/NamedType.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/NamedType.java
@@ -43,6 +43,7 @@
@Override
public String toString() {
- return "[NamedType, class "+_class.getName()+", name: "+(_name == null ? "null" :("'"+_name+"'"))+"]";
+ return "[NamedType, class "+_class.getName()+", name: "
+ +(_name == null ? "null" :("'"+_name+"'"))+"]";
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsArrayTypeDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsArrayTypeDeserializer.java
index 4172c17..250db2b 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsArrayTypeDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsArrayTypeDeserializer.java
@@ -116,7 +116,7 @@
Object value = deser.deserialize(p, ctxt);
// And then need the closing END_ARRAY
if (hadStartArray && p.nextToken() != JsonToken.END_ARRAY) {
- ctxt.reportWrongTokenException(p, JsonToken.END_ARRAY,
+ ctxt.reportWrongTokenException(baseType(), JsonToken.END_ARRAY,
"expected closing END_ARRAY after type information and deserialized value");
// 05-May-2016, tatu: Not 100% what to do if exception is stored for
// future, and not thrown immediately: should probably skip until END_ARRAY
@@ -134,7 +134,7 @@
if (_defaultImpl != null) {
return _idResolver.idFromBaseType();
}
- ctxt.reportWrongTokenException(p, JsonToken.START_ARRAY,
+ ctxt.reportWrongTokenException(baseType(), JsonToken.START_ARRAY,
"need JSON Array to contain As.WRAPPER_ARRAY type information for class "+baseTypeName());
return null;
}
@@ -148,7 +148,8 @@
if (_defaultImpl != null) {
return _idResolver.idFromBaseType();
}
- ctxt.reportWrongTokenException(p, JsonToken.VALUE_STRING, "need JSON String that contains type id (for subtype of "+baseTypeName()+")");
+ ctxt.reportWrongTokenException(baseType(), JsonToken.VALUE_STRING,
+ "need JSON String that contains type id (for subtype of %s)", baseTypeName());
return null;
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsArrayTypeSerializer.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsArrayTypeSerializer.java
index adde36e..7d7321e 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsArrayTypeSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsArrayTypeSerializer.java
@@ -37,9 +37,7 @@
final String typeId = idFromValue(value);
// NOTE: can not always avoid writing type id, even if null
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
g.writeStartArray();
g.writeString(typeId);
@@ -52,9 +50,7 @@
final String typeId = idFromValueAndType(value, type);
// NOTE: can not always avoid writing type id, even if null
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
g.writeStartArray();
g.writeString(typeId);
@@ -66,9 +62,7 @@
public void writeTypePrefixForArray(Object value, JsonGenerator g) throws IOException {
final String typeId = idFromValue(value);
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
g.writeStartArray();
g.writeString(typeId);
@@ -80,9 +74,7 @@
public void writeTypePrefixForArray(Object value, JsonGenerator g, Class<?> type) throws IOException {
final String typeId = idFromValueAndType(value, type);
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
g.writeStartArray();
g.writeString(typeId);
@@ -94,9 +86,7 @@
public void writeTypePrefixForScalar(Object value, JsonGenerator g) throws IOException {
final String typeId = idFromValue(value);
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
// only need the wrapper array
g.writeStartArray();
@@ -108,9 +98,7 @@
public void writeTypePrefixForScalar(Object value, JsonGenerator g, Class<?> type) throws IOException {
final String typeId = idFromValueAndType(value, type);
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
// only need the wrapper array
g.writeStartArray();
@@ -158,9 +146,7 @@
@Override
public void writeCustomTypePrefixForObject(Object value, JsonGenerator g, String typeId) throws IOException {
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
g.writeStartArray();
g.writeString(typeId);
@@ -171,9 +157,7 @@
@Override
public void writeCustomTypePrefixForArray(Object value, JsonGenerator g, String typeId) throws IOException {
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
g.writeStartArray();
g.writeString(typeId);
@@ -184,9 +168,7 @@
@Override
public void writeCustomTypePrefixForScalar(Object value, JsonGenerator g, String typeId) throws IOException {
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
g.writeStartArray();
g.writeString(typeId);
@@ -213,4 +195,18 @@
writeTypeSuffixForScalar(value, g); // standard impl works fine
}
}
+
+ /*
+ /**********************************************************
+ /* Internal helper methods
+ /**********************************************************
+ */
+
+ // @since 2.9
+ protected final void _writeTypeId(JsonGenerator g, String typeId) throws IOException
+ {
+ if (typeId != null) {
+ g.writeTypeId(typeId);
+ }
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsExistingPropertyTypeSerializer.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsExistingPropertyTypeSerializer.java
index fd10302..d0fb684 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsExistingPropertyTypeSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsExistingPropertyTypeSerializer.java
@@ -15,7 +15,6 @@
public class AsExistingPropertyTypeSerializer
extends AsPropertyTypeSerializer
{
-
public AsExistingPropertyTypeSerializer(TypeIdResolver idRes,
BeanProperty property, String propName)
{
@@ -34,9 +33,11 @@
@Override
public void writeTypePrefixForObject(Object value, JsonGenerator gen) throws IOException
{
- final String typeId = idFromValue(value);
- if ((typeId != null) && gen.canWriteTypeId()) {
- gen.writeTypeId(typeId);
+ if (gen.canWriteTypeId()) { // only write explicitly if native type id
+ final String typeId = idFromValue(value);
+ if (typeId != null) {
+ gen.writeTypeId(typeId);
+ }
}
gen.writeStartObject();
}
@@ -44,9 +45,11 @@
@Override
public void writeTypePrefixForObject(Object value, JsonGenerator gen, Class<?> type) throws IOException
{
- final String typeId = idFromValueAndType(value, type);
- if ((typeId != null) && gen.canWriteTypeId()) {
- gen.writeTypeId(typeId);
+ if (gen.canWriteTypeId()) { // only write explicitly if native type id
+ final String typeId = idFromValueAndType(value, type);
+ if (typeId != null) {
+ gen.writeTypeId(typeId);
+ }
}
gen.writeStartObject();
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsPropertyTypeDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsPropertyTypeDeserializer.java
index daf3271..9bfab80 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsPropertyTypeDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsPropertyTypeDeserializer.java
@@ -106,7 +106,8 @@
}
@SuppressWarnings("resource")
- protected Object _deserializeTypedForId(JsonParser p, DeserializationContext ctxt, TokenBuffer tb) throws IOException
+ protected Object _deserializeTypedForId(JsonParser p, DeserializationContext ctxt,
+ TokenBuffer tb) throws IOException
{
String typeId = p.getText();
JsonDeserializer<Object> deser = _findDeserializer(ctxt, typeId);
@@ -131,40 +132,50 @@
// off-lined to keep main method lean and mean...
@SuppressWarnings("resource")
- protected Object _deserializeTypedUsingDefaultImpl(JsonParser p, DeserializationContext ctxt,
- TokenBuffer tb) throws IOException
+ protected Object _deserializeTypedUsingDefaultImpl(JsonParser p,
+ DeserializationContext ctxt, TokenBuffer tb) throws IOException
{
- // As per [JACKSON-614], may have default implementation to use
+ // May have default implementation to use
JsonDeserializer<Object> deser = _findDefaultImplDeserializer(ctxt);
- if (deser != null) {
- if (tb != null) {
- tb.writeEndObject();
- p = tb.asParser(p);
- // must move to point to the first token:
- p.nextToken();
+ if (deser == null) {
+ // or, perhaps we just bumped into a "natural" value (boolean/int/double/String)?
+ Object result = TypeDeserializer.deserializeIfNatural(p, ctxt, _baseType);
+ if (result != null) {
+ return result;
}
- return deser.deserialize(p, ctxt);
- }
- // or, perhaps we just bumped into a "natural" value (boolean/int/double/String)?
- Object result = TypeDeserializer.deserializeIfNatural(p, ctxt, _baseType);
- if (result != null) {
- return result;
- }
- // or, something for which "as-property" won't work, changed into "wrapper-array" type:
- if (p.isExpectedStartArrayToken()) {
- return super.deserializeTypedFromAny(p, ctxt);
- }
- if (p.hasToken(JsonToken.VALUE_STRING)) {
- if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
- String str = p.getText().trim();
- if (str.isEmpty()) {
- return null;
+ // or, something for which "as-property" won't work, changed into "wrapper-array" type:
+ if (p.isExpectedStartArrayToken()) {
+ return super.deserializeTypedFromAny(p, ctxt);
+ }
+ if (p.hasToken(JsonToken.VALUE_STRING)) {
+ if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
+ String str = p.getText().trim();
+ if (str.isEmpty()) {
+ return null;
+ }
}
}
+ String msg = String.format("missing type id property '%s'",
+ _typePropertyName);
+ // even better, may know POJO property polymorphic value would be assigned to
+ if (_property != null) {
+ msg = String.format("%s (for POJO property '%s')", msg, _property.getName());
+ }
+ JavaType t = _handleMissingTypeId(ctxt, msg);
+ if (t == null) {
+ // 09-Mar-2017, tatu: Is this the right thing to do?
+ return null;
+ }
+ // ... would this actually work?
+ deser = ctxt.findContextualValueDeserializer(t, _property);
}
- ctxt.reportWrongTokenException(p, JsonToken.FIELD_NAME,
- "missing property '"+_typePropertyName+"' that is to contain type id (for class "+baseTypeName()+")");
- return null;
+ if (tb != null) {
+ tb.writeEndObject();
+ p = tb.asParser(p);
+ // must move to point to the first token:
+ p.nextToken();
+ }
+ return deser.deserialize(p, ctxt);
}
/* Also need to re-route "unknown" version. Need to think
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsWrapperTypeDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsWrapperTypeDeserializer.java
index 02f26ca..643f921 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsWrapperTypeDeserializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsWrapperTypeDeserializer.java
@@ -93,11 +93,11 @@
if (t == JsonToken.START_OBJECT) {
// should always get field name, but just in case...
if (p.nextToken() != JsonToken.FIELD_NAME) {
- ctxt.reportWrongTokenException(p, JsonToken.FIELD_NAME,
+ ctxt.reportWrongTokenException(baseType(), JsonToken.FIELD_NAME,
"need JSON String that contains type id (for subtype of "+baseTypeName()+")");
}
} else if (t != JsonToken.FIELD_NAME) {
- ctxt.reportWrongTokenException(p, JsonToken.START_OBJECT,
+ ctxt.reportWrongTokenException(baseType(), JsonToken.START_OBJECT,
"need JSON Object to contain As.WRAPPER_OBJECT type information for class "+baseTypeName());
}
final String typeId = p.getText();
@@ -121,7 +121,7 @@
Object value = deser.deserialize(p, ctxt);
// And then need the closing END_OBJECT
if (p.nextToken() != JsonToken.END_OBJECT) {
- ctxt.reportWrongTokenException(p, JsonToken.END_OBJECT,
+ ctxt.reportWrongTokenException(baseType(), JsonToken.END_OBJECT,
"expected closing END_OBJECT after type information and deserialized value");
}
return value;
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsWrapperTypeSerializer.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsWrapperTypeSerializer.java
index feab592..014fca7 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsWrapperTypeSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsWrapperTypeSerializer.java
@@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Type wrapper that tries to use an extra JSON Object, with a single
@@ -29,15 +30,13 @@
@Override
public As getTypeInclusion() { return As.WRAPPER_OBJECT; }
-
+
@Override
public void writeTypePrefixForObject(Object value, JsonGenerator g) throws IOException
{
String typeId = idFromValue(value);
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
g.writeStartObject();
} else {
// wrapper
@@ -55,9 +54,7 @@
{
String typeId = idFromValueAndType(value, type);
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
g.writeStartObject();
} else {
// wrapper
@@ -75,9 +72,7 @@
{
String typeId = idFromValue(value);
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
g.writeStartArray();
} else {
// can still wrap ok
@@ -91,9 +86,7 @@
{
final String typeId = idFromValueAndType(value, type);
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
g.writeStartArray();
} else {
// can still wrap ok
@@ -107,9 +100,7 @@
public void writeTypePrefixForScalar(Object value, JsonGenerator g) throws IOException {
final String typeId = idFromValue(value);
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
// can still wrap ok
g.writeStartObject();
@@ -122,9 +113,7 @@
{
final String typeId = idFromValueAndType(value, type);
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
// can still wrap ok
g.writeStartObject();
@@ -171,9 +160,7 @@
@Override
public void writeCustomTypePrefixForObject(Object value, JsonGenerator g, String typeId) throws IOException {
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
g.writeStartObject();
} else {
g.writeStartObject();
@@ -184,9 +171,7 @@
@Override
public void writeCustomTypePrefixForArray(Object value, JsonGenerator g, String typeId) throws IOException {
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
g.writeStartArray();
} else {
g.writeStartObject();
@@ -197,9 +182,7 @@
@Override
public void writeCustomTypePrefixForScalar(Object value, JsonGenerator g, String typeId) throws IOException {
if (g.canWriteTypeId()) {
- if (typeId != null) {
- g.writeTypeId(typeId);
- }
+ _writeTypeId(g, typeId);
} else {
g.writeStartObject();
g.writeFieldName(_validTypeId(typeId));
@@ -240,6 +223,14 @@
* @since 2.6
*/
protected String _validTypeId(String typeId) {
- return (typeId == null) ? "" : typeId;
+ return ClassUtil.nonNullString(typeId);
+ }
+
+ // @since 2.9
+ protected final void _writeTypeId(JsonGenerator g, String typeId) throws IOException
+ {
+ if (typeId != null) {
+ g.writeTypeId(typeId);
+ }
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java
index fd7b097..14da913 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java
@@ -44,40 +44,23 @@
protected JavaType _typeFromId(String id, DatabindContext ctxt) throws IOException
{
- /* 30-Jan-2010, tatu: Most ids are basic class names; so let's first
- * check if any generics info is added; and only then ask factory
- * to do translation when necessary
- */
- TypeFactory tf = ctxt.getTypeFactory();
- if (id.indexOf('<') > 0) {
- // note: may want to try combining with specialization (esp for EnumMap)?
- return tf.constructFromCanonical(id);
- }
- Class<?> cls;
- try {
- cls = tf.findClass(id);
- } catch (ClassNotFoundException e) {
- // 24-May-2016, tatu: Ok, this is pretty ugly, but we should always get
- // DeserializationContext, just playing it safe
+ JavaType t = ctxt.resolveSubType(_baseType, id);
+ if (t == null) {
if (ctxt instanceof DeserializationContext) {
- DeserializationContext dctxt = (DeserializationContext) ctxt;
// First: we may have problem handlers that can deal with it?
- return dctxt.handleUnknownTypeId(_baseType, id, this, "no such class found");
+ return ((DeserializationContext) ctxt).handleUnknownTypeId(_baseType, id, this, "no such class found");
}
// ... meaning that we really should never get here.
- return null;
- } catch (Exception e) {
- throw new IllegalArgumentException("Invalid type id '"+id+"' (for id type 'Id.class'): "+e.getMessage(), e);
}
- return tf.constructSpecializedType(_baseType, cls);
+ return t;
}
-
+
/*
/**********************************************************
/* Internal methods
/**********************************************************
*/
-
+
protected final String _idFrom(Object value, Class<?> cls, TypeFactory typeFactory)
{
// Need to ensure that "enum subtypes" work too
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdSubtypeResolver.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdSubtypeResolver.java
index 0158215..9cb8bec 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdSubtypeResolver.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdSubtypeResolver.java
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.databind.jsontype.impl;
+import java.lang.reflect.Modifier;
import java.util.*;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
@@ -21,7 +22,7 @@
protected LinkedHashSet<NamedType> _registeredSubtypes;
public StdSubtypeResolver() { }
-
+
/*
/**********************************************************
/* Subtype registration
@@ -67,23 +68,27 @@
for (NamedType subtype : _registeredSubtypes) {
// is it a subtype of root type?
if (rawBase.isAssignableFrom(subtype.getType())) { // yes
- AnnotatedClass curr = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), config);
+ AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
+ subtype.getType());
_collectAndResolve(curr, subtype, config, ai, collected);
}
}
}
// then annotated types for property itself
- Collection<NamedType> st = ai.findSubtypes(property);
- if (st != null) {
- for (NamedType nt : st) {
- AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(nt.getType(), config);
- _collectAndResolve(ac, nt, config, ai, collected);
- }
+ if (property != null) {
+ Collection<NamedType> st = ai.findSubtypes(property);
+ if (st != null) {
+ for (NamedType nt : st) {
+ AnnotatedClass ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
+ nt.getType());
+ _collectAndResolve(ac, nt, config, ai, collected);
+ }
+ }
}
-
+
NamedType rootType = new NamedType(rawBase, null);
- AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(rawBase, config);
+ AnnotatedClass ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config, rawBase);
// and finally subtypes via annotations from base type (recursively)
_collectAndResolve(ac, rootType, config, ai, collected);
@@ -103,7 +108,8 @@
for (NamedType subtype : _registeredSubtypes) {
// is it a subtype of root type?
if (rawBase.isAssignableFrom(subtype.getType())) { // yes
- AnnotatedClass curr = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), config);
+ AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
+ subtype.getType());
_collectAndResolve(curr, subtype, config, ai, subtypes);
}
}
@@ -125,7 +131,7 @@
AnnotatedMember property, JavaType baseType)
{
final AnnotationIntrospector ai = config.getAnnotationIntrospector();
- Class<?> rawBase = (baseType == null) ? property.getRawType() : baseType.getRawClass();
+ Class<?> rawBase = baseType.getRawClass();
// Need to keep track of classes that have been handled already
Set<Class<?>> typesHandled = new HashSet<Class<?>>();
@@ -133,52 +139,56 @@
// start with lowest-precedence, which is from type hierarchy
NamedType rootType = new NamedType(rawBase, null);
- AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(rawBase, config);
+ AnnotatedClass ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
+ rawBase);
_collectAndResolveByTypeId(ac, rootType, config, typesHandled, byName);
// then with definitions from property
- Collection<NamedType> st = ai.findSubtypes(property);
- if (st != null) {
- for (NamedType nt : st) {
- ac = AnnotatedClass.constructWithoutSuperTypes(nt.getType(), config);
- _collectAndResolveByTypeId(ac, nt, config, typesHandled, byName);
- }
+ if (property != null) {
+ Collection<NamedType> st = ai.findSubtypes(property);
+ if (st != null) {
+ for (NamedType nt : st) {
+ ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config, nt.getType());
+ _collectAndResolveByTypeId(ac, nt, config, typesHandled, byName);
+ }
+ }
}
-
// and finally explicit type registrations (highest precedence)
if (_registeredSubtypes != null) {
for (NamedType subtype : _registeredSubtypes) {
// is it a subtype of root type?
if (rawBase.isAssignableFrom(subtype.getType())) { // yes
- AnnotatedClass curr = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), config);
+ AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
+ subtype.getType());
_collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName);
}
}
}
- return _combineNamedAndUnnamed(typesHandled, byName);
+ return _combineNamedAndUnnamed(rawBase, typesHandled, byName);
}
@Override
public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> config,
- AnnotatedClass type)
+ AnnotatedClass baseType)
{
+ final Class<?> rawBase = baseType.getRawType();
Set<Class<?>> typesHandled = new HashSet<Class<?>>();
Map<String,NamedType> byName = new LinkedHashMap<String,NamedType>();
- NamedType rootType = new NamedType(type.getRawType(), null);
- _collectAndResolveByTypeId(type, rootType, config, typesHandled, byName);
+ NamedType rootType = new NamedType(rawBase, null);
+ _collectAndResolveByTypeId(baseType, rootType, config, typesHandled, byName);
if (_registeredSubtypes != null) {
- Class<?> rawBase = type.getRawType();
for (NamedType subtype : _registeredSubtypes) {
// is it a subtype of root type?
if (rawBase.isAssignableFrom(subtype.getType())) { // yes
- AnnotatedClass curr = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), config);
+ AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
+ subtype.getType());
_collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName);
}
}
}
- return _combineNamedAndUnnamed(typesHandled, byName);
+ return _combineNamedAndUnnamed(rawBase, typesHandled, byName);
}
/*
@@ -218,7 +228,8 @@
Collection<NamedType> st = ai.findSubtypes(annotatedType);
if (st != null && !st.isEmpty()) {
for (NamedType subtype : st) {
- AnnotatedClass subtypeClass = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), config);
+ AnnotatedClass subtypeClass = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
+ subtype.getType());
_collectAndResolve(subtypeClass, subtype, config, ai, collectedSubtypes);
}
}
@@ -248,7 +259,8 @@
Collection<NamedType> st = ai.findSubtypes(annotatedType);
if (st != null && !st.isEmpty()) {
for (NamedType subtype : st) {
- AnnotatedClass subtypeClass = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), config);
+ AnnotatedClass subtypeClass = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
+ subtype.getType());
_collectAndResolveByTypeId(subtypeClass, subtype, config, typesHandled, byName);
}
}
@@ -259,8 +271,8 @@
* Helper method used for merging explicitly named types and handled classes
* without explicit names.
*/
- protected Collection<NamedType> _combineNamedAndUnnamed(Set<Class<?>> typesHandled,
- Map<String,NamedType> byName)
+ protected Collection<NamedType> _combineNamedAndUnnamed(Class<?> rawBase,
+ Set<Class<?>> typesHandled, Map<String,NamedType> byName)
{
ArrayList<NamedType> result = new ArrayList<NamedType>(byName.values());
@@ -271,6 +283,11 @@
typesHandled.remove(t.getType());
}
for (Class<?> cls : typesHandled) {
+ // 27-Apr-2017, tatu: [databind#1616] Do not add base type itself unless
+ // it is concrete (or has explicit type name)
+ if ((cls == rawBase) && Modifier.isAbstract(cls.getModifiers())) {
+ continue;
+ }
result.add(new NamedType(cls));
}
return result;
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java
index e5a3e4d..7536d3f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java
@@ -46,6 +46,16 @@
public StdTypeResolverBuilder() { }
+ /**
+ * @since 2.9
+ */
+ protected StdTypeResolverBuilder(JsonTypeInfo.Id idType,
+ JsonTypeInfo.As idAs, String propName) {
+ _idType = idType;
+ _includeAs = idAs;
+ _typeProperty = propName;
+ }
+
public static StdTypeResolverBuilder noTypeInfoBuilder() {
return new StdTypeResolverBuilder().init(JsonTypeInfo.Id.NONE, null);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeDeserializerBase.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeDeserializerBase.java
index 7284b47..379b6f9 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeDeserializerBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeDeserializerBase.java
@@ -74,8 +74,7 @@
{
_baseType = baseType;
_idResolver = idRes;
- // 22-Dec-2015, tatu: as per [databind#1055], avoid NPE
- _typePropertyName = (typePropertyName == null) ? "" : typePropertyName;
+ _typePropertyName = ClassUtil.nonNullString(typePropertyName);
_typeIdVisible = typeIdVisible;
// defaults are fine, although shouldn't need much concurrency
_deserializers = new ConcurrentHashMap<String, JsonDeserializer<Object>>(16, 0.75f, 2);
@@ -119,7 +118,14 @@
public Class<?> getDefaultImpl() {
return (_defaultImpl == null) ? null : _defaultImpl.getRawClass();
}
-
+
+ /**
+ * @since 2.9
+ */
+ public JavaType baseType() {
+ return _baseType;
+ }
+
@Override
public String toString()
{
@@ -142,18 +148,18 @@
{
JsonDeserializer<Object> deser = _deserializers.get(typeId);
if (deser == null) {
- /* As per [Databind#305], need to provide contextual info. But for
+ /* As per [databind#305], need to provide contextual info. But for
* backwards compatibility, let's start by only supporting this
* for base class, not via interface. Later on we can add this
* to the interface, assuming deprecation at base class helps.
*/
JavaType type = _idResolver.typeFromId(ctxt, typeId);
if (type == null) {
- // As per [JACKSON-614], use the default impl if no type id available:
+ // use the default impl if no type id available:
deser = _findDefaultImplDeserializer(ctxt);
if (deser == null) {
// 10-May-2016, tatu: We may get some help...
- JavaType actual = _handleUnknownTypeId(ctxt, typeId, _idResolver, _baseType);
+ JavaType actual = _handleUnknownTypeId(ctxt, typeId);
if (actual == null) { // what should this be taken to mean?
// TODO: try to figure out something better
return null;
@@ -246,8 +252,8 @@
*/
deser = _findDefaultImplDeserializer(ctxt);
if (deser == null) {
- ctxt.reportMappingException("No (native) type id found when one was expected for polymorphic type handling");
- return null;
+ return ctxt.reportInputMismatch(baseType(),
+ "No (native) type id found when one was expected for polymorphic type handling");
}
} else {
String typeIdStr = (typeId instanceof String) ? (String) typeId : String.valueOf(typeId);
@@ -269,16 +275,28 @@
*
* @since 2.8
*/
- protected JavaType _handleUnknownTypeId(DeserializationContext ctxt, String typeId,
- TypeIdResolver idResolver, JavaType baseType)
+ protected JavaType _handleUnknownTypeId(DeserializationContext ctxt, String typeId)
throws IOException
{
- String extraDesc = idResolver.getDescForKnownTypeIds();
+ String extraDesc = _idResolver.getDescForKnownTypeIds();
if (extraDesc == null) {
- extraDesc = "known type ids are not statically known";
+ extraDesc = "type ids are not statically known";
} else {
extraDesc = "known type ids = " + extraDesc;
}
- return ctxt.handleUnknownTypeId(_baseType, typeId, idResolver, extraDesc);
+ if (_property != null) {
+ extraDesc = String.format("%s (for POJO property '%s')", extraDesc,
+ _property.getName());
+ }
+ return ctxt.handleUnknownTypeId(_baseType, typeId, _idResolver, extraDesc);
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected JavaType _handleMissingTypeId(DeserializationContext ctxt, String extraDesc)
+ throws IOException
+ {
+ return ctxt.handleMissingTypeId(_baseType, _idResolver, extraDesc);
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeSerializerBase.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeSerializerBase.java
index 5397946..a8193d3 100644
--- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeSerializerBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeSerializerBase.java
@@ -51,7 +51,7 @@
// As per [databind#633], maybe better just not do anything...
protected void handleMissingId(Object value) {
/*
- String typeDesc = (value == null) ? "NULL" : value.getClass().getName();
+ String typeDesc = ClassUtil.classNameOf(value, "NULL");
throw new IllegalArgumentException("Can not resolve type id for "
+typeDesc+" (using "+_idResolver.getClass().getName()+")");
*/
diff --git a/src/main/java/com/fasterxml/jackson/databind/module/SimpleDeserializers.java b/src/main/java/com/fasterxml/jackson/databind/module/SimpleDeserializers.java
index 35c6b37..0a9f443 100644
--- a/src/main/java/com/fasterxml/jackson/databind/module/SimpleDeserializers.java
+++ b/src/main/java/com/fasterxml/jackson/databind/module/SimpleDeserializers.java
@@ -85,7 +85,7 @@
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
- return (_classMappings == null) ? null : _classMappings.get(new ClassKey(type.getRawClass()));
+ return _find(type);
}
@Override
@@ -93,7 +93,7 @@
DeserializationConfig config, BeanDescription beanDesc)
throws JsonMappingException
{
- return (_classMappings == null) ? null : _classMappings.get(new ClassKey(type.getRawClass()));
+ return _find(type);
}
@Override
@@ -103,7 +103,7 @@
JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
- return (_classMappings == null) ? null : _classMappings.get(new ClassKey(type.getRawClass()));
+ return _find(type);
}
@Override
@@ -113,7 +113,7 @@
JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
- return (_classMappings == null) ? null : _classMappings.get(new ClassKey(type.getRawClass()));
+ return _find(type);
}
@Override
@@ -138,9 +138,12 @@
DeserializationConfig config, BeanDescription beanDesc)
throws JsonMappingException
{
- return (_classMappings == null) ? null : _classMappings.get(new ClassKey(nodeType));
+ if (_classMappings == null) {
+ return null;
+ }
+ return _classMappings.get(new ClassKey(nodeType));
}
-
+
@Override
public JsonDeserializer<?> findReferenceDeserializer(ReferenceType refType,
DeserializationConfig config, BeanDescription beanDesc,
@@ -148,9 +151,9 @@
throws JsonMappingException {
// 21-Oct-2015, tatu: Unlikely this will really get used (reference types need more
// work, simple registration probably not sufficient). But whatever.
- return (_classMappings == null) ? null : _classMappings.get(new ClassKey(refType.getRawClass()));
+ return _find(refType);
}
-
+
@Override
public JsonDeserializer<?> findMapDeserializer(MapType type,
DeserializationConfig config, BeanDescription beanDesc,
@@ -159,7 +162,7 @@
JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
- return (_classMappings == null) ? null : _classMappings.get(new ClassKey(type.getRawClass()));
+ return _find(type);
}
@Override
@@ -170,6 +173,13 @@
JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
- return (_classMappings == null) ? null : _classMappings.get(new ClassKey(type.getRawClass()));
+ return _find(type);
+ }
+
+ private final JsonDeserializer<?> _find(JavaType type) {
+ if (_classMappings == null) {
+ return null;
+ }
+ return _classMappings.get(new ClassKey(type.getRawClass()));
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/module/SimpleModule.java b/src/main/java/com/fasterxml/jackson/databind/module/SimpleModule.java
index f6485b8..b0f20c4 100644
--- a/src/main/java/com/fasterxml/jackson/databind/module/SimpleModule.java
+++ b/src/main/java/com/fasterxml/jackson/databind/module/SimpleModule.java
@@ -33,7 +33,7 @@
protected final String _name;
protected final Version _version;
-
+
protected SimpleSerializers _serializers = null;
protected SimpleDeserializers _deserializers = null;
@@ -251,12 +251,13 @@
/*
/**********************************************************
- /* Configuration methods
+ /* Configuration methods, adding serializers
/**********************************************************
*/
public SimpleModule addSerializer(JsonSerializer<?> ser)
{
+ _checkNotNull(ser, "serializer");
if (_serializers == null) {
_serializers = new SimpleSerializers();
}
@@ -266,6 +267,8 @@
public <T> SimpleModule addSerializer(Class<? extends T> type, JsonSerializer<T> ser)
{
+ _checkNotNull(type, "type to register serializer for");
+ _checkNotNull(ser, "serializer");
if (_serializers == null) {
_serializers = new SimpleSerializers();
}
@@ -275,15 +278,25 @@
public <T> SimpleModule addKeySerializer(Class<? extends T> type, JsonSerializer<T> ser)
{
+ _checkNotNull(type, "type to register key serializer for");
+ _checkNotNull(ser, "key serializer");
if (_keySerializers == null) {
_keySerializers = new SimpleSerializers();
}
_keySerializers.addSerializer(type, ser);
return this;
}
+
+ /*
+ /**********************************************************
+ /* Configuration methods, adding deserializers
+ /**********************************************************
+ */
public <T> SimpleModule addDeserializer(Class<T> type, JsonDeserializer<? extends T> deser)
{
+ _checkNotNull(type, "type to register deserializer for");
+ _checkNotNull(deser, "deserializer");
if (_deserializers == null) {
_deserializers = new SimpleDeserializers();
}
@@ -293,6 +306,8 @@
public SimpleModule addKeyDeserializer(Class<?> type, KeyDeserializer deser)
{
+ _checkNotNull(type, "type to register key deserializer for");
+ _checkNotNull(deser, "key deserializer");
if (_keyDeserializers == null) {
_keyDeserializers = new SimpleKeyDeserializers();
}
@@ -300,6 +315,12 @@
return this;
}
+ /*
+ /**********************************************************
+ /* Configuration methods, type mapping
+ /**********************************************************
+ */
+
/**
* Lazily-constructed resolver used for storing mappings from
* abstract classes to more specific implementing classes
@@ -308,6 +329,8 @@
public <T> SimpleModule addAbstractTypeMapping(Class<T> superType,
Class<? extends T> subType)
{
+ _checkNotNull(superType, "abstract type to map");
+ _checkNotNull(subType, "concrete type to map to");
if (_abstractTypes == null) {
_abstractTypes = new SimpleAbstractTypeResolver();
}
@@ -317,22 +340,6 @@
}
/**
- * Method for registering {@link ValueInstantiator} to use when deserializing
- * instances of type <code>beanType</code>.
- *<p>
- * Instantiator is
- * registered when module is registered for <code>ObjectMapper</code>.
- */
- public SimpleModule addValueInstantiator(Class<?> beanType, ValueInstantiator inst)
- {
- if (_valueInstantiators == null) {
- _valueInstantiators = new SimpleValueInstantiators();
- }
- _valueInstantiators = _valueInstantiators.addValueInstantiator(beanType, inst);
- return this;
- }
-
- /**
* Method for adding set of subtypes to be registered with
* {@link ObjectMapper}
* this is an alternative to using annotations in super type to indicate subtypes.
@@ -343,6 +350,7 @@
_subtypes = new LinkedHashSet<NamedType>(Math.max(16, subtypes.length));
}
for (Class<?> subtype : subtypes) {
+ _checkNotNull(subtype, "subtype to register");
_subtypes.add(new NamedType(subtype));
}
return this;
@@ -359,11 +367,36 @@
_subtypes = new LinkedHashSet<NamedType>(Math.max(16, subtypes.length));
}
for (NamedType subtype : subtypes) {
+ _checkNotNull(subtype, "subtype to register");
_subtypes.add(subtype);
}
return this;
}
+ /*
+ /**********************************************************
+ /* Configuration methods, add other handlers
+ /**********************************************************
+ */
+
+ /**
+ * Method for registering {@link ValueInstantiator} to use when deserializing
+ * instances of type <code>beanType</code>.
+ *<p>
+ * Instantiator is
+ * registered when module is registered for <code>ObjectMapper</code>.
+ */
+ public SimpleModule addValueInstantiator(Class<?> beanType, ValueInstantiator inst)
+ {
+ _checkNotNull(beanType, "class to register value instantiator for");
+ _checkNotNull(inst, "value instantiator");
+ if (_valueInstantiators == null) {
+ _valueInstantiators = new SimpleValueInstantiators();
+ }
+ _valueInstantiators = _valueInstantiators.addValueInstantiator(beanType, inst);
+ return this;
+ }
+
/**
* Method for specifying that annotations define by <code>mixinClass</code>
* should be "mixed in" with annotations that <code>targetType</code>
@@ -374,13 +407,15 @@
*/
public SimpleModule setMixInAnnotation(Class<?> targetType, Class<?> mixinClass)
{
+ _checkNotNull(targetType, "target type");
+ _checkNotNull(mixinClass, "mixin class");
if (_mixins == null) {
_mixins = new HashMap<Class<?>, Class<?>>();
}
_mixins.put(targetType, mixinClass);
return this;
}
-
+
/*
/**********************************************************
/* Module impl
@@ -441,4 +476,21 @@
@Override
public Version version() { return _version; }
+
+ /*
+ /**********************************************************
+ /* Helper methods
+ /**********************************************************
+ */
+
+ /**
+ * @since 2.9
+ */
+ protected void _checkNotNull(Object thingy, String type)
+ {
+ if (thingy == null) {
+ throw new IllegalArgumentException(String.format(
+ "Can not pass `null` as %s", type));
+ }
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java b/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java
index 345834a..3ff154c 100644
--- a/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java
+++ b/src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java
@@ -2,13 +2,13 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.JsonSerializable;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.util.RawValue;
import java.io.IOException;
import java.math.BigDecimal;
+import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
@@ -156,24 +156,20 @@
for (int i = 0; i < size; ++i) { // we'll typically have array list
// For now, assuming it's either BaseJsonNode, JsonSerializable
JsonNode n = c.get(i);
- if (n instanceof BaseJsonNode) {
- ((BaseJsonNode) n).serialize(f, provider);
- } else {
- ((JsonSerializable) n).serialize(f, provider);
- }
+ ((BaseJsonNode) n).serialize(f, provider);
}
f.writeEndArray();
}
@Override
- public void serializeWithType(JsonGenerator jg, SerializerProvider provider, TypeSerializer typeSer)
+ public void serializeWithType(JsonGenerator g, SerializerProvider provider, TypeSerializer typeSer)
throws IOException
{
- typeSer.writeTypePrefixForArray(this, jg);
+ typeSer.writeTypePrefixForArray(this, g);
for (JsonNode n : _children) {
- ((BaseJsonNode)n).serialize(jg, provider);
+ ((BaseJsonNode)n).serialize(g, provider);
}
- typeSer.writeTypeSuffixForArray(this, jg);
+ typeSer.writeTypeSuffixForArray(this, g);
}
/*
@@ -521,6 +517,20 @@
}
/**
+ * Method for adding specified number at the end of this array.
+ *
+ * @return This array node, to allow chaining
+ *
+ * @since 2.9
+ */
+ public ArrayNode add(BigInteger v) {
+ if (v == null) {
+ return addNull();
+ }
+ return _add(numberNode(v));
+ }
+
+ /**
* Method for adding specified String value at the end of this array.
*
* @return This array node, to allow chaining
@@ -729,6 +739,21 @@
}
/**
+ * Method that will insert specified numeric value
+ * at specified position in this array.
+ *
+ * @return This array node, to allow chaining
+ *
+ * @since 2.9
+ */
+ public ArrayNode insert(int index, BigInteger v) {
+ if (v == null) {
+ return insertNull(index);
+ }
+ return _insert(index, numberNode(v));
+ }
+
+ /**
* Method that will insert specified String
* at specified position in this array.
*
diff --git a/src/main/java/com/fasterxml/jackson/databind/node/ContainerNode.java b/src/main/java/com/fasterxml/jackson/databind/node/ContainerNode.java
index f2eb608..fcfb23a 100644
--- a/src/main/java/com/fasterxml/jackson/databind/node/ContainerNode.java
+++ b/src/main/java/com/fasterxml/jackson/databind/node/ContainerNode.java
@@ -96,18 +96,16 @@
return _nodeFactory.numberNode(v);
}
- // was missing from 2.2 and before
- @Override
- public final NumericNode numberNode(BigInteger v) { return _nodeFactory.numberNode(v); }
-
@Override
public final NumericNode numberNode(float v) { return _nodeFactory.numberNode(v); }
@Override
public final NumericNode numberNode(double v) { return _nodeFactory.numberNode(v); }
- @Override
- public final NumericNode numberNode(BigDecimal v) { return (_nodeFactory.numberNode(v)); }
- // // Wrapper types, missing from 2.2 and before
+ @Override
+ public final ValueNode numberNode(BigInteger v) { return _nodeFactory.numberNode(v); }
+ @Override
+ public final ValueNode numberNode(BigDecimal v) { return (_nodeFactory.numberNode(v)); }
+
@Override
public final ValueNode numberNode(Byte v) { return _nodeFactory.numberNode(v); }
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/node/DoubleNode.java b/src/main/java/com/fasterxml/jackson/databind/node/DoubleNode.java
index f268b85..ae14bdf 100644
--- a/src/main/java/com/fasterxml/jackson/databind/node/DoubleNode.java
+++ b/src/main/java/com/fasterxml/jackson/databind/node/DoubleNode.java
@@ -74,7 +74,7 @@
@Override
public float floatValue() { return (float) _value; }
-
+
@Override
public double doubleValue() { return _value; }
@@ -91,11 +91,15 @@
return NumberOutput.toString(_value);
}
+ // @since 2.9
@Override
- public final void serialize(JsonGenerator jg, SerializerProvider provider)
- throws IOException, JsonProcessingException
- {
- jg.writeNumber(_value);
+ public boolean isNaN() {
+ return Double.isNaN(_value) || Double.isInfinite(_value);
+ }
+
+ @Override
+ public final void serialize(JsonGenerator g, SerializerProvider provider) throws IOException {
+ g.writeNumber(_value);
}
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/node/FloatNode.java b/src/main/java/com/fasterxml/jackson/databind/node/FloatNode.java
index b4dc9fb..2d8a182 100644
--- a/src/main/java/com/fasterxml/jackson/databind/node/FloatNode.java
+++ b/src/main/java/com/fasterxml/jackson/databind/node/FloatNode.java
@@ -5,6 +5,7 @@
import java.math.BigInteger;
import com.fasterxml.jackson.core.*;
+import com.fasterxml.jackson.core.io.NumberOutput;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
@@ -74,7 +75,7 @@
@Override
public float floatValue() { return _value; }
-
+
@Override
public double doubleValue() { return _value; }
@@ -88,16 +89,18 @@
@Override
public String asText() {
- // As per [jackson-databind#707]
-// return NumberOutput.toString(_value);
- // TODO: in 2.7, call `NumberOutput.toString (added in 2.6); not yet for backwards compat
- return Float.toString(_value);
+ return NumberOutput.toString(_value);
+ }
+
+ // @since 2.9
+ @Override
+ public boolean isNaN() {
+ return Float.isNaN(_value) || Float.isInfinite(_value);
}
@Override
- public final void serialize(JsonGenerator jg, SerializerProvider provider) throws IOException
- {
- jg.writeNumber(_value);
+ public final void serialize(JsonGenerator g, SerializerProvider provider) throws IOException {
+ g.writeNumber(_value);
}
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/node/JsonNodeFactory.java b/src/main/java/com/fasterxml/jackson/databind/node/JsonNodeFactory.java
index 70ec2f7..e933bc8 100644
--- a/src/main/java/com/fasterxml/jackson/databind/node/JsonNodeFactory.java
+++ b/src/main/java/com/fasterxml/jackson/databind/node/JsonNodeFactory.java
@@ -183,11 +183,11 @@
* {@link NumericNode}, but just {@link ValueNode}.
*/
@Override
- public ValueNode numberNode(Long value) {
- if (value == null) {
+ public ValueNode numberNode(Long v) {
+ if (v == null) {
return nullNode();
}
- return LongNode.valueOf(value.longValue());
+ return LongNode.valueOf(v.longValue());
}
/**
@@ -195,7 +195,12 @@
* that expresses given unlimited range integer value
*/
@Override
- public NumericNode numberNode(BigInteger v) { return BigIntegerNode.valueOf(v); }
+ public ValueNode numberNode(BigInteger v) {
+ if (v == null) {
+ return nullNode();
+ }
+ return BigIntegerNode.valueOf(v);
+ }
/**
* Factory method for getting an instance of JSON numeric value
@@ -244,8 +249,12 @@
* @see #JsonNodeFactory(boolean)
*/
@Override
- public NumericNode numberNode(BigDecimal v)
+ public ValueNode numberNode(BigDecimal v)
{
+ if (v == null) {
+ return nullNode();
+ }
+
/*
* If the user wants the exact representation of this big decimal,
* return the value directly
diff --git a/src/main/java/com/fasterxml/jackson/databind/node/NumericNode.java b/src/main/java/com/fasterxml/jackson/databind/node/NumericNode.java
index 3a80971..a70a6b1 100644
--- a/src/main/java/com/fasterxml/jackson/databind/node/NumericNode.java
+++ b/src/main/java/com/fasterxml/jackson/databind/node/NumericNode.java
@@ -33,13 +33,13 @@
@Override public abstract boolean canConvertToInt();
@Override public abstract boolean canConvertToLong();
-
+
/*
/**********************************************************
/* General type coercions
/**********************************************************
*/
-
+
@Override
public abstract String asText();
@@ -47,6 +47,7 @@
public final int asInt() {
return intValue();
}
+
@Override
public final int asInt(int defaultValue) {
return intValue();
@@ -56,17 +57,37 @@
public final long asLong() {
return longValue();
}
+
@Override
public final long asLong(long defaultValue) {
return longValue();
}
-
+
@Override
public final double asDouble() {
return doubleValue();
}
+
@Override
public final double asDouble(double defaultValue) {
return doubleValue();
}
+
+ /*
+ /**********************************************************
+ /* Other
+ /**********************************************************
+ */
+
+ /**
+ * Convenience method for checking whether this node is a
+ * {@link FloatNode} or {@link DoubleNode} that contains
+ * "not-a-number" (NaN) value.
+ *
+ * @since 2.9
+ */
+ public boolean isNaN() {
+ return false;
+ }
+
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java b/src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java
index 387943e..673c332 100644
--- a/src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java
+++ b/src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java
@@ -7,6 +7,7 @@
import java.io.IOException;
import java.math.BigDecimal;
+import java.math.BigInteger;
import java.util.*;
/**
@@ -761,6 +762,18 @@
}
/**
+ * Method for setting value of a field to specified numeric value.
+ *
+ * @return This node (to allow chaining)
+ *
+ * @since 2.9
+ */
+ public ObjectNode put(String fieldName, BigInteger v) {
+ return _put(fieldName, (v == null) ? nullNode()
+ : numberNode(v));
+ }
+
+ /**
* Method for setting value of a field to specified String value.
*
* @return This node (to allow chaining)
diff --git a/src/main/java/com/fasterxml/jackson/databind/node/TextNode.java b/src/main/java/com/fasterxml/jackson/databind/node/TextNode.java
index 2e595a1..7d4d5e2 100644
--- a/src/main/java/com/fasterxml/jackson/databind/node/TextNode.java
+++ b/src/main/java/com/fasterxml/jackson/databind/node/TextNode.java
@@ -6,7 +6,9 @@
import com.fasterxml.jackson.core.io.CharTypes;
import com.fasterxml.jackson.core.io.NumberInput;
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
+
import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.exc.InvalidFormatException;
/**
* Value node that contains a text value.
@@ -60,93 +62,16 @@
@SuppressWarnings("resource")
public byte[] getBinaryValue(Base64Variant b64variant) throws IOException
{
- ByteArrayBuilder builder = new ByteArrayBuilder(100);
- final String str = _value;
- int ptr = 0;
- int len = str.length();
-
- main_loop:
- while (ptr < len) {
- // first, we'll skip preceding white space, if any
- char ch;
- do {
- ch = str.charAt(ptr++);
- if (ptr >= len) {
- break main_loop;
- }
- } while (ch <= ' ');
- int bits = b64variant.decodeBase64Char(ch);
- if (bits < 0) {
- _reportInvalidBase64(b64variant, ch, 0);
- }
- int decodedData = bits;
- // then second base64 char; can't get padding yet, nor ws
- if (ptr >= len) {
- _reportBase64EOF();
- }
- ch = str.charAt(ptr++);
- bits = b64variant.decodeBase64Char(ch);
- if (bits < 0) {
- _reportInvalidBase64(b64variant, ch, 1);
- }
- decodedData = (decodedData << 6) | bits;
- // third base64 char; can be padding, but not ws
- if (ptr >= len) {
- // but as per [JACKSON-631] can be end-of-input, iff not using padding
- if (!b64variant.usesPadding()) {
- // Got 12 bits, only need 8, need to shift
- decodedData >>= 4;
- builder.append(decodedData);
- break;
- }
- _reportBase64EOF();
- }
- ch = str.charAt(ptr++);
- bits = b64variant.decodeBase64Char(ch);
-
- // First branch: can get padding (-> 1 byte)
- if (bits < 0) {
- if (bits != Base64Variant.BASE64_VALUE_PADDING) {
- _reportInvalidBase64(b64variant, ch, 2);
- }
- // Ok, must get padding
- if (ptr >= len) {
- _reportBase64EOF();
- }
- ch = str.charAt(ptr++);
- if (!b64variant.usesPaddingChar(ch)) {
- _reportInvalidBase64(b64variant, ch, 3, "expected padding character '"+b64variant.getPaddingChar()+"'");
- }
- // Got 12 bits, only need 8, need to shift
- decodedData >>= 4;
- builder.append(decodedData);
- continue;
- }
- // Nope, 2 or 3 bytes
- decodedData = (decodedData << 6) | bits;
- // fourth and last base64 char; can be padding, but not ws
- if (ptr >= len) {
- // but as per [JACKSON-631] can be end-of-input, iff not using padding
- if (!b64variant.usesPadding()) {
- decodedData >>= 2;
- builder.appendTwoBytes(decodedData);
- break;
- }
- _reportBase64EOF();
- }
- ch = str.charAt(ptr++);
- bits = b64variant.decodeBase64Char(ch);
- if (bits < 0) {
- if (bits != Base64Variant.BASE64_VALUE_PADDING) {
- _reportInvalidBase64(b64variant, ch, 3);
- }
- decodedData >>= 2;
- builder.appendTwoBytes(decodedData);
- } else {
- // otherwise, our triple is now complete
- decodedData = (decodedData << 6) | bits;
- builder.appendThreeBytes(decodedData);
- }
+ final String str = _value.trim();
+ ByteArrayBuilder builder = new ByteArrayBuilder(4 + ((str.length() * 3) << 2));
+ try {
+ b64variant.decode(str, builder);
+ } catch (IllegalArgumentException e) {
+ throw InvalidFormatException.from(null,
+ String.format(
+"Can not access contents of TextNode as binary due to broken Base64 encoding: %s",
+e.getMessage()),
+ str, byte[].class);
}
return builder.toByteArray();
}
@@ -155,7 +80,7 @@
public byte[] binaryValue() throws IOException {
return getBinaryValue(Base64Variants.getDefaultVariant());
}
-
+
/*
/**********************************************************
/* General type coercions
@@ -210,12 +135,12 @@
*/
@Override
- public final void serialize(JsonGenerator jg, SerializerProvider provider) throws IOException
+ public final void serialize(JsonGenerator g, SerializerProvider provider) throws IOException
{
if (_value == null) {
- jg.writeNull();
+ g.writeNull();
} else {
- jg.writeString(_value);
+ g.writeString(_value);
}
}
@@ -258,44 +183,4 @@
CharTypes.appendQuoted(sb, content);
sb.append('"');
}
-
- /*
- /**********************************************************
- /* Helper methods
- /**********************************************************
- */
-
- protected void _reportInvalidBase64(Base64Variant b64variant, char ch, int bindex)
- throws JsonParseException
- {
- _reportInvalidBase64(b64variant, ch, bindex, null);
- }
-
- /**
- * @param bindex Relative index within base64 character unit; between 0
- * and 3 (as unit has exactly 4 characters)
- */
- protected void _reportInvalidBase64(Base64Variant b64variant, char ch, int bindex, String msg)
- throws JsonParseException
- {
- String base;
- if (ch <= ' ') {
- base = "Illegal white space character (code 0x"+Integer.toHexString(ch)+") as character #"+(bindex+1)+" of 4-char base64 unit: can only used between units";
- } else if (b64variant.usesPaddingChar(ch)) {
- base = "Unexpected padding character ('"+b64variant.getPaddingChar()+"') as character #"+(bindex+1)+" of 4-char base64 unit: padding only legal as 3rd or 4th character";
- } else if (!Character.isDefined(ch) || Character.isISOControl(ch)) {
- // Not sure if we can really get here... ? (most illegal xml chars are caught at lower level)
- base = "Illegal character (code 0x"+Integer.toHexString(ch)+") in base64 content";
- } else {
- base = "Illegal character '"+ch+"' (code 0x"+Integer.toHexString(ch)+") in base64 content";
- }
- if (msg != null) {
- base = base + ": " + msg;
- }
- throw new JsonParseException(null, base, JsonLocation.NA);
- }
-
- protected void _reportBase64EOF() throws JsonParseException {
- throw new JsonParseException(null, "Unexpected end-of-String when base64 content");
- }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/node/TreeTraversingParser.java b/src/main/java/com/fasterxml/jackson/databind/node/TreeTraversingParser.java
index 9ede6bd..a57c14e 100644
--- a/src/main/java/com/fasterxml/jackson/databind/node/TreeTraversingParser.java
+++ b/src/main/java/com/fasterxml/jackson/databind/node/TreeTraversingParser.java
@@ -334,6 +334,17 @@
return null;
}
+ @Override
+ public boolean isNaN() {
+ if (!_closed) {
+ JsonNode n = currentNode();
+ if (n instanceof NumericNode) {
+ return ((NumericNode) n).isNaN();
+ }
+ }
+ return false;
+ }
+
/*
/**********************************************************
/* Public API, typed binary (base64) access
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/AnyGetterWriter.java b/src/main/java/com/fasterxml/jackson/databind/ser/AnyGetterWriter.java
index bedf67e..db14484 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/AnyGetterWriter.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/AnyGetterWriter.java
@@ -38,7 +38,7 @@
}
/**
- * @since 0.8.3
+ * @since 2.8.3
*/
public void fixAccess(SerializationConfig config) {
_accessor.fixAccess(
@@ -53,8 +53,9 @@
return;
}
if (!(value instanceof Map<?,?>)) {
- provider.reportMappingProblem("Value returned by 'any-getter' %s() not java.util.Map but %s",
- _accessor.getName(), value.getClass().getName());
+ provider.reportBadDefinition(_property.getType(), String.format(
+ "Value returned by 'any-getter' %s() not java.util.Map but %s",
+ _accessor.getName(), value.getClass().getName()));
}
// 23-Feb-2015, tatu: Nasty, but has to do (for now)
if (_mapSerializer != null) {
@@ -69,15 +70,16 @@
*/
public void getAndFilter(Object bean, JsonGenerator gen, SerializerProvider provider,
PropertyFilter filter)
- throws Exception
+ throws Exception
{
Object value = _accessor.getValue(bean);
if (value == null) {
return;
}
if (!(value instanceof Map<?,?>)) {
- provider.reportMappingProblem("Value returned by 'any-getter' (%s()) not java.util.Map but %s",
- _accessor.getName(), value.getClass().getName());
+ provider.reportBadDefinition(_property.getType(),
+ String.format("Value returned by 'any-getter' (%s()) not java.util.Map but %s",
+ _accessor.getName(), value.getClass().getName()));
}
// 19-Oct-2014, tatu: Should we try to support @JsonInclude options here?
if (_mapSerializer != null) {
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java b/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java
index 29c4a49..aff6214 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java
@@ -1,6 +1,5 @@
package com.fasterxml.jackson.databind.ser;
-import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
@@ -57,7 +56,7 @@
* not instances
*/
protected final static HashMap<String, Class<? extends JsonSerializer<?>>> _concreteLazy;
-
+
static {
HashMap<String, Class<? extends JsonSerializer<?>>> concLazy
= new HashMap<String, Class<? extends JsonSerializer<?>>>();
@@ -94,12 +93,10 @@
Object value = en.getValue();
if (value instanceof JsonSerializer<?>) {
concrete.put(en.getKey().getName(), (JsonSerializer<?>) value);
- } else if (value instanceof Class<?>) {
+ } else {
@SuppressWarnings("unchecked")
Class<? extends JsonSerializer<?>> cls = (Class<? extends JsonSerializer<?>>) value;
concLazy.put(en.getKey().getName(), cls);
- } else { // should never happen, but:
- throw new IllegalStateException("Internal error: unrecognized value of type "+en.getClass().getName());
}
}
@@ -226,14 +223,14 @@
// As per [databind#47], also need to support @JsonValue
if (ser == null) {
beanDesc = config.introspect(keyType);
- AnnotatedMethod am = beanDesc.findJsonValueMethod();
+ AnnotatedMember am = beanDesc.findJsonValueAccessor();
if (am != null) {
- final Class<?> rawType = am.getRawReturnType();
+ final Class<?> rawType = am.getRawType();
JsonSerializer<?> delegate = StdKeySerializers.getStdKeySerializer(config,
rawType, true);
- Method m = am.getAnnotated();
if (config.canOverrideAccessModifiers()) {
- ClassUtil.checkAndFixAccess(m, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
+ ClassUtil.checkAndFixAccess(am.getMember(),
+ config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
}
ser = new JsonValueSerializer(am, delegate);
} else {
@@ -310,12 +307,10 @@
if (ser == null) {
Class<? extends JsonSerializer<?>> serClass = _concreteLazy.get(clsName);
if (serClass != null) {
- try {
- return serClass.newInstance();
- } catch (Exception e) {
- throw new IllegalStateException("Failed to instantiate standard serializer (of type "+serClass.getName()+"): "
- +e.getMessage(), e);
- }
+ // 07-Jan-2017, tatu: Should never fail (since we control constructors),
+ // but if it does will throw `IllegalArgumentException` with description,
+ // which we could catch, re-title.
+ return ClassUtil.createInstance(serClass, false);
}
}
return ser;
@@ -347,14 +342,14 @@
return SerializableSerializer.instance;
}
// Second: @JsonValue for any type
- AnnotatedMethod valueMethod = beanDesc.findJsonValueMethod();
- if (valueMethod != null) {
- Method m = valueMethod.getAnnotated();
+ AnnotatedMember valueAccessor = beanDesc.findJsonValueAccessor();
+ if (valueAccessor != null) {
if (prov.canOverrideAccessModifiers()) {
- ClassUtil.checkAndFixAccess(m, prov.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
+ ClassUtil.checkAndFixAccess(valueAccessor.getMember(),
+ prov.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
}
- JsonSerializer<Object> ser = findSerializerFromAnnotation(prov, valueMethod);
- return new JsonValueSerializer(valueMethod, ser);
+ JsonSerializer<Object> ser = findSerializerFromAnnotation(prov, valueAccessor);
+ return new JsonValueSerializer(valueAccessor, ser);
}
// No well-known annotations...
return null;
@@ -393,7 +388,7 @@
// 28-Apr-2015, tatu: TypeFactory does it all for us already so
JavaType kt = mapEntryType.containedTypeOrUnknown(0);
JavaType vt = mapEntryType.containedTypeOrUnknown(1);
- return buildMapEntrySerializer(prov.getConfig(), type, beanDesc, staticTyping, kt, vt);
+ return buildMapEntrySerializer(prov, type, beanDesc, staticTyping, kt, vt);
}
if (ByteBuffer.class.isAssignableFrom(raw)) {
return new ByteBufferSerializer();
@@ -544,7 +539,7 @@
* leave it as is; no clean way to make it work.
*/
if (!staticTyping && type.useStaticType()) {
- if (!type.isContainerType() || type.getContentType().getRawClass() != Object.class) {
+ if (!type.isContainerType() || !type.getContentType().isJavaLangObject()) {
staticTyping = true;
}
}
@@ -660,7 +655,7 @@
// We may also want to use serialize Collections "as beans", if (and only if)
// this is specified with `@JsonFormat(shape=Object)`
JsonFormat.Value format = beanDesc.findExpectedFormat(null);
- if (format != null && format.getShape() == JsonFormat.Shape.OBJECT) {
+ if ((format != null) && format.getShape() == JsonFormat.Shape.OBJECT) {
return null;
}
Class<?> raw = type.getRawClass();
@@ -677,7 +672,7 @@
if (isIndexedList(raw)) {
if (elementRaw == String.class) {
// [JACKSON-829] Must NOT use if we have custom serializer
- if (elementValueSerializer == null || ClassUtil.isJacksonStdImpl(elementValueSerializer)) {
+ if (ClassUtil.isJacksonStdImpl(elementValueSerializer)) {
ser = IndexedStringListSerializer.instance;
}
} else {
@@ -686,7 +681,7 @@
}
} else if (elementRaw == String.class) {
// [JACKSON-829] Must NOT use if we have custom serializer
- if (elementValueSerializer == null || ClassUtil.isJacksonStdImpl(elementValueSerializer)) {
+ if (ClassUtil.isJacksonStdImpl(elementValueSerializer)) {
ser = StringCollectionSerializer.instance;
}
}
@@ -721,6 +716,7 @@
boolean staticTyping, TypeSerializer vts, JsonSerializer<Object> valueSerializer) {
return new IndexedListSerializer(elemType, staticTyping, vts, valueSerializer);
}
+
public ContainerSerializer<?> buildCollectionSerializer(JavaType elemType,
boolean staticTyping, TypeSerializer vts, JsonSerializer<Object> valueSerializer) {
return new CollectionSerializer(elemType, staticTyping, vts, valueSerializer);
@@ -735,7 +731,7 @@
/* Factory methods, for Maps
/**********************************************************
*/
-
+
/**
* Helper method that handles configuration details when constructing serializers for
* {@link java.util.Map} types.
@@ -746,7 +742,13 @@
TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer)
throws JsonMappingException
{
- final SerializationConfig config = prov.getConfig();
+ // [databind#467]: This is where we could allow serialization "as POJO": But! It's
+ // nasty to undo, and does not apply on per-property basis. So, hardly optimal
+ JsonFormat.Value format = beanDesc.findExpectedFormat(null);
+ if ((format != null) && format.getShape() == JsonFormat.Shape.OBJECT) {
+ return null;
+ }
+
JsonSerializer<?> ser = null;
// Order of lookups:
@@ -754,6 +756,7 @@
// 2. Annotations (@JsonValue, @JsonDeserialize)
// 3. Defaults
+ final SerializationConfig config = prov.getConfig();
for (Serializers serializers : customSerializers()) { // (1) Custom
ser = serializers.findMapSerializer(config, type, beanDesc,
keySerializer, elementTypeSerializer, elementValueSerializer);
@@ -774,12 +777,7 @@
MapSerializer mapSer = MapSerializer.construct(ignored,
type, staticTyping, elementTypeSerializer,
keySerializer, elementValueSerializer, filterId);
- Object suppressableValue = findSuppressableContentValue(config,
- type.getContentType(), beanDesc);
- if (suppressableValue != null) {
- mapSer = mapSer.withContentInclusion(suppressableValue);
- }
- ser = mapSer;
+ ser = _checkMapContentInclusion(prov, beanDesc, mapSer);
}
}
// [databind#120]: Allow post-processing
@@ -792,44 +790,174 @@
}
/**
- *<p>
- * NOTE: although return type is left opaque, it really needs to be
- * <code>JsonInclude.Include</code> for things to work as expected.
+ * Helper method that does figures out content inclusion value to use, if any,
+ * and construct re-configured {@link MapSerializer} appropriately.
*
- * @since 2.5
+ * @since 2.9
*/
- protected Object findSuppressableContentValue(SerializationConfig config,
- JavaType contentType, BeanDescription beanDesc)
+ @SuppressWarnings("deprecation")
+ protected MapSerializer _checkMapContentInclusion(SerializerProvider prov,
+ BeanDescription beanDesc, MapSerializer mapSer)
throws JsonMappingException
{
- /* 16-Apr-2016, tatu: Should this consider possible property-config overrides?
- * Quite possibly yes, but would need to carefully check that content type being
- * used is appropriate.
- */
- JsonInclude.Value inclV = beanDesc.findPropertyInclusion(config.getDefaultPropertyInclusion());
+ final JavaType contentType = mapSer.getContentType();
+ JsonInclude.Value inclV = _findInclusionWithContent(prov, beanDesc,
+ contentType, Map.class);
- if (inclV == null) {
- return null;
+ // Need to support global legacy setting, for now:
+ JsonInclude.Include incl = (inclV == null) ? JsonInclude.Include.USE_DEFAULTS : inclV.getContentInclusion();
+ if (incl == JsonInclude.Include.USE_DEFAULTS
+ || incl == JsonInclude.Include.ALWAYS) {
+ if (!prov.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES)) {
+ return mapSer.withContentInclusion(null, true);
+ }
+ return mapSer;
}
- JsonInclude.Include incl = inclV.getContentInclusion();
+
+ // NOTE: mostly copied from `PropertyBuilder`; would be nice to refactor
+ // but code is not identical nor are these types related
+ Object valueToSuppress;
+ boolean suppressNulls = true; // almost always, but possibly not with CUSTOM
+
switch (incl) {
- case USE_DEFAULTS: // means "dunno"
- return null;
case NON_DEFAULT:
- // 19-Oct-2014, tatu: Not sure what this'd mean; so take it to mean "NON_EMPTY"...
- // 11-Nov-2015, tatu: With 2.6, we did indeed revert to "NON_EMPTY", but that did
- // not go well, so with 2.7, we'll do this instead...
- // But not 100% sure if we ought to call new `JsonSerializer.findDefaultValue()`;
- // to do that, would need to locate said serializer
-// incl = JsonInclude.Include.NON_EMPTY;
+ valueToSuppress = BeanUtil.getDefaultValue(contentType);
+ if (valueToSuppress != null) {
+ if (valueToSuppress.getClass().isArray()) {
+ valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
+ }
+ }
break;
- default:
- // all other modes actually good as is, unless we'll find better ways
+ case NON_ABSENT: // new with 2.6, to support Guava/JDK8 Optionals
+ // and for referential types, also "empty", which in their case means "absent"
+ valueToSuppress = contentType.isReferenceType()
+ ? MapSerializer.MARKER_FOR_EMPTY : null;
+ break;
+ case NON_EMPTY:
+ valueToSuppress = MapSerializer.MARKER_FOR_EMPTY;
+ break;
+ case CUSTOM: // new with 2.9
+ valueToSuppress = prov.includeFilterInstance(null, inclV.getContentFilter());
+ if (valueToSuppress == null) { // is this legal?
+ suppressNulls = true;
+ } else {
+ suppressNulls = prov.includeFilterSuppressNulls(valueToSuppress);
+ }
+ break;
+ case NON_NULL:
+ default: // should not matter but...
+ valueToSuppress = null;
break;
}
- return incl;
+ return mapSer.withContentInclusion(valueToSuppress, suppressNulls);
}
+ /**
+ * @since 2.9
+ */
+ protected JsonSerializer<?> buildMapEntrySerializer(SerializerProvider prov,
+ JavaType type, BeanDescription beanDesc, boolean staticTyping,
+ JavaType keyType, JavaType valueType)
+ throws JsonMappingException
+ {
+ // [databind#865]: Allow serialization "as POJO" -- note: to undo, declare
+ // serialization as `Shape.NATURAL` instead; that's JSON Object too.
+ JsonFormat.Value format = beanDesc.findExpectedFormat(null);
+ if (format != null && format.getShape() == JsonFormat.Shape.OBJECT) {
+ return null;
+ }
+ MapEntrySerializer ser = new MapEntrySerializer(valueType, keyType, valueType,
+ staticTyping, createTypeSerializer(prov.getConfig(), valueType), null);
+
+ final JavaType contentType = ser.getContentType();
+ JsonInclude.Value inclV = _findInclusionWithContent(prov, beanDesc,
+ contentType, Map.Entry.class);
+
+ // Need to support global legacy setting, for now:
+ JsonInclude.Include incl = (inclV == null) ? JsonInclude.Include.USE_DEFAULTS : inclV.getContentInclusion();
+ if (incl == JsonInclude.Include.USE_DEFAULTS
+ || incl == JsonInclude.Include.ALWAYS) {
+ return ser;
+ }
+
+ // NOTE: mostly copied from `PropertyBuilder`; would be nice to refactor
+ // but code is not identical nor are these types related
+ Object valueToSuppress;
+ boolean suppressNulls = true; // almost always, but possibly not with CUSTOM
+
+ switch (incl) {
+ case NON_DEFAULT:
+ valueToSuppress = BeanUtil.getDefaultValue(contentType);
+ if (valueToSuppress != null) {
+ if (valueToSuppress.getClass().isArray()) {
+ valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
+ }
+ }
+ break;
+ case NON_ABSENT:
+ valueToSuppress = contentType.isReferenceType()
+ ? MapSerializer.MARKER_FOR_EMPTY : null;
+ break;
+ case NON_EMPTY:
+ valueToSuppress = MapSerializer.MARKER_FOR_EMPTY;
+ break;
+ case CUSTOM:
+ valueToSuppress = prov.includeFilterInstance(null, inclV.getContentFilter());
+ if (valueToSuppress == null) { // is this legal?
+ suppressNulls = true;
+ } else {
+ suppressNulls = prov.includeFilterSuppressNulls(valueToSuppress);
+ }
+ break;
+ case NON_NULL:
+ default: // should not matter but...
+ valueToSuppress = null;
+ break;
+ }
+ return ser.withContentInclusion(valueToSuppress, suppressNulls);
+ }
+
+ /**
+ * Helper method used for finding inclusion definitions for structured
+ * container types like <code>Map</code>s and referential types
+ * (like <code>AtomicReference</code>).
+ *
+ * @param contentType Declared full content type of container
+ * @param configType Raw base type under which `configOverride`, if any, needs to be defined
+ */
+ protected JsonInclude.Value _findInclusionWithContent(SerializerProvider prov,
+ BeanDescription beanDesc,
+ JavaType contentType, Class<?> configType)
+ throws JsonMappingException
+ {
+ final SerializationConfig config = prov.getConfig();
+
+ // Defaulting gets complicated because we might have two distinct
+ // axis to consider: Container type itself , and then value (content) type.
+ // Start with Container-defaults, then use more-specific value override, if any.
+
+ // Start by getting global setting, overridden by Map-type-override
+ JsonInclude.Value inclV = beanDesc.findPropertyInclusion(config.getDefaultPropertyInclusion());
+ inclV = config.getDefaultPropertyInclusion(configType, inclV);
+
+ // and then merge content-type overrides, if any. But note that there's
+ // content-to-value inclusion shift we have to do
+ JsonInclude.Value valueIncl = config.getDefaultPropertyInclusion(contentType.getRawClass(), null);
+
+ if (valueIncl != null) {
+ switch (valueIncl.getValueInclusion()) {
+ case USE_DEFAULTS:
+ break;
+ case CUSTOM:
+ inclV = inclV.withContentFilter(valueIncl.getContentFilter());
+ break;
+ default:
+ inclV = inclV.withContentInclusion(valueIncl.getValueInclusion());
+ }
+ }
+ return inclV;
+ }
+
/*
/**********************************************************
/* Factory methods, for Arrays
@@ -914,18 +1042,6 @@
return new IterableSerializer(valueType, staticTyping, createTypeSerializer(config, valueType));
}
- /**
- * @since 2.5
- */
- protected JsonSerializer<?> buildMapEntrySerializer(SerializationConfig config,
- JavaType type, BeanDescription beanDesc, boolean staticTyping,
- JavaType keyType, JavaType valueType)
- throws JsonMappingException
- {
- return new MapEntrySerializer(valueType, keyType, valueType,
- staticTyping, createTypeSerializer(config, valueType), null);
- }
-
protected JsonSerializer<?> buildEnumSerializer(SerializationConfig config,
JavaType type, BeanDescription beanDesc)
throws JsonMappingException
@@ -1027,6 +1143,8 @@
return config.isEnabled(MapperFeature.USE_STATIC_TYPING);
}
+ // Commented out in 2.9
+ /*
protected Class<?> _verifyAsClass(Object src, String methodName, Class<?> noneClass)
{
if (src == null) {
@@ -1041,4 +1159,5 @@
}
return cls;
}
+ */
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java b/src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java
index 49c87af..a7b4584 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java
@@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.std.BeanSerializerBase;
import com.fasterxml.jackson.databind.util.Annotations;
+import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.NameTransformer;
/**
@@ -201,19 +202,23 @@
/***********************************************************
*/
+ /**
+ * @since 2.9 (added `includeInViews` since 2.8)
+ */
@SuppressWarnings("unchecked")
public BeanPropertyWriter(BeanPropertyDefinition propDef,
AnnotatedMember member, Annotations contextAnnotations,
- JavaType declaredType, JsonSerializer<?> ser,
- TypeSerializer typeSer, JavaType serType, boolean suppressNulls,
- Object suppressableValue) {
+ JavaType declaredType,
+ JsonSerializer<?> ser, TypeSerializer typeSer, JavaType serType,
+ boolean suppressNulls, Object suppressableValue,
+ Class<?>[] includeInViews)
+ {
super(propDef);
_member = member;
_contextAnnotations = contextAnnotations;
_name = new SerializedString(propDef.getName());
_wrapperName = propDef.getWrapperName();
- _includeInViews = propDef.findViews();
_declaredType = declaredType;
_serializer = (JsonSerializer<Object>) ser;
@@ -239,6 +244,19 @@
// this will be resolved later on, unless nulls are to be suppressed
_nullSerializer = null;
+ _includeInViews = includeInViews;
+ }
+
+ @Deprecated // Since 2.9
+ public BeanPropertyWriter(BeanPropertyDefinition propDef,
+ AnnotatedMember member, Annotations contextAnnotations,
+ JavaType declaredType,
+ JsonSerializer<?> ser, TypeSerializer typeSer, JavaType serType,
+ boolean suppressNulls, Object suppressableValue)
+ {
+ this(propDef, member, contextAnnotations, declaredType,
+ ser, typeSer, serType, suppressNulls, suppressableValue,
+ null);
}
/**
@@ -372,8 +390,10 @@
*/
public void assignSerializer(JsonSerializer<Object> ser) {
// may need to disable check in future?
- if (_serializer != null && _serializer != ser) {
- throw new IllegalStateException("Can not override serializer");
+ if ((_serializer != null) && (_serializer != ser)) {
+ throw new IllegalStateException(String.format(
+ "Can not override _serializer: had a %s, trying to set to %s",
+ ClassUtil.classNameOf(_serializer), ClassUtil.classNameOf(ser)));
}
_serializer = ser;
}
@@ -384,7 +404,9 @@
public void assignNullSerializer(JsonSerializer<Object> nullSer) {
// may need to disable check in future?
if ((_nullSerializer != null) && (_nullSerializer != nullSer)) {
- throw new IllegalStateException("Can not override null serializer");
+ throw new IllegalStateException(String.format(
+ "Can not override _nullSerializer: had a %s, trying to set to %s",
+ ClassUtil.classNameOf(_nullSerializer), ClassUtil.classNameOf(nullSer)));
}
_nullSerializer = nullSer;
}
@@ -605,6 +627,7 @@
return _cfgSerializationType;
}
+ @Deprecated // since 2.9
public Class<?> getRawSerializationType() {
return (_cfgSerializationType == null) ? null : _cfgSerializationType
.getRawClass();
@@ -662,7 +685,7 @@
SerializerProvider prov) throws Exception {
// inlined 'get()'
final Object value = (_accessorMethod == null) ? _field.get(bean)
- : _accessorMethod.invoke(bean);
+ : _accessorMethod.invoke(bean, (Object[]) null);
// Null handling is bit different, check that first
if (value == null) {
@@ -734,7 +757,7 @@
SerializerProvider prov) throws Exception {
// inlined 'get()'
final Object value = (_accessorMethod == null) ? _field.get(bean)
- : _accessorMethod.invoke(bean);
+ : _accessorMethod.invoke(bean, (Object[]) null);
if (value == null) { // nulls need specialized handling
if (_nullSerializer != null) {
_nullSerializer.serialize(null, gen, prov);
@@ -890,7 +913,7 @@
*/
public final Object get(Object bean) throws Exception {
return (_accessorMethod == null) ? _field.get(bean) : _accessorMethod
- .invoke(bean);
+ .invoke(bean, (Object[]) null);
}
/**
@@ -918,7 +941,7 @@
// (something
// OTHER than {@link BeanSerializerBase}
if (ser instanceof BeanSerializerBase) {
- prov.reportMappingProblem("Direct self-reference leading to cycle");
+ prov.reportBadDefinition(getType(), "Direct self-reference leading to cycle");
}
}
return false;
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializer.java
index 7dc0d17..a0b2d8f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializer.java
@@ -26,7 +26,7 @@
public class BeanSerializer
extends BeanSerializerBase
{
- private static final long serialVersionUID = -3618164443537292758L;
+ private static final long serialVersionUID = 29; // as per jackson 2.9
/*
/**********************************************************
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerBuilder.java b/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerBuilder.java
index 95613e1..c89d380 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerBuilder.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerBuilder.java
@@ -37,7 +37,7 @@
/**
* Bean properties, in order of serialization
*/
- protected List<BeanPropertyWriter> _properties;
+ protected List<BeanPropertyWriter> _properties = Collections.emptyList();
/**
* Optional array of filtered property writers; if null, no
@@ -66,13 +66,13 @@
* type, if any.
*/
protected ObjectIdWriter _objectIdWriter;
-
+
/*
/**********************************************************
/* Construction and setter methods
/**********************************************************
*/
-
+
public BeanSerializerBuilder(BeanDescription beanDesc) {
_beanDesc = beanDesc;
}
@@ -105,10 +105,22 @@
_properties = properties;
}
+ /**
+ * @param properties Number and order of properties here MUST match that
+ * of "regular" properties set earlier using {@link #setProperties(List)}; if not,
+ * an {@link IllegalArgumentException} will be thrown
+ */
public void setFilteredProperties(BeanPropertyWriter[] properties) {
+ if (properties != null) {
+ if (properties.length != _properties.size()) { // as per [databind#1612]
+ throw new IllegalArgumentException(String.format(
+ "Trying to set %d filtered properties; must match length of non-filtered `properties` (%d)",
+ properties.length, _properties.size()));
+ }
+ }
_filteredProperties = properties;
}
-
+
public void setAnyGetter(AnyGetterWriter anyGetter) {
_anyGetter = anyGetter;
}
@@ -185,6 +197,14 @@
}
}
}
+ // 27-Apr-2017, tatu: Verify that filtered-properties settings are compatible
+ if (_filteredProperties != null) {
+ if (_filteredProperties.length != _properties.size()) {
+ throw new IllegalStateException(String.format(
+"Mismatch between `properties` size (%d), `filteredProperties` (%s): should have as many (or `null` for latter)",
+_properties.size(), _filteredProperties.length));
+ }
+ }
if (_anyGetter != null) {
_anyGetter.fixAccess(_config);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java b/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java
index 921d67e..50436bb 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java
@@ -4,12 +4,12 @@
import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.databind.*;
-import com.fasterxml.jackson.databind.cfg.ConfigOverride;
import com.fasterxml.jackson.databind.cfg.SerializerFactoryConfig;
import com.fasterxml.jackson.databind.introspect.*;
import com.fasterxml.jackson.databind.jsontype.NamedType;
@@ -22,6 +22,8 @@
import com.fasterxml.jackson.databind.ser.std.MapSerializer;
import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer;
import com.fasterxml.jackson.databind.type.ReferenceType;
+import com.fasterxml.jackson.databind.util.ArrayBuilders;
+import com.fasterxml.jackson.databind.util.BeanUtil;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.Converter;
@@ -110,7 +112,7 @@
protected Iterable<Serializers> customSerializers() {
return _factoryConfig.serializers();
}
-
+
/*
/**********************************************************
/* SerializerFactory impl
@@ -304,12 +306,67 @@
}
}
if (refType.isTypeOrSubTypeOf(AtomicReference.class)) {
- return new AtomicReferenceSerializer(refType, staticTyping,
+ return buildAtomicReferenceSerializer(prov, refType, beanDesc, staticTyping,
contentTypeSerializer, contentSerializer);
}
return null;
}
+ protected JsonSerializer<?> buildAtomicReferenceSerializer(SerializerProvider prov,
+ ReferenceType refType, BeanDescription beanDesc, boolean staticTyping,
+ TypeSerializer contentTypeSerializer, JsonSerializer<Object> contentSerializer)
+ throws JsonMappingException
+ {
+ final JavaType contentType = refType.getReferencedType();
+ JsonInclude.Value inclV = _findInclusionWithContent(prov, beanDesc,
+ contentType, AtomicReference.class);
+
+ // Need to support global legacy setting, for now:
+ JsonInclude.Include incl = (inclV == null) ? JsonInclude.Include.USE_DEFAULTS : inclV.getContentInclusion();
+ Object valueToSuppress;
+ boolean suppressNulls;
+
+ if (incl == JsonInclude.Include.USE_DEFAULTS
+ || incl == JsonInclude.Include.ALWAYS) {
+ valueToSuppress = null;
+ suppressNulls = false;
+ } else {
+ suppressNulls = true;
+ switch (incl) {
+ case NON_DEFAULT:
+ valueToSuppress = BeanUtil.getDefaultValue(contentType);
+ if (valueToSuppress != null) {
+ if (valueToSuppress.getClass().isArray()) {
+ valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
+ }
+ }
+ break;
+ case NON_ABSENT:
+ valueToSuppress = contentType.isReferenceType()
+ ? MapSerializer.MARKER_FOR_EMPTY : null;
+ break;
+ case NON_EMPTY:
+ valueToSuppress = MapSerializer.MARKER_FOR_EMPTY;
+ break;
+ case CUSTOM:
+ valueToSuppress = prov.includeFilterInstance(null, inclV.getContentFilter());
+ if (valueToSuppress == null) { // is this legal?
+ suppressNulls = true;
+ } else {
+ suppressNulls = prov.includeFilterSuppressNulls(valueToSuppress);
+ }
+ break;
+ case NON_NULL:
+ default: // should not matter but...
+ valueToSuppress = null;
+ break;
+ }
+ }
+ AtomicReferenceSerializer ser = new AtomicReferenceSerializer(refType, staticTyping,
+ contentTypeSerializer, contentSerializer);
+ return ser.withContentInclusion(valueToSuppress, suppressNulls);
+ }
+
/**
* Method called to create a type information serializer for values of given
* non-container property
@@ -450,7 +507,7 @@
// TODO: can we find full PropertyName?
PropertyName name = PropertyName.construct(anyGetter.getName());
BeanProperty.Std anyProp = new BeanProperty.Std(name, valueType, null,
- beanDesc.getClassAnnotations(), anyGetter, PropertyMetadata.STD_OPTIONAL);
+ anyGetter, PropertyMetadata.STD_OPTIONAL);
builder.setAnyGetter(new AnyGetterWriter(anyProp, anyGetter, anySer));
}
// Next: need to gather view information, if any:
@@ -463,7 +520,13 @@
}
}
- JsonSerializer<Object> ser = (JsonSerializer<Object>) builder.build();
+ JsonSerializer<Object> ser = null;
+ try {
+ ser = (JsonSerializer<Object>) builder.build();
+ } catch (RuntimeException e) {
+ prov.reportBadTypeDefinition(beanDesc, "Failed to construct BeanSerializer for %s: (%s) %s",
+ beanDesc.getType(), e.getClass().getName(), e.getMessage());
+ }
if (ser == null) {
// If we get this far, there were no properties found, so no regular BeanSerializer
// would be constructed. But, couple of exceptions.
@@ -689,6 +752,7 @@
* Method that will apply by-type limitations (as per [JACKSON-429]);
* by default this is based on {@link com.fasterxml.jackson.annotation.JsonIgnoreType}
* annotation but can be supplied by module-provided introspectors too.
+ * Starting with 2.8 there are also "Config overrides" to consider.
*/
protected void removeIgnorableTypes(SerializationConfig config, BeanDescription beanDesc,
List<BeanPropertyDefinition> properties)
@@ -699,18 +763,19 @@
while (it.hasNext()) {
BeanPropertyDefinition property = it.next();
AnnotatedMember accessor = property.getAccessor();
+ /* 22-Oct-2016, tatu: Looks like this removal is an important part of
+ * processing, as taking it out will result in a few test failures...
+ * But should probably be done somewhere else, not here?
+ */
if (accessor == null) {
it.remove();
continue;
}
- Class<?> type = accessor.getRawType();
+ Class<?> type = property.getRawPrimaryType();
Boolean result = ignores.get(type);
if (result == null) {
// 21-Apr-2016, tatu: For 2.8, can specify config overrides
- ConfigOverride override = config.findConfigOverride(type);
- if (override != null) {
- result = override.getIsIgnoredType();
- }
+ result = config.getConfigOverride(type).getIsIgnoredType();
if (result == null) {
BeanDescription desc = config.introspectClassAnnotations(type);
AnnotatedClass ac = desc.getClassInfo();
@@ -793,7 +858,7 @@
final PropertyName name = propDef.getFullName();
JavaType type = accessor.getType();
BeanProperty.Std property = new BeanProperty.Std(name, type, propDef.getWrapperName(),
- pb.getClassAnnotations(), accessor, propDef.getMetadata());
+ accessor, propDef.getMetadata());
// Does member specify a serializer? If so, let's use it.
JsonSerializer<?> annotatedSerializer = findSerializerFromAnnotation(prov,
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/ContainerSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/ContainerSerializer.java
index 637150a..c7d4bea 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/ContainerSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/ContainerSerializer.java
@@ -90,16 +90,7 @@
/**********************************************************
*/
- /* Overridden as abstract, to force re-implementation; necessary for all
- * collection types.
- */
- @Override
- @Deprecated
- public boolean isEmpty(T value) {
- return isEmpty(null, value);
- }
-
- // since 2.5: should be declared abstract in future (2.6)
+// since 2.5: should be declared abstract in future (2.9?)
// @Override
// public abstract boolean isEmpty(SerializerProvider prov, T value);
@@ -111,6 +102,10 @@
* like "getElementCount()" method, this would not work well for
* containers that do not keep track of size (like linked lists may
* not).
+ *<p>
+ * Note, too, that as of now (2.9) this method is only called by serializer
+ * itself; and specifically is not used for non-array/collection types
+ * like <code>Map</code> or <code>Map.Entry</code> instances.
*/
public abstract boolean hasSingleElement(T value);
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/DefaultSerializerProvider.java b/src/main/java/com/fasterxml/jackson/databind/ser/DefaultSerializerProvider.java
index ed8e965..c5d116f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/DefaultSerializerProvider.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/DefaultSerializerProvider.java
@@ -9,6 +9,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fasterxml.jackson.databind.introspect.Annotated;
+import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsonschema.SchemaAware;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
@@ -100,7 +101,8 @@
*/
@Override
- public JsonSerializer<Object> serializerInstance(Annotated annotated, Object serDef) throws JsonMappingException
+ public JsonSerializer<Object> serializerInstance(Annotated annotated, Object serDef)
+ throws JsonMappingException
{
if (serDef == null) {
return null;
@@ -110,11 +112,11 @@
if (serDef instanceof JsonSerializer) {
ser = (JsonSerializer<?>) serDef;
} else {
- /* Alas, there's no way to force return type of "either class
- * X or Y" -- need to throw an exception after the fact
- */
+ // Alas, there's no way to force return type of "either class
+ // X or Y" -- need to throw an exception after the fact
if (!(serDef instanceof Class)) {
- throw new IllegalStateException("AnnotationIntrospector returned serializer definition of type "
+ reportBadDefinition(annotated.getType(),
+ "AnnotationIntrospector returned serializer definition of type "
+serDef.getClass().getName()+"; expected type JsonSerializer or Class<JsonSerializer> instead");
}
Class<?> serClass = (Class<?>)serDef;
@@ -123,7 +125,8 @@
return null;
}
if (!JsonSerializer.class.isAssignableFrom(serClass)) {
- throw new IllegalStateException("AnnotationIntrospector returned Class "
+ reportBadDefinition(annotated.getType(),
+ "AnnotationIntrospector returned Class "
+serClass.getName()+"; expected Class<JsonSerializer>");
}
HandlerInstantiator hi = _config.getHandlerInstantiator();
@@ -136,6 +139,41 @@
return (JsonSerializer<Object>) _handleResolvable(ser);
}
+ @Override
+ public Object includeFilterInstance(BeanPropertyDefinition forProperty,
+ Class<?> filterClass)
+ {
+ if (filterClass == null) {
+ return null;
+ }
+ HandlerInstantiator hi = _config.getHandlerInstantiator();
+ Object filter = (hi == null) ? null : hi.includeFilterInstance(_config, forProperty, filterClass);
+ if (filter == null) {
+ filter = ClassUtil.createInstance(filterClass,
+ _config.canOverrideAccessModifiers());
+ }
+ return filter;
+ }
+
+ @Override
+ public boolean includeFilterSuppressNulls(Object filter) throws JsonMappingException
+ {
+ if (filter == null) {
+ return true;
+ }
+ // should let filter decide what to do with nulls:
+ // But just case, let's handle unexpected (from our perspective) problems explicitly
+ try {
+ return filter.equals(null);
+ } catch (Throwable t) {
+ String msg = String.format(
+"Problem determining whether filter of type '%s' should filter out `null` values: (%s) %s",
+filter.getClass().getName(), t.getClass().getName(), t.getMessage());
+ reportBadDefinition(filter.getClass(), msg, t);
+ return false; // never gets here
+ }
+ }
+
/*
/**********************************************************
/* Object Id handling
@@ -265,43 +303,21 @@
_serializeNull(gen);
return;
}
- Class<?> cls = value.getClass();
+ final Class<?> cls = value.getClass();
// true, since we do want to cache root-level typed serializers (ditto for null property)
final JsonSerializer<Object> ser = findTypedValueSerializer(cls, true, null);
- // Ok: should we wrap result in an additional property ("root name")?
- final boolean wrap;
PropertyName rootName = _config.getFullRootName();
-
if (rootName == null) { // not explicitly specified
- wrap = _config.isEnabled(SerializationFeature.WRAP_ROOT_VALUE);
- if (wrap) {
- gen.writeStartObject();
- PropertyName pname = _config.findRootName(value.getClass());
- gen.writeFieldName(pname.simpleAsEncoded(_config));
+ if (_config.isEnabled(SerializationFeature.WRAP_ROOT_VALUE)) {
+ _serialize(gen, value, ser, _config.findRootName(cls));
+ return;
}
- } else if (rootName.isEmpty()) {
- wrap = false;
- } else { // [JACKSON-764]
- // empty String means explicitly disabled; non-empty that it is enabled
- wrap = true;
- gen.writeStartObject();
- gen.writeFieldName(rootName.getSimpleName());
+ } else if (!rootName.isEmpty()) {
+ _serialize(gen, value, ser, rootName);
+ return;
}
- try {
- ser.serialize(value, gen, this);
- if (wrap) {
- gen.writeEndObject();
- }
- } catch (IOException ioe) { // As per [JACKSON-99], pass IOException and subtypes as-is
- throw ioe;
- } catch (Exception e) { // but wrap RuntimeExceptions, to get path information
- String msg = e.getMessage();
- if (msg == null) {
- msg = "[no message for "+e.getClass().getName()+"]";
- }
- throw new JsonMappingException(gen, msg, e);
- }
+ _serialize(gen, value, ser);
}
/**
@@ -328,39 +344,17 @@
}
// root value, not reached via property:
JsonSerializer<Object> ser = findTypedValueSerializer(rootType, true, null);
-
- // Ok: should we wrap result in an additional property ("root name")?
- final boolean wrap;
PropertyName rootName = _config.getFullRootName();
if (rootName == null) { // not explicitly specified
- wrap = _config.isEnabled(SerializationFeature.WRAP_ROOT_VALUE);
- if (wrap) {
- gen.writeStartObject();
- PropertyName pname = _config.findRootName(value.getClass());
- gen.writeFieldName(pname.simpleAsEncoded(_config));
+ if (_config.isEnabled(SerializationFeature.WRAP_ROOT_VALUE)) {
+ _serialize(gen, value, ser, _config.findRootName(rootType));
+ return;
}
- } else if (rootName.isEmpty()) {
- wrap = false;
- } else {
- // empty String means explicitly disabled; non-empty that it is enabled
- wrap = true;
- gen.writeStartObject();
- gen.writeFieldName(rootName.getSimpleName());
+ } else if (!rootName.isEmpty()) {
+ _serialize(gen, value, ser, rootName);
+ return;
}
- try {
- ser.serialize(value, gen, this);
- if (wrap) {
- gen.writeEndObject();
- }
- } catch (IOException ioe) { // no wrapping for IO (and derived)
- throw ioe;
- } catch (Exception e) { // but others do need to be, to get path etc
- String msg = e.getMessage();
- if (msg == null) {
- msg = "[no message for "+e.getClass().getName()+"]";
- }
- reportMappingProblem(e, msg);
- }
+ _serialize(gen, value, ser);
}
/**
@@ -391,41 +385,20 @@
if (ser == null) {
ser = findTypedValueSerializer(rootType, true, null);
}
- // Ok: should we wrap result in an additional property ("root name")?
- final boolean wrap;
PropertyName rootName = _config.getFullRootName();
if (rootName == null) { // not explicitly specified
- // [JACKSON-163]
- wrap = _config.isEnabled(SerializationFeature.WRAP_ROOT_VALUE);
- if (wrap) {
- gen.writeStartObject();
- PropertyName pname = (rootType == null)
+ if (_config.isEnabled(SerializationFeature.WRAP_ROOT_VALUE)) {
+ rootName = (rootType == null)
? _config.findRootName(value.getClass())
: _config.findRootName(rootType);
- gen.writeFieldName(pname.simpleAsEncoded(_config));
+ _serialize(gen, value, ser, rootName);
+ return;
}
- } else if (rootName.isEmpty()) {
- wrap = false;
- } else { // [JACKSON-764]
- // empty String means explicitly disabled; non-empty that it is enabled
- wrap = true;
- gen.writeStartObject();
- gen.writeFieldName(rootName.getSimpleName());
+ } else if (!rootName.isEmpty()) {
+ _serialize(gen, value, ser, rootName);
+ return;
}
- try {
- ser.serialize(value, gen, this);
- if (wrap) {
- gen.writeEndObject();
- }
- } catch (IOException ioe) { // no wrapping for IO (and derived)
- throw ioe;
- } catch (Exception e) { // but others do need to be, to get path etc
- String msg = e.getMessage();
- if (msg == null) {
- msg = "[no message for "+e.getClass().getName()+"]";
- }
- reportMappingProblem(e, msg);
- }
+ _serialize(gen, value, ser);
}
/**
@@ -481,26 +454,34 @@
if (wrap) {
gen.writeEndObject();
}
- } catch (IOException ioe) { // no wrapping for IO (and derived)
- throw ioe;
- } catch (Exception e) { // but others do need to be, to get path etc
- String msg = e.getMessage();
- if (msg == null) {
- msg = "[no message for "+e.getClass().getName()+"]";
- }
- reportMappingProblem(e, msg);
+ } catch (Exception e) {
+ throw _wrapAsIOE(gen, e);
}
}
- /**
- * @deprecated since 2.6; remove from 2.7 or later
- */
- @Deprecated
- public void serializePolymorphic(JsonGenerator gen, Object value, TypeSerializer typeSer)
- throws IOException
+ private final void _serialize(JsonGenerator gen, Object value,
+ JsonSerializer<Object> ser, PropertyName rootName)
+ throws IOException
{
- JavaType t = (value == null) ? null : _config.constructType(value.getClass());
- serializePolymorphic(gen, value, t, null, typeSer);
+ try {
+ gen.writeStartObject();
+ gen.writeFieldName(rootName.simpleAsEncoded(_config));
+ ser.serialize(value, gen, this);
+ gen.writeEndObject();
+ } catch (Exception e) {
+ throw _wrapAsIOE(gen, e);
+ }
+ }
+
+ private final void _serialize(JsonGenerator gen, Object value,
+ JsonSerializer<Object> ser)
+ throws IOException
+ {
+ try {
+ ser.serialize(value, gen, this);
+ } catch (Exception e) {
+ throw _wrapAsIOE(gen, e);
+ }
}
/**
@@ -513,16 +494,22 @@
JsonSerializer<Object> ser = getDefaultNullValueSerializer();
try {
ser.serialize(null, gen, this);
- } catch (IOException ioe) { // no wrapping for IO (and derived)
- throw ioe;
- } catch (Exception e) { // but others do need to be, to get path etc
- String msg = e.getMessage();
- if (msg == null) {
- msg = "[no message for "+e.getClass().getName()+"]";
- }
- reportMappingProblem(e, msg);
+ } catch (Exception e) {
+ throw _wrapAsIOE(gen, e);
}
}
+
+ private IOException _wrapAsIOE(JsonGenerator g, Exception e) {
+ if (e instanceof IOException) {
+ return (IOException) e;
+ }
+ String msg = e.getMessage();
+ if (msg == null) {
+ msg = "[no message for "+e.getClass().getName()+"]";
+ }
+ return new JsonMappingException(g, msg, e);
+ }
+
/*
/********************************************************
/* Access to caching details
@@ -593,9 +580,6 @@
public com.fasterxml.jackson.databind.jsonschema.JsonSchema generateJsonSchema(Class<?> type)
throws JsonMappingException
{
- if (type == null) {
- throw new IllegalArgumentException("A class must be provided");
- }
/* no need for embedded type information for JSON schema generation (all
* type information it needs is accessible via "untyped" serializer)
*/
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/PropertyBuilder.java b/src/main/java/com/fasterxml/jackson/databind/ser/PropertyBuilder.java
index 6363421..b3499e7 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/PropertyBuilder.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/PropertyBuilder.java
@@ -48,7 +48,7 @@
* @since 2.8
*/
final protected boolean _useRealPropertyDefaults;
-
+
public PropertyBuilder(SerializationConfig config, BeanDescription beanDesc)
{
_config = config;
@@ -56,8 +56,10 @@
// 08-Sep-2016, tatu: This gets tricky, with 3 levels of definitions:
// (a) global default inclusion
// (b) per-type default inclusion (from annotation or config overrides;
- // latter having precedence
- // Cc) per-property override
+ // config override having precedence)
+ // (c) per-property override (from annotation on specific property or
+ // config overrides per type of property;
+ // annotation having precedence)
//
// and not only requiring merging, but also considering special handling
// for NON_DEFAULT in case of (b) (vs (a) or (c))
@@ -86,7 +88,6 @@
* to use for contained values (only used for properties that are
* of container type)
*/
- @SuppressWarnings("deprecation")
protected BeanPropertyWriter buildWriter(SerializerProvider prov,
BeanPropertyDefinition propDef, JavaType declaredType, JsonSerializer<?> ser,
TypeSerializer typeSer, TypeSerializer contentTypeSer,
@@ -98,6 +99,9 @@
try {
serializationType = findSerializationType(am, defaultUseStaticTyping, declaredType);
} catch (JsonMappingException e) {
+ if (propDef == null) {
+ return prov.reportBadDefinition(declaredType, e.getMessage());
+ }
return prov.reportBadPropertyDefinition(_beanDesc, propDef, e.getMessage());
}
@@ -127,21 +131,28 @@
// 12-Jul-2016, tatu: [databind#1256] Need to make sure we consider type refinement
JavaType actualType = (serializationType == null) ? declaredType : serializationType;
+ // 17-Mar-2017: [databind#1522] Allow config override per property type
+ AnnotatedMember accessor = propDef.getAccessor();
+ if (accessor == null) {
+ // neither Setter nor ConstructorParameter are expected here
+ return prov.reportBadPropertyDefinition(_beanDesc, propDef,
+ "could not determine property type");
+ }
+ Class<?> rawPropertyType = accessor.getRawType();
+
// 17-Aug-2016, tatu: Default inclusion covers global default (for all types), as well
// as type-default for enclosing POJO. What we need, then, is per-type default (if any)
// for declared property type... and finally property annotation overrides
- JsonInclude.Value inclV = _config.getDefaultPropertyInclusion(actualType.getRawClass(),
- _defaultInclusion);
+ JsonInclude.Value inclV = _config.getDefaultInclusion(actualType.getRawClass(),
+ rawPropertyType, _defaultInclusion);
// property annotation override
inclV = inclV.withOverrides(propDef.findInclusion());
JsonInclude.Include inclusion = inclV.getValueInclusion();
-
if (inclusion == JsonInclude.Include.USE_DEFAULTS) { // should not occur but...
inclusion = JsonInclude.Include.ALWAYS;
}
-
switch (inclusion) {
case NON_DEFAULT:
// 11-Nov-2015, tatu: This is tricky because semantics differ between cases,
@@ -166,7 +177,7 @@
_throwWrapped(e, propDef.getName(), defaultBean);
}
} else {
- valueToSuppress = getDefaultValue(actualType);
+ valueToSuppress = BeanUtil.getDefaultValue(actualType);
suppressNulls = true;
}
if (valueToSuppress == null) {
@@ -191,21 +202,33 @@
// but possibly also 'empty' values:
valueToSuppress = BeanPropertyWriter.MARKER_FOR_EMPTY;
break;
+ case CUSTOM: // new with 2.9
+ valueToSuppress = prov.includeFilterInstance(propDef, inclV.getValueFilter());
+ if (valueToSuppress == null) { // is this legal?
+ suppressNulls = true;
+ } else {
+ suppressNulls = prov.includeFilterSuppressNulls(valueToSuppress);
+ }
+ break;
case NON_NULL:
suppressNulls = true;
// fall through
case ALWAYS: // default
default:
- // we may still want to suppress empty collections, as per [JACKSON-254]:
+ // we may still want to suppress empty collections
if (actualType.isContainerType()
&& !_config.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS)) {
valueToSuppress = BeanPropertyWriter.MARKER_FOR_EMPTY;
}
break;
}
+ Class<?>[] views = propDef.findViews();
+ if (views == null) {
+ views = _beanDesc.findDefaultViews();
+ }
BeanPropertyWriter bpw = new BeanPropertyWriter(propDef,
am, _beanDesc.getClassAnnotations(), declaredType,
- ser, typeSer, serializationType, suppressNulls, valueToSuppress);
+ ser, typeSer, serializationType, suppressNulls, valueToSuppress, views);
// How about custom null serializer?
Object serDef = _annotationIntrospector.findNullSerializer(am);
@@ -317,10 +340,10 @@
* {@link com.fasterxml.jackson.annotation.JsonInclude.Include#NON_EMPTY} requires special handling.
*
* @since 2.7
- * @deprecated Since 2.8.5 since this will not allow determining difference between "no default instance"
+ * @deprecated Since 2.9 since this will not allow determining difference between "no default instance"
* case and default being `null`.
*/
- @Deprecated // since 2.8.5
+ @Deprecated // since 2.9
protected Object getPropertyDefaultValue(String name, AnnotatedMember member,
JavaType type)
{
@@ -336,37 +359,13 @@
}
/**
- * Accessor used to find out "default value" to use for comparing values to
- * serialize, to determine whether to exclude value from serialization with
- * inclusion type of {@link com.fasterxml.jackson.annotation.JsonInclude.Include#NON_DEFAULT}.
- *<p>
- * Default logic is such that for primitives and wrapper types for primitives, expected
- * defaults (0 for `int` and `java.lang.Integer`) are returned; for Strings, empty String,
- * and for structured (Maps, Collections, arrays) and reference types, criteria
- * {@link com.fasterxml.jackson.annotation.JsonInclude.Include#NON_DEFAULT}
- * is used.
- *
- * @since 2.7
+ * @deprecated Since 2.9
*/
- protected Object getDefaultValue(JavaType type)
- {
- // 06-Nov-2015, tatu: Returning null is fine for Object types; but need special
- // handling for primitives since they are never passed as nulls.
- Class<?> cls = type.getRawClass();
-
- Class<?> prim = ClassUtil.primitiveType(cls);
- if (prim != null) {
- return ClassUtil.defaultValue(prim);
- }
- if (type.isContainerType() || type.isReferenceType()) {
- return JsonInclude.Include.NON_EMPTY;
- }
- if (cls == String.class) {
- return "";
- }
- return null;
+ @Deprecated // since 2.9
+ protected Object getDefaultValue(JavaType type) {
+ return BeanUtil.getDefaultValue(type);
}
-
+
/*
/**********************************************************
/* Helper methods for exception handling
@@ -379,8 +378,8 @@
while (t.getCause() != null) {
t = t.getCause();
}
- if (t instanceof Error) throw (Error) t;
- if (t instanceof RuntimeException) throw (RuntimeException) t;
+ ClassUtil.throwIfError(t);
+ ClassUtil.throwIfRTE(t);
throw new IllegalArgumentException("Failed to get property '"+propName+"' of default "+defaultBean.getClass().getName()+" instance");
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/VirtualBeanPropertyWriter.java b/src/main/java/com/fasterxml/jackson/databind/ser/VirtualBeanPropertyWriter.java
index 54b9d72..ad6cd35 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/VirtualBeanPropertyWriter.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/VirtualBeanPropertyWriter.java
@@ -52,13 +52,23 @@
protected VirtualBeanPropertyWriter(BeanPropertyDefinition propDef,
Annotations contextAnnotations, JavaType declaredType,
JsonSerializer<?> ser, TypeSerializer typeSer, JavaType serType,
- JsonInclude.Value inclusion)
+ JsonInclude.Value inclusion, Class<?>[] includeInViews)
{
super(propDef, propDef.getPrimaryMember(), contextAnnotations, declaredType,
ser, typeSer, serType,
- _suppressNulls(inclusion), _suppressableValue(inclusion));
+ _suppressNulls(inclusion), _suppressableValue(inclusion),
+ includeInViews);
}
+ @Deprecated // since 2.8
+ protected VirtualBeanPropertyWriter(BeanPropertyDefinition propDef,
+ Annotations contextAnnotations, JavaType declaredType,
+ JsonSerializer<?> ser, TypeSerializer typeSer, JavaType serType,
+ JsonInclude.Value inclusion)
+ {
+ this(propDef, contextAnnotations, declaredType, ser, typeSer, serType, inclusion, null);
+ }
+
protected VirtualBeanPropertyWriter(VirtualBeanPropertyWriter base) {
super(base);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/AttributePropertyWriter.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/AttributePropertyWriter.java
index a1cd172..477f9ee 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/AttributePropertyWriter.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/AttributePropertyWriter.java
@@ -41,7 +41,9 @@
{
super(propDef, contextAnnotations, declaredType,
/* value serializer */ null, /* type serializer */ null, /* ser type */ null,
- inclusion);
+ inclusion,
+ // 10-Oct-2016, tatu: Could enable per-view settings too in future
+ null);
_attrName = attrName;
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/FilteredBeanPropertyWriter.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/FilteredBeanPropertyWriter.java
index ef7e635..e21d8ff 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/FilteredBeanPropertyWriter.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/FilteredBeanPropertyWriter.java
@@ -61,26 +61,26 @@
}
@Override
- public void serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov)
+ public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov)
throws Exception
{
Class<?> activeView = prov.getActiveView();
if (activeView == null || _view.isAssignableFrom(activeView)) {
- _delegate.serializeAsField(bean, jgen, prov);
+ _delegate.serializeAsField(bean, gen, prov);
} else {
- _delegate.serializeAsOmittedField(bean, jgen, prov);
+ _delegate.serializeAsOmittedField(bean, gen, prov);
}
}
@Override
- public void serializeAsElement(Object bean, JsonGenerator jgen, SerializerProvider prov)
+ public void serializeAsElement(Object bean, JsonGenerator gen, SerializerProvider prov)
throws Exception
{
Class<?> activeView = prov.getActiveView();
if (activeView == null || _view.isAssignableFrom(activeView)) {
- _delegate.serializeAsElement(bean, jgen, prov);
+ _delegate.serializeAsElement(bean, gen, prov);
} else {
- _delegate.serializeAsPlaceholder(bean, jgen, prov);
+ _delegate.serializeAsPlaceholder(bean, gen, prov);
}
}
@@ -127,58 +127,48 @@
}
@Override
- public void serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov)
+ public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov)
throws Exception
{
- final Class<?> activeView = prov.getActiveView();
- if (activeView != null) {
- int i = 0, len = _views.length;
- for (; i < len; ++i) {
- if (_views[i].isAssignableFrom(activeView)) break;
- }
- // not included, bail out:
- if (i == len) {
- _delegate.serializeAsOmittedField(bean, jgen, prov);
- return;
- }
+ if (_inView(prov.getActiveView())) {
+ _delegate.serializeAsField(bean, gen, prov);
+ return;
}
- _delegate.serializeAsField(bean, jgen, prov);
+ _delegate.serializeAsOmittedField(bean, gen, prov);
}
@Override
- public void serializeAsElement(Object bean, JsonGenerator jgen, SerializerProvider prov)
+ public void serializeAsElement(Object bean, JsonGenerator gen, SerializerProvider prov)
throws Exception
{
- final Class<?> activeView = prov.getActiveView();
- if (activeView != null) {
- int i = 0, len = _views.length;
- for (; i < len; ++i) {
- if (_views[i].isAssignableFrom(activeView)) break;
- }
- // not included, bail out:
- if (i == len) {
- _delegate.serializeAsPlaceholder(bean, jgen, prov);
- return;
- }
+ if (_inView(prov.getActiveView())) {
+ _delegate.serializeAsElement(bean, gen, prov);
+ return;
}
- _delegate.serializeAsElement(bean, jgen, prov);
+ _delegate.serializeAsPlaceholder(bean, gen, prov);
}
@Override
public void depositSchemaProperty(JsonObjectFormatVisitor v,
SerializerProvider provider) throws JsonMappingException
{
- Class<?> activeView = provider.getActiveView();
- if (activeView != null) {
- int i = 0, len = _views.length;
- for (; i < len; ++i) {
- if (_views[i].isAssignableFrom(activeView)) break;
- }
- if (i == len) { // not match? Just don't deposit
- return;
+ if (_inView(provider.getActiveView())) {
+ super.depositSchemaProperty(v, provider);
+ }
+ }
+
+ private final boolean _inView(Class<?> activeView)
+ {
+ if (activeView == null) {
+ return true;
+ }
+ final int len = _views.length;
+ for (int i = 0; i < len; ++i) {
+ if (_views[i].isAssignableFrom(activeView)) {
+ return true;
}
}
- super.depositSchemaProperty(v, provider);
+ return false;
}
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/IndexedListSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/IndexedListSerializer.java
index ffa5742..512f2c0 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/IndexedListSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/IndexedListSerializer.java
@@ -48,7 +48,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, List<?> value) {
- return (value == null) || value.isEmpty();
+ return value.isEmpty();
}
@Override
@@ -81,15 +81,15 @@
}
@Override
- public void serializeContents(List<?> value, JsonGenerator jgen, SerializerProvider provider)
+ public void serializeContents(List<?> value, JsonGenerator g, SerializerProvider provider)
throws IOException
{
if (_elementSerializer != null) {
- serializeContentsUsing(value, jgen, provider, _elementSerializer);
+ serializeContentsUsing(value, g, provider, _elementSerializer);
return;
}
if (_valueTypeSerializer != null) {
- serializeTypedContents(value, jgen, provider);
+ serializeTypedContents(value, g, provider);
return;
}
final int len = value.size();
@@ -102,7 +102,7 @@
for (; i < len; ++i) {
Object elem = value.get(i);
if (elem == null) {
- provider.defaultSerializeNull(jgen);
+ provider.defaultSerializeNull(g);
} else {
Class<?> cc = elem.getClass();
JsonSerializer<Object> serializer = serializers.serializerFor(cc);
@@ -116,7 +116,7 @@
}
serializers = _dynamicSerializers;
}
- serializer.serialize(elem, jgen, provider);
+ serializer.serialize(elem, g, provider);
}
}
} catch (Exception e) {
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/IndexedStringListSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/IndexedStringListSerializer.java
index e95dbaf..a247e4b 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/IndexedStringListSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/IndexedStringListSerializer.java
@@ -37,14 +37,13 @@
}
public IndexedStringListSerializer(IndexedStringListSerializer src,
- JsonSerializer<?> ser, Boolean unwrapSingle) {
- super(src, ser, unwrapSingle);
+ Boolean unwrapSingle) {
+ super(src, unwrapSingle);
}
@Override
- public JsonSerializer<?> _withResolved(BeanProperty prop,
- JsonSerializer<?> ser, Boolean unwrapSingle) {
- return new IndexedStringListSerializer(this, ser, unwrapSingle);
+ public JsonSerializer<?> _withResolved(BeanProperty prop, Boolean unwrapSingle) {
+ return new IndexedStringListSerializer(this, unwrapSingle);
}
@Override protected JsonNode contentSchema() { return createSchemaNode("string", true); }
@@ -61,7 +60,7 @@
*/
@Override
- public void serialize(List<String> value, JsonGenerator gen,
+ public void serialize(List<String> value, JsonGenerator g,
SerializerProvider provider) throws IOException
{
final int len = value.size();
@@ -69,75 +68,37 @@
if (((_unwrapSingle == null) &&
provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
|| (_unwrapSingle == Boolean.TRUE)) {
- _serializeUnwrapped(value, gen, provider);
+ serializeContents(value, g, provider, 1);
return;
}
}
-
- gen.writeStartArray(len);
- if (_serializer == null) {
- serializeContents(value, gen, provider, len);
- } else {
- serializeUsingCustom(value, gen, provider, len);
- }
- gen.writeEndArray();
+ g.writeStartArray(len);
+ serializeContents(value, g, provider, len);
+ g.writeEndArray();
}
- private final void _serializeUnwrapped(List<String> value, JsonGenerator gen,
- SerializerProvider provider) throws IOException
- {
- if (_serializer == null) {
- serializeContents(value, gen, provider, 1);
- } else {
- serializeUsingCustom(value, gen, provider, 1);
- }
- }
-
@Override
- public void serializeWithType(List<String> value, JsonGenerator gen,
- SerializerProvider provider,
- TypeSerializer typeSer) throws IOException
+ public void serializeWithType(List<String> value, JsonGenerator g, SerializerProvider provider,
+ TypeSerializer typeSer)
+ throws IOException
{
- final int len = value.size();
- typeSer.writeTypePrefixForArray(value, gen);
- if (_serializer == null) {
- serializeContents(value, gen, provider, len);
- } else {
- serializeUsingCustom(value, gen, provider, len);
- }
- typeSer.writeTypeSuffixForArray(value, gen);
+ typeSer.writeTypePrefixForArray(value, g);
+ serializeContents(value, g, provider, value.size());
+ typeSer.writeTypeSuffixForArray(value, g);
}
- private final void serializeContents(List<String> value, JsonGenerator gen,
+ private final void serializeContents(List<String> value, JsonGenerator g,
SerializerProvider provider, int len) throws IOException
{
+ g.setCurrentValue(value);
int i = 0;
try {
for (; i < len; ++i) {
String str = value.get(i);
if (str == null) {
- provider.defaultSerializeNull(gen);
+ provider.defaultSerializeNull(g);
} else {
- gen.writeString(str);
- }
- }
- } catch (Exception e) {
- wrapAndThrow(provider, e, value, i);
- }
- }
-
- private final void serializeUsingCustom(List<String> value, JsonGenerator gen,
- SerializerProvider provider, int len) throws IOException
- {
- int i = 0;
- try {
- final JsonSerializer<String> ser = _serializer;
- for (i = 0; i < len; ++i) {
- String str = value.get(i);
- if (str == null) {
- provider.defaultSerializeNull(gen);
- } else {
- ser.serialize(str, gen, provider);
+ g.writeString(str);
}
}
} catch (Exception e) {
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/IteratorSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/IteratorSerializer.java
index 818e3bd..9fbb7e3 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/IteratorSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/IteratorSerializer.java
@@ -27,7 +27,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, Iterator<?> value) {
- return (value == null) || !value.hasNext();
+ return !value.hasNext();
}
@Override
@@ -52,6 +52,8 @@
public final void serialize(Iterator<?> value, JsonGenerator gen,
SerializerProvider provider) throws IOException
{
+ // 02-Dec-2016, tatu: As per comments above, can't determine single element so...
+ /*
if (((_unwrapSingle == null) &&
provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
|| (_unwrapSingle == Boolean.TRUE)) {
@@ -60,43 +62,65 @@
return;
}
}
+ */
gen.writeStartArray();
serializeContents(value, gen, provider);
gen.writeEndArray();
}
@Override
- public void serializeContents(Iterator<?> value, JsonGenerator gen,
+ public void serializeContents(Iterator<?> value, JsonGenerator g,
SerializerProvider provider) throws IOException
{
- if (value.hasNext()) {
- final TypeSerializer typeSer = _valueTypeSerializer;
- JsonSerializer<Object> prevSerializer = null;
- Class<?> prevClass = null;
- do {
- Object elem = value.next();
- if (elem == null) {
- provider.defaultSerializeNull(gen);
- continue;
- }
- JsonSerializer<Object> currSerializer = _elementSerializer;
- if (currSerializer == null) {
- // Minor optimization to avoid most lookups:
- Class<?> cc = elem.getClass();
- if (cc == prevClass) {
- currSerializer = prevSerializer;
- } else {
- currSerializer = provider.findValueSerializer(cc, _property);
- prevSerializer = currSerializer;
- prevClass = cc;
- }
- }
- if (typeSer == null) {
- currSerializer.serialize(elem, gen, provider);
- } else {
- currSerializer.serializeWithType(elem, gen, provider, typeSer);
- }
- } while (value.hasNext());
+ if (!value.hasNext()) {
+ return;
}
+ JsonSerializer<Object> serializer = _elementSerializer;
+ if (serializer == null) {
+ _serializeDynamicContents(value, g, provider);
+ return;
+ }
+ final TypeSerializer typeSer = _valueTypeSerializer;
+ do {
+ Object elem = value.next();
+ if (elem == null) {
+ provider.defaultSerializeNull(g);
+ } else if (typeSer == null) {
+ serializer.serialize(elem, g, provider);
+ } else {
+ serializer.serializeWithType(elem, g, provider, typeSer);
+ }
+ } while (value.hasNext());
+ }
+
+ protected void _serializeDynamicContents(Iterator<?> value, JsonGenerator g,
+ SerializerProvider provider) throws IOException
+ {
+ JsonSerializer<Object> serializer = _elementSerializer;
+ final TypeSerializer typeSer = _valueTypeSerializer;
+ PropertySerializerMap serializers = _dynamicSerializers;
+ do {
+ Object elem = value.next();
+ if (elem == null) {
+ provider.defaultSerializeNull(g);
+ continue;
+ }
+ Class<?> cc = elem.getClass();
+ serializers.serializerFor(cc);
+ if (serializer == null) {
+ if (_elementType.hasGenericTypes()) {
+ serializer = _findAndAddDynamic(serializers,
+ provider.constructSpecializedType(_elementType, cc), provider);
+ } else {
+ serializer = _findAndAddDynamic(serializers, cc, provider);
+ }
+ serializers = _dynamicSerializers;
+ }
+ if (typeSer == null) {
+ serializer.serialize(elem, g, provider);
+ } else {
+ serializer.serializeWithType(elem, g, provider, typeSer);
+ }
+ } while (value.hasNext());
}
}
\ No newline at end of file
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/MapEntrySerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/MapEntrySerializer.java
index 7deb535..55a6af7 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/MapEntrySerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/MapEntrySerializer.java
@@ -4,14 +4,18 @@
import java.util.Map;
import java.util.Map.Entry;
-import com.fasterxml.jackson.core.JsonGenerationException;
+import com.fasterxml.jackson.annotation.JsonInclude;
+
import com.fasterxml.jackson.core.JsonGenerator;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.ContainerSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
+import com.fasterxml.jackson.databind.util.ArrayBuilders;
+import com.fasterxml.jackson.databind.util.BeanUtil;
/**
* @since 2.5
@@ -23,6 +27,11 @@
implements ContextualSerializer
{
/**
+ * @since 2.9
+ */
+ public final static Object MARKER_FOR_EMPTY = JsonInclude.Include.NON_EMPTY;
+
+ /**
* Map-valued property being serialized with this instance
*/
protected final BeanProperty _property;
@@ -35,11 +44,17 @@
protected final JavaType _entryType, _keyType, _valueType;
+ /*
+ /**********************************************************
+ /* Serializers used
+ /**********************************************************
+ */
+
/**
* Key serializer to use, if it can be statically determined
*/
protected JsonSerializer<Object> _keySerializer;
-
+
/**
* Value serializer to use, if it can be statically determined
*/
@@ -58,10 +73,36 @@
/*
/**********************************************************
+ /* Config settings, filtering
+ /**********************************************************
+ */
+
+ /**
+ * Value that indicates suppression mechanism to use for <b>values contained</b>;
+ * either "filter" (of which <code>equals()</code> is called), or marker
+ * value of {@link #MARKER_FOR_EMPTY}, or null to indicate no filtering for
+ * non-null values.
+ * Note that inclusion value for Map instance itself is handled by caller (POJO
+ * property that refers to the Map value).
+ *
+ * @since 2.5
+ */
+ protected final Object _suppressableValue;
+
+ /**
+ * Flag that indicates what to do with `null` values, distinct from
+ * handling of {@link #_suppressableValue}
+ *
+ * @since 2.9
+ */
+ protected final boolean _suppressNulls;
+
+ /*
+ /**********************************************************
/* Construction, initialization
/**********************************************************
*/
-
+
public MapEntrySerializer(JavaType type, JavaType keyType, JavaType valueType,
boolean staticTyping, TypeSerializer vts,
BeanProperty property)
@@ -74,13 +115,25 @@
_valueTypeSerializer = vts;
_property = property;
_dynamicValueSerializers = PropertySerializerMap.emptyForProperties();
+ _suppressableValue = null;
+ _suppressNulls = false;
}
- @SuppressWarnings("unchecked")
+ @Deprecated // since 2.9
protected MapEntrySerializer(MapEntrySerializer src, BeanProperty property,
TypeSerializer vts,
JsonSerializer<?> keySer, JsonSerializer<?> valueSer)
{
+ this(src, property, vts, keySer, valueSer,
+ src._suppressableValue, src._suppressNulls);
+ }
+
+ @SuppressWarnings("unchecked")
+ protected MapEntrySerializer(MapEntrySerializer src, BeanProperty property,
+ TypeSerializer vts,
+ JsonSerializer<?> keySer, JsonSerializer<?> valueSer,
+ Object suppressableValue, boolean suppressNulls)
+ {
super(Map.class, false);
_entryType = src._entryType;
_keyType = src._keyType;
@@ -91,16 +144,37 @@
_valueSerializer = (JsonSerializer<Object>) valueSer;
_dynamicValueSerializers = src._dynamicValueSerializers;
_property = src._property;
+ _suppressableValue = suppressableValue;
+ _suppressNulls = suppressNulls;
}
@Override
public ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts) {
- return new MapEntrySerializer(this, _property, vts, _keySerializer, _valueSerializer);
+ return new MapEntrySerializer(this, _property, vts, _keySerializer, _valueSerializer,
+ _suppressableValue, _suppressNulls);
}
+ /**
+ * @since 2.9
+ */
public MapEntrySerializer withResolved(BeanProperty property,
- JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer) {
- return new MapEntrySerializer(this, property, _valueTypeSerializer, keySerializer, valueSerializer);
+ JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer,
+ Object suppressableValue, boolean suppressNulls) {
+ return new MapEntrySerializer(this, property, _valueTypeSerializer,
+ keySerializer, valueSerializer, suppressableValue, suppressNulls);
+ }
+
+ /**
+ * @since 2.9
+ */
+ public MapEntrySerializer withContentInclusion(Object suppressableValue,
+ boolean suppressNulls) {
+ if ((_suppressableValue == suppressableValue)
+ && (_suppressNulls == suppressNulls)) {
+ return this;
+ }
+ return new MapEntrySerializer(this, _property, _valueTypeSerializer,
+ _keySerializer, _valueSerializer, suppressableValue, suppressNulls);
}
@Override
@@ -127,7 +201,7 @@
ser = _valueSerializer;
}
// [databind#124]: May have a content converter
- ser = findConvertingContentSerializer(provider, property, ser);
+ ser = findContextualConvertingSerializer(provider, property, ser);
if (ser == null) {
// 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
// we can consider it a static case as well.
@@ -135,8 +209,6 @@
if (_valueTypeIsStatic && !_valueType.isJavaLangObject()) {
ser = provider.findValueSerializer(_valueType, property);
}
- } else {
- ser = provider.handleSecondaryContextualization(ser, property);
}
if (keySer == null) {
keySer = _keySerializer;
@@ -146,8 +218,59 @@
} else {
keySer = provider.handleSecondaryContextualization(keySer, property);
}
- MapEntrySerializer mser = withResolved(property, keySer, ser);
- // but note: no filtering, ignored entries or sorting (unlike Maps)
+
+ Object valueToSuppress = _suppressableValue;
+ boolean suppressNulls = _suppressNulls;
+ if (property != null) {
+ JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), null);
+ if (inclV != null) {
+ JsonInclude.Include incl = inclV.getContentInclusion();
+ if (incl != JsonInclude.Include.USE_DEFAULTS) {
+ switch (incl) {
+ case NON_DEFAULT:
+ valueToSuppress = BeanUtil.getDefaultValue(_valueType);
+ suppressNulls = true;
+ if (valueToSuppress != null) {
+ if (valueToSuppress.getClass().isArray()) {
+ valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
+ }
+ }
+ break;
+ case NON_ABSENT:
+ suppressNulls = true;
+ valueToSuppress = _valueType.isReferenceType() ? MARKER_FOR_EMPTY : null;
+ break;
+ case NON_EMPTY:
+ suppressNulls = true;
+ valueToSuppress = MARKER_FOR_EMPTY;
+ break;
+ case CUSTOM:
+ valueToSuppress = provider.includeFilterInstance(null, inclV.getContentFilter());
+ if (valueToSuppress == null) { // is this legal?
+ suppressNulls = true;
+ } else {
+ suppressNulls = provider.includeFilterSuppressNulls(valueToSuppress);
+ }
+ break;
+ case NON_NULL:
+ valueToSuppress = null;
+ suppressNulls = true;
+ break;
+ case ALWAYS: // default
+ default:
+ valueToSuppress = null;
+ // 30-Sep-2016, tatu: Should not need to check global flags here,
+ // if inclusion forced to be ALWAYS
+ suppressNulls = false;
+ break;
+ }
+ }
+ }
+ }
+
+ MapEntrySerializer mser = withResolved(property, keySer, ser,
+ valueToSuppress, suppressNulls);
+ // but note: no (full) filtering or sorting (unlike Maps)
return mser;
}
@@ -173,8 +296,33 @@
}
@Override
- public boolean isEmpty(SerializerProvider prov, Entry<?, ?> value) {
- return (value == null);
+ public boolean isEmpty(SerializerProvider prov, Entry<?, ?> entry)
+ {
+ Object value = entry.getValue();
+ if (value == null) {
+ return _suppressNulls;
+ }
+ if (_suppressableValue == null) {
+ return false;
+ }
+ JsonSerializer<Object> valueSer = _valueSerializer;
+ if (valueSer == null) {
+ // Let's not worry about generic types here, actually;
+ // unlikely to make any difference, but does add significant overhead
+ Class<?> cc = value.getClass();
+ valueSer = _dynamicValueSerializers.serializerFor(cc.getClass());
+ if (valueSer == null) {
+ try {
+ valueSer = _findAndAddDynamic(_dynamicValueSerializers, cc, prov);
+ } catch (JsonMappingException e) { // Ugh... can not just throw as-is, so...
+ return false;
+ }
+ }
+ }
+ if (_suppressableValue == MARKER_FOR_EMPTY) {
+ return valueSer.isEmpty(prov, value);
+ }
+ return _suppressableValue.equals(value);
}
/*
@@ -188,11 +336,7 @@
throws IOException
{
gen.writeStartObject(value);
- if (_valueSerializer != null) {
- serializeUsing(value, gen, provider, _valueSerializer);
- } else {
- serializeDynamic(value, gen, provider);
- }
+ serializeDynamic(value, gen, provider);
gen.writeEndObject();
}
@@ -203,97 +347,67 @@
typeSer.writeTypePrefixForObject(value, gen);
// [databind#631]: Assign current value, to be accessible by custom serializers
gen.setCurrentValue(value);
- if (_valueSerializer != null) {
- serializeUsing(value, gen, provider, _valueSerializer);
- } else {
- serializeDynamic(value, gen, provider);
- }
+ serializeDynamic(value, gen, provider);
typeSer.writeTypeSuffixForObject(value, gen);
}
- protected void serializeDynamic(Map.Entry<?, ?> value, JsonGenerator jgen, SerializerProvider provider)
+ protected void serializeDynamic(Map.Entry<?, ?> value, JsonGenerator gen,
+ SerializerProvider provider)
throws IOException
{
- final JsonSerializer<Object> keySerializer = _keySerializer;
- final boolean skipNulls = !provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES);
final TypeSerializer vts = _valueTypeSerializer;
+ final Object keyElem = value.getKey();
- PropertySerializerMap serializers = _dynamicValueSerializers;
-
- Object valueElem = value.getValue();
- Object keyElem = value.getKey();
+ JsonSerializer<Object> keySerializer;
if (keyElem == null) {
- provider.findNullKeySerializer(_keyType, _property).serialize(null, jgen, provider);
+ keySerializer = provider.findNullKeySerializer(_keyType, _property);
} else {
- // [JACKSON-314] skip entries with null values?
- if (skipNulls && valueElem == null) return;
- keySerializer.serialize(keyElem, jgen, provider);
+ keySerializer = _keySerializer;
}
+ // or by value; nulls often suppressed
+ final Object valueElem = value.getValue();
+ JsonSerializer<Object> valueSer;
// And then value
if (valueElem == null) {
- provider.defaultSerializeNull(jgen);
- } else {
- Class<?> cc = valueElem.getClass();
- JsonSerializer<Object> ser = serializers.serializerFor(cc);
- if (ser == null) {
- if (_valueType.hasGenericTypes()) {
- ser = _findAndAddDynamic(serializers,
- provider.constructSpecializedType(_valueType, cc), provider);
- } else {
- ser = _findAndAddDynamic(serializers, cc, provider);
- }
- serializers = _dynamicValueSerializers;
+ if (_suppressNulls) {
+ return;
}
- try {
- if (vts == null) {
- ser.serialize(valueElem, jgen, provider);
- } else {
- ser.serializeWithType(valueElem, jgen, provider, vts);
+ valueSer = provider.getDefaultNullValueSerializer();
+ } else {
+ valueSer = _valueSerializer;
+ if (valueSer == null) {
+ Class<?> cc = valueElem.getClass();
+ valueSer = _dynamicValueSerializers.serializerFor(cc);
+ if (valueSer == null) {
+ if (_valueType.hasGenericTypes()) {
+ valueSer = _findAndAddDynamic(_dynamicValueSerializers,
+ provider.constructSpecializedType(_valueType, cc), provider);
+ } else {
+ valueSer = _findAndAddDynamic(_dynamicValueSerializers, cc, provider);
+ }
}
- } catch (Exception e) {
- // [JACKSON-55] Need to add reference information
- String keyDesc = ""+keyElem;
- wrapAndThrow(provider, e, value, keyDesc);
+ }
+ // also may need to skip non-empty values:
+ if (_suppressableValue != null) {
+ if (_suppressableValue == MARKER_FOR_EMPTY) {
+ if (valueSer.isEmpty(provider, valueElem)) {
+ return;
+ }
+ } if (_suppressableValue.equals(valueElem)) {
+ return;
+ }
}
}
- }
-
- /**
- * Method called to serialize fields, when the value type is statically known,
- * so that value serializer is passed and does not need to be fetched from
- * provider.
- */
- protected void serializeUsing(Map.Entry<?, ?> value, JsonGenerator jgen, SerializerProvider provider,
- JsonSerializer<Object> ser)
- throws IOException, JsonGenerationException
- {
- final JsonSerializer<Object> keySerializer = _keySerializer;
- final TypeSerializer vts = _valueTypeSerializer;
- final boolean skipNulls = !provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES);
-
- Object valueElem = value.getValue();
- Object keyElem = value.getKey();
- if (keyElem == null) {
- provider.findNullKeySerializer(_keyType, _property).serialize(null, jgen, provider);
- } else {
- // [JACKSON-314] also may need to skip entries with null values
- if (skipNulls && valueElem == null) return;
- keySerializer.serialize(keyElem, jgen, provider);
- }
- if (valueElem == null) {
- provider.defaultSerializeNull(jgen);
- } else {
- try {
- if (vts == null) {
- ser.serialize(valueElem, jgen, provider);
- } else {
- ser.serializeWithType(valueElem, jgen, provider, vts);
- }
- } catch (Exception e) {
- // [JACKSON-55] Need to add reference information
- String keyDesc = ""+keyElem;
- wrapAndThrow(provider, e, value, keyDesc);
+ keySerializer.serialize(keyElem, gen, provider);
+ try {
+ if (vts == null) {
+ valueSer.serialize(valueElem, gen, provider);
+ } else {
+ valueSer.serializeWithType(valueElem, gen, provider, vts);
}
+ } catch (Exception e) {
+ String keyDesc = ""+keyElem;
+ wrapAndThrow(provider, e, value, keyDesc);
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/ObjectIdWriter.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/ObjectIdWriter.java
index 42b4972..e532ad0 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/ObjectIdWriter.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/ObjectIdWriter.java
@@ -68,14 +68,7 @@
ObjectIdGenerator<?> generator, boolean alwaysAsId)
{
String simpleName = (propName == null) ? null : propName.getSimpleName();
- return construct(idType, simpleName, generator, alwaysAsId);
- }
-
- @Deprecated // since 2.3
- public static ObjectIdWriter construct(JavaType idType, String propName,
- ObjectIdGenerator<?> generator, boolean alwaysAsId)
- {
- SerializableString serName = (propName == null) ? null : new SerializedString(propName);
+ SerializableString serName = (simpleName == null) ? null : new SerializedString(simpleName);
return new ObjectIdWriter(idType, serName, generator, null, alwaysAsId);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringArraySerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringArraySerializer.java
index bbd9e23..1011bab 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringArraySerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringArraySerializer.java
@@ -107,11 +107,9 @@
ser = _elementSerializer;
}
// May have a content converter
- ser = findConvertingContentSerializer(provider, property, ser);
+ ser = findContextualConvertingSerializer(provider, property, ser);
if (ser == null) {
ser = provider.findValueSerializer(String.class, property);
- } else {
- ser = provider.handleSecondaryContextualization(ser, property);
}
// Optimization: default serializer just writes String, so we can avoid a call:
if (isDefaultSerializer(ser)) {
@@ -142,7 +140,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, String[] value) {
- return (value == null) || (value.length == 0);
+ return (value.length == 0);
}
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringCollectionSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringCollectionSerializer.java
index b45d8e9..bd6f53f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringCollectionSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/StringCollectionSerializer.java
@@ -36,17 +36,16 @@
}
protected StringCollectionSerializer(StringCollectionSerializer src,
- JsonSerializer<?> ser, Boolean unwrapSingle)
+ Boolean unwrapSingle)
{
- super(src, ser, unwrapSingle);
+ super(src, unwrapSingle);
}
@Override
- public JsonSerializer<?> _withResolved(BeanProperty prop,
- JsonSerializer<?> ser, Boolean unwrapSingle) {
- return new StringCollectionSerializer(this, ser, unwrapSingle);
+ public JsonSerializer<?> _withResolved(BeanProperty prop, Boolean unwrapSingle) {
+ return new StringCollectionSerializer(this, unwrapSingle);
}
-
+
@Override protected JsonNode contentSchema() {
return createSchemaNode("string", true);
}
@@ -72,80 +71,43 @@
if (((_unwrapSingle == null) &&
provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
|| (_unwrapSingle == Boolean.TRUE)) {
- _serializeUnwrapped(value, gen, provider);
+ serializeContents(value, gen, provider);
return;
}
- }
- gen.writeStartArray(len);
- if (_serializer == null) {
- serializeContents(value, gen, provider);
- } else {
- serializeUsingCustom(value, gen, provider);
}
+ gen.writeStartArray(len);
+ serializeContents(value, gen, provider);
gen.writeEndArray();
}
- private final void _serializeUnwrapped(Collection<String> value, JsonGenerator gen,
- SerializerProvider provider) throws IOException
- {
- if (_serializer == null) {
- serializeContents(value, gen, provider);
- } else {
- serializeUsingCustom(value, gen, provider);
- }
- }
-
@Override
- public void serializeWithType(Collection<String> value, JsonGenerator jgen, SerializerProvider provider,
+ public void serializeWithType(Collection<String> value, JsonGenerator g, SerializerProvider provider,
TypeSerializer typeSer)
- throws IOException, JsonGenerationException
+ throws IOException
{
- typeSer.writeTypePrefixForArray(value, jgen);
- if (_serializer == null) {
- serializeContents(value, jgen, provider);
- } else {
- serializeUsingCustom(value, jgen, provider);
- }
- typeSer.writeTypeSuffixForArray(value, jgen);
+ typeSer.writeTypePrefixForArray(value, g);
+ serializeContents(value, g, provider);
+ typeSer.writeTypeSuffixForArray(value, g);
}
- private final void serializeContents(Collection<String> value, JsonGenerator jgen, SerializerProvider provider)
- throws IOException, JsonGenerationException
+ private final void serializeContents(Collection<String> value, JsonGenerator g, SerializerProvider provider)
+ throws IOException
{
- if (_serializer != null) {
- serializeUsingCustom(value, jgen, provider);
- return;
- }
+ g.setCurrentValue(value);
+
int i = 0;
- for (String str : value) {
- try {
+
+ try {
+ for (String str : value) {
if (str == null) {
- provider.defaultSerializeNull(jgen);
+ provider.defaultSerializeNull(g);
} else {
- jgen.writeString(str);
+ g.writeString(str);
}
++i;
- } catch (Exception e) {
- wrapAndThrow(provider, e, value, i);
}
+ } catch (Exception e) {
+ wrapAndThrow(provider, e, value, i);
}
}
-
- private void serializeUsingCustom(Collection<String> value, JsonGenerator jgen, SerializerProvider provider)
- throws IOException, JsonGenerationException
- {
- final JsonSerializer<String> ser = _serializer;
- int i = 0;
- for (String str : value) {
- try {
- if (str == null) {
- provider.defaultSerializeNull(jgen);
- } else {
- ser.serialize(str, jgen, provider);
- }
- } catch (Exception e) {
- wrapAndThrow(provider, e, value, i);
- }
- }
- }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/TypeWrappedSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/TypeWrappedSerializer.java
index e3837a6..cb19209 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/TypeWrappedSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/TypeWrappedSerializer.java
@@ -3,10 +3,9 @@
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
-
-import com.fasterxml.jackson.databind.JsonSerializer;
-import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
+import com.fasterxml.jackson.databind.ser.ContextualSerializer;
/**
* Simple serializer that will call configured type serializer, passing
@@ -15,6 +14,7 @@
*/
public final class TypeWrappedSerializer
extends JsonSerializer<Object>
+ implements ContextualSerializer // since 2.9
{
final protected TypeSerializer _typeSerializer;
final protected JsonSerializer<Object> _serializer;
@@ -47,6 +47,27 @@
/*
/**********************************************************
+ /* ContextualDeserializer
+ /**********************************************************
+ */
+
+ @Override // since 2.9
+ public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property)
+ throws JsonMappingException
+ {
+ // 13-Mar-2017, tatu: Should we call `TypeSerializer.forProperty()`?
+ JsonSerializer<?> ser = _serializer;
+ if (ser instanceof ContextualSerializer) {
+ ser = provider.handleSecondaryContextualization(ser, property);
+ }
+ if (ser == _serializer) {
+ return this;
+ }
+ return new TypeWrappedSerializer(_typeSerializer, ser);
+ }
+
+ /*
+ /**********************************************************
/* Extended API for other core classes
/**********************************************************
*/
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnknownSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnknownSerializer.java
index 6c1530b..841d4bc 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnknownSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnknownSerializer.java
@@ -66,7 +66,8 @@
protected void failForEmpty(SerializerProvider prov, Object value)
throws JsonMappingException {
- prov.reportMappingProblem("No serializer found for class %s and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)",
- value.getClass().getName());
+ prov.reportBadDefinition(handledType(), String.format(
+ "No serializer found for class %s and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)",
+ value.getClass().getName()));
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanPropertyWriter.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanPropertyWriter.java
index 9b558ce..baf978d 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanPropertyWriter.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanPropertyWriter.java
@@ -134,14 +134,14 @@
@Override
public void assignSerializer(JsonSerializer<Object> ser)
{
- super.assignSerializer(ser);
- if (_serializer != null) {
+ if (ser != null) {
NameTransformer t = _nameTransformer;
- if (_serializer.isUnwrappingSerializer()) {
- t = NameTransformer.chainedTransformer(t, ((UnwrappingBeanSerializer) _serializer)._nameTransformer);
+ if (ser.isUnwrappingSerializer()) {
+ t = NameTransformer.chainedTransformer(t, ((UnwrappingBeanSerializer) ser)._nameTransformer);
}
- _serializer = _serializer.unwrappingSerializer(t);
+ ser = ser.unwrappingSerializer(t);
}
+ super.assignSerializer(ser);
}
/*
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanSerializer.java
index 834e03a..481c3cf 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanSerializer.java
@@ -126,7 +126,8 @@
TypeSerializer typeSer) throws IOException
{
if (provider.isEnabled(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS)) {
- provider.reportMappingProblem("Unwrapped property requires use of type information: can not serialize without disabling `SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS`");
+ provider.reportBadDefinition(handledType(),
+ "Unwrapped property requires use of type information: can not serialize without disabling `SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS`");
}
gen.setCurrentValue(bean); // [databind#631]
if (_objectIdWriter != null) {
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/ArraySerializerBase.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/ArraySerializerBase.java
index c89409c..349df45 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/ArraySerializerBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/ArraySerializerBase.java
@@ -29,7 +29,7 @@
* @since 2.6
*/
protected final Boolean _unwrapSingle;
-
+
protected ArraySerializerBase(Class<T> cls)
{
super(cls);
@@ -111,16 +111,14 @@
@Override
public void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOException
{
- if (((_unwrapSingle == null) &&
- provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
- || (_unwrapSingle == Boolean.TRUE)) {
+ if (_shouldUnwrapSingle(provider)) {
if (hasSingleElement(value)) {
serializeContents(value, gen, provider);
return;
}
}
- gen.setCurrentValue(value);
gen.writeStartArray();
+ gen.setCurrentValue(value);
// [databind#631]: Assign current value, to be accessible by custom serializers
serializeContents(value, gen, provider);
gen.writeEndArray();
@@ -140,4 +138,14 @@
protected abstract void serializeContents(T value, JsonGenerator jgen, SerializerProvider provider)
throws IOException;
+
+ /**
+ * @since 2.9
+ */
+ protected final boolean _shouldUnwrapSingle(SerializerProvider provider) {
+ if (_unwrapSingle == null) {
+ return provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
+ }
+ return _unwrapSingle.booleanValue();
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/AsArraySerializerBase.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/AsArraySerializerBase.java
index a1bd6b9..b6c41ee 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/AsArraySerializerBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/AsArraySerializerBase.java
@@ -191,7 +191,7 @@
ser = _elementSerializer;
}
// 18-Feb-2013, tatu: May have a content converter:
- ser = findConvertingContentSerializer(serializers, property, ser);
+ ser = findContextualConvertingSerializer(serializers, property, ser);
if (ser == null) {
// 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
// we can consider it a static case as well.
@@ -200,8 +200,6 @@
ser = serializers.findValueSerializer(_elementType, property);
}
}
- } else {
- ser = serializers.handleSecondaryContextualization(ser, property);
}
if ((ser != _elementSerializer)
|| (property != _property)
@@ -273,15 +271,10 @@
throws JsonMappingException
{
ObjectNode o = createSchemaNode("array", true);
- JavaType contentType = _elementType;
- if (contentType != null) {
+ if (_elementSerializer != null) {
JsonNode schemaNode = null;
- // 15-Oct-2010, tatu: We can't serialize plain Object.class; but what should it produce here? Untyped?
- if (contentType.getRawClass() != Object.class) {
- JsonSerializer<Object> ser = provider.findValueSerializer(contentType, _property);
- if (ser instanceof SchemaAware) {
- schemaNode = ((SchemaAware) ser).getSchema(provider, null);
- }
+ if (_elementSerializer instanceof SchemaAware) {
+ schemaNode = ((SchemaAware) _elementSerializer).getSchema(provider, null);
}
if (schemaNode == null) {
schemaNode = com.fasterxml.jackson.databind.jsonschema.JsonSchema.getDefaultSchemaNode();
@@ -297,7 +290,11 @@
{
JsonSerializer<?> valueSer = _elementSerializer;
if (valueSer == null) {
- valueSer = visitor.getProvider().findValueSerializer(_elementType, _property);
+ // 19-Oct-2016, tatu: Apparently we get null for untyped/raw `EnumSet`s... not 100%
+ // sure what'd be the clean way but let's try this for now:
+ if (_elementType != null) {
+ valueSer = visitor.getProvider().findValueSerializer(_elementType, _property);
+ }
}
visitArrayFormat(visitor, typeHint, valueSer, _elementType);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/AtomicReferenceSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/AtomicReferenceSerializer.java
index 7726bb0..d44d9af 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/AtomicReferenceSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/AtomicReferenceSerializer.java
@@ -2,8 +2,6 @@
import java.util.concurrent.atomic.AtomicReference;
-import com.fasterxml.jackson.annotation.JsonInclude;
-
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.type.ReferenceType;
@@ -29,23 +27,28 @@
protected AtomicReferenceSerializer(AtomicReferenceSerializer base, BeanProperty property,
TypeSerializer vts, JsonSerializer<?> valueSer,
NameTransformer unwrapper,
- JsonInclude.Include contentIncl)
+ Object suppressableValue, boolean suppressNulls)
{
- super(base, property, vts, valueSer, unwrapper, contentIncl);
+ super(base, property, vts, valueSer, unwrapper,
+ suppressableValue, suppressNulls);
}
@Override
- protected AtomicReferenceSerializer withResolved(BeanProperty prop,
+ protected ReferenceTypeSerializer<AtomicReference<?>> withResolved(BeanProperty prop,
TypeSerializer vts, JsonSerializer<?> valueSer,
- NameTransformer unwrapper,
- JsonInclude.Include contentIncl)
+ NameTransformer unwrapper)
{
- if ((_property == prop) && (contentIncl == _contentInclusion)
- && (_valueTypeSerializer == vts) && (_valueSerializer == valueSer)
- && (_unwrapper == unwrapper)) {
- return this;
- }
- return new AtomicReferenceSerializer(this, prop, vts, valueSer, unwrapper, contentIncl);
+ return new AtomicReferenceSerializer(this, prop, vts, valueSer, unwrapper,
+ _suppressableValue, _suppressNulls);
+ }
+
+ @Override
+ public ReferenceTypeSerializer<AtomicReference<?>> withContentInclusion(Object suppressableValue,
+ boolean suppressNulls)
+ {
+ return new AtomicReferenceSerializer(this, _property, _valueTypeSerializer,
+ _valueSerializer, _unwrapper,
+ suppressableValue, suppressNulls);
}
/*
@@ -55,8 +58,8 @@
*/
@Override
- protected boolean _isValueEmpty(AtomicReference<?> value) {
- return value.get() == null;
+ protected boolean _isValuePresent(AtomicReference<?> value) {
+ return value.get() != null;
}
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java
index ab0850e..e798c70 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java
@@ -17,6 +17,7 @@
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.*;
+import com.fasterxml.jackson.databind.ser.impl.MapEntrySerializer;
import com.fasterxml.jackson.databind.ser.impl.ObjectIdWriter;
import com.fasterxml.jackson.databind.ser.impl.PropertyBasedObjectIdGenerator;
import com.fasterxml.jackson.databind.ser.impl.WritableObjectId;
@@ -37,7 +38,7 @@
JsonFormatVisitable, SchemaAware
{
protected final static PropertyName NAME_FOR_OBJECT_REF = new PropertyName("#object-ref");
-
+
final protected static BeanPropertyWriter[] NO_PROPS = new BeanPropertyWriter[0];
/*
@@ -47,6 +48,11 @@
*/
/**
+ * @since 2.9
+ */
+ final protected JavaType _beanType;
+
+ /**
* Writers used for outputting actual property values
*/
final protected BeanPropertyWriter[] _props;
@@ -103,6 +109,7 @@
BeanPropertyWriter[] properties, BeanPropertyWriter[] filteredProperties)
{
super(type);
+ _beanType = type;
_props = properties;
_filteredProps = filteredProperties;
if (builder == null) { // mostly for testing
@@ -125,6 +132,7 @@
BeanPropertyWriter[] properties, BeanPropertyWriter[] filteredProperties)
{
super(src._handledType);
+ _beanType = src._beanType;
_props = properties;
_filteredProps = filteredProperties;
@@ -148,6 +156,7 @@
ObjectIdWriter objectIdWriter, Object filterId)
{
super(src._handledType);
+ _beanType = src._beanType;
_props = src._props;
_filteredProps = src._filteredProps;
@@ -168,6 +177,7 @@
{
super(src._handledType);
+ _beanType = src._beanType;
final BeanPropertyWriter[] propsIn = src._props;
final BeanPropertyWriter[] fpropsIn = src._filteredProps;
final int len = propsIn.length;
@@ -318,9 +328,6 @@
// It not, we can use declared return type if and only if declared type is final:
// if not, we don't really know the actual type until we get the instance.
if (type == null) {
- // 30-Oct-2015, tatu: Not sure why this was used
-// type = provider.constructType(prop.getGenericPropertyType());
- // but this looks better
type = prop.getType();
if (!type.isFinal()) {
if (type.isContainerType() || type.containedTypeCount() > 0) {
@@ -346,14 +353,18 @@
}
}
}
- prop.assignSerializer(ser);
- // and maybe replace filtered property too? (see [JACKSON-364])
+ // and maybe replace filtered property too?
if (i < filteredCount) {
BeanPropertyWriter w2 = _filteredProps[i];
if (w2 != null) {
w2.assignSerializer(ser);
+ // 17-Mar-2017, tatu: Typically will lead to chained call to original property,
+ // which would lead to double set. Not a problem itself, except... unwrapping
+ // may require work to be done, which does lead to an actual issue.
+ continue;
}
}
+ prop.assignSerializer(ser);
}
// also, any-getter may need to be resolved
@@ -418,11 +429,27 @@
case NUMBER_INT:
// 12-Oct-2014, tatu: May need to introspect full annotations... but
// for now, just do class ones
- BeanDescription desc = config.introspectClassAnnotations(_handledType);
- JsonSerializer<?> ser = EnumSerializer.construct(_handledType,
+ BeanDescription desc = config.introspectClassAnnotations(_beanType);
+ JsonSerializer<?> ser = EnumSerializer.construct(_beanType.getRawClass(),
provider.getConfig(), desc, format);
return provider.handlePrimaryContextualization(ser, property);
}
+ // 16-Oct-2016, tatu: Ditto for `Map`, `Map.Entry` subtypes
+ } else if (shape == JsonFormat.Shape.NATURAL) {
+ if (_beanType.isMapLikeType() && Map.class.isAssignableFrom(_handledType)) {
+;
+ } else if (Map.Entry.class.isAssignableFrom(_handledType)) {
+ JavaType mapEntryType = _beanType.findSuperType(Map.Entry.class);
+
+ JavaType kt = mapEntryType.containedTypeOrUnknown(0);
+ JavaType vt = mapEntryType.containedTypeOrUnknown(1);
+
+ // 16-Oct-2016, tatu: could have problems with type handling, as we do not
+ // see if "static" typing is needed, nor look for `TypeSerializer` yet...
+ JsonSerializer<?> ser = new MapEntrySerializer(_beanType, kt, vt,
+ false, null, property);
+ return provider.handlePrimaryContextualization(ser, property);
+ }
}
}
}
@@ -461,10 +488,11 @@
String propName = objectIdInfo.getPropertyName().getSimpleName();
BeanPropertyWriter idProp = null;
- for (int i = 0, len = _props.length ;; ++i) {
+ for (int i = 0, len = _props.length; ; ++i) {
if (i == len) {
- throw new IllegalArgumentException("Invalid Object Id definition for "+_handledType.getName()
- +": can not find property with name '"+propName+"'");
+ provider.reportBadDefinition(_beanType, String.format(
+ "Invalid Object Id definition for %s: can not find property with name '%s'",
+ handledType().getName(), propName));
}
BeanPropertyWriter prop = _props[i];
if (propName.equals(prop.getName())) {
@@ -493,7 +521,6 @@
objectIdInfo.getAlwaysAsId());
}
}
-
// Or change Filter Id in use?
Object filterId = intr.findFilterId(accessor);
if (filterId != null) {
@@ -522,6 +549,7 @@
if (shape == null) {
shape = _serializationShape;
}
+ // last but not least; may need to transmute into as-array serialization
if (shape == JsonFormat.Shape.ARRAY) {
return contextual.asArraySerializer();
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/BooleanSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/BooleanSerializer.java
index a69a88c..9aaff5f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/BooleanSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/BooleanSerializer.java
@@ -3,25 +3,32 @@
import java.io.IOException;
import java.lang.reflect.Type;
+import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;
-
+import com.fasterxml.jackson.core.JsonParser.NumberType;
+import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
+import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
+import com.fasterxml.jackson.databind.ser.ContextualSerializer;
/**
* Serializer used for primitive boolean, as well as java.util.Boolean
* wrapper type.
*<p>
- * Since this is one of "native" types, no type information is ever
- * included on serialization (unlike for most scalar types as of 1.5)
+ * Since this is one of "natural" (aka "native") types, no type information is ever
+ * included on serialization (unlike for most other scalar types)
*/
@JacksonStdImpl
public final class BooleanSerializer
- extends NonTypedScalarSerializerBase<Boolean>
+//In 2.9, removed use of intermediate type `NonTypedScalarSerializerBase`
+ extends StdScalarSerializer<Object>
+ implements ContextualSerializer
{
private static final long serialVersionUID = 1L;
@@ -32,25 +39,106 @@
protected final boolean _forPrimitive;
public BooleanSerializer(boolean forPrimitive) {
- super(Boolean.class);
+ super(forPrimitive ? Boolean.TYPE : Boolean.class, false);
_forPrimitive = forPrimitive;
}
@Override
- public void serialize(Boolean value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
- jgen.writeBoolean(value.booleanValue());
+ public JsonSerializer<?> createContextual(SerializerProvider serializers,
+ BeanProperty property) throws JsonMappingException
+ {
+ JsonFormat.Value format = findFormatOverrides(serializers,
+ property, Boolean.class);
+ if (format != null) {
+ JsonFormat.Shape shape = format.getShape();
+ if (shape.isNumeric()) {
+ return new AsNumber(_forPrimitive);
+ }
+ }
+ return this;
+ }
+
+ @Override
+ public void serialize(Object value, JsonGenerator g, SerializerProvider provider) throws IOException {
+ g.writeBoolean(Boolean.TRUE.equals(value));
+ }
+
+ @Override
+ public final void serializeWithType(Object value, JsonGenerator g, SerializerProvider provider,
+ TypeSerializer typeSer) throws IOException
+ {
+ g.writeBoolean(Boolean.TRUE.equals(value));
}
@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
return createSchemaNode("boolean", !_forPrimitive);
}
-
+
@Override
- public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException
- {
- if (visitor != null) {
- visitor.expectBooleanFormat(typeHint);
- }
+ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {
+ visitor.expectBooleanFormat(typeHint);
}
-}
\ No newline at end of file
+
+ /**
+ * Alternate implementation that is used when values are to be serialized
+ * as numbers <code>0</code> (false) or <code>1</code> (true).
+ *
+ * @since 2.9
+ */
+ final static class AsNumber
+ extends StdScalarSerializer<Object>
+ implements ContextualSerializer
+ {
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Whether type serialized is primitive (boolean) or wrapper
+ * (java.lang.Boolean); if true, former, if false, latter.
+ */
+ protected final boolean _forPrimitive;
+
+ public AsNumber(boolean forPrimitive) {
+ super(forPrimitive ? Boolean.TYPE : Boolean.class, false);
+ _forPrimitive = forPrimitive;
+ }
+
+ @Override
+ public void serialize(Object value, JsonGenerator g, SerializerProvider provider) throws IOException {
+ g.writeNumber((Boolean.FALSE.equals(value)) ? 0 : 1);
+ }
+
+ @Override
+ public final void serializeWithType(Object value, JsonGenerator g, SerializerProvider provider,
+ TypeSerializer typeSer) throws IOException
+ {
+ // 27-Mar-2017, tatu: Actually here we CAN NOT serialize as number without type,
+ // since with natural types that would map to number, not boolean. So choice
+ // comes to between either add type id, or serialize as boolean. Choose
+ // latter at this point
+ g.writeBoolean(Boolean.TRUE.equals(value));
+ }
+
+ @Override
+ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {
+ // 27-Mar-2017, tatu: As usual, bit tricky but... seems like we should call
+ // visitor for actual representation
+ visitIntFormat(visitor, typeHint, NumberType.INT);
+ }
+
+ @Override
+ public JsonSerializer<?> createContextual(SerializerProvider serializers,
+ BeanProperty property) throws JsonMappingException
+ {
+ JsonFormat.Value format = findFormatOverrides(serializers,
+ property, Boolean.class);
+ if (format != null) {
+ JsonFormat.Shape shape = format.getShape();
+ if (!shape.isNumeric()) {
+ return new BooleanSerializer(_forPrimitive);
+ }
+ }
+ return this;
+ }
+}
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteArraySerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteArraySerializer.java
index e19ba93..54566c3 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteArraySerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteArraySerializer.java
@@ -36,7 +36,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, byte[] value) {
- return (value == null) || (value.length == 0);
+ return value.length == 0;
}
@Override
@@ -65,7 +65,7 @@
ObjectNode itemSchema = createSchemaNode("byte"); //binary values written as strings?
return o.set("items", itemSchema);
}
-
+
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException
@@ -76,11 +76,9 @@
//
// TODO: for 2.8, make work either as String/base64, or array of numbers,
// with a qualifier that can be used to determine it's byte[]
- if (visitor != null) {
- JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
- if (v2 != null) {
- v2.itemsFormat(JsonFormatTypes.INTEGER);
- }
+ JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
+ if (v2 != null) {
+ v2.itemsFormat(JsonFormatTypes.INTEGER);
}
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteBufferSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteBufferSerializer.java
index e838001..d381b2b 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteBufferSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteBufferSerializer.java
@@ -5,6 +5,9 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor;
+import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
+import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream;
@SuppressWarnings("serial")
@@ -30,4 +33,15 @@
gen.writeBinary(in, copy.remaining());
in.close();
}
+
+ @Override // since 2.9
+ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
+ throws JsonMappingException
+ {
+ // 31-Mar-2017, tatu: Use same type as `ByteArraySerializer`: not optimal but has to do
+ JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
+ if (v2 != null) {
+ v2.itemsFormat(JsonFormatTypes.INTEGER);
+ }
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/CollectionSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/CollectionSerializer.java
index 7cb1885..6dec6a7 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/CollectionSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/CollectionSerializer.java
@@ -77,17 +77,12 @@
@Override
public boolean isEmpty(SerializerProvider prov, Collection<?> value) {
- return (value == null) || value.isEmpty();
+ return value.isEmpty();
}
@Override
public boolean hasSingleElement(Collection<?> value) {
- Iterator<?> it = value.iterator();
- if (!it.hasNext()) {
- return false;
- }
- it.next();
- return !it.hasNext();
+ return value.size() == 1;
}
/*
@@ -97,27 +92,28 @@
*/
@Override
- public final void serialize(Collection<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException
+ public final void serialize(Collection<?> value, JsonGenerator g, SerializerProvider provider) throws IOException
{
final int len = value.size();
if (len == 1) {
if (((_unwrapSingle == null) &&
provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
|| (_unwrapSingle == Boolean.TRUE)) {
- serializeContents(value, jgen, provider);
+ serializeContents(value, g, provider);
return;
}
}
- jgen.writeStartArray(len);
- serializeContents(value, jgen, provider);
- jgen.writeEndArray();
+ g.writeStartArray(len);
+ serializeContents(value, g, provider);
+ g.writeEndArray();
}
@Override
- public void serializeContents(Collection<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException
+ public void serializeContents(Collection<?> value, JsonGenerator g, SerializerProvider provider) throws IOException
{
+ g.setCurrentValue(value);
if (_elementSerializer != null) {
- serializeContentsUsing(value, jgen, provider, _elementSerializer);
+ serializeContentsUsing(value, g, provider, _elementSerializer);
return;
}
Iterator<?> it = value.iterator();
@@ -132,7 +128,7 @@
do {
Object elem = it.next();
if (elem == null) {
- provider.defaultSerializeNull(jgen);
+ provider.defaultSerializeNull(g);
} else {
Class<?> cc = elem.getClass();
JsonSerializer<Object> serializer = serializers.serializerFor(cc);
@@ -146,9 +142,9 @@
serializers = _dynamicSerializers;
}
if (typeSer == null) {
- serializer.serialize(elem, jgen, provider);
+ serializer.serialize(elem, g, provider);
} else {
- serializer.serializeWithType(elem, jgen, provider, typeSer);
+ serializer.serializeWithType(elem, g, provider, typeSer);
}
}
++i;
@@ -158,9 +154,8 @@
}
}
- public void serializeContentsUsing(Collection<?> value, JsonGenerator jgen, SerializerProvider provider,
- JsonSerializer<Object> ser)
- throws IOException, JsonGenerationException
+ public void serializeContentsUsing(Collection<?> value, JsonGenerator g, SerializerProvider provider,
+ JsonSerializer<Object> ser) throws IOException
{
Iterator<?> it = value.iterator();
if (it.hasNext()) {
@@ -170,12 +165,12 @@
Object elem = it.next();
try {
if (elem == null) {
- provider.defaultSerializeNull(jgen);
+ provider.defaultSerializeNull(g);
} else {
if (typeSer == null) {
- ser.serialize(elem, jgen, provider);
+ ser.serialize(elem, g, provider);
} else {
- ser.serializeWithType(elem, jgen, provider, typeSer);
+ ser.serializeWithType(elem, g, provider, typeSer);
}
}
++i;
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/DateTimeSerializerBase.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/DateTimeSerializerBase.java
index ea67b88..d96f1f2 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/DateTimeSerializerBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/DateTimeSerializerBase.java
@@ -84,21 +84,16 @@
/**********************************************************
*/
- @Deprecated
- @Override
- public boolean isEmpty(T value) {
- // let's assume "null date" (timestamp 0) qualifies for empty
- return (value == null) || (_timestamp(value) == 0L);
- }
-
@Override
public boolean isEmpty(SerializerProvider serializers, T value) {
- // let's assume "null date" (timestamp 0) qualifies for empty
- return (value == null) || (_timestamp(value) == 0L);
+ // 09-Mar-2017, tatu: as per [databind#1550] timestamp 0 is NOT "empty"; but
+ // with versions up to 2.8.x this was the case. Fixed for 2.9.
+// return _timestamp(value) == 0L;
+ return false;
}
-
+
protected abstract long _timestamp(T value);
-
+
@Override
public JsonNode getSchema(SerializerProvider serializers, Type typeHint) {
//todo: (ryan) add a format for the date in the schema?
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/EnumSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/EnumSerializer.java
index dae384a..3a048b8 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/EnumSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/EnumSerializer.java
@@ -52,14 +52,6 @@
/* Construction, initialization
/**********************************************************
*/
-
- /**
- * @deprecated Since 2.1
- */
- @Deprecated
- public EnumSerializer(EnumValues v) {
- this(v, null);
- }
public EnumSerializer(EnumValues v, Boolean serializeAsIndex)
{
@@ -96,15 +88,14 @@
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property) throws JsonMappingException
{
- if (property != null) {
- JsonFormat.Value format = findFormatOverrides(serializers,
- property, handledType());
- if (format != null) {
- Boolean serializeAsIndex = _isShapeWrittenUsingIndex(property.getType().getRawClass(),
- format, false, _serializeAsIndex);
- if (serializeAsIndex != _serializeAsIndex) {
- return new EnumSerializer(_values, serializeAsIndex);
- }
+ JsonFormat.Value format = findFormatOverrides(serializers,
+ property, handledType());
+ if (format != null) {
+ Class<?> type = handledType();
+ Boolean serializeAsIndex = _isShapeWrittenUsingIndex(type,
+ format, false, _serializeAsIndex);
+ if (serializeAsIndex != _serializeAsIndex) {
+ return new EnumSerializer(_values, serializeAsIndex);
}
}
return this;
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/EnumSetSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/EnumSetSerializer.java
index af522dd..51394ab 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/EnumSetSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/EnumSetSerializer.java
@@ -18,14 +18,6 @@
super(EnumSet.class, elemType, true, null, null);
}
- /**
- * @deprecated since 2.6
- */
- @Deprecated // since 2.6
- public EnumSetSerializer(JavaType elemType, BeanProperty property) {
- this(elemType);
- }
-
public EnumSetSerializer(EnumSetSerializer src,
BeanProperty property, TypeSerializer vts, JsonSerializer<?> valueSerializer,
Boolean unwrapSingle) {
@@ -47,7 +39,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, EnumSet<? extends Enum<?>> value) {
- return (value == null) || value.isEmpty();
+ return value.isEmpty();
}
@Override
@@ -59,7 +51,7 @@
public final void serialize(EnumSet<? extends Enum<?>> value, JsonGenerator gen,
SerializerProvider provider) throws IOException
{
- final int len = value.size();
+ final int len = value.size();
if (len == 1) {
if (((_unwrapSingle == null)
&& provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/InetAddressSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/InetAddressSerializer.java
index 3371481..2f6cb95 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/InetAddressSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/InetAddressSerializer.java
@@ -3,38 +3,90 @@
import java.io.IOException;
import java.net.InetAddress;
+import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.*;
+import com.fasterxml.jackson.databind.BeanProperty;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
+import com.fasterxml.jackson.databind.ser.ContextualSerializer;
/**
* Simple serializer for {@link java.net.InetAddress}. Main complexity is
* with registration, since same serializer is to be used for sub-classes.
+ *<p>
+ * Since 2.9 allows use of {@link JsonFormat} configuration (annotation,
+ * per-type defaulting) so that if <code>JsonFormat.Shape.NUMBER</code>
+ * (or <code>ARRAY</code>) is used, will serialize as "host address"
+ * (dotted numbers) instead of simple conversion.
*/
@SuppressWarnings("serial")
public class InetAddressSerializer
extends StdScalarSerializer<InetAddress>
+ implements ContextualSerializer
{
- public InetAddressSerializer() { super(InetAddress.class); }
+ /**
+ * @since 2.9
+ */
+ protected final boolean _asNumeric;
+ public InetAddressSerializer() {
+ this(false);
+ }
+
+ /**
+ * @since 2.9
+ */
+ public InetAddressSerializer(boolean asNumeric) {
+ super(InetAddress.class);
+ _asNumeric = asNumeric;
+ }
+
@Override
- public void serialize(InetAddress value, JsonGenerator jgen, SerializerProvider provider) throws IOException
+ public JsonSerializer<?> createContextual(SerializerProvider serializers,
+ BeanProperty property) throws JsonMappingException
{
- // Ok: get textual description; choose "more specific" part
- String str = value.toString().trim();
- int ix = str.indexOf('/');
- if (ix >= 0) {
- if (ix == 0) { // missing host name; use address
- str = str.substring(1);
- } else { // otherwise use name
- str = str.substring(0, ix);
+ JsonFormat.Value format = findFormatOverrides(serializers,
+ property, handledType());
+ boolean asNumeric = false;
+ if (format != null) {
+ JsonFormat.Shape shape = format.getShape();
+ if (shape.isNumeric() || shape == JsonFormat.Shape.ARRAY) {
+ asNumeric = true;
}
}
- jgen.writeString(str);
+ if (asNumeric != _asNumeric) {
+ return new InetAddressSerializer(asNumeric);
+ }
+ return this;
}
@Override
- public void serializeWithType(InetAddress value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) throws IOException, JsonGenerationException
+ public void serialize(InetAddress value, JsonGenerator g, SerializerProvider provider) throws IOException
+ {
+ String str;
+
+ if (_asNumeric) { // since 2.9
+ str = value.getHostAddress();
+ } else {
+ // Ok: get textual description; choose "more specific" part
+ str = value.toString().trim();
+ int ix = str.indexOf('/');
+ if (ix >= 0) {
+ if (ix == 0) { // missing host name; use address
+ str = str.substring(1);
+ } else { // otherwise use name
+ str = str.substring(0, ix);
+ }
+ }
+ }
+ g.writeString(str);
+ }
+
+ @Override
+ public void serializeWithType(InetAddress value, JsonGenerator jgen, SerializerProvider provider,
+ TypeSerializer typeSer) throws IOException, JsonGenerationException
{
// Better ensure we don't use specific sub-classes...
typeSer.writeTypePrefixForScalar(value, jgen, InetAddress.class);
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/IterableSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/IterableSerializer.java
index 6c4082d..5cc5068 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/IterableSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/IterableSerializer.java
@@ -40,7 +40,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, Iterable<?> value) {
// Not really good way to implement this, but has to do for now:
- return (value == null) || !value.iterator().hasNext();
+ return !value.iterator().hasNext();
}
@Override
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/JsonValueSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/JsonValueSerializer.java
index 7582c3a..0e939cc 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/JsonValueSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/JsonValueSerializer.java
@@ -10,7 +10,7 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
-import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
+import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor;
@@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.BeanSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Serializer class that can serialize Object that have a
@@ -37,11 +38,11 @@
public class JsonValueSerializer
extends StdSerializer<Object>
implements ContextualSerializer, JsonFormatVisitable, SchemaAware
- {
+{
/**
- * @since 2.8 (was "plain" method before)
+ * @since 2.9
*/
- protected final AnnotatedMethod _accessorMethod;
+ protected final AnnotatedMember _accessor;
protected final JsonSerializer<Object> _valueSerializer;
@@ -71,10 +72,10 @@
* to information we need
*/
@SuppressWarnings("unchecked")
- public JsonValueSerializer(AnnotatedMethod valueMethod, JsonSerializer<?> ser)
+ public JsonValueSerializer(AnnotatedMember accessor, JsonSerializer<?> ser)
{
- super(valueMethod.getType());
- _accessorMethod = valueMethod;
+ super(accessor.getType());
+ _accessor = accessor;
_valueSerializer = (JsonSerializer<Object>) ser;
_property = null;
_forceTypeInformation = true; // gets reconsidered when we are contextualized
@@ -85,7 +86,7 @@
JsonSerializer<?> ser, boolean forceTypeInfo)
{
super(_notNullClass(src.handledType()));
- _accessorMethod = src._accessorMethod;
+ _accessor = src._accessor;
_valueSerializer = (JsonSerializer<Object>) ser;
_property = property;
_forceTypeInformation = forceTypeInfo;
@@ -127,7 +128,7 @@
* if not, we don't really know the actual type until we get the instance.
*/
// 10-Mar-2010, tatu: Except if static typing is to be used
- JavaType t = _accessorMethod.getType();
+ JavaType t = _accessor.getType();
if (provider.isEnabled(MapperFeature.USE_STATIC_TYPING) || t.isFinal()) {
// false -> no need to cache
/* 10-Mar-2010, tatu: Ideally we would actually separate out type
@@ -161,7 +162,7 @@
public void serialize(Object bean, JsonGenerator gen, SerializerProvider prov) throws IOException
{
try {
- Object value = _accessorMethod.getValue(bean);
+ Object value = _accessor.getValue(bean);
if (value == null) {
prov.defaultSerializeNull(gen);
return;
@@ -177,20 +178,8 @@
ser = prov.findTypedValueSerializer(c, true, _property);
}
ser.serialize(value, gen, prov);
- } catch (IOException ioe) {
- throw ioe;
} catch (Exception e) {
- Throwable t = e;
- // Need to unwrap this specific type, to see infinite recursion...
- while (t instanceof InvocationTargetException && t.getCause() != null) {
- t = t.getCause();
- }
- // Errors shouldn't be wrapped (and often can't, as well)
- if (t instanceof Error) {
- throw (Error) t;
- }
- // let's try to indicate the path best we can...
- throw JsonMappingException.wrapWithPath(t, bean, _accessorMethod.getName() + "()");
+ wrapAndThrow(prov, e, bean, _accessor.getName() + "()");
}
}
@@ -201,7 +190,7 @@
// Regardless of other parts, first need to find value to serialize:
Object value = null;
try {
- value = _accessorMethod.getValue(bean);
+ value = _accessor.getValue(bean);
// and if we got null, can also just write it directly
if (value == null) {
provider.defaultSerializeNull(gen);
@@ -227,20 +216,8 @@
// (delegat type).
TypeSerializerRerouter rr = new TypeSerializerRerouter(typeSer0, bean);
ser.serializeWithType(value, gen, provider, rr);
- } catch (IOException ioe) {
- throw ioe;
} catch (Exception e) {
- Throwable t = e;
- // Need to unwrap this specific type, to see infinite recursion...
- while (t instanceof InvocationTargetException && t.getCause() != null) {
- t = t.getCause();
- }
- // Errors shouldn't be wrapped (and often can't, as well)
- if (t instanceof Error) {
- throw (Error) t;
- }
- // let's try to indicate the path best we can...
- throw JsonMappingException.wrapWithPath(t, bean, _accessorMethod.getName() + "()");
+ wrapAndThrow(provider, e, bean, _accessor.getName() + "()");
}
}
@@ -268,8 +245,8 @@
*
* Note that meaning of JsonValue, then, is very different for Enums. Sigh.
*/
- final JavaType type = _accessorMethod.getType();
- Class<?> declaring = _accessorMethod.getDeclaringClass();
+ final JavaType type = _accessor.getType();
+ Class<?> declaring = _accessor.getDeclaringClass();
if ((declaring != null) && declaring.isEnum()) {
if (_acceptJsonFormatVisitorForEnum(visitor, typeHint, declaring)) {
return;
@@ -308,16 +285,14 @@
// 21-Apr-2016, tatu: This is convoluted to the max, but essentially we
// call `@JsonValue`-annotated accessor method on all Enum members,
// so it all "works out". To some degree.
- enums.add(String.valueOf(_accessorMethod.callOn(en)));
+ enums.add(String.valueOf(_accessor.getValue(en)));
} catch (Exception e) {
Throwable t = e;
while (t instanceof InvocationTargetException && t.getCause() != null) {
t = t.getCause();
}
- if (t instanceof Error) {
- throw (Error) t;
- }
- throw JsonMappingException.wrapWithPath(t, en, _accessorMethod.getName() + "()");
+ ClassUtil.throwIfError(t);
+ throw JsonMappingException.wrapWithPath(t, en, _accessor.getName() + "()");
}
}
stringVisitor.enumTypes(enums);
@@ -349,7 +324,7 @@
@Override
public String toString() {
- return "(@JsonValue serializer for method " + _accessorMethod.getDeclaringClass() + "#" + _accessorMethod.getName() + ")";
+ return "(@JsonValue serializer for method " + _accessor.getDeclaringClass() + "#" + _accessor.getName() + ")";
}
/*
@@ -438,7 +413,7 @@
public void writeTypePrefixForArray(Object value, JsonGenerator gen, Class<?> type) throws IOException {
_typeSerializer.writeTypePrefixForArray(_forObject, gen, type);
}
-
+
@Override
public void writeCustomTypePrefixForScalar(Object value, JsonGenerator gen, String typeId)
throws IOException {
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/MapProperty.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/MapProperty.java
index 44e94c8..41fdb1e 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/MapProperty.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/MapProperty.java
@@ -4,19 +4,13 @@
import java.lang.annotation.Annotation;
import com.fasterxml.jackson.core.JsonGenerator;
-import com.fasterxml.jackson.databind.BeanProperty;
-import com.fasterxml.jackson.databind.JavaType;
-import com.fasterxml.jackson.databind.JsonMappingException;
-import com.fasterxml.jackson.databind.JsonSerializer;
-import com.fasterxml.jackson.databind.PropertyMetadata;
-import com.fasterxml.jackson.databind.PropertyName;
-import com.fasterxml.jackson.databind.SerializerProvider;
+
+import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
-import com.fasterxml.jackson.databind.type.TypeFactory;
/**
* Helper class needed to support flexible filtering of Map properties
@@ -28,6 +22,8 @@
{
private static final long serialVersionUID = 1L;
+ private final static BeanProperty BOGUS_PROP = new BeanProperty.Bogus();
+
protected final TypeSerializer _typeSerializer;
protected final BeanProperty _property;
@@ -40,7 +36,7 @@
{
super((prop == null) ? PropertyMetadata.STD_REQUIRED_OR_OPTIONAL : prop.getMetadata());
_typeSerializer = typeSer;
- _property = prop;
+ _property = (prop == null) ? BOGUS_PROP : prop;
}
/**
@@ -70,12 +66,12 @@
@Override
public <A extends Annotation> A getAnnotation(Class<A> acls) {
- return (_property == null) ? null : _property.getAnnotation(acls);
+ return _property.getAnnotation(acls);
}
@Override
public <A extends Annotation> A getContextAnnotation(Class<A> acls) {
- return (_property == null) ? null : _property.getContextAnnotation(acls);
+ return _property.getContextAnnotation(acls);
}
@Override
@@ -128,9 +124,7 @@
SerializerProvider provider)
throws JsonMappingException
{
- if (_property != null) {
- _property.depositSchemaProperty(objectVisitor, provider);
- }
+ _property.depositSchemaProperty(objectVisitor, provider);
}
@Override
@@ -142,16 +136,16 @@
@Override
public JavaType getType() {
- return (_property == null) ? TypeFactory.unknownType() : _property.getType();
+ return _property.getType();
}
@Override
public PropertyName getWrapperName() {
- return (_property == null) ? null : _property.getWrapperName();
+ return _property.getWrapperName();
}
@Override
public AnnotatedMember getMember() {
- return (_property == null) ? null : _property.getMember();
+ return _property.getMember();
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java
index a3af8cc..2bfb904 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java
@@ -20,6 +20,8 @@
import com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.ArrayBuilders;
+import com.fasterxml.jackson.databind.util.BeanUtil;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Standard serializer implementation for serializing {link java.util.Map} types.
@@ -37,16 +39,22 @@
protected final static JavaType UNSPECIFIED_TYPE = TypeFactory.unknownType();
/**
+ * @since 2.9
+ */
+ public final static Object MARKER_FOR_EMPTY = JsonInclude.Include.NON_EMPTY;
+
+ /*
+ /**********************************************************
+ /* Basic information about referring property, type
+ /**********************************************************
+ */
+
+ /**
* Map-valued property being serialized with this instance
*/
protected final BeanProperty _property;
/**
- * Set of entries to omit during serialization, if any
- */
- protected final Set<String> _ignoredEntries;
-
- /**
* Whether static types should be used for serialization of values
* or not (if not, dynamic runtime type is used)
*/
@@ -62,6 +70,12 @@
*/
protected final JavaType _valueType;
+ /*
+ /**********************************************************
+ /* Serializers used
+ /**********************************************************
+ */
+
/**
* Key serializer to use, if it can be statically determined
*/
@@ -83,6 +97,17 @@
*/
protected PropertySerializerMap _dynamicValueSerializers;
+ /*
+ /**********************************************************
+ /* Config settings, filtering
+ /**********************************************************
+ */
+
+ /**
+ * Set of entries to omit during serialization, if any
+ */
+ protected final Set<String> _ignoredEntries;
+
/**
* Id of the property filter to use, if any; null if none.
*
@@ -91,17 +116,10 @@
protected final Object _filterId;
/**
- * Flag set if output is forced to be sorted by keys (usually due
- * to annotation).
- *
- * @since 2.4
- */
- protected final boolean _sortKeys;
-
- /**
* Value that indicates suppression mechanism to use for <b>values contained</b>;
- * either one of values of {@link com.fasterxml.jackson.annotation.JsonInclude.Include},
- * or actual object to compare against ("default value").
+ * either "filter" (of which <code>equals()</code> is called), or marker
+ * value of {@link #MARKER_FOR_EMPTY}, or null to indicate no filtering for
+ * non-null values.
* Note that inclusion value for Map instance itself is handled by caller (POJO
* property that refers to the Map value).
*
@@ -109,6 +127,28 @@
*/
protected final Object _suppressableValue;
+ /**
+ * Flag that indicates what to do with `null` values, distinct from
+ * handling of {@link #_suppressableValue}
+ *
+ * @since 2.9
+ */
+ protected final boolean _suppressNulls;
+
+ /*
+ /**********************************************************
+ /* Config settings, other
+ /**********************************************************
+ */
+
+ /**
+ * Flag set if output is forced to be sorted by keys (usually due
+ * to annotation).
+ *
+ * @since 2.4
+ */
+ protected final boolean _sortKeys;
+
/*
/**********************************************************
/* Life-cycle
@@ -138,17 +178,9 @@
_filterId = null;
_sortKeys = false;
_suppressableValue = null;
+ _suppressNulls = false;
}
- /**
- * @since 2.5
- */
- protected void _ensureOverride() {
- if (getClass() != MapSerializer.class) {
- throw new IllegalStateException("Missing override in class "+getClass().getName());
- }
- }
-
@SuppressWarnings("unchecked")
protected MapSerializer(MapSerializer src, BeanProperty property,
JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer,
@@ -168,18 +200,14 @@
_filterId = src._filterId;
_sortKeys = src._sortKeys;
_suppressableValue = src._suppressableValue;
- }
-
- @Deprecated // since 2.5
- protected MapSerializer(MapSerializer src, TypeSerializer vts) {
- this(src, vts, src._suppressableValue);
+ _suppressNulls = src._suppressNulls;
}
/**
- * @since 2.5
+ * @since 2.9
*/
protected MapSerializer(MapSerializer src, TypeSerializer vts,
- Object suppressableValue)
+ Object suppressableValue, boolean suppressNulls)
{
super(Map.class, false);
_ignoredEntries = src._ignoredEntries;
@@ -193,12 +221,8 @@
_property = src._property;
_filterId = src._filterId;
_sortKeys = src._sortKeys;
- // 05-Jun-2015, tatu: For referential, this is same as NON_EMPTY; for others, NON_NULL, so:
- if (suppressableValue == JsonInclude.Include.NON_ABSENT) {
- suppressableValue = _valueType.isReferenceType() ?
- JsonInclude.Include.NON_EMPTY : JsonInclude.Include.NON_NULL;
- }
_suppressableValue = suppressableValue;
+ _suppressNulls = suppressNulls;
}
protected MapSerializer(MapSerializer src, Object filterId, boolean sortKeys)
@@ -216,6 +240,7 @@
_filterId = filterId;
_sortKeys = sortKeys;
_suppressableValue = src._suppressableValue;
+ _suppressNulls = src._suppressNulls;
}
@Override
@@ -223,8 +248,8 @@
if (_valueTypeSerializer == vts) {
return this;
}
- _ensureOverride();
- return new MapSerializer(this, vts, null);
+ _ensureOverride("_withValueTypeSerializer");
+ return new MapSerializer(this, vts, _suppressableValue, _suppressNulls);
}
/**
@@ -234,7 +259,7 @@
JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer,
Set<String> ignored, boolean sortKeys)
{
- _ensureOverride();
+ _ensureOverride("withResolved");
MapSerializer ser = new MapSerializer(this, property, keySerializer, valueSerializer, ignored);
if (sortKeys != ser._sortKeys) {
ser = new MapSerializer(ser, _filterId, sortKeys);
@@ -247,7 +272,7 @@
if (_filterId == filterId) {
return this;
}
- _ensureOverride();
+ _ensureOverride("withFilterId");
return new MapSerializer(this, filterId, _sortKeys);
}
@@ -255,31 +280,14 @@
* Mutant factory for constructing an instance with different inclusion strategy
* for content (Map values).
*
- * @since 2.5
+ * @since 2.9
*/
- public MapSerializer withContentInclusion(Object suppressableValue) {
- if (suppressableValue == _suppressableValue) {
+ public MapSerializer withContentInclusion(Object suppressableValue, boolean suppressNulls) {
+ if ((suppressableValue == _suppressableValue) && (suppressNulls == _suppressNulls)) {
return this;
}
- _ensureOverride();
- return new MapSerializer(this, _valueTypeSerializer, suppressableValue);
- }
-
- /**
- * @since 2.3
- *
- * @deprecated Since 2.8 use the other overload
- */
- @Deprecated // since 2.8
- public static MapSerializer construct(String[] ignoredList, JavaType mapType,
- boolean staticValueType, TypeSerializer vts,
- JsonSerializer<Object> keySerializer, JsonSerializer<Object> valueSerializer,
- Object filterId)
- {
- Set<String> ignoredEntries = (ignoredList == null || ignoredList.length == 0)
- ? null : ArrayBuilders.arrayToSet(ignoredList);
- return construct(ignoredEntries, mapType, staticValueType, vts,
- keySerializer, valueSerializer, filterId);
+ _ensureOverride("withContentInclusion");
+ return new MapSerializer(this, _valueTypeSerializer, suppressableValue, suppressNulls);
}
/**
@@ -315,6 +323,62 @@
return ser;
}
+ /**
+ * @since 2.9
+ */
+ protected void _ensureOverride(String method) {
+ ClassUtil.verifyMustOverride(MapSerializer.class, this, method);
+ }
+
+ /**
+ * @since 2.5
+ */
+ @Deprecated // since 2.9
+ protected void _ensureOverride() {
+ _ensureOverride("N/A");
+ }
+
+ /*
+ /**********************************************************
+ /* Deprecated creators
+ /**********************************************************
+ */
+
+ /**
+ * @since 2.5
+ * @deprecated // since 2.9
+ */
+ @Deprecated // since 2.9
+ protected MapSerializer(MapSerializer src, TypeSerializer vts,
+ Object suppressableValue)
+ {
+ this(src, vts, suppressableValue, false);
+ }
+
+ /**
+ * @deprecated since 2.9
+ */
+ @Deprecated // since 2.9
+ public MapSerializer withContentInclusion(Object suppressableValue) {
+ return new MapSerializer(this, _valueTypeSerializer, suppressableValue, _suppressNulls);
+ }
+
+ /**
+ * @since 2.3
+ *
+ * @deprecated Since 2.8 use the other overload
+ */
+ @Deprecated // since 2.8
+ public static MapSerializer construct(String[] ignoredList, JavaType mapType,
+ boolean staticValueType, TypeSerializer vts,
+ JsonSerializer<Object> keySerializer, JsonSerializer<Object> valueSerializer,
+ Object filterId)
+ {
+ Set<String> ignoredEntries = ArrayBuilders.arrayToSet(ignoredList);
+ return construct(ignoredEntries, mapType, staticValueType, vts,
+ keySerializer, valueSerializer, filterId);
+ }
+
/*
/**********************************************************
/* Post-processing (contextualization)
@@ -330,10 +394,9 @@
JsonSerializer<?> keySer = null;
final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
final AnnotatedMember propertyAcc = (property == null) ? null : property.getMember();
- Object suppressableValue = _suppressableValue;
// First: if we have a property, may have property-annotation overrides
- if ((propertyAcc != null) && (intr != null)) {
+ if (_neitherNull(propertyAcc, intr)) {
Object serDef = intr.findKeySerializer(propertyAcc);
if (serDef != null) {
keySer = provider.serializerInstance(propertyAcc, serDef);
@@ -343,17 +406,11 @@
ser = provider.serializerInstance(propertyAcc, serDef);
}
}
-
- JsonInclude.Value inclV = findIncludeOverrides(provider, property, Map.class);
- JsonInclude.Include incl = inclV.getContentInclusion();
- if ((incl != null) && (incl != JsonInclude.Include.USE_DEFAULTS)) {
- suppressableValue = incl;
- }
if (ser == null) {
ser = _valueSerializer;
}
// [databind#124]: May have a content converter
- ser = findConvertingContentSerializer(provider, property, ser);
+ ser = findContextualConvertingSerializer(provider, property, ser);
if (ser == null) {
// 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
// we can consider it a static case as well.
@@ -361,8 +418,6 @@
if (_valueTypeIsStatic && !_valueType.isJavaLangObject()) {
ser = provider.findValueSerializer(_valueType, property);
}
- } else {
- ser = provider.handleSecondaryContextualization(ser, property);
}
if (keySer == null) {
keySer = _keySerializer;
@@ -374,11 +429,11 @@
}
Set<String> ignored = _ignoredEntries;
boolean sortKeys = false;
- if ((intr != null) && (propertyAcc != null)) {
+ if (_neitherNull(propertyAcc, intr)) {
JsonIgnoreProperties.Value ignorals = intr.findPropertyIgnorals(propertyAcc);
if (ignorals != null){
Set<String> newIgnored = ignorals.findIgnoredForSerialization();
- if ((newIgnored != null) && !newIgnored.isEmpty()) {
+ if (_nonEmpty(newIgnored)) {
ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
for (String str : newIgnored) {
ignored.add(str);
@@ -386,7 +441,7 @@
}
}
Boolean b = intr.findSerializationSortAlphabetically(propertyAcc);
- sortKeys = (b != null) && b.booleanValue();
+ sortKeys = Boolean.TRUE.equals(b);
}
JsonFormat.Value format = findFormatOverrides(provider, property, Map.class);
if (format != null) {
@@ -396,9 +451,6 @@
}
}
MapSerializer mser = withResolved(property, keySer, ser, ignored, sortKeys);
- if (suppressableValue != _suppressableValue) {
- mser = mser.withContentInclusion(suppressableValue);
- }
// [databind#307]: allow filtering
if (property != null) {
@@ -409,10 +461,58 @@
mser = mser.withFilterId(filterId);
}
}
+ JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), null);
+ if (inclV != null) {
+ JsonInclude.Include incl = inclV.getContentInclusion();
+
+ if (incl != JsonInclude.Include.USE_DEFAULTS) {
+ Object valueToSuppress;
+ boolean suppressNulls;
+ switch (incl) {
+ case NON_DEFAULT:
+ valueToSuppress = BeanUtil.getDefaultValue(_valueType);
+ suppressNulls = true;
+ if (valueToSuppress != null) {
+ if (valueToSuppress.getClass().isArray()) {
+ valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
+ }
+ }
+ break;
+ case NON_ABSENT:
+ suppressNulls = true;
+ valueToSuppress = _valueType.isReferenceType() ? MARKER_FOR_EMPTY : null;
+ break;
+ case NON_EMPTY:
+ suppressNulls = true;
+ valueToSuppress = MARKER_FOR_EMPTY;
+ break;
+ case CUSTOM:
+ valueToSuppress = provider.includeFilterInstance(null, inclV.getContentFilter());
+ if (valueToSuppress == null) { // is this legal?
+ suppressNulls = true;
+ } else {
+ suppressNulls = provider.includeFilterSuppressNulls(valueToSuppress);
+ }
+ break;
+ case NON_NULL:
+ valueToSuppress = null;
+ suppressNulls = true;
+ break;
+ case ALWAYS: // default
+ default:
+ valueToSuppress = null;
+ // 30-Sep-2016, tatu: Should not need to check global flags here,
+ // if inclusion forced to be ALWAYS
+ suppressNulls = false;
+ break;
+ }
+ mser = mser.withContentInclusion(valueToSuppress, suppressNulls);
+ }
+ }
}
return mser;
}
-
+
/*
/**********************************************************
/* Accessors
@@ -432,45 +532,55 @@
@Override
public boolean isEmpty(SerializerProvider prov, Map<?,?> value)
{
- if (value == null || value.isEmpty()) {
+ if (value.isEmpty()) {
return true;
}
+
// 05-Nove-2015, tatu: Simple cases are cheap, but for recursive
// emptiness checking we actually need to see if values are empty as well.
Object supp = _suppressableValue;
-
- if ((supp == null) || (supp == JsonInclude.Include.ALWAYS)) {
+ if ((supp == null) && !_suppressNulls) {
return false;
}
JsonSerializer<Object> valueSer = _valueSerializer;
+ final boolean checkEmpty = (MARKER_FOR_EMPTY == supp);
if (valueSer != null) {
for (Object elemValue : value.values()) {
- if ((elemValue != null) && !valueSer.isEmpty(prov, elemValue)) {
+ if (elemValue == null) {
+ if (_suppressNulls) {
+ continue;
+ }
+ return false;
+ }
+ if (checkEmpty) {
+ if (!valueSer.isEmpty(prov, elemValue)) {
+ return false;
+ }
+ } else if ((supp == null) || !supp.equals(value)) {
return false;
}
}
return true;
}
// But if not statically known, try this:
- PropertySerializerMap serializers = _dynamicValueSerializers;
for (Object elemValue : value.values()) {
if (elemValue == null) {
- continue;
+ if (_suppressNulls) {
+ continue;
+ }
+ return false;
}
- Class<?> cc = elemValue.getClass();
- // 05-Nov-2015, tatu: Let's not worry about generic types here, actually;
- // unlikely to make any difference, but does add significant overhead
- valueSer = serializers.serializerFor(cc);
- if (valueSer == null) {
- try {
- valueSer = _findAndAddDynamic(serializers, cc, prov);
- } catch (JsonMappingException e) { // Ugh... can not just throw as-is, so...
- // 05-Nov-2015, tatu: For now, probably best not to assume empty then
+ try {
+ valueSer = _findSerializer(prov, elemValue);
+ } catch (JsonMappingException e) { // Ugh... can not just throw as-is, so...
+ // 05-Nov-2015, tatu: For now, probably best not to assume empty then
+ return false;
+ }
+ if (checkEmpty) {
+ if (!valueSer.isEmpty(prov, elemValue)) {
return false;
}
- serializers = _dynamicValueSerializers;
- }
- if (!valueSer.isEmpty(prov, elemValue)) {
+ } else if ((supp == null) || !supp.equals(value)) {
return false;
}
}
@@ -481,7 +591,7 @@
public boolean hasSingleElement(Map<?,?> value) {
return (value.size() == 1);
}
-
+
/*
/**********************************************************
/* Extended API
@@ -514,22 +624,14 @@
{
gen.writeStartObject(value);
if (!value.isEmpty()) {
- Object suppressableValue = _suppressableValue;
- if (suppressableValue == JsonInclude.Include.ALWAYS) {
- suppressableValue = null;
- } else if (suppressableValue == null) {
- if (!provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES)) {
- suppressableValue = JsonInclude.Include.NON_NULL;
- }
- }
if (_sortKeys || provider.isEnabled(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)) {
- value = _orderEntries(value, gen, provider, suppressableValue);
+ value = _orderEntries(value, gen, provider);
}
PropertyFilter pf;
if ((_filterId != null) && (pf = findPropertyFilter(provider, _filterId, value)) != null) {
- serializeFilteredFields(value, gen, provider, pf, suppressableValue);
- } else if (suppressableValue != null) {
- serializeOptionalFields(value, gen, provider, suppressableValue);
+ serializeFilteredFields(value, gen, provider, pf, _suppressableValue);
+ } else if ((_suppressableValue != null) || _suppressNulls) {
+ serializeOptionalFields(value, gen, provider, _suppressableValue);
} else if (_valueSerializer != null) {
serializeFieldsUsing(value, gen, provider, _valueSerializer);
} else {
@@ -548,22 +650,14 @@
// [databind#631]: Assign current value, to be accessible by custom serializers
gen.setCurrentValue(value);
if (!value.isEmpty()) {
- Object suppressableValue = _suppressableValue;
- if (suppressableValue == JsonInclude.Include.ALWAYS) {
- suppressableValue = null;
- } else if (suppressableValue == null) {
- if (!provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES)) {
- suppressableValue = JsonInclude.Include.NON_NULL;
- }
- }
if (_sortKeys || provider.isEnabled(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)) {
- value = _orderEntries(value, gen, provider, suppressableValue);
+ value = _orderEntries(value, gen, provider);
}
PropertyFilter pf;
if ((_filterId != null) && (pf = findPropertyFilter(provider, _filterId, value)) != null) {
- serializeFilteredFields(value, gen, provider, pf, suppressableValue);
- } else if (suppressableValue != null) {
- serializeOptionalFields(value, gen, provider, suppressableValue);
+ serializeFilteredFields(value, gen, provider, pf, _suppressableValue);
+ } else if ((_suppressableValue != null) || _suppressNulls) {
+ serializeOptionalFields(value, gen, provider, _suppressableValue);
} else if (_valueSerializer != null) {
serializeFieldsUsing(value, gen, provider, _valueSerializer);
} else {
@@ -594,48 +688,35 @@
}
final JsonSerializer<Object> keySerializer = _keySerializer;
final Set<String> ignored = _ignoredEntries;
+ Object keyElem = null;
- PropertySerializerMap serializers = _dynamicValueSerializers;
-
- for (Map.Entry<?,?> entry : value.entrySet()) {
- Object valueElem = entry.getValue();
- // First, serialize key
- Object keyElem = entry.getKey();
-
- if (keyElem == null) {
- provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider);
- } else {
- // One twist: is entry ignorable? If so, skip
- if ((ignored != null) && ignored.contains(keyElem)) continue;
- keySerializer.serialize(keyElem, gen, provider);
- }
-
- // And then value
- if (valueElem == null) {
- provider.defaultSerializeNull(gen);
- continue;
- }
- JsonSerializer<Object> serializer = _valueSerializer;
- if (serializer == null) {
- Class<?> cc = valueElem.getClass();
- serializer = serializers.serializerFor(cc);
- if (serializer == null) {
- if (_valueType.hasGenericTypes()) {
- serializer = _findAndAddDynamic(serializers,
- provider.constructSpecializedType(_valueType, cc), provider);
- } else {
- serializer = _findAndAddDynamic(serializers, cc, provider);
+ try {
+ for (Map.Entry<?,?> entry : value.entrySet()) {
+ Object valueElem = entry.getValue();
+ // First, serialize key
+ keyElem = entry.getKey();
+ if (keyElem == null) {
+ provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider);
+ } else {
+ // One twist: is entry ignorable? If so, skip
+ if ((ignored != null) && ignored.contains(keyElem)) {
+ continue;
}
- serializers = _dynamicValueSerializers;
+ keySerializer.serialize(keyElem, gen, provider);
}
- }
- try {
+ // And then value
+ if (valueElem == null) {
+ provider.defaultSerializeNull(gen);
+ continue;
+ }
+ JsonSerializer<Object> serializer = _valueSerializer;
+ if (serializer == null) {
+ serializer = _findSerializer(provider, valueElem);
+ }
serializer.serialize(valueElem, gen, provider);
- } catch (Exception e) {
- // Add reference information
- String keyDesc = ""+keyElem;
- wrapAndThrow(provider, e, value, keyDesc);
}
+ } catch (Exception e) { // Add reference information
+ wrapAndThrow(provider, e, value, String.valueOf(keyElem));
}
}
@@ -652,7 +733,7 @@
return;
}
final Set<String> ignored = _ignoredEntries;
- PropertySerializerMap serializers = _dynamicValueSerializers;
+ final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);
for (Map.Entry<?,?> entry : value.entrySet()) {
// First find key serializer
@@ -669,29 +750,24 @@
final Object valueElem = entry.getValue();
JsonSerializer<Object> valueSer;
if (valueElem == null) {
- if (suppressableValue != null) { // all suppressions include null-suppression
+ if (_suppressNulls) { // all suppressions include null-suppression
continue;
}
valueSer = provider.getDefaultNullValueSerializer();
} else {
valueSer = _valueSerializer;
if (valueSer == null) {
- Class<?> cc = valueElem.getClass();
- valueSer = serializers.serializerFor(cc);
- if (valueSer == null) {
- if (_valueType.hasGenericTypes()) {
- valueSer = _findAndAddDynamic(serializers,
- provider.constructSpecializedType(_valueType, cc), provider);
- } else {
- valueSer = _findAndAddDynamic(serializers, cc, provider);
- }
- serializers = _dynamicValueSerializers;
- }
+ valueSer = _findSerializer(provider, valueElem);
}
// also may need to skip non-empty values:
- if ((suppressableValue == JsonInclude.Include.NON_EMPTY)
- && valueSer.isEmpty(provider, valueElem)) {
- continue;
+ if (checkEmpty) {
+ if (valueSer.isEmpty(provider, valueElem)) {
+ continue;
+ }
+ } else if (suppressableValue != null) {
+ if (suppressableValue.equals(valueElem)) {
+ continue;
+ }
}
}
// and then serialize, if all went well
@@ -699,8 +775,7 @@
keySerializer.serialize(keyElem, gen, provider);
valueSer.serialize(valueElem, gen, provider);
} catch (Exception e) {
- String keyDesc = ""+keyElem;
- wrapAndThrow(provider, e, value, keyDesc);
+ wrapAndThrow(provider, e, value, String.valueOf(keyElem));
}
}
}
@@ -738,8 +813,7 @@
ser.serializeWithType(valueElem, gen, provider, typeSer);
}
} catch (Exception e) {
- String keyDesc = ""+keyElem;
- wrapAndThrow(provider, e, value, keyDesc);
+ wrapAndThrow(provider, e, value, String.valueOf(keyElem));
}
}
}
@@ -757,10 +831,9 @@
throws IOException
{
final Set<String> ignored = _ignoredEntries;
-
- PropertySerializerMap serializers = _dynamicValueSerializers;
final MapProperty prop = new MapProperty(_valueTypeSerializer, _property);
-
+ final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);
+
for (Map.Entry<?,?> entry : value.entrySet()) {
// First, serialize key; unless ignorable by key
final Object keyElem = entry.getKey();
@@ -778,29 +851,24 @@
JsonSerializer<Object> valueSer;
// And then value
if (valueElem == null) {
- if (suppressableValue != null) { // all suppressions include null-suppression
+ if (_suppressNulls) {
continue;
}
valueSer = provider.getDefaultNullValueSerializer();
} else {
valueSer = _valueSerializer;
if (valueSer == null) {
- Class<?> cc = valueElem.getClass();
- valueSer = serializers.serializerFor(cc);
- if (valueSer == null) {
- if (_valueType.hasGenericTypes()) {
- valueSer = _findAndAddDynamic(serializers,
- provider.constructSpecializedType(_valueType, cc), provider);
- } else {
- valueSer = _findAndAddDynamic(serializers, cc, provider);
- }
- serializers = _dynamicValueSerializers;
- }
+ valueSer = _findSerializer(provider, valueElem);
}
// also may need to skip non-empty values:
- if ((suppressableValue == JsonInclude.Include.NON_EMPTY)
- && valueSer.isEmpty(provider, valueElem)) {
- continue;
+ if (checkEmpty) {
+ if (valueSer.isEmpty(provider, valueElem)) {
+ continue;
+ }
+ } else if (suppressableValue != null) {
+ if (suppressableValue.equals(valueElem)) {
+ continue;
+ }
}
}
// and with that, ask filter to handle it
@@ -808,19 +876,11 @@
try {
filter.serializeAsField(valueElem, gen, provider, prop);
} catch (Exception e) {
- String keyDesc = ""+keyElem;
- wrapAndThrow(provider, e, value, keyDesc);
+ wrapAndThrow(provider, e, value, String.valueOf(keyElem));
}
}
}
- @Deprecated // since 2.5
- public void serializeFilteredFields(Map<?,?> value, JsonGenerator gen, SerializerProvider provider,
- PropertyFilter filter) throws IOException {
- serializeFilteredFields(value, gen, provider, filter,
- provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES) ? null : JsonInclude.Include.NON_NULL);
- }
-
/**
* @since 2.5
*/
@@ -829,7 +889,7 @@
throws IOException
{
final Set<String> ignored = _ignoredEntries;
- PropertySerializerMap serializers = _dynamicValueSerializers;
+ final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);
for (Map.Entry<?,?> entry : value.entrySet()) {
Object keyElem = entry.getKey();
@@ -846,46 +906,35 @@
// And then value
JsonSerializer<Object> valueSer;
if (valueElem == null) {
- if (suppressableValue != null) { // all suppression include null suppression
+ if (_suppressNulls) { // all suppression include null suppression
continue;
}
valueSer = provider.getDefaultNullValueSerializer();
} else {
valueSer = _valueSerializer;
- Class<?> cc = valueElem.getClass();
- valueSer = serializers.serializerFor(cc);
if (valueSer == null) {
- if (_valueType.hasGenericTypes()) {
- valueSer = _findAndAddDynamic(serializers,
- provider.constructSpecializedType(_valueType, cc), provider);
- } else {
- valueSer = _findAndAddDynamic(serializers, cc, provider);
- }
- serializers = _dynamicValueSerializers;
+ valueSer = _findSerializer(provider, valueElem);
}
// also may need to skip non-empty values:
- if ((suppressableValue == JsonInclude.Include.NON_EMPTY)
- && valueSer.isEmpty(provider, valueElem)) {
- continue;
+ if (checkEmpty) {
+ if (valueSer.isEmpty(provider, valueElem)) {
+ continue;
+ }
+ } else if (suppressableValue != null) {
+ if (suppressableValue.equals(valueElem)) {
+ continue;
+ }
}
}
keySerializer.serialize(keyElem, gen, provider);
try {
valueSer.serializeWithType(valueElem, gen, provider, _valueTypeSerializer);
} catch (Exception e) {
- String keyDesc = ""+keyElem;
- wrapAndThrow(provider, e, value, keyDesc);
+ wrapAndThrow(provider, e, value, String.valueOf(keyElem));
}
}
}
- @Deprecated // since 2.5
- protected void serializeTypedFields(Map<?,?> value, JsonGenerator gen, SerializerProvider provider)
- throws IOException {
- serializeTypedFields(value, gen, provider,
- provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES) ? null : JsonInclude.Include.NON_NULL);
- }
-
/*
/**********************************************************
/* Schema related functionality
@@ -904,7 +953,7 @@
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException
{
- JsonMapFormatVisitor v2 = (visitor == null) ? null : visitor.expectMapFormat(typeHint);
+ JsonMapFormatVisitor v2 = visitor.expectMapFormat(typeHint);
if (v2 != null) {
v2.keyFormat(_keySerializer, _keyType);
JsonSerializer<?> valueSer = _valueSerializer;
@@ -921,7 +970,7 @@
/* Internal helper methods
/**********************************************************
*/
-
+
protected final JsonSerializer<Object> _findAndAddDynamic(PropertySerializerMap map,
Class<?> type, SerializerProvider provider) throws JsonMappingException
{
@@ -944,7 +993,7 @@
}
protected Map<?,?> _orderEntries(Map<?,?> input, JsonGenerator gen,
- SerializerProvider provider, Object suppressableValue) throws IOException
+ SerializerProvider provider) throws IOException
{
// minor optimization: may already be sorted?
if (input instanceof SortedMap<?,?>) {
@@ -959,7 +1008,7 @@
for (Map.Entry<?,?> entry : input.entrySet()) {
Object key = entry.getKey();
if (key == null) {
- _writeNullKeyedEntry(gen, provider, suppressableValue, entry.getValue());
+ _writeNullKeyedEntry(gen, provider, entry.getValue());
continue;
}
result.put(key, entry.getValue());
@@ -986,42 +1035,50 @@
}
protected void _writeNullKeyedEntry(JsonGenerator gen, SerializerProvider provider,
- Object suppressableValue, Object value) throws IOException
+ Object value) throws IOException
{
JsonSerializer<Object> keySerializer = provider.findNullKeySerializer(_keyType, _property);
JsonSerializer<Object> valueSer;
if (value == null) {
- if (suppressableValue != null) { // all suppressions include null-suppression
+ if (_suppressNulls) {
return;
}
valueSer = provider.getDefaultNullValueSerializer();
} else {
valueSer = _valueSerializer;
if (valueSer == null) {
- Class<?> cc = value.getClass();
- valueSer = _dynamicValueSerializers.serializerFor(cc);
- if (valueSer == null) {
- if (_valueType.hasGenericTypes()) {
- valueSer = _findAndAddDynamic(_dynamicValueSerializers,
- provider.constructSpecializedType(_valueType, cc), provider);
- } else {
- valueSer = _findAndAddDynamic(_dynamicValueSerializers, cc, provider);
- }
- }
+ valueSer = _findSerializer(provider, value);
}
- // also may need to skip non-empty values:
- if ((suppressableValue == JsonInclude.Include.NON_EMPTY)
- && valueSer.isEmpty(provider, value)) {
+ if (_suppressableValue == MARKER_FOR_EMPTY) {
+ if (valueSer.isEmpty(provider, value)) {
+ return;
+ }
+ } else if ((_suppressableValue != null)
+ && (_suppressableValue.equals(value))) {
return;
}
}
- // and then serialize, if all went well
+
try {
keySerializer.serialize(null, gen, provider);
valueSer.serialize(value, gen, provider);
} catch (Exception e) {
- String keyDesc = "";
- wrapAndThrow(provider, e, value, keyDesc);
+ wrapAndThrow(provider, e, value, "");
}
}
+
+ private final JsonSerializer<Object> _findSerializer(SerializerProvider provider,
+ Object value) throws JsonMappingException
+ {
+ final Class<?> cc = value.getClass();
+ JsonSerializer<Object> valueSer = _dynamicValueSerializers.serializerFor(cc);
+ if (valueSer != null) {
+ return valueSer;
+ }
+ if (_valueType.hasGenericTypes()) {
+ return _findAndAddDynamic(_dynamicValueSerializers,
+ provider.constructSpecializedType(_valueType, cc), provider);
+ }
+ return _findAndAddDynamic(_dynamicValueSerializers, cc, provider);
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/NonTypedScalarSerializerBase.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/NonTypedScalarSerializerBase.java
index a0f75a3..04b1cd5 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/NonTypedScalarSerializerBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/NonTypedScalarSerializerBase.java
@@ -14,6 +14,7 @@
* {@link java.lang.Double} and {@link java.lang.Boolean}.
*/
@SuppressWarnings("serial")
+@Deprecated // since 2.9
public abstract class NonTypedScalarSerializerBase<T>
extends StdScalarSerializer<T>
{
@@ -24,7 +25,7 @@
protected NonTypedScalarSerializerBase(Class<?> t, boolean bogus) {
super(t, bogus);
}
-
+
@Override
public final void serializeWithType(T value, JsonGenerator gen, SerializerProvider provider,
TypeSerializer typeSer) throws IOException
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/ObjectArraySerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/ObjectArraySerializer.java
index d71bc40..3030dcc 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/ObjectArraySerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/ObjectArraySerializer.java
@@ -1,23 +1,20 @@
package com.fasterxml.jackson.databind.ser.std;
import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Type;
import com.fasterxml.jackson.annotation.JsonFormat;
+
import com.fasterxml.jackson.core.*;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
-import com.fasterxml.jackson.databind.jsonschema.SchemaAware;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
-import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.ContainerSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap;
-import com.fasterxml.jackson.databind.type.ArrayType;
import com.fasterxml.jackson.databind.type.TypeFactory;
/**
@@ -153,7 +150,7 @@
ser = _elementSerializer;
}
// [databind#124]: May have a content converter
- ser = findConvertingContentSerializer(serializers, property, ser);
+ ser = findContextualConvertingSerializer(serializers, property, ser);
if (ser == null) {
// 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
// we can consider it a static case as well.
@@ -162,8 +159,6 @@
ser = serializers.findValueSerializer(_elementType, property);
}
}
- } else {
- ser = serializers.handleSecondaryContextualization(ser, property);
}
return withResolved(property, vts, ser, unwrapSingle);
}
@@ -186,7 +181,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, Object[] value) {
- return (value == null) || (value.length == 0);
+ return value.length == 0;
}
@Override
@@ -245,7 +240,6 @@
Class<?> cc = elem.getClass();
JsonSerializer<Object> serializer = serializers.serializerFor(cc);
if (serializer == null) {
- // To fix [JACKSON-508]
if (_elementType.hasGenericTypes()) {
serializer = _findAndAddDynamic(serializers,
provider.constructSpecializedType(_elementType, cc), provider);
@@ -255,22 +249,8 @@
}
serializer.serialize(elem, gen, provider);
}
- } catch (IOException ioe) {
- throw ioe;
} catch (Exception e) {
- // [JACKSON-55] Need to add reference information
- /* 05-Mar-2009, tatu: But one nasty edge is when we get
- * StackOverflow: usually due to infinite loop. But that gets
- * hidden within an InvocationTargetException...
- */
- Throwable t = e;
- while (t instanceof InvocationTargetException && t.getCause() != null) {
- t = t.getCause();
- }
- if (t instanceof Error) {
- throw (Error) t;
- }
- throw JsonMappingException.wrapWithPath(t, elem, i);
+ wrapAndThrow(provider, e, elem, i);
}
}
@@ -295,17 +275,8 @@
ser.serializeWithType(elem, jgen, provider, typeSer);
}
}
- } catch (IOException ioe) {
- throw ioe;
} catch (Exception e) {
- Throwable t = e;
- while (t instanceof InvocationTargetException && t.getCause() != null) {
- t = t.getCause();
- }
- if (t instanceof Error) {
- throw (Error) t;
- }
- throw JsonMappingException.wrapWithPath(t, elem, i);
+ wrapAndThrow(provider, e, elem, i);
}
}
@@ -330,44 +301,10 @@
}
serializer.serializeWithType(elem, jgen, provider, typeSer);
}
- } catch (IOException ioe) {
- throw ioe;
} catch (Exception e) {
- Throwable t = e;
- while (t instanceof InvocationTargetException && t.getCause() != null) {
- t = t.getCause();
- }
- if (t instanceof Error) {
- throw (Error) t;
- }
- throw JsonMappingException.wrapWithPath(t, elem, i);
+ wrapAndThrow(provider, e, elem, i);
}
}
-
- @SuppressWarnings("deprecation")
- @Override
- public JsonNode getSchema(SerializerProvider provider, Type typeHint)
- throws JsonMappingException
- {
- ObjectNode o = createSchemaNode("array", true);
- if (typeHint != null) {
- JavaType javaType = provider.constructType(typeHint);
- if (javaType.isArrayType()) {
- Class<?> componentType = ((ArrayType) javaType).getContentType().getRawClass();
- // 15-Oct-2010, tatu: We can't serialize plain Object.class; but what should it produce here? Untyped?
- if (componentType == Object.class) {
- o.set("items", com.fasterxml.jackson.databind.jsonschema.JsonSchema.getDefaultSchemaNode());
- } else {
- JsonSerializer<Object> ser = provider.findValueSerializer(componentType, _property);
- JsonNode schemaNode = (ser instanceof SchemaAware) ?
- ((SchemaAware) ser).getSchema(provider, null) :
- com.fasterxml.jackson.databind.jsonschema.JsonSchema.getDefaultSchemaNode();
- o.set("items", schemaNode);
- }
- }
- }
- return o;
- }
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
@@ -378,7 +315,8 @@
TypeFactory tf = visitor.getProvider().getTypeFactory();
JavaType contentType = tf.moreSpecificType(_elementType, typeHint.getContentType());
if (contentType == null) {
- throw JsonMappingException.from(visitor.getProvider(), "Could not resolve type");
+ visitor.getProvider().reportBadDefinition(_elementType,
+ "Could not resolve type: "+_elementType);
}
JsonSerializer<?> valueSer = _elementSerializer;
if (valueSer == null) {
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/ReferenceTypeSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/ReferenceTypeSerializer.java
index 095d209..8871ac1 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/ReferenceTypeSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/ReferenceTypeSerializer.java
@@ -12,6 +12,8 @@
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap;
import com.fasterxml.jackson.databind.type.ReferenceType;
+import com.fasterxml.jackson.databind.util.ArrayBuilders;
+import com.fasterxml.jackson.databind.util.BeanUtil;
import com.fasterxml.jackson.databind.util.NameTransformer;
/**
@@ -28,6 +30,11 @@
private static final long serialVersionUID = 1L;
/**
+ * @since 2.9
+ */
+ public final static Object MARKER_FOR_EMPTY = JsonInclude.Include.NON_EMPTY;
+
+ /**
* Value type
*/
protected final JavaType _referredType;
@@ -50,12 +57,6 @@
protected final NameTransformer _unwrapper;
/**
- * Further guidance on serialization-inclusion (or not), regarding
- * contained value (if any).
- */
- protected final JsonInclude.Include _contentInclusion;
-
- /**
* If element type can not be statically determined, mapping from
* runtime type to serializer is handled using this object
*/
@@ -63,6 +64,32 @@
/*
/**********************************************************
+ /* Config settings, filtering
+ /**********************************************************
+ */
+
+ /**
+ * Value that indicates suppression mechanism to use for <b>values contained</b>;
+ * either "filter" (of which <code>equals()</code> is called), or marker
+ * value of {@link #MARKER_FOR_EMPTY}, or null to indicate no filtering for
+ * non-null values.
+ * Note that inclusion value for Map instance itself is handled by caller (POJO
+ * property that refers to the Map value).
+ *
+ * @since 2.9
+ */
+ protected final Object _suppressableValue;
+
+ /**
+ * Flag that indicates what to do with `null` values, distinct from
+ * handling of {@link #_suppressableValue}
+ *
+ * @since 2.9
+ */
+ protected final boolean _suppressNulls;
+
+ /*
+ /**********************************************************
/* Constructors, factory methods
/**********************************************************
*/
@@ -76,7 +103,8 @@
_valueTypeSerializer = vts;
_valueSerializer = ser;
_unwrapper = null;
- _contentInclusion = null;
+ _suppressableValue = null;
+ _suppressNulls = false;
_dynamicSerializers = PropertySerializerMap.emptyForProperties();
}
@@ -84,7 +112,7 @@
protected ReferenceTypeSerializer(ReferenceTypeSerializer<?> base, BeanProperty property,
TypeSerializer vts, JsonSerializer<?> valueSer,
NameTransformer unwrapper,
- JsonInclude.Include contentIncl)
+ Object suppressableValue, boolean suppressNulls)
{
super(base);
_referredType = base._referredType;
@@ -93,23 +121,22 @@
_valueTypeSerializer = vts;
_valueSerializer = (JsonSerializer<Object>) valueSer;
_unwrapper = unwrapper;
- if ((contentIncl == JsonInclude.Include.USE_DEFAULTS)
- || (contentIncl == JsonInclude.Include.ALWAYS)) {
- _contentInclusion = null;
- } else {
- _contentInclusion = contentIncl;
- }
+ _suppressableValue = suppressableValue;
+ _suppressNulls = suppressNulls;
}
@Override
public JsonSerializer<T> unwrappingSerializer(NameTransformer transformer) {
- JsonSerializer<Object> ser = _valueSerializer;
- if (ser != null) {
- ser = ser.unwrappingSerializer(transformer);
+ JsonSerializer<Object> valueSer = _valueSerializer;
+ if (valueSer != null) {
+ valueSer = valueSer.unwrappingSerializer(transformer);
}
NameTransformer unwrapper = (_unwrapper == null) ? transformer
: NameTransformer.chainedTransformer(transformer, _unwrapper);
- return withResolved(_property, _valueTypeSerializer, ser, unwrapper, _contentInclusion);
+ if ((_valueSerializer == valueSer) && (_unwrapper == unwrapper)) {
+ return this;
+ }
+ return withResolved(_property, _valueTypeSerializer, valueSer, unwrapper);
}
/*
@@ -117,13 +144,39 @@
/* Abstract methods to implement
/**********************************************************
*/
-
+
+ /**
+ * Mutant factory method called when changes are needed; should construct
+ * newly configured instance with new values as indicated.
+ *<p>
+ * NOTE: caller has verified that there are changes, so implementations
+ * need NOT check if a new instance is needed.
+ *
+ * @since 2.9
+ */
protected abstract ReferenceTypeSerializer<T> withResolved(BeanProperty prop,
TypeSerializer vts, JsonSerializer<?> valueSer,
- NameTransformer unwrapper,
- JsonInclude.Include contentIncl);
+ NameTransformer unwrapper);
- protected abstract boolean _isValueEmpty(T value);
+ /**
+ * Mutant factory method called to create a differently constructed instance,
+ * specifically with different exclusion rules for contained value.
+ *<p>
+ * NOTE: caller has verified that there are changes, so implementations
+ * need NOT check if a new instance is needed.
+ *
+ * @since 2.9
+ */
+ public abstract ReferenceTypeSerializer<T> withContentInclusion(Object suppressableValue,
+ boolean suppressNulls);
+
+ /**
+ * Method called to see if there is a value present or not.
+ * Note that value itself may still be `null`, even if present,
+ * if referential type allows three states (absent, present-null,
+ * present-non-null); some only allow two (absent, present-non-null).
+ */
+ protected abstract boolean _isValuePresent(T value);
protected abstract Object _getReferenced(T value);
@@ -144,7 +197,7 @@
typeSer = typeSer.forProperty(property);
}
// First: do we have an annotation override from property?
- JsonSerializer<?> ser = findAnnotatedContentSerializer(provider, property);;
+ JsonSerializer<?> ser = findAnnotatedContentSerializer(provider, property);
if (ser == null) {
// If not, use whatever was configured by type
ser = _valueSerializer;
@@ -157,14 +210,68 @@
ser = provider.handlePrimaryContextualization(ser, property);
}
}
- // Also: may want to have more refined exclusion based on referenced value
- JsonInclude.Include contentIncl = _contentInclusion;
- JsonInclude.Value incl = findIncludeOverrides(provider, property, handledType());
- JsonInclude.Include newIncl = incl.getContentInclusion();
- if ((newIncl != contentIncl) && (newIncl != JsonInclude.Include.USE_DEFAULTS)) {
- contentIncl = newIncl;
+ // First, resolve wrt property, resolved serializers
+ ReferenceTypeSerializer<?> refSer;
+ if ((_property == property)
+ && (_valueTypeSerializer == typeSer) && (_valueSerializer == ser)) {
+ refSer = this;
+ } else {
+ refSer = withResolved(property, typeSer, ser, _unwrapper);
}
- return withResolved(property, typeSer, ser, _unwrapper, contentIncl);
+
+ // and then see if we have property-inclusion overrides
+ if (property != null) {
+ JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), handledType());
+ if (inclV != null) {
+ JsonInclude.Include incl = inclV.getContentInclusion();
+
+ if (incl != JsonInclude.Include.USE_DEFAULTS) {
+ Object valueToSuppress;
+ boolean suppressNulls;
+ switch (incl) {
+ case NON_DEFAULT:
+ valueToSuppress = BeanUtil.getDefaultValue(_referredType);
+ suppressNulls = true;
+ if (valueToSuppress != null) {
+ if (valueToSuppress.getClass().isArray()) {
+ valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
+ }
+ }
+ break;
+ case NON_ABSENT:
+ suppressNulls = true;
+ valueToSuppress = _referredType.isReferenceType() ? MARKER_FOR_EMPTY : null;
+ break;
+ case NON_EMPTY:
+ suppressNulls = true;
+ valueToSuppress = MARKER_FOR_EMPTY;
+ break;
+ case CUSTOM:
+ valueToSuppress = provider.includeFilterInstance(null, inclV.getContentFilter());
+ if (valueToSuppress == null) { // is this legal?
+ suppressNulls = true;
+ } else {
+ suppressNulls = provider.includeFilterSuppressNulls(valueToSuppress);
+ }
+ break;
+ case NON_NULL:
+ valueToSuppress = null;
+ suppressNulls = true;
+ break;
+ case ALWAYS: // default
+ default:
+ valueToSuppress = null;
+ suppressNulls = false;
+ break;
+ }
+ if ((_suppressableValue != valueToSuppress)
+ || (_suppressNulls != suppressNulls)) {
+ refSer = refSer.withContentInclusion(valueToSuppress, suppressNulls);
+ }
+ }
+ }
+ }
+ return refSer;
}
protected boolean _useStatic(SerializerProvider provider, BeanProperty property,
@@ -209,13 +316,17 @@
@Override
public boolean isEmpty(SerializerProvider provider, T value)
{
- if ((value == null) || _isValueEmpty(value)) {
+ // First, absent value (note: null check is just sanity check here)
+ if (!_isValuePresent(value)) {
return true;
}
- if (_contentInclusion == null) {
+ Object contents = _getReferenced(value);
+ if (contents == null) { // possible for explicitly contained `null`
+ return _suppressNulls;
+ }
+ if (_suppressableValue == null) {
return false;
}
- Object contents = _getReferenced(value);
JsonSerializer<Object> ser = _valueSerializer;
if (ser == null) {
try {
@@ -224,7 +335,10 @@
throw new RuntimeJsonMappingException(e);
}
}
- return ser.isEmpty(provider, contents);
+ if (_suppressableValue == MARKER_FOR_EMPTY) {
+ return ser.isEmpty(provider, contents);
+ }
+ return _suppressableValue.equals(contents);
}
@Override
@@ -232,6 +346,13 @@
return (_unwrapper != null);
}
+ /**
+ * @since 2.9
+ */
+ public JavaType getReferredType() {
+ return _referredType;
+ }
+
/*
/**********************************************************
/* Serialization methods
@@ -339,7 +460,7 @@
{
// 13-Mar-2017, tatu: Used to call `findTypeValueSerializer()`, but contextualization
// not working for that case for some reason
- // return provider.findTypedValueSerializer(type, true, prop);
+// return provider.findTypedValueSerializer(type, true, prop);
return provider.findValueSerializer(type, prop);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/SerializableSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/SerializableSerializer.java
index 793a49c..f2659e5 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/SerializableSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/SerializableSerializer.java
@@ -1,22 +1,15 @@
package com.fasterxml.jackson.databind.ser.std;
import java.io.IOException;
-import java.lang.reflect.Type;
-import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
-import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializable;
-import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
-import com.fasterxml.jackson.databind.jsonschema.JsonSerializableSchema;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.fasterxml.jackson.databind.type.TypeFactory;
/**
* Generic handler for types that implement {@link JsonSerializable}.
@@ -31,9 +24,6 @@
{
public final static SerializableSerializer instance = new SerializableSerializer();
- // Ugh. Should NOT need this...
- private final static AtomicReference<ObjectMapper> _mapperReference = new AtomicReference<ObjectMapper>();
-
protected SerializableSerializer() { super(JsonSerializable.class); }
@Override
@@ -54,61 +44,6 @@
TypeSerializer typeSer) throws IOException {
value.serializeWithType(gen, serializers, typeSer);
}
-
- @Override
- @SuppressWarnings("deprecation")
- public JsonNode getSchema(SerializerProvider provider, Type typeHint)
- throws JsonMappingException
- {
- ObjectNode objectNode = createObjectNode();
- String schemaType = "any";
- String objectProperties = null;
- String itemDefinition = null;
- if (typeHint != null) {
- Class<?> rawClass = TypeFactory.rawClass(typeHint);
- if (rawClass.isAnnotationPresent(JsonSerializableSchema.class)) {
- JsonSerializableSchema schemaInfo = rawClass.getAnnotation(JsonSerializableSchema.class);
- schemaType = schemaInfo.schemaType();
- if (!JsonSerializableSchema.NO_VALUE.equals(schemaInfo.schemaObjectPropertiesDefinition())) {
- objectProperties = schemaInfo.schemaObjectPropertiesDefinition();
- }
- if (!JsonSerializableSchema.NO_VALUE.equals(schemaInfo.schemaItemDefinition())) {
- itemDefinition = schemaInfo.schemaItemDefinition();
- }
- }
- }
- /* 19-Mar-2012, tatu: geez, this is butt-ugly abonimation of code...
- * really, really should not require back ref to an ObjectMapper.
- */
- objectNode.put("type", schemaType);
- if (objectProperties != null) {
- try {
- objectNode.set("properties", _getObjectMapper().readTree(objectProperties));
- } catch (IOException e) {
- provider.reportMappingProblem("Failed to parse @JsonSerializableSchema.schemaObjectPropertiesDefinition value");
- }
- }
- if (itemDefinition != null) {
- try {
- objectNode.set("items", _getObjectMapper().readTree(itemDefinition));
- } catch (IOException e) {
- provider.reportMappingProblem("Failed to parse @JsonSerializableSchema.schemaItemDefinition value");
- }
- }
- // always optional, no need to specify:
- //objectNode.put("required", false);
- return objectNode;
- }
-
- private final static synchronized ObjectMapper _getObjectMapper()
- {
- ObjectMapper mapper = _mapperReference.get();
- if (mapper == null) {
- mapper = new ObjectMapper();
- _mapperReference.set(mapper);
- }
- return mapper;
- }
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/SqlDateSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/SqlDateSerializer.java
index 2857b6f..c14d14d 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/SqlDateSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/SqlDateSerializer.java
@@ -4,8 +4,8 @@
import java.lang.reflect.Type;
import java.text.DateFormat;
-import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
@@ -21,19 +21,17 @@
extends DateTimeSerializerBase<java.sql.Date>
{
public SqlDateSerializer() {
- /* 12-Apr-2014, tatu: for now, pass explicit 'false' to mean 'not using timestamp',
- * for backwards compatibility; this differs from other Date/Calendar types.
- */
- this(Boolean.FALSE);
+ // 11-Oct-2016, tatu: As per [databind#219] fixed for 2.9; was passing `false` prior
+ this(null, null);
}
- protected SqlDateSerializer(Boolean useTimestamp) {
- super(java.sql.Date.class, useTimestamp, null);
+ protected SqlDateSerializer(Boolean useTimestamp, DateFormat customFormat) {
+ super(java.sql.Date.class, useTimestamp, customFormat);
}
@Override
public SqlDateSerializer withFormat(Boolean timestamp, DateFormat customFormat) {
- return new SqlDateSerializer(timestamp);
+ return new SqlDateSerializer(timestamp, customFormat);
}
@Override
@@ -43,11 +41,19 @@
@Override
public void serialize(java.sql.Date value, JsonGenerator gen, SerializerProvider provider)
- throws IOException, JsonGenerationException
+ throws IOException
{
if (_asTimestamp(provider)) {
gen.writeNumber(_timestamp(value));
+ } else if (_customFormat != null) {
+ // 11-Oct-2016, tatu: As per [databind#219], same as with `java.util.Date`
+ synchronized (_customFormat) {
+ gen.writeString(_customFormat.format(value));
+ }
} else {
+ // 11-Oct-2016, tatu: For backwards-compatibility purposes, we shall just use
+ // the awful standard JDK serialization via `sqlDate.toString()`... this
+ // is problematic in multiple ways (including using arbitrary timezone...)
gen.writeString(value.toString());
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StaticListSerializerBase.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StaticListSerializerBase.java
index 759c46d..84ac2ba 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StaticListSerializerBase.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StaticListSerializerBase.java
@@ -1,13 +1,16 @@
package com.fasterxml.jackson.databind.ser.std;
+import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
+import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
/**
@@ -19,8 +22,6 @@
extends StdSerializer<T>
implements ContextualSerializer
{
- protected final JsonSerializer<String> _serializer;
-
/**
* Setting for specific local override for "unwrap single element arrays":
* true for enable unwrapping, false for preventing it, `null` for using
@@ -29,29 +30,26 @@
* @since 2.6
*/
protected final Boolean _unwrapSingle;
-
+
protected StaticListSerializerBase(Class<?> cls) {
super(cls, false);
- _serializer = null;
_unwrapSingle = null;
}
/**
- * @since 2.6
+ * @since 2.9
*/
- @SuppressWarnings("unchecked")
protected StaticListSerializerBase(StaticListSerializerBase<?> src,
- JsonSerializer<?> ser, Boolean unwrapSingle) {
+ Boolean unwrapSingle) {
super(src);
- _serializer = (JsonSerializer<String>) ser;
_unwrapSingle = unwrapSingle;
}
/**
- * @since 2.6
+ * @since 2.9
*/
public abstract JsonSerializer<?> _withResolved(BeanProperty prop,
- JsonSerializer<?> ser, Boolean unwrapSingle);
+ Boolean unwrapSingle);
/*
/**********************************************************
@@ -59,6 +57,7 @@
/**********************************************************
*/
+ @SuppressWarnings("unchecked")
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property)
@@ -81,31 +80,22 @@
if (format != null) {
unwrapSingle = format.getFeature(JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
}
- if (ser == null) {
- ser = _serializer;
- }
// [databind#124]: May have a content converter
- ser = findConvertingContentSerializer(serializers, property, ser);
+ ser = findContextualConvertingSerializer(serializers, property, ser);
if (ser == null) {
ser = serializers.findValueSerializer(String.class, property);
- } else {
- ser = serializers.handleSecondaryContextualization(ser, property);
}
// Optimization: default serializer just writes String, so we can avoid a call:
if (isDefaultSerializer(ser)) {
- ser = null;
+ if (unwrapSingle == _unwrapSingle) {
+ return this;
+ }
+ return _withResolved(property, unwrapSingle);
}
+ // otherwise...
// note: will never have TypeSerializer, because Strings are "natural" type
- if ((ser == _serializer) && (unwrapSingle == _unwrapSingle)) {
- return this;
- }
- return _withResolved(property, ser, unwrapSingle);
- }
-
- @Deprecated // since 2.5
- @Override
- public boolean isEmpty(T value) {
- return isEmpty(null, value);
+ return new CollectionSerializer(serializers.constructType(String.class),
+ true, /*TypeSerializer*/ null, (JsonSerializer<Object>) ser);
}
@Override
@@ -117,7 +107,7 @@
public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
return createSchemaNode("array", true).set("items", contentSchema());
}
-
+
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {
acceptContentVisitor(visitor.expectArrayFormat(typeHint));
@@ -130,7 +120,12 @@
*/
protected abstract JsonNode contentSchema();
-
+
protected abstract void acceptContentVisitor(JsonArrayFormatVisitor visitor)
throws JsonMappingException;
+
+ // just to make sure it gets implemented:
+ @Override
+ public abstract void serializeWithType(T value, JsonGenerator g,
+ SerializerProvider provider, TypeSerializer typeSer) throws IOException;
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdArraySerializers.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdArraySerializers.java
index 16c7e96..0663542 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdArraySerializers.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdArraySerializers.java
@@ -58,23 +58,24 @@
protected abstract static class TypedPrimitiveArraySerializer<T>
extends ArraySerializerBase<T>
{
- /**
- * Type serializer to use for values, if any.
- */
- protected final TypeSerializer _valueTypeSerializer;
-
protected TypedPrimitiveArraySerializer(Class<T> cls) {
super(cls);
- _valueTypeSerializer = null;
}
protected TypedPrimitiveArraySerializer(TypedPrimitiveArraySerializer<T> src,
- BeanProperty prop, TypeSerializer vts, Boolean unwrapSingle) {
+ BeanProperty prop, Boolean unwrapSingle) {
super(src, prop, unwrapSingle);
- _valueTypeSerializer = vts;
+ }
+
+ // 01-Dec-2016, tatu: Only now realized that due strong typing of Java arrays,
+ // we can not really ever have value type serializers
+ @Override
+ public final ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts) {
+ // throw exception or just do nothing?
+ return this;
}
}
-
+
/*
/****************************************************************
/* Concrete serializers, arrays
@@ -123,7 +124,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, boolean[] value) {
- return (value == null) || (value.length == 0);
+ return value.length == 0;
}
@Override
@@ -135,22 +136,19 @@
public final void serialize(boolean[] value, JsonGenerator g, SerializerProvider provider) throws IOException
{
final int len = value.length;
- if (len == 1) {
- if (((_unwrapSingle == null) &&
- provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
- || (_unwrapSingle == Boolean.TRUE)) {
- serializeContents(value, g, provider);
- return;
- }
+ if ((len == 1) && _shouldUnwrapSingle(provider)) {
+ serializeContents(value, g, provider);
+ return;
}
g.writeStartArray(len);
+ g.setCurrentValue(value);
serializeContents(value, g, provider);
g.writeEndArray();
}
-
+
@Override
public void serializeContents(boolean[] value, JsonGenerator g, SerializerProvider provider)
- throws IOException, JsonGenerationException
+ throws IOException
{
for (int i = 0, len = value.length; i < len; ++i) {
g.writeBoolean(value[i]);
@@ -182,18 +180,13 @@
public ShortArraySerializer() { super(short[].class); }
public ShortArraySerializer(ShortArraySerializer src, BeanProperty prop,
- TypeSerializer vts, Boolean unwrapSingle) {
- super(src, prop, vts, unwrapSingle);
+ Boolean unwrapSingle) {
+ super(src, prop, unwrapSingle);
}
@Override
public JsonSerializer<?> _withResolved(BeanProperty prop,Boolean unwrapSingle) {
- return new ShortArraySerializer(this, prop, _valueTypeSerializer, unwrapSingle);
- }
-
- @Override
- public ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts) {
- return new ShortArraySerializer(this, _property, vts, _unwrapSingle);
+ return new ShortArraySerializer(this, prop, unwrapSingle);
}
@Override
@@ -209,7 +202,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, short[] value) {
- return (value == null) || (value.length == 0);
+ return value.length == 0;
}
@Override
@@ -220,16 +213,13 @@
@Override
public final void serialize(short[] value, JsonGenerator g, SerializerProvider provider) throws IOException
{
- final int len = value.length;
- if (len == 1) {
- if (((_unwrapSingle == null) &&
- provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
- || (_unwrapSingle == Boolean.TRUE)) {
- serializeContents(value, g, provider);
- return;
- }
+ final int len = value.length;
+ if ((len == 1) && _shouldUnwrapSingle(provider)) {
+ serializeContents(value, g, provider);
+ return;
}
g.writeStartArray(len);
+ g.setCurrentValue(value);
serializeContents(value, g, provider);
g.writeEndArray();
}
@@ -237,16 +227,8 @@
@SuppressWarnings("cast")
@Override
public void serializeContents(short[] value, JsonGenerator g, SerializerProvider provider)
- throws IOException, JsonGenerationException
+ throws IOException
{
- if (_valueTypeSerializer != null) {
- for (int i = 0, len = value.length; i < len; ++i) {
- _valueTypeSerializer.writeTypePrefixForScalar(null, g, Short.TYPE);
- g.writeNumber(value[i]);
- _valueTypeSerializer.writeTypeSuffixForScalar(null, g);
- }
- return;
- }
for (int i = 0, len = value.length; i < len; ++i) {
g.writeNumber((int)value[i]);
}
@@ -282,16 +264,17 @@
@Override
public boolean isEmpty(SerializerProvider prov, char[] value) {
- return (value == null) || (value.length == 0);
+ return value.length == 0;
}
@Override
public void serialize(char[] value, JsonGenerator g, SerializerProvider provider)
- throws IOException, JsonGenerationException
+ throws IOException
{
// [JACKSON-289] allows serializing as 'sparse' char array too:
if (provider.isEnabled(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS)) {
g.writeStartArray(value.length);
+ g.setCurrentValue(value);
_writeArrayContents(g, value);
g.writeEndArray();
} else {
@@ -302,7 +285,7 @@
@Override
public void serializeWithType(char[] value, JsonGenerator g, SerializerProvider provider,
TypeSerializer typeSer)
- throws IOException, JsonGenerationException
+ throws IOException
{
// [JACKSON-289] allows serializing as 'sparse' char array too:
if (provider.isEnabled(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS)) {
@@ -317,7 +300,7 @@
}
private final void _writeArrayContents(JsonGenerator g, char[] value)
- throws IOException, JsonGenerationException
+ throws IOException
{
for (int i = 0, len = value.length; i < len; ++i) {
g.writeString(value, i, 1);
@@ -385,7 +368,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, int[] value) {
- return (value == null) || (value.length == 0);
+ return value.length == 0;
}
@Override
@@ -397,13 +380,9 @@
public final void serialize(int[] value, JsonGenerator g, SerializerProvider provider) throws IOException
{
final int len = value.length;
- if (len == 1) {
- if (((_unwrapSingle == null) &&
- provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
- || (_unwrapSingle == Boolean.TRUE)) {
- serializeContents(value, g, provider);
- return;
- }
+ if ((len == 1) && _shouldUnwrapSingle(provider)) {
+ serializeContents(value, g, provider);
+ return;
}
// 11-May-2016, tatu: As per [core#277] we have efficient `writeArray(...)` available
g.setCurrentValue(value);
@@ -440,18 +419,13 @@
public LongArraySerializer() { super(long[].class); }
public LongArraySerializer(LongArraySerializer src, BeanProperty prop,
- TypeSerializer vts, Boolean unwrapSingle) {
- super(src, prop, vts, unwrapSingle);
+ Boolean unwrapSingle) {
+ super(src, prop, unwrapSingle);
}
@Override
public JsonSerializer<?> _withResolved(BeanProperty prop,Boolean unwrapSingle) {
- return new LongArraySerializer(this, prop, _valueTypeSerializer, unwrapSingle);
- }
-
- @Override
- public ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts) {
- return new LongArraySerializer(this, _property, vts, _unwrapSingle);
+ return new LongArraySerializer(this, prop, unwrapSingle);
}
@Override
@@ -467,7 +441,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, long[] value) {
- return (value == null) || (value.length == 0);
+ return value.length == 0;
}
@Override
@@ -479,13 +453,9 @@
public final void serialize(long[] value, JsonGenerator g, SerializerProvider provider) throws IOException
{
final int len = value.length;
- if (len == 1) {
- if (((_unwrapSingle == null) &&
- provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
- || (_unwrapSingle == Boolean.TRUE)) {
- serializeContents(value, g, provider);
- return;
- }
+ if ((len == 1) && _shouldUnwrapSingle(provider)) {
+ serializeContents(value, g, provider);
+ return;
}
// 11-May-2016, tatu: As per [core#277] we have efficient `writeArray(...)` available
g.setCurrentValue(value);
@@ -496,15 +466,6 @@
public void serializeContents(long[] value, JsonGenerator g, SerializerProvider provider)
throws IOException
{
- if (_valueTypeSerializer != null) {
- for (int i = 0, len = value.length; i < len; ++i) {
- _valueTypeSerializer.writeTypePrefixForScalar(null, g, Long.TYPE);
- g.writeNumber(value[i]);
- _valueTypeSerializer.writeTypeSuffixForScalar(null, g);
- }
- return;
- }
-
for (int i = 0, len = value.length; i < len; ++i) {
g.writeNumber(value[i]);
}
@@ -536,18 +497,13 @@
super(float[].class);
}
public FloatArraySerializer(FloatArraySerializer src, BeanProperty prop,
- TypeSerializer vts, Boolean unwrapSingle) {
- super(src, prop, vts, unwrapSingle);
- }
-
- @Override
- public ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts) {
- return new FloatArraySerializer(this, _property, vts, _unwrapSingle);
+ Boolean unwrapSingle) {
+ super(src, prop, unwrapSingle);
}
@Override
public JsonSerializer<?> _withResolved(BeanProperty prop,Boolean unwrapSingle) {
- return new FloatArraySerializer(this, prop, _valueTypeSerializer, unwrapSingle);
+ return new FloatArraySerializer(this, prop, unwrapSingle);
}
@Override
@@ -563,7 +519,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, float[] value) {
- return (value == null) || (value.length == 0);
+ return value.length == 0;
}
@Override
@@ -575,31 +531,20 @@
public final void serialize(float[] value, JsonGenerator g, SerializerProvider provider) throws IOException
{
final int len = value.length;
- if (len == 1) {
- if (((_unwrapSingle == null) &&
- provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
- || (_unwrapSingle == Boolean.TRUE)) {
- serializeContents(value, g, provider);
- return;
- }
+ if ((len == 1) && _shouldUnwrapSingle(provider)) {
+ serializeContents(value, g, provider);
+ return;
}
g.writeStartArray(len);
+ g.setCurrentValue(value);
serializeContents(value, g, provider);
g.writeEndArray();
}
@Override
public void serializeContents(float[] value, JsonGenerator g, SerializerProvider provider)
- throws IOException, JsonGenerationException
+ throws IOException
{
- if (_valueTypeSerializer != null) {
- for (int i = 0, len = value.length; i < len; ++i) {
- _valueTypeSerializer.writeTypePrefixForScalar(null, g, Float.TYPE);
- g.writeNumber(value[i]);
- _valueTypeSerializer.writeTypeSuffixForScalar(null, g);
- }
- return;
- }
for (int i = 0, len = value.length; i < len; ++i) {
g.writeNumber(value[i]);
}
@@ -661,7 +606,7 @@
@Override
public boolean isEmpty(SerializerProvider prov, double[] value) {
- return (value == null) || (value.length == 0);
+ return value.length == 0;
}
@Override
@@ -673,16 +618,12 @@
public final void serialize(double[] value, JsonGenerator g, SerializerProvider provider) throws IOException
{
final int len = value.length;
- if (len == 1) {
- if (((_unwrapSingle == null) &&
- provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
- || (_unwrapSingle == Boolean.TRUE)) {
- serializeContents(value, g, provider);
- return;
- }
+ if ((len == 1) && _shouldUnwrapSingle(provider)) {
+ serializeContents(value, g, provider);
+ return;
}
- // 11-May-2016, tatu: As per [core#277] we have efficient `writeArray(...)` available
g.setCurrentValue(value);
+ // 11-May-2016, tatu: As per [core#277] we have efficient `writeArray(...)` available
g.writeArray(value, 0, value.length);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdDelegatingSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdDelegatingSerializer.java
index 067ebc6..f162101 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdDelegatingSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdDelegatingSerializer.java
@@ -9,6 +9,7 @@
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
+import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.Converter;
import java.io.IOException;
@@ -83,9 +84,7 @@
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
JavaType delegateType, JsonSerializer<?> delegateSerializer)
{
- if (getClass() != StdDelegatingSerializer.class) {
- throw new IllegalStateException("Sub-class "+getClass().getName()+" must override 'withDelegate'");
- }
+ ClassUtil.verifyMustOverride(StdDelegatingSerializer.class, this, "withDelegate");
return new StdDelegatingSerializer(converter, delegateType, delegateSerializer);
}
@@ -116,9 +115,8 @@
if (delegateType == null) {
delegateType = _converter.getOutputType(provider.getTypeFactory());
}
- /* 02-Apr-2015, tatu: For "dynamic case", where type is only specified as
- * java.lang.Object (or missing generic), [databind#731]
- */
+ // 02-Apr-2015, tatu: For "dynamic case", where type is only specified as
+ // java.lang.Object (or missing generic), [databind#731]
if (!delegateType.isJavaLangObject()) {
delSer = provider.findValueSerializer(delegateType);
}
@@ -186,15 +184,12 @@
}
@Override
- @Deprecated // since 2.5
- public boolean isEmpty(Object value) {
- return isEmpty(null, value);
- }
-
- @Override
public boolean isEmpty(SerializerProvider prov, Object value)
{
Object delegateValue = convertValue(value);
+ if (delegateValue == null) {
+ return true;
+ }
if (_delegateSerializer == null) { // best we can do for now, too costly to look up
return (value == null);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdJdkSerializers.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdJdkSerializers.java
index a87ce64..b99bab3 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdJdkSerializers.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdJdkSerializers.java
@@ -27,15 +27,13 @@
HashMap<Class<?>,Object> sers = new HashMap<Class<?>,Object>();
// First things that 'toString()' can handle
- final ToStringSerializer sls = ToStringSerializer.instance;
+ sers.put(java.net.URL.class, new ToStringSerializer(java.net.URL.class));
+ sers.put(java.net.URI.class, new ToStringSerializer(java.net.URI.class));
- sers.put(java.net.URL.class, sls);
- sers.put(java.net.URI.class, sls);
-
- sers.put(Currency.class, sls);
+ sers.put(Currency.class, new ToStringSerializer(Currency.class));
sers.put(UUID.class, new UUIDSerializer());
- sers.put(java.util.regex.Pattern.class, sls);
- sers.put(Locale.class, sls);
+ sers.put(java.util.regex.Pattern.class, new ToStringSerializer(java.util.regex.Pattern.class));
+ sers.put(Locale.class, new ToStringSerializer(Locale.class));
// then atomic types (note: AtomicReference defined elsewhere)
sers.put(AtomicBoolean.class, AtomicBooleanSerializer.class);
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializer.java
index fceba52..c62e444 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializer.java
@@ -1,17 +1,13 @@
package com.fasterxml.jackson.databind.ser.std;
import java.io.IOException;
-import java.lang.reflect.Type;
-import java.util.Date;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
-import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
/**
- * Specialized serializer that can be used as the generic key
- * serializer, when serializing {@link java.util.Map}s to JSON
- * Objects.
+ * Specialized serializer that can be used as the generic key serializer,
+ * when serializing {@link java.util.Map}s to JSON Objects.
*
* @deprecated Since 2.8, use {@link StdKeySerializers.Default} instead.
*/
@@ -23,42 +19,7 @@
@Override
public void serialize(Object value, JsonGenerator g, SerializerProvider provider) throws IOException {
- String str;
- Class<?> cls = value.getClass();
-
- if (cls == String.class) {
- str = (String) value;
- } else if (cls.isEnum()) {
- // 24-Sep-2015, tatu: Minor improvement over older (2.6.2 and before) code: at least
- // use name/toString() variation for as per configuration
- if (provider.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)) {
- str = value.toString();
- } else {
- Enum<?> en = (Enum<?>) value;
- if (provider.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)) {
- str = String.valueOf(en.ordinal());
- } else {
- str = en.name();
- }
- }
- } else if (value instanceof Date) {
- provider.defaultSerializeDateKey((Date) value, g);
- return;
- } else if (cls == Class.class) {
- str = ((Class<?>) value).getName();
- } else {
- str = value.toString();
- }
- g.writeFieldName(str);
- }
-
- @Override
- public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException {
- return createSchemaNode("string");
- }
-
- @Override
- public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {
- visitStringFormat(visitor, typeHint);
+ // 19-Oct-2016, tatu: Simplified to bare essentials since this is deprecated
+ g.writeFieldName(value.toString());
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java
index 2462f2e..ea99d6f 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java
@@ -8,18 +8,17 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap;
+import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.EnumValues;
@SuppressWarnings("serial")
-public class StdKeySerializers
+public abstract class StdKeySerializers
{
@SuppressWarnings("deprecation")
protected final static JsonSerializer<Object> DEFAULT_KEY_SERIALIZER = new StdKeySerializer();
protected final static JsonSerializer<Object> DEFAULT_STRING_SERIALIZER = new StringKeySerializer();
- private StdKeySerializers() { }
-
/**
* @param config Serialization configuration in use, may be needed in choosing
* serializer to use
@@ -42,6 +41,15 @@
if (rawKeyType == String.class) {
return DEFAULT_STRING_SERIALIZER;
}
+ if (rawKeyType.isPrimitive()) {
+ rawKeyType = ClassUtil.wrapperType(rawKeyType);
+ }
+ if (rawKeyType == Integer.class) {
+ return new Default(Default.TYPE_INTEGER, rawKeyType);
+ }
+ if (rawKeyType == Long.class) {
+ return new Default(Default.TYPE_LONG, rawKeyType);
+ }
if (rawKeyType.isPrimitive() || Number.class.isAssignableFrom(rawKeyType)) {
// 28-Jun-2016, tatu: Used to just return DEFAULT_KEY_SERIALIZER, but makes
// more sense to use simpler one directly
@@ -60,8 +68,12 @@
if (rawKeyType == java.util.UUID.class) {
return new Default(Default.TYPE_TO_STRING, rawKeyType);
}
+ if (rawKeyType == byte[].class) {
+ return new Default(Default.TYPE_BYTE_ARRAY, rawKeyType);
+ }
if (useDefault) {
- return DEFAULT_KEY_SERIALIZER;
+ // 19-Oct-2016, tatu: Used to just return DEFAULT_KEY_SERIALIZER but why not:
+ return new Default(Default.TYPE_TO_STRING, rawKeyType);
}
return null;
}
@@ -91,7 +103,8 @@
EnumValues.constructFromName(config, (Class<Enum<?>>) rawKeyType));
}
}
- return DEFAULT_KEY_SERIALIZER;
+ // 19-Oct-2016, tatu: Used to just return DEFAULT_KEY_SERIALIZER but why not:
+ return new Default(Default.TYPE_TO_STRING, rawKeyType);
}
/**
@@ -121,7 +134,10 @@
final static int TYPE_CALENDAR = 2;
final static int TYPE_CLASS = 3;
final static int TYPE_ENUM = 4;
- final static int TYPE_TO_STRING = 5;
+ final static int TYPE_INTEGER = 5; // since 2.9
+ final static int TYPE_LONG = 6; // since 2.9
+ final static int TYPE_BYTE_ARRAY = 7; // since 2.9
+ final static int TYPE_TO_STRING = 8;
protected final int _typeId;
@@ -159,6 +175,16 @@
g.writeFieldName(key);
}
break;
+ case TYPE_INTEGER:
+ case TYPE_LONG:
+ g.writeFieldId(((Number) value).longValue());
+ break;
+ case TYPE_BYTE_ARRAY:
+ {
+ String encoded = provider.getConfig().getBase64Variant().encode((byte[]) value);
+ g.writeFieldName(encoded);
+ }
+ break;
case TYPE_TO_STRING:
default:
g.writeFieldName(value.toString());
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdSerializer.java
index a1108e8..aee6593 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StdSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StdSerializer.java
@@ -3,6 +3,9 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
+import java.util.Collection;
+import java.util.IdentityHashMap;
+import java.util.Map;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -30,21 +33,17 @@
extends JsonSerializer<T>
implements JsonFormatVisitable, SchemaAware, java.io.Serializable
{
- /**
- * Unique key we use to store a temporary lock, to prevent infinite recursion
- * when resolving content converters (see [databind#357]).
- *<p>
- * NOTE: may need to revisit this if nested content converters are needed; if so,
- * may need to create per-call lock object. But let's start with a simpler
- * solution for now.
- *
- * @since 2.7
- */
- private final static Object CONVERTING_CONTENT_CONVERTER_LOCK = new Object();
-
private static final long serialVersionUID = 1L;
/**
+ * Key used for storing a lock object to prevent infinite recursion when
+ * constructing converting serializers.
+ *
+ * @since 2.9
+ */
+ private final static Object KEY_CONTENT_CONVERTER_LOCK = new Object();
+
+ /**
* Nominal type supported, usually declared type of
* property for which serializer is used.
*/
@@ -138,7 +137,7 @@
{
ObjectNode schema = (ObjectNode) getSchema(provider, typeHint);
if (!isOptional) {
- schema.put("required", !isOptional);
+ schema.put("required", !isOptional);
}
return schema;
}
@@ -149,13 +148,9 @@
/**********************************************************
*/
- protected ObjectNode createObjectNode() {
- return JsonNodeFactory.instance.objectNode();
- }
-
protected ObjectNode createSchemaNode(String type)
{
- ObjectNode schema = createObjectNode();
+ ObjectNode schema = JsonNodeFactory.instance.objectNode();
schema.put("type", type);
return schema;
}
@@ -177,9 +172,7 @@
*/
protected void visitStringFormat(JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException {
- if (visitor != null) {
- /*JsonStringFormatVisitor v2 =*/ visitor.expectStringFormat(typeHint);
- }
+ /*JsonStringFormatVisitor v2 =*/ visitor.expectStringFormat(typeHint);
}
/**
@@ -193,11 +186,9 @@
JsonValueFormat format)
throws JsonMappingException
{
- if (visitor != null) {
- JsonStringFormatVisitor v2 = visitor.expectStringFormat(typeHint);
- if (v2 != null) {
- v2.format(format);
- }
+ JsonStringFormatVisitor v2 = visitor.expectStringFormat(typeHint);
+ if (v2 != null) {
+ v2.format(format);
}
}
@@ -211,13 +202,9 @@
NumberType numberType)
throws JsonMappingException
{
- if (visitor != null) {
- JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
- if (v2 != null) {
- if (numberType != null) {
- v2.numberType(numberType);
- }
- }
+ JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
+ if (_neitherNull(v2, numberType)) {
+ v2.numberType(numberType);
}
}
@@ -232,15 +219,13 @@
NumberType numberType, JsonValueFormat format)
throws JsonMappingException
{
- if (visitor != null) {
- JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
- if (v2 != null) {
- if (numberType != null) {
- v2.numberType(numberType);
- }
- if (format != null) {
- v2.format(format);
- }
+ JsonIntegerFormatVisitor v2 = visitor.expectIntegerFormat(typeHint);
+ if (v2 != null) {
+ if (numberType != null) {
+ v2.numberType(numberType);
+ }
+ if (format != null) {
+ v2.format(format);
}
}
}
@@ -255,11 +240,9 @@
NumberType numberType)
throws JsonMappingException
{
- if (visitor != null) {
- JsonNumberFormatVisitor v2 = visitor.expectNumberFormat(typeHint);
- if (v2 != null) {
- v2.numberType(numberType);
- }
+ JsonNumberFormatVisitor v2 = visitor.expectNumberFormat(typeHint);
+ if (v2 != null) {
+ v2.numberType(numberType);
}
}
@@ -270,13 +253,9 @@
JsonSerializer<?> itemSerializer, JavaType itemType)
throws JsonMappingException
{
- if (visitor != null) {
- JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
- if (v2 != null) {
- if (itemSerializer != null) {
- v2.itemsFormat(itemSerializer, itemType);
- }
- }
+ JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
+ if (_neitherNull(v2, itemSerializer)) {
+ v2.itemsFormat(itemSerializer, itemType);
}
}
@@ -287,14 +266,12 @@
JsonFormatTypes itemType)
throws JsonMappingException
{
- if (visitor != null) {
- JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
- if (v2 != null) {
- v2.itemsFormat(itemType);
- }
+ JsonArrayFormatVisitor v2 = visitor.expectArrayFormat(typeHint);
+ if (v2 != null) {
+ v2.itemsFormat(itemType);
}
}
-
+
/*
/**********************************************************
/* Helper methods for exception handling
@@ -324,20 +301,16 @@
while (t instanceof InvocationTargetException && t.getCause() != null) {
t = t.getCause();
}
- // Errors and "plain" IOExceptions to be passed as is
- if (t instanceof Error) {
- throw (Error) t;
- }
+ // Errors and "plain" to be passed as is
+ ClassUtil.throwIfError(t);
// Ditto for IOExceptions... except for mapping exceptions!
boolean wrap = (provider == null) || provider.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
if (t instanceof IOException) {
if (!wrap || !(t instanceof JsonMappingException)) {
throw (IOException) t;
}
- } else if (!wrap) { // [JACKSON-407] -- allow disabling wrapping for unchecked exceptions
- if (t instanceof RuntimeException) {
- throw (RuntimeException) t;
- }
+ } else if (!wrap) {
+ ClassUtil.throwIfRTE(t);
}
// Need to add reference information
throw JsonMappingException.wrapWithPath(t, bean, fieldName);
@@ -351,19 +324,15 @@
t = t.getCause();
}
// Errors are to be passed as is
- if (t instanceof Error) {
- throw (Error) t;
- }
+ ClassUtil.throwIfError(t);
// Ditto for IOExceptions... except for mapping exceptions!
boolean wrap = (provider == null) || provider.isEnabled(SerializationFeature.WRAP_EXCEPTIONS);
if (t instanceof IOException) {
if (!wrap || !(t instanceof JsonMappingException)) {
throw (IOException) t;
}
- } else if (!wrap) { // [JACKSON-407] -- allow disabling wrapping for unchecked exceptions
- if (t instanceof RuntimeException) {
- throw (RuntimeException) t;
- }
+ } else if (!wrap) {
+ ClassUtil.throwIfRTE(t);
}
// Need to add reference information
throw JsonMappingException.wrapWithPath(t, bean, index);
@@ -383,34 +352,50 @@
* @param existingSerializer (optional) configured content
* serializer if one already exists.
*
- * @since 2.2
+ * @since 2.9
*/
+ protected JsonSerializer<?> findContextualConvertingSerializer(SerializerProvider provider,
+ BeanProperty property, JsonSerializer<?> existingSerializer)
+ throws JsonMappingException
+ {
+ // 08-Dec-2016, tatu: to fix [databind#357], need to prevent recursive calls for
+ // same property
+ @SuppressWarnings("unchecked")
+ Map<Object,Object> conversions = (Map<Object,Object>) provider.getAttribute(KEY_CONTENT_CONVERTER_LOCK);
+ if (conversions != null) {
+ Object lock = conversions.get(property);
+ if (lock != null) {
+ return existingSerializer;
+ }
+ } else {
+ conversions = new IdentityHashMap<>();
+ provider.setAttribute(KEY_CONTENT_CONVERTER_LOCK, conversions);
+ }
+ conversions.put(property, Boolean.TRUE);
+ try {
+ JsonSerializer<?> ser = findConvertingContentSerializer(provider, property, existingSerializer);
+ if (ser != null) {
+ return provider.handleSecondaryContextualization(ser, property);
+ }
+ } finally {
+ conversions.remove(property);
+ }
+ return existingSerializer;
+ }
+
+ /**
+ * @deprecated Since 2.9 use {link {@link #findContextualConvertingSerializer} instead
+ */
+ @Deprecated
protected JsonSerializer<?> findConvertingContentSerializer(SerializerProvider provider,
BeanProperty prop, JsonSerializer<?> existingSerializer)
throws JsonMappingException
{
- /* 19-Oct-2014, tatu: As per [databind#357], need to avoid infinite loop
- * when applying contextual content converter; this is not ideal way,
- * but should work for most cases.
- */
- Object ob = provider.getAttribute(CONVERTING_CONTENT_CONVERTER_LOCK);
- if (ob != null) {
- if (ob == Boolean.TRUE) { // just to ensure it's value we added.
- return existingSerializer;
- }
- }
-
final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
- if (intr != null && prop != null) {
+ if (_neitherNull(intr, prop)) {
AnnotatedMember m = prop.getMember();
if (m != null) {
- provider.setAttribute(CONVERTING_CONTENT_CONVERTER_LOCK, Boolean.TRUE);
- Object convDef;
- try {
- convDef = intr.findSerializationContentConverter(m);
- } finally {
- provider.setAttribute(CONVERTING_CONTENT_CONVERTER_LOCK, null);
- }
+ Object convDef = intr.findSerializationContentConverter(m);
if (convDef != null) {
Converter<Object,Object> conv = provider.converterInstance(prop.getMember(), convDef);
JavaType delegateType = conv.getOutputType(provider.getTypeFactory());
@@ -438,7 +423,7 @@
FilterProvider filters = provider.getFilterProvider();
// Not ok to miss the provider, if a filter is declared to be needed.
if (filters == null) {
- throw JsonMappingException.from(provider,
+ provider.reportBadDefinition(handledType(),
"Can not resolve PropertyFilter with id '"+filterId+"'; no FilterProvider configured");
}
// But whether unknown ids are ok just depends on filter provider; if we get null that's fine
@@ -534,4 +519,18 @@
protected boolean isDefaultSerializer(JsonSerializer<?> serializer) {
return ClassUtil.isJacksonStdImpl(serializer);
}
+
+ /**
+ * @since 2.9
+ */
+ protected final static boolean _neitherNull(Object a, Object b) {
+ return (a != null) && (b != null);
+ }
+
+ /**
+ * @since 2.9
+ */
+ protected final static boolean _nonEmpty(Collection<?> c) {
+ return (c != null) && !c.isEmpty();
+ }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/StringSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/StringSerializer.java
index 3ec907c..d3c621c 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/StringSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/StringSerializer.java
@@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
+import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
/**
* This is the special serializer for regular {@link java.lang.String}s.
@@ -22,26 +23,17 @@
public final class StringSerializer
// NOTE: generic parameter changed from String to Object in 2.6, to avoid
// use of bridge methods
- extends NonTypedScalarSerializerBase<Object>
+// In 2.9, removed use of intermediate type `NonTypedScalarSerializerBase`
+ extends StdScalarSerializer<Object>
{
private static final long serialVersionUID = 1L;
public StringSerializer() { super(String.class, false); }
- /**
- * For Strings, both null and Empty String qualify for emptiness.
- */
- @Override
- @Deprecated
- public boolean isEmpty(Object value) {
- String str = (String) value;
- return (str == null) || (str.length() == 0);
- }
-
@Override
public boolean isEmpty(SerializerProvider prov, Object value) {
String str = (String) value;
- return (str == null) || (str.length() == 0);
+ return str.length() == 0;
}
@Override
@@ -50,6 +42,14 @@
}
@Override
+ public final void serializeWithType(Object value, JsonGenerator gen, SerializerProvider provider,
+ TypeSerializer typeSer) throws IOException
+ {
+ // no type info, just regular serialization
+ gen.writeString((String) value);
+ }
+
+ @Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
return createSchemaNode("string", true);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/ToStringSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/ToStringSerializer.java
index 5d3aa91..6a572b4 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/ToStringSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/ToStringSerializer.java
@@ -48,13 +48,9 @@
@Override
public boolean isEmpty(SerializerProvider prov, Object value) {
- if (value == null) {
- return true;
- }
- String str = value.toString();
- return str.isEmpty();
+ return value.toString().isEmpty();
}
-
+
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider)
throws IOException
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/TokenBufferSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/TokenBufferSerializer.java
index d7539b2..065a3be 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/TokenBufferSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/TokenBufferSerializer.java
@@ -28,7 +28,7 @@
@Override
public void serialize(TokenBuffer value, JsonGenerator jgen, SerializerProvider provider)
- throws IOException
+ throws IOException
{
value.serialize(jgen);
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/std/UUIDSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/std/UUIDSerializer.java
index cd9e5a1..21768e7 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ser/std/UUIDSerializer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ser/std/UUIDSerializer.java
@@ -25,9 +25,6 @@
@Override
public boolean isEmpty(SerializerProvider prov, UUID value)
{
- if (value == null) {
- return true;
- }
// Null UUID is empty, so...
if (value.getLeastSignificantBits() == 0L
&& value.getMostSignificantBits() == 0L) {
diff --git a/src/main/java/com/fasterxml/jackson/databind/type/TypeBindings.java b/src/main/java/com/fasterxml/jackson/databind/type/TypeBindings.java
index 6752f20..416eeb0 100644
--- a/src/main/java/com/fasterxml/jackson/databind/type/TypeBindings.java
+++ b/src/main/java/com/fasterxml/jackson/databind/type/TypeBindings.java
@@ -4,6 +4,7 @@
import java.util.*;
import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Helper class used for resolving type parameters for given class
@@ -335,7 +336,9 @@
@Override public boolean equals(Object o)
{
if (o == this) return true;
- if (o == null || o.getClass() != getClass()) return false;
+ if (!ClassUtil.hasClass(o, getClass())) {
+ return false;
+ }
TypeBindings other = (TypeBindings) o;
int len = _types.length;
if (len != other.size()) {
diff --git a/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java b/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java
index 08455bf..5e35ccf 100644
--- a/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java
+++ b/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java
@@ -297,15 +297,13 @@
prob = ClassUtil.getRootCause(e);
}
}
- if (prob instanceof RuntimeException) {
- throw (RuntimeException) prob;
- }
+ ClassUtil.throwIfRTE(prob);
throw new ClassNotFoundException(prob.getMessage(), prob);
}
protected Class<?> classForName(String name, boolean initialize,
- ClassLoader loader) throws ClassNotFoundException {
- return Class.forName(name, true, loader);
+ ClassLoader loader) throws ClassNotFoundException {
+ return Class.forName(name, true, loader);
}
protected Class<?> classForName(String name) throws ClassNotFoundException {
@@ -729,11 +727,22 @@
* for contained types.
*/
public CollectionType constructCollectionType(Class<? extends Collection> collectionClass,
- JavaType elementType) {
- // 19-Oct-2015, tatu: Allow case of no-type-variables, since it seems likely to be
- // a valid use case here
- return (CollectionType) _fromClass(null, collectionClass,
- TypeBindings.create(collectionClass, elementType));
+ JavaType elementType)
+ {
+ TypeBindings bindings = TypeBindings.createIfNeeded(collectionClass, elementType);
+ CollectionType result = (CollectionType) _fromClass(null, collectionClass, bindings);
+ // 17-May-2017, tatu: As per [databind#1415], we better verify bound values if (but only if)
+ // type being resolved was non-generic (i.e.element type was ignored)
+ if (bindings.isEmpty() && (elementType != null)) {
+ JavaType t = result.findSuperType(Collection.class);
+ JavaType realET = t.getContentType();
+ if (!realET.equals(elementType)) {
+ throw new IllegalArgumentException(String.format(
+ "Non-generic Collection class %s did not resolve to something with element type %s but %s ",
+ ClassUtil.nameOf(collectionClass), elementType, realET));
+ }
+ }
+ return result;
}
/**
@@ -787,8 +796,26 @@
* for contained types.
*/
public MapType constructMapType(Class<? extends Map> mapClass, JavaType keyType, JavaType valueType) {
- return (MapType) _fromClass(null, mapClass,
- TypeBindings.create(mapClass, keyType, valueType));
+ TypeBindings bindings = TypeBindings.createIfNeeded(mapClass, new JavaType[] { keyType, valueType });
+ MapType result = (MapType) _fromClass(null, mapClass, bindings);
+ // 17-May-2017, tatu: As per [databind#1415], we better verify bound values if (but only if)
+ // type being resolved was non-generic (i.e.element type was ignored)
+ if (bindings.isEmpty()) {
+ JavaType t = result.findSuperType(Map.class);
+ JavaType realKT = t.getKeyType();
+ if (!realKT.equals(keyType)) {
+ throw new IllegalArgumentException(String.format(
+ "Non-generic Map class %s did not resolve to something with key type %s but %s ",
+ ClassUtil.nameOf(mapClass), keyType, realKT));
+ }
+ JavaType realVT = t.getContentType();
+ if (!realVT.equals(valueType)) {
+ throw new IllegalArgumentException(String.format(
+ "Non-generic Map class %s did not resolve to something with value type %s but %s ",
+ ClassUtil.nameOf(mapClass), valueType, realVT));
+ }
+ }
+ return result;
}
/**
@@ -876,15 +903,15 @@
* type <code>List<Set<Integer>></code>, you could
* call
*<pre>
- * JavaType inner = TypeFactory.constructParametrizedType(Set.class, Set.class, Integer.class);
- * return TypeFactory.constructParametrizedType(ArrayList.class, List.class, inner);
+ * JavaType inner = TypeFactory.constructParametricType(Set.class, Set.class, Integer.class);
+ * return TypeFactory.constructParametricType(ArrayList.class, List.class, inner);
*</pre>
*<p>
* The reason for first two arguments to be separate is that parameterization may
* apply to a super-type. For example, if generic type was instead to be
* constructed for <code>ArrayList<Integer></code>, the usual call would be:
*<pre>
- * TypeFactory.constructParametrizedType(ArrayList.class, List.class, Integer.class);
+ * TypeFactory.constructParametricType(ArrayList.class, List.class, Integer.class);
*</pre>
* since parameterization is applied to {@link java.util.List}.
* In most cases distinction does not matter, but there are types where it does;
@@ -912,15 +939,15 @@
* type <code>List<Set<Integer>></code>, you could
* call
*<pre>
- * JavaType inner = TypeFactory.constructParametrizedType(Set.class, Set.class, Integer.class);
- * return TypeFactory.constructParametrizedType(ArrayList.class, List.class, inner);
+ * JavaType inner = TypeFactory.constructParametricType(Set.class, Set.class, Integer.class);
+ * return TypeFactory.constructParametricType(ArrayList.class, List.class, inner);
*</pre>
*<p>
* The reason for first two arguments to be separate is that parameterization may
* apply to a super-type. For example, if generic type was instead to be
* constructed for <code>ArrayList<Integer></code>, the usual call would be:
*<pre>
- * TypeFactory.constructParametrizedType(ArrayList.class, List.class, Integer.class);
+ * TypeFactory.constructParametricType(ArrayList.class, List.class, Integer.class);
*</pre>
* since parameterization is applied to {@link java.util.List}.
* In most cases distinction does not matter, but there are types where it does;
@@ -940,7 +967,10 @@
/**
* @since 2.5 -- but will probably deprecated in 2.7 or 2.8 (not needed with 2.7)
+ *
+ * @deprecated since 2.9 Use {@link #constructParametricType(Class,JavaType...)} instead
*/
+ @Deprecated
public JavaType constructParametrizedType(Class<?> parametrized, Class<?> parametersFor,
JavaType... parameterTypes)
{
@@ -949,7 +979,10 @@
/**
* @since 2.5 -- but will probably deprecated in 2.7 or 2.8 (not needed with 2.7)
+ *
+ * @deprecated since 2.9 Use {@link #constructParametricType(Class,Class...)} instead
*/
+ @Deprecated
public JavaType constructParametrizedType(Class<?> parametrized, Class<?> parametersFor,
Class<?>... parameterClasses)
{
diff --git a/src/main/java/com/fasterxml/jackson/databind/type/TypeParser.java b/src/main/java/com/fasterxml/jackson/databind/type/TypeParser.java
index fe2d096..72a475e 100644
--- a/src/main/java/com/fasterxml/jackson/databind/type/TypeParser.java
+++ b/src/main/java/com/fasterxml/jackson/databind/type/TypeParser.java
@@ -3,6 +3,7 @@
import java.util.*;
import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.util.ClassUtil;
/**
* Simple recursive-descent parser for parsing canonical {@link JavaType}
@@ -81,9 +82,7 @@
try {
return _factory.findClass(className);
} catch (Exception e) {
- if (e instanceof RuntimeException) {
- throw (RuntimeException) e;
- }
+ ClassUtil.throwIfRTE(e);
throw _problem(tokens, "Can not locate class '"+className+"', problem: "+e.getMessage());
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/AccessPattern.java b/src/main/java/com/fasterxml/jackson/databind/util/AccessPattern.java
new file mode 100644
index 0000000..03cabcd
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/util/AccessPattern.java
@@ -0,0 +1,31 @@
+package com.fasterxml.jackson.databind.util;
+
+/**
+ * Enumeration used to indicate required access pattern for providers:
+ * this can sometimes be used to optimize out dynamic calls.
+ * The main difference is between constant values (which can be resolved once)
+ * and dynamic ones (which must be resolved anew every time).
+ */
+public enum AccessPattern {
+ /**
+ * Value that indicates that provider never returns anything other than
+ * Java `null`.
+ */
+ ALWAYS_NULL,
+
+ /**
+ * Value that indicates that provider will always return a constant
+ * value, regardless of when it is called; and also that it never
+ * uses `context` argument (which may then be passed as `null`)
+ */
+ CONSTANT,
+
+ /**
+ * Value that indicates that provider may return different values
+ * at different times (often a freshly constructed empty container),
+ * and thus must be called every time "null replacement" value is
+ * needed.
+ */
+ DYNAMIC
+ ;
+}
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/Annotations.java b/src/main/java/com/fasterxml/jackson/databind/util/Annotations.java
index cdb7ea9..d362403 100644
--- a/src/main/java/com/fasterxml/jackson/databind/util/Annotations.java
+++ b/src/main/java/com/fasterxml/jackson/databind/util/Annotations.java
@@ -18,6 +18,16 @@
public <A extends Annotation> A get(Class<A> cls);
/**
+ * @since 2.9
+ */
+ public boolean has(Class<?> cls);
+
+ /**
+ * @since 2.9
+ */
+ public boolean hasOneOf(Class<? extends Annotation>[] annoClasses);
+
+ /**
* Returns number of annotation entries in this collection.
*/
public int size();
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/ArrayBuilders.java b/src/main/java/com/fasterxml/jackson/databind/util/ArrayBuilders.java
index 834a96e..ae2939a 100644
--- a/src/main/java/com/fasterxml/jackson/databind/util/ArrayBuilders.java
+++ b/src/main/java/com/fasterxml/jackson/databind/util/ArrayBuilders.java
@@ -162,7 +162,7 @@
@Override
public boolean equals(Object other) {
if (other == this) return true;
- if (other == null || other.getClass() != defaultValueType) {
+ if (!ClassUtil.hasClass(other, defaultValueType)) {
return false;
}
if (Array.getLength(other) != length) return false;
@@ -184,76 +184,15 @@
public static <T> HashSet<T> arrayToSet(T[] elements)
{
- HashSet<T> result = new HashSet<T>();
if (elements != null) {
- for (T elem : elements) {
- result.add(elem);
+ int len = elements.length;
+ HashSet<T> result = new HashSet<T>(len);
+ for (int i = 0; i < len; ++i) {
+ result.add(elements[i]);
}
+ return result;
}
- return result;
- }
-
- public static <T> ArrayList<T> arrayToList(T[] elements)
- {
- ArrayList<T> result = new ArrayList<T>();
- if (elements != null) {
- for (T elem : elements) {
- result.add(elem);
- }
- }
- return result;
- }
-
- public static <T> HashSet<T> setAndArray(Set<T> set, T[] elements)
- {
- HashSet<T> result = new HashSet<T>();
- if (set != null) {
- result.addAll(set);
- }
- if (elements != null) {
- for (T value : elements) {
- result.add(value);
- }
- }
- return result;
- }
-
- /**
- * Helper method for adding specified element to a List, but also
- * considering case where the List may not have been yet constructed
- * (that is, null is passed instead).
- *
- * @param list List to add to; may be null to indicate that a new
- * List is to be constructed
- * @param element Element to add to list
- *
- * @return List in which element was added; either <code>list</code>
- * (if it was not null), or a newly constructed List.
- */
- public static <T> List<T> addToList(List<T> list, T element)
- {
- if (list == null) {
- list = new ArrayList<T>();
- }
- list.add(element);
- return list;
- }
-
- /**
- * Helper method for constructing a new array that contains specified
- * element followed by contents of the given array. No checking is done
- * to see if element being inserted is duplicate.
- */
- public static <T> T[] insertInList(T[] array, T element)
- {
- int len = array.length;
- @SuppressWarnings("unchecked")
- T[] result = (T[]) Array.newInstance(array.getClass().getComponentType(), len+1);
- if (len > 0) {
- System.arraycopy(array, 0, result, 1, len);
- }
- result[0] = element;
- return result;
+ return new HashSet<T>();
}
/**
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/BeanUtil.java b/src/main/java/com/fasterxml/jackson/databind/util/BeanUtil.java
index 5fcaa8d..6127a8a 100644
--- a/src/main/java/com/fasterxml/jackson/databind/util/BeanUtil.java
+++ b/src/main/java/com/fasterxml/jackson/databind/util/BeanUtil.java
@@ -1,5 +1,11 @@
package com.fasterxml.jackson.databind.util;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
/**
@@ -77,6 +83,7 @@
/**
* @since 2.5
*/
+ @Deprecated // since 2.9, not used any more
public static String okNameForSetter(AnnotatedMethod am, boolean stdNaming) {
String name = okNameForMutator(am, "set", stdNaming);
if ((name != null)
@@ -103,33 +110,52 @@
/*
/**********************************************************
- /* Handling property names, deprecated methods
+ /* Value defaulting helpers
/**********************************************************
*/
+
+ /**
+ * Accessor used to find out "default value" to use for comparing values to
+ * serialize, to determine whether to exclude value from serialization with
+ * inclusion type of {@link com.fasterxml.jackson.annotation.JsonInclude.Include#NON_DEFAULT}.
+ *<p>
+ * Default logic is such that for primitives and wrapper types for primitives, expected
+ * defaults (0 for `int` and `java.lang.Integer`) are returned; for Strings, empty String,
+ * and for structured (Maps, Collections, arrays) and reference types, criteria
+ * {@link com.fasterxml.jackson.annotation.JsonInclude.Include#NON_DEFAULT}
+ * is used.
+ *
+ * @since 2.7
+ */
+ public static Object getDefaultValue(JavaType type)
+ {
+ // 06-Nov-2015, tatu: Returning null is fine for Object types; but need special
+ // handling for primitives since they are never passed as nulls.
+ Class<?> cls = type.getRawClass();
- @Deprecated // since 2.5
- public static String okNameForGetter(AnnotatedMethod am) {
- return okNameForGetter(am, false);
- }
-
- @Deprecated // since 2.5
- public static String okNameForRegularGetter(AnnotatedMethod am, String name) {
- return okNameForRegularGetter(am, name, false);
- }
-
- @Deprecated // since 2.5
- public static String okNameForIsGetter(AnnotatedMethod am, String name) {
- return okNameForIsGetter(am, name, false);
- }
-
- @Deprecated // since 2.5
- public static String okNameForSetter(AnnotatedMethod am) {
- return okNameForSetter(am, false);
- }
-
- @Deprecated // since 2.5
- public static String okNameForMutator(AnnotatedMethod am, String prefix) {
- return okNameForMutator(am, prefix, false);
+ // 30-Sep-2016, tatu: Also works for Wrappers, so both `Integer.TYPE` and `Integer.class`
+ // would return `Integer.TYPE`
+ Class<?> prim = ClassUtil.primitiveType(cls);
+ if (prim != null) {
+ return ClassUtil.defaultValue(prim);
+ }
+ if (type.isContainerType() || type.isReferenceType()) {
+ return JsonInclude.Include.NON_EMPTY;
+ }
+ if (cls == String.class) {
+ return "";
+ }
+ // 09-Mar-2016, tatu: Not sure how far this path we want to go but for now
+ // let's add `java.util.Date` and `java.util.Calendar`, as per [databind#1550]
+ if (type.isTypeOrSubTypeOf(Date.class)) {
+ return new Date(0L);
+ }
+ if (type.isTypeOrSubTypeOf(Calendar.class)) {
+ Calendar c = new GregorianCalendar();
+ c.setTimeInMillis(0L);
+ return c;
+ }
+ return null;
}
/*
@@ -150,25 +176,21 @@
{
Class<?> rt = am.getRawType();
// Ok, first: must return an array type
- if (rt == null || !rt.isArray()) {
- return false;
- }
- /* And that type needs to be "net.sf.cglib.proxy.Callback".
- * Theoretically could just be a type that implements it, but
- * for now let's keep things simple, fix if need be.
- */
- Class<?> compType = rt.getComponentType();
- // Actually, let's just verify it's a "net.sf.cglib.*" class/interface
- String pkgName = ClassUtil.getPackageName(compType);
- if (pkgName != null) {
- if (pkgName.contains(".cglib")) {
- if (pkgName.startsWith("net.sf.cglib")
- // also, as per [JACKSON-177]
- || pkgName.startsWith("org.hibernate.repackage.cglib")
- // and [core#674]
- || pkgName.startsWith("org.springframework.cglib")
- ) {
- return true;
+ if (rt.isArray()) {
+ /* And that type needs to be "net.sf.cglib.proxy.Callback".
+ * Theoretically could just be a type that implements it, but
+ * for now let's keep things simple, fix if need be.
+ */
+ Class<?> compType = rt.getComponentType();
+ // Actually, let's just verify it's a "net.sf.cglib.*" class/interface
+ String pkgName = ClassUtil.getPackageName(compType);
+ if (pkgName != null) {
+ if (pkgName.contains(".cglib")) {
+ return pkgName.startsWith("net.sf.cglib")
+ // also, as per [JACKSON-177]
+ || pkgName.startsWith("org.hibernate.repackage.cglib")
+ // and [core#674]
+ || pkgName.startsWith("org.springframework.cglib");
}
}
}
@@ -183,10 +205,7 @@
{
Class<?> argType = am.getRawParameterType(0);
String pkgName = ClassUtil.getPackageName(argType);
- if (pkgName != null && pkgName.startsWith("groovy.lang")) {
- return true;
- }
- return false;
+ return (pkgName != null) && pkgName.startsWith("groovy.lang");
}
/**
@@ -194,15 +213,8 @@
*/
protected static boolean isGroovyMetaClassGetter(AnnotatedMethod am)
{
- Class<?> rt = am.getRawType();
- if (rt == null || rt.isArray()) {
- return false;
- }
- String pkgName = ClassUtil.getPackageName(rt);
- if (pkgName != null && pkgName.startsWith("groovy.lang")) {
- return true;
- }
- return false;
+ String pkgName = ClassUtil.getPackageName(am.getRawType());
+ return (pkgName != null) && pkgName.startsWith("groovy.lang");
}
/*
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java b/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java
index 5d14255..375f3e7 100644
--- a/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java
+++ b/src/main/java/com/fasterxml/jackson/databind/util/ClassUtil.java
@@ -7,7 +7,9 @@
import java.util.*;
import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
public final class ClassUtil
@@ -16,24 +18,8 @@
private final static Annotation[] NO_ANNOTATIONS = new Annotation[0];
private final static Ctor[] NO_CTORS = new Ctor[0];
-
- /*
- /**********************************************************
- /* Helper classes
- /**********************************************************
- */
- /* 21-Feb-2016, tatu: Unfortunately `Collections.emptyIterator()` only
- * comes with JDK7, so we'll still have to include our bogus implementation
- * for as long as we want JDK6 runtime compatibility
- */
- private final static class EmptyIterator<T> implements Iterator<T> {
- @Override public boolean hasNext() { return false; }
- @Override public T next() { throw new NoSuchElementException(); }
- @Override public void remove() { throw new UnsupportedOperationException(); }
- }
-
- private final static EmptyIterator<?> EMPTY_ITERATOR = new EmptyIterator<Object>();
+ private final static Iterator<?> EMPTY_ITERATOR = Collections.emptyIterator();
/*
/**********************************************************
@@ -46,8 +32,6 @@
*/
@SuppressWarnings("unchecked")
public static <T> Iterator<T> emptyIterator() {
-// 21-Feb-2016, tatu: As per above, use a locally defined empty iterator
-// return Collections.emptyIterator();
return (Iterator<T>) EMPTY_ITERATOR;
}
@@ -288,73 +272,43 @@
return false;
}
- /*
- /**********************************************************
- /* Type name handling methods
- /**********************************************************
- */
-
- /**
- * Helper method used to construct appropriate description
- * when passed either type (Class) or an instance; in latter
- * case, class of instance is to be used.
- */
- public static String getClassDescription(Object classOrInstance)
- {
- if (classOrInstance == null) {
- return "unknown";
- }
- Class<?> cls = (classOrInstance instanceof Class<?>) ?
- (Class<?>) classOrInstance : classOrInstance.getClass();
- return cls.getName();
+ public static boolean isBogusClass(Class<?> cls) {
+ return (cls == Void.class || cls == Void.TYPE
+ || cls == com.fasterxml.jackson.databind.annotation.NoClass.class);
}
- /*
- /**********************************************************
- /* Class loading
- /**********************************************************
- */
+ public static boolean isNonStaticInnerClass(Class<?> cls) {
+ return !Modifier.isStatic(cls.getModifiers())
+ && (getEnclosingClass(cls) != null);
+ }
/**
- * @deprecated Since 2.6, use method in {@link com.fasterxml.jackson.databind.type.TypeFactory}.
+ * @since 2.7
*/
- @Deprecated
- public static Class<?> findClass(String className) throws ClassNotFoundException
+ public static boolean isObjectOrPrimitive(Class<?> cls) {
+ return (cls == CLS_OBJECT) || cls.isPrimitive();
+ }
+
+ /**
+ * @since 2.9
+ */
+ public static boolean hasClass(Object inst, Class<?> raw) {
+ // 10-Nov-2016, tatu: Could use `Class.isInstance()` if we didn't care
+ // about being exactly that type
+ return (inst != null) && (inst.getClass() == raw);
+ }
+
+ /**
+ * @since 2.9
+ */
+ public static void verifyMustOverride(Class<?> expType, Object instance,
+ String method)
{
- // [JACKSON-597]: support primitive types (and void)
- if (className.indexOf('.') < 0) {
- if ("int".equals(className)) return Integer.TYPE;
- if ("long".equals(className)) return Long.TYPE;
- if ("float".equals(className)) return Float.TYPE;
- if ("double".equals(className)) return Double.TYPE;
- if ("boolean".equals(className)) return Boolean.TYPE;
- if ("byte".equals(className)) return Byte.TYPE;
- if ("char".equals(className)) return Character.TYPE;
- if ("short".equals(className)) return Short.TYPE;
- if ("void".equals(className)) return Void.TYPE;
+ if (instance.getClass() != expType) {
+ throw new IllegalStateException(String.format(
+ "Sub-class %s (of class %s) must override method '%s'",
+ instance.getClass().getName(), expType.getName(), method));
}
- // Two-phase lookup: first using context ClassLoader; then default
- Throwable prob = null;
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
-
- if (loader != null) {
- try {
- return Class.forName(className, true, loader);
- } catch (Exception e) {
- prob = getRootCause(e);
- }
- }
- try {
- return Class.forName(className);
- } catch (Exception e) {
- if (prob == null) {
- prob = getRootCause(e);
- }
- }
- if (prob instanceof RuntimeException) {
- throw (RuntimeException) prob;
- }
- throw new ClassNotFoundException(prob.getMessage(), prob);
}
/*
@@ -388,11 +342,56 @@
/*
/**********************************************************
- /* Exception handling
+ /* Exception handling; simple re-throw
/**********************************************************
*/
/**
+ * Helper method that will check if argument is an {@link Error},
+ * and if so, (re)throw it; otherwise just return
+ *
+ * @since 2.9
+ */
+ public static Throwable throwIfError(Throwable t) {
+ if (t instanceof Error) {
+ throw (Error) t;
+ }
+ return t;
+ }
+
+ /**
+ * Helper method that will check if argument is an {@link RuntimeException},
+ * and if so, (re)throw it; otherwise just return
+ *
+ * @since 2.9
+ */
+ public static Throwable throwIfRTE(Throwable t) {
+ if (t instanceof RuntimeException) {
+ throw (RuntimeException) t;
+ }
+ return t;
+ }
+
+ /**
+ * Helper method that will check if argument is an {@link IOException},
+ * and if so, (re)throw it; otherwise just return
+ *
+ * @since 2.9
+ */
+ public static Throwable throwIfIOE(Throwable t) throws IOException {
+ if (t instanceof IOException) {
+ throw (IOException) t;
+ }
+ return t;
+ }
+
+ /*
+ /**********************************************************
+ /* Exception handling; other
+ /**********************************************************
+ */
+
+ /**
* Method that can be used to find the "root cause", innermost
* of chained (wrapped) exceptions.
*/
@@ -405,41 +404,21 @@
}
/**
- * Method that will unwrap root causes of given Throwable, and throw
- * the innermost {@link Exception} or {@link Error} as is.
- * This is useful in cases where mandatory wrapping is added, which
- * is often done by Reflection API.
- */
- public static void throwRootCause(Throwable t) throws Exception
- {
- t = getRootCause(t);
- if (t instanceof Exception) {
- throw (Exception) t;
- }
- throw (Error) t;
- }
-
- /**
- * Method that works like {@link #throwRootCause} if (and only if)
- * root cause is an {@link IOException}; otherwise returns root cause
+ * Method that works like by calling {@link #getRootCause} and then
+ * either throwing it (if instanceof {@link IOException}), or
+ * return.
*
* @since 2.8
*/
- public static Throwable throwRootCauseIfIOE(Throwable t) throws IOException
- {
- t = getRootCause(t);
- if (t instanceof IOException) {
- throw (IOException) t;
- }
- return t;
+ public static Throwable throwRootCauseIfIOE(Throwable t) throws IOException {
+ return throwIfIOE(getRootCause(t));
}
/**
* Method that will wrap 't' as an {@link IllegalArgumentException} if it
* is a checked exception; otherwise (runtime exception or error) throw as is
*/
- public static void throwAsIAE(Throwable t)
- {
+ public static void throwAsIAE(Throwable t) {
throwAsIAE(t, t.getMessage());
}
@@ -450,16 +429,25 @@
*/
public static void throwAsIAE(Throwable t, String msg)
{
- if (t instanceof RuntimeException) {
- throw (RuntimeException) t;
- }
- if (t instanceof Error) {
- throw (Error) t;
- }
+ throwIfRTE(t);
+ throwIfError(t);
throw new IllegalArgumentException(msg, t);
}
/**
+ * @since 2.9
+ */
+ public static <T> T throwAsMappingException(DeserializationContext ctxt,
+ IOException e0) throws JsonMappingException {
+ if (e0 instanceof JsonMappingException) {
+ throw (JsonMappingException) e0;
+ }
+ JsonMappingException e = JsonMappingException.from(ctxt, e0.getMessage());
+ e.initCause(e0);
+ throw e;
+ }
+
+ /**
* Method that will locate the innermost exception for given Throwable;
* and then wrap it as an {@link IllegalArgumentException} if it
* is a checked exception; otherwise (runtime exception or error) throw as is
@@ -500,12 +488,8 @@
} catch (Exception e) {
fail.addSuppressed(e);
}
- if (fail instanceof IOException) {
- throw (IOException) fail;
- }
- if (fail instanceof RuntimeException) {
- throw (RuntimeException) fail;
- }
+ throwIfIOE(fail);
+ throwIfRTE(fail);
throw new RuntimeException(fail);
}
@@ -537,12 +521,8 @@
fail.addSuppressed(e);
}
}
- if (fail instanceof IOException) {
- throw (IOException) fail;
- }
- if (fail instanceof RuntimeException) {
- throw (RuntimeException) fail;
- }
+ throwIfIOE(fail);
+ throwIfRTE(fail);
throw new RuntimeException(fail);
}
@@ -580,13 +560,13 @@
}
}
- public static <T> Constructor<T> findConstructor(Class<T> cls, boolean canFixAccess)
+ public static <T> Constructor<T> findConstructor(Class<T> cls, boolean forceAccess)
throws IllegalArgumentException
{
try {
Constructor<T> ctor = cls.getDeclaredConstructor();
- if (canFixAccess) {
- checkAndFixAccess(ctor);
+ if (forceAccess) {
+ checkAndFixAccess(ctor, forceAccess);
} else {
// Has to be public...
if (!Modifier.isPublic(ctor.getModifiers())) {
@@ -604,6 +584,125 @@
/*
/**********************************************************
+ /* Class name, description access
+ /**********************************************************
+ */
+
+ /**
+ * @since 2.9
+ */
+ public static Class<?> classOf(Object inst) {
+ if (inst == null) {
+ return null;
+ }
+ return inst.getClass();
+ }
+
+ /**
+ * @since 2.9
+ */
+ public static <T> T nonNull(T valueOrNull, T defaultValue) {
+ return (valueOrNull == null) ? defaultValue : valueOrNull;
+ }
+
+ /**
+ * @since 2.9
+ */
+ public static String nullOrToString(Object value) {
+ if (value == null) {
+ return null;
+ }
+ return value.toString();
+ }
+
+ /**
+ * @since 2.9
+ */
+ public static String nonNullString(String str) {
+ if (str == null) {
+ return "";
+ }
+ return str;
+ }
+
+ /**
+ * Returns either quoted value (with double-quotes) -- if argument non-null
+ * String -- or String NULL (no quotes) (if null).
+ *
+ * @since 2.9
+ */
+ public static String quotedOr(Object str, String forNull) {
+ if (str == null) {
+ return forNull;
+ }
+ return String.format("\"%s\"", str);
+ }
+
+ /*
+ /**********************************************************
+ /* Type name handling methods
+ /**********************************************************
+ */
+
+ /**
+ * Helper method used to construct appropriate description
+ * when passed either type (Class) or an instance; in latter
+ * case, class of instance is to be used.
+ */
+ public static String getClassDescription(Object classOrInstance)
+ {
+ if (classOrInstance == null) {
+ return "unknown";
+ }
+ Class<?> cls = (classOrInstance instanceof Class<?>) ?
+ (Class<?>) classOrInstance : classOrInstance.getClass();
+ return cls.getName();
+ }
+
+ /**
+ * @since 2.9
+ */
+ public static String classNameOf(Object inst) {
+ if (inst == null) {
+ return "[null]";
+ }
+ return inst.getClass().getName();
+ }
+
+ /**
+ * Returns either `cls.getName()` (if `cls` not null),
+ * or "[null]" if `cls` is null.
+ *
+ * @since 2.9
+ */
+ public static String nameOf(Class<?> cls) {
+ if (cls == null) {
+ return "[null]";
+ }
+ if (cls.isArray()) {
+ return nameOf(cls.getComponentType())+"[]";
+ }
+ if (cls.isPrimitive()) {
+ cls.getSimpleName();
+ }
+ return cls.getName();
+ }
+
+ /**
+ * Returns either (double-)quoted `named.getName()` (if `named` not null),
+ * or "[null]" if `named` is null.
+ *
+ * @since 2.9
+ */
+ public static String nameOf(Named named) {
+ if (named == null) {
+ return "[null]";
+ }
+ return String.format("'%s'", named.getName());
+ }
+
+ /*
+ /**********************************************************
/* Primitive type support
/**********************************************************
*/
@@ -873,39 +972,25 @@
/* Jackson-specific stuff
/**********************************************************
*/
-
+
/**
* Method that can be called to determine if given Object is the default
* implementation Jackson uses; as opposed to a custom serializer installed by
* a module or calling application. Determination is done using
* {@link JacksonStdImpl} annotation on handler (serializer, deserializer etc)
* class.
+ *<p>
+ * NOTE: passing `null` is legal, and will result in <code>true</code>
+ * being returned.
*/
public static boolean isJacksonStdImpl(Object impl) {
- return (impl != null) && isJacksonStdImpl(impl.getClass());
+ return (impl == null) || isJacksonStdImpl(impl.getClass());
}
public static boolean isJacksonStdImpl(Class<?> implClass) {
return (implClass.getAnnotation(JacksonStdImpl.class) != null);
}
- public static boolean isBogusClass(Class<?> cls) {
- return (cls == Void.class || cls == Void.TYPE
- || cls == com.fasterxml.jackson.databind.annotation.NoClass.class);
- }
-
- public static boolean isNonStaticInnerClass(Class<?> cls) {
- return !Modifier.isStatic(cls.getModifiers())
- && (getEnclosingClass(cls) != null);
- }
-
- /**
- * @since 2.7
- */
- public static boolean isObjectOrPrimitive(Class<?> cls) {
- return (cls == CLS_OBJECT) || cls.isPrimitive();
- }
-
/*
/**********************************************************
/* Access to various Class definition aspects; possibly
@@ -955,6 +1040,36 @@
}
/**
+ * Helper method that gets methods declared in given class; usually a simple thing,
+ * but sometimes (as per [databind#785]) more complicated, depending on classloader
+ * setup.
+ *
+ * @since 2.9
+ */
+ public static Method[] getClassMethods(Class<?> cls)
+ {
+ try {
+ return ClassUtil.getDeclaredMethods(cls);
+ } catch (final NoClassDefFoundError ex) {
+ // One of the methods had a class that was not found in the cls.getClassLoader.
+ // Maybe the developer was nice and has a different class loader for this context.
+ final ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if (loader == null){
+ // Nope... this is going to end poorly
+ throw ex;
+ }
+ final Class<?> contextClass;
+ try {
+ contextClass = loader.loadClass(cls.getName());
+ } catch (ClassNotFoundException e) {
+ ex.addSuppressed(e);
+ throw ex;
+ }
+ return contextClass.getDeclaredMethods(); // Cross fingers
+ }
+ }
+
+ /**
* @since 2.7
*/
public static Ctor[] getConstructors(Class<?> cls) {
@@ -1132,7 +1247,6 @@
return _ctor.getDeclaringClass();
}
- // Modest boost: maybe 1%?
public Annotation[] getDeclaredAnnotations() {
Annotation[] result = _annotations;
if (result == null) {
@@ -1142,7 +1256,6 @@
return result;
}
- // Modest boost: maybe 1%?
public Annotation[][] getParameterAnnotations() {
Annotation[][] result = _paramAnnotations;
if (result == null) {
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/CompactStringObjectMap.java b/src/main/java/com/fasterxml/jackson/databind/util/CompactStringObjectMap.java
index 022248b..f27c0c7 100644
--- a/src/main/java/com/fasterxml/jackson/databind/util/CompactStringObjectMap.java
+++ b/src/main/java/com/fasterxml/jackson/databind/util/CompactStringObjectMap.java
@@ -124,6 +124,22 @@
return null;
}
+ /**
+ * @since 2.9
+ */
+ public Object findCaseInsensitive(String key) {
+ for (int i = 0, end = _hashArea.length; i < end; i += 2) {
+ Object k2 = _hashArea[i];
+ if (k2 != null) {
+ String s = (String) k2;
+ if (s.equalsIgnoreCase(key)) {
+ return _hashArea[i+1];
+ }
+ }
+ }
+ return null;
+ }
+
public List<String> keys() {
final int end = _hashArea.length;
List<String> keys = new ArrayList<String>(end >> 2);
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/EnumResolver.java b/src/main/java/com/fasterxml/jackson/databind/util/EnumResolver.java
index d6ef5e6..f95ea55 100644
--- a/src/main/java/com/fasterxml/jackson/databind/util/EnumResolver.java
+++ b/src/main/java/com/fasterxml/jackson/databind/util/EnumResolver.java
@@ -1,9 +1,9 @@
package com.fasterxml.jackson.databind.util;
-import java.lang.reflect.Method;
import java.util.*;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
+import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
/**
* Helper class used to resolve String values (either JSON Object field
@@ -83,17 +83,10 @@
}
/**
- * @deprecated Since 2.8, use {@link #constructUsingMethod(Class, Method, AnnotationIntrospector)} instead
+ * @since 2.9
*/
- @Deprecated
- public static EnumResolver constructUsingMethod(Class<Enum<?>> enumCls, Method accessor) {
- return constructUsingMethod(enumCls, accessor, null);
- }
-
- /**
- * @since 2.8
- */
- public static EnumResolver constructUsingMethod(Class<Enum<?>> enumCls, Method accessor,
+ public static EnumResolver constructUsingMethod(Class<Enum<?>> enumCls,
+ AnnotatedMember accessor,
AnnotationIntrospector ai)
{
Enum<?>[] enumValues = enumCls.getEnumConstants();
@@ -102,7 +95,7 @@
for (int i = enumValues.length; --i >= 0; ) {
Enum<?> en = enumValues[i];
try {
- Object o = accessor.invoke(en);
+ Object o = accessor.getValue(en);
if (o != null) {
map.put(o.toString(), en);
}
@@ -129,15 +122,6 @@
}
/**
- * @deprecated Since 2.8, use {@link #constructUnsafeUsingToString(Class, AnnotationIntrospector)} instead
- */
- @Deprecated
- public static EnumResolver constructUnsafeUsingToString(Class<?> rawEnumCls)
- {
- return constructUnsafeUsingToString(rawEnumCls, null);
- }
-
- /**
* Method that needs to be used instead of {@link #constructUsingToString}
* if static type of enum is not known.
*
@@ -153,21 +137,14 @@
}
/**
- * @deprecated Since 2.8, use {@link #constructUnsafeUsingMethod(Class, Method, AnnotationIntrospector)} instead.
- */
- @Deprecated
- public static EnumResolver constructUnsafeUsingMethod(Class<?> rawEnumCls, Method accessor) {
- return constructUnsafeUsingMethod(rawEnumCls, accessor, null);
- }
-
- /**
* Method used when actual String serialization is indicated using @JsonValue
* on a method.
*
- * @since 2.8
+ * @since 2.9
*/
@SuppressWarnings({ "unchecked" })
- public static EnumResolver constructUnsafeUsingMethod(Class<?> rawEnumCls, Method accessor,
+ public static EnumResolver constructUnsafeUsingMethod(Class<?> rawEnumCls,
+ AnnotatedMember accessor,
AnnotationIntrospector ai)
{
// wrong as ever but:
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/ObjectBuffer.java b/src/main/java/com/fasterxml/jackson/databind/util/ObjectBuffer.java
index f04f816..034e487 100644
--- a/src/main/java/com/fasterxml/jackson/databind/util/ObjectBuffer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/util/ObjectBuffer.java
@@ -5,7 +5,7 @@
/**
* Helper class to use for constructing Object arrays by appending entries
- * to create arrays of various lengths (length that is not known a priori).
+ * to create arrays of various lengths (length that is not known a priori).
*/
public final class ObjectBuffer
{
@@ -67,12 +67,25 @@
{
_reset();
if (_freeBuffer == null) {
- return new Object[12];
+ return (_freeBuffer = new Object[12]);
}
return _freeBuffer;
}
/**
+ * @since 2.9
+ */
+ public Object[] resetAndStart(Object[] base, int count)
+ {
+ _reset();
+ if ((_freeBuffer == null) || (_freeBuffer.length < count)) {
+ _freeBuffer = new Object[Math.max(12, count)];
+ }
+ System.arraycopy(base, 0, _freeBuffer, 0, count);
+ return _freeBuffer;
+ }
+
+ /**
* Method called to add a full Object array as a chunk buffered within
* this buffer, and to obtain a new array to fill. Caller is not to use
* the array it gives; but to use the returned array for continued
@@ -121,6 +134,7 @@
int totalSize = lastChunkEntries + _size;
Object[] result = new Object[totalSize];
_copyTo(result, totalSize, lastChunk, lastChunkEntries);
+ _reset();
return result;
}
@@ -154,6 +168,7 @@
for (int i = 0; i < lastChunkEntries; ++i) {
resultList.add(lastChunk[i]);
}
+ _reset();
}
/**
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/RawValue.java b/src/main/java/com/fasterxml/jackson/databind/util/RawValue.java
index b9f1b02..aea4427 100644
--- a/src/main/java/com/fasterxml/jackson/databind/util/RawValue.java
+++ b/src/main/java/com/fasterxml/jackson/databind/util/RawValue.java
@@ -119,7 +119,6 @@
@Override
public String toString() {
- return String.format("[RawValue of type %s]",
- (_value == null) ? "NULL" : _value.getClass().getName());
+ return String.format("[RawValue of type %s]", ClassUtil.classNameOf(_value));
}
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/SimpleBeanPropertyDefinition.java b/src/main/java/com/fasterxml/jackson/databind/util/SimpleBeanPropertyDefinition.java
index 575ad68..cd9cdf2 100644
--- a/src/main/java/com/fasterxml/jackson/databind/util/SimpleBeanPropertyDefinition.java
+++ b/src/main/java/com/fasterxml/jackson/databind/util/SimpleBeanPropertyDefinition.java
@@ -4,11 +4,11 @@
import java.util.Iterator;
import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.databind.AnnotationIntrospector;
-import com.fasterxml.jackson.databind.PropertyMetadata;
-import com.fasterxml.jackson.databind.PropertyName;
+
+import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.*;
+import com.fasterxml.jackson.databind.type.TypeFactory;
/**
* Simple immutable {@link BeanPropertyDefinition} implementation that can
@@ -19,7 +19,7 @@
public class SimpleBeanPropertyDefinition
extends BeanPropertyDefinition
{
- protected final AnnotationIntrospector _introspector;
+ protected final AnnotationIntrospector _annotationIntrospector;
/**
* Member that defines logical property. Assumption is that it
@@ -45,67 +45,34 @@
*/
protected final JsonInclude.Value _inclusion;
- /**
- * @deprecated Since 2.5 use <code>_fullName</code> instead.
- */
- @Deprecated
- protected final String _name;
-
/*
/**********************************************************
/* Construction
/**********************************************************
*/
- protected SimpleBeanPropertyDefinition(AnnotatedMember member, PropertyName fullName,
- AnnotationIntrospector intr, PropertyMetadata metadata,
- JsonInclude.Include inclusion)
- {
- this(member, fullName, intr, metadata,
- ((inclusion == null) || (inclusion == JsonInclude.Include.USE_DEFAULTS)
- ? EMPTY_INCLUDE : JsonInclude.Value.construct(inclusion, null)));
- }
-
- protected SimpleBeanPropertyDefinition(AnnotatedMember member, PropertyName fullName,
- AnnotationIntrospector intr, PropertyMetadata metadata,
+ /**
+ * @since 2.9
+ */
+ protected SimpleBeanPropertyDefinition(AnnotationIntrospector intr,
+ AnnotatedMember member, PropertyName fullName, PropertyMetadata metadata,
JsonInclude.Value inclusion)
{
- _introspector = intr;
+ _annotationIntrospector = intr;
_member = member;
_fullName = fullName;
- _name = fullName.getSimpleName();
_metadata = (metadata == null) ? PropertyMetadata.STD_OPTIONAL: metadata;
_inclusion = inclusion;
}
/**
- * @deprecated Since 2.5 Use variant that takes PropertyName
- */
- @Deprecated
- protected SimpleBeanPropertyDefinition(AnnotatedMember member, String name,
- AnnotationIntrospector intr) {
- this(member, new PropertyName(name), intr, null, EMPTY_INCLUDE);
- }
-
- /**
* @since 2.2
*/
public static SimpleBeanPropertyDefinition construct(MapperConfig<?> config,
- AnnotatedMember member) {
- return new SimpleBeanPropertyDefinition(member, PropertyName.construct(member.getName()),
- (config == null) ? null : config.getAnnotationIntrospector(),
- null, EMPTY_INCLUDE);
- }
-
- /**
- * @deprecated Since 2.5
- */
- @Deprecated
- public static SimpleBeanPropertyDefinition construct(MapperConfig<?> config,
- AnnotatedMember member, String name) {
- return new SimpleBeanPropertyDefinition(member, PropertyName.construct(name),
- (config == null) ? null : config.getAnnotationIntrospector(),
- null, EMPTY_INCLUDE);
+ AnnotatedMember member)
+ {
+ return new SimpleBeanPropertyDefinition(config.getAnnotationIntrospector(),
+ member, PropertyName.construct(member.getName()), null, EMPTY_INCLUDE);
}
/**
@@ -117,14 +84,19 @@
}
/**
+ * Method called to create instance for virtual properties.
+ *
* @since 2.5
*/
public static SimpleBeanPropertyDefinition construct(MapperConfig<?> config,
AnnotatedMember member, PropertyName name, PropertyMetadata metadata,
- JsonInclude.Include inclusion) {
- return new SimpleBeanPropertyDefinition(member, name,
- (config == null) ? null : config.getAnnotationIntrospector(),
- metadata, inclusion);
+ JsonInclude.Include inclusion)
+ {
+ JsonInclude.Value inclValue
+ = ((inclusion == null) || (inclusion == JsonInclude.Include.USE_DEFAULTS))
+ ? EMPTY_INCLUDE : JsonInclude.Value.construct(inclusion, null);
+ return new SimpleBeanPropertyDefinition(config.getAnnotationIntrospector(),
+ member, name, metadata, inclValue);
}
/**
@@ -133,9 +105,8 @@
public static SimpleBeanPropertyDefinition construct(MapperConfig<?> config,
AnnotatedMember member, PropertyName name, PropertyMetadata metadata,
JsonInclude.Value inclusion) {
- return new SimpleBeanPropertyDefinition(member, name,
- (config == null) ? null : config.getAnnotationIntrospector(),
- metadata, inclusion);
+ return new SimpleBeanPropertyDefinition(config.getAnnotationIntrospector(),
+ member, name, metadata, inclusion);
}
/*
@@ -144,19 +115,13 @@
/**********************************************************
*/
- // Note: removed from base class in 2.6; left here until 2.7
- @Deprecated // since 2.3 (remove in 2.7)
- public BeanPropertyDefinition withName(String newName) {
- return withSimpleName(newName);
- }
-
@Override
public BeanPropertyDefinition withSimpleName(String newName) {
if (_fullName.hasSimpleName(newName) && !_fullName.hasNamespace()) {
return this;
}
- return new SimpleBeanPropertyDefinition(_member, new PropertyName(newName),
- _introspector, _metadata, _inclusion);
+ return new SimpleBeanPropertyDefinition(_annotationIntrospector,
+ _member, new PropertyName(newName), _metadata, _inclusion);
}
@Override
@@ -164,8 +129,8 @@
if (_fullName.equals(newName)) {
return this;
}
- return new SimpleBeanPropertyDefinition(_member, newName,
- _introspector, _metadata, _inclusion);
+ return new SimpleBeanPropertyDefinition(_annotationIntrospector,
+ _member, newName, _metadata, _inclusion);
}
/**
@@ -175,8 +140,8 @@
if (metadata.equals(_metadata)) {
return this;
}
- return new SimpleBeanPropertyDefinition(_member, _fullName,
- _introspector, metadata, _inclusion);
+ return new SimpleBeanPropertyDefinition(_annotationIntrospector,
+ _member, _fullName, metadata, _inclusion);
}
/**
@@ -186,8 +151,8 @@
if (_inclusion == inclusion) {
return this;
}
- return new SimpleBeanPropertyDefinition(_member, _fullName,
- _introspector, _metadata, inclusion);
+ return new SimpleBeanPropertyDefinition(_annotationIntrospector,
+ _member, _fullName, _metadata, inclusion);
}
/*
@@ -212,8 +177,10 @@
@Override
public PropertyName getWrapperName() {
- return ((_introspector == null) && (_member != null))
- ? null : _introspector.findWrapperName(_member);
+ if ((_annotationIntrospector == null) || (_member == null)) {
+ return null;
+ }
+ return _annotationIntrospector.findWrapperName(_member);
}
// hmmh. what should we claim here?
@@ -231,6 +198,22 @@
}
@Override
+ public JavaType getPrimaryType() {
+ if (_member == null) {
+ return TypeFactory.unknownType();
+ }
+ return _member.getType();
+ }
+
+ @Override
+ public Class<?> getRawPrimaryType() {
+ if (_member == null) {
+ return Object.class;
+ }
+ return _member.getRawType();
+ }
+
+ @Override
public JsonInclude.Value findInclusion() {
return _inclusion;
}
@@ -290,46 +273,6 @@
return Collections.singleton(param).iterator();
}
- /**
- * Method used to find accessor (getter, field to access) to use for accessing
- * value of the property.
- * Null if no such member exists.
- */
- @Override
- public AnnotatedMember getAccessor() {
- AnnotatedMember acc = getGetter();
- if (acc == null) {
- acc = getField();
- }
- return acc;
- }
-
- /**
- * Method used to find mutator (constructor parameter, setter, field) to use for
- * changing value of the property.
- * Null if no such member exists.
- */
- @Override
- public AnnotatedMember getMutator() {
- AnnotatedMember acc = getConstructorParameter();
- if (acc == null) {
- acc = getSetter();
- if (acc == null) {
- acc = getField();
- }
- }
- return acc;
- }
-
- @Override
- public AnnotatedMember getNonConstructorMutator() {
- AnnotatedMember acc = getSetter();
- if (acc == null) {
- acc = getField();
- }
- return acc;
- }
-
@Override
public AnnotatedMember getPrimaryMember() { return _member; }
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/StdDateFormat.java b/src/main/java/com/fasterxml/jackson/databind/util/StdDateFormat.java
index 8bc7921..172ff22 100644
--- a/src/main/java/com/fasterxml/jackson/databind/util/StdDateFormat.java
+++ b/src/main/java/com/fasterxml/jackson/databind/util/StdDateFormat.java
@@ -171,7 +171,17 @@
}
return new StdDateFormat(_timezone, loc, _lenient);
}
-
+
+ /**
+ * @since 2.9
+ */
+ public StdDateFormat withLenient(Boolean b) {
+ if (_equals(b, _lenient)) {
+ return this;
+ }
+ return new StdDateFormat(_timezone, _locale, b);
+ }
+
@Override
public StdDateFormat clone() {
/* Although there is that much state to share, we do need to
@@ -211,14 +221,6 @@
tz, loc, null);
}
- /**
- * @deprecated Since 2.4; use variant that takes Locale
- */
- @Deprecated
- public static DateFormat getRFC1123Format(TimeZone tz) {
- return getRFC1123Format(tz, DEFAULT_LOCALE);
- }
-
/*
/**********************************************************
/* Public API, configuration
@@ -249,8 +251,8 @@
*/
@Override // since 2.7
public void setLenient(boolean enabled) {
- Boolean newValue = enabled;
- if (_lenient != newValue) {
+ Boolean newValue = Boolean.valueOf(enabled);
+ if (!_equals(newValue, _lenient)) {
_lenient = newValue;
// and since leniency settings may have been used:
_clearFormats();
@@ -259,11 +261,8 @@
@Override // since 2.7
public boolean isLenient() {
- if (_lenient == null) {
- // default is, I believe, true
- return true;
- }
- return _lenient.booleanValue();
+ // default is, I believe, true
+ return (_lenient == null) || _lenient.booleanValue();
}
/*
@@ -375,16 +374,25 @@
/* Std overrides
/**********************************************************
*/
-
+
@Override
public String toString() {
- String str = "DateFormat "+getClass().getName();
- TimeZone tz = _timezone;
- if (tz != null) {
- str += " (timezone: "+tz+")";
- }
- str += "(locale: "+_locale+")";
- return str;
+ return String.format("DateFormat %s: (timezone: %s, locale: %s)",
+ getClass().getName(), _timezone, _locale);
+ }
+
+ public String toPattern() { // same as SimpleDateFormat
+ StringBuilder sb = new StringBuilder(100);
+ sb.append("[one of: '")
+ .append(DATE_FORMAT_STR_ISO8601)
+ .append("', '")
+ .append(DATE_FORMAT_STR_RFC1123)
+ .append("' (")
+ ;
+ sb.append(Boolean.FALSE.equals(_lenient) ?
+ "strict" : "lenient")
+ .append(")]");
+ return sb.toString();
}
@Override // since 2.7[.2], as per [databind#1130]
@@ -403,6 +411,13 @@
/**********************************************************
*/
+ protected static <T> boolean _equals(T value1, T value2) {
+ if (value1 == value2) {
+ return true;
+ }
+ return (value1 != null) && value1.equals(value2);
+ }
+
/**
* Overridable helper method used to figure out which of supported
* formats is the likeliest match.
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java b/src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java
index 1f05c32..0d3a4af 100644
--- a/src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java
+++ b/src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java
@@ -7,7 +7,6 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.base.ParserMinimalBase;
-import com.fasterxml.jackson.core.json.JsonReadContext;
import com.fasterxml.jackson.core.json.JsonWriteContext;
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
import com.fasterxml.jackson.databind.*;
@@ -46,6 +45,14 @@
protected ObjectCodec _objectCodec;
/**
+ * Parse context from "parent" parser (one from which content to buffer is read,
+ * if specified). Used, if available, when reading content, to present full
+ * context as if content was read from the original parser: this is useful
+ * in error reporting and sometimes processing as well.
+ */
+ protected JsonStreamContext _parentContext;
+
+ /**
* Bit flag composed of bits that indicate which
* {@link com.fasterxml.jackson.core.JsonGenerator.Feature}s
* are enabled.
@@ -136,18 +143,6 @@
* @param codec Object codec to use for stream-based object
* conversion through parser/generator interfaces. If null,
* such methods can not be used.
- *
- * @deprecated since 2.3 preferred variant is one that takes {@link JsonParser} or additional boolean parameter.
- */
- @Deprecated
- public TokenBuffer(ObjectCodec codec) {
- this(codec, false);
- }
-
- /**
- * @param codec Object codec to use for stream-based object
- * conversion through parser/generator interfaces. If null,
- * such methods can not be used.
* @param hasNativeIds Whether resulting {@link JsonParser} (if created)
* is considered to support native type and object ids
*/
@@ -178,6 +173,7 @@
public TokenBuffer(JsonParser p, DeserializationContext ctxt)
{
_objectCodec = p.getCodec();
+ _parentContext = p.getParsingContext();
_generatorFeatures = DEFAULT_GENERATOR_FEATURES;
_writeContext = JsonWriteContext.createRootContext(null);
// at first we have just one segment
@@ -191,6 +187,35 @@
}
/**
+ * Convenience method, equivalent to:
+ *<pre>
+ * TokenBuffer b = new TokenBuffer(p);
+ * b.copyCurrentStructure(p);
+ * return b;
+ *</pre>
+ *
+ * @since 2.9
+ */
+ public static TokenBuffer asCopyOfValue(JsonParser p) throws IOException {
+ TokenBuffer b = new TokenBuffer(p);
+ b.copyCurrentStructure(p);
+ return b;
+ }
+
+ /**
+ * Method that allows explicitly specifying parent parse context to associate
+ * with contents of this buffer. Usually context is assigned at construction,
+ * based on given parser; but it is not always available, and may not contain
+ * intended context.
+ *
+ * @since 2.9
+ */
+ public TokenBuffer overrideParentContext(JsonStreamContext ctxt) {
+ _parentContext = ctxt;
+ return this;
+ }
+
+ /**
* @since 2.7
*/
public TokenBuffer forceUseOfBigDecimal(boolean b) {
@@ -213,12 +238,27 @@
*
* @return Parser that can be used for reading contents stored in this buffer
*/
- public JsonParser asParser()
- {
+ public JsonParser asParser() {
return asParser(_objectCodec);
}
/**
+ * Same as:
+ *<pre>
+ * JsonParser p = asParser();
+ * p.nextToken();
+ * return p;
+ *</pre>
+ *
+ * @since 2.9
+ */
+ public JsonParser asParserOnFirstToken() throws IOException {
+ JsonParser p = asParser(_objectCodec);
+ p.nextToken();
+ return p;
+ }
+
+ /**
* Method used to create a {@link JsonParser} that can read contents
* stored in this buffer.
*<p>
@@ -233,7 +273,7 @@
*/
public JsonParser asParser(ObjectCodec codec)
{
- return new Parser(_first, codec, _hasNativeTypeIds, _hasNativeObjectIds);
+ return new Parser(_first, codec, _hasNativeTypeIds, _hasNativeObjectIds, _parentContext);
}
/**
@@ -242,11 +282,11 @@
*/
public JsonParser asParser(JsonParser src)
{
- Parser p = new Parser(_first, src.getCodec(), _hasNativeTypeIds, _hasNativeObjectIds);
+ Parser p = new Parser(_first, src.getCodec(), _hasNativeTypeIds, _hasNativeObjectIds, _parentContext);
p.setLocation(src.getTokenLocation());
return p;
}
-
+
/*
/**********************************************************
/* Additional accessors
@@ -254,12 +294,10 @@
*/
public JsonToken firstToken() {
- if (_first != null) {
- return _first.type(0);
- }
- return null;
+ // no need to null check; never create without `_first`
+ return _first.type(0);
}
-
+
/*
/**********************************************************
/* Other custom methods not needed for implementing interfaces
@@ -291,7 +329,7 @@
}
return this;
}
-
+
/**
* Helper method that will write all contents of this buffer
* using given {@link JsonGenerator}.
@@ -453,7 +491,7 @@
copyCurrentStructure(p);
} while ((t = p.nextToken()) == JsonToken.FIELD_NAME);
if (t != JsonToken.END_OBJECT) {
- ctxt.reportWrongTokenException(p, JsonToken.END_OBJECT,
+ ctxt.reportWrongTokenException(TokenBuffer.class, JsonToken.END_OBJECT,
"Expected END_OBJECT after copying contents of a JsonParser into TokenBuffer, got "+t);
// never gets here
}
@@ -1157,7 +1195,9 @@
_appendAt = 1;
}
}
-
+
+ // 21-Oct-2016, tatu: Does not seem to be used or needed
+ /*
protected final void _appendRaw(int rawType, Object value)
{
Segment next = _hasNativeId
@@ -1170,6 +1210,7 @@
_appendAt = 1;
}
}
+ */
@Override
protected void _reportUnsupportedOperation() {
@@ -1225,7 +1266,7 @@
* Information about parser context, context in which
* the next token is to be parsed (root, array, object).
*/
- protected JsonReadContext _parsingContext;
+ protected TokenBufferReadContext _parsingContext;
protected boolean _closed;
@@ -1239,15 +1280,22 @@
/**********************************************************
*/
+ @Deprecated // since 2.9
public Parser(Segment firstSeg, ObjectCodec codec,
- boolean hasNativeTypeIds,
- boolean hasNativeObjectIds)
+ boolean hasNativeTypeIds, boolean hasNativeObjectIds)
+ {
+ this(firstSeg, codec, hasNativeTypeIds, hasNativeObjectIds, null);
+ }
+
+ public Parser(Segment firstSeg, ObjectCodec codec,
+ boolean hasNativeTypeIds, boolean hasNativeObjectIds,
+ JsonStreamContext parentContext)
{
super(0);
_segment = firstSeg;
_segmentPtr = -1; // not yet read
_codec = codec;
- _parsingContext = JsonReadContext.createRootContext(null);
+ _parsingContext = TokenBufferReadContext.createRootContext(parentContext);
_hasNativeTypeIds = hasNativeTypeIds;
_hasNativeObjectIds = hasNativeObjectIds;
_hasNativeIds = (hasNativeTypeIds | hasNativeObjectIds);
@@ -1327,17 +1375,13 @@
String name = (ob instanceof String) ? ((String) ob) : ob.toString();
_parsingContext.setCurrentName(name);
} else if (_currToken == JsonToken.START_OBJECT) {
- _parsingContext = _parsingContext.createChildObjectContext(-1, -1);
+ _parsingContext = _parsingContext.createChildObjectContext();
} else if (_currToken == JsonToken.START_ARRAY) {
- _parsingContext = _parsingContext.createChildArrayContext(-1, -1);
+ _parsingContext = _parsingContext.createChildArrayContext();
} else if (_currToken == JsonToken.END_OBJECT
|| _currToken == JsonToken.END_ARRAY) {
// Closing JSON Object/Array? Close matching context
- _parsingContext = _parsingContext.getParent();
- // but allow unbalanced cases too (more close markers)
- if (_parsingContext == null) {
- _parsingContext = JsonReadContext.createRootContext(null);
- }
+ _parsingContext = _parsingContext.parentOrCopy();
}
return _currToken;
}
@@ -1383,7 +1427,7 @@
public String getCurrentName() {
// 25-Jun-2015, tatu: as per [databind#838], needs to be same as ParserBase
if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) {
- JsonReadContext parent = _parsingContext.getParent();
+ JsonStreamContext parent = _parsingContext.getParent();
return parent.getCurrentName();
}
return _parsingContext.getCurrentName();
@@ -1393,14 +1437,16 @@
public void overrideCurrentName(String name)
{
// Simple, but need to look for START_OBJECT/ARRAY's "off-by-one" thing:
- JsonReadContext ctxt = _parsingContext;
+ JsonStreamContext ctxt = _parsingContext;
if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) {
ctxt = ctxt.getParent();
}
- try {
- ctxt.setCurrentName(name);
- } catch (IOException e) {
- throw new RuntimeException(e);
+ if (ctxt instanceof TokenBufferReadContext) {
+ try {
+ ((TokenBufferReadContext) ctxt).setCurrentName(name);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
}
}
@@ -1420,7 +1466,7 @@
if (ob instanceof String) {
return (String) ob;
}
- return (ob == null) ? null : ob.toString();
+ return ClassUtil.nullOrToString(ob);
}
if (_currToken == null) {
return null;
@@ -1428,8 +1474,7 @@
switch (_currToken) {
case VALUE_NUMBER_INT:
case VALUE_NUMBER_FLOAT:
- Object ob = _currentObject();
- return (ob == null) ? null : ob.toString();
+ return ClassUtil.nullOrToString(_currentObject());
default:
return _currToken.asString();
}
@@ -1455,7 +1500,7 @@
// We never have raw buffer available, so:
return false;
}
-
+
/*
/**********************************************************
/* Public API, access to token information, numeric
@@ -1463,6 +1508,23 @@
*/
@Override
+ public boolean isNaN() {
+ // can only occur for floating-point numbers
+ if (_currToken == JsonToken.VALUE_NUMBER_FLOAT) {
+ Object value = _currentObject();
+ if (value instanceof Double) {
+ Double v = (Double) value;
+ return v.isNaN() || v.isInfinite();
+ }
+ if (value instanceof Float) {
+ Float v = (Float) value;
+ return v.isNaN() || v.isInfinite();
+ }
+ }
+ return false;
+ }
+
+ @Override
public BigInteger getBigIntegerValue() throws IOException
{
Number n = getNumberValue();
@@ -1795,6 +1857,7 @@
return _next;
}
+ /*
public Segment appendRaw(int index, int rawTokenType, Object value)
{
if (index < TOKENS_PER_SEGMENT) {
@@ -1818,6 +1881,28 @@
return _next;
}
+ private void set(int index, int rawTokenType, Object value, Object objectId, Object typeId)
+ {
+ _tokens[index] = value;
+ long typeCode = (long) rawTokenType;
+ if (index > 0) {
+ typeCode <<= (index << 2);
+ }
+ _tokenTypes |= typeCode;
+ assignNativeIds(index, objectId, typeId);
+ }
+
+ private void set(int index, int rawTokenType, Object value)
+ {
+ _tokens[index] = value;
+ long typeCode = (long) rawTokenType;
+ if (index > 0) {
+ typeCode <<= (index << 2);
+ }
+ _tokenTypes |= typeCode;
+ }
+ */
+
private void set(int index, JsonToken tokenType)
{
/* Assumption here is that there are no overwrites, just appends;
@@ -1863,27 +1948,6 @@
assignNativeIds(index, objectId, typeId);
}
- private void set(int index, int rawTokenType, Object value)
- {
- _tokens[index] = value;
- long typeCode = (long) rawTokenType;
- if (index > 0) {
- typeCode <<= (index << 2);
- }
- _tokenTypes |= typeCode;
- }
-
- private void set(int index, int rawTokenType, Object value, Object objectId, Object typeId)
- {
- _tokens[index] = value;
- long typeCode = (long) rawTokenType;
- if (index > 0) {
- typeCode <<= (index << 2);
- }
- _tokenTypes |= typeCode;
- assignNativeIds(index, objectId, typeId);
- }
-
private final void assignNativeIds(int index, Object objectId, Object typeId)
{
if (_nativeIds == null) {
@@ -1900,14 +1964,14 @@
/**
* @since 2.3
*/
- public Object findObjectId(int index) {
+ private Object findObjectId(int index) {
return (_nativeIds == null) ? null : _nativeIds.get(_objectIdIndex(index));
}
/**
* @since 2.3
*/
- public Object findTypeId(int index) {
+ private Object findTypeId(int index) {
return (_nativeIds == null) ? null : _nativeIds.get(_typeIdIndex(index));
}
diff --git a/src/main/java/com/fasterxml/jackson/databind/util/TokenBufferReadContext.java b/src/main/java/com/fasterxml/jackson/databind/util/TokenBufferReadContext.java
new file mode 100644
index 0000000..6652036
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/databind/util/TokenBufferReadContext.java
@@ -0,0 +1,135 @@
+package com.fasterxml.jackson.databind.util;
+
+import com.fasterxml.jackson.core.*;
+import com.fasterxml.jackson.core.json.JsonReadContext;
+
+/**
+ * Implementation of {@link JsonStreamContext} used by {@link TokenBuffer}
+ * to link back to the original context to try to keep location information
+ * consistent between source location and buffered content when it's re-read
+ * from the buffer.
+ *
+ * @since 2.9
+ */
+public class TokenBufferReadContext extends JsonStreamContext
+{
+ protected final JsonStreamContext _parent;
+
+ protected final JsonLocation _startLocation;
+
+ // Benefit for reusing?
+// protected JsonReadContext _child;
+
+ /*
+ /**********************************************************
+ /* Location/state information (minus source reference)
+ /**********************************************************
+ */
+
+ protected String _currentName;
+
+ protected Object _currentValue;
+
+ protected TokenBufferReadContext(JsonStreamContext base, Object srcRef) {
+ super(base);
+ _parent = base.getParent();
+ _currentName = base.getCurrentName();
+ _currentValue = base.getCurrentValue();
+ if (base instanceof JsonReadContext) {
+ JsonReadContext rc = (JsonReadContext) base;
+ _startLocation = rc.getStartLocation(srcRef);
+ } else {
+ _startLocation = JsonLocation.NA;
+ }
+ }
+
+ protected TokenBufferReadContext(JsonStreamContext base, JsonLocation startLoc) {
+ super(base);
+ _parent = base.getParent();
+ _currentName = base.getCurrentName();
+ _currentValue = base.getCurrentValue();
+ _startLocation = startLoc;
+ }
+
+ /**
+ * Constructor for case where there is no real surrounding context: just create
+ * virtual ROOT
+ */
+ protected TokenBufferReadContext() {
+ super(TYPE_ROOT, -1);
+ _parent = null;
+ _startLocation = JsonLocation.NA;
+ }
+
+ protected TokenBufferReadContext(TokenBufferReadContext parent, int type, int index) {
+ super(type, index);
+ _parent = parent;
+ _startLocation = parent._startLocation;
+ }
+
+ @Override
+ public Object getCurrentValue() {
+ return _currentValue;
+ }
+
+ @Override
+ public void setCurrentValue(Object v) {
+ _currentValue = v;
+ }
+
+ /*
+ /**********************************************************
+ /* Factory methods
+ /**********************************************************
+ */
+
+ public static TokenBufferReadContext createRootContext(JsonStreamContext origContext) {
+ // First: possible to have no current context; if so, just create bogus ROOT context
+ if (origContext == null) {
+ return new TokenBufferReadContext();
+ }
+ return new TokenBufferReadContext(origContext, null);
+ }
+
+ public TokenBufferReadContext createChildArrayContext() {
+ return new TokenBufferReadContext(this, TYPE_ARRAY, -1);
+ }
+
+ public TokenBufferReadContext createChildObjectContext() {
+ return new TokenBufferReadContext(this, TYPE_OBJECT, -1);
+ }
+
+ /**
+ * Helper method we need to handle discontinuity between "real" contexts buffer
+ * creates, and ones from parent: problem being they are of different types.
+ */
+ public TokenBufferReadContext parentOrCopy() {
+ // 30-Apr-2017, tatu: This is bit awkward since part on ancestor stack is of different
+ // type (usually `JsonReadContext`)... and so for unbalanced buffers (with extra
+ // END_OBJECT / END_ARRAY), we may need to create
+ if (_parent instanceof TokenBufferReadContext) {
+ return (TokenBufferReadContext) _parent;
+ }
+ if (_parent == null) { // unlikely, but just in case let's support
+ return new TokenBufferReadContext();
+ }
+ return new TokenBufferReadContext(_parent, _startLocation);
+ }
+
+ /*
+ /**********************************************************
+ /* Abstract method implementation
+ /**********************************************************
+ */
+
+ @Override public String getCurrentName() { return _currentName; }
+
+ // @since 2.9
+ @Override public boolean hasCurrentName() { return _currentName != null; }
+
+ @Override public JsonStreamContext getParent() { return _parent; }
+
+ public void setCurrentName(String name) throws JsonProcessingException {
+ _currentName = name;
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java b/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java
index 1417cd5..4771d79 100644
--- a/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java
@@ -6,11 +6,9 @@
import static org.junit.Assert.*;
import com.fasterxml.jackson.annotation.JsonCreator;
-import com.fasterxml.jackson.annotation.JsonValue;
-import com.fasterxml.jackson.core.FormatSchema;
-import com.fasterxml.jackson.core.JsonGenerator;
-import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.core.JsonProcessingException;
+
+import com.fasterxml.jackson.core.*;
+
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
@@ -41,10 +39,8 @@
protected static class BooleanWrapper {
public Boolean b;
- @JsonCreator
+ public BooleanWrapper() { }
public BooleanWrapper(Boolean value) { b = value; }
-
- @JsonValue public Boolean value() { return b; }
}
protected static class IntWrapper {
@@ -109,9 +105,14 @@
{
public Map<K,V> map;
+ public MapWrapper() { }
public MapWrapper(Map<K,V> m) {
map = m;
}
+ public MapWrapper(K key, V value) {
+ map = new LinkedHashMap<>();
+ map.put(key, value);
+ }
}
protected static class ArrayWrapper<T>
@@ -209,11 +210,11 @@
protected ObjectMapper objectMapper() {
if (SHARED_MAPPER == null) {
- SHARED_MAPPER = new ObjectMapper();
+ SHARED_MAPPER = newObjectMapper();
}
return SHARED_MAPPER;
}
-
+
protected ObjectWriter objectWriter() {
return objectMapper().writer();
}
@@ -226,6 +227,11 @@
return objectMapper().readerFor(cls);
}
+ // @since 2.9
+ protected static ObjectMapper newObjectMapper() {
+ return new ObjectMapper();
+ }
+
// @since 2.7
protected TypeFactory newTypeFactory() {
// this is a work-around; no null modifier added
@@ -243,6 +249,11 @@
assertArrayEquals(exp, act);
}
+ protected void assertEquals(byte[] exp, byte[] act)
+ {
+ assertArrayEquals(exp, act);
+ }
+
/**
* Helper method for verifying 3 basic cookie cutter cases;
* identity comparison (true), and against null (false),
@@ -280,13 +291,13 @@
protected String serializeAsString(Object value)
throws IOException
{
- return serializeAsString(SHARED_MAPPER, value);
+ return serializeAsString(objectMapper(), value);
}
protected String asJSONObjectValueString(Object... args)
throws IOException
{
- return asJSONObjectValueString(SHARED_MAPPER, args);
+ return asJSONObjectValueString(objectMapper(), args);
}
protected String asJSONObjectValueString(ObjectMapper m, Object... args)
@@ -308,7 +319,7 @@
protected <T> T readAndMapFromString(String input, Class<T> cls)
throws IOException
{
- return readAndMapFromString(SHARED_MAPPER, input, cls);
+ return readAndMapFromString(objectMapper(), input, cls);
}
protected <T> T readAndMapFromString(ObjectMapper m, String input, Class<T> cls) throws IOException
diff --git a/src/test/java/com/fasterxml/jackson/databind/BaseTest.java b/src/test/java/com/fasterxml/jackson/databind/BaseTest.java
index 400cdcd..faf0146 100644
--- a/src/test/java/com/fasterxml/jackson/databind/BaseTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/BaseTest.java
@@ -59,28 +59,28 @@
public static class Name
{
- private String _first, _last;
+ private String _first, _last;
- public Name() { }
- public Name(String f, String l) {
- _first = f;
- _last = l;
- }
-
- public String getFirst() { return _first; }
- public String getLast() { return _last; }
+ public Name() { }
+ public Name(String f, String l) {
+ _first = f;
+ _last = l;
+ }
- public void setFirst(String s) { _first = s; }
- public void setLast(String s) { _last = s; }
+ public String getFirst() { return _first; }
+ public String getLast() { return _last; }
- @Override
- public boolean equals(Object o)
- {
- if (o == this) return true;
- if (o == null || o.getClass() != getClass()) return false;
- Name other = (Name) o;
- return _first.equals(other._first) && _last.equals(other._last);
- }
+ public void setFirst(String s) { _first = s; }
+ public void setLast(String s) { _last = s; }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if (o == this) return true;
+ if (o == null || o.getClass() != getClass()) return false;
+ Name other = (Name) o;
+ return _first.equals(other._first) && _last.equals(other._last);
+ }
}
private Gender _gender;
@@ -267,16 +267,6 @@
assertEquals(String.valueOf(expValue), jp.getText());
}
- /**
- * Method that checks whether Unit tests appear to run from Ant build
- * scripts.
- *
- * @since 1.6
- */
- protected static boolean runsFromAnt() {
- return "true".equals(System.getProperty("FROM_ANT"));
- }
-
/*
/**********************************************************
/* Parser/generator construction
diff --git a/src/test/java/com/fasterxml/jackson/databind/TestParserUsingMapper.java b/src/test/java/com/fasterxml/jackson/databind/MapperViaParserTest.java
similarity index 85%
rename from src/test/java/com/fasterxml/jackson/databind/TestParserUsingMapper.java
rename to src/test/java/com/fasterxml/jackson/databind/MapperViaParserTest.java
index 1b8f6b1..54400df 100644
--- a/src/test/java/com/fasterxml/jackson/databind/TestParserUsingMapper.java
+++ b/src/test/java/com/fasterxml/jackson/databind/MapperViaParserTest.java
@@ -8,7 +8,7 @@
import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.databind.ObjectMapper;
-public class TestParserUsingMapper extends BaseMapTest
+public class MapperViaParserTest extends BaseMapTest
{
final static int TWO_BYTE_ESCAPED = 0x111;
final static int THREE_BYTE_ESCAPED = 0x1111;
@@ -72,27 +72,6 @@
/********************************************************
*/
- public void testReadingArrayAsTree() throws IOException
- {
- JsonFactory jf = new MappingJsonFactory();
- final String JSON = "[ 1, 2, false ]";
-
- for (int i = 0; i < 2; ++i) {
- JsonParser jp = jf.createParser(new StringReader(JSON));
- // whether to try advancing first or not? Try both
- if (i == 0) {
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
- }
- JsonNode root = (JsonNode) jp.readValueAsTree();
- jp.close();
- assertTrue(root.isArray());
- assertEquals(3, root.size());
- assertEquals(1, root.get(0).intValue());
- assertEquals(2, root.get(1).intValue());
- assertFalse(root.get(2).booleanValue());
- }
- }
-
public void testPojoReading() throws IOException
{
JsonFactory jf = new MappingJsonFactory();
diff --git a/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java b/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java
index 02a5d76..f59eabc 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java
@@ -3,14 +3,16 @@
import java.io.*;
import java.util.*;
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonSetter;
+import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.core.*;
-
import com.fasterxml.jackson.core.util.MinimalPrettyPrinter;
-import com.fasterxml.jackson.databind.deser.DefaultDeserializationContext;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
+import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
import com.fasterxml.jackson.databind.node.*;
-import com.fasterxml.jackson.databind.type.TypeFactory;
public class ObjectMapperTest extends BaseMapTest
{
@@ -18,19 +20,12 @@
int value = 3;
public void setX(int v) { value = v; }
+
+ protected Bean() { }
+ public Bean(int v) { value = v; }
}
static class EmptyBean { }
-
- // for [databind#206]
- @SuppressWarnings("serial")
- static class CustomMapper extends ObjectMapper {
- @Override
- protected DefaultDeserializationContext createDeserializationContext(JsonParser jp,
- DeserializationConfig cfg) {
- return super.createDeserializationContext(jp, cfg);
- }
- }
@SuppressWarnings("serial")
static class MyAnnotationIntrospector extends JacksonAnnotationIntrospector { }
@@ -48,15 +43,166 @@
g.writeRaw(" , ");
}
}
-
+
+ // for [databind#206]
+ @SuppressWarnings("serial")
+ static class NoCopyMapper extends ObjectMapper { }
+
+ final ObjectMapper MAPPER = new ObjectMapper();
+
/*
/**********************************************************
- /* Test methods
+ /* Test methods, config
/**********************************************************
*/
-
- final static ObjectMapper MAPPER = new ObjectMapper();
-
+
+ public void testFactorFeatures()
+ {
+ assertTrue(MAPPER.isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES));
+ }
+
+ public void testGeneratorFeatures()
+ {
+ // and also for mapper
+ ObjectMapper mapper = new ObjectMapper();
+ assertFalse(mapper.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
+ assertTrue(mapper.isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));
+ mapper.disable(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM,
+ JsonGenerator.Feature.QUOTE_FIELD_NAMES);
+ }
+
+ public void testParserFeatures()
+ {
+ // and also for mapper
+ ObjectMapper mapper = new ObjectMapper();
+
+ assertTrue(mapper.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
+ assertFalse(mapper.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
+
+ mapper.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE,
+ JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
+ assertFalse(mapper.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, mapper.copy()
+ /**********************************************************
+ */
+
+ // [databind#28]: ObjectMapper.copy()
+ public void testCopy() throws Exception
+ {
+ ObjectMapper m = new ObjectMapper();
+ assertTrue(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
+ m.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
+ assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
+ InjectableValues inj = new InjectableValues.Std();
+ m.setInjectableValues(inj);
+ assertFalse(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
+ m.enable(JsonParser.Feature.ALLOW_COMMENTS);
+ assertTrue(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
+
+ // // First: verify that handling of features is decoupled:
+
+ ObjectMapper m2 = m.copy();
+ assertFalse(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
+ m2.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
+ assertTrue(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
+ assertSame(inj, m2.getInjectableValues());
+
+ // but should NOT change the original
+ assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
+
+ // nor vice versa:
+ assertFalse(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
+ assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
+ m.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
+ assertTrue(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
+ assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
+
+ // // Also, underlying JsonFactory instances should be distinct
+
+ assertNotSame(m.getFactory(), m2.getFactory());
+
+ // [databind#122]: Need to ensure mix-ins are not shared
+ assertEquals(0, m.getSerializationConfig().mixInCount());
+ assertEquals(0, m2.getSerializationConfig().mixInCount());
+ assertEquals(0, m.getDeserializationConfig().mixInCount());
+ assertEquals(0, m2.getDeserializationConfig().mixInCount());
+
+ m.addMixIn(String.class, Integer.class);
+ assertEquals(1, m.getSerializationConfig().mixInCount());
+ assertEquals(0, m2.getSerializationConfig().mixInCount());
+ assertEquals(1, m.getDeserializationConfig().mixInCount());
+ assertEquals(0, m2.getDeserializationConfig().mixInCount());
+
+ // [databind#913]: Ensure JsonFactory Features copied
+ assertTrue(m2.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
+ }
+
+ // [databind#1580]
+ public void testCopyOfConfigOverrides() throws Exception
+ {
+ ObjectMapper m = new ObjectMapper();
+ SerializationConfig config = m.getSerializationConfig();
+ assertEquals(JsonInclude.Value.empty(), config.getDefaultPropertyInclusion());
+ assertEquals(JsonSetter.Value.empty(), config.getDefaultSetterInfo());
+ assertNull(config.getDefaultMergeable());
+ VisibilityChecker<?> defaultVis = config.getDefaultVisibilityChecker();
+ assertEquals(VisibilityChecker.Std.class, defaultVis.getClass());
+
+ // change
+ JsonInclude.Value customIncl = JsonInclude.Value.empty().withValueInclusion(JsonInclude.Include.NON_DEFAULT);
+ m.setDefaultPropertyInclusion(customIncl);
+ JsonSetter.Value customSetter = JsonSetter.Value.forValueNulls(Nulls.SKIP);
+ m.setDefaultSetterInfo(customSetter);
+ m.setDefaultMergeable(Boolean.TRUE);
+ VisibilityChecker<?> customVis = VisibilityChecker.Std.defaultInstance()
+ .withFieldVisibility(Visibility.ANY);
+ m.setVisibility(customVis);
+ assertSame(customVis, m.getVisibilityChecker());
+
+ // and verify that copy retains these settings
+ ObjectMapper m2 = m.copy();
+ SerializationConfig config2 = m2.getSerializationConfig();
+ assertSame(customIncl, config2.getDefaultPropertyInclusion());
+ assertSame(customSetter, config2.getDefaultSetterInfo());
+ assertEquals(Boolean.TRUE, config2.getDefaultMergeable());
+ assertSame(customVis, config2.getDefaultVisibilityChecker());
+ }
+
+ public void testFailedCopy() throws Exception
+ {
+ NoCopyMapper src = new NoCopyMapper();
+ try {
+ src.copy();
+ fail("Should not pass");
+ } catch (IllegalStateException e) {
+ verifyException(e, "does not override copy()");
+ }
+ }
+
+ public void testAnnotationIntrospectorCopyin()
+ {
+ ObjectMapper m = new ObjectMapper();
+ m.setAnnotationIntrospector(new MyAnnotationIntrospector());
+ assertEquals(MyAnnotationIntrospector.class,
+ m.getDeserializationConfig().getAnnotationIntrospector().getClass());
+ ObjectMapper m2 = m.copy();
+
+ assertEquals(MyAnnotationIntrospector.class,
+ m2.getDeserializationConfig().getAnnotationIntrospector().getClass());
+ assertEquals(MyAnnotationIntrospector.class,
+ m2.getSerializationConfig().getAnnotationIntrospector().getClass());
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, other
+ /**********************************************************
+ */
+
public void testProps()
{
ObjectMapper m = new ObjectMapper();
@@ -68,25 +214,6 @@
assertSame(nf, m.getNodeFactory());
}
- public void testSupport()
- {
- assertTrue(MAPPER.canSerialize(String.class));
- assertTrue(MAPPER.canDeserialize(TypeFactory.defaultInstance().constructType(String.class)));
- }
-
- public void testTreeRead() throws Exception
- {
- String JSON = "{ }";
- JsonNode n = MAPPER.readTree(JSON);
- assertTrue(n instanceof ObjectNode);
-
- n = MAPPER.readTree(new StringReader(JSON));
- assertTrue(n instanceof ObjectNode);
-
- n = MAPPER.readTree(new ByteArrayInputStream(JSON.getBytes("UTF-8")));
- assertTrue(n instanceof ObjectNode);
- }
-
// Test to ensure that we can check property ordering defaults...
public void testConfigForPropertySorting() throws Exception
{
@@ -123,10 +250,7 @@
assertSame(f, m.getFactory());
assertSame(m, f.getCodec());
}
-
- /**
- * Test for verifying working of [JACKSON-191]
- */
+
public void testProviderConfig() throws Exception
{
ObjectMapper m = new ObjectMapper();
@@ -149,72 +273,6 @@
// 4 deserializers (int, List<?>, Map<?,?>, Object)
assertEquals(4, m._deserializationContext._cache.cachedDeserializersCount());
}
-
- // [databind#28]: ObjectMapper.copy()
- public void testCopy() throws Exception
- {
- ObjectMapper m = new ObjectMapper();
- assertTrue(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
- m.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
- assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
- InjectableValues inj = new InjectableValues.Std();
- m.setInjectableValues(inj);
- assertFalse(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
- m.enable(JsonParser.Feature.ALLOW_COMMENTS);
- assertTrue(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
-
- // // First: verify that handling of features is decoupled:
-
- ObjectMapper m2 = m.copy();
- assertFalse(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
- m2.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
- assertTrue(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
- assertSame(inj, m2.getInjectableValues());
-
- // but should NOT change the original
- assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
-
- // nor vice versa:
- assertFalse(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
- assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
- m.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
- assertTrue(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
- assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
-
- // // Also, underlying JsonFactory instances should be distinct
-
- assertNotSame(m.getFactory(), m2.getFactory());
-
- // [Issue#122]: Need to ensure mix-ins are not shared
- assertEquals(0, m.getSerializationConfig().mixInCount());
- assertEquals(0, m2.getSerializationConfig().mixInCount());
- assertEquals(0, m.getDeserializationConfig().mixInCount());
- assertEquals(0, m2.getDeserializationConfig().mixInCount());
-
- m.addMixIn(String.class, Integer.class);
- assertEquals(1, m.getSerializationConfig().mixInCount());
- assertEquals(0, m2.getSerializationConfig().mixInCount());
- assertEquals(1, m.getDeserializationConfig().mixInCount());
- assertEquals(0, m2.getDeserializationConfig().mixInCount());
-
- // [Issue#913]: Ensure JsonFactory Features copied
- assertTrue(m2.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
-
- }
-
- public void testAnnotationIntrospectorCopyin()
- {
- ObjectMapper m = new ObjectMapper();
- m.setAnnotationIntrospector(new MyAnnotationIntrospector());
- assertEquals(MyAnnotationIntrospector.class,
- m.getDeserializationConfig().getAnnotationIntrospector().getClass());
- ObjectMapper m2 = m.copy();
-
- assertEquals(MyAnnotationIntrospector.class,
- m2.getDeserializationConfig().getAnnotationIntrospector().getClass());
- assertEquals(MyAnnotationIntrospector.class,
- m2.getSerializationConfig().getAnnotationIntrospector().getClass());
- }
// For [databind#689]
public void testCustomDefaultPrettyPrinter() throws Exception
diff --git a/src/test/java/com/fasterxml/jackson/databind/seq/ObjectReaderTest.java b/src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java
similarity index 65%
rename from src/test/java/com/fasterxml/jackson/databind/seq/ObjectReaderTest.java
rename to src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java
index f64d50b..67c94de 100644
--- a/src/test/java/com/fasterxml/jackson/databind/seq/ObjectReaderTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java
@@ -1,17 +1,17 @@
-package com.fasterxml.jackson.databind.seq;
+package com.fasterxml.jackson.databind;
+import java.io.StringWriter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
-import com.fasterxml.jackson.core.FormatSchema;
-import com.fasterxml.jackson.core.JsonFactory;
-import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.core.JsonPointer;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.core.*;
+
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
+import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
+import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
public class ObjectReaderTest extends BaseMapTest
{
@@ -31,6 +31,20 @@
assertTrue(ob instanceof List<?>);
}
+ public void testSimpleAltSources() throws Exception
+ {
+ final String JSON = "[1]";
+ final byte[] BYTES = JSON.getBytes("UTF-8");
+ Object ob = MAPPER.readerFor(Object.class)
+ .readValue(BYTES);
+ assertTrue(ob instanceof List<?>);
+
+ ob = MAPPER.readerFor(Object.class)
+ .readValue(BYTES, 0, BYTES.length);
+ assertTrue(ob instanceof List<?>);
+ assertEquals(1, ((List<?>) ob).size());
+ }
+
public void testParserFeatures() throws Exception
{
final String JSON = "[ /* foo */ 7 ]";
@@ -123,30 +137,18 @@
{
JsonNodeFactory nodes = new JsonNodeFactory(true);
ObjectReader r = MAPPER.reader().with(nodes);
+ // but also no further changes if attempting again
+ assertSame(r, r.with(nodes));
assertTrue(r.createArrayNode().isArray());
assertTrue(r.createObjectNode().isObject());
}
- public void testSettings() throws Exception
+ public void testFeatureSettings() throws Exception
{
ObjectReader r = MAPPER.reader();
- assertSame(MAPPER.getFactory(), r.getFactory());
-
- JsonFactory f = new JsonFactory();
- r = r.with(f);
- assertSame(f, r.getFactory());
-
- assertNotNull(r.getTypeFactory());
- assertNull(r.getInjectableValues());
-
- r = r.withAttributes(Collections.emptyMap());
- ContextAttributes attrs = r.getAttributes();
- assertNotNull(attrs);
- assertNull(attrs.getAttribute("abc"));
-
- r = r.forType(MAPPER.constructType(String.class));
- r = r.withRootName(PropertyName.construct("foo"));
-
+ assertFalse(r.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
+ assertFalse(r.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
+
r = r.withoutFeatures(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,
DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
assertFalse(r.isEnabled(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES));
@@ -155,6 +157,56 @@
DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
assertTrue(r.isEnabled(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES));
assertTrue(r.isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE));
+
+ // alternative method too... can't recall why two
+ assertSame(r, r.with(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,
+ DeserializationFeature.FAIL_ON_INVALID_SUBTYPE));
+ }
+
+ public void testMiscSettings() throws Exception
+ {
+ ObjectReader r = MAPPER.reader();
+ assertSame(MAPPER.getFactory(), r.getFactory());
+
+ JsonFactory f = new JsonFactory();
+ r = r.with(f);
+ assertSame(f, r.getFactory());
+ assertSame(r, r.with(f));
+
+ assertNotNull(r.getTypeFactory());
+ assertNull(r.getInjectableValues());
+
+ r = r.withAttributes(Collections.emptyMap());
+ ContextAttributes attrs = r.getAttributes();
+ assertNotNull(attrs);
+ assertNull(attrs.getAttribute("abc"));
+ assertSame(r, r.withoutAttribute("foo"));
+
+ ObjectReader newR = r.forType(MAPPER.constructType(String.class));
+ assertNotSame(r, newR);
+ assertSame(newR, newR.forType(String.class));
+
+ DeserializationProblemHandler probH = new DeserializationProblemHandler() {
+ };
+ newR = r.withHandler(probH);
+ assertNotSame(r, newR);
+ assertSame(newR, newR.withHandler(probH));
+ r = newR;
+ }
+
+ @SuppressWarnings("deprecation")
+ public void testDeprecatedSettings() throws Exception
+ {
+ ObjectReader r = MAPPER.reader();
+
+ // and deprecated variants
+ ObjectReader newR = r.forType(MAPPER.constructType(String.class));
+ assertSame(newR, newR.withType(String.class));
+ assertSame(newR, newR.withType(MAPPER.constructType(String.class)));
+
+ newR = newR.withRootName(PropertyName.construct("foo"));
+ assertNotSame(r, newR);
+ assertSame(newR, newR.withRootName(PropertyName.construct("foo")));
}
public void testNoPrefetch() throws Exception
@@ -167,7 +219,44 @@
/*
/**********************************************************
- /* Test methods, failures
+ /* Test methods, ObjectCodec
+ /**********************************************************
+ */
+
+ public void testTreeToValue() throws Exception
+ {
+ ArrayNode n = MAPPER.createArrayNode();
+ n.add("xyz");
+ ObjectReader r = MAPPER.readerFor(String.class);
+ List<?> list = r.treeToValue(n, List.class);
+ assertEquals(1, list.size());
+ }
+
+ public void testCodecUnsupportedWrites() throws Exception
+ {
+ ObjectReader r = MAPPER.readerFor(String.class);
+ JsonGenerator g = MAPPER.getFactory().createGenerator(new StringWriter());
+ ObjectNode n = MAPPER.createObjectNode();
+ try {
+ r.writeTree(g, n);
+ fail("Should not pass");
+ } catch (UnsupportedOperationException e) {
+ ;
+ }
+ try {
+ r.writeValue(g, "Foo");
+ fail("Should not pass");
+ } catch (UnsupportedOperationException e) {
+ ;
+ }
+ g.close();
+
+ g.close();
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, failures, other
/**********************************************************
*/
diff --git a/src/test/java/com/fasterxml/jackson/databind/seq/ObjectWriterTest.java b/src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java
similarity index 60%
rename from src/test/java/com/fasterxml/jackson/databind/seq/ObjectWriterTest.java
rename to src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java
index b4cc2a8..b29c827 100644
--- a/src/test/java/com/fasterxml/jackson/databind/seq/ObjectWriterTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java
@@ -1,8 +1,9 @@
-package com.fasterxml.jackson.databind.seq;
+package com.fasterxml.jackson.databind;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
+import java.io.StringWriter;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@@ -10,7 +11,6 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.SerializedString;
-import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
@@ -146,13 +146,32 @@
assertNotNull(json);
assertTrue(input.closed);
input.close();
+
+ // and via explicitly passed generator
+ JsonGenerator g = MAPPER.getFactory().createGenerator(new StringWriter());
+ input = new CloseableValue();
+ assertFalse(input.closed);
+ w.writeValue(g, input);
+ assertTrue(input.closed);
+ g.close();
+ input.close();
}
- public void testSettings() throws Exception
+ public void testViewSettings() throws Exception
{
ObjectWriter w = MAPPER.writer();
- assertFalse(w.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
- assertFalse(w.isEnabled(JsonGenerator.Feature.STRICT_DUPLICATE_DETECTION));
+ ObjectWriter newW = w.withView(String.class);
+ assertNotSame(w, newW);
+ assertSame(newW, newW.withView(String.class));
+
+ newW = w.with(Locale.CANADA);
+ assertNotSame(w, newW);
+ assertSame(newW, newW.with(Locale.CANADA));
+ }
+
+ public void testMiscSettings() throws Exception
+ {
+ ObjectWriter w = MAPPER.writer();
assertSame(MAPPER.getFactory(), w.getFactory());
assertFalse(w.hasPrefetchedSerializer());
assertNotNull(w.getTypeFactory());
@@ -160,16 +179,96 @@
JsonFactory f = new JsonFactory();
w = w.with(f);
assertSame(f, w.getFactory());
-
- w = w.withView(String.class);
+ ObjectWriter newW = w.with(Base64Variants.MODIFIED_FOR_URL);
+ assertNotSame(w, newW);
+ assertSame(newW, newW.with(Base64Variants.MODIFIED_FOR_URL));
+
w = w.withAttributes(Collections.emptyMap());
w = w.withAttribute("a", "b");
assertEquals("b", w.getAttributes().getAttribute("a"));
w = w.withoutAttribute("a");
assertNull(w.getAttributes().getAttribute("a"));
- w = w.withRootValueSeparator(new SerializedString(","));
+
+ FormatSchema schema = new BogusSchema();
+ try {
+ newW = w.with(schema);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not use FormatSchema");
+ }
}
+ public void testRootValueSettings() throws Exception
+ {
+ ObjectWriter w = MAPPER.writer();
+
+ // First, root name:
+ ObjectWriter newW = w.withRootName("foo");
+ assertNotSame(w, newW);
+ assertSame(newW, newW.withRootName(PropertyName.construct("foo")));
+ w = newW;
+ newW = w.withRootName((String) null);
+ assertNotSame(w, newW);
+ assertSame(newW, newW.withRootName((PropertyName) null));
+
+ // Then root value separator
+
+ w = w.withRootValueSeparator(new SerializedString(","));
+ assertSame(w, w.withRootValueSeparator(new SerializedString(",")));
+ assertSame(w, w.withRootValueSeparator(","));
+
+ newW = w.withRootValueSeparator("/");
+ assertNotSame(w, newW);
+ assertSame(newW, newW.withRootValueSeparator("/"));
+
+ newW = w.withRootValueSeparator((String) null);
+ assertNotSame(w, newW);
+
+ newW = w.withRootValueSeparator((SerializableString) null);
+ assertNotSame(w, newW);
+ }
+
+ public void testFeatureSettings() throws Exception
+ {
+ ObjectWriter w = MAPPER.writer();
+ assertFalse(w.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
+ assertFalse(w.isEnabled(JsonGenerator.Feature.STRICT_DUPLICATE_DETECTION));
+ ObjectWriter newW = w.with(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS,
+ SerializationFeature.INDENT_OUTPUT);
+ assertNotSame(w, newW);
+ assertTrue(newW.isEnabled(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS));
+ assertTrue(newW.isEnabled(SerializationFeature.INDENT_OUTPUT));
+ assertSame(newW, newW.with(SerializationFeature.INDENT_OUTPUT));
+ assertSame(newW, newW.withFeatures(SerializationFeature.INDENT_OUTPUT));
+
+ newW = w.withFeatures(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS,
+ SerializationFeature.INDENT_OUTPUT);
+ assertNotSame(w, newW);
+
+ newW = w.without(SerializationFeature.FAIL_ON_EMPTY_BEANS,
+ SerializationFeature.EAGER_SERIALIZER_FETCH);
+ assertNotSame(w, newW);
+ assertFalse(newW.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
+ assertFalse(newW.isEnabled(SerializationFeature.EAGER_SERIALIZER_FETCH));
+ assertSame(newW, newW.without(SerializationFeature.FAIL_ON_EMPTY_BEANS));
+ assertSame(newW, newW.withoutFeatures(SerializationFeature.FAIL_ON_EMPTY_BEANS));
+
+ assertNotSame(w, w.withoutFeatures(SerializationFeature.FAIL_ON_EMPTY_BEANS,
+ SerializationFeature.EAGER_SERIALIZER_FETCH));
+ }
+
+ public void testGeneratorFeatures() throws Exception
+ {
+ ObjectWriter w = MAPPER.writer();
+ assertFalse(w.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
+ assertNotSame(w, w.with(JsonGenerator.Feature.ESCAPE_NON_ASCII));
+ assertNotSame(w, w.withFeatures(JsonGenerator.Feature.ESCAPE_NON_ASCII));
+
+ assertTrue(w.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET));
+ assertNotSame(w, w.without(JsonGenerator.Feature.AUTO_CLOSE_TARGET));
+ assertNotSame(w, w.withoutFeatures(JsonGenerator.Feature.AUTO_CLOSE_TARGET));
+ }
+
/*
/**********************************************************
/* Test methods, failures
diff --git a/src/test/java/com/fasterxml/jackson/databind/TestGeneratorUsingMapper.java b/src/test/java/com/fasterxml/jackson/databind/TestGeneratorUsingMapper.java
deleted file mode 100644
index 78955c8..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/TestGeneratorUsingMapper.java
+++ /dev/null
@@ -1,84 +0,0 @@
-package com.fasterxml.jackson.databind;
-
-import java.io.IOException;
-import java.io.StringWriter;
-
-import com.fasterxml.jackson.core.JsonFactory;
-import com.fasterxml.jackson.core.JsonGenerator;
-import com.fasterxml.jackson.core.SerializableString;
-import com.fasterxml.jackson.core.io.CharacterEscapes;
-
-public class TestGeneratorUsingMapper extends BaseMapTest
-{
- final static class Pojo
- {
- public int getX() { return 4; }
- }
-
- /*
- /**********************************************************
- /* Tests for data binding integration
- /**********************************************************
- */
-
- public void testPojoWriting()
- throws IOException
- {
- JsonFactory jf = new MappingJsonFactory();
- StringWriter sw = new StringWriter();
- JsonGenerator gen = jf.createGenerator(sw);
- gen.writeObject(new Pojo());
- gen.close();
- // trimming needed if main-level object has leading space
- String act = sw.toString().trim();
- assertEquals("{\"x\":4}", act);
- }
-
- public void testPojoWritingFailing()
- throws IOException
- {
- // regular factory can't do it, without a call to setCodec()
- JsonFactory jf = new JsonFactory();
- try {
- StringWriter sw = new StringWriter();
- JsonGenerator gen = jf.createGenerator(sw);
- gen.writeObject(new Pojo());
- gen.close();
- fail("Expected an exception: got sw '"+sw.toString()+"'");
- } catch (IllegalStateException e) {
- verifyException(e, "No ObjectCodec defined");
- }
- }
-
- public void testIssue820() throws IOException
- {
- StringBuffer sb = new StringBuffer();
- while (sb.length() <= 5000) {
- sb.append("Yet another line of text...\n");
- }
- String sampleText = sb.toString();
- assertTrue(
- "Sanity check so I don't mess up the sample text later.",
- sampleText.contains("\n"));
-
- final ObjectMapper mapper = new ObjectMapper();
- final CharacterEscapes defaultCharacterEscapes = new CharacterEscapes() {
- private static final long serialVersionUID = 1L;
-
- @Override
- public int[] getEscapeCodesForAscii() {
- return standardAsciiEscapesForJSON();
- }
-
- @Override
- public SerializableString getEscapeSequence(final int ch) {
- return null;
- }
- };
-
- mapper.getFactory().setCharacterEscapes(defaultCharacterEscapes);
- String jacksonJson = mapper.writeValueAsString(sampleText);
- boolean hasLFs = jacksonJson.indexOf('\n') > 0;
- assertFalse("Should NOT contain linefeeds, should have been escaped", hasLFs);
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/TestJDKSerialization.java b/src/test/java/com/fasterxml/jackson/databind/TestJDKSerialization.java
index dd5c4fe..7adf99e 100644
--- a/src/test/java/com/fasterxml/jackson/databind/TestJDKSerialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/TestJDKSerialization.java
@@ -3,6 +3,14 @@
import java.io.*;
import java.util.*;
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonAnySetter;
+import com.fasterxml.jackson.databind.DeserializationConfig;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectReader;
+import com.fasterxml.jackson.databind.ObjectWriter;
+import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.LRUMap;
@@ -35,6 +43,25 @@
public Map<String,ABC> stuff = new LinkedHashMap<String,ABC>();
}
+ static class AnyBean {
+ HashMap<String,Object> _map;
+
+ public AnyBean() {
+ _map = new HashMap<String,Object>();
+ }
+
+ @JsonAnySetter
+ AnyBean addEntry(String key, Object value) {
+ _map.put(key, value);
+ return this;
+ }
+
+ @JsonAnyGetter
+ public Map<String,Object> properties() {
+ return _map;
+ }
+ }
+
/*
/**********************************************************
/* Tests for individual objects
@@ -107,6 +134,9 @@
final String EXP_JSON = "{\"x\":2,\"y\":3}";
final MyPojo p = new MyPojo(2, 3);
assertEquals(EXP_JSON, origWriter.writeValueAsString(p));
+ String json = origWriter.writeValueAsString(new AnyBean()
+ .addEntry("a", "b"));
+ assertNotNull(json);
byte[] bytes = jdkSerialize(origWriter);
ObjectWriter writer2 = jdkDeserialize(bytes);
assertEquals(EXP_JSON, writer2.writeValueAsString(p));
@@ -115,13 +145,21 @@
public void testObjectReader() throws IOException
{
ObjectReader origReader = MAPPER.readerFor(MyPojo.class);
- final String JSON = "{\"x\":1,\"y\":2}";
+ String JSON = "{\"x\":1,\"y\":2}";
MyPojo p1 = origReader.readValue(JSON);
assertEquals(2, p1.y);
- byte[] bytes = jdkSerialize(origReader);
- ObjectReader reader2 = jdkDeserialize(bytes);
+ ObjectReader anyReader = MAPPER.readerFor(AnyBean.class);
+ AnyBean any = anyReader.readValue(JSON);
+ assertEquals(Integer.valueOf(2), any.properties().get("y"));
+
+ byte[] readerBytes = jdkSerialize(origReader);
+ ObjectReader reader2 = jdkDeserialize(readerBytes);
MyPojo p2 = reader2.readValue(JSON);
assertEquals(2, p2.y);
+
+ ObjectReader anyReader2 = jdkDeserialize(jdkSerialize(anyReader));
+ AnyBean any2 = anyReader2.readValue(JSON);
+ assertEquals(Integer.valueOf(2), any2.properties().get("y"));
}
public void testObjectMapper() throws IOException
diff --git a/src/test/java/com/fasterxml/jackson/databind/TestObjectMapperBeanDeserializer.java b/src/test/java/com/fasterxml/jackson/databind/TestObjectMapperBeanDeserializer.java
deleted file mode 100644
index 5d96b07..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/TestObjectMapperBeanDeserializer.java
+++ /dev/null
@@ -1,329 +0,0 @@
-package com.fasterxml.jackson.databind;
-
-import java.io.*;
-import java.net.URI;
-import java.util.*;
-
-import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.JsonMappingException;
-import com.fasterxml.jackson.databind.JsonSerializable;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializerProvider;
-import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
-
-/**
- * Unit tests for verifying deserialization of Beans.
- */
-public class TestObjectMapperBeanDeserializer
- extends BaseTest
-{
- /*
- /**********************************************************
- /* Helper classes
- /**********************************************************
- */
-
- final static class CtorValueBean
- implements JsonSerializable // so we can output as simple String
- {
- final String _desc;
-
- public CtorValueBean(String d) { _desc = d; }
- public CtorValueBean(int value) { _desc = String.valueOf(value); }
- public CtorValueBean(long value) { _desc = String.valueOf(value); }
- public CtorValueBean(double value) { _desc = String.valueOf(value); }
-
- @Override
- public void serialize(JsonGenerator jgen, SerializerProvider provider)
- throws IOException, JsonGenerationException
- {
- jgen.writeString(_desc);
- }
-
- @Override public String toString() { return _desc; }
-
- @Override public boolean equals(Object o) {
- if (!(o instanceof CtorValueBean)) return false;
- CtorValueBean other = (CtorValueBean) o;
- return _desc.equals(other._desc);
- }
- @Override
- public void serializeWithType(JsonGenerator jgen,
- SerializerProvider provider, TypeSerializer typeSer)
- throws IOException, JsonProcessingException {
- }
- }
-
- final static class FactoryValueBean
- {
- final String _desc;
-
- protected FactoryValueBean(String desc, int dummy) { _desc = desc; }
-
- public static FactoryValueBean valueOf(String v) { return new FactoryValueBean(v, 0); }
- public static FactoryValueBean valueOf(int v) { return new FactoryValueBean(String.valueOf(v), 0); }
- public static FactoryValueBean valueOf(long v) { return new FactoryValueBean(String.valueOf(v), 0); }
-
- @Override public String toString() { return _desc; }
- }
-
- static class OtherBean {
- SomeIncompatibleType o;
-
- protected OtherBean(SomeIncompatibleType o) {
- this.o = o;
- }
-
- static class SomeIncompatibleType { }
- }
-
- /**
- * Simple test bean
- */
- public final static class TestBean
- {
- int _x;
- long _y;
- String _desc;
- URI _uri;
- Collection<?> _misc;
-
- // Explicit constructor
- public TestBean(int x, long y, String desc, URI uri, Collection<?> misc)
- {
- _x = x;
- _y = y;
- _desc = desc;
- _uri = uri;
- _misc = misc;
- }
-
- // plus default one that is needed for deserialization
- public TestBean() { }
-
- public String getDesc() { return _desc; }
- public int getX() { return _x; }
- public long getY() { return _y; }
- public URI getURI() { return _uri; }
- public Collection<?> getMisc() { return _misc; }
-
- public void setDesc(String value) { _desc = value; }
- public void setX(int value) { _x = value; }
- public void setY(long value) { _y = value; }
- public void setURI(URI value) { _uri = value; }
- public void setMisc(Collection<?> value) { _misc = value; }
-
- @Override
- public boolean equals(Object o)
- {
- if (o == null || o.getClass() != getClass()) return false;
- TestBean other = (TestBean) o;
- return (other._x == _x)
- && (other._y == _y)
- && (other._desc.equals(_desc))
- && (other._uri.equals(_uri))
- && (other._misc.equals(_misc))
- ;
- }
-
- @Override
- public String toString()
- {
- StringBuilder sb = new StringBuilder();
- sb.append("[TestBean ");
- sb.append("x=").append(_x);
- sb.append(" y=").append(_y);
- sb.append(" desc=").append(_desc);
- sb.append(" uri=").append(_uri);
- sb.append(" misc=").append(_misc);
- sb.append("]");
- return sb.toString();
- }
- }
-
- /**
- * Another test bean, this one containing a typed list. Needed to ensure
- * that generics type information is properly accessed via mutator methods.
- * Note: List elements must be something other than what 'untyped' mapper
- * would produce from serialization.
- */
- public final static class BeanWithList
- {
- List<CtorValueBean> _beans;
-
- public BeanWithList() { }
- public BeanWithList(List<CtorValueBean> beans) { _beans = beans; }
-
- public List<CtorValueBean> getBeans() { return _beans; }
-
- public void setBeans(List<CtorValueBean> beans) {
- _beans = beans;
- }
-
- @Override
- public int hashCode() { return (_beans == null) ? -1 : _beans.size(); }
-
- @Override
- public boolean equals(Object o) {
- if (!(o instanceof BeanWithList)) return false;
- BeanWithList other = BeanWithList.class.cast(o);
- return _beans.equals(other._beans);
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("[Bean, list ");
- if (_beans == null) {
- sb.append("NULL");
- } else {
- sb.append('(').append(_beans.size()).append('/');
- sb.append(_beans.getClass().getName()).append(") ");
- boolean type = false;
- for (CtorValueBean bean : _beans) {
- if (!type) {
- sb.append("(").append(bean.getClass().getSimpleName()).append(")");
- type = true;
- }
- sb.append(bean);
- sb.append(' ');
- }
- }
- sb.append(']');
- return sb.toString();
- }
- }
-
- /*
- /**********************************************************
- /* Deserialization from simple types (String, int)
- /**********************************************************
- */
-
- private final ObjectMapper MAPPER = new ObjectMapper();
-
- public void testFromStringCtor() throws Exception
- {
- CtorValueBean result = MAPPER.readValue("\"abc\"", CtorValueBean.class);
- assertEquals("abc", result.toString());
- }
-
- public void testFromIntCtor() throws Exception
- {
- CtorValueBean result = MAPPER.readValue("13", CtorValueBean.class);
- assertEquals("13", result.toString());
-
- try {
- OtherBean otherResult = MAPPER.readValue("13", OtherBean.class);
- fail("Expected an exception, but got result value: "+otherResult.o);
- } catch (JsonMappingException e) {
- verifyException(e, "deserialize from Number value",
- "no int/Integer-argument constructor/factory method");
- assertValidLocation(e.getLocation());
- }
- }
-
- public void testFromLongCtor() throws Exception
- {
- // Must use something that is forced as Long...
- long value = 12345678901244L;
- CtorValueBean result = MAPPER.readValue(""+value, CtorValueBean.class);
- assertEquals(""+value, result.toString());
-
- try {
- OtherBean otherResult = MAPPER.readValue(""+value, OtherBean.class);
- fail("Expected an exception, but got result value: "+otherResult.o);
- } catch (JsonMappingException e) {
- verifyException(e, "deserialize from Number value",
- "no long/Long-argument constructor/factory method");
- assertValidLocation(e.getLocation());
- }
- }
-
- public void testFromDoubleCtor() throws Exception
- {
- CtorValueBean result = MAPPER.readValue("13.5", CtorValueBean.class);
- assertEquals("13.5", result.toString());
-
- try {
- OtherBean otherResult = MAPPER.readValue("13.5", OtherBean.class);
- fail("Expected an exception, but got result value: "+otherResult.o);
- } catch (JsonMappingException e) {
- verifyException(e, "deserialize from Number value",
- "no double/Double-argument constructor/factory method");
- assertValidLocation(e.getLocation());
- }
- }
-
- public void testFromStringFactory() throws Exception
- {
- FactoryValueBean result = MAPPER.readValue("\"abc\"", FactoryValueBean.class);
- assertEquals("abc", result.toString());
- }
-
- public void testFromIntFactory() throws Exception
- {
- FactoryValueBean result = MAPPER.readValue("13", FactoryValueBean.class);
- assertEquals("13", result.toString());
- }
-
- public void testFromLongFactory() throws Exception
- {
- // Must use something that is forced as Long...
- long value = 12345678901244L;
- FactoryValueBean result = MAPPER.readValue(""+value, FactoryValueBean.class);
- assertEquals(""+value, result.toString());
- }
-
- /*
- /**********************************************************
- /* Deserialization from JSON Object
- /**********************************************************
- */
-
- public void testSimpleBean() throws Exception
- {
- ArrayList<Object> misc = new ArrayList<Object>();
- misc.add("xyz");
- misc.add(42);
- misc.add(null);
- misc.add(Boolean.TRUE);
- TestBean bean = new TestBean(13, -900L, "\"test\"", new URI("http://foobar.com"), misc);
-
- // Hmmh. We probably should use serializer too... easier
- String json = MAPPER.writeValueAsString(bean);
-
- TestBean result = MAPPER.readValue(json, TestBean.class);
- assertEquals(bean, result);
- }
-
- public void testListBean() throws Exception
- {
- final int COUNT = 13;
- ArrayList<CtorValueBean> beans = new ArrayList<CtorValueBean>();
- for (int i = 0; i < COUNT; ++i) {
- beans.add(new CtorValueBean(i));
- }
- BeanWithList bean = new BeanWithList(beans);
-
- StringWriter sw = new StringWriter();
- MAPPER.writeValue(sw, bean);
-
- BeanWithList result = MAPPER.readValue(sw.toString(), BeanWithList.class);
- assertEquals(bean, result);
- }
-
- /**
- * Also, let's verify that unknown fields cause an exception with default
- * settings.
- */
- public void testUnknownFields() throws Exception
- {
- try {
- TestBean bean = MAPPER.readValue("{ \"foobar\" : 3 }", TestBean.class);
- fail("Expected an exception, got bean: "+bean);
- } catch (JsonMappingException jse) {
- ;
- }
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/TestObjectMapperBeanSerializer.java b/src/test/java/com/fasterxml/jackson/databind/TestObjectMapperBeanSerializer.java
deleted file mode 100644
index 11deefe..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/TestObjectMapperBeanSerializer.java
+++ /dev/null
@@ -1,231 +0,0 @@
-package com.fasterxml.jackson.databind;
-
-
-import java.io.*;
-import java.net.*;
-import java.math.BigDecimal;
-import java.math.BigInteger;
-
-import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
-/**
- * This unit test suite tries to verify that the "Native" java type
- * mapper can properly serialize Java core objects to JSON.
- *
- * @author Scott Dixon
- */
-public class TestObjectMapperBeanSerializer
- extends BaseTest
-{
- /**
- * Sanity test to ensure the pieces all work when put together.
- */
- public void testComplexObject()
- throws Exception
- {
- FixtureObject aTestObj = new FixtureObject();
- ObjectMapper aMapper = new ObjectMapper();
- StringWriter aWriter = new StringWriter();
- JsonGenerator aGen = new JsonFactory().createGenerator(aWriter);
- aMapper.writeValue(aGen, aTestObj);
- aGen.close();
- JsonParser jp = new JsonFactory().createParser(new StringReader(aWriter.toString()));
-
- assertEquals(JsonToken.START_OBJECT, jp.nextToken());
-
- while (jp.nextToken() != JsonToken.END_OBJECT) {
- assertEquals(JsonToken.FIELD_NAME, jp.getCurrentToken());
- String name = jp.getCurrentName();
- JsonToken t = jp.nextToken();
-
- if (name.equals("uri") || name.equals("url")) {
- assertToken(JsonToken.VALUE_STRING, t);
- assertEquals(FixtureObjectBase.VALUE_URSTR, getAndVerifyText(jp));
- } else if (name.equals("testNull")) {
- assertToken(JsonToken.VALUE_NULL, t);
- } else if (name.equals("testString")) {
- assertToken(JsonToken.VALUE_STRING, t);
- assertEquals(FixtureObjectBase.VALUE_STRING, getAndVerifyText(jp));
- } else if (name.equals("testBoolean")) {
- assertToken(JsonToken.VALUE_TRUE, t);
- } else if (name.equals("testEnum")) {
- assertToken(JsonToken.VALUE_STRING, t);
- assertEquals(FixtureObjectBase.VALUE_ENUM.toString(),getAndVerifyText(jp));
- } else if (name.equals("testInteger")) {
- assertToken(JsonToken.VALUE_NUMBER_INT, t);
- assertEquals(jp.getIntValue(),FixtureObjectBase.VALUE_INT);
- } else if (name.equals("testLong")) {
- assertToken(JsonToken.VALUE_NUMBER_INT, t);
- assertEquals(jp.getLongValue(),FixtureObjectBase.VALUE_LONG);
- } else if (name.equals("testBigInteger")) {
- assertToken(JsonToken.VALUE_NUMBER_INT, t);
- assertEquals(jp.getLongValue(),FixtureObjectBase.VALUE_BIGINT.longValue());
- } else if (name.equals("testBigDecimal")) {
- assertToken(JsonToken.VALUE_NUMBER_FLOAT, t);
- assertEquals(jp.getText(), FixtureObjectBase.VALUE_BIGDEC.toString());
- } else if (name.equals("testCharacter")) {
- assertToken(JsonToken.VALUE_STRING, t);
- assertEquals(String.valueOf(FixtureObjectBase.VALUE_CHAR), getAndVerifyText(jp));
- } else if (name.equals("testShort")) {
- assertToken(JsonToken.VALUE_NUMBER_INT, t);
- assertEquals(jp.getIntValue(),FixtureObjectBase.VALUE_SHORT);
- } else if (name.equals("testByte")) {
- assertToken(JsonToken.VALUE_NUMBER_INT, t);
- assertEquals(jp.getIntValue(),FixtureObjectBase.VALUE_BYTE);
- } else if (name.equals("testFloat")) {
- assertToken(JsonToken.VALUE_NUMBER_FLOAT, t);
- assertEquals(jp.getDecimalValue().floatValue(),FixtureObjectBase.VALUE_FLOAT);
- } else if (name.equals("testDouble")) {
- assertToken(JsonToken.VALUE_NUMBER_FLOAT, t);
- assertEquals(jp.getDoubleValue(),FixtureObjectBase.VALUE_DBL);
- } else if (name.equals("testStringBuffer")) {
- assertToken(JsonToken.VALUE_STRING, t);
- assertEquals(FixtureObjectBase.VALUE_STRING, getAndVerifyText(jp));
- } else if (name.equals("testError")) {
- // More complicated...
- assertToken(JsonToken.START_OBJECT, t);
-
- //getTestError->Exception::getCause
-
- while (jp.nextToken() == JsonToken.FIELD_NAME) {
- name = jp.getCurrentName();
- if (name.equals("cause")) {
- assertEquals(JsonToken.VALUE_NULL, jp.nextToken());
- } else if (name.equals("message")) {
- assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
- assertEquals(FixtureObjectBase.VALUE_ERRTXT, getAndVerifyText(jp));
- } else if (name.equals("localizedMessage")) {
- assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
- } else if (name.equals("stackTrace")) {
- assertEquals(JsonToken.START_ARRAY,jp.nextToken());
- int i = 0;
- while(jp.nextToken() != JsonToken.END_ARRAY) {
- if(i >= 100000) {
- assertTrue("Probably run away loop in test. StackTrack Array was not properly closed.",false);
- }
- }
- } else if (name.equals("suppressed")) {
- // JDK 7 has introduced a new property 'suppressed' to Throwable; skip if seen
- assertEquals(JsonToken.START_ARRAY,jp.nextToken());
- assertEquals(JsonToken.END_ARRAY,jp.nextToken());
- } else {
- fail("Unexpected field name '"+name+"'");
- }
- }
- //CLOSE OF THE EXCEPTION
- assertEquals(JsonToken.END_OBJECT, jp.getCurrentToken());
- } else {
- fail("Unexpected field, name '"+name+"'");
- }
- }
-
- //END OF TOKEN PARSING
- assertNull(jp.nextToken());
- jp.close();
- }
-
- private static enum EFixtureEnum
- {
- THIS_IS_AN_ENUM_VALUE_0,
- THIS_IS_AN_ENUM_VALUE_1,
- THIS_IS_AN_ENUM_VALUE_2,
- THIS_IS_AN_ENUM_VALUE_3,
- }
-
- static class FixtureObjectBase
- {
- public static final String VALUE_STRING = "foobar";
- public static final EFixtureEnum VALUE_ENUM = EFixtureEnum.THIS_IS_AN_ENUM_VALUE_2;
- public static final int VALUE_INT = Integer.MIN_VALUE;
- public static final long VALUE_LONG = Long.MIN_VALUE;
- public static final BigInteger VALUE_BIGINT = new BigInteger((new Long(Long.MAX_VALUE)).toString());
- public static final BigDecimal VALUE_BIGDEC = new BigDecimal((new Double(Double.MAX_VALUE)).toString());
- // this is not necessarily a good char to check
- public static final char VALUE_CHAR = Character.MAX_VALUE;
- public static final short VALUE_SHORT = Short.MAX_VALUE;
- public static final byte VALUE_BYTE = Byte.MAX_VALUE;
- public static final float VALUE_FLOAT = Float.MAX_VALUE;
- public static final double VALUE_DBL = Double.MAX_VALUE;
- public static final String VALUE_ERRTXT = "This is the message text for the test error.";
-
- public static final String VALUE_URSTR = "http://jackson.codehaus.org/hi?var1=foo%20bar";
-
- public URL getURL() throws IOException
- {
- return new URL(VALUE_URSTR);
- }
-
- public URI getURI() throws IOException
- {
- try {
- return new URI(VALUE_URSTR);
- } catch (Exception e) {
- throw new IllegalArgumentException(e);
- }
- }
- public String getTestNull()
- {
- return null;
- }
- public String getTestString()
- {
- return VALUE_STRING;
- }
- public boolean getTestBoolean()
- {
- return true;
- }
- public EFixtureEnum getTestEnum()
- {
- return VALUE_ENUM;
- }
- public int getTestInteger()
- {
- return VALUE_INT;
- }
- public long getTestLong()
- {
- return VALUE_LONG;
- }
- public BigInteger getTestBigInteger()
- {
- return VALUE_BIGINT;
- }
- public BigDecimal getTestBigDecimal()
- {
- return VALUE_BIGDEC;
- }
- public char getTestCharacter()
- {
- return VALUE_CHAR;
- }
- public short getTestShort()
- {
- return VALUE_SHORT;
- }
- public byte getTestByte()
- {
- return VALUE_BYTE;
- }
- public float getTestFloat()
- {
- return VALUE_FLOAT;
- }
- public double getTestDouble()
- {
- return VALUE_DBL;
- }
- public StringBuffer getTestStringBuffer()
- {
- return new StringBuffer(VALUE_STRING);
- }
- }
-
- static class FixtureObject extends FixtureObjectBase
- {
- public Exception getTestError() {
- return new Exception(VALUE_ERRTXT);
- }
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/TestStdDateFormat.java b/src/test/java/com/fasterxml/jackson/databind/TestStdDateFormat.java
index adff361..c65be64 100644
--- a/src/test/java/com/fasterxml/jackson/databind/TestStdDateFormat.java
+++ b/src/test/java/com/fasterxml/jackson/databind/TestStdDateFormat.java
@@ -15,8 +15,8 @@
assertNotNull(StdDateFormat.getRFC1123Format(tz, loc));
}
- // [databind#803
- public void testLenient() throws Exception
+ // [databind#803]
+ public void testLenientDefaults() throws Exception
{
StdDateFormat f = StdDateFormat.instance;
@@ -37,22 +37,28 @@
assertFalse(f2.isLenient());
StdDateFormat f3 = f2.clone();
assertFalse(f3.isLenient());
+ }
+
+ public void testLenientParsing() throws Exception
+ {
+ StdDateFormat f = StdDateFormat.instance.clone();
+ f.setLenient(false);
// first, legal dates are... legal
- Date dt = f3.parse("2015-11-30");
+ Date dt = f.parse("2015-11-30");
assertNotNull(dt);
// but as importantly, when not lenient, do not allow
try {
- f3.parse("2015-11-32");
+ f.parse("2015-11-32");
fail("Should not pass");
} catch (ParseException e) {
verifyException(e, "can not parse date");
}
// ... yet, with lenient, do allow
- f3.setLenient(true);
- dt = f3.parse("2015-11-32");
+ f.setLenient(true);
+ dt = f.parse("2015-11-32");
assertNotNull(dt);
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/access/TestSerAnyGetter.java b/src/test/java/com/fasterxml/jackson/databind/access/TestAnyGetterAccess.java
similarity index 97%
rename from src/test/java/com/fasterxml/jackson/databind/access/TestSerAnyGetter.java
rename to src/test/java/com/fasterxml/jackson/databind/access/TestAnyGetterAccess.java
index 4ca471b..311a86b 100644
--- a/src/test/java/com/fasterxml/jackson/databind/access/TestSerAnyGetter.java
+++ b/src/test/java/com/fasterxml/jackson/databind/access/TestAnyGetterAccess.java
@@ -9,7 +9,7 @@
* Separate tests located in different package than code being
* exercised; needed to trigger some access-related failures.
*/
-public class TestSerAnyGetter
+public class TestAnyGetterAccess
extends BaseMapTest
{
/*
diff --git a/src/test/java/com/fasterxml/jackson/databind/cfg/BogusFormatFeature.java b/src/test/java/com/fasterxml/jackson/databind/cfg/BogusFormatFeature.java
new file mode 100644
index 0000000..c059148
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/cfg/BogusFormatFeature.java
@@ -0,0 +1,31 @@
+package com.fasterxml.jackson.databind.cfg;
+
+import com.fasterxml.jackson.core.FormatFeature;
+
+public enum BogusFormatFeature
+ implements FormatFeature
+{
+ FF_ENABLED_BY_DEFAULT(true),
+ FF_DISABLED_BY_DEFAULT(false);
+
+ private boolean _default;
+
+ private BogusFormatFeature(boolean d) {
+ _default = d;
+ }
+
+ @Override
+ public boolean enabledByDefault() {
+ return _default;
+ }
+
+ @Override
+ public int getMask() {
+ return (1 << ordinal());
+ }
+
+ @Override
+ public boolean enabledIn(int flags) {
+ return (flags & getMask()) != 0;
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/cfg/ConfigObjectsTest.java b/src/test/java/com/fasterxml/jackson/databind/cfg/ConfigObjectsTest.java
new file mode 100644
index 0000000..0ace3b0
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/cfg/ConfigObjectsTest.java
@@ -0,0 +1,30 @@
+package com.fasterxml.jackson.databind.cfg;
+
+import com.fasterxml.jackson.databind.*;
+
+import com.fasterxml.jackson.databind.jsontype.SubtypeResolver;
+import com.fasterxml.jackson.databind.jsontype.impl.StdSubtypeResolver;
+
+public class ConfigObjectsTest extends BaseMapTest
+{
+ static class Base { }
+ static class Sub extends Base { }
+
+ public void testSubtypeResolver() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ SubtypeResolver res = mapper.getSubtypeResolver();
+ assertTrue(res instanceof StdSubtypeResolver);
+
+ StdSubtypeResolver repl = new StdSubtypeResolver();
+ repl.registerSubtypes(Sub.class);
+ mapper.setSubtypeResolver(repl);
+ assertSame(repl, mapper.getSubtypeResolver());
+ }
+
+ public void testMics() throws Exception
+ {
+ assertFalse(MapperFeature.AUTO_DETECT_FIELDS.enabledIn(0));
+ assertTrue(MapperFeature.AUTO_DETECT_FIELDS.enabledIn(-1));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/cfg/DatabindContextTest.java b/src/test/java/com/fasterxml/jackson/databind/cfg/DatabindContextTest.java
new file mode 100644
index 0000000..9da87a0
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/cfg/DatabindContextTest.java
@@ -0,0 +1,22 @@
+package com.fasterxml.jackson.databind.cfg;
+
+import com.fasterxml.jackson.databind.*;
+
+public class DatabindContextTest extends BaseMapTest
+{
+ private final ObjectMapper MAPPER = objectMapper();
+
+ public void testDeserializationContext() throws Exception
+ {
+ DeserializationContext ctxt = MAPPER.getDeserializationContext();
+ // should be ok to try to resolve `null`
+ assertNull(ctxt.constructType((Class<?>) null));
+ assertNull(ctxt.constructType((java.lang.reflect.Type) null));
+ }
+
+ public void testSerializationContext() throws Exception
+ {
+ SerializerProvider ctxt = MAPPER.getSerializerProvider();
+ assertNull(ctxt.constructType(null));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/cfg/DeserializationConfigTest.java b/src/test/java/com/fasterxml/jackson/databind/cfg/DeserializationConfigTest.java
new file mode 100644
index 0000000..5a78e89
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/cfg/DeserializationConfigTest.java
@@ -0,0 +1,127 @@
+package com.fasterxml.jackson.databind.cfg;
+
+import java.util.Collections;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.introspect.ClassIntrospector;
+
+public class DeserializationConfigTest extends BaseMapTest
+{
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testFeatureDefaults()
+ {
+ ObjectMapper m = new ObjectMapper();
+ DeserializationConfig cfg = m.getDeserializationConfig();
+
+ // Expected defaults:
+ assertTrue(cfg.isEnabled(MapperFeature.USE_ANNOTATIONS));
+ assertTrue(cfg.isEnabled(MapperFeature.AUTO_DETECT_SETTERS));
+ assertTrue(cfg.isEnabled(MapperFeature.AUTO_DETECT_CREATORS));
+ assertTrue(cfg.isEnabled(MapperFeature.USE_GETTERS_AS_SETTERS));
+ assertTrue(cfg.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS));
+
+ assertFalse(cfg.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS));
+ assertFalse(cfg.isEnabled(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS));
+
+ assertTrue(cfg.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
+ }
+
+ public void testBasicFeatures() throws Exception
+ {
+ DeserializationConfig config = MAPPER.getDeserializationConfig();
+ assertTrue(config.hasDeserializationFeatures(DeserializationFeature.EAGER_DESERIALIZER_FETCH.getMask()));
+ assertFalse(config.hasDeserializationFeatures(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY.getMask()));
+ assertTrue(config.hasSomeOfFeatures(DeserializationFeature.EAGER_DESERIALIZER_FETCH.getMask()
+ + DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY.getMask()));
+ assertFalse(config.hasSomeOfFeatures(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY.getMask()));
+
+ // if no changes then same config object
+ assertSame(config, config.without());
+ assertSame(config, config.with());
+ assertSame(config, config.with(MAPPER.getSubtypeResolver()));
+
+ // and then change
+ DeserializationConfig newConfig = config.with(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
+ assertNotSame(config, newConfig);
+ config = newConfig;
+
+ // but another attempt with no real change returns same
+ assertSame(config, config.with(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
+ assertNotSame(config, config.with(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, false));
+
+ assertNotSame(config, config.with(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT,
+ DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES));
+ }
+
+ public void testParserFeatures() throws Exception
+ {
+ DeserializationConfig config = MAPPER.getDeserializationConfig();
+ assertNotSame(config, config.with(JsonParser.Feature.ALLOW_COMMENTS));
+ assertNotSame(config, config.withFeatures(JsonParser.Feature.ALLOW_COMMENTS,
+ JsonParser.Feature.ALLOW_MISSING_VALUES));
+
+ assertNotSame(config, config.without(JsonParser.Feature.ALLOW_COMMENTS));
+ assertNotSame(config, config.withoutFeatures(JsonParser.Feature.ALLOW_COMMENTS,
+ JsonParser.Feature.ALLOW_MISSING_VALUES));
+ }
+
+ public void testFormatFeatures() throws Exception
+ {
+ DeserializationConfig config = MAPPER.getDeserializationConfig();
+ assertNotSame(config, config.with(BogusFormatFeature.FF_DISABLED_BY_DEFAULT));
+ assertNotSame(config, config.withFeatures(BogusFormatFeature.FF_DISABLED_BY_DEFAULT,
+ BogusFormatFeature.FF_ENABLED_BY_DEFAULT));
+ assertNotSame(config, config.without(BogusFormatFeature.FF_ENABLED_BY_DEFAULT));
+ assertNotSame(config, config.withoutFeatures(BogusFormatFeature.FF_DISABLED_BY_DEFAULT,
+ BogusFormatFeature.FF_ENABLED_BY_DEFAULT));
+ }
+
+ /* Test to verify that we don't overflow number of features; if we
+ * hit the limit, need to change implementation -- this test just
+ * gives low-water mark
+ */
+ public void testEnumIndexes()
+ {
+ int max = 0;
+
+ for (DeserializationFeature f : DeserializationFeature.values()) {
+ max = Math.max(max, f.ordinal());
+ }
+ if (max >= 31) { // 31 is actually ok; 32 not
+ fail("Max number of DeserializationFeature enums reached: "+max);
+ }
+ }
+
+ public void testOverrideIntrospectors()
+ {
+ ObjectMapper m = new ObjectMapper();
+ DeserializationConfig cfg = m.getDeserializationConfig();
+ // and finally, ensure we could override introspectors
+ cfg = cfg.with((ClassIntrospector) null); // no way to verify tho
+ cfg = cfg.with((AnnotationIntrospector) null);
+ assertNull(cfg.getAnnotationIntrospector());
+ }
+
+ public void testMisc() throws Exception
+ {
+ DeserializationConfig config = MAPPER.getDeserializationConfig();
+ assertEquals(JsonInclude.Value.empty(), config.getDefaultPropertyInclusion());
+ assertEquals(JsonInclude.Value.empty(), config.getDefaultPropertyInclusion(String.class));
+
+ assertSame(config, config.withRootName((PropertyName) null)); // defaults to 'none'
+
+ DeserializationConfig newConfig = config.withRootName(PropertyName.construct("foobar"));
+ assertNotSame(config, newConfig);
+ config = newConfig;
+ assertSame(config, config.withRootName(PropertyName.construct("foobar")));
+
+ assertSame(config, config.with(config.getAttributes()));
+ assertNotSame(config, config.with(new ContextAttributes.Impl(Collections.singletonMap("a", "b"))));
+
+ // should also be able to introspect:
+ assertNotNull(config.introspectDirectClassAnnotations(getClass()));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/cfg/SerConfigTest.java b/src/test/java/com/fasterxml/jackson/databind/cfg/SerConfigTest.java
new file mode 100644
index 0000000..adb3b18
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/cfg/SerConfigTest.java
@@ -0,0 +1,77 @@
+package com.fasterxml.jackson.databind.cfg;
+
+import java.util.Collections;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonGenerator;
+
+import com.fasterxml.jackson.databind.*;
+
+public class SerConfigTest extends BaseMapTest
+{
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testSerConfig() throws Exception
+ {
+ SerializationConfig config = MAPPER.getSerializationConfig();
+ assertTrue(config.hasSerializationFeatures(SerializationFeature.FAIL_ON_EMPTY_BEANS.getMask()));
+ assertFalse(config.hasSerializationFeatures(SerializationFeature.CLOSE_CLOSEABLE.getMask()));
+ assertEquals(JsonInclude.Value.empty(), config.getDefaultPropertyInclusion());
+ assertEquals(JsonInclude.Value.empty(), config.getDefaultPropertyInclusion(String.class));
+ assertFalse(config.useRootWrapping());
+
+ // if no changes then same config object
+ assertSame(config, config.without());
+ assertSame(config, config.with());
+ assertSame(config, config.with(MAPPER.getSubtypeResolver()));
+
+ // and then change
+ SerializationConfig newConfig = config.with(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
+ assertNotSame(config, newConfig);
+ config = newConfig;
+ assertSame(config, config.with(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
+ assertNotSame(config, config.with(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, false));
+
+ assertNotSame(config, config.with(SerializationFeature.INDENT_OUTPUT,
+ SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS));
+
+ assertSame(config, config.withRootName((PropertyName) null)); // defaults to 'none'
+
+ newConfig = config.withRootName(PropertyName.construct("foobar"));
+ assertNotSame(config, newConfig);
+ assertTrue(newConfig.useRootWrapping());
+
+ assertSame(config, config.with(config.getAttributes()));
+ assertNotSame(config, config.with(new ContextAttributes.Impl(Collections.singletonMap("a", "b"))));
+
+ assertNotNull(config.introspectDirectClassAnnotations(getClass()));
+ }
+
+ public void testGeneratorFeatures() throws Exception
+ {
+ SerializationConfig config = MAPPER.getSerializationConfig();
+ JsonFactory f = MAPPER.getFactory();
+ assertFalse(config.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII, f));
+ assertNotSame(config, config.with(JsonGenerator.Feature.ESCAPE_NON_ASCII));
+ SerializationConfig newConfig = config.withFeatures(JsonGenerator.Feature.ESCAPE_NON_ASCII,
+ JsonGenerator.Feature.IGNORE_UNKNOWN);
+ assertNotSame(config, newConfig);
+ assertTrue(newConfig.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII, f));
+
+ assertNotSame(config, config.without(JsonGenerator.Feature.ESCAPE_NON_ASCII));
+ assertNotSame(config, config.withoutFeatures(JsonGenerator.Feature.ESCAPE_NON_ASCII,
+ JsonGenerator.Feature.IGNORE_UNKNOWN));
+ }
+
+ public void testFormatFeatures() throws Exception
+ {
+ SerializationConfig config = MAPPER.getSerializationConfig();
+ assertNotSame(config, config.with(BogusFormatFeature.FF_DISABLED_BY_DEFAULT));
+ assertNotSame(config, config.withFeatures(BogusFormatFeature.FF_DISABLED_BY_DEFAULT,
+ BogusFormatFeature.FF_ENABLED_BY_DEFAULT));
+ assertNotSame(config, config.without(BogusFormatFeature.FF_ENABLED_BY_DEFAULT));
+ assertNotSame(config, config.withoutFeatures(BogusFormatFeature.FF_DISABLED_BY_DEFAULT,
+ BogusFormatFeature.FF_ENABLED_BY_DEFAULT));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/NumericConversionTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/NumericConversionTest.java
index 8055146..d8a5cce 100644
--- a/src/test/java/com/fasterxml/jackson/databind/convert/NumericConversionTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/convert/NumericConversionTest.java
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.databind.convert;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
public class NumericConversionTest extends BaseMapTest
{
@@ -14,6 +15,8 @@
assertEquals(1, I.intValue());
IntWrapper w = MAPPER.readValue("{\"i\":-2.25 }", IntWrapper.class);
assertEquals(-2, w.i);
+ int[] arr = MAPPER.readValue("[ 1.25 ]", int[].class);
+ assertEquals(1, arr[0]);
try {
R.forType(Integer.class).readValue("1.5");
@@ -21,20 +24,24 @@
} catch (JsonMappingException e) {
verifyException(e, "Can not coerce a floating-point");
}
-
try {
R.forType(Integer.TYPE).readValue("1.5");
fail("Should not pass");
} catch (JsonMappingException e) {
verifyException(e, "Can not coerce a floating-point");
}
-
try {
R.forType(IntWrapper.class).readValue("{\"i\":-2.25 }");
fail("Should not pass");
} catch (JsonMappingException e) {
verifyException(e, "Can not coerce a floating-point");
}
+ try {
+ R.forType(int[].class).readValue("[ 2.5 ]");
+ fail("Should not pass");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not coerce a floating-point");
+ }
}
public void testDoubleToLong() throws Exception
@@ -44,25 +51,33 @@
assertEquals(3L, L.longValue());
LongWrapper w = MAPPER.readValue("{\"l\":-2.25 }", LongWrapper.class);
assertEquals(-2L, w.l);
+ long[] arr = MAPPER.readValue("[ 1.25 ]", long[].class);
+ assertEquals(1, arr[0]);
try {
R.forType(Long.class).readValue("1.5");
fail("Should not pass");
- } catch (JsonMappingException e) {
+ } catch (MismatchedInputException e) {
verifyException(e, "Can not coerce a floating-point");
}
try {
R.forType(Long.TYPE).readValue("1.5");
fail("Should not pass");
- } catch (JsonMappingException e) {
+ } catch (MismatchedInputException e) {
verifyException(e, "Can not coerce a floating-point");
}
try {
R.forType(LongWrapper.class).readValue("{\"l\": 7.7 }");
fail("Should not pass");
- } catch (JsonMappingException e) {
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Can not coerce a floating-point");
+ }
+ try {
+ R.forType(long[].class).readValue("[ 2.5 ]");
+ fail("Should not pass");
+ } catch (MismatchedInputException e) {
verifyException(e, "Can not coerce a floating-point");
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/ScalarConversionTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/ScalarConversionTest.java
new file mode 100644
index 0000000..0157f97
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/convert/ScalarConversionTest.java
@@ -0,0 +1,34 @@
+package com.fasterxml.jackson.databind.convert;
+
+import com.fasterxml.jackson.databind.*;
+
+public class ScalarConversionTest extends BaseMapTest
+{
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ // [databind#1433]
+ public void testConvertValueNullPrimitive() throws Exception
+ {
+ assertEquals(Byte.valueOf((byte) 0), MAPPER.convertValue(null, Byte.TYPE));
+ assertEquals(Short.valueOf((short) 0), MAPPER.convertValue(null, Short.TYPE));
+ assertEquals(Integer.valueOf(0), MAPPER.convertValue(null, Integer.TYPE));
+ assertEquals(Long.valueOf(0L), MAPPER.convertValue(null, Long.TYPE));
+ assertEquals(Float.valueOf(0f), MAPPER.convertValue(null, Float.TYPE));
+ assertEquals(Double.valueOf(0d), MAPPER.convertValue(null, Double.TYPE));
+ assertEquals(Character.valueOf('\0'), MAPPER.convertValue(null, Character.TYPE));
+ assertEquals(Boolean.FALSE, MAPPER.convertValue(null, Boolean.TYPE));
+ }
+
+ // [databind#1433]
+ public void testConvertValueNullBoxed() throws Exception
+ {
+ assertNull(MAPPER.convertValue(null, Byte.class));
+ assertNull(MAPPER.convertValue(null, Short.class));
+ assertNull(MAPPER.convertValue(null, Integer.class));
+ assertNull(MAPPER.convertValue(null, Long.class));
+ assertNull(MAPPER.convertValue(null, Float.class));
+ assertNull(MAPPER.convertValue(null, Double.class));
+ assertNull(MAPPER.convertValue(null, Character.class));
+ assertNull(MAPPER.convertValue(null, Boolean.class));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestBeanConversions.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestBeanConversions.java
index 34c161e..a71c033 100644
--- a/src/test/java/com/fasterxml/jackson/databind/convert/TestBeanConversions.java
+++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestBeanConversions.java
@@ -3,18 +3,22 @@
import java.util.LinkedHashMap;
import java.util.Map;
+import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.util.StdConverter;
+/**
+ * Tests for various conversions, especially ones using
+ * {@link ObjectMapper#convertValue(Object, Class)}.
+ */
public class TestBeanConversions
extends com.fasterxml.jackson.databind.BaseMapTest
{
- final ObjectMapper MAPPER = new ObjectMapper();
-
static class PointZ {
public int x, y;
@@ -64,7 +68,7 @@
public Leaf(int v) { value = v; }
}
- // [Issue#288]
+ // [databind#288]
@JsonSerialize(converter = ConvertingBeanConverter.class)
static class ConvertingBean {
@@ -90,6 +94,23 @@
return new DummyBean(cb.x, cb.y);
}
}
+
+ @JsonDeserialize(using = NullBeanDeserializer.class)
+ static class NullBean {
+ public static final NullBean NULL_INSTANCE = new NullBean();
+ }
+
+ static class NullBeanDeserializer extends JsonDeserializer<NullBean> {
+ @Override
+ public NullBean getNullValue(final DeserializationContext context) {
+ return NullBean.NULL_INSTANCE;
+ }
+
+ @Override
+ public NullBean deserialize(final JsonParser parser, final DeserializationContext context) {
+ throw new UnsupportedOperationException();
+ }
+ }
/*
/**********************************************************
@@ -97,6 +118,8 @@
/**********************************************************
*/
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
public void testBeanConvert()
{
// should have no problems convert between compatible beans...
@@ -252,5 +275,15 @@
String json = MAPPER.writeValueAsString(new ConvertingBean(1, 2));
// must be {"a":2,"b":4}
assertEquals("{\"a\":2,\"b\":4}", json);
- }
+ }
+
+ // Test null conversions from [databind#1433]
+ public void testConversionIssue1433() throws Exception
+ {
+ assertNull(MAPPER.convertValue(null, Object.class));
+ assertNull(MAPPER.convertValue(null, PointZ.class));
+
+ assertSame(NullBean.NULL_INSTANCE,
+ MAPPER.convertValue(null, NullBean.class));
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingDeserializer.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingDeserializer.java
index d88e397..5db3bdf 100644
--- a/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingDeserializer.java
+++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingDeserializer.java
@@ -114,7 +114,7 @@
@JsonDeserialize(converter=ToNumberConverter.class)
public Number value;
}
-
+
/*
/**********************************************************
/* Test methods
diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingSerializer.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingSerializer.java
index 938b67b..89b9e2f 100644
--- a/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingSerializer.java
+++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestConvertingSerializer.java
@@ -95,7 +95,22 @@
}
}
- // [Issue#359]
+ // [databind#357]
+ static class Value { }
+
+ static class ListWrapper {
+ @JsonSerialize(contentConverter = ValueToStringListConverter.class)
+ public List<Value> list = Arrays.asList(new Value());
+ }
+
+ static class ValueToStringListConverter extends StdConverter<Value, List<String>> {
+ @Override
+ public List<String> convert(Value value) {
+ return Arrays.asList("Hello world!");
+ }
+ }
+
+ // [databind#359]
static class Bean359 {
@JsonSerialize(as = List.class, contentAs = Source.class)
public List<Source> stuff = Arrays.asList(new Source());
@@ -124,7 +139,7 @@
}
}
- // [Issue#731]
+ // [databind#731]
public static class DummyBean {
public final int a, b;
public DummyBean(int v1, int v2) {
@@ -150,21 +165,6 @@
}
}
- // [databind#357]
- static class Value { }
-
- static class ListWrapper {
- @JsonSerialize(contentConverter = ValueToStringListConverter.class)
- public List<Value> list = Arrays.asList(new Value());
- }
-
- static class ValueToStringListConverter extends StdConverter<Value, List<String>> {
- @Override
- public List<String> convert(Value value) {
- return Arrays.asList("Hello world!");
- }
- }
-
/*
/**********************************************************
/* Test methods
@@ -205,6 +205,12 @@
assertEquals("{\"values\":{\"a\":[1,2]}}", json);
}
+ // [databind#357]
+ public void testConverterForList357() throws Exception {
+ String json = objectWriter().writeValueAsString(new ListWrapper());
+ assertEquals("{\"list\":[[\"Hello world!\"]]}", json);
+ }
+
// [databind#359]
public void testIssue359() throws Exception {
String json = objectWriter().writeValueAsString(new Bean359());
diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/TestUpdateValue.java b/src/test/java/com/fasterxml/jackson/databind/convert/TestUpdateViaObjectReader.java
similarity index 94%
rename from src/test/java/com/fasterxml/jackson/databind/convert/TestUpdateValue.java
rename to src/test/java/com/fasterxml/jackson/databind/convert/TestUpdateViaObjectReader.java
index ef86c5f..f125c1d 100644
--- a/src/test/java/com/fasterxml/jackson/databind/convert/TestUpdateValue.java
+++ b/src/test/java/com/fasterxml/jackson/databind/convert/TestUpdateViaObjectReader.java
@@ -15,14 +15,8 @@
* Unit tests for verifying that "updating reader" works as
* expected.
*/
-public class TestUpdateValue extends BaseMapTest
+public class TestUpdateViaObjectReader extends BaseMapTest
{
- /*
- /********************************************************
- /* Helper types
- /********************************************************
- */
-
static class Bean {
public String a = "a";
public String b = "b";
@@ -36,7 +30,6 @@
public int x, y;
}
- // [JACKSON-824]
public class TextView {}
public class NumView {}
@@ -70,9 +63,9 @@
@Override
public DataA deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
if (p.getCurrentToken() != JsonToken.START_OBJECT) {
- ctxt.reportWrongTokenException(p, JsonToken.START_OBJECT,
- "Wrong current token, expected START_OBJECT, got: %s",
- p.getCurrentToken());
+ ctxt.reportInputMismatch(DataA.class,
+ "Wrong current token, expected START_OBJECT, got: "
+ +p.getCurrentToken());
// never gets here
}
/*JsonNode node =*/ p.readValueAsTree();
@@ -85,7 +78,7 @@
/*
/********************************************************
- /* Unit tests
+ /* Test methods
/********************************************************
*/
diff --git a/src/test/java/com/fasterxml/jackson/databind/convert/UpdateValueTest.java b/src/test/java/com/fasterxml/jackson/databind/convert/UpdateValueTest.java
new file mode 100644
index 0000000..99db96c
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/convert/UpdateValueTest.java
@@ -0,0 +1,101 @@
+package com.fasterxml.jackson.databind.convert;
+
+import java.util.*;
+
+import com.fasterxml.jackson.databind.*;
+
+/**
+ * Tests for {@link ObjectMapper#updateValue}.
+ *
+ * @since 2.9
+ */
+public class UpdateValueTest extends BaseMapTest
+{
+ /*
+ /********************************************************
+ /* Test methods; simple containers
+ /********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testMapUpdate() throws Exception
+ {
+ Map<String,Object> base = new LinkedHashMap<>();
+ base.put("a", 345);
+ Map<String,Object> overrides = new LinkedHashMap<>();
+ overrides.put("xyz", Boolean.TRUE);
+ overrides.put("foo", "bar");
+
+ Map<String,Object> ob = MAPPER.updateValue(base, overrides);
+ // first: should return first argument
+ assertSame(base, ob);
+ assertEquals(3, ob.size());
+ assertEquals(Integer.valueOf(345), ob.get("a"));
+ assertEquals("bar", ob.get("foo"));
+ assertEquals(Boolean.TRUE, ob.get("xyz"));
+ }
+
+ public void testListUpdate() throws Exception
+ {
+ List<Object> base = new ArrayList<>();
+ base.add(123456);
+ base.add(Boolean.FALSE);
+ Object[] overrides = new Object[] { Boolean.TRUE, "zoink!" };
+
+ List<Object> ob = MAPPER.updateValue(base, overrides);
+ // first: should return first argument
+ assertSame(base, ob);
+ assertEquals(4, ob.size());
+ assertEquals(Integer.valueOf(123456), ob.get(0));
+ assertEquals(Boolean.FALSE, ob.get(1));
+ assertEquals(overrides[0], ob.get(2));
+ assertEquals(overrides[1], ob.get(3));
+ }
+
+ public void testArrayUpdate() throws Exception
+ {
+ // Since Arrays are immutable, not sure what "right answer" ought to be
+ Object[] base = new Object[] { Boolean.FALSE, Integer.valueOf(3) };
+ Object[] overrides = new Object[] { Boolean.TRUE, "zoink!" };
+
+ Object[] ob = MAPPER.updateValue(base, overrides);
+ assertEquals(4, ob.length);
+ assertEquals(base[0], ob[0]);
+ assertEquals(base[1], ob[1]);
+ assertEquals(overrides[0], ob[2]);
+ assertEquals(overrides[1], ob[3]);
+ }
+
+ /*
+ /********************************************************
+ /* Test methods; POJOs
+ /********************************************************
+ */
+
+ public void testPOJO() throws Exception
+ {
+ Point base = new Point(42, 28);
+ Map<String,Object> overrides = new LinkedHashMap<>();
+ overrides.put("y", 1234);
+ Point result = MAPPER.updateValue(base, overrides);
+ assertSame(base, result);
+ assertEquals(42, result.x);
+ assertEquals(1234, result.y);
+ }
+
+ /*
+ /********************************************************
+ /* Test methods; other
+ /********************************************************
+ */
+
+ public void testMisc() throws Exception
+ {
+ // if either is `null`, should return first arg
+ assertNull(MAPPER.updateValue(null, "foo"));
+ List<String> input = new ArrayList<>();
+ assertSame(input, MAPPER.updateValue(input, null));
+ }
+
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/creators/Creator1476Test.java b/src/test/java/com/fasterxml/jackson/databind/creators/Creator1476Test.java
deleted file mode 100644
index 94a1eee..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/creators/Creator1476Test.java
+++ /dev/null
@@ -1,43 +0,0 @@
-package com.fasterxml.jackson.databind.creators;
-
-import com.fasterxml.jackson.annotation.*;
-
-import com.fasterxml.jackson.databind.*;
-
-public class Creator1476Test extends BaseMapTest
-{
- static final class SimplePojo {
- private final int intField;
- private final String stringField;
-
- public SimplePojo(@JsonProperty("intField") int intField) {
- this(intField, "empty");
- }
-
- public SimplePojo(@JsonProperty("stringField") String stringField) {
- this(-1, stringField);
- }
-
- @JsonCreator
- public SimplePojo(@JsonProperty("intField") int intField, @JsonProperty("stringField") String stringField) {
- this.intField = intField;
- this.stringField = stringField;
- }
-
- public int getIntField() {
- return intField;
- }
-
- public String getStringField() {
- return stringField;
- }
- }
-
- public void testConstructorChoice() throws Exception {
- ObjectMapper mapper = new ObjectMapper();
- SimplePojo pojo = mapper.readValue("{ \"intField\": 1, \"stringField\": \"foo\" }", SimplePojo.class);
-
- assertEquals(1, pojo.getIntField());
- assertEquals("foo", pojo.getStringField());
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/creators/CreatorPropertiesTest.java b/src/test/java/com/fasterxml/jackson/databind/creators/CreatorPropertiesTest.java
index 93ad973..4df4a37 100644
--- a/src/test/java/com/fasterxml/jackson/databind/creators/CreatorPropertiesTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/creators/CreatorPropertiesTest.java
@@ -25,7 +25,6 @@
// for [databind#1122]
static class Ambiguity {
-
@JsonProperty("bar")
private int foo;
@@ -46,6 +45,19 @@
}
}
+ // for [databind#1371]
+ static class Lombok1371Bean {
+ public int x, y;
+
+ protected Lombok1371Bean() { }
+
+ @ConstructorProperties({ "x", "y" })
+ public Lombok1371Bean(int _x, int _y) {
+ x = _x + 1;
+ y = _y + 1;
+ }
+ }
+
/*
/**********************************************************
/* Test methods
@@ -71,4 +83,24 @@
assertNotNull(amb);
assertEquals(3, amb.getFoo());
}
+
+ // [databind#1371]: MapperFeature.INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES
+ public void testConstructorPropertiesInference() throws Exception
+ {
+ final String JSON = aposToQuotes("{'x':3,'y':5}");
+
+ // by default, should detect and use arguments-taking constructor as creator
+ assertTrue(MAPPER.isEnabled(MapperFeature.INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES));
+ Lombok1371Bean result = MAPPER.readValue(JSON, Lombok1371Bean.class);
+ assertEquals(4, result.x);
+ assertEquals(6, result.y);
+
+ // but change if configuration changed
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.disable(MapperFeature.INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES);
+ // in which case fields are set directly:
+ result = mapper.readValue(JSON, Lombok1371Bean.class);
+ assertEquals(3, result.x);
+ assertEquals(5, result.y);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/creators/MultiArgConstructorTest.java b/src/test/java/com/fasterxml/jackson/databind/creators/MultiArgConstructorTest.java
index 883457c..ecbdff7 100644
--- a/src/test/java/com/fasterxml/jackson/databind/creators/MultiArgConstructorTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/creators/MultiArgConstructorTest.java
@@ -1,9 +1,11 @@
package com.fasterxml.jackson.databind.creators;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.PropertyAccessor;
+
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
@@ -92,13 +94,15 @@
{
final ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new MyParamIntrospector());
- mapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE);
+ mapper.setDefaultVisibility(
+ JsonAutoDetect.Value.noOverrides()
+ .withCreatorVisibility(Visibility.NONE));
try {
/*MultiArgCtorBean bean =*/ mapper.readValue(aposToQuotes("{'b':13, 'a':-99}"),
MultiArgCtorBean.class);
fail("Should not have passed");
- } catch (JsonMappingException e) {
- verifyException(e, "No suitable constructor");
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "no Creators");
}
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/creators/TestCreators2.java b/src/test/java/com/fasterxml/jackson/databind/creators/TestCreators2.java
index f075bcb..b5e4429 100644
--- a/src/test/java/com/fasterxml/jackson/databind/creators/TestCreators2.java
+++ b/src/test/java/com/fasterxml/jackson/databind/creators/TestCreators2.java
@@ -79,16 +79,6 @@
}
}
- static class MapBean
- {
- protected Map<String,Long> map;
-
- @JsonCreator
- public MapBean(Map<String, Long> map) {
- this.map = map;
- }
- }
-
// For [JACKSON-470]: should be appropriately detected, reported error about
static class BrokenCreatorBean
{
@@ -159,6 +149,34 @@
public String getItem() { return null; }
}
+ static final class MultiPropCreator1476 {
+ private final int intField;
+ private final String stringField;
+
+ public MultiPropCreator1476(@JsonProperty("intField") int intField) {
+ this(intField, "empty");
+ }
+
+ public MultiPropCreator1476(@JsonProperty("stringField") String stringField) {
+ this(-1, stringField);
+ }
+
+ @JsonCreator
+ public MultiPropCreator1476(@JsonProperty("intField") int intField,
+ @JsonProperty("stringField") String stringField) {
+ this.intField = intField;
+ this.stringField = stringField;
+ }
+
+ public int getIntField() {
+ return intField;
+ }
+
+ public String getStringField() {
+ return stringField;
+ }
+ }
+
/*
/**********************************************************
/* Test methods
@@ -176,6 +194,9 @@
verifyException(e, ": foobar");
// also: should have nested exception
Throwable t = e.getCause();
+ if (t == null) {
+ fail("Should have assigned cause for: ("+e.getClass().getSimpleName()+") "+e);
+ }
assertNotNull(t);
assertEquals(IllegalArgumentException.class, t.getClass());
assertEquals("foobar", t.getMessage());
@@ -210,47 +231,27 @@
assertNotNull(foo);
}
- // Catch and rethrow exceptions that Creator methods throw
+ // Catch and re-throw exceptions that Creator methods throw
public void testJackson438() throws Exception
{
+ Exception e = null;
try {
MAPPER.readValue("{ \"name\":\"foobar\" }", BeanFor438.class);
fail("Should have failed");
- } catch (Exception e) {
- if (!(e instanceof JsonMappingException)) {
- fail("Should have received JsonMappingException, caught "+e.getClass().getName());
- }
- verifyException(e, "don't like that name");
- // Ok: also, let's ensure root cause is directly linked, without other extra wrapping:
- Throwable t = e.getCause();
- assertNotNull(t);
- assertEquals(IllegalArgumentException.class, t.getClass());
- verifyException(e, "don't like that name");
+ } catch (Exception e0) {
+ e = e0;
}
- }
-
- @SuppressWarnings("unchecked")
- public void testIssue465() throws Exception
- {
- final String JSON = "{\"A\":12}";
-
- // first, test with regular Map, non empty
- Map<String,Long> map = MAPPER.readValue(JSON, Map.class);
- assertEquals(1, map.size());
- assertEquals(Integer.valueOf(12), map.get("A"));
-
- MapBean bean = MAPPER.readValue(JSON, MapBean.class);
- assertEquals(1, bean.map.size());
- assertEquals(Long.valueOf(12L), bean.map.get("A"));
-
- // and then empty ones
- final String EMPTY_JSON = "{}";
-
- map = MAPPER.readValue(EMPTY_JSON, Map.class);
- assertEquals(0, map.size());
-
- bean = MAPPER.readValue(EMPTY_JSON, MapBean.class);
- assertEquals(0, bean.map.size());
+ if (!(e instanceof JsonMappingException)) {
+ fail("Should have received JsonMappingException, caught "+e.getClass().getName());
+ }
+ verifyException(e, "don't like that name");
+ // Ok: also, let's ensure root cause is directly linked, without other extra wrapping:
+ Throwable t = e.getCause();
+ if (t == null) {
+ fail("Should have assigned cause for: ("+e.getClass().getSimpleName()+") "+e);
+ }
+ assertEquals(IllegalArgumentException.class, t.getClass());
+ verifyException(e, "don't like that name");
}
public void testCreatorWithDupNames() throws Exception
@@ -294,4 +295,13 @@
Issue700Bean value = MAPPER.readValue("{ \"item\" : \"foo\" }", Issue700Bean.class);
assertNotNull(value);
}
+
+ // [databind#1476]
+ public void testConstructorChoice() throws Exception {
+ ObjectMapper mapper = new ObjectMapper();
+ MultiPropCreator1476 pojo = mapper.readValue("{ \"intField\": 1, \"stringField\": \"foo\" }",
+ MultiPropCreator1476.class);
+ assertEquals(1, pojo.getIntField());
+ assertEquals("foo", pojo.getStringField());
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/creators/TestCreatorsDelegating.java b/src/test/java/com/fasterxml/jackson/databind/creators/TestCreatorsDelegating.java
index 0989f8b..18e59ef 100644
--- a/src/test/java/com/fasterxml/jackson/databind/creators/TestCreatorsDelegating.java
+++ b/src/test/java/com/fasterxml/jackson/databind/creators/TestCreatorsDelegating.java
@@ -1,6 +1,9 @@
package com.fasterxml.jackson.databind.creators;
import com.fasterxml.jackson.annotation.JsonCreator;
+
+import java.util.Map;
+
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.core.JsonParser;
@@ -70,21 +73,32 @@
}
}
+ static class MapBean
+ {
+ protected Map<String,Long> map;
+
+ @JsonCreator
+ public MapBean(Map<String, Long> map) {
+ this.map = map;
+ }
+ }
+
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
public void testBooleanDelegate() throws Exception
{
- ObjectMapper m = new ObjectMapper();
// should obviously work with booleans...
- BooleanBean bb = m.readValue("true", BooleanBean.class);
+ BooleanBean bb = MAPPER.readValue("true", BooleanBean.class);
assertEquals(Boolean.TRUE, bb.value);
// but also with value conversion from String
- bb = m.readValue(quote("true"), BooleanBean.class);
+ bb = MAPPER.readValue(quote("true"), BooleanBean.class);
assertEquals(Boolean.TRUE, bb.value);
}
@@ -125,8 +139,7 @@
// [databind#592]
public void testDelegateWithTokenBuffer() throws Exception
{
- ObjectMapper mapper = new ObjectMapper();
- Value592 value = mapper.readValue("{\"a\":1,\"b\":2}", Value592.class);
+ Value592 value = MAPPER.readValue("{\"a\":1,\"b\":2}", Value592.class);
assertNotNull(value);
Object ob = value.stuff;
assertEquals(TokenBuffer.class, ob.getClass());
@@ -144,4 +157,27 @@
jp.close();
}
+ @SuppressWarnings("unchecked")
+ public void testIssue465() throws Exception
+ {
+ final String JSON = "{\"A\":12}";
+
+ // first, test with regular Map, non empty
+ Map<String,Long> map = MAPPER.readValue(JSON, Map.class);
+ assertEquals(1, map.size());
+ assertEquals(Integer.valueOf(12), map.get("A"));
+
+ MapBean bean = MAPPER.readValue(JSON, MapBean.class);
+ assertEquals(1, bean.map.size());
+ assertEquals(Long.valueOf(12L), bean.map.get("A"));
+
+ // and then empty ones
+ final String EMPTY_JSON = "{}";
+
+ map = MAPPER.readValue(EMPTY_JSON, Map.class);
+ assertEquals(0, map.size());
+
+ bean = MAPPER.readValue(EMPTY_JSON, MapBean.class);
+ assertEquals(0, bean.map.size());
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/creators/TestValueInstantiator.java b/src/test/java/com/fasterxml/jackson/databind/creators/TestValueInstantiator.java
index 744d3d0..fd0feb7 100644
--- a/src/test/java/com/fasterxml/jackson/databind/creators/TestValueInstantiator.java
+++ b/src/test/java/com/fasterxml/jackson/databind/creators/TestValueInstantiator.java
@@ -1,12 +1,15 @@
package com.fasterxml.jackson.databind.creators;
import java.io.IOException;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonValueInstantiator;
import com.fasterxml.jackson.databind.deser.*;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.TypeFactory;
@@ -19,7 +22,7 @@
static class MyBean
{
String _secret;
-
+
public MyBean(String s, boolean bogus) {
_secret = s;
}
@@ -28,16 +31,16 @@
static class MysteryBean
{
Object value;
-
+
public MysteryBean(Object v) { value = v; }
}
-
+
static class CreatorBean
{
String _secret;
public String value;
-
+
protected CreatorBean(String s) {
_secret = s;
}
@@ -587,7 +590,9 @@
fail("Should not succeed");
} catch (JsonMappingException e) {
verifyException(e, "Can not construct instance of");
- verifyException(e, "missing default constructor");
+ verifyException(e, "no Creators");
+ // as per [databind#1414], is definition problem
+ assertEquals(InvalidDefinitionException.class, e.getClass());
}
}
@@ -601,6 +606,8 @@
} catch (JsonMappingException e) {
verifyException(e, "Can not construct instance of");
verifyException(e, "no String-argument constructor/factory");
+ // as per [databind#1414], is definition problem
+ assertEquals(InvalidDefinitionException.class, e.getClass());
}
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/AnySetter349Test.java b/src/test/java/com/fasterxml/jackson/databind/deser/AnySetter349Test.java
index 5ff1d2b..7cfa4d4 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/AnySetter349Test.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/AnySetter349Test.java
@@ -12,6 +12,7 @@
static class Bean349
{
public String type;
+ public int x, y;
private Map<String, Object> props = new HashMap<>();
@@ -32,18 +33,34 @@
static class IdentityDTO349 {
public int x, y;
}
+
+ final static String UNWRAPPED_JSON_349 = aposToQuotes(
+"{ 'type' : 'IST',\n"
++" 'x' : 3,\n"
+//+" 'name' : 'BLAH-New',\n"
+//+" 'description' : 'namespace.name: X THIN FIR.DR-WD12-New',\n"
++" 'ZoomLinks': [ 'foofoofoofoo', 'barbarbarbar' ],\n"
++" 'y' : 4, 'z' : 8 }"
+ );
public void testUnwrappedWithAny() throws Exception
{
final ObjectMapper mapper = objectMapper();
- final String json = aposToQuotes(
-"{ 'type' : 'IST',\n"
-//+" 'spacename' : 'Foo Models',\n"
-//+" 'name' : 'BLAH-New',\n"
-//+" 'description' : 'namespace.name: X THIN FIR.DR-WD12-New',\n"
-+" 'ZoomLinks': [ 'foofoofoofoo', 'barbarbarbar' ] }"
- );
- Bean349 value = mapper.readValue(json, Bean349.class);
+ Bean349 value = mapper.readValue(UNWRAPPED_JSON_349, Bean349.class);
assertNotNull(value);
+ assertEquals(3, value.x);
+ assertEquals(4, value.y);
+ assertEquals(2, value.props.size());
+ }
+
+ public void testUnwrappedWithAnyAsUpdate() throws Exception
+ {
+ final ObjectMapper mapper = objectMapper();
+ Bean349 bean = mapper.readerFor(Bean349.class)
+ .withValueToUpdate(new Bean349())
+ .readValue(UNWRAPPED_JSON_349);
+ assertEquals(3, bean.x);
+ assertEquals(4, bean.y);
+ assertEquals(2, bean.props.size());
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestAnyProperties.java b/src/test/java/com/fasterxml/jackson/databind/deser/AnySetterTest.java
similarity index 62%
rename from src/test/java/com/fasterxml/jackson/databind/deser/TestAnyProperties.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/AnySetterTest.java
index fb7ef8e..598e9b2 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestAnyProperties.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/AnySetterTest.java
@@ -10,7 +10,7 @@
* Unit tests for verifying that {@link JsonAnySetter} annotation
* works as expected.
*/
-public class TestAnyProperties
+public class AnySetterTest
extends BaseMapTest
{
static class MapImitator
@@ -28,6 +28,16 @@
}
}
+ // for [databind#1376]
+ static class MapImitatorDisabled extends MapImitator
+ {
+ @Override
+ @JsonAnySetter(enabled=false)
+ void addEntry(String key, Object value) {
+ throw new RuntimeException("Should not get called");
+ }
+ }
+
/**
* Let's also verify that it is possible to define different
* value: not often useful, but possible.
@@ -135,34 +145,77 @@
}
}
- static class JsonAnySetterOnMap {
- public int id;
+ static class JsonAnySetterOnMap {
+ public int id;
- @JsonAnySetter
- protected HashMap<String, String> other = new HashMap<String, String>();
+ @JsonAnySetter
+ protected HashMap<String, String> other = new HashMap<String, String>();
- @JsonAnyGetter
- public Map<String, String> any() {
- return other;
- }
+ @JsonAnyGetter
+ public Map<String, String> any() {
+ return other;
+ }
+ }
- }
+ static class JsonAnySetterOnNullMap {
+ public int id;
- static class JsonAnySetterOnNullMap {
- public int id;
+ @JsonAnySetter
+ protected HashMap<String, String> other;
- @JsonAnySetter
- protected HashMap<String, String> other;
+ @JsonAnyGetter
+ public Map<String, String> any() {
+ return other;
+ }
+ }
- @JsonAnyGetter
- public Map<String, String> any() {
- return other;
- }
-
- }
-
+ static class MyGeneric<T>
+ {
+ private String staticallyMappedProperty;
+ private Map<T, Integer> dynamicallyMappedProperties = new HashMap<T, Integer>();
- /*
+ public String getStaticallyMappedProperty() {
+ return staticallyMappedProperty;
+ }
+
+ @JsonAnySetter
+ public void addDynamicallyMappedProperty(T key, int value) {
+ dynamicallyMappedProperties.put(key, value);
+ }
+
+ public void setStaticallyMappedProperty(String staticallyMappedProperty) {
+ this.staticallyMappedProperty = staticallyMappedProperty;
+ }
+
+ @JsonAnyGetter
+ public Map<T, Integer> getDynamicallyMappedProperties() {
+ return dynamicallyMappedProperties;
+ }
+ }
+
+ static class MyWrapper
+ {
+ private MyGeneric<String> myStringGeneric;
+ private MyGeneric<Integer> myIntegerGeneric;
+
+ public MyGeneric<String> getMyStringGeneric() {
+ return myStringGeneric;
+ }
+
+ public void setMyStringGeneric(MyGeneric<String> myStringGeneric) {
+ this.myStringGeneric = myStringGeneric;
+ }
+
+ public MyGeneric<Integer> getMyIntegerGeneric() {
+ return myIntegerGeneric;
+ }
+
+ public void setMyIntegerGeneric(MyGeneric<Integer> myIntegerGeneric) {
+ this.myIntegerGeneric = myIntegerGeneric;
+ }
+ }
+
+ /*
/**********************************************************
/* Test methods
/**********************************************************
@@ -185,6 +238,18 @@
assertEquals(Integer.valueOf(3), l.get(2));
}
+ public void testAnySetterDisable() throws Exception
+ {
+ try {
+ MAPPER.readValue(aposToQuotes("{'value':3}"),
+ MapImitatorDisabled.class);
+ fail("Should not pass");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Unrecognized field \"value\"");
+ }
+
+ }
+
public void testSimpleTyped() throws Exception
{
MapImitatorWithValue mapHolder = MAPPER.readValue
@@ -202,7 +267,7 @@
Broken b = MAPPER.readValue("{ \"a\" : 3 }", Broken.class);
fail("Should have gotten an exception");
} catch (JsonMappingException e) {
- verifyException(e, "Multiple 'any-setters'");
+ verifyException(e, "Multiple 'any-setter' methods");
}
}
@@ -264,9 +329,44 @@
JsonAnySetterOnNullMap.class);
assertEquals(2, result.id);
assertNull(result.other);
- }
-
- /*
+ }
+
+ // [databind#1035]
+ public void testGenericAnySetter() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper();
+
+ Map<String, Integer> stringGenericMap = new HashMap<String, Integer>();
+ stringGenericMap.put("testStringKey", 5);
+ Map<Integer, Integer> integerGenericMap = new HashMap<Integer, Integer>();
+ integerGenericMap.put(111, 6);
+
+ MyWrapper deserialized = mapper.readValue(aposToQuotes(
+ "{'myStringGeneric':{'staticallyMappedProperty':'Test','testStringKey':5},'myIntegerGeneric':{'staticallyMappedProperty':'Test2','111':6}}"
+ ), MyWrapper.class);
+ MyGeneric<String> stringGeneric = deserialized.getMyStringGeneric();
+ MyGeneric<Integer> integerGeneric = deserialized.getMyIntegerGeneric();
+
+ assertNotNull(stringGeneric);
+ assertEquals(stringGeneric.getStaticallyMappedProperty(), "Test");
+ for(Map.Entry<String, Integer> entry : stringGeneric.getDynamicallyMappedProperties().entrySet()) {
+ assertTrue("A key in MyGeneric<String> is not an String.", entry.getKey() instanceof String);
+ assertTrue("A value in MyGeneric<Integer> is not an Integer.", entry.getValue() instanceof Integer);
+ }
+ assertEquals(stringGeneric.getDynamicallyMappedProperties(), stringGenericMap);
+
+ assertNotNull(integerGeneric);
+ assertEquals(integerGeneric.getStaticallyMappedProperty(), "Test2");
+ for(Map.Entry<Integer, Integer> entry : integerGeneric.getDynamicallyMappedProperties().entrySet()) {
+ Object key = entry.getKey();
+ assertEquals("A key in MyGeneric<Integer> is not an Integer.", Integer.class, key.getClass());
+ Object value = entry.getValue();
+ assertEquals("A value in MyGeneric<Integer> is not an Integer.", Integer.class, value.getClass());
+ }
+ assertEquals(integerGeneric.getDynamicallyMappedProperties(), integerGenericMap);
+ }
+
+ /*
/**********************************************************
/* Private helper methods
/**********************************************************
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestAnnotationIgnore.java b/src/test/java/com/fasterxml/jackson/databind/deser/IgnoreWithDeserTest.java
similarity index 98%
rename from src/test/java/com/fasterxml/jackson/databind/deser/TestAnnotationIgnore.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/IgnoreWithDeserTest.java
index 74c656c..e1dfa5b 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestAnnotationIgnore.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/IgnoreWithDeserTest.java
@@ -8,7 +8,7 @@
* This unit test suite that tests use of {@link JsonIgnore}
* annotation with deserialization.
*/
-public class TestAnnotationIgnore
+public class IgnoreWithDeserTest
extends BaseMapTest
{
// Class for testing {@link JsonIgnore} annotations with setters
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/JDKScalarsTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/JDKScalarsTest.java
deleted file mode 100644
index 7679745..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/deser/JDKScalarsTest.java
+++ /dev/null
@@ -1,1002 +0,0 @@
-package com.fasterxml.jackson.databind.deser;
-
-import java.io.*;
-import java.math.BigDecimal;
-import java.math.BigInteger;
-
-import org.junit.Assert;
-
-import com.fasterxml.jackson.annotation.JsonCreator;
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.*;
-
-/**
- * Unit tests for verifying handling of simple basic non-structured
- * types; primitives (and/or their wrappers), Strings.
- */
-public class JDKScalarsTest
- extends BaseMapTest
-{
- final static String NAN_STRING = "NaN";
-
- final static class BooleanBean {
- boolean _v;
- void setV(boolean v) { _v = v; }
- }
-
- static class BooleanWrapper {
- public Boolean wrapper;
- public boolean primitive;
-
- protected Boolean ctor;
-
- @JsonCreator
- public BooleanWrapper(@JsonProperty("ctor") Boolean foo) {
- ctor = foo;
- }
- }
-
- static class IntBean {
- int _v;
- void setV(int v) { _v = v; }
- }
-
- final static class DoubleBean {
- double _v;
- void setV(double v) { _v = v; }
- }
-
- final static class FloatBean {
- float _v;
- void setV(float v) { _v = v; }
- }
-
- final static class CharacterBean {
- char _v;
- void setV(char v) { _v = v; }
- char getV() { return _v; }
- }
-
- final static class CharacterWrapperBean {
- Character _v;
- void setV(Character v) { _v = v; }
- Character getV() { return _v; }
- }
-
- /**
- * Also, let's ensure that it's ok to override methods.
- */
- static class IntBean2
- extends IntBean
- {
- @Override
- void setV(int v2) { super.setV(v2+1); }
- }
-
- static class PrimitivesBean
- {
- public boolean booleanValue = true;
- public byte byteValue = 3;
- public char charValue = 'a';
- public short shortValue = 37;
- public int intValue = 1;
- public long longValue = 100L;
- public float floatValue = 0.25f;
- public double doubleValue = -1.0;
- }
-
- static class WrappersBean
- {
- public Boolean booleanValue;
- public Byte byteValue;
- public Character charValue;
- public Short shortValue;
- public Integer intValue;
- public Long longValue;
- public Float floatValue;
- public Double doubleValue;
- }
-
- private final ObjectMapper MAPPER = new ObjectMapper();
-
- /*
- /**********************************************************
- /* Scalar tests for boolean
- /**********************************************************
- */
-
- public void testBooleanPrimitive() throws Exception
- {
- // first, simple case:
- BooleanBean result = MAPPER.readValue(new StringReader("{\"v\":true}"), BooleanBean.class);
- assertTrue(result._v);
- result = MAPPER.readValue(new StringReader("{\"v\":null}"), BooleanBean.class);
- assertNotNull(result);
- assertFalse(result._v);
- // [databind#1480]
- result = MAPPER.readValue(new StringReader("{\"v\":1}"), BooleanBean.class);
- assertNotNull(result);
- assertTrue(result._v);
-
- // should work with arrays too..
- boolean[] array = MAPPER.readValue(new StringReader("[ null ]"), boolean[].class);
- assertNotNull(array);
- assertEquals(1, array.length);
- assertFalse(array[0]);
-
- }
-
- public void testBooleanPrimitiveArrayUnwrap() throws Exception
- {
- // [databind#381]
- final ObjectMapper mapper = new ObjectMapper();
- mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
- BooleanBean result = mapper.readValue(new StringReader("{\"v\":[true]}"), BooleanBean.class);
- assertTrue(result._v);
-
- try {
- mapper.readValue(new StringReader("[{\"v\":[true,true]}]"), BooleanBean.class);
- fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled");
- } catch (JsonMappingException exp) {
- //threw exception as required
- }
-
- result = mapper.readValue(new StringReader("{\"v\":[null]}"), BooleanBean.class);
- assertNotNull(result);
- assertFalse(result._v);
-
- result = mapper.readValue(new StringReader("[{\"v\":[null]}]"), BooleanBean.class);
- assertNotNull(result);
- assertFalse(result._v);
-
- boolean[] array = mapper.readValue(new StringReader("[ [ null ] ]"), boolean[].class);
- assertNotNull(array);
- assertEquals(1, array.length);
- assertFalse(array[0]);
- }
-
- /**
- * Simple unit test to verify that we can map boolean values to
- * java.lang.Boolean.
- */
- public void testBooleanWrapper() throws Exception
- {
- Boolean result = MAPPER.readValue(new StringReader("true"), Boolean.class);
- assertEquals(Boolean.TRUE, result);
- result = MAPPER.readValue(new StringReader("false"), Boolean.class);
- assertEquals(Boolean.FALSE, result);
-
- // should accept ints too, (0 == false, otherwise true)
- result = MAPPER.readValue("0", Boolean.class);
- assertEquals(Boolean.FALSE, result);
- result = MAPPER.readValue("1", Boolean.class);
- assertEquals(Boolean.TRUE, result);
- }
-
- // Test for verifying that Long values are coerced to boolean correctly as well
- public void testLongToBoolean() throws Exception
- {
- long value = 1L + Integer.MAX_VALUE;
- BooleanWrapper b = MAPPER.readValue("{\"primitive\" : "+value+", \"wrapper\":"+value+", \"ctor\":"+value+"}",
- BooleanWrapper.class);
- assertEquals(Boolean.TRUE, b.wrapper);
- assertTrue(b.primitive);
- assertEquals(Boolean.TRUE, b.ctor);
-
- // but ensure we can also get `false`
- b = MAPPER.readValue("{\"primitive\" : 0 , \"wrapper\":0, \"ctor\":0}",
- BooleanWrapper.class);
- assertEquals(Boolean.FALSE, b.wrapper);
- assertFalse(b.primitive);
- assertEquals(Boolean.FALSE, b.ctor);
- }
-
- /*
- /**********************************************************
- /* Scalar tests for integral types
- /**********************************************************
- */
-
- public void testIntPrimitive() throws Exception
- {
- // first, simple case:
- IntBean result = MAPPER.readValue(new StringReader("{\"v\":3}"), IntBean.class);
- assertEquals(3, result._v);
- result = MAPPER.readValue(new StringReader("{\"v\":null}"), IntBean.class);
- assertNotNull(result);
- assertEquals(0, result._v);
-
- // should work with arrays too..
- int[] array = MAPPER.readValue(new StringReader("[ null ]"), int[].class);
- assertNotNull(array);
- assertEquals(1, array.length);
- assertEquals(0, array[0]);
-
- // [Issue#381]
- final ObjectMapper mapper = new ObjectMapper();
- mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
- try {
- mapper.readValue(new StringReader("{\"v\":[3]}"), IntBean.class);
- fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled");
- } catch (JsonMappingException exp) {
- //Correctly threw exception
- }
-
- mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
-
- result = mapper.readValue(new StringReader("{\"v\":[3]}"), IntBean.class);
- assertEquals(3, result._v);
-
- result = mapper.readValue(new StringReader("[{\"v\":[3]}]"), IntBean.class);
- assertEquals(3, result._v);
-
- try {
- mapper.readValue("[{\"v\":[3,3]}]", IntBean.class);
- fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled");
- } catch (JsonMappingException exp) {
- //threw exception as required
- }
-
- result = mapper.readValue("{\"v\":[null]}", IntBean.class);
- assertNotNull(result);
- assertEquals(0, result._v);
-
- array = mapper.readValue("[ [ null ] ]", int[].class);
- assertNotNull(array);
- assertEquals(1, array.length);
- assertEquals(0, array[0]);
- }
-
- public void testByteWrapper() throws Exception
- {
- Byte result = MAPPER.readValue(new StringReader(" -42\t"), Byte.class);
- assertEquals(Byte.valueOf((byte)-42), result);
-
- // Also: should be able to coerce floats, strings:
- result = MAPPER.readValue(new StringReader(" \"-12\""), Byte.class);
- assertEquals(Byte.valueOf((byte)-12), result);
-
- result = MAPPER.readValue(new StringReader(" 39.07"), Byte.class);
- assertEquals(Byte.valueOf((byte)39), result);
- }
-
- public void testShortWrapper() throws Exception
- {
- Short result = MAPPER.readValue(new StringReader("37"), Short.class);
- assertEquals(Short.valueOf((short)37), result);
-
- // Also: should be able to coerce floats, strings:
- result = MAPPER.readValue(new StringReader(" \"-1009\""), Short.class);
- assertEquals(Short.valueOf((short)-1009), result);
-
- result = MAPPER.readValue(new StringReader("-12.9"), Short.class);
- assertEquals(Short.valueOf((short)-12), result);
- }
-
- public void testCharacterWrapper() throws Exception
- {
- // First: canonical value is 1-char string
- Character result = MAPPER.readValue(new StringReader("\"a\""), Character.class);
- assertEquals(Character.valueOf('a'), result);
-
- // But can also pass in ascii code
- result = MAPPER.readValue(new StringReader(" "+((int) 'X')), Character.class);
- assertEquals(Character.valueOf('X'), result);
-
- final CharacterWrapperBean wrapper = MAPPER.readValue(new StringReader("{\"v\":null}"), CharacterWrapperBean.class);
- assertNotNull(wrapper);
- assertNull(wrapper.getV());
-
- final ObjectMapper mapper = new ObjectMapper();
- mapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
- try {
- mapper.readValue("{\"v\":null}", CharacterBean.class);
- fail("Attempting to deserialize a 'null' JSON reference into a 'char' property did not throw an exception");
- } catch (JsonMappingException exp) {
- //Exception thrown as required
- }
-
- mapper.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
- final CharacterBean charBean = MAPPER.readValue(new StringReader("{\"v\":null}"), CharacterBean.class);
- assertNotNull(wrapper);
- assertEquals('\u0000', charBean.getV());
- }
-
- public void testIntWrapper() throws Exception
- {
- Integer result = MAPPER.readValue(new StringReader(" -42\t"), Integer.class);
- assertEquals(Integer.valueOf(-42), result);
-
- // Also: should be able to coerce floats, strings:
- result = MAPPER.readValue(new StringReader(" \"-1200\""), Integer.class);
- assertEquals(Integer.valueOf(-1200), result);
-
- result = MAPPER.readValue(new StringReader(" 39.07"), Integer.class);
- assertEquals(Integer.valueOf(39), result);
- }
-
- public void testLongWrapper() throws Exception
- {
- Long result = MAPPER.readValue(new StringReader("12345678901"), Long.class);
- assertEquals(Long.valueOf(12345678901L), result);
-
- // Also: should be able to coerce floats, strings:
- result = MAPPER.readValue(new StringReader(" \"-9876\""), Long.class);
- assertEquals(Long.valueOf(-9876), result);
-
- result = MAPPER.readValue(new StringReader("1918.3"), Long.class);
- assertEquals(Long.valueOf(1918), result);
- }
-
- /**
- * Beyond simple case, let's also ensure that method overriding works as
- * expected.
- */
- public void testIntWithOverride() throws Exception
- {
- IntBean2 result = MAPPER.readValue(new StringReader("{\"v\":8}"), IntBean2.class);
- assertEquals(9, result._v);
- }
-
- /*
- /**********************************************************
- /* Scalar tests for floating point types
- /**********************************************************
- */
-
- public void testDoublePrimitive() throws Exception
- {
- // first, simple case:
- // bit tricky with binary fps but...
- final double value = 0.016;
- DoubleBean result = MAPPER.readValue(new StringReader("{\"v\":"+value+"}"), DoubleBean.class);
- assertEquals(value, result._v);
- // then [JACKSON-79]:
- result = MAPPER.readValue(new StringReader("{\"v\":null}"), DoubleBean.class);
- assertNotNull(result);
- assertEquals(0.0, result._v);
-
- // should work with arrays too..
- double[] array = MAPPER.readValue(new StringReader("[ null ]"), double[].class);
- assertNotNull(array);
- assertEquals(1, array.length);
- assertEquals(0.0, array[0]);
- }
-
- /* Note: dealing with floating-point values is tricky; not sure if
- * we can really use equality tests here... JDK does have decent
- * conversions though, to retain accuracy and round-trippability.
- * But still...
- */
- public void testFloatWrapper() throws Exception
- {
- // Also: should be able to coerce floats, strings:
- String[] STRS = new String[] {
- "1.0", "0.0", "-0.3", "0.7", "42.012", "-999.0", NAN_STRING
- };
-
- for (String str : STRS) {
- Float exp = Float.valueOf(str);
- Float result;
-
- if (NAN_STRING != str) {
- // First, as regular floating point value
- result = MAPPER.readValue(new StringReader(str), Float.class);
- assertEquals(exp, result);
- }
-
- // and then as coerced String:
- result = MAPPER.readValue(new StringReader(" \""+str+"\""), Float.class);
- assertEquals(exp, result);
- }
- }
-
- public void testDoubleWrapper() throws Exception
- {
- // Also: should be able to coerce doubles, strings:
- String[] STRS = new String[] {
- "1.0", "0.0", "-0.3", "0.7", "42.012", "-999.0", NAN_STRING
- };
-
- for (String str : STRS) {
- Double exp = Double.valueOf(str);
- Double result;
-
- // First, as regular double value
- if (NAN_STRING != str) {
- result = MAPPER.readValue(str, Double.class);
- assertEquals(exp, result);
- }
- // and then as coerced String:
- result = MAPPER.readValue(new StringReader(" \""+str+"\""), Double.class);
- assertEquals(exp, result);
- }
- }
-
- public void testDoubleAsArray() throws Exception
- {
- final ObjectMapper mapper = new ObjectMapper();
- mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
- final double value = 0.016;
- try {
- mapper.readValue(new StringReader("{\"v\":[" + value + "]}"), DoubleBean.class);
- fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled");
- } catch (JsonMappingException exp) {
- //Correctly threw exception
- }
-
- mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
-
- DoubleBean result = mapper.readValue(new StringReader("{\"v\":[" + value + "]}"),
- DoubleBean.class);
- assertEquals(value, result._v);
-
- result = mapper.readValue(new StringReader("[{\"v\":[" + value + "]}]"), DoubleBean.class);
- assertEquals(value, result._v);
-
- try {
- mapper.readValue(new StringReader("[{\"v\":[" + value + "," + value + "]}]"), DoubleBean.class);
- fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled");
- } catch (JsonMappingException exp) {
- //threw exception as required
- }
-
- result = mapper.readValue(new StringReader("{\"v\":[null]}"), DoubleBean.class);
- assertNotNull(result);
- assertEquals(0d, result._v);
-
- double[] array = mapper.readValue(new StringReader("[ [ null ] ]"), double[].class);
- assertNotNull(array);
- assertEquals(1, array.length);
- assertEquals(0d, array[0]);
- }
-
- public void testDoublePrimitiveNonNumeric() throws Exception
- {
- // first, simple case:
- // bit tricky with binary fps but...
- double value = Double.POSITIVE_INFINITY;
- DoubleBean result = MAPPER.readValue(new StringReader("{\"v\":\""+value+"\"}"), DoubleBean.class);
- assertEquals(value, result._v);
-
- // should work with arrays too..
- double[] array = MAPPER.readValue(new StringReader("[ \"Infinity\" ]"), double[].class);
- assertNotNull(array);
- assertEquals(1, array.length);
- assertEquals(Double.POSITIVE_INFINITY, array[0]);
- }
-
- public void testFloatPrimitiveNonNumeric() throws Exception
- {
- // bit tricky with binary fps but...
- float value = Float.POSITIVE_INFINITY;
- FloatBean result = MAPPER.readValue(new StringReader("{\"v\":\""+value+"\"}"), FloatBean.class);
- assertEquals(value, result._v);
-
- // should work with arrays too..
- float[] array = MAPPER.readValue(new StringReader("[ \"Infinity\" ]"), float[].class);
- assertNotNull(array);
- assertEquals(1, array.length);
- assertEquals(Float.POSITIVE_INFINITY, array[0]);
- }
-
- /*
- /**********************************************************
- /* Scalar tests, other
- /**********************************************************
- */
-
- public void testEmptyToNullCoercionForPrimitives() throws Exception {
- _testEmptyToNullCoercion(int.class, Integer.valueOf(0));
- _testEmptyToNullCoercion(long.class, Long.valueOf(0));
- _testEmptyToNullCoercion(double.class, Double.valueOf(0.0));
- _testEmptyToNullCoercion(float.class, Float.valueOf(0.0f));
- }
-
- private void _testEmptyToNullCoercion(Class<?> primType, Object emptyValue) throws Exception
- {
- final String EMPTY = "\"\"";
-
- // as per [databind#1095] should only allow coercion from empty String,
- // if `null` is acceptable
- ObjectReader intR = MAPPER.readerFor(primType);
- assertEquals(emptyValue, intR.readValue(EMPTY));
- try {
- intR.with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
- .readValue("\"\"");
- fail("Should not have passed");
- } catch (JsonMappingException e) {
- verifyException(e, "Can not map Empty String");
- }
- }
-
- public void testBase64Variants() throws Exception
- {
- final byte[] INPUT = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890X".getBytes("UTF-8");
-
- // default encoding is "MIME, no linefeeds", so:
- Assert.assertArrayEquals(INPUT, MAPPER.readValue(
- quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwWA=="),
- byte[].class));
- ObjectReader reader = MAPPER.readerFor(byte[].class);
- Assert.assertArrayEquals(INPUT, (byte[]) reader.with(Base64Variants.MIME_NO_LINEFEEDS).readValue(
- quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwWA=="
- )));
-
- // but others should be slightly different
- Assert.assertArrayEquals(INPUT, (byte[]) reader.with(Base64Variants.MIME).readValue(
- quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamtsbW5vcHFyc3R1\\ndnd4eXoxMjM0NTY3ODkwWA=="
- )));
- Assert.assertArrayEquals(INPUT, (byte[]) reader.with(Base64Variants.MODIFIED_FOR_URL).readValue(
- quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwWA"
- )));
- // PEM mandates 64 char lines:
- Assert.assertArrayEquals(INPUT, (byte[]) reader.with(Base64Variants.PEM).readValue(
- quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamts\\nbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwWA=="
- )));
- }
- /*
- /**********************************************************
- /* Simple non-primitive types
- /**********************************************************
- */
-
- public void testSingleString() throws Exception
- {
- String value = "FOO!";
- String result = MAPPER.readValue(new StringReader("\""+value+"\""), String.class);
- assertEquals(value, result);
- }
-
- public void testSingleStringWrapped() throws Exception
- {
- final ObjectMapper mapper = new ObjectMapper();
- mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
-
- String value = "FOO!";
- try {
- mapper.readValue(new StringReader("[\""+value+"\"]"), String.class);
- fail("Exception not thrown when attempting to unwrap a single value 'String' array into a simple String");
- } catch (JsonMappingException exp) {
- //exception thrown correctly
- }
-
- mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
-
- try {
- mapper.readValue(new StringReader("[\""+value+"\",\""+value+"\"]"), String.class);
- fail("Exception not thrown when attempting to unwrap a single value 'String' array that contained more than one value into a simple String");
- } catch (JsonMappingException exp) {
- //exception thrown correctly
- }
-
- String result = mapper.readValue(new StringReader("[\""+value+"\"]"), String.class);
- assertEquals(value, result);
- }
-
- public void testBigDecimal() throws Exception
- {
- final ObjectMapper mapper = objectMapper();
- mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
-
- BigDecimal value = new BigDecimal("0.001");
- BigDecimal result = mapper.readValue(value.toString(), BigDecimal.class);
- assertEquals(value, result);
- try {
- mapper.readValue("[" + value.toString() + "]", BigDecimal.class);
- fail("Exception was not thrown when attempting to read a single value array of BigDecimal when UNWRAP_SINGLE_VALUE_ARRAYS feature is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
- result = mapper.readValue("[" + value.toString() + "]", BigDecimal.class);
- assertEquals(value, result);
-
- try {
- mapper.readValue("[" + value.toString() + "," + value.toString() + "]", BigDecimal.class);
- fail("Exception was not thrown when attempting to read a muti value array of BigDecimal when UNWRAP_SINGLE_VALUE_ARRAYS feature is enabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- }
-
- public void testBigInteger() throws Exception
- {
- final ObjectMapper mapper = objectMapper();
- mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
-
- BigInteger value = new BigInteger("-1234567890123456789012345567809");
- BigInteger result = mapper.readValue(new StringReader(value.toString()), BigInteger.class);
- assertEquals(value, result);
-
- //Issue#381
- try {
- mapper.readValue("[" + value.toString() + "]", BigInteger.class);
- fail("Exception was not thrown when attempting to read a single value array of BigInteger when UNWRAP_SINGLE_VALUE_ARRAYS feature is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
- result = mapper.readValue("[" + value.toString() + "]", BigInteger.class);
- assertEquals(value, result);
-
- try {
- mapper.readValue("[" + value.toString() + "," + value.toString() + "]", BigInteger.class);
- fail("Exception was not thrown when attempting to read a muti value array of BigInteger when UNWRAP_SINGLE_VALUE_ARRAYS feature is enabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- }
-
- /*
- /**********************************************************
- /* Sequence tests
- /**********************************************************
- */
-
- /**
- * Then a unit test to verify that we can conveniently bind sequence of
- * space-separate simple values
- */
- public void testSequenceOfInts() throws Exception
- {
- final int NR_OF_INTS = 100;
-
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < NR_OF_INTS; ++i) {
- sb.append(" ");
- sb.append(i);
- }
- JsonParser jp = MAPPER.getFactory().createParser(sb.toString());
- for (int i = 0; i < NR_OF_INTS; ++i) {
- Integer result = MAPPER.readValue(jp, Integer.class);
- assertEquals(Integer.valueOf(i), result);
- }
- jp.close();
- }
-
- /*
- /**********************************************************
- /* Single-element as array tests
- /**********************************************************
- */
-
- // [databind#381]
- public void testSingleElementScalarArrays() throws Exception {
- final int intTest = 932832;
- final double doubleTest = 32.3234;
- final long longTest = 2374237428374293423L;
- final short shortTest = (short) intTest;
- final float floatTest = 84.3743f;
- final byte byteTest = (byte) 43;
- final char charTest = 'c';
-
- final ObjectMapper mapper = new ObjectMapper();
- mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
-
- final int intValue = mapper.readValue(asArray(intTest), Integer.TYPE);
- assertEquals(intTest, intValue);
- final Integer integerWrapperValue = mapper.readValue(asArray(Integer.valueOf(intTest)), Integer.class);
- assertEquals(Integer.valueOf(intTest), integerWrapperValue);
-
- final double doubleValue = mapper.readValue(asArray(doubleTest), Double.class);
- assertEquals(doubleTest, doubleValue);
- final Double doubleWrapperValue = mapper.readValue(asArray(Double.valueOf(doubleTest)), Double.class);
- assertEquals(Double.valueOf(doubleTest), doubleWrapperValue);
-
- final long longValue = mapper.readValue(asArray(longTest), Long.TYPE);
- assertEquals(longTest, longValue);
- final Long longWrapperValue = mapper.readValue(asArray(Long.valueOf(longTest)), Long.class);
- assertEquals(Long.valueOf(longTest), longWrapperValue);
-
- final short shortValue = mapper.readValue(asArray(shortTest), Short.TYPE);
- assertEquals(shortTest, shortValue);
- final Short shortWrapperValue = mapper.readValue(asArray(Short.valueOf(shortTest)), Short.class);
- assertEquals(Short.valueOf(shortTest), shortWrapperValue);
-
- final float floatValue = mapper.readValue(asArray(floatTest), Float.TYPE);
- assertEquals(floatTest, floatValue);
- final Float floatWrapperValue = mapper.readValue(asArray(Float.valueOf(floatTest)), Float.class);
- assertEquals(Float.valueOf(floatTest), floatWrapperValue);
-
- final byte byteValue = mapper.readValue(asArray(byteTest), Byte.TYPE);
- assertEquals(byteTest, byteValue);
- final Byte byteWrapperValue = mapper.readValue(asArray(Byte.valueOf(byteTest)), Byte.class);
- assertEquals(Byte.valueOf(byteTest), byteWrapperValue);
-
- final char charValue = mapper.readValue(asArray(quote(String.valueOf(charTest))), Character.TYPE);
- assertEquals(charTest, charValue);
- final Character charWrapperValue = mapper.readValue(asArray(quote(String.valueOf(charTest))), Character.class);
- assertEquals(Character.valueOf(charTest), charWrapperValue);
-
- final boolean booleanTrueValue = mapper.readValue(asArray(true), Boolean.TYPE);
- assertTrue(booleanTrueValue);
-
- final boolean booleanFalseValue = mapper.readValue(asArray(false), Boolean.TYPE);
- assertFalse(booleanFalseValue);
-
- final Boolean booleanWrapperTrueValue = mapper.readValue(asArray(Boolean.valueOf(true)), Boolean.class);
- assertEquals(Boolean.TRUE, booleanWrapperTrueValue);
- }
-
- public void testSingleElementArrayDisabled() throws Exception {
- final ObjectMapper mapper = new ObjectMapper();
- mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
- try {
- mapper.readValue("[42]", Integer.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[42]", Integer.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[42.273]", Double.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[42.2723]", Double.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[42342342342342]", Long.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[42342342342342342]", Long.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[42]", Short.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[42]", Short.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[327.2323]", Float.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[82.81902]", Float.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[22]", Byte.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[22]", Byte.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("['d']", Character.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("['d']", Character.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[true]", Boolean.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[true]", Boolean.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- }
-
- public void testMultiValueArrayException() throws IOException {
- final ObjectMapper mapper = new ObjectMapper();
- mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
-
- try {
- mapper.readValue("[42,42]", Integer.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[42,42]", Integer.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[42.273,42.273]", Double.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[42.2723,42.273]", Double.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[42342342342342,42342342342342]", Long.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[42342342342342342,42342342342342]", Long.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[42,42]", Short.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[42,42]", Short.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[327.2323,327.2323]", Float.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[82.81902,327.2323]", Float.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[22,23]", Byte.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[22,23]", Byte.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue(asArray(quote("c") + "," + quote("d")), Character.class);
-
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue(asArray(quote("c") + "," + quote("d")), Character.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
-
- try {
- mapper.readValue("[true,false]", Boolean.class);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- try {
- mapper.readValue("[true,false]", Boolean.TYPE);
- fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException exp) {
- //Exception was thrown correctly
- }
- }
-
- private static String asArray(Object value) {
- final String stringVal = value.toString();
- return new StringBuilder(stringVal.length() + 2).append("[").append(stringVal).append("]").toString();
- }
-
- /*
- /**********************************************************
- /* Empty String coercion, handling
- /**********************************************************
- */
-
- // by default, should return nulls, n'est pas?
- public void testEmptyStringForWrappers() throws IOException
- {
- WrappersBean bean;
-
- // by default, ok to rely on defaults
- bean = MAPPER.readValue("{\"booleanValue\":\"\"}", WrappersBean.class);
- assertNull(bean.booleanValue);
- bean = MAPPER.readValue("{\"byteValue\":\"\"}", WrappersBean.class);
- assertNull(bean.byteValue);
-
- // char/Character is different... not sure if this should work or not:
- bean = MAPPER.readValue("{\"charValue\":\"\"}", WrappersBean.class);
- assertNull(bean.charValue);
-
- bean = MAPPER.readValue("{\"shortValue\":\"\"}", WrappersBean.class);
- assertNull(bean.shortValue);
- bean = MAPPER.readValue("{\"intValue\":\"\"}", WrappersBean.class);
- assertNull(bean.intValue);
- bean = MAPPER.readValue("{\"longValue\":\"\"}", WrappersBean.class);
- assertNull(bean.longValue);
- bean = MAPPER.readValue("{\"floatValue\":\"\"}", WrappersBean.class);
- assertNull(bean.floatValue);
- bean = MAPPER.readValue("{\"doubleValue\":\"\"}", WrappersBean.class);
- assertNull(bean.doubleValue);
- }
-
- public void testEmptyStringForPrimitives() throws IOException
- {
- PrimitivesBean bean;
- bean = MAPPER.readValue("{\"booleanValue\":\"\"}", PrimitivesBean.class);
- assertFalse(bean.booleanValue);
- bean = MAPPER.readValue("{\"byteValue\":\"\"}", PrimitivesBean.class);
- assertEquals((byte) 0, bean.byteValue);
- bean = MAPPER.readValue("{\"charValue\":\"\"}", PrimitivesBean.class);
- assertEquals((char) 0, bean.charValue);
- bean = MAPPER.readValue("{\"shortValue\":\"\"}", PrimitivesBean.class);
- assertEquals((short) 0, bean.shortValue);
- bean = MAPPER.readValue("{\"intValue\":\"\"}", PrimitivesBean.class);
- assertEquals(0, bean.intValue);
- bean = MAPPER.readValue("{\"longValue\":\"\"}", PrimitivesBean.class);
- assertEquals(0L, bean.longValue);
- bean = MAPPER.readValue("{\"floatValue\":\"\"}", PrimitivesBean.class);
- assertEquals(0.0f, bean.floatValue);
- bean = MAPPER.readValue("{\"doubleValue\":\"\"}", PrimitivesBean.class);
- assertEquals(0.0, bean.doubleValue);
- }
-}
-
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/KeyDeser1429Test.java b/src/test/java/com/fasterxml/jackson/databind/deser/KeyDeser1429Test.java
deleted file mode 100644
index 7922928..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/deser/KeyDeser1429Test.java
+++ /dev/null
@@ -1,48 +0,0 @@
-package com.fasterxml.jackson.databind.deser;
-
-import java.util.Map;
-
-import com.fasterxml.jackson.annotation.JsonCreator;
-import com.fasterxml.jackson.annotation.JsonValue;
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.*;
-
-public class KeyDeser1429Test extends BaseMapTest
-{
- static class FullName {
- private String _firstname, _lastname;
-
- private FullName(String firstname, String lastname) {
- _firstname = firstname;
- _lastname = lastname;
- }
-
- @JsonCreator
- public static FullName valueOf(String value) {
- String[] mySplit = value.split("\\.");
- return new FullName(mySplit[0], mySplit[1]);
- }
-
- public static FullName valueOf(String firstname, String lastname) {
- return new FullName(firstname, lastname);
- }
-
- @JsonValue
- @Override
- public String toString() {
- return _firstname + "." + _lastname;
- }
- }
-
- public void testDeserializeKeyViaFactory() throws Exception
- {
- Map<FullName, Double> map =
- new ObjectMapper().readValue("{\"first.last\": 42}",
- new TypeReference<Map<FullName, Double>>() { });
- Map.Entry<FullName, Double> entry = map.entrySet().iterator().next();
- FullName key = entry.getKey();
- assertEquals(key._firstname, "first");
- assertEquals(key._lastname, "last");
- assertEquals(entry.getValue().doubleValue(), 42, 0);
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/NullHandlingTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/NullHandlingTest.java
index 21eeb3c..836e786 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/NullHandlingTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/NullHandlingTest.java
@@ -12,7 +12,6 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
-import com.fasterxml.jackson.databind.deser.JDKScalarsTest.PrimitivesBean;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class NullHandlingTest extends BaseMapTest
@@ -138,82 +137,6 @@
assertEquals("funny", str);
}
- public void testNullForPrimitives() throws IOException
- {
- // by default, ok to rely on defaults
- PrimitivesBean bean = MAPPER.readValue(
- "{\"intValue\":null, \"booleanValue\":null, \"doubleValue\":null}",
- PrimitivesBean.class);
- assertNotNull(bean);
- assertEquals(0, bean.intValue);
- assertEquals(false, bean.booleanValue);
- assertEquals(0.0, bean.doubleValue);
-
- bean = MAPPER.readValue("{\"byteValue\":null, \"longValue\":null, \"floatValue\":null}",
- PrimitivesBean.class);
- assertNotNull(bean);
- assertEquals((byte) 0, bean.byteValue);
- assertEquals(0L, bean.longValue);
- assertEquals(0.0f, bean.floatValue);
-
- // but not when enabled
- final ObjectReader reader = MAPPER
- .readerFor(PrimitivesBean.class)
- .with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
- // boolean
- try {
- reader.readValue("{\"booleanValue\":null}");
- fail("Expected failure for boolean + null");
- } catch (JsonMappingException e) {
- verifyException(e, "Can not map JSON null into type boolean");
- }
- // byte/char/short/int/long
- try {
- reader.readValue("{\"byteValue\":null}");
- fail("Expected failure for byte + null");
- } catch (JsonMappingException e) {
- verifyException(e, "Can not map JSON null into type byte");
- }
- try {
- reader.readValue("{\"charValue\":null}");
- fail("Expected failure for char + null");
- } catch (JsonMappingException e) {
- verifyException(e, "Can not map JSON null into type char");
- }
- try {
- reader.readValue("{\"shortValue\":null}");
- fail("Expected failure for short + null");
- } catch (JsonMappingException e) {
- verifyException(e, "Can not map JSON null into type short");
- }
- try {
- reader.readValue("{\"intValue\":null}");
- fail("Expected failure for int + null");
- } catch (JsonMappingException e) {
- verifyException(e, "Can not map JSON null into type int");
- }
- try {
- reader.readValue("{\"longValue\":null}");
- fail("Expected failure for long + null");
- } catch (JsonMappingException e) {
- verifyException(e, "Can not map JSON null into type long");
- }
-
- // float/double
- try {
- reader.readValue("{\"floatValue\":null}");
- fail("Expected failure for float + null");
- } catch (JsonMappingException e) {
- verifyException(e, "Can not map JSON null into type float");
- }
- try {
- reader.readValue("{\"doubleValue\":null}");
- fail("Expected failure for double + null");
- } catch (JsonMappingException e) {
- verifyException(e, "Can not map JSON null into type double");
- }
- }
-
// [databind#407]
public void testListOfNulls() throws Exception
{
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/PropertyAliasTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/PropertyAliasTest.java
new file mode 100644
index 0000000..f6df4f8
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/PropertyAliasTest.java
@@ -0,0 +1,59 @@
+package com.fasterxml.jackson.databind.deser;
+
+import com.fasterxml.jackson.annotation.JsonAlias;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class PropertyAliasTest extends BaseMapTest
+{
+ static class AliasBean {
+ @JsonAlias({ "nm", "Name" })
+ public String name;
+
+ int _xyz;
+
+ int _a;
+
+ @JsonCreator
+ public AliasBean(@JsonProperty("a")
+ @JsonAlias("A") int a) {
+ _a = a;
+ }
+
+ @JsonAlias({ "Xyz" })
+ public void setXyz(int x) {
+ _xyz = x;
+ }
+ }
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ // [databind#1029]
+ public void testSimpleAliases() throws Exception
+ {
+ AliasBean bean;
+
+ // first, one indicated by field annotation, set via field
+ bean = MAPPER.readValue(aposToQuotes("{'Name':'Foobar','a':3,'xyz':37}"),
+ AliasBean.class);
+ assertEquals("Foobar", bean.name);
+ assertEquals(3, bean._a);
+ assertEquals(37, bean._xyz);
+
+ // then method-bound one
+ bean = MAPPER.readValue(aposToQuotes("{'name':'Foobar','a':3,'Xyz':37}"),
+ AliasBean.class);
+ assertEquals("Foobar", bean.name);
+ assertEquals(3, bean._a);
+ assertEquals(37, bean._xyz);
+
+ // and finally, constructor-backed one
+ bean = MAPPER.readValue(aposToQuotes("{'name':'Foobar','A':3,'xyz':37}"),
+ AliasBean.class);
+ assertEquals("Foobar", bean.name);
+ assertEquals(3, bean._a);
+ assertEquals(37, bean._xyz);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestAbstract.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestAbstract.java
deleted file mode 100644
index 28830fe..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestAbstract.java
+++ /dev/null
@@ -1,38 +0,0 @@
-package com.fasterxml.jackson.databind.deser;
-
-
-import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.*;
-
-/**
- * Tests for checking handling of abstract types.
- */
-public class TestAbstract
- extends BaseMapTest
-{
- static abstract class Abstract {
- public int x;
- }
-
- /*
- /**********************************************************
- /* Unit tests
- /**********************************************************
- */
-
- /**
- * Test to verify details of how trying to deserialize into
- * abstract type should fail (if there is no way to determine
- * actual type information for the concrete type to use)
- */
- public void testAbstractFailure() throws Exception
- {
- ObjectMapper m = new ObjectMapper();
- try {
- m.readValue("{ \"x\" : 3 }", Abstract.class);
- fail("Should fail on trying to deserialize abstract type");
- } catch (JsonProcessingException e) {
- verifyException(e, "can not construct");
- }
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestBasicAnnotations.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestBasicAnnotations.java
index 1240bd3..4796e40 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestBasicAnnotations.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/TestBasicAnnotations.java
@@ -3,7 +3,7 @@
import java.io.*;
import com.fasterxml.jackson.annotation.*;
-
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@@ -18,12 +18,6 @@
public class TestBasicAnnotations
extends BaseMapTest
{
- /*
- /**********************************************************
- /* Annotated helper classes
- /**********************************************************
- */
-
/// Class for testing {@link JsonProperty} annotations
final static class SizeClassSetter
{
@@ -88,6 +82,24 @@
@JsonDeserialize protected int a;
}
+ @JsonAutoDetect(setterVisibility=Visibility.NONE)
+ final static class Dummy { }
+
+ final static class EmptyDummy { }
+
+ static class AnnoBean {
+ int value = 3;
+
+ @JsonProperty("y")
+ public void setX(int v) { value = v; }
+ }
+
+ enum Alpha { A, B, C; }
+
+ public static class SimpleBean {
+ public int x, y;
+ }
+
/*
/**********************************************************
/* Other helper classes
@@ -104,10 +116,10 @@
return new int[] { jp.getIntValue() };
}
}
-
+
/*
/**********************************************************
- /* Test methods
+ /* Test methods, basic
/**********************************************************
*/
@@ -168,4 +180,45 @@
Issue442Bean bean = MAPPER.readValue("{\"i\":5}", Issue442Bean.class);
assertEquals(5, bean.w.i);
}
+
+ /*
+ /**********************************************************
+ /* Test methods, annotations disabled
+ /**********************************************************
+ */
+
+ public void testAnnotationsDisabled() throws Exception
+ {
+ // first: verify that annotation introspection is enabled by default
+ assertTrue(MAPPER.getDeserializationConfig().isEnabled(MapperFeature.USE_ANNOTATIONS));
+ // with annotations, property is renamed
+ AnnoBean bean = MAPPER.readValue("{ \"y\" : 0 }", AnnoBean.class);
+ assertEquals(0, bean.value);
+
+ ObjectMapper m = new ObjectMapper();
+ m.configure(MapperFeature.USE_ANNOTATIONS, false);
+ // without annotations, should default to default bean-based name...
+ bean = m.readValue("{ \"x\" : 0 }", AnnoBean.class);
+ assertEquals(0, bean.value);
+ }
+
+ public void testEnumsWhenDisabled() throws Exception
+ {
+ ObjectMapper m = new ObjectMapper();
+ assertEquals(Alpha.B, m.readValue(quote("B"), Alpha.class));
+
+ m = new ObjectMapper();
+ m.configure(MapperFeature.USE_ANNOTATIONS, false);
+ // should still use the basic name handling here
+ assertEquals(Alpha.B, m.readValue(quote("B"), Alpha.class));
+ }
+
+ public void testNoAccessOverrides() throws Exception
+ {
+ ObjectMapper m = new ObjectMapper();
+ m.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS);
+ SimpleBean bean = m.readValue("{\"x\":1,\"y\":2}", SimpleBean.class);
+ assertEquals(1, bean.x);
+ assertEquals(2, bean.y);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestBeanDeserializer.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestBeanDeserializer.java
index cd1a506..4e6da17 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestBeanDeserializer.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/TestBeanDeserializer.java
@@ -18,11 +18,9 @@
@SuppressWarnings("serial")
public class TestBeanDeserializer extends BaseMapTest
{
- /*
- /**********************************************************
- /* Helper types
- /**********************************************************
- */
+ static abstract class Abstract {
+ public int x;
+ }
static class Bean {
public String b = "b";
@@ -242,6 +240,20 @@
private final ObjectMapper MAPPER = new ObjectMapper();
+ /**
+ * Test to verify details of how trying to deserialize into
+ * abstract type should fail (if there is no way to determine
+ * actual type information for the concrete type to use)
+ */
+ public void testAbstractFailure() throws Exception
+ {
+ try {
+ MAPPER.readValue("{ \"x\" : 3 }", Abstract.class);
+ fail("Should fail on trying to deserialize abstract type");
+ } catch (JsonProcessingException e) {
+ verifyException(e, "can not construct");
+ }
+ }
public void testPropertyRemoval() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestConfig.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestConfig.java
deleted file mode 100644
index 6b2dbf4..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestConfig.java
+++ /dev/null
@@ -1,120 +0,0 @@
-package com.fasterxml.jackson.databind.deser;
-
-import com.fasterxml.jackson.annotation.*;
-import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
-
-import com.fasterxml.jackson.databind.*;
-import com.fasterxml.jackson.databind.introspect.ClassIntrospector;
-
-/**
- * Unit tests for checking handling of DeserializationConfig.
- */
-public class TestConfig
- extends BaseMapTest
-{
- @JsonAutoDetect(setterVisibility=Visibility.NONE)
- final static class Dummy { }
-
- final static class EmptyDummy { }
-
- static class AnnoBean {
- int value = 3;
-
- @JsonProperty("y")
- public void setX(int v) { value = v; }
- }
-
- enum Alpha { A, B, C; }
-
- public static class SimpleBean {
- public int x, y;
- }
-
- /*
- /**********************************************************
- /* Main tests
- /**********************************************************
- */
-
- /* Test to verify that we don't overflow number of features; if we
- * hit the limit, need to change implementation -- this test just
- * gives low-water mark
- */
- public void testEnumIndexes()
- {
- int max = 0;
-
- for (DeserializationFeature f : DeserializationFeature.values()) {
- max = Math.max(max, f.ordinal());
- }
- if (max >= 31) { // 31 is actually ok; 32 not
- fail("Max number of DeserializationFeature enums reached: "+max);
- }
- }
-
- public void testDefaults()
- {
- ObjectMapper m = new ObjectMapper();
- DeserializationConfig cfg = m.getDeserializationConfig();
-
- // Expected defaults:
- assertTrue(cfg.isEnabled(MapperFeature.USE_ANNOTATIONS));
- assertTrue(cfg.isEnabled(MapperFeature.AUTO_DETECT_SETTERS));
- assertTrue(cfg.isEnabled(MapperFeature.AUTO_DETECT_CREATORS));
- assertTrue(cfg.isEnabled(MapperFeature.USE_GETTERS_AS_SETTERS));
- assertTrue(cfg.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS));
-
-
- assertFalse(cfg.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS));
- assertFalse(cfg.isEnabled(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS));
-
- assertTrue(cfg.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
- }
-
- public void testOverrideIntrospectors()
- {
- ObjectMapper m = new ObjectMapper();
- DeserializationConfig cfg = m.getDeserializationConfig();
- // and finally, ensure we could override introspectors
- cfg = cfg.with((ClassIntrospector) null); // no way to verify tho
- cfg = cfg.with((AnnotationIntrospector) null);
- assertNull(cfg.getAnnotationIntrospector());
- }
-
- public void testAnnotationsDisabled() throws Exception
- {
- // first: verify that annotation introspection is enabled by default
- ObjectMapper m = new ObjectMapper();
- assertTrue(m.getDeserializationConfig().isEnabled(MapperFeature.USE_ANNOTATIONS));
- // with annotations, property is renamed
- AnnoBean bean = m.readValue("{ \"y\" : 0 }", AnnoBean.class);
- assertEquals(0, bean.value);
-
- m = new ObjectMapper();
- m.configure(MapperFeature.USE_ANNOTATIONS, false);
- // without annotations, should default to default bean-based name...
- bean = m.readValue("{ \"x\" : 0 }", AnnoBean.class);
- assertEquals(0, bean.value);
- }
-
- // [JACKSON-875]
- public void testEnumsWhenDisabled() throws Exception
- {
- ObjectMapper m = new ObjectMapper();
- assertEquals(Alpha.B, m.readValue(quote("B"), Alpha.class));
-
- m = new ObjectMapper();
- m.configure(MapperFeature.USE_ANNOTATIONS, false);
- // should still use the basic name handling here
- assertEquals(Alpha.B, m.readValue(quote("B"), Alpha.class));
- }
-
- public void testNoAccessOverrides() throws Exception
- {
- ObjectMapper m = new ObjectMapper();
- m.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS);
- SimpleBean bean = m.readValue("{\"x\":1,\"y\":2}", SimpleBean.class);
- assertEquals(1, bean.x);
- assertEquals(2, bean.y);
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestCustomDeserializers.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestCustomDeserializers.java
index 58c5251..8a1fee4 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestCustomDeserializers.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/TestCustomDeserializers.java
@@ -6,7 +6,9 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
+
import com.fasterxml.jackson.core.*;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.*;
import com.fasterxml.jackson.databind.deser.std.*;
@@ -37,11 +39,11 @@
}
@Override
- public T deserialize(JsonParser jp, DeserializationContext ctxt)
- throws IOException, JsonProcessingException
+ public T deserialize(JsonParser p, DeserializationContext ctxt)
+ throws IOException
{
// need to skip, if structured...
- jp.skipChildren();
+ p.skipChildren();
return value;
}
}
@@ -65,29 +67,29 @@
static class CustomBeanDeserializer extends JsonDeserializer<CustomBean>
{
@Override
- public CustomBean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException
+ public CustomBean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
int a = 0, b = 0;
- JsonToken t = jp.getCurrentToken();
+ JsonToken t = p.getCurrentToken();
if (t == JsonToken.START_OBJECT) {
- t = jp.nextToken();
+ t = p.nextToken();
} else if (t != JsonToken.FIELD_NAME) {
throw new Error();
}
while(t == JsonToken.FIELD_NAME) {
- final String fieldName = jp.getCurrentName();
- t = jp.nextToken();
+ final String fieldName = p.getCurrentName();
+ t = p.nextToken();
if (t != JsonToken.VALUE_NUMBER_INT) {
- throw new JsonParseException(jp, "expecting number got "+ t);
+ throw new JsonParseException(p, "expecting number got "+ t);
}
if (fieldName.equals("a")) {
- a = jp.getIntValue();
+ a = p.getIntValue();
} else if (fieldName.equals("b")) {
- b = jp.getIntValue();
+ b = p.getIntValue();
} else {
throw new Error();
}
- t = jp.nextToken();
+ t = p.nextToken();
}
return new CustomBean(a, b);
}
@@ -102,7 +104,6 @@
}
}
- // [JACKSON-882]
public static class CustomKey {
private final int id;
@@ -130,8 +131,8 @@
static class CustomKeySerializer extends JsonSerializer<CustomKey> {
@Override
- public void serialize(CustomKey value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
- jgen.writeFieldName(String.valueOf(value.getId()));
+ public void serialize(CustomKey value, JsonGenerator g, SerializerProvider provider) throws IOException {
+ g.writeFieldName(String.valueOf(value.getId()));
}
}
@@ -142,7 +143,7 @@
}
}
- // [#375]
+ // [databind#375]
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@@ -201,9 +202,9 @@
}
@Override
- public Bean375Inner deserialize(JsonParser jp, DeserializationContext ctxt)
+ public Bean375Inner deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
- int x = jp.getIntValue();
+ int x = p.getIntValue();
if (negative) {
x = -x;
} else {
@@ -257,7 +258,55 @@
return p.getText().toUpperCase();
}
}
-
+
+ static class DelegatingModuleImpl extends SimpleModule
+ {
+ public DelegatingModuleImpl() {
+ super("test", Version.unknownVersion());
+ }
+
+ @Override
+ public void setupModule(SetupContext context)
+ {
+ super.setupModule(context);
+ context.addBeanDeserializerModifier(new BeanDeserializerModifier() {
+ @Override
+ public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
+ BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
+ if (deserializer.handledType() == String.class) {
+ JsonDeserializer<?> d = new MyStringDeserializer(deserializer);
+ // just for test coverage purposes...
+ if (d.getDelegatee() != deserializer) {
+ throw new Error("Can not access delegatee!");
+ }
+ return d;
+ }
+ return deserializer;
+ }
+ });
+ }
+ }
+
+ static class MyStringDeserializer extends DelegatingDeserializer
+ {
+ public MyStringDeserializer(JsonDeserializer<?> newDel) {
+ super(newDel);
+ }
+
+ @Override
+ protected JsonDeserializer<?> newDelegatingInstance(JsonDeserializer<?> newDel) {
+ return new MyStringDeserializer(newDel);
+ }
+
+ @Override
+ public Object deserialize(JsonParser p, DeserializationContext ctxt)
+ throws IOException
+ {
+ Object ob = _delegatee.deserialize(p, ctxt);
+ return "MY:"+ob;
+ }
+ }
+
/*
/**********************************************************
/* Unit tests
@@ -399,4 +448,12 @@
assertNotNull(sw);
assertEquals("FOO", sw.str);
}
+
+ public void testDelegatingDeserializer() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper().registerModule(
+ new DelegatingModuleImpl());
+ String str = mapper.readValue(quote("foo"), String.class);
+ assertEquals("MY:foo", str);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestGenericCollectionDeser.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestGenericCollectionDeser.java
index f99bbb2..a87670e 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestGenericCollectionDeser.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/TestGenericCollectionDeser.java
@@ -2,6 +2,8 @@
import java.util.*;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@@ -9,12 +11,6 @@
public class TestGenericCollectionDeser
extends BaseMapTest
{
- /*
- /**********************************************************
- /* Test classes, enums
- /**********************************************************
- */
-
static class ListSubClass extends ArrayList<StringWrapper> { }
/**
@@ -24,15 +20,18 @@
@JsonDeserialize(contentAs=StringWrapper.class)
static class AnnotatedStringList extends ArrayList<Object> { }
- @JsonDeserialize(contentAs=BooleanWrapper.class)
+ @JsonDeserialize(contentAs=BooleanElement.class)
static class AnnotatedBooleanList extends ArrayList<Object> { }
- /*
- /**********************************************************
- /* Test methods
- /**********************************************************
- */
+ protected static class BooleanElement {
+ public Boolean b;
+ @JsonCreator
+ public BooleanElement(Boolean value) { b = value; }
+
+ @JsonValue public Boolean value() { return b; }
+ }
+
/*
/**********************************************************
/* Tests for sub-classing
@@ -76,7 +75,7 @@
AnnotatedBooleanList result = mapper.readValue("[ false ]", AnnotatedBooleanList.class);
assertEquals(1, result.size());
Object ob = result.get(0);
- assertEquals(BooleanWrapper.class, ob.getClass());
- assertFalse(((BooleanWrapper) ob).b);
+ assertEquals(BooleanElement.class, ob.getClass());
+ assertFalse(((BooleanElement) ob).b);
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestInnerClass.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestInnerClass.java
index 4be0a63..556e996 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestInnerClass.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/TestInnerClass.java
@@ -1,10 +1,10 @@
package com.fasterxml.jackson.databind.deser;
+import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.*;
public class TestInnerClass extends BaseMapTest
{
- // [JACKSON-594]
static class Dog
{
public String name;
@@ -16,9 +16,10 @@
brain = new Brain();
brain.isThinking = thinking;
}
-
+
// note: non-static
public class Brain {
+ @JsonProperty("brainiac")
public boolean isThinking;
public String parentName() { return name; }
@@ -45,5 +46,11 @@
assertEquals("Smurf", output.brain.parentName());
output.name = "Foo";
assertEquals("Foo", output.brain.parentName());
+
+ // also, null handling
+ input.brain = null;
+
+ output = mapper.readValue(mapper.writeValueAsString(input), Dog.class);
+ assertNull(output.brain);
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderAdvancedTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderAdvancedTest.java
new file mode 100644
index 0000000..e3f2644
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderAdvancedTest.java
@@ -0,0 +1,63 @@
+package com.fasterxml.jackson.databind.deser.builder;
+
+import com.fasterxml.jackson.annotation.*;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+
+public class BuilderAdvancedTest extends BaseMapTest
+{
+ @JsonDeserialize(builder=InjectableBuilderXY.class)
+ static class InjectableXY
+ {
+ final int _x, _y;
+ final String _stuff;
+
+ protected InjectableXY(int x, int y, String stuff) {
+ _x = x+1;
+ _y = y+1;
+ _stuff = stuff;
+ }
+ }
+
+ static class InjectableBuilderXY
+ {
+ public int x, y;
+
+ @JacksonInject
+ protected String stuff;
+
+ public InjectableBuilderXY withX(int x0) {
+ this.x = x0;
+ return this;
+ }
+
+ public InjectableBuilderXY withY(int y0) {
+ this.y = y0;
+ return this;
+ }
+
+ public InjectableXY build() {
+ return new InjectableXY(x, y, stuff);
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Unit tests
+ /**********************************************************
+ */
+
+ public void testWithInjectable() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.setInjectableValues(new InjectableValues.Std()
+ .addValue(String.class, "stuffValue")
+ );
+ InjectableXY bean = mapper.readValue(aposToQuotes("{'y':3,'x':7}"),
+ InjectableXY.class);
+ assertEquals(8, bean._x);
+ assertEquals(4, bean._y);
+ assertEquals("stuffValue", bean._stuff);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderErrorHandling.java b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderErrorHandling.java
new file mode 100644
index 0000000..f7f67b0
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderErrorHandling.java
@@ -0,0 +1,66 @@
+package com.fasterxml.jackson.databind.deser.builder;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+
+public class BuilderErrorHandling extends BaseMapTest
+{
+ @JsonDeserialize(builder=SimpleBuilderXY.class)
+ static class ValueClassXY
+ {
+ final int _x, _y;
+
+ protected ValueClassXY(int x, int y) {
+ _x = x+1;
+ _y = y+1;
+ }
+ }
+
+ static class SimpleBuilderXY
+ {
+ int x, y;
+
+ public SimpleBuilderXY withX(int x0) {
+ this.x = x0;
+ return this;
+ }
+
+ public SimpleBuilderXY withY(int y0) {
+ this.y = y0;
+ return this;
+ }
+
+ public ValueClassXY build() {
+ return new ValueClassXY(x, y);
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Unit tests
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testUnknownProperty() throws Exception
+ {
+ // first, default failure
+ String json = aposToQuotes("{'x':1,'z':2,'y':4}");
+ try {
+ MAPPER.readValue(json, ValueClassXY.class);
+ fail("Should not pass");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "unrecognized field");
+ }
+ // but pass if ok to ignore
+ ValueClassXY result = MAPPER.readerFor(ValueClassXY.class)
+ .without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
+ .readValue(json);
+ assertEquals(2, result._x);
+ assertEquals(5, result._y);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderFailTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderFailTest.java
new file mode 100644
index 0000000..b674a89
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderFailTest.java
@@ -0,0 +1,88 @@
+package com.fasterxml.jackson.databind.deser.builder;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
+import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
+
+public class BuilderFailTest extends BaseMapTest
+{
+ @JsonDeserialize(builder=SimpleBuilderXY.class)
+ static class ValueClassXY
+ {
+ final int _x, _y;
+
+ protected ValueClassXY(int x, int y) {
+ _x = x+1;
+ _y = y+1;
+ }
+ }
+
+ static class SimpleBuilderXY
+ {
+ public int x, y;
+
+ public SimpleBuilderXY withX(int x0) {
+ this.x = x0;
+ return this;
+ }
+
+ public SimpleBuilderXY withY(int y0) {
+ this.y = y0;
+ return this;
+ }
+
+ public ValueClassXY build() {
+ return new ValueClassXY(x, y);
+ }
+ }
+
+ // for [databind#761]
+ @JsonDeserialize(builder = ValueBuilderWrongBuildType.class)
+ static class ValueClassWrongBuildType {
+ }
+
+ static class ValueBuilderWrongBuildType
+ {
+ public int x;
+
+ public ValueBuilderWrongBuildType withX(int x0) {
+ this.x = x0;
+ return this;
+ }
+
+ public ValueClassXY build() {
+ return null;
+ }
+ }
+ /*
+ /**********************************************************
+ /* Unit tests
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testBuilderMethodReturnInvalidType() throws Exception
+ {
+ final String json = "{\"x\":1}";
+ try {
+ MAPPER.readValue(json, ValueClassWrongBuildType.class);
+ fail("Missing expected JsonProcessingException exception");
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "Build method");
+ verifyException(e, "has wrong return type");
+ }
+ }
+
+ public void testExtraFields() throws Exception
+ {
+ final String json = aposToQuotes("{'x':1,'y':2,'z':3}");
+ try {
+ MAPPER.readValue(json, ValueClassXY.class);
+ fail("should not pass");
+ } catch (UnrecognizedPropertyException e) {
+ verifyException(e, "Unrecognized field \"z\"");
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/creators/BuilderSimpleTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderSimpleTest.java
similarity index 77%
rename from src/test/java/com/fasterxml/jackson/databind/creators/BuilderSimpleTest.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderSimpleTest.java
index 3ae76e7..e7a40e7 100644
--- a/src/test/java/com/fasterxml/jackson/databind/creators/BuilderSimpleTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderSimpleTest.java
@@ -1,21 +1,21 @@
-package com.fasterxml.jackson.databind.creators;
+package com.fasterxml.jackson.databind.deser.builder;
import java.util.*;
-import com.fasterxml.jackson.annotation.JsonAnySetter;
-import com.fasterxml.jackson.annotation.JsonCreator;
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonSetter;
-import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.annotation.*;
+
+import com.fasterxml.jackson.core.Version;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
+import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
+import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
public class BuilderSimpleTest extends BaseMapTest
{
// // Simple 2-property value class, builder with standard naming
-
+
@JsonDeserialize(builder=SimpleBuilderXY.class)
static class ValueClassXY
{
@@ -47,7 +47,7 @@
}
// // 3-property value, with more varied builder
-
+
@JsonDeserialize(builder=BuildABC.class)
static class ValueClassABC
{
@@ -60,6 +60,7 @@
}
}
+ @JsonIgnoreProperties({ "d" })
static class BuildABC
{
public int a; // to be used as is
@@ -105,7 +106,6 @@
return new ValueImmutable(value);
}
}
-
// And then with custom naming:
@JsonDeserialize(builder=BuildFoo.class)
@@ -128,40 +128,6 @@
}
}
- // And with creator(s)
-
- @JsonDeserialize(builder=CreatorBuilder.class)
- static class CreatorValue
- {
- final int a, b, c;
-
- protected CreatorValue(int a, int b, int c) {
- this.a = a;
- this.b = b;
- this.c = c;
- }
- }
-
- static class CreatorBuilder {
- private final int a, b;
- private int c;
-
- @JsonCreator
- public CreatorBuilder(@JsonProperty("a") int a,
- @JsonProperty("b") int b)
- {
- this.a = a;
- this.b = b;
- }
-
- public CreatorBuilder withC(int v) {
- c = v;
- return this;
- }
- public CreatorValue build() {
- return new CreatorValue(a, b, c);
- }
- }
// for [databind#761]
@@ -231,25 +197,6 @@
return new ValueInterface2Impl(x);
}
}
-
- // for [databind#761]
- @JsonDeserialize(builder = ValueBuilderWrongBuildType.class)
- static class ValueClassWrongBuildType {
- }
-
- static class ValueBuilderWrongBuildType
- {
- public int x;
-
- public ValueBuilderWrongBuildType withX(int x0) {
- this.x = x0;
- return this;
- }
-
- public ValueClassXY build() {
- return null;
- }
- }
// [databind#777]
@JsonDeserialize(builder = SelfBuilder777.class)
@@ -296,6 +243,31 @@
}
}
+ protected static class NopModule1557 extends Module
+ {
+ @Override
+ public String getModuleName() {
+ return "NopModule";
+ }
+
+ @Override
+ public Version version() {
+ return Version.unknownVersion();
+ }
+
+ @Override
+ public void setupModule(SetupContext setupContext) {
+ // This annotation introspector has no opinion about builders, make sure it doesn't interfere
+ setupContext.insertAnnotationIntrospector(new NopAnnotationIntrospector() {
+ private static final long serialVersionUID = 1L;
+ @Override
+ public Version version() {
+ return Version.unknownVersion();
+ }
+ });
+ }
+ }
+
/*
/**********************************************************
/* Unit tests
@@ -306,7 +278,7 @@
public void testSimple() throws Exception
{
- String json = "{\"x\":1,\"y\":2}";
+ String json = aposToQuotes("{'x':1,'y':2}");
Object o = MAPPER.readValue(json, ValueClassXY.class);
assertNotNull(o);
assertSame(ValueClassXY.class, o.getClass());
@@ -320,13 +292,14 @@
public void testSimpleWithIgnores() throws Exception
{
// 'z' is unknown, and would fail by default:
- String json = "{\"x\":1,\"y\":2,\"z\":3}";
+ final String json = aposToQuotes("{'x':1,'y':2,'z':4}");
Object o = null;
try {
o = MAPPER.readValue(json, ValueClassXY.class);
fail("Should not pass");
- } catch (JsonMappingException e) {
+ } catch (UnrecognizedPropertyException e) {
+ assertEquals("z", e.getPropertyName());
verifyException(e, "Unrecognized field \"z\"");
}
@@ -345,13 +318,19 @@
public void testMultiAccess() throws Exception
{
- String json = "{\"c\":3,\"a\":2,\"b\":-9}";
+ String json = aposToQuotes("{'c':3,'a':2,'b':-9}");
ValueClassABC value = MAPPER.readValue(json, ValueClassABC.class);
assertNotNull(value);
- // note: ctor adds one to both values
- assertEquals(value.a, 2);
- assertEquals(value.b, -9);
- assertEquals(value.c, 3);
+ assertEquals(2, value.a);
+ assertEquals(-9, value.b);
+ assertEquals(3, value.c);
+
+ // also, since we can ignore some properties:
+ value = MAPPER.readValue(aposToQuotes("{'c':3,'d':5,'b':-9}"), ValueClassABC.class);
+ assertNotNull(value);
+ assertEquals(0, value.a);
+ assertEquals(-9, value.b);
+ assertEquals(3, value.c);
}
// test for Immutable builder, to ensure return value is used
@@ -370,16 +349,6 @@
assertEquals(1, value.value);
}
- // test to ensure @JsonCreator also work
- public void testWithCreator() throws Exception
- {
- final String json = "{\"a\":1,\"c\":3,\"b\":2}";
- CreatorValue value = MAPPER.readValue(json, CreatorValue.class);
- assertEquals(1, value.a);
- assertEquals(2, value.b);
- assertEquals(3, value.c);
- }
-
// for [databind#761]
public void testBuilderMethodReturnMoreGeneral() throws Exception
@@ -395,19 +364,6 @@
ValueInterface2 value = MAPPER.readValue(json, ValueInterface2.class);
assertEquals(2, value.getX());
}
-
- public void testBuilderMethodReturnInvalidType() throws Exception
- {
- final String json = "{\"x\":1}";
- try {
- MAPPER.readValue(json, ValueClassWrongBuildType.class);
- fail("Missing expected JsonProcessingException exception");
- } catch(JsonProcessingException e) {
- assertTrue(
- "Exception cause must be IllegalArgumentException",
- e.getCause() instanceof IllegalArgumentException);
- }
- }
public void testSelfBuilder777() throws Exception
{
@@ -432,5 +388,11 @@
assertTrue(((List<?>) ob).isEmpty());
}
-
+ public void testPOJOConfigResolution1557() throws Exception
+ {
+ final String json = "{\"value\":1}";
+ MAPPER.registerModule(new NopModule1557());
+ ValueFoo value = MAPPER.readValue(json, ValueFoo.class);
+ assertEquals(1, value.value);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderViaUpdateTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderViaUpdateTest.java
new file mode 100644
index 0000000..f5b63a5
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderViaUpdateTest.java
@@ -0,0 +1,89 @@
+package com.fasterxml.jackson.databind.deser.builder;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
+
+/**
+ * Tests to ensure that use of "updateValue()" will fail with builder-based deserializers.
+ *
+ * @since 2.9
+ */
+public class BuilderViaUpdateTest extends BaseMapTest
+{
+ @JsonDeserialize(builder=SimpleBuilderXY.class)
+ static class ValueClassXY
+ {
+ protected int x, y;
+
+ protected ValueClassXY(int x, int y) {
+ x = x+1;
+ y = y+1;
+ }
+ }
+
+ static class SimpleBuilderXY
+ {
+ public int x, y;
+
+ public SimpleBuilderXY withX(int x0) {
+ this.x = x0;
+ return this;
+ }
+
+ public SimpleBuilderXY withY(int y0) {
+ this.y = y0;
+ return this;
+ }
+
+ public ValueClassXY build() {
+ return new ValueClassXY(x, y);
+ }
+ }
+
+ /*
+ /*****************************************************
+ /* Basic tests, potential (but not current) success cases
+ /*****************************************************
+ */
+
+ private final static ObjectMapper MAPPER = new ObjectMapper();
+
+ // Tests where result value is passed as thing to update
+ public void testBuilderUpdateWithValue() throws Exception
+ {
+ try {
+ /*ValueClassXY value =*/ MAPPER.readerFor(ValueClassXY.class)
+ .withValueToUpdate(new ValueClassXY(6, 7))
+ .readValue(aposToQuotes("{'x':1,'y:'2'}"));
+ fail("Should not have passed");
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "Deserialization of");
+ verifyException(e, "by passing existing instance");
+ verifyException(e, "ValueClassXY");
+ }
+ }
+
+ /*
+ /*****************************************************
+ /* Failing test cases
+ /*****************************************************
+ */
+
+ // and then test to ensure error handling works as expected if attempts
+ // is made to pass builder (API requires value, not builder)
+ public void testBuilderWithWrongType() throws Exception
+ {
+ try {
+ /* Object result =*/ MAPPER.readerFor(ValueClassXY.class)
+ .withValueToUpdate(new SimpleBuilderXY())
+ .readValue(aposToQuotes("{'x':1,'y:'2'}"));
+ fail("Should not have passed");
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "Deserialization of");
+ verifyException(e, "by passing existing Builder");
+ verifyException(e, "SimpleBuilderXY");
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderWithCreatorTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderWithCreatorTest.java
new file mode 100644
index 0000000..fd2e44c
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderWithCreatorTest.java
@@ -0,0 +1,176 @@
+package com.fasterxml.jackson.databind.deser.builder;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+
+public class BuilderWithCreatorTest extends BaseMapTest
+{
+ @JsonDeserialize(builder=PropertyCreatorBuilder.class)
+ static class PropertyCreatorValue
+ {
+ final int a, b, c;
+
+ protected PropertyCreatorValue(int a, int b, int c) {
+ this.a = a;
+ this.b = b;
+ this.c = c;
+ }
+ }
+
+ static class PropertyCreatorBuilder {
+ private final int a, b;
+ private int c;
+
+ @JsonCreator
+ public PropertyCreatorBuilder(@JsonProperty("a") int a,
+ @JsonProperty("b") int b)
+ {
+ this.a = a;
+ this.b = b;
+ }
+
+ public PropertyCreatorBuilder withC(int v) {
+ c = v;
+ return this;
+ }
+ public PropertyCreatorValue build() {
+ return new PropertyCreatorValue(a, b, c);
+ }
+ }
+
+ // With String
+
+ @JsonDeserialize(builder=StringCreatorBuilder.class)
+ static class StringCreatorValue
+ {
+ final String str;
+
+ protected StringCreatorValue(String s) { str = s; }
+ }
+
+ static class StringCreatorBuilder {
+ private final String v;
+
+ @JsonCreator
+ public StringCreatorBuilder(String str) {
+ v = str;
+ }
+
+ public StringCreatorValue build() {
+ return new StringCreatorValue(v);
+ }
+ }
+
+ // With boolean
+
+ @JsonDeserialize(builder=BooleanCreatorBuilder.class)
+ static class BooleanCreatorValue
+ {
+ final boolean value;
+
+ protected BooleanCreatorValue(boolean v) { value = v; }
+ }
+
+ static class BooleanCreatorBuilder {
+ private final boolean value;
+
+ @JsonCreator
+ public BooleanCreatorBuilder(boolean v) {
+ value = v;
+ }
+
+ public BooleanCreatorValue build() {
+ return new BooleanCreatorValue(value);
+ }
+ }
+
+ // With Int
+
+ @JsonDeserialize(builder=IntCreatorBuilder.class)
+ static class IntCreatorValue
+ {
+ final int value;
+
+ protected IntCreatorValue(int v) { value = v; }
+ }
+
+ static class IntCreatorBuilder {
+ private final int value;
+
+ @JsonCreator
+ public IntCreatorBuilder(int v) {
+ value = v;
+ }
+
+ public IntCreatorValue build() {
+ return new IntCreatorValue(value);
+ }
+ }
+
+ // With Double
+
+ @JsonDeserialize(builder=DoubleCreatorBuilder.class)
+ static class DoubleCreatorValue
+ {
+ final double value;
+
+ protected DoubleCreatorValue(double v) { value = v; }
+ }
+
+ static class DoubleCreatorBuilder {
+ private final double value;
+
+ @JsonCreator
+ public DoubleCreatorBuilder(double v) {
+ value = v;
+ }
+
+ public DoubleCreatorValue build() {
+ return new DoubleCreatorValue(value);
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Unit tests
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testWithPropertiesCreator() throws Exception
+ {
+ final String json = aposToQuotes("{'a':1,'c':3,'b':2}");
+ PropertyCreatorValue value = MAPPER.readValue(json, PropertyCreatorValue.class);
+ assertEquals(1, value.a);
+ assertEquals(2, value.b);
+ assertEquals(3, value.c);
+ }
+
+ public void testWithDelegatingStringCreator() throws Exception
+ {
+ final int EXP = 139;
+ IntCreatorValue value = MAPPER.readValue(String.valueOf(EXP),
+ IntCreatorValue.class);
+ assertEquals(EXP, value.value);
+ }
+
+ public void testWithDelegatingIntCreator() throws Exception
+ {
+ final double EXP = -3.75;
+ DoubleCreatorValue value = MAPPER.readValue(String.valueOf(EXP),
+ DoubleCreatorValue.class);
+ assertEquals(EXP, value.value);
+ }
+
+ public void testWithDelegatingBooleanCreator() throws Exception
+ {
+ final boolean EXP = true;
+ BooleanCreatorValue value = MAPPER.readValue(String.valueOf(EXP),
+ BooleanCreatorValue.class);
+ assertEquals(EXP, value.value);
+ }
+}
+
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderWithUnwrappedTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderWithUnwrappedTest.java
index 89726cb..a787584 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderWithUnwrappedTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderWithUnwrappedTest.java
@@ -8,13 +8,8 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
-public class BuilderWithUnwrappedTest extends BaseMapTest {
- /*
- *************************************
- * Mock classes
- *************************************
- */
-
+public class BuilderWithUnwrappedTest extends BaseMapTest
+{
final static class Name {
private final String first;
private final String last;
@@ -161,9 +156,9 @@
}
/*
- *************************************
- * Unit tests
- *************************************
+ /**********************************************************
+ /* Unit tests
+ /**********************************************************
*/
public void testWithUnwrappedAndCreatorSingleParameterAtBeginning() throws Exception {
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderWithViewTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderWithViewTest.java
new file mode 100644
index 0000000..346112e
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/builder/BuilderWithViewTest.java
@@ -0,0 +1,114 @@
+package com.fasterxml.jackson.databind.deser.builder;
+
+import com.fasterxml.jackson.annotation.*;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+
+public class BuilderWithViewTest extends BaseMapTest
+{
+ static class ViewX { }
+ static class ViewY { }
+
+ @JsonDeserialize(builder=SimpleBuilderXY.class)
+ static class ValueClassXY
+ {
+ final int _x, _y;
+
+ protected ValueClassXY(int x, int y) {
+ _x = x+1;
+ _y = y+1;
+ }
+ }
+
+ static class SimpleBuilderXY
+ {
+ public int x, y;
+
+ @JsonView(ViewX.class)
+ public SimpleBuilderXY withX(int x0) {
+ this.x = x0;
+ return this;
+ }
+
+ @JsonView(ViewY.class)
+ public SimpleBuilderXY withY(int y0) {
+ this.y = y0;
+ return this;
+ }
+
+ public ValueClassXY build() {
+ return new ValueClassXY(x, y);
+ }
+ }
+
+ @JsonDeserialize(builder=CreatorBuilderXY.class)
+ static class CreatorValueXY
+ {
+ final int _x, _y;
+
+ protected CreatorValueXY(int x, int y) {
+ _x = x;
+ _y = y;
+ }
+ }
+
+ @JsonIgnoreProperties({ "bogus" })
+ static class CreatorBuilderXY
+ {
+ public int x, y;
+
+ @JsonCreator
+ public CreatorBuilderXY(@JsonProperty("x") @JsonView(ViewX.class) int x,
+ @JsonProperty("y") @JsonView(ViewY.class) int y)
+ {
+ this.x = x;
+ this.y = y;
+ }
+
+ public CreatorValueXY build() {
+ return new CreatorValueXY(x, y);
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Unit tests
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testSimpleViews() throws Exception
+ {
+ final String json = aposToQuotes("{'x':5,'y':10}");
+ ValueClassXY resultX = MAPPER.readerFor(ValueClassXY.class)
+ .withView(ViewX.class)
+ .readValue(json);
+ assertEquals(6, resultX._x);
+ assertEquals(1, resultX._y);
+
+ ValueClassXY resultY = MAPPER.readerFor(ValueClassXY.class)
+ .withView(ViewY.class)
+ .readValue(json);
+ assertEquals(1, resultY._x);
+ assertEquals(11, resultY._y);
+ }
+
+ public void testCreatorViews() throws Exception
+ {
+ final String json = aposToQuotes("{'x':5,'y':10,'bogus':false}");
+ CreatorValueXY resultX = MAPPER.readerFor(CreatorValueXY.class)
+ .withView(ViewX.class)
+ .readValue(json);
+ assertEquals(5, resultX._x);
+ assertEquals(0, resultX._y);
+
+ CreatorValueXY resultY = MAPPER.readerFor(CreatorValueXY.class)
+ .withView(ViewY.class)
+ .readValue(json);
+ assertEquals(0, resultY._x);
+ assertEquals(10, resultY._y);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/IgnoreCreatorProp1317Test.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/IgnoreCreatorProp1317Test.java
similarity index 89%
rename from src/test/java/com/fasterxml/jackson/databind/filter/IgnoreCreatorProp1317Test.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/filter/IgnoreCreatorProp1317Test.java
index 4df9611..7117007 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/IgnoreCreatorProp1317Test.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/IgnoreCreatorProp1317Test.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.deser.filter;
import java.beans.ConstructorProperties;
@@ -41,10 +41,10 @@
}
public void testThatJsonIgnoreWorksWithConstructorProperties() throws Exception {
+ ObjectMapper om = objectMapper();
Testing testing = new Testing("shouldBeIgnored", "notIgnore");
- ObjectMapper om = new ObjectMapper();
String json = om.writeValueAsString(testing);
- System.out.println(json);
+// System.out.println(json);
assertFalse(json.contains("shouldBeIgnored"));
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/IgnorePropertyOnDeserTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/IgnorePropertyOnDeserTest.java
similarity index 95%
rename from src/test/java/com/fasterxml/jackson/databind/filter/IgnorePropertyOnDeserTest.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/filter/IgnorePropertyOnDeserTest.java
index 08b882d..5255d4b 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/IgnorePropertyOnDeserTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/IgnorePropertyOnDeserTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.deser.filter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@@ -40,8 +40,8 @@
/* Unit tests
/****************************************************************
*/
-
- private final ObjectMapper MAPPER = new ObjectMapper();
+
+ private final ObjectMapper MAPPER = newObjectMapper();
// [databind#1217]
public void testIgnoreOnProperty1217() throws Exception
@@ -54,7 +54,7 @@
assertEquals(1, result.obj.x);
assertEquals(2, result.obj2.y);
-
+
TestIgnoreObject result1 = MAPPER.readValue(
aposToQuotes("{'obj':{'x': 20, 'y': 30}, 'obj2':{'x': 20, 'y': 40}}"),
TestIgnoreObject.class);
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsForContentTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsForContentTest.java
new file mode 100644
index 0000000..dfb3d37
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsForContentTest.java
@@ -0,0 +1,434 @@
+package com.fasterxml.jackson.databind.deser.filter;
+
+import java.util.*;
+
+import com.fasterxml.jackson.annotation.JsonSetter;
+import com.fasterxml.jackson.annotation.Nulls;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.InvalidNullException;
+
+// For [databind#1402]; configurable null handling, for contents of
+// Collections, Maps, arrays
+public class NullConversionsForContentTest extends BaseMapTest
+{
+ static class NullContentFail<T> {
+ public T nullsOk;
+
+ @JsonSetter(contentNulls=Nulls.FAIL)
+ public T noNulls;
+ }
+
+ static class NullContentAsEmpty<T> {
+ @JsonSetter(contentNulls=Nulls.AS_EMPTY)
+ public T values;
+ }
+
+ static class NullContentSkip<T> {
+ @JsonSetter(contentNulls=Nulls.SKIP)
+ public T values;
+ }
+
+ static class NullContentUndefined<T> {
+ @JsonSetter // leave with defaults
+ public T values;
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, fail-on-null
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper();
+
+ // Tests to verify that we can set default settings for failure
+ public void testFailOnNullFromDefaults() throws Exception
+ {
+ final String JSON = aposToQuotes("{'values':[null]}");
+ TypeReference<?> listType = new TypeReference<NullContentUndefined<List<String>>>() { };
+
+ // by default fine to get nulls
+ NullContentUndefined<List<String>> result = MAPPER.readValue(JSON, listType);
+ assertNotNull(result.values);
+ assertEquals(1, result.values.size());
+ assertNull(result.values.get(0));
+
+ // but not when overridden globally:
+ ObjectMapper mapper = newObjectMapper();
+ mapper.setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.FAIL));
+ try {
+ mapper.readValue(JSON, listType);
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"values\"");
+ }
+
+ // or configured for type:
+ mapper = newObjectMapper();
+ mapper.configOverride(List.class)
+ .setSetterInfo(JsonSetter.Value.forContentNulls(Nulls.FAIL));
+ try {
+ mapper.readValue(JSON, listType);
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"values\"");
+ }
+ }
+
+ public void testFailOnNullWithCollections() throws Exception
+ {
+ TypeReference<?> typeRef = new TypeReference<NullContentFail<List<Integer>>>() { };
+
+ // first, ok if assigning non-null to not-nullable, null for nullable
+ NullContentFail<List<Integer>> result = MAPPER.readValue(aposToQuotes("{'nullsOk':[null]}"),
+ typeRef);
+ assertNotNull(result.nullsOk);
+ assertEquals(1, result.nullsOk.size());
+ assertNull(result.nullsOk.get(0));
+
+ // and then see that nulls are not ok for non-nullable.
+
+ // List<Integer>
+ final String JSON = aposToQuotes("{'noNulls':[null]}");
+ try {
+ MAPPER.readValue(JSON, typeRef);
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"noNulls\"");
+ }
+
+ // List<String>
+ try {
+ MAPPER.readValue(JSON, new TypeReference<NullContentFail<List<String>>>() { });
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"noNulls\"");
+ }
+ }
+
+ public void testFailOnNullWithArrays() throws Exception
+ {
+ final String JSON = aposToQuotes("{'noNulls':[null]}");
+ // Object[]
+ try {
+ MAPPER.readValue(JSON, new TypeReference<NullContentFail<Object[]>>() { });
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"noNulls\"");
+ }
+
+ // String[]
+ try {
+ MAPPER.readValue(JSON, new TypeReference<NullContentFail<String[]>>() { });
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"noNulls\"");
+ }
+ }
+
+ public void testFailOnNullWithPrimitiveArrays() throws Exception
+ {
+ final String JSON = aposToQuotes("{'noNulls':[null]}");
+
+ // boolean[]
+ try {
+ MAPPER.readValue(JSON, new TypeReference<NullContentFail<boolean[]>>() { });
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"noNulls\"");
+ }
+ // int[]
+ try {
+ MAPPER.readValue(JSON, new TypeReference<NullContentFail<int[]>>() { });
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"noNulls\"");
+ }
+ // double[]
+ try {
+ MAPPER.readValue(JSON, new TypeReference<NullContentFail<double[]>>() { });
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"noNulls\"");
+ }
+ }
+
+ public void testFailOnNullWithMaps() throws Exception
+ {
+ // Then: Map<String,String>
+ try {
+ final String MAP_JSON = aposToQuotes("{'noNulls':{'a':null}}");
+ MAPPER.readValue(MAP_JSON, new TypeReference<NullContentFail<Map<String,String>>>() { });
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"noNulls\"");
+ }
+
+ // Then: EnumMap<Enum,String>
+ try {
+ final String MAP_JSON = aposToQuotes("{'noNulls':{'A':null}}");
+ MAPPER.readValue(MAP_JSON, new TypeReference<NullContentFail<EnumMap<ABC,String>>>() { });
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"noNulls\"");
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, null-as-empty
+ /**********************************************************
+ */
+
+ public void testNullsAsEmptyWithCollections() throws Exception
+ {
+ final String JSON = aposToQuotes("{'values':[null]}");
+
+ // List<Integer>
+ {
+ NullContentAsEmpty<List<Integer>> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentAsEmpty<List<Integer>>>() { });
+ assertEquals(1, result.values.size());
+ assertEquals(Integer.valueOf(0), result.values.get(0));
+ }
+
+ // List<String>
+ {
+ NullContentAsEmpty<List<String>> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentAsEmpty<List<String>>>() { });
+ assertEquals(1, result.values.size());
+ assertEquals("", result.values.get(0));
+ }
+ }
+
+ public void testNullsAsEmptyUsingDefaults() throws Exception
+ {
+ final String JSON = aposToQuotes("{'values':[null]}");
+ TypeReference<?> listType = new TypeReference<NullContentUndefined<List<Integer>>>() { };
+
+ // Let's see defaulting in action
+ ObjectMapper mapper = newObjectMapper();
+ mapper.setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.AS_EMPTY));
+ NullContentUndefined<List<Integer>> result = mapper.readValue(JSON, listType);
+ assertEquals(1, result.values.size());
+ assertEquals(Integer.valueOf(0), result.values.get(0));
+
+ // or configured for type:
+ mapper = newObjectMapper();
+ mapper.configOverride(List.class)
+ .setSetterInfo(JsonSetter.Value.forContentNulls(Nulls.AS_EMPTY));
+ result = mapper.readValue(JSON, listType);
+ assertEquals(1, result.values.size());
+ assertEquals(Integer.valueOf(0), result.values.get(0));
+ }
+
+ public void testNullsAsEmptyWithArrays() throws Exception
+ {
+ // Note: skip `Object[]`, no default empty value at this point
+ final String JSON = aposToQuotes("{'values':[null]}");
+
+ // Then: String[]
+ {
+ NullContentAsEmpty<String[]> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentAsEmpty<String[]>>() { });
+ assertEquals(1, result.values.length);
+ assertEquals("", result.values[0]);
+ }
+ }
+
+ public void testNullsAsEmptyWithPrimitiveArrays() throws Exception
+ {
+ final String JSON = aposToQuotes("{'values':[null]}");
+
+ // int[]
+ {
+ NullContentAsEmpty<int[]> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentAsEmpty<int[]>>() { });
+ assertEquals(1, result.values.length);
+ assertEquals(0, result.values[0]);
+ }
+
+ // long[]
+ {
+ NullContentAsEmpty<long[]> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentAsEmpty<long[]>>() { });
+ assertEquals(1, result.values.length);
+ assertEquals(0L, result.values[0]);
+ }
+
+ // boolean[]
+ {
+ NullContentAsEmpty<boolean[]> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentAsEmpty<boolean[]>>() { });
+ assertEquals(1, result.values.length);
+ assertEquals(false, result.values[0]);
+ }
+}
+
+ public void testNullsAsEmptyWithMaps() throws Exception
+ {
+ // Then: Map<String,String>
+ final String MAP_JSON = aposToQuotes("{'values':{'A':null}}");
+ {
+ NullContentAsEmpty<Map<String,String>> result
+ = MAPPER.readValue(MAP_JSON, new TypeReference<NullContentAsEmpty<Map<String,String>>>() { });
+ assertEquals(1, result.values.size());
+ assertEquals("A", result.values.entrySet().iterator().next().getKey());
+ assertEquals("", result.values.entrySet().iterator().next().getValue());
+ }
+
+ // Then: EnumMap<Enum,String>
+ {
+ NullContentAsEmpty<EnumMap<ABC,String>> result
+ = MAPPER.readValue(MAP_JSON, new TypeReference<NullContentAsEmpty<EnumMap<ABC,String>>>() { });
+ assertEquals(1, result.values.size());
+ assertEquals(ABC.A, result.values.entrySet().iterator().next().getKey());
+ assertEquals("", result.values.entrySet().iterator().next().getValue());
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, skip-nulls
+ /**********************************************************
+ */
+
+ public void testNullsSkipUsingDefaults() throws Exception
+ {
+ final String JSON = aposToQuotes("{'values':[null]}");
+ TypeReference<?> listType = new TypeReference<NullContentUndefined<List<Long>>>() { };
+
+ // Let's see defaulting in action
+ ObjectMapper mapper = newObjectMapper();
+ mapper.setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP));
+ NullContentUndefined<List<Long>> result = mapper.readValue(JSON, listType);
+ assertEquals(0, result.values.size());
+
+ // or configured for type:
+ mapper = newObjectMapper();
+ mapper.configOverride(List.class)
+ .setSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP));
+ result = mapper.readValue(JSON, listType);
+ assertEquals(0, result.values.size());
+ }
+
+ // Test to verify that per-property setting overrides defaults:
+ public void testNullsSkipWithOverrides() throws Exception
+ {
+ final String JSON = aposToQuotes("{'values':[null]}");
+ TypeReference<?> listType = new TypeReference<NullContentSkip<List<Long>>>() { };
+
+ ObjectMapper mapper = newObjectMapper();
+ // defaults call for fail; but POJO specifies "skip"; latter should win
+ mapper.setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.FAIL));
+ NullContentSkip<List<Long>> result = mapper.readValue(JSON, listType);
+ assertEquals(0, result.values.size());
+
+ // ditto for per-type defaults
+ mapper = newObjectMapper();
+ mapper.configOverride(List.class)
+ .setSetterInfo(JsonSetter.Value.forContentNulls(Nulls.FAIL));
+ result = mapper.readValue(JSON, listType);
+ assertEquals(0, result.values.size());
+ }
+
+ public void testNullsSkipWithCollections() throws Exception
+ {
+ // List<Integer>
+ {
+ final String JSON = aposToQuotes("{'values':[1,null,2]}");
+ NullContentSkip<List<Integer>> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentSkip<List<Integer>>>() { });
+ assertEquals(2, result.values.size());
+ assertEquals(Integer.valueOf(1), result.values.get(0));
+ assertEquals(Integer.valueOf(2), result.values.get(1));
+ }
+
+ // List<String>
+ {
+ final String JSON = aposToQuotes("{'values':['ab',null,'xy']}");
+ NullContentSkip<List<String>> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentSkip<List<String>>>() { });
+ assertEquals(2, result.values.size());
+ assertEquals("ab", result.values.get(0));
+ assertEquals("xy", result.values.get(1));
+ }
+ }
+
+ public void testNullsSkipWithArrays() throws Exception
+ {
+ final String JSON = aposToQuotes("{'values':['a',null,'xy']}");
+ // Object[]
+ {
+ NullContentSkip<Object[]> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentSkip<Object[]>>() { });
+ assertEquals(2, result.values.length);
+ assertEquals("a", result.values[0]);
+ assertEquals("xy", result.values[1]);
+ }
+ // String[]
+ {
+ NullContentSkip<String[]> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentSkip<String[]>>() { });
+ assertEquals(2, result.values.length);
+ assertEquals("a", result.values[0]);
+ assertEquals("xy", result.values[1]);
+ }
+ }
+
+ public void testNullsSkipWithPrimitiveArrays() throws Exception
+ {
+ // int[]
+ {
+ final String JSON = aposToQuotes("{'values':[3,null,7]}");
+ NullContentSkip<int[]> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentSkip<int[]>>() { });
+ assertEquals(2, result.values.length);
+ assertEquals(3, result.values[0]);
+ assertEquals(7, result.values[1]);
+ }
+
+ // long[]
+ {
+ final String JSON = aposToQuotes("{'values':[-13,null,999]}");
+ NullContentSkip<long[]> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentSkip<long[]>>() { });
+ assertEquals(2, result.values.length);
+ assertEquals(-13L, result.values[0]);
+ assertEquals(999L, result.values[1]);
+ }
+
+ // boolean[]
+ {
+ final String JSON = aposToQuotes("{'values':[true,null,true]}");
+ NullContentSkip<boolean[]> result = MAPPER.readValue(JSON,
+ new TypeReference<NullContentSkip<boolean[]>>() { });
+ assertEquals(2, result.values.length);
+ assertEquals(true, result.values[0]);
+ assertEquals(true, result.values[1]);
+ }
+ }
+
+ public void testNullsSkipWithMaps() throws Exception
+ {
+ // Then: Map<String,String>
+ final String MAP_JSON = aposToQuotes("{'values':{'A':'foo','B':null,'C':'bar'}}");
+ {
+ NullContentSkip<Map<String,String>> result
+ = MAPPER.readValue(MAP_JSON, new TypeReference<NullContentSkip<Map<String,String>>>() { });
+ assertEquals(2, result.values.size());
+ assertEquals("foo", result.values.get("A"));
+ assertEquals("bar", result.values.get("C"));
+ }
+
+ // Then: EnumMap<Enum,String>
+ {
+ NullContentSkip<EnumMap<ABC,String>> result
+ = MAPPER.readValue(MAP_JSON, new TypeReference<NullContentSkip<EnumMap<ABC,String>>>() { });
+ assertEquals(2, result.values.size());
+ assertEquals("foo", result.values.get(ABC.A));
+ assertEquals("bar", result.values.get(ABC.C));
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsGenericTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsGenericTest.java
new file mode 100644
index 0000000..f48aa95
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsGenericTest.java
@@ -0,0 +1,114 @@
+package com.fasterxml.jackson.databind.deser.filter;
+
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonSetter;
+import com.fasterxml.jackson.annotation.Nulls;
+import com.fasterxml.jackson.core.type.TypeReference;
+
+import com.fasterxml.jackson.databind.*;
+
+// for [databind#1402]; configurable null handling, for values themselves,
+// using generic types
+public class NullConversionsGenericTest extends BaseMapTest
+{
+ static class GeneralEmpty<T> {
+ // 09-Feb-2017, tatu: Should only need annotation either for field OR setter, not both:
+// @JsonSetter(nulls=JsonSetter.Nulls.AS_EMPTY)
+ T value;
+
+ @JsonSetter(nulls=Nulls.AS_EMPTY)
+ public void setValue(T v) {
+ value = v;
+ }
+ }
+
+ static class NoCtorWrapper {
+ @JsonSetter(nulls=Nulls.AS_EMPTY)
+ public NoCtorPOJO value;
+ }
+
+ static class NoCtorPOJO {
+ public NoCtorPOJO(boolean b) { }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper();
+
+ public void testNullsToEmptyPojo() throws Exception
+ {
+ GeneralEmpty<Point> result = MAPPER.readValue(aposToQuotes("{'value':null}"),
+ new TypeReference<GeneralEmpty<Point>>() { });
+ assertNotNull(result.value);
+ Point p = result.value;
+ assertEquals(0, p.x);
+ assertEquals(0, p.y);
+
+ // and then also failing case with no suitable creator:
+ try {
+ /* NoCtorWrapper nogo =*/ MAPPER.readValue(aposToQuotes("{'value':null}"),
+ NoCtorWrapper.class);
+ fail("Should not pass");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not create empty instance");
+ }
+ }
+
+ public void testNullsToEmptyCollection() throws Exception
+ {
+ GeneralEmpty<List<String>> result = MAPPER.readValue(aposToQuotes("{'value':null}"),
+ new TypeReference<GeneralEmpty<List<String>>>() { });
+ assertNotNull(result.value);
+ assertEquals(0, result.value.size());
+
+ // but also non-String type, since impls vary
+ GeneralEmpty<List<Integer>> result2 = MAPPER.readValue(aposToQuotes("{'value':null}"),
+ new TypeReference<GeneralEmpty<List<Integer>>>() { });
+ assertNotNull(result2.value);
+ assertEquals(0, result2.value.size());
+ }
+
+ public void testNullsToEmptyMap() throws Exception
+ {
+ GeneralEmpty<Map<String,String>> result = MAPPER.readValue(aposToQuotes("{'value':null}"),
+ new TypeReference<GeneralEmpty<Map<String,String>>>() { });
+ assertNotNull(result.value);
+ assertEquals(0, result.value.size());
+ }
+
+ public void testNullsToEmptyArrays() throws Exception
+ {
+ final String json = aposToQuotes("{'value':null}");
+
+ GeneralEmpty<Object[]> result = MAPPER.readValue(json,
+ new TypeReference<GeneralEmpty<Object[]>>() { });
+ assertNotNull(result.value);
+ assertEquals(0, result.value.length);
+
+ GeneralEmpty<String[]> result2 = MAPPER.readValue(json,
+ new TypeReference<GeneralEmpty<String[]>>() { });
+ assertNotNull(result2.value);
+ assertEquals(0, result2.value.length);
+
+ GeneralEmpty<int[]> result3 = MAPPER.readValue(json,
+ new TypeReference<GeneralEmpty<int[]>>() { });
+ assertNotNull(result3.value);
+ assertEquals(0, result3.value.length);
+
+ GeneralEmpty<double[]> result4 = MAPPER.readValue(json,
+ new TypeReference<GeneralEmpty<double[]>>() { });
+ assertNotNull(result4.value);
+ assertEquals(0, result4.value.length);
+
+ GeneralEmpty<boolean[]> result5 = MAPPER.readValue(json,
+ new TypeReference<GeneralEmpty<boolean[]>>() { });
+ assertNotNull(result5.value);
+ assertEquals(0, result5.value.length);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsPojoTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsPojoTest.java
new file mode 100644
index 0000000..b022fc6
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsPojoTest.java
@@ -0,0 +1,107 @@
+package com.fasterxml.jackson.databind.deser.filter;
+
+import com.fasterxml.jackson.annotation.JsonSetter;
+import com.fasterxml.jackson.annotation.Nulls;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.InvalidNullException;
+
+// for [databind#1402]; configurable null handling, for values themselves
+public class NullConversionsPojoTest extends BaseMapTest
+{
+ static class NullFail {
+ public String nullsOk = "a";
+
+ @JsonSetter(nulls=Nulls.FAIL)
+ public String noNulls = "b";
+ }
+
+ static class NullAsEmpty {
+ public String nullsOk = "a";
+
+ @JsonSetter(nulls=Nulls.AS_EMPTY)
+ public String nullAsEmpty = "b";
+ }
+
+ static class NullsForString {
+ /*
+ String n = "foo";
+
+ public void setName(String name) {
+ n = name;
+ }
+ */
+
+ String n = "foo";
+
+ public void setName(String n0) { n = n0; }
+ public String getName() { return n; }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper();
+
+ public void testFailOnNull() throws Exception
+ {
+ // first, ok if assigning non-null to not-nullable, null for nullable
+ NullFail result = MAPPER.readValue(aposToQuotes("{'noNulls':'foo', 'nullsOk':null}"),
+ NullFail.class);
+ assertEquals("foo", result.noNulls);
+ assertNull(result.nullsOk);
+
+ // and then see that nulls are not ok for non-nullable
+ try {
+ result = MAPPER.readValue(aposToQuotes("{'noNulls':null}"),
+ NullFail.class);
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"noNulls\"");
+ }
+ }
+
+ public void testFailOnNullWithDefaults() throws Exception
+ {
+ // also: config overrides by type should work
+ String json = aposToQuotes("{'name':null}");
+ NullsForString def = MAPPER.readValue(json, NullsForString.class);
+ assertNull(def.getName());
+
+ ObjectMapper mapper = newObjectMapper();
+ mapper.configOverride(String.class)
+ .setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.FAIL));
+ try {
+ mapper.readValue(json, NullsForString.class);
+ fail("Should not pass");
+ } catch (InvalidNullException e) {
+ verifyException(e, "property \"name\"");
+ }
+ }
+
+ public void testNullsToEmptyScalar() throws Exception
+ {
+ NullAsEmpty result = MAPPER.readValue(aposToQuotes("{'nullAsEmpty':'foo', 'nullsOk':null}"),
+ NullAsEmpty.class);
+ assertEquals("foo", result.nullAsEmpty);
+ assertNull(result.nullsOk);
+
+ // and then see that nulls are not ok for non-nullable
+ result = MAPPER.readValue(aposToQuotes("{'nullAsEmpty':null}"),
+ NullAsEmpty.class);
+ assertEquals("", result.nullAsEmpty);
+
+ // also: config overrides by type should work
+ String json = aposToQuotes("{'name':null}");
+ NullsForString def = MAPPER.readValue(json, NullsForString.class);
+ assertNull(def.getName());
+
+ ObjectMapper mapper = newObjectMapper();
+ mapper.configOverride(String.class)
+ .setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
+ NullsForString named = mapper.readValue(json, NullsForString.class);
+ assertEquals("", named.getName());
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsSkipTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsSkipTest.java
new file mode 100644
index 0000000..7ff98ec
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/NullConversionsSkipTest.java
@@ -0,0 +1,93 @@
+package com.fasterxml.jackson.databind.deser.filter;
+
+import com.fasterxml.jackson.annotation.JsonSetter;
+import com.fasterxml.jackson.annotation.Nulls;
+import com.fasterxml.jackson.databind.*;
+
+// for [databind#1402]; configurable null handling, specifically with SKIP
+public class NullConversionsSkipTest extends BaseMapTest
+{
+ static class NullSkipField {
+ public String nullsOk = "a";
+
+ @JsonSetter(nulls=Nulls.SKIP)
+ public String noNulls = "b";
+ }
+
+ static class NullSkipMethod {
+ String _nullsOk = "a";
+ String _noNulls = "b";
+
+ public void setNullsOk(String v) {
+ _nullsOk = v;
+ }
+
+ @JsonSetter(nulls=Nulls.SKIP)
+ public void setNoNulls(String v) {
+ _noNulls = v;
+ }
+ }
+
+ static class StringValue {
+ String value = "default";
+
+ public void setValue(String v) {
+ value = v;
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, straight annotation
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper();
+
+ public void testSkipNullField() throws Exception
+ {
+ // first, ok if assigning non-null to not-nullable, null for nullable
+ NullSkipField result = MAPPER.readValue(aposToQuotes("{'noNulls':'foo', 'nullsOk':null}"),
+ NullSkipField.class);
+ assertEquals("foo", result.noNulls);
+ assertNull(result.nullsOk);
+
+ // and then see that nulls are not ok for non-nullable
+ result = MAPPER.readValue(aposToQuotes("{'noNulls':null}"),
+ NullSkipField.class);
+ assertEquals("b", result.noNulls);
+ assertEquals("a", result.nullsOk);
+ }
+
+ public void testSkipNullMethod() throws Exception
+ {
+ NullSkipMethod result = MAPPER.readValue(aposToQuotes("{'noNulls':'foo', 'nullsOk':null}"),
+ NullSkipMethod.class);
+ assertEquals("foo", result._noNulls);
+ assertNull(result._nullsOk);
+
+ result = MAPPER.readValue(aposToQuotes("{'noNulls':null}"),
+ NullSkipMethod.class);
+ assertEquals("b", result._noNulls);
+ assertEquals("a", result._nullsOk);
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, defaulting
+ /**********************************************************
+ */
+
+ public void testSkipNullWithDefaults() throws Exception
+ {
+ String json = aposToQuotes("{'value':null}");
+ StringValue result = MAPPER.readValue(json, StringValue.class);
+ assertNull(result.value);
+
+ ObjectMapper mapper = newObjectMapper();
+ mapper.configOverride(String.class)
+ .setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.SKIP));
+ result = mapper.readValue(json, StringValue.class);
+ assertEquals("default", result.value);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandlerLocation1440Test.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandlerLocation1440Test.java
new file mode 100644
index 0000000..14464e7
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandlerLocation1440Test.java
@@ -0,0 +1,141 @@
+package com.fasterxml.jackson.databind.deser.filter;
+
+import java.io.IOException;
+import java.util.*;
+
+import com.fasterxml.jackson.annotation.*;
+
+import com.fasterxml.jackson.core.*;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
+
+// Test(s) to verify [databind#1440]
+public class ProblemHandlerLocation1440Test extends BaseMapTest
+{
+ static class DeserializationProblem {
+ public List<String> unknownProperties = new ArrayList<>();
+
+ public DeserializationProblem() { }
+
+ public void addUnknownProperty(final String prop) {
+ unknownProperties.add(prop);
+ }
+ public boolean foundProblems() {
+ return !unknownProperties.isEmpty();
+ }
+
+ @Override
+ public String toString() {
+ return "DeserializationProblem{" +"unknownProperties=" + unknownProperties +'}';
+ }
+ }
+
+ static class DeserializationProblemLogger extends DeserializationProblemHandler {
+
+ public DeserializationProblem probs = new DeserializationProblem();
+
+ public List<String> problems() {
+ return probs.unknownProperties;
+ }
+
+ @Override
+ public boolean handleUnknownProperty(final DeserializationContext ctxt, final JsonParser p,
+ JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName)
+ throws IOException
+ {
+ final JsonStreamContext parsingContext = p.getParsingContext();
+ final List<String> pathList = new ArrayList<>();
+ addParent(parsingContext, pathList);
+ Collections.reverse(pathList);
+ final String path = _join(".", pathList) + "#" + propertyName;
+
+ probs.addUnknownProperty(path);
+
+ p.skipChildren();
+ return true;
+ }
+
+ static String _join(String sep, Collection<String> parts) {
+ StringBuilder sb = new StringBuilder();
+ for (String part : parts) {
+ if (sb.length() > 0) {
+ sb.append(sep);
+ }
+ sb.append(part);
+ }
+ return sb.toString();
+ }
+
+ private void addParent(final JsonStreamContext streamContext, final List<String> pathList) {
+ if (streamContext != null && streamContext.getCurrentName() != null) {
+ pathList.add(streamContext.getCurrentName());
+ addParent(streamContext.getParent(), pathList);
+ }
+ }
+ }
+
+ @JsonInclude(JsonInclude.Include.NON_NULL)
+ static class Activity {
+ public ActivityEntity actor;
+ public String verb;
+ public ActivityEntity object;
+ public ActivityEntity target;
+
+ @JsonCreator
+ public Activity(@JsonProperty("actor") final ActivityEntity actor, @JsonProperty("object") final ActivityEntity object, @JsonProperty("target") final ActivityEntity target, @JsonProperty("verb") final String verb) {
+ this.actor = actor;
+ this.verb = verb;
+ this.object = object;
+ this.target = target;
+ }
+ }
+
+ @JsonInclude(JsonInclude.Include.NON_NULL)
+ static class ActivityEntity {
+ public String id;
+ public String type;
+ public String status;
+ public String context;
+
+ @JsonCreator
+ public ActivityEntity(@JsonProperty("id") final String id, @JsonProperty("type") final String type, @JsonProperty("status") final String status, @JsonProperty("context") final String context) {
+ this.id = id;
+ this.type = type;
+ this.status = status;
+ this.context = context;
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ public void testIncorrectContext() throws Exception
+ {
+ // need invalid to trigger problem:
+ final String invalidInput = aposToQuotes(
+"{'actor': {'id': 'actor_id','type': 'actor_type',"
++"'status': 'actor_status','context':'actor_context','invalid_1': 'actor_invalid_1'},"
++"'verb': 'verb','object': {'id': 'object_id','type': 'object_type',"
++"'invalid_2': 'object_invalid_2','status': 'object_status','context': 'object_context'},"
++"'target': {'id': 'target_id','type': 'target_type','invalid_3': 'target_invalid_3',"
++"'invalid_4': 'target_invalid_4','status': 'target_status','context': 'target_context'}}"
+);
+
+ ObjectMapper mapper = newObjectMapper();
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ final DeserializationProblemLogger logger = new DeserializationProblemLogger();
+ mapper.addHandler(logger);
+ mapper.readValue(invalidInput, Activity.class);
+
+ List<String> probs = logger.problems();
+ assertEquals(4, probs.size());
+ assertEquals("actor.invalid_1#invalid_1", probs.get(0));
+ assertEquals("object.invalid_2#invalid_2", probs.get(1));
+ assertEquals("target.invalid_3#invalid_3", probs.get(2));
+ assertEquals("target.invalid_4#invalid_4", probs.get(3));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/ProblemHandlerTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandlerTest.java
similarity index 77%
rename from src/test/java/com/fasterxml/jackson/databind/filter/ProblemHandlerTest.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandlerTest.java
index 1a9f396..0bd73f5 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/ProblemHandlerTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandlerTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.deser.filter;
import java.io.IOException;
import java.util.Map;
@@ -9,6 +9,8 @@
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
+import com.fasterxml.jackson.databind.deser.ValueInstantiator;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
@@ -96,6 +98,9 @@
Class<?> instClass, Object argument, Throwable t)
throws IOException
{
+ if (!(t instanceof InvalidDefinitionException)) {
+ throw new IllegalArgumentException("Should have gotten `InvalidDefinitionException`, instead got: "+t);
+ }
return value;
}
}
@@ -108,10 +113,10 @@
public MissingInstantiationHandler(Object v0) {
value = v0;
}
-
+
@Override
public Object handleMissingInstantiator(DeserializationContext ctxt,
- Class<?> instClass, JsonParser p, String msg)
+ Class<?> instClass, ValueInstantiator inst, JsonParser p, String msg)
throws IOException
{
p.skipChildren();
@@ -123,11 +128,11 @@
extends DeserializationProblemHandler
{
protected final Object value;
-
+
public WeirdTokenHandler(Object v) {
value = v;
}
-
+
@Override
public Object handleUnexpectedToken(DeserializationContext ctxt,
Class<?> targetType, JsonToken t, JsonParser p,
@@ -138,12 +143,12 @@
}
}
- static class TypeIdHandler
+ static class UnknownTypeIdHandler
extends DeserializationProblemHandler
{
protected final Class<?> raw;
- public TypeIdHandler(Class<?> r) { raw = r; }
+ public UnknownTypeIdHandler(Class<?> r) { raw = r; }
@Override
public JavaType handleUnknownTypeId(DeserializationContext ctxt,
@@ -155,6 +160,23 @@
}
}
+ static class MissingTypeIdHandler
+ extends DeserializationProblemHandler
+ {
+ protected final Class<?> raw;
+
+ public MissingTypeIdHandler(Class<?> r) { raw = r; }
+
+ @Override
+ public JavaType handleMissingTypeId(DeserializationContext ctxt,
+ JavaType baseType, TypeIdResolver idResolver,
+ String failureMsg)
+ throws IOException
+ {
+ return ctxt.constructType(raw);
+ }
+ }
+
/*
/**********************************************************
/* Other helper types
@@ -193,7 +215,7 @@
public final static BustedCtor INST = new BustedCtor(true);
public BustedCtor() {
- throw new RuntimeException("Fail!");
+ throw new RuntimeException("Fail! (to be caught by handler)");
}
private BustedCtor(boolean b) { }
}
@@ -210,11 +232,11 @@
/**********************************************************
*/
- private final ObjectMapper MAPPER = new ObjectMapper();
+ private final ObjectMapper MAPPER = newObjectMapper();
public void testWeirdKeyHandling() throws Exception
{
- ObjectMapper mapper = new ObjectMapper()
+ ObjectMapper mapper = newObjectMapper()
.addHandler(new WeirdKeyHandler(7));
IntKeyMapWrapper w = mapper.readValue("{\"stuff\":{\"foo\":\"abc\"}}",
IntKeyMapWrapper.class);
@@ -226,7 +248,7 @@
public void testWeirdNumberHandling() throws Exception
{
- ObjectMapper mapper = new ObjectMapper()
+ ObjectMapper mapper = newObjectMapper()
.addHandler(new WeirdNumberHandler(SingleValuedEnum.A))
;
SingleValuedEnum result = mapper.readValue("3", SingleValuedEnum.class);
@@ -235,7 +257,7 @@
public void testWeirdStringHandling() throws Exception
{
- ObjectMapper mapper = new ObjectMapper()
+ ObjectMapper mapper = newObjectMapper()
.addHandler(new WeirdStringHandler(SingleValuedEnum.A))
;
SingleValuedEnum result = mapper.readValue("\"B\"", SingleValuedEnum.class);
@@ -250,25 +272,46 @@
public void testInvalidTypeId() throws Exception
{
- ObjectMapper mapper = new ObjectMapper()
- .addHandler(new TypeIdHandler(BaseImpl.class));
+ ObjectMapper mapper = newObjectMapper()
+ .addHandler(new UnknownTypeIdHandler(BaseImpl.class));
BaseWrapper w = mapper.readValue("{\"value\":{\"type\":\"foo\",\"a\":4}}",
BaseWrapper.class);
assertNotNull(w);
assertEquals(BaseImpl.class, w.value.getClass());
}
-
public void testInvalidClassAsId() throws Exception
{
- ObjectMapper mapper = new ObjectMapper()
- .addHandler(new TypeIdHandler(Base2Impl.class));
+ ObjectMapper mapper = newObjectMapper()
+ .addHandler(new UnknownTypeIdHandler(Base2Impl.class));
Base2Wrapper w = mapper.readValue("{\"value\":{\"clazz\":\"com.fizz\",\"a\":4}}",
Base2Wrapper.class);
assertNotNull(w);
assertEquals(Base2Impl.class, w.value.getClass());
}
+ // 2.9: missing type id, distinct from unknown
+
+ public void testMissingTypeId() throws Exception
+ {
+ ObjectMapper mapper = newObjectMapper()
+ .addHandler(new MissingTypeIdHandler(BaseImpl.class));
+ BaseWrapper w = mapper.readValue("{\"value\":{\"a\":4}}",
+ BaseWrapper.class);
+ assertNotNull(w);
+ assertEquals(BaseImpl.class, w.value.getClass());
+ }
+
+ public void testMissingClassAsId() throws Exception
+ {
+ ObjectMapper mapper = newObjectMapper()
+ .addHandler(new MissingTypeIdHandler(Base2Impl.class));
+ Base2Wrapper w = mapper.readValue("{\"value\":{\"a\":4}}",
+ Base2Wrapper.class);
+ assertNotNull(w);
+ assertEquals(Base2Impl.class, w.value.getClass());
+ }
+
// verify that by default we get special exception type
public void testInvalidTypeIdFail() throws Exception
{
@@ -285,7 +328,7 @@
public void testInstantiationExceptionHandling() throws Exception
{
- ObjectMapper mapper = new ObjectMapper()
+ ObjectMapper mapper = newObjectMapper()
.addHandler(new InstantiationProblemHandler(BustedCtor.INST));
BustedCtor w = mapper.readValue("{ }",
BustedCtor.class);
@@ -294,7 +337,7 @@
public void testMissingInstantiatorHandling() throws Exception
{
- ObjectMapper mapper = new ObjectMapper()
+ ObjectMapper mapper = newObjectMapper()
.addHandler(new MissingInstantiationHandler(new NoDefaultCtor(13)))
;
NoDefaultCtor w = mapper.readValue("{ \"x\" : true }", NoDefaultCtor.class);
@@ -304,7 +347,7 @@
public void testUnexpectedTokenHandling() throws Exception
{
- ObjectMapper mapper = new ObjectMapper()
+ ObjectMapper mapper = newObjectMapper()
.addHandler(new WeirdTokenHandler(Integer.valueOf(13)))
;
Integer v = mapper.readValue("true", Integer.class);
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/filter/RecursiveIgnorePropertiesTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/RecursiveIgnorePropertiesTest.java
new file mode 100644
index 0000000..1ffd7f8
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/RecursiveIgnorePropertiesTest.java
@@ -0,0 +1,44 @@
+package com.fasterxml.jackson.databind.deser.filter;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import com.fasterxml.jackson.databind.*;
+
+public class RecursiveIgnorePropertiesTest extends BaseMapTest
+{
+ static class Person {
+ public String name;
+
+ @JsonProperty("person_z") // renaming this to person_p works
+ @JsonIgnoreProperties({"person_z"}) // renaming this to person_p works
+// public Set<Person> personZ;
+ public Person personZ;
+ }
+
+ public void testRecursiveForDeser() throws Exception
+ {
+ String st = aposToQuotes("{ 'name': 'admin',\n"
+// + " 'person_z': [ { 'name': 'admin' } ]"
+ + " 'person_z': { 'name': 'admin' }"
+ + "}");
+
+ ObjectMapper mapper = newObjectMapper();
+ Person result = mapper.readValue(st, Person.class);
+ assertEquals("admin", result.name);
+ }
+
+ public void testRecursiveForSer() throws Exception
+ {
+ ObjectMapper mapper = newObjectMapper();
+ Person input = new Person();
+ input.name = "Bob";
+ Person p2 = new Person();
+ p2.name = "Bill";
+ input.personZ = p2;
+ p2.personZ = input;
+
+ String json = mapper.writeValueAsString(input);
+ assertNotNull(json);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/TestUnknownPropertyDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/TestUnknownPropertyDeserialization.java
similarity index 96%
rename from src/test/java/com/fasterxml/jackson/databind/filter/TestUnknownPropertyDeserialization.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/filter/TestUnknownPropertyDeserialization.java
index b362822..dacf423 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/TestUnknownPropertyDeserialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/TestUnknownPropertyDeserialization.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.deser.filter;
import java.io.*;
import java.util.*;
@@ -125,7 +125,7 @@
/**********************************************************
*/
- private final ObjectMapper MAPPER = new ObjectMapper();
+ private final ObjectMapper MAPPER = newObjectMapper();
/**
* By default we should just get an exception if an unknown property
@@ -146,7 +146,7 @@
*/
public void testUnknownHandlingIgnoreWithHandler() throws Exception
{
- ObjectMapper mapper = new ObjectMapper();
+ ObjectMapper mapper = newObjectMapper();
mapper.clearProblemHandlers();
mapper.addHandler(new MyHandler());
TestBean result = mapper.readValue(new StringReader(JSON_UNKNOWN_FIELD), TestBean.class);
@@ -162,7 +162,7 @@
*/
public void testUnknownHandlingIgnoreWithHandlerAndObjectReader() throws Exception
{
- ObjectMapper mapper = new ObjectMapper();
+ ObjectMapper mapper = newObjectMapper();
mapper.clearProblemHandlers();
TestBean result = mapper.readerFor(TestBean.class).withHandler(new MyHandler()).readValue(new StringReader(JSON_UNKNOWN_FIELD));
assertNotNull(result);
@@ -177,7 +177,7 @@
*/
public void testUnknownHandlingIgnoreWithFeature() throws Exception
{
- ObjectMapper mapper = new ObjectMapper();
+ ObjectMapper mapper = newObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
TestBean result = null;
try {
@@ -271,7 +271,7 @@
public void testIssue987() throws Exception
{
- ObjectMapper jsonMapper = new ObjectMapper();
+ ObjectMapper jsonMapper = newObjectMapper();
jsonMapper.addHandler(new DeserializationProblemHandler() {
@Override
public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException, JsonProcessingException {
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/inject/InvalidInjectionTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/inject/InvalidInjectionTest.java
new file mode 100644
index 0000000..a6e2543
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/inject/InvalidInjectionTest.java
@@ -0,0 +1,45 @@
+package com.fasterxml.jackson.databind.deser.inject;
+
+import com.fasterxml.jackson.annotation.JacksonInject;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
+
+public class InvalidInjectionTest extends BaseMapTest
+{
+ static class BadBean1 {
+ @JacksonInject protected String prop1;
+ @JacksonInject protected String prop2;
+ }
+
+ static class BadBean2 {
+ @JacksonInject("x") protected String prop1;
+ @JacksonInject("x") protected String prop2;
+ }
+
+ /*
+ /**********************************************************
+ /* Unit tests
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper();
+
+ public void testInvalidDup() throws Exception
+ {
+ try {
+ MAPPER.readValue("{}", BadBean1.class);
+ fail("Should not pass");
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "Duplicate injectable value");
+ }
+ try {
+ MAPPER.readValue("{}", BadBean2.class);
+ fail("Should not pass");
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "Duplicate injectable value");
+ }
+ }
+
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestInjectables.java b/src/test/java/com/fasterxml/jackson/databind/deser/inject/TestInjectables.java
similarity index 85%
rename from src/test/java/com/fasterxml/jackson/databind/deser/TestInjectables.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/inject/TestInjectables.java
index 2a1c3ef..f22367c 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestInjectables.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/inject/TestInjectables.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser;
+package com.fasterxml.jackson.databind.deser.inject;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
@@ -21,16 +21,6 @@
public void injectThird(long v) {
third = v;
}
- }
-
- static class BadBean1 {
- @JacksonInject protected String prop1;
- @JacksonInject protected String prop2;
- }
-
- static class BadBean2 {
- @JacksonInject("x") protected String prop1;
- @JacksonInject("x") protected String prop2;
}
static class CtorBean {
@@ -93,18 +83,18 @@
public int value;
}
-
+
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
- private final ObjectMapper MAPPER = new ObjectMapper();
+ private final ObjectMapper MAPPER = newObjectMapper();
public void testSimple() throws Exception
{
- ObjectMapper mapper = new ObjectMapper();
+ ObjectMapper mapper = newObjectMapper();
mapper.setInjectableValues(new InjectableValues.Std()
.addValue(String.class, "stuffValue")
.addValue("myId", "xyz")
@@ -127,7 +117,6 @@
assertEquals("Bubba", bean.name);
}
- // [Issue-13]
public void testTwoInjectablesViaCreator() throws Exception
{
CtorBean2 bean = MAPPER.readerFor(CtorBean2.class)
@@ -139,19 +128,6 @@
assertEquals("Bob", bean.name);
}
- public void testInvalidDup() throws Exception
- {
- try {
- MAPPER.readValue("{}", BadBean1.class);
- } catch (Exception e) {
- verifyException(e, "Duplicate injectable value");
- }
- try {
- MAPPER.readValue("{}", BadBean2.class);
- } catch (Exception e) {
- verifyException(e, "Duplicate injectable value");
- }
- }
public void testIssueGH471() throws Exception
{
@@ -159,7 +135,7 @@
final Object methodInjected = "methodInjected";
final Object fieldInjected = "fieldInjected";
- ObjectMapper mapper = new ObjectMapper()
+ ObjectMapper mapper = newObjectMapper()
.setInjectableValues(new InjectableValues.Std()
.addValue("constructor_injected", constructorInjected)
.addValue("method_injected", methodInjected)
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/Base64DecodingTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/Base64DecodingTest.java
new file mode 100644
index 0000000..41c80fa
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/Base64DecodingTest.java
@@ -0,0 +1,50 @@
+package com.fasterxml.jackson.databind.deser.jdk;
+
+import java.nio.charset.StandardCharsets;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+
+// Mostly for [databind#1425]; not in optimal place (as it also has
+// tree-access tests), but has to do for now
+public class Base64DecodingTest extends BaseMapTest
+{
+ private final ObjectMapper MAPPER = objectMapper();
+
+ private final byte[] HELLO_BYTES = "hello".getBytes(StandardCharsets.UTF_8);
+ private final String BASE64_HELLO = "aGVsbG8=";
+
+ // for [databind#1425]
+ public void testInvalidBase64() throws Exception
+ {
+ byte[] b = MAPPER.readValue(quote(BASE64_HELLO), byte[].class);
+ assertEquals(HELLO_BYTES, b);
+
+ _testInvalidBase64(MAPPER, BASE64_HELLO+"!");
+ _testInvalidBase64(MAPPER, BASE64_HELLO+"!!");
+ }
+
+ private void _testInvalidBase64(ObjectMapper mapper, String value) throws Exception
+ {
+ // First, use data-binding
+ try {
+ MAPPER.readValue(quote(value), byte[].class);
+ fail("Should not pass");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Failed to decode");
+ verifyException(e, "as base64");
+ verifyException(e, "Illegal character '!'");
+ }
+
+ // and then tree model
+ JsonNode tree = mapper.readTree(String.format("{\"foo\":\"%s\"}", value));
+ JsonNode nodeValue = tree.get("foo");
+ try {
+ /*byte[] b =*/ nodeValue.binaryValue();
+ fail("Should not pass");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Can not access contents of TextNode as binary");
+ verifyException(e, "Illegal character '!'");
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/DateAdjustment204Test.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/DateAdjustment204Test.java
new file mode 100644
index 0000000..c112d94
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/DateAdjustment204Test.java
@@ -0,0 +1,57 @@
+package com.fasterxml.jackson.databind.deser.jdk;
+
+import java.util.Calendar;
+import java.util.TimeZone;
+
+import com.fasterxml.jackson.databind.*;
+
+public class DateAdjustment204Test extends BaseMapTest
+{
+ /*
+ /**********************************************************
+ /* Unit tests
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ // for [databind#204]
+ public void testContextTimezone() throws Exception
+ {
+ String inputStr = "1997-07-16T19:20:30.45+0100";
+
+ // this is enabled by default:
+ assertTrue(MAPPER.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE));
+ final ObjectReader r = MAPPER
+ .readerFor(Calendar.class)
+ .with(TimeZone.getTimeZone("PST"));
+
+ // by default use contextual timezone:
+ Calendar cal = r.readValue(quote(inputStr));
+ TimeZone tz = cal.getTimeZone();
+ assertEquals("PST", tz.getID());
+
+ assertEquals(1997, cal.get(Calendar.YEAR));
+ assertEquals(Calendar.JULY, cal.get(Calendar.MONTH));
+ assertEquals(16, cal.get(Calendar.DAY_OF_MONTH));
+
+ // Translated from original into PST differs:
+ assertEquals(11, cal.get(Calendar.HOUR_OF_DAY));
+ assertEquals(20, cal.get(Calendar.MINUTE));
+ assertEquals(30, cal.get(Calendar.SECOND));
+
+ // but if disabled, should use what's been sent in:
+ cal = r.without(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
+ .readValue(quote(inputStr));
+
+
+ // !!! TODO: would not yet pass
+/*
+ System.err.println("CAL/2 == "+cal);
+
+ System.err.println("tz == "+cal.getTimeZone());
+ */
+
+ }
+
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestDateDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/DateDeserializationTest.java
similarity index 84%
rename from src/test/java/com/fasterxml/jackson/databind/deser/TestDateDeserialization.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/jdk/DateDeserializationTest.java
index 634cf00..4debf0d 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestDateDeserialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/DateDeserializationTest.java
@@ -1,18 +1,22 @@
-package com.fasterxml.jackson.databind.deser;
+package com.fasterxml.jackson.databind.deser.jdk;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
-import java.util.*;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.OptBoolean;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
-public class TestDateDeserialization
+public class DateDeserializationTest
extends BaseMapTest
{
- // Test for [JACKSON-435]
static class DateAsStringBean
{
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="/yyyy/MM/dd/")
@@ -35,7 +39,22 @@
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd,HH", timezone="CET")
public Date date;
}
+
+ static class CalendarBean {
+ Calendar _v;
+ void setV(Calendar v) { _v = v; }
+ }
+
+ static class LenientCalendarBean {
+ @JsonFormat(lenient=OptBoolean.TRUE)
+ public Calendar value;
+ }
+ static class StrictCalendarBean {
+ @JsonFormat(lenient=OptBoolean.FALSE)
+ public Calendar value;
+ }
+
/*
/**********************************************************
/* Unit tests
@@ -63,7 +82,7 @@
public void testDateUtilWithStringTimestamp() throws Exception
{
long now = 1321992375446L;
- /* As of 1.5.0, should be ok to pass as JSON String, as long
+ /* Should be ok to pass as JSON String, as long
* as it is plain timestamp (all numbers, 64-bit)
*/
String json = quote(String.valueOf(now));
@@ -263,7 +282,7 @@
assertEquals(0, c.get(Calendar.MILLISECOND));
}
- // [Issue#338]
+ // [databind#338]
public void testDateUtilISO8601NoMilliseconds() throws Exception
{
final String INPUT_STR = "2013-10-31T17:27:00";
@@ -284,7 +303,7 @@
// 03-Nov-2013, tatu: This wouldn't work, and is the nominal reason
// for #338 I think
/*
- inputDate = ISO8601Utils.parse(INPUT_STR);
+ inputDate = ISO8601Utils.parse(INPUT_STR, new ParsePosition(0));
c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
c.setTime(inputDate);
assertEquals(2013, c.get(Calendar.YEAR));
@@ -390,7 +409,6 @@
assertNull(MAPPER.readValue(quote(""), java.sql.Date.class));
}
- // for [JACKSON-334]
public void test8601DateTimeNoMilliSecs() throws Exception
{
// ok, Zebra, no milliseconds
@@ -506,6 +524,80 @@
/*
/**********************************************************
+ /* Test(s) for array unwrapping
+ /**********************************************************
+ */
+
+ public void testCalendarArrayUnwrap() throws Exception
+ {
+ ObjectReader reader = new ObjectMapper()
+ .readerFor(CalendarBean.class)
+ .without(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+ final String inputDate = "1972-12-28T00:00:00.000+0000";
+ final String input = aposToQuotes("{'v':['"+inputDate+"']}");
+ try {
+ reader.readValue(input);
+ fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled");
+ } catch (MismatchedInputException exp) {
+ verifyException(exp, "Can not deserialize");
+ verifyException(exp, "out of START_ARRAY");
+ }
+
+ reader = reader.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+ CalendarBean bean = reader.readValue(input);
+ assertNotNull(bean._v);
+ assertEquals(1972, bean._v.get(Calendar.YEAR));
+
+ // and finally, a fail due to multiple values:
+ try {
+ reader.readValue(aposToQuotes("{'v':['"+inputDate+"','"+inputDate+"']}"));
+ fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled");
+ } catch (JsonMappingException exp) {
+ verifyException(exp, "Attempted to unwrap");
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Tests for leniency
+ /**********************************************************
+ */
+
+ public void testLenientCalendar() throws Exception
+ {
+ final String JSON = aposToQuotes("{'value':'2015-11-32'}");
+
+ // with lenient, can parse fine
+ LenientCalendarBean lenBean = MAPPER.readValue(JSON, LenientCalendarBean.class);
+ assertEquals(Calendar.DECEMBER, lenBean.value.get(Calendar.MONTH));
+ assertEquals(2, lenBean.value.get(Calendar.DAY_OF_MONTH));
+
+ // with strict, ought to produce exception
+ try {
+ MAPPER.readValue(JSON, StrictCalendarBean.class);
+ fail("Should not pass with invalid (with strict) date value");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Can not deserialize value of type java.util.Calendar");
+ verifyException(e, "from String \"2015-11-32\"");
+ verifyException(e, "expected format");
+ }
+
+ // similarly with Date...
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configOverride(java.util.Date.class)
+ .setFormat(JsonFormat.Value.forLeniency(Boolean.FALSE));
+ try {
+ mapper.readValue(quote("2015-11-32"), java.util.Date.class);
+ fail("Should not pass with invalid (with strict) date value");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Can not deserialize value of type java.util.Date");
+ verifyException(e, "from String \"2015-11-32\"");
+ verifyException(e, "expected format");
+ }
+ }
+
+ /*
+ /**********************************************************
/* Tests to verify failing cases
/**********************************************************
*/
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumAltIdTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumAltIdTest.java
new file mode 100644
index 0000000..76f1882
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumAltIdTest.java
@@ -0,0 +1,132 @@
+package com.fasterxml.jackson.databind.deser.jdk;
+
+import java.io.IOException;
+import java.util.EnumSet;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.MapperFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectReader;
+import com.fasterxml.jackson.databind.exc.InvalidFormatException;
+
+public class EnumAltIdTest extends BaseMapTest
+{
+ // [databind#1313]
+
+ enum TestEnum { JACKSON, RULES, OK; }
+ protected enum LowerCaseEnum {
+ A, B, C;
+ private LowerCaseEnum() { }
+ @Override
+ public String toString() { return name().toLowerCase(); }
+ }
+
+ protected static class EnumBean {
+ @JsonFormat(with={ JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES })
+ public TestEnum value;
+ }
+
+ protected static class StrictCaseBean {
+ @JsonFormat(without={ JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES })
+ public TestEnum value;
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, basic
+ /**********************************************************
+ */
+
+ protected final ObjectMapper MAPPER = new ObjectMapper();
+ protected final ObjectMapper MAPPER_IGNORE_CASE;
+ {
+ MAPPER_IGNORE_CASE = new ObjectMapper();
+ MAPPER_IGNORE_CASE.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
+ }
+
+ protected final ObjectReader READER_DEFAULT = MAPPER.reader();
+ protected final ObjectReader READER_IGNORE_CASE = MAPPER_IGNORE_CASE.reader();
+
+ // Tests for [databind#1313], case-insensitive
+
+ public void testFailWhenCaseSensitiveAndNameIsNotUpperCase() throws IOException {
+ try {
+ READER_DEFAULT.forType(TestEnum.class).readValue("\"Jackson\"");
+ fail("InvalidFormatException expected");
+ } catch (InvalidFormatException e) {
+ verifyException(e, "value not one of declared Enum instance names: [JACKSON, OK, RULES]");
+ }
+ }
+
+ public void testFailWhenCaseSensitiveAndToStringIsUpperCase() throws IOException {
+ ObjectReader r = READER_DEFAULT.forType(LowerCaseEnum.class)
+ .with(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
+ try {
+ r.readValue("\"A\"");
+ fail("InvalidFormatException expected");
+ } catch (InvalidFormatException e) {
+ verifyException(e, "value not one of declared Enum instance names: [a, b, c]");
+ }
+ }
+
+ public void testEnumDesIgnoringCaseWithLowerCaseContent() throws IOException {
+ assertEquals(TestEnum.JACKSON,
+ READER_IGNORE_CASE.forType(TestEnum.class).readValue(quote("jackson")));
+ }
+
+ public void testEnumDesIgnoringCaseWithUpperCaseToString() throws IOException {
+ ObjectReader r = MAPPER_IGNORE_CASE.readerFor(LowerCaseEnum.class)
+ .with(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
+ assertEquals(LowerCaseEnum.A, r.readValue("\"A\""));
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, containers
+ /**********************************************************
+ */
+
+ public void testIgnoreCaseInEnumList() throws Exception {
+ TestEnum[] enums = READER_IGNORE_CASE.forType(TestEnum[].class)
+ .readValue("[\"jacksON\", \"ruLes\"]");
+
+ assertEquals(2, enums.length);
+ assertEquals(TestEnum.JACKSON, enums[0]);
+ assertEquals(TestEnum.RULES, enums[1]);
+ }
+
+ public void testIgnoreCaseInEnumSet() throws IOException {
+ ObjectReader r = READER_IGNORE_CASE.forType(new TypeReference<EnumSet<TestEnum>>() { });
+ EnumSet<TestEnum> set = r.readValue("[\"jackson\"]");
+ assertEquals(1, set.size());
+ assertTrue(set.contains(TestEnum.JACKSON));
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, property overrides
+ /**********************************************************
+ */
+
+ public void testIgnoreCaseViaFormat() throws Exception
+ {
+ final String JSON = aposToQuotes("{'value':'ok'}");
+
+ // should be able to allow on per-case basis:
+ EnumBean pojo = READER_DEFAULT.forType(EnumBean.class)
+ .readValue(JSON);
+ assertEquals(TestEnum.OK, pojo.value);
+
+ // including disabling acceptance
+ try {
+ READER_DEFAULT.forType(StrictCaseBean.class)
+ .readValue(JSON);
+ fail("Should not pass");
+ } catch (InvalidFormatException e) {
+ verifyException(e, "value not one of declared Enum instance names: [JACKSON, OK, RULES]");
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/EnumDefaultReadTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDefaultReadTest.java
similarity index 99%
rename from src/test/java/com/fasterxml/jackson/databind/deser/EnumDefaultReadTest.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDefaultReadTest.java
index f1ef94f..38712fb 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/EnumDefaultReadTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDefaultReadTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser;
+package com.fasterxml.jackson.databind.deser.jdk;
import java.io.IOException;
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/EnumDeserializationTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDeserializationTest.java
similarity index 93%
rename from src/test/java/com/fasterxml/jackson/databind/deser/EnumDeserializationTest.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDeserializationTest.java
index a1b6782..2a0bc19 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/EnumDeserializationTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDeserializationTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser;
+package com.fasterxml.jackson.databind.deser.jdk;
import java.io.IOException;
import java.util.*;
@@ -46,7 +46,7 @@
return TestEnum.valueOf(jp.getText().toUpperCase());
}
}
-
+
protected enum LowerCaseEnum {
A, B, C;
private LowerCaseEnum() { }
@@ -170,17 +170,6 @@
}
}
- // [databind#1626]
- enum NumberEnum {
- @JsonProperty("2")
- EN2,
- @JsonProperty("0")
- EN0,
- @JsonProperty("1")
- EN1
- ;
- }
-
/*
/**********************************************************
/* Test methods
@@ -244,13 +233,6 @@
assertEquals(AnnotatedTestEnum.OK, e);
}
- public void testEnumMaps() throws Exception
- {
- EnumMap<TestEnum,String> value = MAPPER.readValue("{\"OK\":\"value\"}",
- new TypeReference<EnumMap<TestEnum,String>>() { });
- assertEquals("value", value.get(TestEnum.OK));
- }
-
public void testSubclassedEnums() throws Exception
{
EnumWithSubClass value = MAPPER.readValue("\"A\"", EnumWithSubClass.class);
@@ -266,16 +248,6 @@
assertEquals(LowerCaseEnum.C, value);
}
- public void testToStringEnumMaps() throws Exception
- {
- // can't reuse global one due to reconfig
- ObjectMapper m = new ObjectMapper();
- m.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
- EnumMap<LowerCaseEnum,String> value = m.readValue("{\"a\":\"value\"}",
- new TypeReference<EnumMap<LowerCaseEnum,String>>() { });
- assertEquals("value", value.get(LowerCaseEnum.A));
- }
-
public void testNumbersToEnums() throws Exception
{
// by default numbers are fine:
@@ -521,12 +493,4 @@
assertTrue(e.getMessage().contains("Undefined AnEnum"));
}
}
-
- // [databind#1626]
- public void testNumericEnumName() throws Exception
- {
- String json = MAPPER.writeValueAsString(NumberEnum.EN2);
- assertEquals(quote("2"), json);
- assertEquals(NumberEnum.EN2, MAPPER.readValue(json, NumberEnum.class));
- }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumMapDeserializationTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumMapDeserializationTest.java
new file mode 100644
index 0000000..94f8bb4
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumMapDeserializationTest.java
@@ -0,0 +1,118 @@
+package com.fasterxml.jackson.databind.deser.jdk;
+
+import java.util.EnumMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.*;
+
+@SuppressWarnings("serial")
+public class EnumMapDeserializationTest extends BaseMapTest
+{
+ enum TestEnum { JACKSON, RULES, OK; }
+
+ protected enum LowerCaseEnum {
+ A, B, C;
+ private LowerCaseEnum() { }
+ @Override
+ public String toString() { return name().toLowerCase(); }
+ }
+
+ static class MySimpleEnumMap extends EnumMap<TestEnum,String> {
+ public MySimpleEnumMap() {
+ super(TestEnum.class);
+ }
+ }
+
+ static class FromStringEnumMap extends EnumMap<TestEnum,String> {
+ @JsonCreator
+ public FromStringEnumMap(String value) {
+ super(TestEnum.class);
+ put(TestEnum.JACKSON, value);
+ }
+ }
+
+ static class FromDelegateEnumMap extends EnumMap<TestEnum,String> {
+ @JsonCreator
+ public FromDelegateEnumMap(Map<Object,Object> stuff) {
+ super(TestEnum.class);
+ put(TestEnum.OK, String.valueOf(stuff));
+ }
+ }
+
+ static class FromPropertiesEnumMap extends EnumMap<TestEnum,String> {
+ int a0, b0;
+
+ @JsonCreator
+ public FromPropertiesEnumMap(@JsonProperty("a") int a,
+ @JsonProperty("b") int b) {
+ super(TestEnum.class);
+ a0 = a;
+ b0 = b;
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ protected final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testEnumMaps() throws Exception
+ {
+ EnumMap<TestEnum,String> value = MAPPER.readValue("{\"OK\":\"value\"}",
+ new TypeReference<EnumMap<TestEnum,String>>() { });
+ assertEquals("value", value.get(TestEnum.OK));
+ }
+
+ public void testToStringEnumMaps() throws Exception
+ {
+ // can't reuse global one due to reconfig
+ ObjectReader r = MAPPER.reader()
+ .with(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
+ EnumMap<LowerCaseEnum,String> value = r.forType(
+ new TypeReference<EnumMap<LowerCaseEnum,String>>() { })
+ .readValue("{\"a\":\"value\"}");
+ assertEquals("value", value.get(LowerCaseEnum.A));
+ }
+
+ public void testCustomEnumMapWithDefaultCtor() throws Exception
+ {
+ MySimpleEnumMap map = MAPPER.readValue(aposToQuotes("{'RULES':'waves'}"),
+ MySimpleEnumMap.class);
+ assertEquals(1, map.size());
+ assertEquals("waves", map.get(TestEnum.RULES));
+ }
+
+ public void testCustomEnumMapFromString() throws Exception
+ {
+ FromStringEnumMap map = MAPPER.readValue(quote("kewl"), FromStringEnumMap.class);
+ assertEquals(1, map.size());
+ assertEquals("kewl", map.get(TestEnum.JACKSON));
+ }
+
+ public void testCustomEnumMapWithDelegate() throws Exception
+ {
+ FromDelegateEnumMap map = MAPPER.readValue(aposToQuotes("{'foo':'bar'}"), FromDelegateEnumMap.class);
+ assertEquals(1, map.size());
+ assertEquals("{foo=bar}", map.get(TestEnum.OK));
+ }
+
+ public void testCustomEnumMapFromProps() throws Exception
+ {
+ FromPropertiesEnumMap map = MAPPER.readValue(aposToQuotes(
+ "{'a':13,'RULES':'jackson','b':-731,'OK':'yes'}"),
+ FromPropertiesEnumMap.class);
+
+ assertEquals(13, map.a0);
+ assertEquals(-731, map.b0);
+
+ assertEquals("jackson", map.get(TestEnum.RULES));
+ assertEquals("yes", map.get(TestEnum.OK));
+ assertEquals(2, map.size());
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/JDKAtomicTypesTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKAtomicTypesTest.java
similarity index 97%
rename from src/test/java/com/fasterxml/jackson/databind/deser/JDKAtomicTypesTest.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKAtomicTypesTest.java
index a3236cf..1d12125 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/JDKAtomicTypesTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKAtomicTypesTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser;
+package com.fasterxml.jackson.databind.deser.jdk;
import java.io.Serializable;
import java.math.BigDecimal;
@@ -40,7 +40,6 @@
static class SimpleWrapper {
public AtomicReference<Object> value;
- public SimpleWrapper() { }
public SimpleWrapper(Object o) { value = new AtomicReference<Object>(o); }
}
@@ -150,7 +149,7 @@
JsonInclude.Value incl =
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.ALWAYS);
ObjectMapper mapper = new ObjectMapper();
- mapper.setPropertyInclusion(incl);
+ mapper.setDefaultPropertyInclusion(incl);
assertEquals(aposToQuotes("{'value':true}"),
mapper.writeValueAsString(new SimpleWrapper(Boolean.TRUE)));
}
@@ -160,7 +159,7 @@
JsonInclude.Value incl =
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.NON_NULL);
ObjectMapper mapper = new ObjectMapper();
- mapper.setPropertyInclusion(incl);
+ mapper.setDefaultPropertyInclusion(incl);
assertEquals(aposToQuotes("{'value':true}"),
mapper.writeValueAsString(new SimpleWrapper(Boolean.TRUE)));
}
@@ -170,7 +169,7 @@
JsonInclude.Value incl =
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.NON_ABSENT);
ObjectMapper mapper = new ObjectMapper();
- mapper.setPropertyInclusion(incl);
+ mapper.setDefaultPropertyInclusion(incl);
assertEquals(aposToQuotes("{'value':true}"),
mapper.writeValueAsString(new SimpleWrapper(Boolean.TRUE)));
}
@@ -180,7 +179,7 @@
JsonInclude.Value incl =
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.NON_EMPTY);
ObjectMapper mapper = new ObjectMapper();
- mapper.setPropertyInclusion(incl);
+ mapper.setDefaultPropertyInclusion(incl);
assertEquals(aposToQuotes("{'value':true}"),
mapper.writeValueAsString(new SimpleWrapper(Boolean.TRUE)));
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/JDKNumberDeserTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKNumberDeserTest.java
similarity index 77%
rename from src/test/java/com/fasterxml/jackson/databind/deser/JDKNumberDeserTest.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKNumberDeserTest.java
index c224f88..6911a26 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/JDKNumberDeserTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKNumberDeserTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser;
+package com.fasterxml.jackson.databind.deser.jdk;
import java.io.IOException;
import java.io.StringReader;
@@ -10,6 +10,7 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
public class JDKNumberDeserTest extends BaseMapTest
{
@@ -81,16 +82,65 @@
assertEquals(Double.valueOf(Double.NEGATIVE_INFINITY), result);
}
+ // 01-Mar-2017, tatu: This is bit tricky... in some ways, mapping to "empty value"
+ // would be best; but due to legacy reasons becomes `null` at this point
public void testEmptyAsNumber() throws Exception
{
+ assertNull(MAPPER.readValue(quote(""), Byte.class));
+ assertNull(MAPPER.readValue(quote(""), Short.class));
+ assertNull(MAPPER.readValue(quote(""), Character.class));
assertNull(MAPPER.readValue(quote(""), Integer.class));
assertNull(MAPPER.readValue(quote(""), Long.class));
assertNull(MAPPER.readValue(quote(""), Float.class));
assertNull(MAPPER.readValue(quote(""), Double.class));
+
assertNull(MAPPER.readValue(quote(""), BigInteger.class));
assertNull(MAPPER.readValue(quote(""), BigDecimal.class));
}
+ public void testTextualNullAsNumber() throws Exception
+ {
+ final String NULL_JSON = quote("null");
+ assertNull(MAPPER.readValue(NULL_JSON, Byte.class));
+ assertNull(MAPPER.readValue(NULL_JSON, Short.class));
+ // Character is bit special, can't do:
+// assertNull(MAPPER.readValue(JSON, Character.class));
+ assertNull(MAPPER.readValue(NULL_JSON, Integer.class));
+ assertNull(MAPPER.readValue(NULL_JSON, Long.class));
+ assertNull(MAPPER.readValue(NULL_JSON, Float.class));
+ assertNull(MAPPER.readValue(NULL_JSON, Double.class));
+
+ assertEquals(Byte.valueOf((byte) 0), MAPPER.readValue(NULL_JSON, Byte.TYPE));
+ assertEquals(Short.valueOf((short) 0), MAPPER.readValue(NULL_JSON, Short.TYPE));
+ // Character is bit special, can't do:
+// assertEquals(Character.valueOf((char) 0), MAPPER.readValue(JSON, Character.TYPE));
+ assertEquals(Integer.valueOf(0), MAPPER.readValue(NULL_JSON, Integer.TYPE));
+ assertEquals(Long.valueOf(0L), MAPPER.readValue(NULL_JSON, Long.TYPE));
+ assertEquals(Float.valueOf(0f), MAPPER.readValue(NULL_JSON, Float.TYPE));
+ assertEquals(Double.valueOf(0d), MAPPER.readValue(NULL_JSON, Double.TYPE));
+
+ assertNull(MAPPER.readValue(NULL_JSON, BigInteger.class));
+ assertNull(MAPPER.readValue(NULL_JSON, BigDecimal.class));
+
+ // Also: verify failure for at least some
+ try {
+ MAPPER.readerFor(Integer.TYPE).with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
+ .readValue(NULL_JSON);
+ fail("Should not have passed");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Can not coerce String \"null\"");
+ }
+
+ ObjectMapper noCoerceMapper = new ObjectMapper();
+ noCoerceMapper.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS);
+ try {
+ noCoerceMapper.readValue(NULL_JSON, Integer.TYPE);
+ fail("Should not have passed");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Can not coerce String \"null\"");
+ }
+ }
+
public void testDeserializeDecimalHappyPath() throws Exception {
String json = "{\"defaultValue\": { \"value\": 123 } }";
MyBeanHolder result = MAPPER.readValue(json, MyBeanHolder.class);
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java
new file mode 100644
index 0000000..7aa6a79
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java
@@ -0,0 +1,849 @@
+package com.fasterxml.jackson.databind.deser.jdk;
+
+import java.io.*;
+import java.lang.reflect.Array;
+
+import org.junit.Assert;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.*;
+import com.fasterxml.jackson.databind.*;
+
+/**
+ * Unit tests for verifying handling of simple basic non-structured
+ * types; primitives (and/or their wrappers), Strings.
+ */
+public class JDKScalarsTest
+ extends BaseMapTest
+{
+ final static String NAN_STRING = "NaN";
+
+ final static class BooleanBean {
+ boolean _v;
+ void setV(boolean v) { _v = v; }
+ }
+
+ static class BooleanWrapper {
+ public Boolean wrapper;
+ public boolean primitive;
+
+ protected Boolean ctor;
+
+ @JsonCreator
+ public BooleanWrapper(@JsonProperty("ctor") Boolean foo) {
+ ctor = foo;
+ }
+ }
+
+ static class IntBean {
+ int _v;
+ void setV(int v) { _v = v; }
+ }
+
+ static class LongBean {
+ long _v;
+ void setV(long v) { _v = v; }
+ }
+
+ final static class DoubleBean {
+ double _v;
+ void setV(double v) { _v = v; }
+ }
+
+ final static class FloatBean {
+ float _v;
+ void setV(float v) { _v = v; }
+ }
+
+ final static class CharacterBean {
+ char _v;
+ void setV(char v) { _v = v; }
+ char getV() { return _v; }
+ }
+
+ final static class CharacterWrapperBean {
+ Character _v;
+ void setV(Character v) { _v = v; }
+ Character getV() { return _v; }
+ }
+
+ /**
+ * Also, let's ensure that it's ok to override methods.
+ */
+ static class IntBean2
+ extends IntBean
+ {
+ @Override
+ void setV(int v2) { super.setV(v2+1); }
+ }
+
+ static class PrimitivesBean
+ {
+ public boolean booleanValue = true;
+ public byte byteValue = 3;
+ public char charValue = 'a';
+ public short shortValue = 37;
+ public int intValue = 1;
+ public long longValue = 100L;
+ public float floatValue = 0.25f;
+ public double doubleValue = -1.0;
+ }
+
+ static class WrappersBean
+ {
+ public Boolean booleanValue;
+ public Byte byteValue;
+ public Character charValue;
+ public Short shortValue;
+ public Integer intValue;
+ public Long longValue;
+ public Float floatValue;
+ public Double doubleValue;
+ }
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ /*
+ /**********************************************************
+ /* Scalar tests for boolean
+ /**********************************************************
+ */
+
+ public void testBooleanPrimitive() throws Exception
+ {
+ // first, simple case:
+ BooleanBean result = MAPPER.readValue("{\"v\":true}", BooleanBean.class);
+ assertTrue(result._v);
+ result = MAPPER.readValue("{\"v\":null}", BooleanBean.class);
+ assertNotNull(result);
+ assertFalse(result._v);
+ result = MAPPER.readValue("{\"v\":1}", BooleanBean.class);
+ assertNotNull(result);
+ assertTrue(result._v);
+
+ // should work with arrays too..
+ boolean[] array = MAPPER.readValue("[ null, false ]", boolean[].class);
+ assertNotNull(array);
+ assertEquals(2, array.length);
+ assertFalse(array[0]);
+ assertFalse(array[1]);
+ }
+
+ /**
+ * Simple unit test to verify that we can map boolean values to
+ * java.lang.Boolean.
+ */
+ public void testBooleanWrapper() throws Exception
+ {
+ Boolean result = MAPPER.readValue("true", Boolean.class);
+ assertEquals(Boolean.TRUE, result);
+ result = MAPPER.readValue("false", Boolean.class);
+ assertEquals(Boolean.FALSE, result);
+
+ // should accept ints too, (0 == false, otherwise true)
+ result = MAPPER.readValue("0", Boolean.class);
+ assertEquals(Boolean.FALSE, result);
+ result = MAPPER.readValue("1", Boolean.class);
+ assertEquals(Boolean.TRUE, result);
+ }
+
+ // Test for verifying that Long values are coerced to boolean correctly as well
+ public void testLongToBoolean() throws Exception
+ {
+ long value = 1L + Integer.MAX_VALUE;
+ BooleanWrapper b = MAPPER.readValue("{\"primitive\" : "+value+", \"wrapper\":"+value+", \"ctor\":"+value+"}",
+ BooleanWrapper.class);
+ assertEquals(Boolean.TRUE, b.wrapper);
+ assertTrue(b.primitive);
+ assertEquals(Boolean.TRUE, b.ctor);
+
+ // but ensure we can also get `false`
+ b = MAPPER.readValue("{\"primitive\" : 0 , \"wrapper\":0, \"ctor\":0}",
+ BooleanWrapper.class);
+ assertEquals(Boolean.FALSE, b.wrapper);
+ assertFalse(b.primitive);
+ assertEquals(Boolean.FALSE, b.ctor);
+
+ boolean[] boo = MAPPER.readValue("[ 0, 15, \"\", \"false\", \"True\" ]",
+ boolean[].class);
+ assertEquals(5, boo.length);
+ assertFalse(boo[0]);
+ assertTrue(boo[1]);
+ assertFalse(boo[2]);
+ assertFalse(boo[3]);
+ assertTrue(boo[4]);
+ }
+
+ /*
+ /**********************************************************
+ /* Scalar tests for integral types
+ /**********************************************************
+ */
+
+ public void testByteWrapper() throws Exception
+ {
+ Byte result = MAPPER.readValue(" -42\t", Byte.class);
+ assertEquals(Byte.valueOf((byte)-42), result);
+
+ // Also: should be able to coerce floats, strings:
+ result = MAPPER.readValue(" \"-12\"", Byte.class);
+ assertEquals(Byte.valueOf((byte)-12), result);
+
+ result = MAPPER.readValue(" 39.07", Byte.class);
+ assertEquals(Byte.valueOf((byte)39), result);
+ }
+
+ public void testShortWrapper() throws Exception
+ {
+ Short result = MAPPER.readValue("37", Short.class);
+ assertEquals(Short.valueOf((short)37), result);
+
+ // Also: should be able to coerce floats, strings:
+ result = MAPPER.readValue(" \"-1009\"", Short.class);
+ assertEquals(Short.valueOf((short)-1009), result);
+
+ result = MAPPER.readValue("-12.9", Short.class);
+ assertEquals(Short.valueOf((short)-12), result);
+ }
+
+ public void testCharacterWrapper() throws Exception
+ {
+ // First: canonical value is 1-char string
+ Character result = MAPPER.readValue("\"a\"", Character.class);
+ assertEquals(Character.valueOf('a'), result);
+
+ // But can also pass in ascii code
+ result = MAPPER.readValue(" "+((int) 'X'), Character.class);
+ assertEquals(Character.valueOf('X'), result);
+
+ final CharacterWrapperBean wrapper = MAPPER.readValue("{\"v\":null}", CharacterWrapperBean.class);
+ assertNotNull(wrapper);
+ assertNull(wrapper.getV());
+
+ final ObjectMapper mapper = new ObjectMapper();
+ mapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
+ try {
+ mapper.readValue("{\"v\":null}", CharacterBean.class);
+ fail("Attempting to deserialize a 'null' JSON reference into a 'char' property did not throw an exception");
+ } catch (JsonMappingException e) {
+ verifyException(e, "can not map `null`");
+ //Exception thrown as required
+ }
+
+ mapper.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
+ final CharacterBean charBean = MAPPER.readValue("{\"v\":null}", CharacterBean.class);
+ assertNotNull(wrapper);
+ assertEquals('\u0000', charBean.getV());
+ }
+
+ public void testIntWrapper() throws Exception
+ {
+ Integer result = MAPPER.readValue(" -42\t", Integer.class);
+ assertEquals(Integer.valueOf(-42), result);
+
+ // Also: should be able to coerce floats, strings:
+ result = MAPPER.readValue(" \"-1200\"", Integer.class);
+ assertEquals(Integer.valueOf(-1200), result);
+
+ result = MAPPER.readValue(" 39.07", Integer.class);
+ assertEquals(Integer.valueOf(39), result);
+ }
+
+ public void testIntPrimitive() throws Exception
+ {
+ // first, simple case:
+ IntBean result = MAPPER.readValue("{\"v\":3}", IntBean.class);
+ assertEquals(3, result._v);
+
+ result = MAPPER.readValue("{\"v\":null}", IntBean.class);
+ assertNotNull(result);
+ assertEquals(0, result._v);
+
+ // should work with arrays too..
+ int[] array = MAPPER.readValue("[ null ]", int[].class);
+ assertNotNull(array);
+ assertEquals(1, array.length);
+ assertEquals(0, array[0]);
+
+ // [databind#381]
+ final ObjectMapper mapper = new ObjectMapper();
+ mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+ try {
+ mapper.readValue("{\"v\":[3]}", IntBean.class);
+ fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled");
+ } catch (JsonMappingException exp) {
+ //Correctly threw exception
+ }
+
+ mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+
+ result = mapper.readValue("{\"v\":[3]}", IntBean.class);
+ assertEquals(3, result._v);
+
+ result = mapper.readValue("[{\"v\":[3]}]", IntBean.class);
+ assertEquals(3, result._v);
+
+ try {
+ mapper.readValue("[{\"v\":[3,3]}]", IntBean.class);
+ fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled");
+ } catch (JsonMappingException exp) {
+ //threw exception as required
+ }
+
+ result = mapper.readValue("{\"v\":[null]}", IntBean.class);
+ assertNotNull(result);
+ assertEquals(0, result._v);
+
+ array = mapper.readValue("[ [ null ] ]", int[].class);
+ assertNotNull(array);
+ assertEquals(1, array.length);
+ assertEquals(0, array[0]);
+ }
+
+ public void testLongWrapper() throws Exception
+ {
+ Long result = MAPPER.readValue("12345678901", Long.class);
+ assertEquals(Long.valueOf(12345678901L), result);
+
+ // Also: should be able to coerce floats, strings:
+ result = MAPPER.readValue(" \"-9876\"", Long.class);
+ assertEquals(Long.valueOf(-9876), result);
+
+ result = MAPPER.readValue("1918.3", Long.class);
+ assertEquals(Long.valueOf(1918), result);
+ }
+
+ public void testLongPrimitive() throws Exception
+ {
+ // first, simple case:
+ LongBean result = MAPPER.readValue("{\"v\":3}", LongBean.class);
+ assertEquals(3, result._v);
+ result = MAPPER.readValue("{\"v\":null}", LongBean.class);
+ assertNotNull(result);
+ assertEquals(0, result._v);
+
+ // should work with arrays too..
+ long[] array = MAPPER.readValue("[ null ]", long[].class);
+ assertNotNull(array);
+ assertEquals(1, array.length);
+ assertEquals(0, array[0]);
+
+ // [databind#381]
+ final ObjectMapper mapper = new ObjectMapper();
+ mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+ try {
+ mapper.readValue("{\"v\":[3]}", LongBean.class);
+ fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled");
+ } catch (JsonMappingException exp) {
+ //Correctly threw exception
+ }
+
+ mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+
+ result = mapper.readValue("{\"v\":[3]}", LongBean.class);
+ assertEquals(3, result._v);
+
+ result = mapper.readValue("[{\"v\":[3]}]", LongBean.class);
+ assertEquals(3, result._v);
+
+ try {
+ mapper.readValue("[{\"v\":[3,3]}]", LongBean.class);
+ fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled");
+ } catch (JsonMappingException exp) {
+ //threw exception as required
+ }
+
+ result = mapper.readValue("{\"v\":[null]}", LongBean.class);
+ assertNotNull(result);
+ assertEquals(0, result._v);
+
+ array = mapper.readValue("[ [ null ] ]", long[].class);
+ assertNotNull(array);
+ assertEquals(1, array.length);
+ assertEquals(0, array[0]);
+ }
+
+ /**
+ * Beyond simple case, let's also ensure that method overriding works as
+ * expected.
+ */
+ public void testIntWithOverride() throws Exception
+ {
+ IntBean2 result = MAPPER.readValue("{\"v\":8}", IntBean2.class);
+ assertEquals(9, result._v);
+ }
+
+ /*
+ /**********************************************************
+ /* Scalar tests for floating point types
+ /**********************************************************
+ */
+
+ public void testDoublePrimitive() throws Exception
+ {
+ // first, simple case:
+ // bit tricky with binary fps but...
+ final double value = 0.016;
+ DoubleBean result = MAPPER.readValue("{\"v\":"+value+"}", DoubleBean.class);
+ assertEquals(value, result._v);
+ // then [JACKSON-79]:
+ result = MAPPER.readValue("{\"v\":null}", DoubleBean.class);
+ assertNotNull(result);
+ assertEquals(0.0, result._v);
+
+ // should work with arrays too..
+ double[] array = MAPPER.readValue("[ null ]", double[].class);
+ assertNotNull(array);
+ assertEquals(1, array.length);
+ assertEquals(0.0, array[0]);
+ }
+
+ /* Note: dealing with floating-point values is tricky; not sure if
+ * we can really use equality tests here... JDK does have decent
+ * conversions though, to retain accuracy and round-trippability.
+ * But still...
+ */
+ public void testFloatWrapper() throws Exception
+ {
+ // Also: should be able to coerce floats, strings:
+ String[] STRS = new String[] {
+ "1.0", "0.0", "-0.3", "0.7", "42.012", "-999.0", NAN_STRING
+ };
+
+ for (String str : STRS) {
+ Float exp = Float.valueOf(str);
+ Float result;
+
+ if (NAN_STRING != str) {
+ // First, as regular floating point value
+ result = MAPPER.readValue(str, Float.class);
+ assertEquals(exp, result);
+ }
+
+ // and then as coerced String:
+ result = MAPPER.readValue(" \""+str+"\"", Float.class);
+ assertEquals(exp, result);
+ }
+ }
+
+ public void testDoubleWrapper() throws Exception
+ {
+ // Also: should be able to coerce doubles, strings:
+ String[] STRS = new String[] {
+ "1.0", "0.0", "-0.3", "0.7", "42.012", "-999.0", NAN_STRING
+ };
+
+ for (String str : STRS) {
+ Double exp = Double.valueOf(str);
+ Double result;
+
+ // First, as regular double value
+ if (NAN_STRING != str) {
+ result = MAPPER.readValue(str, Double.class);
+ assertEquals(exp, result);
+ }
+ // and then as coerced String:
+ result = MAPPER.readValue(" \""+str+"\"", Double.class);
+ assertEquals(exp, result);
+ }
+ }
+
+ public void testDoubleAsArray() throws Exception
+ {
+ final ObjectMapper mapper = new ObjectMapper();
+ mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+ final double value = 0.016;
+ try {
+ mapper.readValue("{\"v\":[" + value + "]}", DoubleBean.class);
+ fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled");
+ } catch (JsonMappingException exp) {
+ //Correctly threw exception
+ }
+
+ mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+
+ DoubleBean result = mapper.readValue("{\"v\":[" + value + "]}",
+ DoubleBean.class);
+ assertEquals(value, result._v);
+
+ result = mapper.readValue("[{\"v\":[" + value + "]}]", DoubleBean.class);
+ assertEquals(value, result._v);
+
+ try {
+ mapper.readValue("[{\"v\":[" + value + "," + value + "]}]", DoubleBean.class);
+ fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled");
+ } catch (JsonMappingException exp) {
+ //threw exception as required
+ }
+
+ result = mapper.readValue("{\"v\":[null]}", DoubleBean.class);
+ assertNotNull(result);
+ assertEquals(0d, result._v);
+
+ double[] array = mapper.readValue("[ [ null ] ]", double[].class);
+ assertNotNull(array);
+ assertEquals(1, array.length);
+ assertEquals(0d, array[0]);
+ }
+
+ public void testDoublePrimitiveNonNumeric() throws Exception
+ {
+ // first, simple case:
+ // bit tricky with binary fps but...
+ double value = Double.POSITIVE_INFINITY;
+ DoubleBean result = MAPPER.readValue("{\"v\":\""+value+"\"}", DoubleBean.class);
+ assertEquals(value, result._v);
+
+ // should work with arrays too..
+ double[] array = MAPPER.readValue("[ \"Infinity\" ]", double[].class);
+ assertNotNull(array);
+ assertEquals(1, array.length);
+ assertEquals(Double.POSITIVE_INFINITY, array[0]);
+ }
+
+ public void testFloatPrimitiveNonNumeric() throws Exception
+ {
+ // bit tricky with binary fps but...
+ float value = Float.POSITIVE_INFINITY;
+ FloatBean result = MAPPER.readValue("{\"v\":\""+value+"\"}", FloatBean.class);
+ assertEquals(value, result._v);
+
+ // should work with arrays too..
+ float[] array = MAPPER.readValue("[ \"Infinity\" ]", float[].class);
+ assertNotNull(array);
+ assertEquals(1, array.length);
+ assertEquals(Float.POSITIVE_INFINITY, array[0]);
+ }
+
+ /*
+ /**********************************************************
+ /* Scalar tests, other
+ /**********************************************************
+ */
+
+ public void testEmptyToNullCoercionForPrimitives() throws Exception {
+ _testEmptyToNullCoercion(int.class, Integer.valueOf(0));
+ _testEmptyToNullCoercion(long.class, Long.valueOf(0));
+ _testEmptyToNullCoercion(double.class, Double.valueOf(0.0));
+ _testEmptyToNullCoercion(float.class, Float.valueOf(0.0f));
+ }
+
+ private void _testEmptyToNullCoercion(Class<?> primType, Object emptyValue) throws Exception
+ {
+ final String EMPTY = "\"\"";
+
+ // as per [databind#1095] should only allow coercion from empty String,
+ // if `null` is acceptable
+ ObjectReader intR = MAPPER.readerFor(primType);
+ assertEquals(emptyValue, intR.readValue(EMPTY));
+ try {
+ intR.with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
+ .readValue("\"\"");
+ fail("Should not have passed");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not coerce empty String");
+ }
+ }
+
+ public void testBase64Variants() throws Exception
+ {
+ final byte[] INPUT = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890X".getBytes("UTF-8");
+
+ // default encoding is "MIME, no linefeeds", so:
+ Assert.assertArrayEquals(INPUT, MAPPER.readValue(
+ quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwWA=="),
+ byte[].class));
+ ObjectReader reader = MAPPER.readerFor(byte[].class);
+ Assert.assertArrayEquals(INPUT, (byte[]) reader.with(Base64Variants.MIME_NO_LINEFEEDS).readValue(
+ quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwWA=="
+ )));
+
+ // but others should be slightly different
+ Assert.assertArrayEquals(INPUT, (byte[]) reader.with(Base64Variants.MIME).readValue(
+ quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamtsbW5vcHFyc3R1\\ndnd4eXoxMjM0NTY3ODkwWA=="
+ )));
+ Assert.assertArrayEquals(INPUT, (byte[]) reader.with(Base64Variants.MODIFIED_FOR_URL).readValue(
+ quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwWA"
+ )));
+ // PEM mandates 64 char lines:
+ Assert.assertArrayEquals(INPUT, (byte[]) reader.with(Base64Variants.PEM).readValue(
+ quote("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwYWJjZGVmZ2hpamts\\nbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkwWA=="
+ )));
+ }
+
+ /*
+ /**********************************************************
+ /* Sequence tests
+ /**********************************************************
+ */
+
+ /**
+ * Then a unit test to verify that we can conveniently bind sequence of
+ * space-separate simple values
+ */
+ public void testSequenceOfInts() throws Exception
+ {
+ final int NR_OF_INTS = 100;
+
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < NR_OF_INTS; ++i) {
+ sb.append(" ");
+ sb.append(i);
+ }
+ JsonParser jp = MAPPER.getFactory().createParser(sb.toString());
+ for (int i = 0; i < NR_OF_INTS; ++i) {
+ Integer result = MAPPER.readValue(jp, Integer.class);
+ assertEquals(Integer.valueOf(i), result);
+ }
+ jp.close();
+ }
+
+
+ /*
+ /**********************************************************
+ /* Empty String coercion, handling
+ /**********************************************************
+ */
+
+ // by default, should return nulls, n'est pas?
+ public void testEmptyStringForWrappers() throws IOException
+ {
+ WrappersBean bean;
+
+ bean = MAPPER.readValue("{\"booleanValue\":\"\"}", WrappersBean.class);
+ assertNull(bean.booleanValue);
+ bean = MAPPER.readValue("{\"byteValue\":\"\"}", WrappersBean.class);
+ assertNull(bean.byteValue);
+
+ // char/Character is different... not sure if this should work or not:
+ bean = MAPPER.readValue("{\"charValue\":\"\"}", WrappersBean.class);
+ assertNull(bean.charValue);
+
+ bean = MAPPER.readValue("{\"shortValue\":\"\"}", WrappersBean.class);
+ assertNull(bean.shortValue);
+ bean = MAPPER.readValue("{\"intValue\":\"\"}", WrappersBean.class);
+ assertNull(bean.intValue);
+ bean = MAPPER.readValue("{\"longValue\":\"\"}", WrappersBean.class);
+ assertNull(bean.longValue);
+ bean = MAPPER.readValue("{\"floatValue\":\"\"}", WrappersBean.class);
+ assertNull(bean.floatValue);
+ bean = MAPPER.readValue("{\"doubleValue\":\"\"}", WrappersBean.class);
+ assertNull(bean.doubleValue);
+ }
+
+ public void testEmptyStringForPrimitives() throws IOException
+ {
+ PrimitivesBean bean;
+ bean = MAPPER.readValue("{\"booleanValue\":\"\"}", PrimitivesBean.class);
+ assertFalse(bean.booleanValue);
+ bean = MAPPER.readValue("{\"byteValue\":\"\"}", PrimitivesBean.class);
+ assertEquals((byte) 0, bean.byteValue);
+ bean = MAPPER.readValue("{\"charValue\":\"\"}", PrimitivesBean.class);
+ assertEquals((char) 0, bean.charValue);
+ bean = MAPPER.readValue("{\"shortValue\":\"\"}", PrimitivesBean.class);
+ assertEquals((short) 0, bean.shortValue);
+ bean = MAPPER.readValue("{\"intValue\":\"\"}", PrimitivesBean.class);
+ assertEquals(0, bean.intValue);
+ bean = MAPPER.readValue("{\"longValue\":\"\"}", PrimitivesBean.class);
+ assertEquals(0L, bean.longValue);
+ bean = MAPPER.readValue("{\"floatValue\":\"\"}", PrimitivesBean.class);
+ assertEquals(0.0f, bean.floatValue);
+ bean = MAPPER.readValue("{\"doubleValue\":\"\"}", PrimitivesBean.class);
+ assertEquals(0.0, bean.doubleValue);
+ }
+
+ private void _verifyEmptyStringFailForPrimitives(String propName) throws IOException
+ {
+ final ObjectReader reader = MAPPER
+ .readerFor(PrimitivesBean.class)
+ .with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
+ try {
+ reader.readValue(aposToQuotes("{'"+propName+"':''}"));
+ fail("Expected failure for boolean + empty String");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not coerce empty String (\"\")");
+ verifyException(e, "to Null value");
+ }
+ }
+
+ // for [databind#403]
+ public void testEmptyStringFailForPrimitives() throws IOException
+ {
+ _verifyEmptyStringFailForPrimitives("booleanValue");
+ _verifyEmptyStringFailForPrimitives("byteValue");
+ _verifyEmptyStringFailForPrimitives("charValue");
+ _verifyEmptyStringFailForPrimitives("shortValue");
+ _verifyEmptyStringFailForPrimitives("intValue");
+ _verifyEmptyStringFailForPrimitives("longValue");
+ _verifyEmptyStringFailForPrimitives("floatValue");
+ _verifyEmptyStringFailForPrimitives("doubleValue");
+ }
+
+ /*
+ /**********************************************************
+ /* Null handling for scalars in POJO
+ /**********************************************************
+ */
+
+ public void testNullForPrimitives() throws IOException
+ {
+ // by default, ok to rely on defaults
+ PrimitivesBean bean = MAPPER.readValue(
+ "{\"intValue\":null, \"booleanValue\":null, \"doubleValue\":null}",
+ PrimitivesBean.class);
+ assertNotNull(bean);
+ assertEquals(0, bean.intValue);
+ assertEquals(false, bean.booleanValue);
+ assertEquals(0.0, bean.doubleValue);
+
+ bean = MAPPER.readValue("{\"byteValue\":null, \"longValue\":null, \"floatValue\":null}",
+ PrimitivesBean.class);
+ assertNotNull(bean);
+ assertEquals((byte) 0, bean.byteValue);
+ assertEquals(0L, bean.longValue);
+ assertEquals(0.0f, bean.floatValue);
+
+ // but not when enabled
+ final ObjectReader reader = MAPPER
+ .readerFor(PrimitivesBean.class)
+ .with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
+ // boolean
+ try {
+ reader.readValue("{\"booleanValue\":null}");
+ fail("Expected failure for boolean + null");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not map `null` into type boolean");
+ }
+ // byte/char/short/int/long
+ try {
+ reader.readValue("{\"byteValue\":null}");
+ fail("Expected failure for byte + null");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not map `null` into type byte");
+ }
+ try {
+ reader.readValue("{\"charValue\":null}");
+ fail("Expected failure for char + null");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not map `null` into type char");
+ }
+ try {
+ reader.readValue("{\"shortValue\":null}");
+ fail("Expected failure for short + null");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not map `null` into type short");
+ }
+ try {
+ reader.readValue("{\"intValue\":null}");
+ fail("Expected failure for int + null");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not map `null` into type int");
+ }
+ try {
+ reader.readValue("{\"longValue\":null}");
+ fail("Expected failure for long + null");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not map `null` into type long");
+ }
+
+ // float/double
+ try {
+ reader.readValue("{\"floatValue\":null}");
+ fail("Expected failure for float + null");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not map `null` into type float");
+ }
+ try {
+ reader.readValue("{\"doubleValue\":null}");
+ fail("Expected failure for double + null");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not map `null` into type double");
+ }
+ }
+
+ public void testNullForPrimitiveArrays() throws IOException
+ {
+ _testNullForPrimitiveArrays(boolean[].class, Boolean.FALSE);
+ _testNullForPrimitiveArrays(byte[].class, Byte.valueOf((byte) 0));
+ _testNullForPrimitiveArrays(char[].class, Character.valueOf((char) 0), false);
+ _testNullForPrimitiveArrays(short[].class, Short.valueOf((short)0));
+ _testNullForPrimitiveArrays(int[].class, Integer.valueOf(0));
+ _testNullForPrimitiveArrays(long[].class, Long.valueOf(0L));
+ _testNullForPrimitiveArrays(float[].class, Float.valueOf(0f));
+ _testNullForPrimitiveArrays(double[].class, Double.valueOf(0d));
+ }
+
+ private void _testNullForPrimitiveArrays(Class<?> cls, Object defValue) throws IOException {
+ _testNullForPrimitiveArrays(cls, defValue, true);
+ }
+
+ private void _testNullForPrimitiveArrays(Class<?> cls, Object defValue,
+ boolean testEmptyString) throws IOException
+ {
+ final String EMPTY_STRING_JSON = "[ \"\" ]";
+ final String JSON_WITH_NULL = "[ null ]";
+ final String SIMPLE_NAME = cls.getSimpleName();
+ final ObjectReader readerCoerceOk = MAPPER.readerFor(cls);
+ final ObjectReader readerNoCoerce = readerCoerceOk
+ .with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
+
+ Object ob = readerCoerceOk.forType(cls).readValue(JSON_WITH_NULL);
+ assertEquals(1, Array.getLength(ob));
+ assertEquals(defValue, Array.get(ob, 0));
+ try {
+ readerNoCoerce.readValue(JSON_WITH_NULL);
+ fail("Should not pass");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not coerce `null`");
+ verifyException(e, "as content of type `"+SIMPLE_NAME+"`");
+ }
+
+ if (testEmptyString) {
+ ob = readerCoerceOk.forType(cls).readValue(EMPTY_STRING_JSON);
+ assertEquals(1, Array.getLength(ob));
+ assertEquals(defValue, Array.get(ob, 0));
+
+ try {
+ readerNoCoerce.readValue(EMPTY_STRING_JSON);
+ fail("Should not pass");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not coerce empty String (\"\")");
+ verifyException(e, "as content of type `"+SIMPLE_NAME+"`");
+ }
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test for invalid String values
+ /**********************************************************
+ */
+
+ public void testInvalidStringCoercionFail() throws IOException
+ {
+ _testInvalidStringCoercionFail(boolean[].class);
+ _testInvalidStringCoercionFail(byte[].class);
+
+ // char[] is special, can not use generalized test here
+// _testInvalidStringCoercionFail(char[].class);
+ _testInvalidStringCoercionFail(short[].class);
+ _testInvalidStringCoercionFail(int[].class);
+ _testInvalidStringCoercionFail(long[].class);
+ _testInvalidStringCoercionFail(float[].class);
+ _testInvalidStringCoercionFail(double[].class);
+ }
+
+ private void _testInvalidStringCoercionFail(Class<?> cls) throws IOException
+ {
+ final String JSON = "[ \"foobar\" ]";
+ final String SIMPLE_NAME = cls.getSimpleName();
+
+ try {
+ MAPPER.readerFor(cls).readValue(JSON);
+ fail("Should not pass");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Can not deserialize value of type "+SIMPLE_NAME+" from String \"foobar\"");
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/JDKStringLikeTypesTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKStringLikeTypesTest.java
similarity index 78%
rename from src/test/java/com/fasterxml/jackson/databind/deser/JDKStringLikeTypesTest.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKStringLikeTypesTest.java
index cef00c1..7f65dd3 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/JDKStringLikeTypesTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKStringLikeTypesTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser;
+package com.fasterxml.jackson.databind.deser.jdk;
import java.io.*;
import java.net.*;
@@ -10,10 +10,11 @@
import java.util.regex.Pattern;
import com.fasterxml.jackson.annotation.*;
-import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
+
import com.fasterxml.jackson.core.Base64Variants;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
+
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
@@ -55,7 +56,6 @@
jp.skipChildren();
return new StackTraceElement("a", "b", "b", StackTraceBean.NUM);
}
-
}
/*
@@ -112,41 +112,6 @@
assertSame(String.class, result.clazz);
}
- public void testClassAsArray() throws Exception
- {
- final ObjectMapper mapper = new ObjectMapper();
- Class<?> result = mapper
- .readerFor(Class.class)
- .with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
- .readValue(quote(String.class.getName()));
- assertEquals(String.class, result);
-
- try {
- mapper
- .readerFor(Class.class)
- .without(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
- .readValue("[" + quote(String.class.getName()) + "]");
- fail("Did not throw exception when UNWRAP_SINGLE_VALUE_ARRAYS feature was disabled and attempted to read a Class array containing one element");
- } catch (JsonMappingException e) {
- verifyException(e, "out of START_ARRAY token");
- }
-
- try {
- mapper
- .readerFor(Class.class)
- .with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
- .readValue("[" + quote(Object.class.getName()) + "," + quote(Object.class.getName()) +"]");
- fail("Did not throw exception when UNWRAP_SINGLE_VALUE_ARRAYS feature was enabled and attempted to read a Class array containing two elements");
- } catch (JsonMappingException e) {
- verifyException(e, "more than a single value in");
- }
- result = mapper
- .readerFor(Class.class)
- .with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
- .readValue("[" + quote(String.class.getName()) + "]");
- assertEquals(String.class, result);
- }
-
public void testCurrency() throws IOException
{
Currency usd = Currency.getInstance("USD");
@@ -163,13 +128,6 @@
String json = MAPPER.writeValueAsString(abs);
File result = MAPPER.readValue(json, File.class);
assertEquals(abs, result.getAbsolutePath());
-
- // Then #170
- final ObjectMapper mapper2 = new ObjectMapper();
- mapper2.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE);
-
- result = mapper2.readValue(json, File.class);
- assertEquals(abs, result.getAbsolutePath());
}
public void testLocale() throws IOException
@@ -317,30 +275,6 @@
}
}
- public void testURIAsArray() throws Exception
- {
- final ObjectReader reader = MAPPER.readerFor(URI.class);
- final URI value = new URI("http://foo.com");
- try {
- reader.without(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
- .readValue("[\""+value.toString()+"\"]");
- fail("Did not throw exception for single value array when UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
- } catch (JsonMappingException e) {
- verifyException(e, "out of START_ARRAY token");
- }
-
- try {
- reader.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
- .readValue("[\""+value.toString()+"\",\""+value.toString()+"\"]");
- fail("Did not throw exception for single value array when there were multiple values");
- } catch (JsonMappingException e) {
- verifyException(e, "more than a single value in the array");
- }
- assertEquals(value,
- reader.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
- .readValue("[\""+value.toString()+"\"]"));
- }
-
public void testURL() throws Exception
{
URL exp = new URL("http://foo.com");
@@ -387,25 +321,6 @@
UUID uuid = UUID.fromString(value);
assertEquals(uuid,
mapper.readValue(quote(value), UUID.class));
-
- try {
- mapper.readValue("[" + quote(value) + "]", UUID.class);
- fail("Exception was not thrown when UNWRAP_SINGLE_VALUE_ARRAYS is disabled and attempted to read a single value array as a single element");
- } catch (JsonMappingException exp) {
- //Exception thrown successfully
- }
-
- mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
-
- assertEquals(uuid,
- mapper.readValue("[" + quote(value) + "]", UUID.class));
-
- try {
- mapper.readValue("[" + quote(value) + "," + quote(value) + "]", UUID.class);
- fail("Exception was not thrown when UNWRAP_SINGLE_VALUE_ARRAYS is enabled and attempted to read a multi value array as a single element");
- } catch (JsonMappingException exp) {
- //Exception thrown successfully
- }
}
// then use templating; note that these are not exactly valid UUIDs
// wrt spec (type bits etc), but JDK UUID should deal ok
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestMapDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapDeserializationTest.java
similarity index 80%
rename from src/test/java/com/fasterxml/jackson/databind/deser/TestMapDeserialization.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapDeserializationTest.java
index 76d9ee6..525671e 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestMapDeserialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapDeserializationTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser;
+package com.fasterxml.jackson.databind.deser.jdk;
import java.io.IOException;
import java.text.DateFormat;
@@ -14,7 +14,7 @@
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
@SuppressWarnings("serial")
-public class TestMapDeserialization
+public class MapDeserializationTest
extends BaseMapTest
{
static enum Key {
@@ -28,18 +28,18 @@
public BrokenMap(boolean dummy) { super(); }
}
- @JsonDeserialize(using=MapDeserializer.class)
+ @JsonDeserialize(using=CustomMapDeserializer.class)
static class CustomMap extends LinkedHashMap<String,String> { }
- static class MapDeserializer extends StdDeserializer<CustomMap>
+ static class CustomMapDeserializer extends StdDeserializer<CustomMap>
{
- public MapDeserializer() { super(CustomMap.class); }
+ public CustomMapDeserializer() { super(CustomMap.class); }
@Override
- public CustomMap deserialize(JsonParser jp, DeserializationContext ctxt)
+ public CustomMap deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
CustomMap result = new CustomMap();
- result.put("x", jp.getText());
+ result.put("x", p.getText());
return result;
}
}
@@ -57,7 +57,6 @@
}
}
- // [databind#142]
public static class EnumMapContainer {
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
public EnumMap<KeyEnum,ITestType> testTypes;
@@ -87,28 +86,6 @@
private final ObjectMapper MAPPER = new ObjectMapper();
- public void testUntypedMap() throws Exception
- {
- // to get "untyped" default map-to-map, pass Object.class
- String JSON = "{ \"foo\" : \"bar\", \"crazy\" : true, \"null\" : null }";
-
- // Not a guaranteed cast theoretically, but will work:
- @SuppressWarnings("unchecked")
- Map<String,Object> result = (Map<String,Object>)MAPPER.readValue(JSON, Object.class);
- assertNotNull(result);
- assertTrue(result instanceof Map<?,?>);
-
- assertEquals(3, result.size());
-
- assertEquals("bar", result.get("foo"));
- assertEquals(Boolean.TRUE, result.get("crazy"));
- assertNull(result.get("null"));
-
- // Plus, non existing:
- assertNull(result.get("bar"));
- assertNull(result.get(3));
- }
-
public void testBigUntypedMap() throws Exception
{
Map<String,Object> map = new LinkedHashMap<String,Object>();
@@ -504,78 +481,6 @@
/*
/**********************************************************
- /* Test methods, annotated Map.Entry
- /**********************************************************
- */
-
- public void testMapEntrySimpleTypes() throws Exception
- {
- List<Map.Entry<String,Long>> stuff = MAPPER.readValue(aposToQuotes("[{'a':15},{'b':42}]"),
- new TypeReference<List<Map.Entry<String,Long>>>() { });
- assertNotNull(stuff);
- assertEquals(2, stuff.size());
- assertNotNull(stuff.get(1));
- assertEquals("b", stuff.get(1).getKey());
- assertEquals(Long.valueOf(42), stuff.get(1).getValue());
- }
-
- public void testMapEntryWithStringBean() throws Exception
- {
- List<Map.Entry<Integer,StringWrapper>> stuff = MAPPER.readValue(aposToQuotes("[{'28':'Foo'},{'13':'Bar'}]"),
- new TypeReference<List<Map.Entry<Integer,StringWrapper>>>() { });
- assertNotNull(stuff);
- assertEquals(2, stuff.size());
- assertNotNull(stuff.get(1));
- assertEquals(Integer.valueOf(13), stuff.get(1).getKey());
-
- StringWrapper sw = stuff.get(1).getValue();
- assertEquals("Bar", sw.str);
- }
-
- public void testMapEntryFail() throws Exception
- {
- try {
- /*List<Map.Entry<Integer,StringWrapper>> stuff =*/ MAPPER.readValue(aposToQuotes("[{'28':'Foo','13':'Bar'}]"),
- new TypeReference<List<Map.Entry<Integer,StringWrapper>>>() { });
- fail("Should not have passed");
- } catch (Exception e) {
- verifyException(e, "more than one entry in JSON");
- }
- }
-
- /*
- /**********************************************************
- /* Test methods, other exotic Map types
- /**********************************************************
- */
-
- // [databind#810]
- public void testReadProperties() throws Exception
- {
- Properties props = MAPPER.readValue(aposToQuotes("{'a':'foo', 'b':123, 'c':true}"),
- Properties.class);
- assertEquals(3, props.size());
- assertEquals("foo", props.getProperty("a"));
- assertEquals("123", props.getProperty("b"));
- assertEquals("true", props.getProperty("c"));
- }
-
- // JDK singletonMap
- public void testSingletonMapRoundtrip() throws Exception
- {
- final TypeReference<?> type = new TypeReference<Map<String,IntWrapper>>() { };
-
- String json = MAPPER.writeValueAsString(Collections.singletonMap("value", new IntWrapper(5)));
- Map<String,IntWrapper> result = MAPPER.readValue(json, type);
- assertNotNull(result);
- assertEquals(1, result.size());
- IntWrapper w = result.get("value");
- assertNotNull(w);
- assertEquals(5, w.i);
- }
-
- /*
- /**********************************************************
/* Error tests
/**********************************************************
*/
@@ -584,7 +489,7 @@
{
try {
Object result = MAPPER.readValue("[ 1, 2 ]",
- new TypeReference<Map<String,String>>() { });
+ new TypeReference<Map<String,String>>() { });
fail("Expected an exception, but got result value: "+result);
} catch (JsonMappingException jex) {
verifyException(jex, "START_ARRAY");
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapKeyDeserializationTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapKeyDeserializationTest.java
new file mode 100644
index 0000000..32a616c
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapKeyDeserializationTest.java
@@ -0,0 +1,141 @@
+package com.fasterxml.jackson.databind.deser.jdk;
+
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonValue;
+import com.fasterxml.jackson.core.Base64Variants;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.*;
+
+import org.junit.Assert;
+
+public class MapKeyDeserializationTest extends BaseMapTest
+{
+ static class FullName {
+ private String _firstname, _lastname;
+
+ private FullName(String firstname, String lastname) {
+ _firstname = firstname;
+ _lastname = lastname;
+ }
+
+ @JsonCreator
+ public static FullName valueOf(String value) {
+ String[] mySplit = value.split("\\.");
+ return new FullName(mySplit[0], mySplit[1]);
+ }
+
+ public static FullName valueOf(String firstname, String lastname) {
+ return new FullName(firstname, lastname);
+ }
+
+ @JsonValue
+ @Override
+ public String toString() {
+ return _firstname + "." + _lastname;
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, wrapper keys
+ /**********************************************************
+ */
+
+ final private ObjectMapper MAPPER = objectMapper();
+
+ public void testBooleanMapKeyDeserialization() throws Exception
+ {
+ TypeReference<?> type = new TypeReference<MapWrapper<Boolean, String>>() { };
+ MapWrapper<byte[], String> result = MAPPER.readValue(aposToQuotes("{'map':{'true':'foobar'}}"), type);
+
+ assertEquals(1, result.map.size());
+ Assert.assertEquals(Boolean.TRUE, result.map.entrySet().iterator().next().getKey());
+
+ result = MAPPER.readValue(aposToQuotes("{'map':{'false':'foobar'}}"), type);
+ assertEquals(1, result.map.size());
+ Assert.assertEquals(Boolean.FALSE, result.map.entrySet().iterator().next().getKey());
+ }
+
+ public void testByteMapKeyDeserialization() throws Exception
+ {
+ TypeReference<?> type = new TypeReference<MapWrapper<Byte, String>>() { };
+ MapWrapper<byte[], String> result = MAPPER.readValue(aposToQuotes("{'map':{'13':'foobar'}}"), type);
+ assertEquals(1, result.map.size());
+ Assert.assertEquals(Byte.valueOf((byte) 13), result.map.entrySet().iterator().next().getKey());
+ }
+
+ public void testShortMapKeyDeserialization() throws Exception
+ {
+ TypeReference<?> type = new TypeReference<MapWrapper<Short, String>>() { };
+ MapWrapper<byte[], String> result = MAPPER.readValue(aposToQuotes("{'map':{'13':'foobar'}}"), type);
+ assertEquals(1, result.map.size());
+ Assert.assertEquals(Short.valueOf((short) 13), result.map.entrySet().iterator().next().getKey());
+ }
+
+ public void testIntegerMapKeyDeserialization() throws Exception
+ {
+ TypeReference<?> type = new TypeReference<MapWrapper<Integer, String>>() { };
+ MapWrapper<byte[], String> result = MAPPER.readValue(aposToQuotes("{'map':{'-3':'foobar'}}"), type);
+ assertEquals(1, result.map.size());
+ Assert.assertEquals(Integer.valueOf(-3), result.map.entrySet().iterator().next().getKey());
+ }
+
+ public void testLongMapKeyDeserialization() throws Exception
+ {
+ TypeReference<?> type = new TypeReference<MapWrapper<Long, String>>() { };
+ MapWrapper<byte[], String> result = MAPPER.readValue(aposToQuotes("{'map':{'42':'foobar'}}"), type);
+ assertEquals(1, result.map.size());
+ Assert.assertEquals(Long.valueOf(42), result.map.entrySet().iterator().next().getKey());
+ }
+
+ public void testFloatMapKeyDeserialization() throws Exception
+ {
+ TypeReference<?> type = new TypeReference<MapWrapper<Float, String>>() { };
+ MapWrapper<byte[], String> result = MAPPER.readValue(aposToQuotes("{'map':{'3.5':'foobar'}}"), type);
+ assertEquals(1, result.map.size());
+ Assert.assertEquals(Float.valueOf(3.5f), result.map.entrySet().iterator().next().getKey());
+ }
+
+ public void testDoubleMapKeyDeserialization() throws Exception
+ {
+ TypeReference<?> type = new TypeReference<MapWrapper<Double, String>>() { };
+ MapWrapper<byte[], String> result = MAPPER.readValue(aposToQuotes("{'map':{'0.25':'foobar'}}"), type);
+ assertEquals(1, result.map.size());
+ Assert.assertEquals(Double.valueOf(0.25), result.map.entrySet().iterator().next().getKey());
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, other
+ /**********************************************************
+ */
+
+ public void testDeserializeKeyViaFactory() throws Exception
+ {
+ Map<FullName, Double> map =
+ MAPPER.readValue("{\"first.last\": 42}",
+ new TypeReference<Map<FullName, Double>>() { });
+ Map.Entry<FullName, Double> entry = map.entrySet().iterator().next();
+ FullName key = entry.getKey();
+ assertEquals(key._firstname, "first");
+ assertEquals(key._lastname, "last");
+ assertEquals(entry.getValue().doubleValue(), 42, 0);
+ }
+
+ public void testByteArrayMapKeyDeserialization() throws Exception
+ {
+ byte[] binary = new byte[] { 1, 2, 4, 8, 16, 33, 79 };
+ String encoded = Base64Variants.MIME.encode(binary);
+
+ MapWrapper<byte[], String> result = MAPPER.readValue(
+ aposToQuotes("{'map':{'"+encoded+"':'foobar'}}"),
+ new TypeReference<MapWrapper<byte[], String>>() { });
+ assertEquals(1, result.map.size());
+ Map.Entry<byte[],String> entry = result.map.entrySet().iterator().next();
+ assertEquals("foobar", entry.getValue());
+ byte[] key = entry.getKey();
+ Assert.assertArrayEquals(binary, key);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapRelatedTypesDeserTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapRelatedTypesDeserTest.java
new file mode 100644
index 0000000..bc8dc16
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapRelatedTypesDeserTest.java
@@ -0,0 +1,84 @@
+package com.fasterxml.jackson.databind.deser.jdk;
+
+import java.util.*;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.*;
+
+public class MapRelatedTypesDeserTest
+ extends BaseMapTest
+{
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ /*
+ /**********************************************************
+ /* Test methods, Map.Entry
+ /**********************************************************
+ */
+
+ public void testMapEntrySimpleTypes() throws Exception
+ {
+ List<Map.Entry<String,Long>> stuff = MAPPER.readValue(aposToQuotes("[{'a':15},{'b':42}]"),
+ new TypeReference<List<Map.Entry<String,Long>>>() { });
+ assertNotNull(stuff);
+ assertEquals(2, stuff.size());
+ assertNotNull(stuff.get(1));
+ assertEquals("b", stuff.get(1).getKey());
+ assertEquals(Long.valueOf(42), stuff.get(1).getValue());
+ }
+
+ public void testMapEntryWithStringBean() throws Exception
+ {
+ List<Map.Entry<Integer,StringWrapper>> stuff = MAPPER.readValue(aposToQuotes("[{'28':'Foo'},{'13':'Bar'}]"),
+ new TypeReference<List<Map.Entry<Integer,StringWrapper>>>() { });
+ assertNotNull(stuff);
+ assertEquals(2, stuff.size());
+ assertNotNull(stuff.get(1));
+ assertEquals(Integer.valueOf(13), stuff.get(1).getKey());
+
+ StringWrapper sw = stuff.get(1).getValue();
+ assertEquals("Bar", sw.str);
+ }
+
+ public void testMapEntryFail() throws Exception
+ {
+ try {
+ /*List<Map.Entry<Integer,StringWrapper>> stuff =*/ MAPPER.readValue(aposToQuotes("[{'28':'Foo','13':'Bar'}]"),
+ new TypeReference<List<Map.Entry<Integer,StringWrapper>>>() { });
+ fail("Should not have passed");
+ } catch (Exception e) {
+ verifyException(e, "more than one entry in JSON");
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, other exotic Map types
+ /**********************************************************
+ */
+
+ // [databind#810]
+ public void testReadProperties() throws Exception
+ {
+ Properties props = MAPPER.readValue(aposToQuotes("{'a':'foo', 'b':123, 'c':true}"),
+ Properties.class);
+ assertEquals(3, props.size());
+ assertEquals("foo", props.getProperty("a"));
+ assertEquals("123", props.getProperty("b"));
+ assertEquals("true", props.getProperty("c"));
+ }
+
+ // JDK singletonMap
+ public void testSingletonMapRoundtrip() throws Exception
+ {
+ final TypeReference<?> type = new TypeReference<Map<String,IntWrapper>>() { };
+
+ String json = MAPPER.writeValueAsString(Collections.singletonMap("value", new IntWrapper(5)));
+ Map<String,IntWrapper> result = MAPPER.readValue(json, type);
+ assertNotNull(result);
+ assertEquals(1, result.size());
+ IntWrapper w = result.get("value");
+ assertNotNull(w);
+ assertEquals(5, w.i);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestUntypedDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/UntypedDeserializationTest.java
similarity index 68%
rename from src/test/java/com/fasterxml/jackson/databind/deser/TestUntypedDeserialization.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/jdk/UntypedDeserializationTest.java
index eaf2bc8..8111b7a 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestUntypedDeserialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/UntypedDeserializationTest.java
@@ -1,13 +1,17 @@
-package com.fasterxml.jackson.databind.deser;
+package com.fasterxml.jackson.databind.deser.jdk;
import java.io.*;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
+import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -17,7 +21,7 @@
* one that only uses core JDK types; wrappers, Maps and Lists.
*/
@SuppressWarnings("serial")
-public class TestUntypedDeserialization
+public class UntypedDeserializationTest
extends BaseMapTest
{
static class UCStringDeserializer
@@ -26,8 +30,8 @@
public UCStringDeserializer() { super(String.class); }
@Override
- public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
- return jp.getText().toUpperCase();
+ public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
+ return p.getText().toUpperCase();
}
}
@@ -42,7 +46,7 @@
}
@Override
- public Number deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
+ public Number deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return value;
}
}
@@ -54,12 +58,12 @@
public ListDeserializer() { super(List.class); }
@Override
- public List<Object> deserialize(JsonParser jp, DeserializationContext ctxt)
+ public List<Object> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
ArrayList<Object> list = new ArrayList<Object>();
- while (jp.nextValue() != JsonToken.END_ARRAY) {
- list.add("X"+jp.getText());
+ while (p.nextValue() != JsonToken.END_ARRAY) {
+ list.add("X"+p.getText());
}
return list;
}
@@ -76,17 +80,17 @@
}
}
- static class MapDeserializer extends StdDeserializer<Map<String,Object>>
+ static class YMapDeserializer extends StdDeserializer<Map<String,Object>>
{
- public MapDeserializer() { super(Map.class); }
+ public YMapDeserializer() { super(Map.class); }
@Override
- public Map<String,Object> deserialize(JsonParser jp, DeserializationContext ctxt)
+ public Map<String,Object> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{
Map<String,Object> map = new LinkedHashMap<String,Object>();
- while (jp.nextValue() != JsonToken.END_OBJECT) {
- map.put(jp.getCurrentName(), "Y"+jp.getText());
+ while (p.nextValue() != JsonToken.END_OBJECT) {
+ map.put(p.getCurrentName(), "Y"+p.getText());
}
return map;
}
@@ -101,6 +105,11 @@
}
}
+ static class WrappedPolymorphicUntyped {
+ @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS)
+ public Object value;
+ }
+
static class WrappedUntyped1460 {
public Object value;
}
@@ -166,7 +175,29 @@
// and that's all folks!
}
-
+
+ public void testUntypedMap() throws Exception
+ {
+ // to get "untyped" default map-to-map, pass Object.class
+ String JSON = "{ \"foo\" : \"bar\", \"crazy\" : true, \"null\" : null }";
+
+ // Not a guaranteed cast theoretically, but will work:
+ @SuppressWarnings("unchecked")
+ Map<String,Object> result = (Map<String,Object>)MAPPER.readValue(JSON, Object.class);
+ assertNotNull(result);
+ assertTrue(result instanceof Map<?,?>);
+
+ assertEquals(3, result.size());
+
+ assertEquals("bar", result.get("foo"));
+ assertEquals(Boolean.TRUE, result.get("crazy"));
+ assertNull(result.get("null"));
+
+ // Plus, non existing:
+ assertNull(result.get("bar"));
+ assertNull(result.get(3));
+ }
+
public void testNestedUntypes() throws IOException
{
// 05-Apr-2014, tatu: Odd failures if using shared mapper; so work around:
@@ -223,6 +254,62 @@
assertEquals(Integer.valueOf(13), value);
}
+ // Test that exercises non-vanilla variant, with just one simple custom deserializer
+ public void testNonVanilla() throws IOException
+ {
+ SimpleModule m = new SimpleModule("test-module");
+ m.addDeserializer(String.class, new UCStringDeserializer());
+ final ObjectMapper mapper = new ObjectMapper()
+ .registerModule(m);
+
+ // Also: since this is now non-vanilla variant, try more alternatives
+ List<?> l = (List<?>) mapper.readValue("[ true, false, 7, 0.5, \"foo\"]", Object.class);
+ assertEquals(5, l.size());
+ assertEquals(Boolean.TRUE, l.get(0));
+ assertEquals(Boolean.FALSE, l.get(1));
+ assertEquals(Integer.valueOf(7), l.get(2));
+ assertEquals(Double.valueOf(0.5), l.get(3));
+ assertEquals("FOO", l.get(4));
+
+ l = (List<?>) mapper.readValue("[ {}, [] ]", Object.class);
+ assertEquals(2, l.size());
+ assertTrue(l.get(0) instanceof Map<?,?>);
+ assertTrue(l.get(1) instanceof List<?>);
+
+ ObjectReader rDefault = mapper.readerFor(WrappedPolymorphicUntyped.class);
+ ObjectReader rAlt = rDefault
+ .with(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS,
+ DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);
+ WrappedPolymorphicUntyped w;
+
+ w = rDefault.readValue(aposToQuotes("{'value':10}"));
+ assertEquals(Integer.valueOf(10), w.value);
+ w = rAlt.readValue(aposToQuotes("{'value':10}"));
+ assertEquals(BigInteger.TEN, w.value);
+
+ w = rDefault.readValue(aposToQuotes("{'value':5.0}"));
+ assertEquals(Double.valueOf(5.0), w.value);
+ w = rAlt.readValue(aposToQuotes("{'value':5.0}"));
+ assertEquals(new BigDecimal("5.0"), w.value);
+
+ StringBuilder sb = new StringBuilder(100).append("[0");
+ for (int i = 1; i < 100; ++i) {
+ sb.append(", ").append(i);
+ }
+ sb.append("]");
+ final String INT_ARRAY_JSON = sb.toString();
+
+ // First read as-is, no type wrapping
+ Object ob = mapper.readerFor(Object.class)
+ .with(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)
+ .readValue(INT_ARRAY_JSON);
+ assertTrue(ob instanceof Object[]);
+ Object[] obs = (Object[]) ob;
+ for (int i = 0; i < 100; ++i) {
+ assertEquals(Integer.valueOf(i), obs[i]);
+ }
+ }
+
public void testUntypedWithListDeser() throws IOException
{
SimpleModule m = new SimpleModule("test-module");
@@ -243,7 +330,7 @@
public void testUntypedWithMapDeser() throws IOException
{
SimpleModule m = new SimpleModule("test-module");
- m.addDeserializer(Map.class, new MapDeserializer());
+ m.addDeserializer(Map.class, new YMapDeserializer());
final ObjectMapper mapper = new ObjectMapper()
.registerModule(m);
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/merge/ArrayMergeTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/merge/ArrayMergeTest.java
new file mode 100644
index 0000000..908ddc5
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/merge/ArrayMergeTest.java
@@ -0,0 +1,161 @@
+package com.fasterxml.jackson.databind.deser.merge;
+
+import org.junit.Assert;
+
+import com.fasterxml.jackson.annotation.JsonMerge;
+import com.fasterxml.jackson.annotation.OptBoolean;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+
+import com.fasterxml.jackson.databind.*;
+
+public class ArrayMergeTest extends BaseMapTest
+{
+ static class MergedX<T>
+ {
+ @JsonMerge(OptBoolean.TRUE)
+ public T value;
+
+ public MergedX(T v) { value = v; }
+ protected MergedX() { }
+ }
+
+ /*
+ /********************************************************
+ /* Test methods
+ /********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper()
+ // 26-Oct-2016, tatu: Make sure we'll report merge problems by default
+ .disable(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)
+ ;
+
+ public void testObjectArrayMerging() throws Exception
+ {
+ MergedX<Object[]> input = new MergedX<Object[]>(new Object[] {
+ "foo"
+ });
+ final JavaType type = MAPPER.getTypeFactory().constructType(new TypeReference<MergedX<Object[]>>() {});
+ MergedX<Object[]> result = MAPPER.readerFor(type)
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':['bar']}"));
+ assertSame(input, result);
+ assertEquals(2, result.value.length);
+ assertEquals("foo", result.value[0]);
+ assertEquals("bar", result.value[1]);
+
+ // and with one trick
+ result = MAPPER.readerFor(type)
+ .with(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':'zap'}"));
+ assertSame(input, result);
+ assertEquals(3, result.value.length);
+ assertEquals("foo", result.value[0]);
+ assertEquals("bar", result.value[1]);
+ assertEquals("zap", result.value[2]);
+ }
+
+ public void testStringArrayMerging() throws Exception
+ {
+ MergedX<String[]> input = new MergedX<String[]>(new String[] { "foo" });
+ MergedX<String[]> result = MAPPER
+ .readerFor(new TypeReference<MergedX<String[]>>() {})
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':['bar']}"));
+ assertSame(input, result);
+ assertEquals(2, result.value.length);
+ assertEquals("foo", result.value[0]);
+ assertEquals("bar", result.value[1]);
+ }
+
+ public void testBooleanArrayMerging() throws Exception
+ {
+ MergedX<boolean[]> input = new MergedX<boolean[]>(new boolean[] { true, false });
+ MergedX<boolean[]> result = MAPPER
+ .readerFor(new TypeReference<MergedX<boolean[]>>() {})
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':[true]}"));
+ assertSame(input, result);
+ assertEquals(3, result.value.length);
+ Assert.assertArrayEquals(new boolean[] { true, false, true }, result.value);
+ }
+
+ public void testByteArrayMerging() throws Exception
+ {
+ MergedX<byte[]> input = new MergedX<byte[]>(new byte[] { 1, 2 });
+ MergedX<byte[]> result = MAPPER
+ .readerFor(new TypeReference<MergedX<byte[]>>() {})
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':[4, 6.0, null]}"));
+ assertSame(input, result);
+ assertEquals(5, result.value.length);
+ Assert.assertArrayEquals(new byte[] { 1, 2, 4, 6, 0 }, result.value);
+ }
+
+ public void testShortArrayMerging() throws Exception
+ {
+ MergedX<short[]> input = new MergedX<short[]>(new short[] { 1, 2 });
+ MergedX<short[]> result = MAPPER
+ .readerFor(new TypeReference<MergedX<short[]>>() {})
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':[4, 6]}"));
+ assertSame(input, result);
+ assertEquals(4, result.value.length);
+ Assert.assertArrayEquals(new short[] { 1, 2, 4, 6 }, result.value);
+ }
+
+ public void testCharArrayMerging() throws Exception
+ {
+ MergedX<char[]> input = new MergedX<char[]>(new char[] { 'a', 'b' });
+ MergedX<char[]> result = MAPPER
+ .readerFor(new TypeReference<MergedX<char[]>>() {})
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':['c']}"));
+ assertSame(input, result);
+ Assert.assertArrayEquals(new char[] { 'a', 'b', 'c' }, result.value);
+
+ // also some variation
+ input = new MergedX<char[]>(new char[] { });
+ result = MAPPER
+ .readerFor(new TypeReference<MergedX<char[]>>() {})
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':['c']}"));
+ assertSame(input, result);
+ Assert.assertArrayEquals(new char[] { 'c' }, result.value);
+ }
+
+ public void testIntArrayMerging() throws Exception
+ {
+ MergedX<int[]> input = new MergedX<int[]>(new int[] { 1, 2 });
+ MergedX<int[]> result = MAPPER
+ .readerFor(new TypeReference<MergedX<int[]>>() {})
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':[4, 6]}"));
+ assertSame(input, result);
+ assertEquals(4, result.value.length);
+ Assert.assertArrayEquals(new int[] { 1, 2, 4, 6 }, result.value);
+
+ // also some variation
+ input = new MergedX<int[]>(new int[] { 3, 4, 6 });
+ result = MAPPER
+ .readerFor(new TypeReference<MergedX<int[]>>() {})
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':[ ]}"));
+ assertSame(input, result);
+ Assert.assertArrayEquals(new int[] { 3, 4, 6 }, result.value);
+ }
+
+ public void testLongArrayMerging() throws Exception
+ {
+ MergedX<long[]> input = new MergedX<long[]>(new long[] { 1, 2 });
+ MergedX<long[]> result = MAPPER
+ .readerFor(new TypeReference<MergedX<long[]>>() {})
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':[4, 6]}"));
+ assertSame(input, result);
+ assertEquals(4, result.value.length);
+ Assert.assertArrayEquals(new long[] { 1, 2, 4, 6 }, result.value);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/merge/CollectionMergeTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/merge/CollectionMergeTest.java
new file mode 100644
index 0000000..3305d37
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/merge/CollectionMergeTest.java
@@ -0,0 +1,102 @@
+package com.fasterxml.jackson.databind.deser.merge;
+
+import java.util.*;
+
+import com.fasterxml.jackson.annotation.JsonMerge;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.MapperFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class CollectionMergeTest extends BaseMapTest
+{
+ static class CollectionWrapper {
+ @JsonMerge
+ public Collection<String> bag = new TreeSet<String>();
+ {
+ bag.add("a");
+ }
+ }
+
+ static class MergedList
+ {
+ @JsonMerge
+ public List<String> values = new ArrayList<>();
+ {
+ values.add("a");
+ }
+ }
+
+ static class MergedEnumSet
+ {
+ @JsonMerge
+ public EnumSet<ABC> abc = EnumSet.of(ABC.B);
+ }
+
+ static class MergedX<T>
+ {
+ @JsonMerge
+ T value;
+
+ public MergedX(T v) { value = v; }
+ protected MergedX() { }
+
+ public void setValue(T v) { value = v; }
+ }
+
+ /*
+ /********************************************************
+ /* Test methods
+ /********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper()
+ // 26-Oct-2016, tatu: Make sure we'll report merge problems by default
+ .disable(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)
+ ;
+
+ public void testCollectionMerging() throws Exception
+ {
+ CollectionWrapper w = MAPPER.readValue(aposToQuotes("{'bag':['b']}"), CollectionWrapper.class);
+ assertEquals(2, w.bag.size());
+ assertTrue(w.bag.contains("a"));
+ assertTrue(w.bag.contains("b"));
+ }
+
+ public void testListMerging() throws Exception
+ {
+ MergedList w = MAPPER.readValue(aposToQuotes("{'values':['x']}"), MergedList.class);
+ assertEquals(2, w.values.size());
+ assertTrue(w.values.contains("a"));
+ assertTrue(w.values.contains("x"));
+ }
+
+ // Test that uses generic type
+ public void testGenericListMerging() throws Exception
+ {
+ Collection<String> l = new ArrayList<>();
+ l.add("foo");
+ MergedX<Collection<String>> input = new MergedX<Collection<String>>(l);
+
+ MergedX<Collection<String>> result = MAPPER
+ .readerFor(new TypeReference<MergedX<Collection<String>>>() {})
+ .withValueToUpdate(input)
+ .readValue(aposToQuotes("{'value':['bar']}"));
+ assertSame(input, result);
+ assertEquals(2, result.value.size());
+ Iterator<String> it = result.value.iterator();
+ assertEquals("foo", it.next());
+ assertEquals("bar", it.next());
+ }
+
+ public void testEnumSetMerging() throws Exception
+ {
+ MergedEnumSet result = MAPPER.readValue(aposToQuotes("{'abc':['A']}"), MergedEnumSet.class);
+ assertEquals(2, result.abc.size());
+ assertTrue(result.abc.contains(ABC.B)); // original
+ assertTrue(result.abc.contains(ABC.A)); // added
+ }
+
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/merge/MapMergeTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/merge/MapMergeTest.java
new file mode 100644
index 0000000..e0c4ebe
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/merge/MapMergeTest.java
@@ -0,0 +1,150 @@
+package com.fasterxml.jackson.databind.deser.merge;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonMerge;
+import com.fasterxml.jackson.annotation.JsonSetter;
+import com.fasterxml.jackson.annotation.Nulls;
+import com.fasterxml.jackson.databind.*;
+
+public class MapMergeTest extends BaseMapTest
+{
+ static class MergedMap
+ {
+ @JsonMerge
+ public Map<String,Object> values;
+
+ protected MergedMap() {
+ values = new LinkedHashMap<>();
+ values.put("a", "x");
+ }
+
+ public MergedMap(String a, String b) {
+ values = new LinkedHashMap<>();
+ values.put(a, b);
+ }
+
+ public MergedMap(Map<String,Object> src) {
+ values = src;
+ }
+ }
+
+ static class MergedIntMap
+ {
+ @JsonMerge
+ public Map<Integer,Object> values;
+
+ protected MergedIntMap() {
+ values = new LinkedHashMap<>();
+ values.put(Integer.valueOf(13), "a");
+ }
+ }
+
+ /*
+ /********************************************************
+ /* Test methods, Map merging
+ /********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper()
+ // 26-Oct-2016, tatu: Make sure we'll report merge problems by default
+ .disable(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)
+ ;
+
+ private final ObjectMapper MAPPER_SKIP_NULLS = newObjectMapper()
+ .setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP));
+ ;
+
+ public void testShallowMapMerging() throws Exception
+ {
+ final String JSON = aposToQuotes("{'values':{'c':'y','d':null}}");
+ MergedMap v = MAPPER.readValue(JSON, MergedMap.class);
+ assertEquals(3, v.values.size());
+ assertEquals("y", v.values.get("c"));
+ assertEquals("x", v.values.get("a"));
+ assertNull(v.values.get("d"));
+
+ // but also, skip nulls
+ v = MAPPER_SKIP_NULLS.readValue(JSON, MergedMap.class);
+ assertEquals(2, v.values.size());
+ assertEquals("y", v.values.get("c"));
+ assertEquals("x", v.values.get("a"));
+ }
+
+ public void testShallowNonStringMerging() throws Exception
+ {
+ final String JSON = aposToQuotes("{'values':{'72':'b','666':null}}");
+ MergedIntMap v = MAPPER.readValue(JSON , MergedIntMap.class);
+ assertEquals(3, v.values.size());
+ assertEquals("a", v.values.get(Integer.valueOf(13)));
+ assertEquals("b", v.values.get(Integer.valueOf(72)));
+ assertNull(v.values.get(Integer.valueOf(666)));
+
+ v = MAPPER_SKIP_NULLS.readValue(JSON , MergedIntMap.class);
+ assertEquals(2, v.values.size());
+ assertEquals("a", v.values.get(Integer.valueOf(13)));
+ assertEquals("b", v.values.get(Integer.valueOf(72)));
+ }
+
+ @SuppressWarnings("unchecked")
+ public void testDeeperMapMerging() throws Exception
+ {
+ // first, create base Map
+ MergedMap base = new MergedMap("name", "foobar");
+ Map<String,Object> props = new LinkedHashMap<>();
+ props.put("default", "yes");
+ props.put("x", "abc");
+ Map<String,Object> innerProps = new LinkedHashMap<>();
+ innerProps.put("z", Integer.valueOf(13));
+ props.put("extra", innerProps);
+ base.values.put("props", props);
+
+ // to be update
+ MergedMap v = MAPPER.readerForUpdating(base)
+ .readValue(aposToQuotes("{'values':{'props':{'x':'xyz','y' : '...','extra':{ 'ab' : true}}}}"));
+ assertEquals(2, v.values.size());
+ assertEquals("foobar", v.values.get("name"));
+ assertNotNull(v.values.get("props"));
+ props = (Map<String,Object>) v.values.get("props");
+ assertEquals(4, props.size());
+ assertEquals("yes", props.get("default"));
+ assertEquals("xyz", props.get("x"));
+ assertEquals("...", props.get("y"));
+ assertNotNull(props.get("extra"));
+ innerProps = (Map<String,Object>) props.get("extra");
+ assertEquals(2, innerProps.size());
+ assertEquals(Integer.valueOf(13), innerProps.get("z"));
+ assertEquals(Boolean.TRUE, innerProps.get("ab"));
+ }
+
+ @SuppressWarnings("unchecked")
+ public void testMapMergingWithArray() throws Exception
+ {
+ // first, create base Map
+ MergedMap base = new MergedMap("name", "foobar");
+ Map<String,Object> props = new LinkedHashMap<>();
+ List<String> names = new ArrayList<>();
+ names.add("foo");
+ props.put("names", names);
+ base.values.put("props", props);
+ props.put("extra", "misc");
+
+ // to be update
+ MergedMap v = MAPPER.readerForUpdating(base)
+ .readValue(aposToQuotes("{'values':{'props':{'names': [ 'bar' ] }}}"));
+ assertEquals(2, v.values.size());
+ assertEquals("foobar", v.values.get("name"));
+ assertNotNull(v.values.get("props"));
+ props = (Map<String,Object>) v.values.get("props");
+ assertEquals(2, props.size());
+ assertEquals("misc", props.get("extra"));
+ assertNotNull(props.get("names"));
+ names = (List<String>) props.get("names");
+ assertEquals(2, names.size());
+ assertEquals("foo", names.get(0));
+ assertEquals("bar", names.get(1));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/merge/MergeWithNullTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/merge/MergeWithNullTest.java
new file mode 100644
index 0000000..bc80ad3
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/merge/MergeWithNullTest.java
@@ -0,0 +1,133 @@
+package com.fasterxml.jackson.databind.deser.merge;
+
+import com.fasterxml.jackson.annotation.JsonMerge;
+import com.fasterxml.jackson.annotation.JsonSetter;
+import com.fasterxml.jackson.annotation.Nulls;
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.MapperFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class MergeWithNullTest extends BaseMapTest
+{
+ static class ConfigDefault {
+ @JsonMerge
+ public AB loc = new AB(1, 2);
+
+ protected ConfigDefault() { }
+ public ConfigDefault(int a, int b) {
+ loc = new AB(a, b);
+ }
+ }
+
+ static class ConfigSkipNull {
+ @JsonMerge
+ @JsonSetter(nulls=Nulls.SKIP)
+ public AB loc = new AB(1, 2);
+
+ protected ConfigSkipNull() { }
+ public ConfigSkipNull(int a, int b) {
+ loc = new AB(a, b);
+ }
+ }
+
+ static class ConfigAllowNullOverwrite {
+ @JsonMerge
+ @JsonSetter(nulls=Nulls.SET)
+ public AB loc = new AB(1, 2);
+
+ protected ConfigAllowNullOverwrite() { }
+ public ConfigAllowNullOverwrite(int a, int b) {
+ loc = new AB(a, b);
+ }
+ }
+
+ // another variant where all we got is a getter
+ static class NoSetterConfig {
+ AB _value = new AB(2, 3);
+
+ @JsonMerge
+ public AB getValue() { return _value; }
+ }
+
+ static class AB {
+ public int a;
+ public int b;
+
+ protected AB() { }
+ public AB(int a0, int b0) {
+ a = a0;
+ b = b0;
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper()
+ // 26-Oct-2016, tatu: Make sure we'll report merge problems by default
+ .disable(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)
+ ;
+
+ public void testBeanMergingWithNullDefault() throws Exception
+ {
+ // By default `null` should simply overwrite value
+ ConfigDefault config = MAPPER.readerForUpdating(new ConfigDefault(5, 7))
+ .readValue(aposToQuotes("{'loc':null}"));
+ assertNotNull(config);
+ assertNull(config.loc);
+
+ // but it should be possible to override setting to, say, skip
+
+ // First: via specific type override
+ // important! We'll specify for value type to be merged
+ ObjectMapper mapper = newObjectMapper();
+ mapper.configOverride(AB.class)
+ .setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.SKIP));
+ config = mapper.readerForUpdating(new ConfigDefault(137, -3))
+ .readValue(aposToQuotes("{'loc':null}"));
+ assertNotNull(config.loc);
+ assertEquals(137, config.loc.a);
+ assertEquals(-3, config.loc.b);
+
+ // Second: by global defaults
+ mapper = newObjectMapper();
+ mapper.setDefaultSetterInfo(JsonSetter.Value.forValueNulls(Nulls.SKIP));
+ config = mapper.readerForUpdating(new ConfigDefault(12, 34))
+ .readValue(aposToQuotes("{'loc':null}"));
+ assertNotNull(config.loc);
+ assertEquals(12, config.loc.a);
+ assertEquals(34, config.loc.b);
+ }
+
+ public void testBeanMergingWithNullSkip() throws Exception
+ {
+ ConfigSkipNull config = MAPPER.readerForUpdating(new ConfigSkipNull(5, 7))
+ .readValue(aposToQuotes("{'loc':null}"));
+ assertNotNull(config);
+ assertNotNull(config.loc);
+ assertEquals(5, config.loc.a);
+ assertEquals(7, config.loc.b);
+ }
+
+ public void testBeanMergingWithNullSet() throws Exception
+ {
+ ConfigAllowNullOverwrite config = MAPPER.readerForUpdating(new ConfigAllowNullOverwrite(5, 7))
+ .readValue(aposToQuotes("{'loc':null}"));
+ assertNotNull(config);
+ assertNull(config.loc);
+ }
+
+ public void testSetterlessMergingWithNull() throws Exception
+ {
+ NoSetterConfig input = new NoSetterConfig();
+ NoSetterConfig result = MAPPER.readerForUpdating(input)
+ .readValue(aposToQuotes("{'value':null}"));
+ assertNotNull(result.getValue());
+ assertEquals(2, result.getValue().a);
+ assertEquals(3, result.getValue().b);
+ assertSame(input, result);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/merge/NodeMergeTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/merge/NodeMergeTest.java
new file mode 100644
index 0000000..435ecdb
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/merge/NodeMergeTest.java
@@ -0,0 +1,117 @@
+package com.fasterxml.jackson.databind.deser.merge;
+
+import com.fasterxml.jackson.annotation.JsonMerge;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+
+public class NodeMergeTest extends BaseMapTest
+{
+ private final static ObjectMapper MAPPER = newObjectMapper()
+ // 26-Oct-2016, tatu: Make sure we'll report merge problems by default
+ .disable(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)
+ ;
+
+ static class ObjectNodeWrapper {
+ @JsonMerge
+ public ObjectNode props = MAPPER.createObjectNode();
+ {
+ props.put("default", "enabled");
+ }
+ }
+
+ static class ArrayNodeWrapper {
+ @JsonMerge
+ public ArrayNode list = MAPPER.createArrayNode();
+ {
+ list.add(123);
+ }
+ }
+
+ /*
+ /********************************************************
+ /* Test methods
+ /********************************************************
+ */
+
+ public void testObjectNodeUpdateValue() throws Exception
+ {
+ ObjectNode base = MAPPER.createObjectNode();
+ base.put("first", "foo");
+ assertSame(base,
+ MAPPER.readerForUpdating(base)
+ .readValue(aposToQuotes("{'second':'bar', 'third':5, 'fourth':true}")));
+ assertEquals(4, base.size());
+ assertEquals("bar", base.path("second").asText());
+ assertEquals("foo", base.path("first").asText());
+ assertEquals(5, base.path("third").asInt());
+ assertTrue(base.path("fourth").asBoolean());
+ }
+
+ public void testObjectNodeMerge() throws Exception
+ {
+ ObjectNodeWrapper w = MAPPER.readValue(aposToQuotes("{'props':{'stuff':'xyz'}}"),
+ ObjectNodeWrapper.class);
+ assertEquals(2, w.props.size());
+ assertEquals("enabled", w.props.path("default").asText());
+ assertEquals("xyz", w.props.path("stuff").asText());
+ }
+
+ public void testObjectDeepUpdate() throws Exception
+ {
+ ObjectNode base = MAPPER.createObjectNode();
+ ObjectNode props = base.putObject("props");
+ props.put("base", 123);
+ props.put("value", 456);
+ ArrayNode a = props.putArray("array");
+ a.add(true);
+ base.putNull("misc");
+ assertSame(base,
+ MAPPER.readerForUpdating(base)
+ .readValue(aposToQuotes(
+ "{'props':{'value':true, 'extra':25.5, 'array' : [ 3 ]}}")));
+ assertEquals(2, base.size());
+ ObjectNode resultProps = (ObjectNode) base.get("props");
+ assertEquals(4, resultProps.size());
+
+ assertEquals(123, resultProps.path("base").asInt());
+ assertTrue(resultProps.path("value").asBoolean());
+ assertEquals(25.5, resultProps.path("extra").asDouble());
+ JsonNode n = resultProps.get("array");
+ assertEquals(ArrayNode.class, n.getClass());
+ assertEquals(2, n.size());
+ assertEquals(3, n.get(1).asInt());
+ }
+
+ public void testArrayNodeUpdateValue() throws Exception
+ {
+ ArrayNode base = MAPPER.createArrayNode();
+ base.add("first");
+ assertSame(base,
+ MAPPER.readerForUpdating(base)
+ .readValue(aposToQuotes("['second',false,null]")));
+ assertEquals(4, base.size());
+ assertEquals("first", base.path(0).asText());
+ assertEquals("second", base.path(1).asText());
+ assertFalse(base.path(2).asBoolean());
+ assertTrue(base.path(3).isNull());
+ }
+
+ public void testArrayNodeMerge() throws Exception
+ {
+ ArrayNodeWrapper w = MAPPER.readValue(aposToQuotes("{'list':[456,true,{}, [], 'foo']}"),
+ ArrayNodeWrapper.class);
+ assertEquals(6, w.list.size());
+ assertEquals(123, w.list.get(0).asInt());
+ assertEquals(456, w.list.get(1).asInt());
+ assertTrue(w.list.get(2).asBoolean());
+ JsonNode n = w.list.get(3);
+ assertTrue(n.isObject());
+ assertEquals(0, n.size());
+ n = w.list.get(4);
+ assertTrue(n.isArray());
+ assertEquals(0, n.size());
+ assertEquals("foo", w.list.get(5).asText());
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/merge/PropertyMergeTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/merge/PropertyMergeTest.java
new file mode 100644
index 0000000..145df17
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/merge/PropertyMergeTest.java
@@ -0,0 +1,229 @@
+package com.fasterxml.jackson.databind.deser.merge;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+import com.fasterxml.jackson.annotation.*;
+import com.fasterxml.jackson.annotation.JsonFormat.Shape;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
+
+/**
+ * Tests to make sure that the new "merging" property of
+ * <code>JsonSetter</code> annotation works as expected.
+ *
+ * @since 2.9
+ */
+@SuppressWarnings("serial")
+public class PropertyMergeTest extends BaseMapTest
+{
+ static class Config {
+ @JsonMerge
+ public AB loc = new AB(1, 2);
+
+ protected Config() { }
+ public Config(int a, int b) {
+ loc = new AB(a, b);
+ }
+ }
+
+ static class NonMergeConfig {
+ public AB loc = new AB(1, 2);
+ }
+
+ // another variant where all we got is a getter
+ static class NoSetterConfig {
+ AB _value = new AB(1, 2);
+
+ @JsonMerge
+ public AB getValue() { return _value; }
+ }
+
+ static class AB {
+ public int a;
+ public int b;
+
+ protected AB() { }
+ public AB(int a0, int b0) {
+ a = a0;
+ b = b0;
+ }
+ }
+
+ @JsonPropertyOrder(alphabetic=true)
+ @JsonFormat(shape=Shape.ARRAY)
+ static class ABAsArray {
+ public int a;
+ public int b;
+ }
+
+ // Custom type that would be deserializable by default
+ static class StringReference extends AtomicReference<String> {
+ public StringReference(String str) {
+ set(str);
+ }
+ }
+
+ static class MergedReference
+ {
+ @JsonMerge
+ public StringReference value = new StringReference("default");
+ }
+
+ static class MergedX<T>
+ {
+ @JsonMerge
+ public T value;
+
+ public MergedX(T v) { value = v; }
+ protected MergedX() { }
+ }
+
+ // // // Classes with invalid merge definition(s)
+
+ static class CantMergeInts {
+ @JsonMerge
+ public int value;
+ }
+
+ /*
+ /********************************************************
+ /* Test methods, POJO merging
+ /********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper()
+ // 26-Oct-2016, tatu: Make sure we'll report merge problems by default
+ .disable(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)
+ ;
+
+ public void testBeanMergingViaProp() throws Exception
+ {
+ Config config = MAPPER.readValue(aposToQuotes("{'loc':{'b':3}}"), Config.class);
+ assertEquals(1, config.loc.a);
+ assertEquals(3, config.loc.b);
+
+ config = MAPPER.readerForUpdating(new Config(5, 7))
+ .readValue(aposToQuotes("{'loc':{'b':2}}"));
+ assertEquals(5, config.loc.a);
+ assertEquals(2, config.loc.b);
+ }
+
+ public void testBeanMergingViaType() throws Exception
+ {
+ // by default, no merging
+ NonMergeConfig config = MAPPER.readValue(aposToQuotes("{'loc':{'a':3}}"), NonMergeConfig.class);
+ assertEquals(3, config.loc.a);
+ assertEquals(0, config.loc.b); // not passed, nor merge from original
+
+ // but with type-overrides
+ ObjectMapper mapper = newObjectMapper();
+ mapper.configOverride(AB.class).setMergeable(true);
+ config = mapper.readValue(aposToQuotes("{'loc':{'a':3}}"), NonMergeConfig.class);
+ assertEquals(3, config.loc.a);
+ assertEquals(2, config.loc.b); // original, merged
+ }
+
+ public void testBeanMergingViaGlobal() throws Exception
+ {
+ // but with type-overrides
+ ObjectMapper mapper = newObjectMapper()
+ .setDefaultMergeable(true);
+ NonMergeConfig config = mapper.readValue(aposToQuotes("{'loc':{'a':3}}"), NonMergeConfig.class);
+ assertEquals(3, config.loc.a);
+ assertEquals(2, config.loc.b); // original, merged
+
+ // also, test with bigger POJO type; just as smoke test
+ FiveMinuteUser user0 = new FiveMinuteUser("Bob", "Bush", true, FiveMinuteUser.Gender.MALE,
+ new byte[] { 1, 2, 3, 4, 5 });
+ FiveMinuteUser user = mapper.readerFor(FiveMinuteUser.class)
+ .withValueToUpdate(user0)
+ .readValue(aposToQuotes("{'name':{'last':'Brown'}}"));
+ assertEquals("Bob", user.getName().getFirst());
+ assertEquals("Brown", user.getName().getLast());
+ }
+
+ // should even work with no setter
+ public void testBeanMergingWithoutSetter() throws Exception
+ {
+ NoSetterConfig config = MAPPER.readValue(aposToQuotes("{'value':{'b':99}}"),
+ NoSetterConfig.class);
+ assertEquals(99, config._value.b);
+ assertEquals(1, config._value.a);
+ }
+
+ /*
+ /********************************************************
+ /* Test methods, as array
+ /********************************************************
+ */
+
+ public void testBeanAsArrayMerging() throws Exception
+ {
+ ABAsArray input = new ABAsArray();
+ input.a = 4;
+ input.b = 6;
+
+ assertSame(input, MAPPER.readerForUpdating(input)
+ .readValue("[1, 3]"));
+ assertEquals(1, input.a);
+ assertEquals(3, input.b);
+
+ // then with one too few
+ assertSame(input, MAPPER.readerForUpdating(input)
+ .readValue("[9]"));
+ assertEquals(9, input.a);
+ assertEquals(3, input.b);
+
+ // and finally with extra, failing
+ try {
+ MAPPER.readerForUpdating(input)
+ .readValue("[9, 8, 14]");
+ fail("Should not pass");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "expected at most 2 properties");
+ }
+
+ try {
+ MAPPER.readerForUpdating(input)
+ .readValue("\"blob\"");
+ fail("Should not pass");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Can not deserialize");
+ verifyException(e, "from non-Array representation");
+ }
+ }
+
+ /*
+ /********************************************************
+ /* Test methods, reference types
+ /********************************************************
+ */
+
+ public void testReferenceMerging() throws Exception
+ {
+ MergedReference result = MAPPER.readValue(aposToQuotes("{'value':'override'}"),
+ MergedReference.class);
+ assertEquals("override", result.value.get());
+ }
+
+ /*
+ /********************************************************
+ /* Test methods, failure checking
+ /********************************************************
+ */
+
+ public void testInvalidPropertyMerge() throws Exception
+ {
+ ObjectMapper mapper = newObjectMapper()
+ .disable(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE);
+
+ try {
+ mapper.readValue("{\"value\":3}", CantMergeInts.class);
+ fail("Should not pass");
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "can not be merged");
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/creators/TestValueUpdate.java b/src/test/java/com/fasterxml/jackson/databind/deser/merge/UpdateValueTest.java
similarity index 75%
rename from src/test/java/com/fasterxml/jackson/databind/creators/TestValueUpdate.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/merge/UpdateValueTest.java
index 759f618..ce38f86 100644
--- a/src/test/java/com/fasterxml/jackson/databind/creators/TestValueUpdate.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/merge/UpdateValueTest.java
@@ -1,11 +1,11 @@
-package com.fasterxml.jackson.databind.creators;
+package com.fasterxml.jackson.databind.deser.merge;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.*;
-public class TestValueUpdate extends BaseMapTest
+public class UpdateValueTest extends BaseMapTest
{
static class Bean
{
@@ -36,7 +36,7 @@
}
}
- private final ObjectMapper MAPPER = new ObjectMapper();
+ private final ObjectMapper MAPPER = newObjectMapper();
// [databind#318] (and Scala module issue #83]
public void testValueUpdateWithCreator() throws Exception
@@ -50,8 +50,11 @@
public void testValueUpdateOther() throws Exception
{
Bean bean = new Bean("abc", "def");
- ObjectReader r = MAPPER.reader().withValueToUpdate(bean);
+ ObjectReader r = MAPPER.readerFor(Bean.class).withValueToUpdate(bean);
// but, changed our minds, no update
r = r.withValueToUpdate(null);
+ // should be safe to read regardless
+ Bean result = r.readValue(aposToQuotes("{'a':'x'}"));
+ assertNotNull(result);
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/exc/BasicExceptionTest.java b/src/test/java/com/fasterxml/jackson/databind/exc/BasicExceptionTest.java
new file mode 100644
index 0000000..3848332
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/exc/BasicExceptionTest.java
@@ -0,0 +1,114 @@
+package com.fasterxml.jackson.databind.exc;
+
+import java.io.StringWriter;
+import java.util.Collection;
+import java.util.Collections;
+
+import com.fasterxml.jackson.core.*;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+
+public class BasicExceptionTest extends BaseMapTest
+{
+ final ObjectMapper MAPPER = new ObjectMapper();
+ final JsonFactory JSON_F = MAPPER.getFactory();
+
+ public void testBadDefinition() throws Exception
+ {
+ JavaType t = TypeFactory.defaultInstance().constructType(String.class);
+ JsonParser p = JSON_F.createParser("[]");
+ InvalidDefinitionException e = new InvalidDefinitionException(p,
+ "Testing", t);
+ assertEquals("Testing", e.getOriginalMessage());
+ assertEquals(String.class, e.getType().getRawClass());
+ assertNull(e.getBeanDescription());
+ assertNull(e.getProperty());
+ assertSame(p, e.getProcessor());
+ p.close();
+
+ // and via factory method:
+ BeanDescription beanDef = MAPPER.getSerializationConfig().introspectClassAnnotations(getClass());
+ e = InvalidDefinitionException.from(p, "Testing",
+ beanDef, (BeanPropertyDefinition) null);
+ assertEquals(beanDef.getType(), e.getType());
+ assertNotNull(e);
+
+ // and the other constructor too
+ JsonGenerator g = JSON_F.createGenerator(new StringWriter());
+ e = new InvalidDefinitionException(p,
+ "Testing", t);
+ assertEquals("Testing", e.getOriginalMessage());
+ assertEquals(String.class, e.getType().getRawClass());
+
+ // and factory
+ e = InvalidDefinitionException.from(g, "Testing",
+ beanDef, (BeanPropertyDefinition) null);
+ assertEquals(beanDef.getType(), e.getType());
+ assertNotNull(e);
+
+ g.close();
+ }
+
+ @SuppressWarnings("deprecation")
+ public void testInvalidFormat() throws Exception
+ {
+ // deprecated methods should still work:
+ InvalidFormatException e = new InvalidFormatException("Testing", Boolean.TRUE,
+ String.class);
+ assertSame(Boolean.TRUE, e.getValue());
+ assertNull(e.getProcessor());
+ assertNotNull(e);
+
+ e = new InvalidFormatException("Testing", JsonLocation.NA,
+ Boolean.TRUE, String.class);
+ assertSame(Boolean.TRUE, e.getValue());
+ assertNull(e.getProcessor());
+ assertNotNull(e);
+ }
+
+ public void testIgnoredProperty() throws Exception
+ {
+ // first just construct valid instance with some variations
+ JsonParser p = JSON_F.createParser("{ }");
+ IgnoredPropertyException e = IgnoredPropertyException.from(p,
+ this, // to get class from
+ "testProp", Collections.<Object>singletonList("x"));
+ assertNotNull(e);
+
+ e = IgnoredPropertyException.from(p,
+ getClass(),
+ "testProp", null);
+ assertNotNull(e);
+ assertNull(e.getKnownPropertyIds());
+ p.close();
+
+ // also, verify failure if null passed for "value"
+ try {
+ IgnoredPropertyException.from(p, null,
+ "testProp", Collections.<Object>singletonList("x"));
+ fail("Should not pass");
+ } catch (NullPointerException e2) {
+ }
+ }
+
+ public void testUnrecognizedProperty() throws Exception
+ {
+ JsonParser p = JSON_F.createParser("{ }");
+ UnrecognizedPropertyException e = UnrecognizedPropertyException.from(p, this,
+ "testProp", Collections.<Object>singletonList("y"));
+ assertNotNull(e);
+ assertEquals(getClass(), e.getReferringClass());
+ Collection<Object> ids = e.getKnownPropertyIds();
+ assertNotNull(ids);
+ assertEquals(1, ids.size());
+ assertTrue(ids.contains("y"));
+
+ e = UnrecognizedPropertyException.from(p, getClass(),
+ "testProp", Collections.<Object>singletonList("y"));
+
+ assertEquals(getClass(), e.getReferringClass());
+ p.close();
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionHandling.java b/src/test/java/com/fasterxml/jackson/databind/exc/DeserExceptionTypeTest.java
similarity index 61%
rename from src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionHandling.java
rename to src/test/java/com/fasterxml/jackson/databind/exc/DeserExceptionTypeTest.java
index 00c88ec..4378623 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionHandling.java
+++ b/src/test/java/com/fasterxml/jackson/databind/exc/DeserExceptionTypeTest.java
@@ -1,34 +1,47 @@
-package com.fasterxml.jackson.databind.deser.exc;
+package com.fasterxml.jackson.databind.exc;
import java.io.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
/**
* Unit test for verifying that exceptions are properly handled (caught,
- * re-thrown or wrapped, depending)
- * with Object deserialization.
+ * re-thrown or wrapped, depending) with Object deserialization,
+ * including using concrete subtypes of {@link JsonMappingException}
+ * (or, for low-level parsing, {@link JsonParseException}).
*/
-public class TestExceptionHandling
+public class DeserExceptionTypeTest
extends BaseMapTest
{
static class Bean {
public String propX;
}
+ // Class that has no applicable creators and thus can not be instantiated;
+ // definition problem
+ static class NoCreatorsBean {
+ public int x;
+
+ // Constructor that is not detectable as Creator
+ public NoCreatorsBean(boolean foo, int foo2) { }
+ }
+
/*
/**********************************************************
/* Test methods
/**********************************************************
*/
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
public void testHandlingOfUnrecognized() throws Exception
{
UnrecognizedPropertyException exc = null;
try {
- new ObjectMapper().readValue("{\"bar\":3}", Bean.class);
+ MAPPER.readValue("{\"bar\":3}", Bean.class);
} catch (UnrecognizedPropertyException e) {
exc = e;
}
@@ -48,12 +61,11 @@
*/
public void testExceptionWithEmpty() throws Exception
{
- ObjectMapper mapper = new ObjectMapper();
try {
- Object result = mapper.readValue(" ", Object.class);
+ Object result = MAPPER.readValue(" ", Object.class);
fail("Expected an exception, but got result value: "+result);
} catch (Exception e) {
- verifyException(e, JsonMappingException.class, "No content");
+ verifyException(e, MismatchedInputException.class, "No content");
}
}
@@ -62,12 +74,10 @@
throws Exception
{
BrokenStringReader r = new BrokenStringReader("[ 1, ", "TEST");
- JsonFactory f = new JsonFactory();
- JsonParser jp = f.createParser(r);
- ObjectMapper mapper = new ObjectMapper();
+ JsonParser p = MAPPER.getFactory().createParser(r);
try {
@SuppressWarnings("unused")
- Object ob = mapper.readValue(jp, Object.class);
+ Object ob = MAPPER.readValue(p, Object.class);
fail("Should have gotten an exception");
} catch (IOException e) {
/* For "bona fide" IO problems (due to low-level problem,
@@ -77,30 +87,37 @@
}
}
- public void testExceptionWithEOF()
- throws Exception
+ public void testExceptionWithEOF() throws Exception
{
- StringReader r = new StringReader(" 3");
- JsonFactory f = new JsonFactory();
- JsonParser jp = f.createParser(r);
- ObjectMapper mapper = new ObjectMapper();
+ JsonParser p = MAPPER.getFactory().createParser(" 3");
- Integer I = mapper.readValue(jp, Integer.class);
+ Integer I = MAPPER.readValue(p, Integer.class);
assertEquals(3, I.intValue());
// and then end-of-input...
try {
- I = mapper.readValue(jp, Integer.class);
+ I = MAPPER.readValue(p, Integer.class);
fail("Should have gotten an exception");
} catch (IOException e) {
- verifyException(e, JsonMappingException.class, "No content");
+ verifyException(e, MismatchedInputException.class, "No content");
}
// also: should have no current token after end-of-input
- JsonToken t = jp.getCurrentToken();
+ JsonToken t = p.getCurrentToken();
if (t != null) {
fail("Expected current token to be null after end-of-stream, was: "+t);
}
- jp.close();
+ p.close();
+ }
+
+ // [databind#1414]
+ public void testExceptionForNoCreators() throws Exception
+ {
+ try {
+ NoCreatorsBean b = MAPPER.readValue("{}", NoCreatorsBean.class);
+ fail("Should not succeed, got: "+b);
+ } catch (JsonMappingException e) {
+ verifyException(e, InvalidDefinitionException.class, "no Creators");
+ }
}
/*
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/exc/ExceptionPathTest.java b/src/test/java/com/fasterxml/jackson/databind/exc/ExceptionPathTest.java
similarity index 95%
rename from src/test/java/com/fasterxml/jackson/databind/deser/exc/ExceptionPathTest.java
rename to src/test/java/com/fasterxml/jackson/databind/exc/ExceptionPathTest.java
index c4a87bb..ae70ad3 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/exc/ExceptionPathTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/exc/ExceptionPathTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser.exc;
+package com.fasterxml.jackson.databind.exc;
import com.fasterxml.jackson.annotation.*;
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionSerialization.java b/src/test/java/com/fasterxml/jackson/databind/exc/ExceptionSerializationTest.java
similarity index 85%
rename from src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionSerialization.java
rename to src/test/java/com/fasterxml/jackson/databind/exc/ExceptionSerializationTest.java
index 07fc70a..0ab9a6b 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionSerialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/exc/ExceptionSerializationTest.java
@@ -1,15 +1,16 @@
-package com.fasterxml.jackson.databind.deser.exc;
+package com.fasterxml.jackson.databind.exc;
import java.io.IOException;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
/**
* Unit tests for verifying that simple exceptions can be serialized.
*/
-public class TestExceptionSerialization
+public class ExceptionSerializationTest
extends BaseMapTest
{
@SuppressWarnings("serial")
@@ -42,7 +43,7 @@
*/
private final ObjectMapper MAPPER = new ObjectMapper();
-
+
public void testSimple() throws Exception
{
String TEST = "test exception";
@@ -66,6 +67,16 @@
}
}
+ // to double-check [databind#1413]
+ public void testSimpleOther() throws Exception
+ {
+ JsonParser p = MAPPER.getFactory().createParser("{ }");
+ InvalidFormatException exc = InvalidFormatException.from(p, "Test", getClass(), String.class);
+ String json = MAPPER.writeValueAsString(exc);
+ p.close();
+ assertNotNull(json);
+ }
+
// for [databind#877]
@SuppressWarnings("unchecked")
public void testIgnorals() throws Exception
@@ -109,15 +120,15 @@
MAPPER.readValue( "{ \"val\": \"foo\" }", NoSerdeConstructor.class );
fail("Should not pass");
} catch (JsonMappingException e0) {
- verifyException(e0, "no suitable constructor");
+ verifyException(e0, "can not deserialize from Object");
e = e0;
}
// but should be able to serialize new exception we got
String json = MAPPER.writeValueAsString(e);
JsonNode root = MAPPER.readTree(json);
String msg = root.path("message").asText();
- String MATCH = "no suitable constructor";
- if (!msg.contains(MATCH)) {
+ String MATCH = "can not construct instance";
+ if (!msg.toLowerCase().contains(MATCH)) {
fail("Exception should contain '"+MATCH+"', does not: '"+msg+"'");
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionDeserialization.java
similarity index 98%
rename from src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionDeserialization.java
rename to src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionDeserialization.java
index b3fe433..3f3c8b8 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionDeserialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionDeserialization.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser.exc;
+package com.fasterxml.jackson.databind.exc;
import java.io.IOException;
import java.util.*;
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionHandlingWithDefaultDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionHandlingWithDefaultDeserialization.java
similarity index 96%
rename from src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionHandlingWithDefaultDeserialization.java
rename to src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionHandlingWithDefaultDeserialization.java
index 3888722..928b083 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionHandlingWithDefaultDeserialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionHandlingWithDefaultDeserialization.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser.exc;
+package com.fasterxml.jackson.databind.exc;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JsonMappingException;
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionHandlingWithJsonCreatorDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionHandlingWithJsonCreatorDeserialization.java
similarity index 96%
rename from src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionHandlingWithJsonCreatorDeserialization.java
rename to src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionHandlingWithJsonCreatorDeserialization.java
index 8065bc4..3797c80 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionHandlingWithJsonCreatorDeserialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionHandlingWithJsonCreatorDeserialization.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser.exc;
+package com.fasterxml.jackson.databind.exc;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionsDuringWriting.java b/src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionsDuringWriting.java
similarity index 98%
rename from src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionsDuringWriting.java
rename to src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionsDuringWriting.java
index df51963..6c96343 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/exc/TestExceptionsDuringWriting.java
+++ b/src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionsDuringWriting.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser.exc;
+package com.fasterxml.jackson.databind.exc;
import java.io.*;
import java.util.*;
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/IgnorePropertiesDeser1575Test.java b/src/test/java/com/fasterxml/jackson/databind/filter/IgnorePropertiesDeser1575Test.java
deleted file mode 100644
index e35e09b..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/filter/IgnorePropertiesDeser1575Test.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package com.fasterxml.jackson.databind.filter;
-
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
-import com.fasterxml.jackson.annotation.JsonProperty;
-
-import com.fasterxml.jackson.databind.*;
-
-public class IgnorePropertiesDeser1575Test extends BaseMapTest
-{
- static class Person {
- public String name;
-
- @JsonProperty("person_z") // renaming this to person_p works
- @JsonIgnoreProperties({"person_z"}) // renaming this to person_p works
-// public Set<Person> personZ;
- public Person personZ;
- }
-
- public void testIgnorePropDeser1575() throws Exception
- {
- String st = aposToQuotes("{ 'name': 'admin',\n"
-// + " 'person_z': [ { 'name': 'admin' } ]"
- + " 'person_z': { 'name': 'admin' }"
- + "}");
-
- ObjectMapper mapper = new ObjectMapper();
- Person result = mapper.readValue(st, Person.class);
- assertEquals("admin", result.name);
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/MapInclusionTest.java b/src/test/java/com/fasterxml/jackson/databind/filter/MapInclusionTest.java
deleted file mode 100644
index a5ccc57..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/filter/MapInclusionTest.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package com.fasterxml.jackson.databind.filter;
-
-import java.io.IOException;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.databind.*;
-
-public class MapInclusionTest extends BaseMapTest
-{
- static class NoEmptiesMapContainer {
- @JsonInclude(value=JsonInclude.Include.NON_EMPTY,
- content=JsonInclude.Include.NON_EMPTY)
- public Map<String,String> stuff = new LinkedHashMap<String,String>();
-
- public NoEmptiesMapContainer add(String key, String value) {
- stuff.put(key, value);
- return this;
- }
- }
-
- /*
- /**********************************************************
- /* Test methods
- /**********************************************************
- */
-
- final private ObjectMapper MAPPER = objectMapper();
-
- // [databind#588]
- public void testNonNullValueMapViaProp() throws IOException
- {
- String json = MAPPER.writeValueAsString(new NoEmptiesMapContainer()
- .add("a", null)
- .add("b", ""));
- assertEquals(aposToQuotes("{}"), json);
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/format/BooleanFormatTest.java b/src/test/java/com/fasterxml/jackson/databind/format/BooleanFormatTest.java
new file mode 100644
index 0000000..5794cf2
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/format/BooleanFormatTest.java
@@ -0,0 +1,71 @@
+package com.fasterxml.jackson.databind.format;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+// [databind#1480]
+public class BooleanFormatTest extends BaseMapTest
+{
+ @JsonPropertyOrder({ "b1", "b2", "b3" })
+ static class BeanWithBoolean
+ {
+ @JsonFormat(shape=JsonFormat.Shape.NUMBER)
+ public boolean b1;
+
+ @JsonFormat(shape=JsonFormat.Shape.NUMBER)
+ public Boolean b2;
+
+ public boolean b3;
+
+ public BeanWithBoolean() { }
+ public BeanWithBoolean(boolean b1, Boolean b2, boolean b3) {
+ this.b1 = b1;
+ this.b2 = b2;
+ this.b3 = b3;
+ }
+ }
+
+ /**
+ * Simple wrapper around boolean types, usually to test value
+ * conversions or wrapping
+ */
+ protected static class BooleanWrapper {
+ public Boolean b;
+
+ public BooleanWrapper() { }
+ public BooleanWrapper(Boolean value) { b = value; }
+ }
+
+ static class AltBoolean extends BooleanWrapper
+ {
+ public AltBoolean() { }
+ public AltBoolean(Boolean b) { super(b); }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final static ObjectMapper MAPPER = newObjectMapper();
+
+ public void testShapeViaDefaults() throws Exception
+ {
+ assertEquals(aposToQuotes("{'b':true}"),
+ MAPPER.writeValueAsString(new BooleanWrapper(true)));
+ ObjectMapper m = newObjectMapper();
+ m.configOverride(Boolean.class)
+ .setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));
+ assertEquals(aposToQuotes("{'b':1}"),
+ m.writeValueAsString(new BooleanWrapper(true)));
+ }
+
+ public void testShapeOnProperty() throws Exception
+ {
+ assertEquals(aposToQuotes("{'b1':1,'b2':0,'b3':true}"),
+ MAPPER.writeValueAsString(new BeanWithBoolean(true, false, true)));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/TestFormatForCollections.java b/src/test/java/com/fasterxml/jackson/databind/format/ColletionFormatShapeTest.java
similarity index 85%
rename from src/test/java/com/fasterxml/jackson/databind/struct/TestFormatForCollections.java
rename to src/test/java/com/fasterxml/jackson/databind/format/ColletionFormatShapeTest.java
index bd308b3..51a3878 100644
--- a/src/test/java/com/fasterxml/jackson/databind/struct/TestFormatForCollections.java
+++ b/src/test/java/com/fasterxml/jackson/databind/format/ColletionFormatShapeTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.struct;
+package com.fasterxml.jackson.databind.format;
import java.util.ArrayList;
import java.util.List;
@@ -8,7 +8,7 @@
import com.fasterxml.jackson.databind.*;
-public class TestFormatForCollections extends BaseMapTest
+public class ColletionFormatShapeTest extends BaseMapTest
{
// [databind#40]: Allow serialization 'as POJO' (resulting in JSON Object)
@JsonPropertyOrder({ "size", "value" })
@@ -40,11 +40,9 @@
/**********************************************************
*/
- private final static ObjectMapper MAPPER = new ObjectMapper();
+ private final static ObjectMapper MAPPER = newObjectMapper();
-
- // [Issue#40]
- public void testListAsObject() throws Exception
+ public void testListAsObjectRoundtrip() throws Exception
{
// First, serialize a "POJO-List"
CollectionAsPOJO list = new CollectionAsPOJO();
diff --git a/src/test/java/com/fasterxml/jackson/databind/format/DateFormatTest.java b/src/test/java/com/fasterxml/jackson/databind/format/DateFormatTest.java
index 19b97f7..f94bd02 100644
--- a/src/test/java/com/fasterxml/jackson/databind/format/DateFormatTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/format/DateFormatTest.java
@@ -18,7 +18,7 @@
public void testTypeDefaults() throws Exception
{
- ObjectMapper mapper = new ObjectMapper();
+ ObjectMapper mapper = newObjectMapper();
mapper.configOverride(Date.class)
.setFormat(JsonFormat.Value.forPattern("yyyy.dd.MM"));
// First serialize, should result in this (in UTC):
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/EnumFormatShapeTest.java b/src/test/java/com/fasterxml/jackson/databind/format/EnumFormatShapeTest.java
similarity index 96%
rename from src/test/java/com/fasterxml/jackson/databind/struct/EnumFormatShapeTest.java
rename to src/test/java/com/fasterxml/jackson/databind/format/EnumFormatShapeTest.java
index 8e7f13b..4e11a8e 100644
--- a/src/test/java/com/fasterxml/jackson/databind/struct/EnumFormatShapeTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/format/EnumFormatShapeTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.struct;
+package com.fasterxml.jackson.databind.format;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
@@ -76,7 +76,7 @@
/**********************************************************
*/
- private final ObjectMapper MAPPER = new ObjectMapper();
+ private final ObjectMapper MAPPER = newObjectMapper();
// Tests for JsonFormat.shape
diff --git a/src/test/java/com/fasterxml/jackson/databind/format/MapEntryFormatTest.java b/src/test/java/com/fasterxml/jackson/databind/format/MapEntryFormatTest.java
new file mode 100644
index 0000000..81c21af
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/format/MapEntryFormatTest.java
@@ -0,0 +1,162 @@
+package com.fasterxml.jackson.databind.format;
+
+import java.util.*;
+import java.util.concurrent.atomic.AtomicReference;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.*;
+
+public class MapEntryFormatTest extends BaseMapTest
+{
+ static class BeanWithMapEntry {
+ // would work with any other shape than OBJECT, or without annotation:
+ @JsonFormat(shape=JsonFormat.Shape.NATURAL)
+ public Map.Entry<String,String> entry;
+
+ protected BeanWithMapEntry() { }
+ public BeanWithMapEntry(String key, String value) {
+ Map<String,String> map = new HashMap<>();
+ map.put(key, value);
+ entry = map.entrySet().iterator().next();
+ }
+ }
+
+ @JsonFormat(shape=JsonFormat.Shape.OBJECT)
+ static class MapEntryAsObject implements Map.Entry<String,String> {
+ protected String key, value;
+
+ protected MapEntryAsObject() { }
+ public MapEntryAsObject(String k, String v) {
+ key = k;
+ value = v;
+ }
+
+ @Override
+ public String getKey() {
+ return key;
+ }
+
+ @Override
+ public String getValue() {
+ return value;
+ }
+
+ @Override
+ public String setValue(String v) {
+ value = v;
+ return v; // wrong, whatever
+ }
+ }
+
+ static class EntryWithNullWrapper {
+ @JsonInclude(value=JsonInclude.Include.NON_EMPTY,
+ content=JsonInclude.Include.NON_NULL)
+ public Map.Entry<String,String> entry;
+
+ public EntryWithNullWrapper(String key, String value) {
+ HashMap<String,String> map = new HashMap<>();
+ map.put(key, value);
+ entry = map.entrySet().iterator().next();
+ }
+ }
+
+ static class EntryWithDefaultWrapper {
+ @JsonInclude(value=JsonInclude.Include.NON_EMPTY,
+ content=JsonInclude.Include.NON_DEFAULT)
+ public Map.Entry<String,String> entry;
+
+ public EntryWithDefaultWrapper(String key, String value) {
+ HashMap<String,String> map = new HashMap<>();
+ map.put(key, value);
+ entry = map.entrySet().iterator().next();
+ }
+ }
+
+ static class EntryWithNonAbsentWrapper {
+ @JsonInclude(value=JsonInclude.Include.NON_EMPTY,
+ content=JsonInclude.Include.NON_ABSENT)
+ public Map.Entry<String,AtomicReference<String>> entry;
+
+ public EntryWithNonAbsentWrapper(String key, String value) {
+ HashMap<String,AtomicReference<String>> map = new HashMap<>();
+ map.put(key, new AtomicReference<String>(value));
+ entry = map.entrySet().iterator().next();
+ }
+ }
+
+ static class EmptyEntryWrapper {
+ @JsonInclude(value=JsonInclude.Include.NON_EMPTY,
+ content=JsonInclude.Include.NON_EMPTY)
+ public Map.Entry<String,String> entry;
+
+ public EmptyEntryWrapper(String key, String value) {
+ HashMap<String,String> map = new HashMap<>();
+ map.put(key, value);
+ entry = map.entrySet().iterator().next();
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper();
+
+ public void testAsNaturalRoundtrip() throws Exception
+ {
+ BeanWithMapEntry input = new BeanWithMapEntry("foo" ,"bar");
+ String json = MAPPER.writeValueAsString(input);
+ assertEquals(aposToQuotes("{'entry':{'foo':'bar'}}"), json);
+ BeanWithMapEntry result = MAPPER.readValue(json, BeanWithMapEntry.class);
+ assertEquals("foo", result.entry.getKey());
+ assertEquals("bar", result.entry.getValue());
+ }
+ // should work via class annotation
+ public void testAsObjectRoundtrip() throws Exception
+ {
+ MapEntryAsObject input = new MapEntryAsObject("foo" ,"bar");
+ String json = MAPPER.writeValueAsString(input);
+ assertEquals(aposToQuotes("{'key':'foo','value':'bar'}"), json);
+
+ // 16-Oct-2016, tatu: Happens to work by default because it's NOT basic
+ // `Map.Entry` but subtype.
+
+ MapEntryAsObject result = MAPPER.readValue(json, MapEntryAsObject.class);
+ assertEquals("foo", result.getKey());
+ assertEquals("bar", result.getValue());
+ }
+
+ public void testInclusion() throws Exception
+ {
+ assertEquals(aposToQuotes("{'entry':{'a':'b'}}"),
+ MAPPER.writeValueAsString(new EmptyEntryWrapper("a", "b")));
+ assertEquals(aposToQuotes("{'entry':{'a':'b'}}"),
+ MAPPER.writeValueAsString(new EntryWithDefaultWrapper("a", "b")));
+ assertEquals(aposToQuotes("{'entry':{'a':'b'}}"),
+ MAPPER.writeValueAsString(new EntryWithNullWrapper("a", "b")));
+
+ assertEquals(aposToQuotes("{}"),
+ MAPPER.writeValueAsString(new EmptyEntryWrapper("a", "")));
+ assertEquals(aposToQuotes("{}"),
+ MAPPER.writeValueAsString(new EntryWithDefaultWrapper("a", "")));
+ assertEquals(aposToQuotes("{'entry':{'a':''}}"),
+ MAPPER.writeValueAsString(new EntryWithNullWrapper("a", "")));
+ assertEquals(aposToQuotes("{}"),
+ MAPPER.writeValueAsString(new EntryWithNullWrapper("a", null)));
+ }
+
+ public void testInclusionWithReference() throws Exception
+ {
+ assertEquals(aposToQuotes("{'entry':{'a':'b'}}"),
+ MAPPER.writeValueAsString(new EntryWithNonAbsentWrapper("a", "b")));
+ // empty String not excluded since reference is not absent, just points to empty
+ // (so would need 3rd level inclusion definition)
+ assertEquals(aposToQuotes("{'entry':{'a':''}}"),
+ MAPPER.writeValueAsString(new EntryWithNonAbsentWrapper("a", "")));
+ assertEquals(aposToQuotes("{}"),
+ MAPPER.writeValueAsString(new EntryWithNonAbsentWrapper("a", null)));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/format/MapFormatShapeTest.java b/src/test/java/com/fasterxml/jackson/databind/format/MapFormatShapeTest.java
new file mode 100644
index 0000000..a475d40
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/format/MapFormatShapeTest.java
@@ -0,0 +1,205 @@
+package com.fasterxml.jackson.databind.format;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.fasterxml.jackson.databind.*;
+
+@SuppressWarnings("serial")
+public class MapFormatShapeTest extends BaseMapTest
+{
+ @JsonPropertyOrder({ "extra" })
+ static class Map476Base extends LinkedHashMap<String,Integer> {
+ public int extra = 13;
+ }
+
+ @JsonFormat(shape=JsonFormat.Shape.OBJECT)
+ static class Map476AsPOJO extends Map476Base { }
+
+ @JsonPropertyOrder({ "a", "b", "c" })
+ @JsonInclude(JsonInclude.Include.NON_NULL)
+ static class Bean476Container
+ {
+ public Map476AsPOJO a;
+ public Map476Base b;
+ @JsonFormat(shape=JsonFormat.Shape.OBJECT)
+ public Map476Base c;
+
+ public Bean476Container(int forA, int forB, int forC) {
+ if (forA != 0) {
+ a = new Map476AsPOJO();
+ a.put("value", forA);
+ }
+ if (forB != 0) {
+ b = new Map476Base();
+ b.put("value", forB);
+ }
+ if (forC != 0) {
+ c = new Map476Base();
+ c.put("value", forC);
+ }
+ }
+ }
+
+ static class Bean476Override
+ {
+ @JsonFormat(shape=JsonFormat.Shape.NATURAL)
+ public Map476AsPOJO stuff;
+
+ public Bean476Override(int value) {
+ stuff = new Map476AsPOJO();
+ stuff.put("value", value);
+ }
+ }
+
+ // from [databind#1540]
+ @JsonFormat(shape = JsonFormat.Shape.OBJECT)
+ @JsonPropertyOrder({ "property", "map" })
+ static class Map1540Implementation implements Map<Integer, Integer> {
+ public int property;
+ public Map<Integer, Integer> map = new HashMap<>();
+
+ public Map<Integer, Integer> getMap() {
+ return map;
+ }
+
+ public void setMap(Map<Integer, Integer> map) {
+ this.map = map;
+ }
+
+ @Override
+ public Integer put(Integer key, Integer value) {
+ return map.put(key, value);
+ }
+
+ @Override
+ public int size() {
+ return map.size();
+ }
+
+ @JsonIgnore
+ @Override
+ public boolean isEmpty() {
+ return map.isEmpty();
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return map.containsKey(key);
+ }
+
+ @Override
+ public boolean containsValue(Object value) {
+ return map.containsValue(value);
+ }
+
+ @Override
+ public Integer get(Object key) {
+ return map.get(key);
+ }
+
+ @Override
+ public Integer remove(Object key) {
+ return map.remove(key);
+ }
+
+ @Override
+ public void putAll(Map<? extends Integer, ? extends Integer> m) {
+ map.putAll(m);
+ }
+
+ @Override
+ public void clear() {
+ map.clear();
+ }
+
+ @Override
+ public Set<Integer> keySet() {
+ return map.keySet();
+ }
+
+ @Override
+ public Collection<Integer> values() {
+ return map.values();
+ }
+
+ @Override
+ public Set<java.util.Map.Entry<Integer, Integer>> entrySet() {
+ return map.entrySet();
+ }
+ }
+
+
+ /*
+ /**********************************************************
+ /* Test methods, serialization
+ /**********************************************************
+ */
+
+ final private ObjectMapper MAPPER = objectMapper();
+
+ // for [databind#476]: Maps as POJOs
+ public void testSerializeAsPOJOViaClass() throws Exception
+ {
+ String result = MAPPER.writeValueAsString(new Bean476Container(1,2,0));
+ assertEquals(aposToQuotes("{'a':{'extra':13,'empty':false},'b':{'value':2}}"),
+ result);
+ }
+
+ // Can't yet use per-property overrides at all, see [databind#1419]
+
+ /*
+ public void testSerializeAsPOJOViaProperty() throws Exception
+ {
+ String result = MAPPER.writeValueAsString(new Bean476Container(1,0,3));
+ assertEquals(aposToQuotes("{'a':{'extra':13,'empty':false},'c':{'empty':false,'value':3}}"),
+ result);
+ }
+
+ public void testSerializeNaturalViaOverride() throws Exception
+ {
+ String result = MAPPER.writeValueAsString(new Bean476Override(123));
+ assertEquals(aposToQuotes("{'stuff':{'value':123}}"),
+ result);
+ }
+ */
+
+ /*
+ /**********************************************************
+ /* Test methods, deserialization/roundtrip
+ /**********************************************************
+ */
+
+ // [databind#1540]
+ public void testRoundTrip() throws Exception
+ {
+ Map1540Implementation input = new Map1540Implementation();
+ input.property = 55;
+ input.put(12, 45);
+ input.put(6, 88);
+
+ String json = MAPPER.writeValueAsString(input);
+
+ assertEquals(aposToQuotes("{'property':55,'map':{'6':88,'12':45}}"), json);
+
+ Map1540Implementation result = MAPPER.readValue(json, Map1540Implementation.class);
+ assertEquals(result.property, input.property);
+ assertEquals(input.getMap(), input.getMap());
+ }
+
+ // [databind#1554]
+ public void testDeserializeAsPOJOViaClass() throws Exception
+ {
+ Map476AsPOJO result = MAPPER.readValue(aposToQuotes("{'extra':42}"),
+ Map476AsPOJO.class);
+ assertEquals(0, result.size());
+ assertEquals(42, result.extra);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/interop/IllegalTypesCheckTest.java b/src/test/java/com/fasterxml/jackson/databind/interop/IllegalTypesCheckTest.java
index 1906ead..620923a 100644
--- a/src/test/java/com/fasterxml/jackson/databind/interop/IllegalTypesCheckTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/interop/IllegalTypesCheckTest.java
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.databind.interop;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
/**
* Test case(s) to guard against handling of types that are illegal to handle
@@ -15,9 +16,10 @@
public void testIssue1599() throws Exception
{
+ final String NASTY_CLASS = "com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl";
final String JSON = aposToQuotes(
"{'id': 124,\n"
-+" 'obj':[ 'com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl',\n"
++" 'obj':[ '"+NASTY_CLASS+"',\n"
+" {\n"
+" 'transletBytecodes' : [ 'AAIAZQ==' ],\n"
+" 'transletName' : 'a.b',\n"
@@ -31,10 +33,13 @@
try {
mapper.readValue(JSON, Bean1599.class);
fail("Should not pass");
- } catch (JsonMappingException e) {
+ } catch (InvalidDefinitionException e) {
verifyException(e, "Illegal type");
verifyException(e, "to deserialize");
verifyException(e, "prevented for security reasons");
+ BeanDescription desc = e.getBeanDescription();
+ assertNotNull(desc);
+ assertEquals(NASTY_CLASS, desc.getBeanClass().getName());
}
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/interop/TestFormatDetection.java b/src/test/java/com/fasterxml/jackson/databind/interop/TestFormatDetection.java
index 18345a6..9f37679 100644
--- a/src/test/java/com/fasterxml/jackson/databind/interop/TestFormatDetection.java
+++ b/src/test/java/com/fasterxml/jackson/databind/interop/TestFormatDetection.java
@@ -23,7 +23,7 @@
/* Test methods
/**********************************************************
*/
-
+
public void testSimpleWithJSON() throws Exception
{
ObjectReader detecting = READER.forType(POJO.class);
@@ -33,6 +33,45 @@
assertEquals(1, pojo.x);
}
+ public void testSequenceWithJSON() throws Exception
+ {
+ ObjectReader detecting = READER.forType(POJO.class);
+ detecting = detecting.withFormatDetection(detecting);
+ MappingIterator<POJO> it = detecting.
+ readValues(utf8Bytes(aposToQuotes("{'x':1}\n{'x':2,'y':5}")));
+
+ assertTrue(it.hasNextValue());
+ POJO pojo = it.nextValue();
+ assertEquals(1, pojo.x);
+
+ assertTrue(it.hasNextValue());
+ pojo = it.nextValue();
+ assertEquals(2, pojo.x);
+ assertEquals(5, pojo.y);
+
+ assertFalse(it.hasNextValue());
+ it.close();
+
+ // And again with nodes
+ ObjectReader r2 = READER.forType(JsonNode.class);
+ r2 = r2.withFormatDetection(r2);
+ MappingIterator<JsonNode> nodes = r2.
+ readValues(utf8Bytes(aposToQuotes("{'x':1}\n{'x':2,'y':5}")));
+
+ assertTrue(nodes.hasNextValue());
+ JsonNode n = nodes.nextValue();
+ assertEquals(1, n.size());
+
+ assertTrue(nodes.hasNextValue());
+ n = nodes.nextValue();
+ assertEquals(2, n.size());
+ assertEquals(2, n.path("x").asInt());
+ assertEquals(5, n.path("y").asInt());
+
+ assertFalse(nodes.hasNextValue());
+ nodes.close();
+ }
+
public void testInvalid() throws Exception
{
ObjectReader detecting = READER.forType(POJO.class);
diff --git a/src/test/java/com/fasterxml/jackson/databind/introspect/IntrospectorPairTest.java b/src/test/java/com/fasterxml/jackson/databind/introspect/IntrospectorPairTest.java
index 73a3a42..43e927d 100644
--- a/src/test/java/com/fasterxml/jackson/databind/introspect/IntrospectorPairTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/introspect/IntrospectorPairTest.java
@@ -1,8 +1,21 @@
package com.fasterxml.jackson.databind.introspect;
+import java.lang.annotation.Annotation;
+import java.util.*;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
+
import com.fasterxml.jackson.core.Version;
+
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.cfg.MapperConfig;
+import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
+import com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer;
+import com.fasterxml.jackson.databind.jsontype.NamedType;
+import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
+import com.fasterxml.jackson.databind.ser.std.StringSerializer;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
// started with [databind#1025] in mind
@SuppressWarnings("serial")
@@ -36,9 +49,405 @@
}
}
+ static class IntrospectorWithHandlers extends AnnotationIntrospector {
+ final Object _deserializer;
+ final Object _serializer;
+
+ public IntrospectorWithHandlers(Object deser, Object ser) {
+ _deserializer = deser;
+ _serializer = ser;
+ }
+
+ @Override
+ public Version version() {
+ return Version.unknownVersion();
+ }
+
+ @Override
+ public Object findDeserializer(Annotated am) {
+ return _deserializer;
+ }
+
+ @Override
+ public Object findSerializer(Annotated am) {
+ return _serializer;
+ }
+ }
+
+ static class IntrospectorWithMap extends AnnotationIntrospector
+ {
+ private final Map<String, Object> values = new HashMap<>();
+
+ private Version version = Version.unknownVersion();
+
+ public IntrospectorWithMap add(String key, Object value) {
+ values.put(key, value);
+ return this;
+ }
+
+ public IntrospectorWithMap version(Version v) {
+ version = v;
+ return this;
+ }
+
+ @Override
+ public Version version() {
+ return version;
+ }
+
+ @Override
+ public JsonInclude.Value findPropertyInclusion(Annotated a) {
+ return JsonInclude.Value.empty()
+ .withContentInclusion(JsonInclude.Include.NON_EMPTY)
+ .withValueInclusion(JsonInclude.Include.USE_DEFAULTS);
+ }
+
+ @Override
+ public boolean isAnnotationBundle(Annotation ann) {
+ return _boolean("isAnnotationBundle");
+ }
+
+ /*
+ /******************************************************
+ /* General class annotations
+ /******************************************************
+ */
+
+ @Override
+ public PropertyName findRootName(AnnotatedClass ac) {
+ return (PropertyName) values.get("findRootName");
+ }
+
+ @Override
+ public JsonIgnoreProperties.Value findPropertyIgnorals(Annotated a) {
+ return (JsonIgnoreProperties.Value) values.get("findPropertyIgnorals");
+ }
+
+ @Override
+ public Boolean isIgnorableType(AnnotatedClass ac) {
+ return (Boolean) values.get("isIgnorableType");
+ }
+
+ @Override
+ public Object findFilterId(Annotated ann) {
+ return (Object) values.get("findFilterId");
+ }
+
+ @Override
+ public Object findNamingStrategy(AnnotatedClass ac) {
+ return (Object) values.get("findNamingStrategy");
+ }
+
+ @Override
+ public String findClassDescription(AnnotatedClass ac) {
+ return (String) values.get("findClassDescription");
+ }
+
+ /*
+ /******************************************************
+ /* Property auto-detection
+ /******************************************************
+ */
+
+ @Override
+ public VisibilityChecker<?> findAutoDetectVisibility(AnnotatedClass ac,
+ VisibilityChecker<?> checker)
+ {
+ VisibilityChecker<?> vc = (VisibilityChecker<?>) values.get("findAutoDetectVisibility");
+ // not really good but:
+ return (vc == null) ? checker : vc;
+ }
+
+ /*
+ /******************************************************
+ /* Type handling
+ /******************************************************
+ */
+
+ @Override
+ public TypeResolverBuilder<?> findTypeResolver(MapperConfig<?> config,
+ AnnotatedClass ac, JavaType baseType)
+ {
+ return (TypeResolverBuilder<?>) values.get("findTypeResolver");
+ }
+
+ @Override
+ public TypeResolverBuilder<?> findPropertyTypeResolver(MapperConfig<?> config,
+ AnnotatedMember am, JavaType baseType)
+ {
+ return (TypeResolverBuilder<?>) values.get("findPropertyTypeResolver");
+ }
+
+ @Override
+ public TypeResolverBuilder<?> findPropertyContentTypeResolver(MapperConfig<?> config,
+ AnnotatedMember am, JavaType baseType)
+ {
+ return (TypeResolverBuilder<?>) values.get("findPropertyContentTypeResolver");
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public List<NamedType> findSubtypes(Annotated a)
+ {
+ return (List<NamedType>) values.get("findSubtypes");
+ }
+
+ @Override
+ public String findTypeName(AnnotatedClass ac) {
+ return (String) values.get("findTypeName");
+ }
+
+ /*
+ /******************************************************
+ /* Helper methods
+ /******************************************************
+ */
+
+ private boolean _boolean(String key) {
+ Object ob = values.get(key);
+ return Boolean.TRUE.equals(ob);
+ }
+ }
+
/*
/**********************************************************
- /* Test methods
+ /* Test methods, misc
+ /**********************************************************
+ */
+
+ private final IntrospectorWithMap NO_ANNOTATIONS = new IntrospectorWithMap();
+
+ public void testVersion() throws Exception
+ {
+ Version v = new Version(1, 2, 3, null,
+ "com.fasterxml", "IntrospectorPairTest");
+ IntrospectorWithMap withVersion = new IntrospectorWithMap()
+ .version(v);
+ assertEquals(v,
+ new AnnotationIntrospectorPair(withVersion, NO_ANNOTATIONS).version());
+ assertEquals(Version.unknownVersion(),
+ new AnnotationIntrospectorPair(NO_ANNOTATIONS, withVersion).version());
+ }
+
+ public void testAccess() throws Exception
+ {
+ IntrospectorWithMap intr1 = new IntrospectorWithMap();
+ AnnotationIntrospectorPair pair = new AnnotationIntrospectorPair(intr1,
+ NO_ANNOTATIONS);
+ Collection<AnnotationIntrospector> intrs = pair.allIntrospectors();
+ assertEquals(2, intrs.size());
+ Iterator<AnnotationIntrospector> it = intrs.iterator();
+ assertSame(intr1, it.next());
+ assertSame(NO_ANNOTATIONS, it.next());
+ }
+
+ public void testAnnotationBundle() throws Exception
+ {
+ IntrospectorWithMap isBundle = new IntrospectorWithMap()
+ .add("isAnnotationBundle", true);
+ assertTrue(new AnnotationIntrospectorPair(NO_ANNOTATIONS, isBundle)
+ .isAnnotationBundle(null));
+ assertTrue(new AnnotationIntrospectorPair(isBundle, NO_ANNOTATIONS)
+ .isAnnotationBundle(null));
+ assertFalse(new AnnotationIntrospectorPair(NO_ANNOTATIONS, NO_ANNOTATIONS)
+ .isAnnotationBundle(null));
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, general class annotations
+ /**********************************************************
+ */
+
+ public void testFindRootName() throws Exception
+ {
+ PropertyName name = new PropertyName("test");
+ IntrospectorWithMap intr = new IntrospectorWithMap()
+ .add("findRootName", name);
+ assertNull(new AnnotationIntrospectorPair(NO_ANNOTATIONS, NO_ANNOTATIONS).findRootName(null));
+ assertEquals(name, new AnnotationIntrospectorPair(NO_ANNOTATIONS, intr).findRootName(null));
+ assertEquals(name, new AnnotationIntrospectorPair(intr, NO_ANNOTATIONS).findRootName(null));
+ }
+
+ public void testPropertyIgnorals() throws Exception
+ {
+ JsonIgnoreProperties.Value incl = JsonIgnoreProperties.Value.forIgnoredProperties("foo");
+ IntrospectorWithMap intr = new IntrospectorWithMap()
+ .add("findPropertyIgnorals", incl);
+ IntrospectorWithMap intrEmpty = new IntrospectorWithMap()
+ .add("findPropertyIgnorals", JsonIgnoreProperties.Value.empty());
+ assertEquals(JsonIgnoreProperties.Value.empty(),
+ new AnnotationIntrospectorPair(intrEmpty, intrEmpty).findPropertyIgnorals(null));
+ // should actually verify inclusion combining, but there are separate tests for that
+ assertEquals(incl, new AnnotationIntrospectorPair(intrEmpty, intr).findPropertyIgnorals(null));
+ assertEquals(incl, new AnnotationIntrospectorPair(intr, intrEmpty).findPropertyIgnorals(null));
+ }
+
+ public void testIsIgnorableType() throws Exception
+ {
+ IntrospectorWithMap intr1 = new IntrospectorWithMap()
+ .add("isIgnorableType", Boolean.TRUE);
+ IntrospectorWithMap intr2 = new IntrospectorWithMap()
+ .add("isIgnorableType", Boolean.FALSE);
+ assertNull(new AnnotationIntrospectorPair(NO_ANNOTATIONS, NO_ANNOTATIONS).isIgnorableType(null));
+ assertEquals(Boolean.TRUE, new AnnotationIntrospectorPair(intr1, intr2).isIgnorableType(null));
+ assertEquals(Boolean.FALSE, new AnnotationIntrospectorPair(intr2, intr1).isIgnorableType(null));
+ }
+
+ public void testFindFilterId() throws Exception
+ {
+ IntrospectorWithMap intr1 = new IntrospectorWithMap()
+ .add("findFilterId", "a");
+ IntrospectorWithMap intr2 = new IntrospectorWithMap()
+ .add("findFilterId", "b");
+ assertNull(new AnnotationIntrospectorPair(NO_ANNOTATIONS, NO_ANNOTATIONS).findFilterId(null));
+ assertEquals("a", new AnnotationIntrospectorPair(intr1, intr2).findFilterId(null));
+ assertEquals("b", new AnnotationIntrospectorPair(intr2, intr1).findFilterId(null));
+ }
+
+ public void testFindNamingStrategy() throws Exception
+ {
+ // shouldn't be bogus Classes for real use, but works here
+ IntrospectorWithMap intr1 = new IntrospectorWithMap()
+ .add("findNamingStrategy", Integer.class);
+ IntrospectorWithMap intr2 = new IntrospectorWithMap()
+ .add("findNamingStrategy", String.class);
+ assertNull(new AnnotationIntrospectorPair(NO_ANNOTATIONS, NO_ANNOTATIONS).findNamingStrategy(null));
+ assertEquals(Integer.class,
+ new AnnotationIntrospectorPair(intr1, intr2).findNamingStrategy(null));
+ assertEquals(String.class,
+ new AnnotationIntrospectorPair(intr2, intr1).findNamingStrategy(null));
+ }
+
+ public void testFindClassDescription() throws Exception
+ {
+ IntrospectorWithMap intr1 = new IntrospectorWithMap()
+ .add("findClassDescription", "Desc1");
+ IntrospectorWithMap intr2 = new IntrospectorWithMap()
+ .add("findClassDescription", "Desc2");
+ assertNull(new AnnotationIntrospectorPair(NO_ANNOTATIONS, NO_ANNOTATIONS).findClassDescription(null));
+ assertEquals("Desc1",
+ new AnnotationIntrospectorPair(intr1, intr2).findClassDescription(null));
+ assertEquals("Desc2",
+ new AnnotationIntrospectorPair(intr2, intr1).findClassDescription(null));
+ }
+
+ // // // 3 deprecated methods, skip
+
+ /*
+ /**********************************************************
+ /* Test methods, ser/deser
+ /**********************************************************
+ */
+
+ public void testFindSerializer() throws Exception
+ {
+ final JsonSerializer<?> serString = new StringSerializer();
+ final JsonSerializer<?> serToString = ToStringSerializer.instance;
+
+ AnnotationIntrospector intr1 = new IntrospectorWithHandlers(null, serString);
+ AnnotationIntrospector intr2 = new IntrospectorWithHandlers(null, serToString);
+ AnnotationIntrospector nop = AnnotationIntrospector.nopInstance();
+ AnnotationIntrospector nop2 = new IntrospectorWithHandlers(null, JsonSerializer.None.class);
+
+ assertSame(serString,
+ new AnnotationIntrospectorPair(intr1, intr2).findSerializer(null));
+ assertSame(serToString,
+ new AnnotationIntrospectorPair(intr2, intr1).findSerializer(null));
+
+ // also: no-op instance should not block real one, regardless
+ assertSame(serString,
+ new AnnotationIntrospectorPair(nop, intr1).findSerializer(null));
+ assertSame(serString,
+ new AnnotationIntrospectorPair(nop2, intr1).findSerializer(null));
+
+ // nor should no-op result in non-null result
+ assertNull(new AnnotationIntrospectorPair(nop, nop2).findSerializer(null));
+ assertNull(new AnnotationIntrospectorPair(nop2, nop).findSerializer(null));
+ }
+
+ public void testFindDeserializer() throws Exception
+ {
+ final JsonDeserializer<?> deserString = StringDeserializer.instance;
+ final JsonDeserializer<?> deserObject = UntypedObjectDeserializer.Vanilla.std;
+
+ AnnotationIntrospector intr1 = new IntrospectorWithHandlers(deserString, null);
+ AnnotationIntrospector intr2 = new IntrospectorWithHandlers(deserObject, null);
+ AnnotationIntrospector nop = AnnotationIntrospector.nopInstance();
+ AnnotationIntrospector nop2 = new IntrospectorWithHandlers(JsonDeserializer.None.class, null);
+
+ assertSame(deserString,
+ new AnnotationIntrospectorPair(intr1, intr2).findDeserializer(null));
+ assertSame(deserObject,
+ new AnnotationIntrospectorPair(intr2, intr1).findDeserializer(null));
+ // also: no-op instance should not block real one, regardless
+ assertSame(deserString,
+ new AnnotationIntrospectorPair(nop, intr1).findDeserializer(null));
+ assertSame(deserString,
+ new AnnotationIntrospectorPair(nop2, intr1).findDeserializer(null));
+
+ // nor should no-op result in non-null result
+ assertNull(new AnnotationIntrospectorPair(nop, nop2).findDeserializer(null));
+ assertNull(new AnnotationIntrospectorPair(nop2, nop).findDeserializer(null));
+ }
+
+ /*
+ /******************************************************
+ /* Property auto-detection
+ /******************************************************
+ */
+
+ public void testFindAutoDetectVisibility() throws Exception
+ {
+ VisibilityChecker<?> vc = VisibilityChecker.Std.defaultInstance();
+ IntrospectorWithMap intr1 = new IntrospectorWithMap()
+ .add("findAutoDetectVisibility", vc);
+ assertNull(new AnnotationIntrospectorPair(NO_ANNOTATIONS, NO_ANNOTATIONS)
+ .findAutoDetectVisibility(null, null));
+ assertSame(vc, new AnnotationIntrospectorPair(intr1, NO_ANNOTATIONS)
+ .findAutoDetectVisibility(null, null));
+ assertSame(vc, new AnnotationIntrospectorPair(NO_ANNOTATIONS, intr1)
+ .findAutoDetectVisibility(null, null));
+ }
+
+ /*
+ /******************************************************
+ /* Type handling
+ /******************************************************
+ */
+
+ public void testFindTypeResolver() throws Exception
+ {
+ /*
+ TypeResolverBuilder<?> findTypeResolver(MapperConfig<?> config,
+ AnnotatedClass ac, JavaType baseType)
+ return (TypeResolverBuilder<?>) values.get("findTypeResolver");
+ */
+ }
+ public void testFindPropertyTypeResolver() {
+ }
+
+ public void testFindPropertyContentTypeResolver() {
+ }
+
+ public void testFindSubtypes() {
+ }
+
+ public void testFindTypeName() {
+ IntrospectorWithMap intr1 = new IntrospectorWithMap()
+ .add("findTypeName", "type1");
+ IntrospectorWithMap intr2 = new IntrospectorWithMap()
+ .add("findTypeName", "type2");
+ assertNull(new AnnotationIntrospectorPair(NO_ANNOTATIONS, NO_ANNOTATIONS).findTypeName(null));
+ assertEquals("type1",
+ new AnnotationIntrospectorPair(intr1, intr2).findTypeName(null));
+ assertEquals("type2",
+ new AnnotationIntrospectorPair(intr2, intr1).findTypeName(null));
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, others
/**********************************************************
*/
@@ -47,7 +456,7 @@
private final AnnotationIntrospectorPair introPair21
= new AnnotationIntrospectorPair(new Introspector2(), new Introspector1());
-
+
// for [databind#1025]
public void testInclusionMerging() throws Exception
{
diff --git a/src/test/java/com/fasterxml/jackson/databind/introspect/PropertyMetadataTest.java b/src/test/java/com/fasterxml/jackson/databind/introspect/PropertyMetadataTest.java
new file mode 100644
index 0000000..84dcc54
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/introspect/PropertyMetadataTest.java
@@ -0,0 +1,82 @@
+package com.fasterxml.jackson.databind.introspect;
+
+import com.fasterxml.jackson.annotation.Nulls;
+import com.fasterxml.jackson.databind.*;
+
+public class PropertyMetadataTest extends BaseMapTest
+{
+ public void testPropertyName()
+ {
+ PropertyName name = PropertyName.NO_NAME;
+
+ assertFalse(name.hasSimpleName());
+ assertFalse(name.hasNamespace());
+ assertSame(name, name.internSimpleName());
+ assertSame(name, name.withSimpleName(null));
+ assertSame(name, name.withSimpleName(""));
+ assertSame(name, name.withNamespace(null));
+ assertEquals("", name.toString());
+ assertTrue(name.isEmpty());
+ assertFalse(name.hasSimpleName("foo"));
+ // just to trigger it, ensure to exception
+ name.hashCode();
+
+ PropertyName newName = name.withNamespace("");
+ assertNotSame(name, newName);
+ assertTrue(name.equals(name));
+ assertFalse(name.equals(newName));
+ assertFalse(newName.equals(name));
+
+ name = name.withSimpleName("foo");
+ assertEquals("foo", name.toString());
+ assertTrue(name.hasSimpleName("foo"));
+ assertFalse(name.isEmpty());
+ newName = name.withNamespace("ns");
+ assertEquals("{ns}foo", newName.toString());
+ assertFalse(newName.equals(name));
+ assertFalse(name.equals(newName));
+
+ // just to trigger it, ensure to exception
+ name.hashCode();
+ }
+
+ public void testPropertyMetadata()
+ {
+ PropertyMetadata md = PropertyMetadata.STD_OPTIONAL;
+ assertNull(md.getValueNulls());
+ assertNull(md.getContentNulls());
+ assertNull(md.getDefaultValue());
+ assertEquals(Boolean.FALSE, md.getRequired());
+
+ md = md.withNulls(Nulls.AS_EMPTY,
+ Nulls.FAIL);
+ assertEquals(Nulls.AS_EMPTY, md.getValueNulls());
+ assertEquals(Nulls.FAIL, md.getContentNulls());
+
+ assertFalse(md.hasDefaultValue());
+ assertSame(md, md.withDefaultValue(null));
+ assertSame(md, md.withDefaultValue(""));
+ md = md.withDefaultValue("foo");
+ assertEquals("foo", md.getDefaultValue());
+ assertTrue(md.hasDefaultValue());
+ assertSame(md, md.withDefaultValue("foo"));
+ md = md.withDefaultValue(null);
+ assertFalse(md.hasDefaultValue());
+ assertNull(md.getDefaultValue());
+
+ md = md.withRequired(null);
+ assertNull(md.getRequired());
+ assertFalse(md.isRequired());
+ md = md.withRequired(Boolean.TRUE);
+ assertTrue(md.isRequired());
+ assertSame(md, md.withRequired(Boolean.TRUE));
+ md = md.withRequired(null);
+ assertNull(md.getRequired());
+ assertFalse(md.isRequired());
+
+ assertFalse(md.hasIndex());
+ md = md.withIndex(Integer.valueOf(3));
+ assertTrue(md.hasIndex());
+ assertEquals(Integer.valueOf(3), md.getIndex());
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/introspect/TestAnnotionBundles.java b/src/test/java/com/fasterxml/jackson/databind/introspect/TestAnnotationBundles.java
similarity index 82%
rename from src/test/java/com/fasterxml/jackson/databind/introspect/TestAnnotionBundles.java
rename to src/test/java/com/fasterxml/jackson/databind/introspect/TestAnnotationBundles.java
index 761f0bc..e9a0714 100644
--- a/src/test/java/com/fasterxml/jackson/databind/introspect/TestAnnotionBundles.java
+++ b/src/test/java/com/fasterxml/jackson/databind/introspect/TestAnnotationBundles.java
@@ -6,6 +6,7 @@
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
+import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -13,7 +14,7 @@
/* Tests mostly for [JACKSON-754]: ability to create "annotation bundles"
*/
-public class TestAnnotionBundles extends com.fasterxml.jackson.databind.BaseMapTest
+public class TestAnnotationBundles extends com.fasterxml.jackson.databind.BaseMapTest
{
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@@ -71,6 +72,18 @@
@HolderA public int unimportant = 42;
}
+ static class RecursiveHolder2 {
+ @HolderA public int getValue() { return 28; }
+ }
+
+ static class RecursiveHolder3 {
+ public int x;
+
+ @JsonCreator
+ @HolderA
+ public RecursiveHolder3(int x) { this.x = x; }
+ }
+
@JsonProperty
@JacksonAnnotationsInside
@Retention(RetentionPolicy.RUNTIME)
@@ -110,11 +123,20 @@
assertEquals("{\"important\":42}", MAPPER.writeValueAsString(new InformingHolder()));
}
- public void testRecursiveBundles() throws Exception
- {
+ public void testRecursiveBundlesField() throws Exception {
assertEquals("{\"unimportant\":42}", MAPPER.writeValueAsString(new RecursiveHolder()));
}
+ public void testRecursiveBundlesMethod() throws Exception {
+ assertEquals("{\"value\":28}", MAPPER.writeValueAsString(new RecursiveHolder2()));
+ }
+
+ public void testRecursiveBundlesConstructor() throws Exception {
+ RecursiveHolder3 result = MAPPER.readValue("17", RecursiveHolder3.class);
+ assertNotNull(result);
+ assertEquals(17, result.x);
+ }
+
public void testBundledIgnore() throws Exception
{
assertEquals("{\"foobar\":13}", MAPPER.writeValueAsString(new Bean()));
diff --git a/src/test/java/com/fasterxml/jackson/databind/introspect/TestAutoDetect.java b/src/test/java/com/fasterxml/jackson/databind/introspect/TestAutoDetect.java
index c5817bc..9ab933d 100644
--- a/src/test/java/com/fasterxml/jackson/databind/introspect/TestAutoDetect.java
+++ b/src/test/java/com/fasterxml/jackson/databind/introspect/TestAutoDetect.java
@@ -1,7 +1,7 @@
package com.fasterxml.jackson.databind.introspect;
import com.fasterxml.jackson.annotation.*;
-
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
@@ -16,13 +16,33 @@
private PrivateBean(String a) { this.a = a; }
}
-
+
+ // test for [databind#1347], config overrides for visibility
+ @JsonPropertyOrder(alphabetic=true)
+ static class Feature1347SerBean {
+ public int field = 2;
+
+ public int getValue() { return 3; }
+ }
+
+ // let's promote use of fields; but not block setters yet
+ @JsonAutoDetect(fieldVisibility=Visibility.NON_PRIVATE)
+ static class Feature1347DeserBean {
+ int value;
+
+ public void setValue(int x) {
+ throw new IllegalArgumentException("Should NOT get called");
+ }
+ }
+
/*
/********************************************************
/* Unit tests
/********************************************************
*/
-
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
public void testPrivateCtor() throws Exception
{
// first, default settings, with which construction works ok
@@ -43,4 +63,42 @@
}
}
+ // [databind#1347]
+ public void testVisibilityConfigOverridesForSer() throws Exception
+ {
+ // first, by default, both field/method should be visible
+ final Feature1347SerBean input = new Feature1347SerBean();
+ assertEquals(aposToQuotes("{'field':2,'value':3}"),
+ MAPPER.writeValueAsString(input));
+
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configOverride(Feature1347SerBean.class)
+ .setVisibility(JsonAutoDetect.Value.construct(PropertyAccessor.GETTER,
+ Visibility.NONE));
+ assertEquals(aposToQuotes("{'field':2}"),
+ mapper.writeValueAsString(input));
+ }
+
+ // [databind#1347]
+ public void testVisibilityConfigOverridesForDeser() throws Exception
+ {
+ final String JSON = aposToQuotes("{'value':3}");
+
+ // by default, should throw exception
+ try {
+ /*Feature1347DeserBean bean =*/
+ MAPPER.readValue(JSON, Feature1347DeserBean.class);
+ fail("Should not pass");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Should NOT get called");
+ }
+
+ // but when instructed to ignore setter, should work
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configOverride(Feature1347DeserBean.class)
+ .setVisibility(JsonAutoDetect.Value.construct(PropertyAccessor.SETTER,
+ Visibility.NONE));
+ Feature1347DeserBean result = mapper.readValue(JSON, Feature1347DeserBean.class);
+ assertEquals(3, result.value);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/introspect/TestJacksonAnnotationIntrospector.java b/src/test/java/com/fasterxml/jackson/databind/introspect/TestJacksonAnnotationIntrospector.java
index d506473..ef9adde 100644
--- a/src/test/java/com/fasterxml/jackson/databind/introspect/TestJacksonAnnotationIntrospector.java
+++ b/src/test/java/com/fasterxml/jackson/databind/introspect/TestJacksonAnnotationIntrospector.java
@@ -185,7 +185,8 @@
{
ObjectMapper mapper = new ObjectMapper();
JacksonAnnotationIntrospector ai = new JacksonAnnotationIntrospector();
- AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(TypeResolverBean.class, mapper.getSerializationConfig());
+ AnnotatedClass ac = AnnotatedClassResolver.resolveWithoutSuperTypes(mapper.getSerializationConfig(),
+ TypeResolverBean.class);
JavaType baseType = TypeFactory.defaultInstance().constructType(TypeResolverBean.class);
TypeResolverBuilder<?> rb = ai.findTypeResolver(mapper.getDeserializationConfig(), ac, baseType);
assertNotNull(rb);
diff --git a/src/test/java/com/fasterxml/jackson/databind/introspect/TestPOJOPropertiesCollector.java b/src/test/java/com/fasterxml/jackson/databind/introspect/TestPOJOPropertiesCollector.java
index e621ed4..8c9994b 100644
--- a/src/test/java/com/fasterxml/jackson/databind/introspect/TestPOJOPropertiesCollector.java
+++ b/src/test/java/com/fasterxml/jackson/databind/introspect/TestPOJOPropertiesCollector.java
@@ -438,13 +438,16 @@
public void testJackson744() throws Exception
{
- BeanDescription beanDesc = MAPPER.getDeserializationConfig().introspect(MAPPER.constructType(Issue744Bean.class));
+ BeanDescription beanDesc = MAPPER.getDeserializationConfig().introspect
+ (MAPPER.constructType(Issue744Bean.class));
assertNotNull(beanDesc);
- AnnotatedMethod setter = beanDesc.findAnySetter();
+ AnnotatedMember setter = beanDesc.findAnySetterAccessor();
assertNotNull(setter);
+ assertEquals("addAdditionalProperty", setter.getName());
+ assertTrue(setter instanceof AnnotatedMethod);
}
- // [#269]: Support new @JsonPropertyDescription
+ // [databind#269]: Support new @JsonPropertyDescription
public void testPropertyDesc() throws Exception
{
// start via deser
@@ -455,7 +458,7 @@
_verifyProperty(beanDesc, true, false, "13");
}
- // [#438]: Support @JsonProperty.index
+ // [databind#438]: Support @JsonProperty.index
public void testPropertyIndex() throws Exception
{
BeanDescription beanDesc = MAPPER.getDeserializationConfig().introspect(MAPPER.constructType(PropDescBean.class));
@@ -523,7 +526,6 @@
}
}
}
-
/*
/**********************************************************
/* Helper methods
diff --git a/src/test/java/com/fasterxml/jackson/databind/introspect/TestScalaLikeImplicitProperties.java b/src/test/java/com/fasterxml/jackson/databind/introspect/TestScalaLikeImplicitProperties.java
index b6121bb..397db2f 100644
--- a/src/test/java/com/fasterxml/jackson/databind/introspect/TestScalaLikeImplicitProperties.java
+++ b/src/test/java/com/fasterxml/jackson/databind/introspect/TestScalaLikeImplicitProperties.java
@@ -1,6 +1,8 @@
package com.fasterxml.jackson.databind.introspect;
+import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.cfg.MapperConfig;
/**
* Tests Scala-style JVM naming patterns for properties.
@@ -49,13 +51,21 @@
return null;
}
+ /* Deprecated since 2.9
@Override
public boolean hasCreatorAnnotation(Annotated a) {
- // A placeholder for legitmate creator detection.
+ return (a instanceof AnnotatedConstructor);
+ }
+ */
+
+ @Override
+ public JsonCreator.Mode findCreatorAnnotation(MapperConfig<?> config, Annotated a) {
+ // A placeholder for legitimate creator detection.
// In Scala, all primary constructors should be creators,
// but I can't obtain a reference to the AnnotatedClass from the
// AnnotatedConstructor, so it's simulated here.
- return (a instanceof AnnotatedConstructor);
+ return (a instanceof AnnotatedConstructor)
+ ? JsonCreator.Mode.DEFAULT : null;
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/introspect/TypeCoercion1592Test.java b/src/test/java/com/fasterxml/jackson/databind/introspect/TypeCoercion1592Test.java
new file mode 100644
index 0000000..c1feb65
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/introspect/TypeCoercion1592Test.java
@@ -0,0 +1,35 @@
+package com.fasterxml.jackson.databind.introspect;
+
+import com.fasterxml.jackson.databind.*;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+
+// [databind#1592]: allow "coercion" between primitive/wrapper (mostly just ignoring)
+public class TypeCoercion1592Test extends BaseMapTest
+{
+ static class Bean1592
+ {
+ @JsonSerialize(as=Integer.class)
+ public int i;
+
+ @JsonDeserialize(as=Long.class)
+ public long l;
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testTypeCoercion1592() throws Exception
+ {
+ // first, serialize
+ MAPPER.writeValueAsString(new Bean1592());
+ Bean1592 result = MAPPER.readValue("{}", Bean1592.class);
+ assertNotNull(result);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/introspect/VisibilityForSerializationTest.java b/src/test/java/com/fasterxml/jackson/databind/introspect/VisibilityForSerializationTest.java
new file mode 100644
index 0000000..949ba6e
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/introspect/VisibilityForSerializationTest.java
@@ -0,0 +1,160 @@
+package com.fasterxml.jackson.databind.introspect;
+
+import java.io.*;
+import java.util.*;
+
+import com.fasterxml.jackson.annotation.*;
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
+
+/**
+ * Unit tests for checking handling of some of {@link MapperFeature}s
+ * and {@link SerializationFeature}s for serialization.
+ */
+public class VisibilityForSerializationTest
+ extends BaseMapTest
+{
+ /**
+ * Class with one explicitly defined getter, one name-based
+ * auto-detectable getter.
+ */
+ static class GetterClass
+ {
+ @JsonProperty("x") public int getX() { return -2; }
+ public int getY() { return 1; }
+ }
+
+ /**
+ * Another test-class that explicitly disables auto-detection
+ */
+ @JsonAutoDetect(getterVisibility=Visibility.NONE)
+ static class DisabledGetterClass
+ {
+ @JsonProperty("x") public int getX() { return -2; }
+ public int getY() { return 1; }
+ }
+
+ /**
+ * Another test-class that explicitly enables auto-detection
+ */
+ @JsonAutoDetect(isGetterVisibility=Visibility.NONE)
+ static class EnabledGetterClass
+ {
+ @JsonProperty("x") public int getX() { return -2; }
+ public int getY() { return 1; }
+
+ // not auto-detected, since "is getter" auto-detect disabled
+ public boolean isOk() { return true; }
+ }
+
+ /**
+ * One more: only detect "isXxx", not "getXXX"
+ */
+ @JsonAutoDetect(getterVisibility=Visibility.NONE)
+ static class EnabledIsGetterClass
+ {
+ // Won't be auto-detected any more
+ public int getY() { return 1; }
+
+ // but this will be
+ public boolean isOk() { return true; }
+ }
+
+ static class TCls {
+ @JsonProperty("groupname")
+ private String groupname;
+
+ public void setName(String str) {
+ this.groupname = str;
+ }
+ public String getName() {
+ return groupname;
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ public void testGlobalAutoDetection() throws IOException
+ {
+ // First: auto-detection enabled (default):
+ ObjectMapper m = new ObjectMapper();
+ Map<String,Object> result = writeAndMap(m, new GetterClass());
+ assertEquals(2, result.size());
+ assertEquals(Integer.valueOf(-2), result.get("x"));
+ assertEquals(Integer.valueOf(1), result.get("y"));
+
+ // Then auto-detection disabled. But note: we MUST create a new
+ // mapper, since old version of serializer may be cached by now
+ m = new ObjectMapper();
+ m.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
+ result = writeAndMap(m, new GetterClass());
+ assertEquals(1, result.size());
+ assertTrue(result.containsKey("x"));
+ }
+
+ public void testPerClassAutoDetection() throws IOException
+ {
+ // First: class-level auto-detection disabling
+ ObjectMapper m = new ObjectMapper();
+ Map<String,Object> result = writeAndMap(m, new DisabledGetterClass());
+ assertEquals(1, result.size());
+ assertTrue(result.containsKey("x"));
+
+ // And then class-level auto-detection enabling, should override defaults
+ m.configure(MapperFeature.AUTO_DETECT_GETTERS, true);
+ result = writeAndMap(m, new EnabledGetterClass());
+ assertEquals(2, result.size());
+ assertTrue(result.containsKey("x"));
+ assertTrue(result.containsKey("y"));
+ }
+
+ public void testPerClassAutoDetectionForIsGetter() throws IOException
+ {
+ ObjectMapper m = new ObjectMapper();
+ // class level should override
+ m.configure(MapperFeature.AUTO_DETECT_GETTERS, true);
+ m.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
+ Map<String,Object> result = writeAndMap(m, new EnabledIsGetterClass());
+ assertEquals(0, result.size());
+ assertFalse(result.containsKey("ok"));
+ }
+
+ // Simple test verifying that chainable methods work ok...
+ public void testConfigChainability()
+ {
+ ObjectMapper m = new ObjectMapper();
+ assertTrue(m.isEnabled(MapperFeature.AUTO_DETECT_SETTERS));
+ assertTrue(m.isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
+ m.configure(MapperFeature.AUTO_DETECT_SETTERS, false)
+ .configure(MapperFeature.AUTO_DETECT_GETTERS, false);
+ assertFalse(m.isEnabled(MapperFeature.AUTO_DETECT_SETTERS));
+ assertFalse(m.isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
+ }
+
+ public void testVisibilityFeatures() throws Exception
+ {
+ ObjectMapper om = new ObjectMapper();
+ // Only use explicitly specified values to be serialized/deserialized (i.e., JSONProperty).
+ om.configure(MapperFeature.AUTO_DETECT_FIELDS, false);
+ om.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
+ om.configure(MapperFeature.AUTO_DETECT_SETTERS, false);
+ om.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
+ om.configure(MapperFeature.USE_GETTERS_AS_SETTERS, false);
+ om.configure(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
+ om.configure(MapperFeature.INFER_PROPERTY_MUTATORS, false);
+ om.configure(MapperFeature.USE_ANNOTATIONS, true);
+
+ JavaType javaType = om.getTypeFactory().constructType(TCls.class);
+ BeanDescription desc = (BeanDescription) om.getSerializationConfig().introspect(javaType);
+ List<BeanPropertyDefinition> props = desc.findProperties();
+ if (props.size() != 1) {
+ fail("Should find 1 property, not "+props.size()+"; properties = "+props);
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsonschema/NewSchemaTest.java b/src/test/java/com/fasterxml/jackson/databind/jsonschema/NewSchemaTest.java
index ea31611..8380fd5 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsonschema/NewSchemaTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsonschema/NewSchemaTest.java
@@ -3,6 +3,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
+import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonValue;
@@ -10,6 +11,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsonFormatVisitors.*;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;
/**
* Basic tests to exercise low-level support added for JSON Schema module and
@@ -45,12 +47,144 @@
public EnumMap<TestEnum,Double> weights;
}
+ static class POJOWithScalars {
+ public boolean boo;
+ public byte b;
+ public char c;
+ public short s;
+ public int i;
+ public long l;
+ public float f;
+ public double d;
+
+ public byte[] arrayBoo;
+ public byte[] arrayb;
+ public char[] arrayc;
+ public short[] arrays;
+ public int[] arrayi;
+ public long[] arrayl;
+ public float[] arrayf;
+ public double[] arrayd;
+
+ public Boolean Boo;
+ public Byte B;
+ public Character C;
+ public Short S;
+ public Integer I;
+ public Long L;
+ public Float F;
+ public Double D;
+
+ public TestEnum en;
+ public String str;
+ public String[] strs;
+ public java.util.Date date;
+ public java.util.Calendar calendar;
+ }
+
+ static class POJOWithRefs {
+ public AtomicReference<POJO> maybePOJO;
+
+ public AtomicReference<String> maybeString;
+ }
+
@JsonPropertyOrder({ "dec", "bigInt" })
static class Numbers {
public BigDecimal dec;
public BigInteger bigInt;
}
+ static class BogusJsonFormatVisitorWrapper
+ extends JsonFormatVisitorWrapper.Base
+ {
+ // Implement handlers just to get more exercise...
+
+ @Override
+ public JsonObjectFormatVisitor expectObjectFormat(JavaType type) {
+ return new JsonObjectFormatVisitor.Base(getProvider()) {
+ @Override
+ public void property(BeanProperty prop) throws JsonMappingException {
+ _visit(prop);
+ }
+
+ @Override
+ public void property(String name, JsonFormatVisitable handler,
+ JavaType propertyTypeHint) { }
+
+ @Override
+ public void optionalProperty(BeanProperty prop) throws JsonMappingException {
+ _visit(prop);
+ }
+
+ @Override
+ public void optionalProperty(String name, JsonFormatVisitable handler,
+ JavaType propertyTypeHint) throws JsonMappingException { }
+
+ private void _visit(BeanProperty prop) throws JsonMappingException
+ {
+ if (!(prop instanceof BeanPropertyWriter)) {
+ return;
+ }
+ BeanPropertyWriter bpw = (BeanPropertyWriter) prop;
+ JsonSerializer<?> ser = bpw.getSerializer();
+ final SerializerProvider prov = getProvider();
+ if (ser == null) {
+ if (prov == null) {
+ throw new Error("SerializerProvider missing");
+ }
+ ser = prov.findValueSerializer(prop.getType(), prop);
+ }
+ // and this just for bit of extra coverage...
+ if (ser instanceof StdSerializer) {
+ assertNotNull(((StdSerializer<?>) ser).getSchema(prov, prop.getType()));
+ }
+ JsonFormatVisitorWrapper visitor = new JsonFormatVisitorWrapper.Base(getProvider());
+ ser.acceptJsonFormatVisitor(visitor, prop.getType());
+ }
+ };
+ }
+
+ @Override
+ public JsonArrayFormatVisitor expectArrayFormat(JavaType type) {
+ return new JsonArrayFormatVisitor.Base(getProvider());
+ }
+
+ @Override
+ public JsonStringFormatVisitor expectStringFormat(JavaType type) {
+ return new JsonStringFormatVisitor.Base();
+ }
+
+ @Override
+ public JsonNumberFormatVisitor expectNumberFormat(JavaType type) {
+ return new JsonNumberFormatVisitor.Base();
+ }
+
+ @Override
+ public JsonIntegerFormatVisitor expectIntegerFormat(JavaType type) {
+ return new JsonIntegerFormatVisitor.Base();
+ }
+
+ @Override
+ public JsonBooleanFormatVisitor expectBooleanFormat(JavaType type) {
+ return new JsonBooleanFormatVisitor.Base();
+ }
+
+ @Override
+ public JsonNullFormatVisitor expectNullFormat(JavaType type) {
+ return new JsonNullFormatVisitor.Base();
+ }
+
+ @Override
+ public JsonAnyFormatVisitor expectAnyFormat(JavaType type) {
+ return new JsonAnyFormatVisitor.Base();
+ }
+
+ @Override
+ public JsonMapFormatVisitor expectMapFormat(JavaType type) {
+ return new JsonMapFormatVisitor.Base();
+ }
+ }
+
/*
/**********************************************************
/* Test methods
@@ -65,9 +199,15 @@
*/
public void testBasicTraversal() throws Exception
{
- MAPPER.acceptJsonFormatVisitor(POJO.class, new JsonFormatVisitorWrapper.Base());
+ MAPPER.acceptJsonFormatVisitor(POJO.class, new BogusJsonFormatVisitorWrapper());
+ MAPPER.acceptJsonFormatVisitor(POJOWithScalars.class, new BogusJsonFormatVisitorWrapper());
+ MAPPER.acceptJsonFormatVisitor(LinkedHashMap.class, new BogusJsonFormatVisitorWrapper());
+ MAPPER.acceptJsonFormatVisitor(ArrayList.class, new BogusJsonFormatVisitorWrapper());
+ MAPPER.acceptJsonFormatVisitor(EnumSet.class, new BogusJsonFormatVisitorWrapper());
+
+ MAPPER.acceptJsonFormatVisitor(POJOWithRefs.class, new BogusJsonFormatVisitorWrapper());
}
-
+
public void testSimpleEnum() throws Exception
{
final Set<String> values = new TreeSet<String>();
@@ -125,7 +265,7 @@
assertEquals(exp, values);
}
- // [2.7]: Ensure JsonValueFormat serializes/deserializes as expected
+ // Ensure JsonValueFormat serializes/deserializes as expected
public void testJsonValueFormatHandling() throws Exception
{
// first: serialize using 'toString()', not name
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForArrays.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForArrays.java
index 154630d..90d6f61 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForArrays.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForArrays.java
@@ -23,7 +23,15 @@
public ArrayBean() { this(null); }
public ArrayBean(Object[] v) { values = v; }
}
-
+
+ static class PrimitiveArrayBean {
+ @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
+ public Object stuff;
+
+ protected PrimitiveArrayBean() { }
+ public PrimitiveArrayBean(Object value) { stuff = value; }
+ }
+
/*
/**********************************************************
/* Unit tests
@@ -57,7 +65,6 @@
assertEquals(String[][].class, result.values.getClass());
}
- // @since 1.8
public void testNodeInArray() throws Exception
{
JsonNode node = new ObjectMapper().readTree("{\"a\":3}");
@@ -72,6 +79,7 @@
assertTrue(ob instanceof JsonNode);
}
+ @SuppressWarnings("deprecation")
public void testNodeInEmptyArray() throws Exception {
Map<String, List<String>> outerMap = new HashMap<String, List<String>>();
outerMap.put("inner", new ArrayList<String>());
@@ -90,7 +98,6 @@
assertEquals("{}", result[0].toString());
}
- // test for [JACKSON-845]
public void testArraysOfArrays() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
@@ -105,6 +112,28 @@
_testArraysAs(mapper, json, Object.class);
}
+ public void testArrayTypingForPrimitiveArrays() throws Exception
+ {
+ ObjectMapper m = new ObjectMapper();
+ m.enableDefaultTyping(DefaultTyping.NON_CONCRETE_AND_ARRAYS);
+ _testArrayTypingForPrimitiveArrays(m, new int[] { 1, 2, 3 });
+ _testArrayTypingForPrimitiveArrays(m, new long[] { 1, 2, 3 });
+ _testArrayTypingForPrimitiveArrays(m, new short[] { 1, 2, 3 });
+ _testArrayTypingForPrimitiveArrays(m, new double[] { 0.5, 5.5, -1.0 });
+ _testArrayTypingForPrimitiveArrays(m, new float[] { 0.5f, 5.5f, -1.0f });
+ _testArrayTypingForPrimitiveArrays(m, new boolean[] { true, false });
+
+ _testArrayTypingForPrimitiveArrays(m, new char[] { 'a', 'b' });
+ }
+
+ private void _testArrayTypingForPrimitiveArrays(ObjectMapper mapper, Object v) throws Exception {
+ PrimitiveArrayBean input = new PrimitiveArrayBean(v);
+ String json = mapper.writeValueAsString(input);
+ PrimitiveArrayBean result = mapper.readValue(json, PrimitiveArrayBean.class);
+ assertNotNull(result.stuff);
+ assertSame(v.getClass(), result.stuff.getClass());
+ }
+
/*
/**********************************************************
/* Helper methods
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForEnums.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForEnums.java
index 645530c..a82f5ac 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForEnums.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForEnums.java
@@ -23,7 +23,7 @@
protected static class TimeUnitBean {
public TimeUnit timeUnit;
}
-
+
/*
/**********************************************************
/* Test methods
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForMaps.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForMaps.java
index c36ebd2..aa5ada4 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForMaps.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForMaps.java
@@ -118,8 +118,6 @@
TypeFactory.defaultInstance().constructType(Object.class), subtypes, forSerialization, !forSerialization);
}
- // // For #234:
-
public void testList() throws Exception
{
final ObjectMapper mapper = new ObjectMapper();
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestPolymorphicDeserialization676.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicDeserialization676.java
similarity index 98%
rename from src/test/java/com/fasterxml/jackson/databind/deser/TestPolymorphicDeserialization676.java
rename to src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicDeserialization676.java
index 945a5e7..d6b1bcc 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestPolymorphicDeserialization676.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicDeserialization676.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser;
+package com.fasterxml.jackson.databind.jsontype;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicWithDefaultImpl.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicWithDefaultImpl.java
index 439b7d6..7d260c7 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicWithDefaultImpl.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicWithDefaultImpl.java
@@ -6,6 +6,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.NoClass;
+import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
/**
* Unit tests related to specialized handling of "default implementation"
@@ -264,9 +265,8 @@
try {
r.readValue("{ \"value\": \"\" }");
fail("Expected " + JsonMappingException.class);
- } catch (JsonMappingException e) {
- verifyException(e, "missing property 'type'");
- verifyException(e, "contain type id");
+ } catch (InvalidTypeIdException e) {
+ verifyException(e, "missing type id property 'type'");
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPropertyTypeInfo.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPropertyTypeInfo.java
index 484f6cc..5f043a3 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPropertyTypeInfo.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPropertyTypeInfo.java
@@ -14,11 +14,14 @@
@SuppressWarnings("serial")
public class TestPropertyTypeInfo extends BaseMapTest
{
- /*
- /**********************************************************
- /* Helper types
- /**********************************************************
- */
+ protected static class BooleanValue {
+ public Boolean b;
+
+ @JsonCreator
+ public BooleanValue(Boolean value) { b = value; }
+
+ @JsonValue public Boolean value() { return b; }
+ }
static class FieldWrapperBean
{
@@ -120,7 +123,7 @@
{
ObjectMapper mapper = new ObjectMapper();
MethodWrapperBeanList list = new MethodWrapperBeanList();
- list.add(new MethodWrapperBean(new BooleanWrapper(true)));
+ list.add(new MethodWrapperBean(new BooleanValue(true)));
list.add(new MethodWrapperBean(new StringWrapper("x")));
list.add(new MethodWrapperBean(new OtherBean()));
String json = mapper.writeValueAsString(list);
@@ -128,8 +131,8 @@
assertNotNull(result);
assertEquals(3, result.size());
MethodWrapperBean bean = result.get(0);
- assertEquals(BooleanWrapper.class, bean.value.getClass());
- assertEquals(((BooleanWrapper) bean.value).b, Boolean.TRUE);
+ assertEquals(BooleanValue.class, bean.value.getClass());
+ assertEquals(((BooleanValue) bean.value).b, Boolean.TRUE);
bean = result.get(1);
assertEquals(StringWrapper.class, bean.value.getClass());
assertEquals(((StringWrapper) bean.value).str, "x");
@@ -141,15 +144,15 @@
{
ObjectMapper mapper = new ObjectMapper();
FieldWrapperBeanArray array = new FieldWrapperBeanArray(new
- FieldWrapperBean[] { new FieldWrapperBean(new BooleanWrapper(true)) });
+ FieldWrapperBean[] { new FieldWrapperBean(new BooleanValue(true)) });
String json = mapper.writeValueAsString(array);
FieldWrapperBeanArray result = mapper.readValue(json, FieldWrapperBeanArray.class);
assertNotNull(result);
FieldWrapperBean[] beans = result.beans;
assertEquals(1, beans.length);
FieldWrapperBean bean = beans[0];
- assertEquals(BooleanWrapper.class, bean.value.getClass());
- assertEquals(((BooleanWrapper) bean.value).b, Boolean.TRUE);
+ assertEquals(BooleanValue.class, bean.value.getClass());
+ assertEquals(((BooleanValue) bean.value).b, Boolean.TRUE);
}
public void testSimpleArrayMethod() throws Exception
@@ -187,7 +190,7 @@
{
ObjectMapper mapper = new ObjectMapper();
MethodWrapperBeanMap map = new MethodWrapperBeanMap();
- map.put("xyz", new MethodWrapperBean(new BooleanWrapper(true)));
+ map.put("xyz", new MethodWrapperBean(new BooleanValue(true)));
String json = mapper.writeValueAsString(map);
MethodWrapperBeanMap result = mapper.readValue(json, MethodWrapperBeanMap.class);
assertNotNull(result);
@@ -195,7 +198,7 @@
MethodWrapperBean bean = result.get("xyz");
assertNotNull(bean);
Object ob = bean.value;
- assertEquals(BooleanWrapper.class, ob.getClass());
- assertEquals(((BooleanWrapper) ob).b, Boolean.TRUE);
+ assertEquals(BooleanValue.class, ob.getClass());
+ assertEquals(((BooleanValue) ob).b, Boolean.TRUE);
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestSubtypes.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestSubtypes.java
index 4450c04..404dbb1 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestSubtypes.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestSubtypes.java
@@ -9,6 +9,7 @@
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -282,8 +283,8 @@
try {
MAPPER.readValue(JSON, SuperTypeWithoutDefault.class);
fail("Expected an exception");
- } catch (JsonMappingException e) {
- verifyException(e, "missing property");
+ } catch (InvalidTypeIdException e) {
+ verifyException(e, "missing type id property '#type'");
}
// but then succeed when we register default impl
@@ -330,9 +331,11 @@
MAPPER.readValue(aposToQuotes("{'value':['"
+TheBomb.class.getName()+"',{'a':13}] }"), DateWrapper.class);
fail("Should not pass");
- } catch (JsonMappingException e) {
- verifyException(e, "not subtype of");
+ } catch (InvalidTypeIdException e) {
+ verifyException(e, "not a subtype");
verifyException(e, TheBomb.class.getName());
+ } catch (Exception e) {
+ fail("Should have hit `InvalidTypeIdException`, not `"+e.getClass().getName()+"`: "+e);
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypeNames.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypeNames.java
index cfe6a6b..735b22d 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypeNames.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypeNames.java
@@ -7,19 +7,31 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
+
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.jsontype.impl.StdSubtypeResolver;
import com.fasterxml.jackson.databind.type.TypeFactory;
/**
* Separate tests for verifying that "type name" type id mechanism
* works.
- *
- * @author tatu
*/
public class TestTypeNames extends BaseMapTest
{
@SuppressWarnings("serial")
static class AnimalMap extends LinkedHashMap<String,Animal> { }
+
+ @JsonTypeInfo(property = "type", include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.NAME)
+ @JsonSubTypes({
+ @JsonSubTypes.Type(value = A1616.class,name = "A"),
+ @JsonSubTypes.Type(value = B1616.class)
+ })
+ static abstract class Base1616 { }
+
+ static class A1616 extends Base1616 { }
+
+ @JsonTypeName("B")
+ static class B1616 extends Base1616 { }
/*
/**********************************************************
@@ -27,31 +39,48 @@
/**********************************************************
*/
+ private final ObjectMapper MAPPER = objectMapper();
+
+ public void testBaseTypeId1616() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ Collection<NamedType> subtypes = new StdSubtypeResolver().collectAndResolveSubtypesByTypeId(
+ mapper.getDeserializationConfig(),
+ // note: `null` is fine here as `AnnotatedMember`:
+ null,
+ mapper.constructType(Base1616.class));
+ assertEquals(2, subtypes.size());
+ Set<String> ok = new HashSet<>(Arrays.asList("A", "B"));
+ for (NamedType type : subtypes) {
+ String id = type.getName();
+ if (!ok.contains(id)) {
+ fail("Unexpected id '"+id+"' (mapping to: "+type.getType()+"), should be one of: "+ok);
+ }
+ }
+ }
+
public void testSerialization() throws Exception
{
- ObjectMapper m = new ObjectMapper();
-
// Note: need to use wrapper array just so that we can define
// static type on serialization. If we had root static types,
// could use those; but at the moment root type is dynamic
assertEquals("[{\"doggy\":{\"name\":\"Spot\",\"ageInYears\":3}}]",
- m.writeValueAsString(new Animal[] { new Dog("Spot", 3) }));
+ MAPPER.writeValueAsString(new Animal[] { new Dog("Spot", 3) }));
assertEquals("[{\"MaineCoon\":{\"name\":\"Belzebub\",\"purrs\":true}}]",
- m.writeValueAsString(new Animal[] { new MaineCoon("Belzebub", true)}));
+ MAPPER.writeValueAsString(new Animal[] { new MaineCoon("Belzebub", true)}));
}
public void testRoundTrip() throws Exception
{
- ObjectMapper m = new ObjectMapper();
Animal[] input = new Animal[] {
new Dog("Odie", 7),
null,
new MaineCoon("Piru", false),
new Persian("Khomeini", true)
};
- String json = m.writeValueAsString(input);
- List<Animal> output = m.readValue(json,
+ String json = MAPPER.writeValueAsString(input);
+ List<Animal> output = MAPPER.readValue(json,
TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Animal.class));
assertEquals(input.length, output.size());
for (int i = 0, len = input.length; i < len; ++i) {
@@ -62,12 +91,11 @@
public void testRoundTripMap() throws Exception
{
- ObjectMapper m = new ObjectMapper();
AnimalMap input = new AnimalMap();
input.put("venla", new MaineCoon("Venla", true));
input.put("ama", new Dog("Amadeus", 13));
- String json = m.writeValueAsString(input);
- AnimalMap output = m.readValue(json, AnimalMap.class);
+ String json = MAPPER.writeValueAsString(input);
+ AnimalMap output = MAPPER.readValue(json, AnimalMap.class);
assertNotNull(output);
assertEquals(AnimalMap.class, output.getClass());
assertEquals(input.size(), output.size());
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypedArraySerialization.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypedArraySerialization.java
index a7ddf02..3b416bd 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypedArraySerialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypedArraySerialization.java
@@ -70,13 +70,14 @@
/**********************************************************
*/
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
public void testListWithPolymorphic() throws Exception
{
- ObjectMapper mapper = new ObjectMapper();
BeanListWrapper beans = new BeanListWrapper();
- assertEquals("{\"beans\":[{\"@type\":\"bean\",\"x\":0}]}", mapper.writeValueAsString(beans));
+ assertEquals("{\"beans\":[{\"@type\":\"bean\",\"x\":0}]}", MAPPER.writeValueAsString(beans));
// Related to [JACKSON-364]
- ObjectWriter w = mapper.writerWithView(Object.class);
+ ObjectWriter w = MAPPER.writerWithView(Object.class);
assertEquals("{\"beans\":[{\"@type\":\"bean\",\"x\":0}]}", w.writeValueAsString(beans));
}
@@ -86,7 +87,8 @@
input.add(5);
input.add(13);
// uses WRAPPER_ARRAY inclusion:
- assertEquals("[\""+TypedList.class.getName()+"\",[5,13]]", serializeAsString(input));
+ assertEquals("[\""+TypedList.class.getName()+"\",[5,13]]",
+ MAPPER.writeValueAsString(input));
}
// Similar to above, but this time let's request adding type info
@@ -99,7 +101,7 @@
input.add("a");
input.add("b");
assertEquals("[\""+TypedListAsProp.class.getName()+"\",[\"a\",\"b\"]]",
- serializeAsString(input));
+ MAPPER.writeValueAsString(input));
}
public void testStringListAsObjectWrapper() throws Exception
@@ -113,7 +115,7 @@
// annotations
String expName = "TestTypedArraySerialization$TypedListAsWrapper";
assertEquals("{\""+expName+"\":[true,null,false]}",
- serializeAsString(input));
+ MAPPER.writeValueAsString(input));
}
/*
@@ -128,7 +130,7 @@
m.addMixIn(int[].class, WrapperMixIn.class);
int[] input = new int[] { 1, 2, 3 };
String clsName = int[].class.getName();
- assertEquals("{\""+clsName+"\":[1,2,3]}", serializeAsString(m, input));
+ assertEquals("{\""+clsName+"\":[1,2,3]}", m.writeValueAsString(input));
}
/*
@@ -139,16 +141,14 @@
public void testGenericArray() throws Exception
{
- ObjectMapper m;
final A[] input = new A[] { new B() };
final String EXP = "[{\"BB\":{\"value\":2}}]";
// first, with defaults
- m = new ObjectMapper();
- assertEquals(EXP, m.writeValueAsString(input));
+ assertEquals(EXP, MAPPER.writeValueAsString(input));
// then with static typing enabled:
- m = new ObjectMapper();
+ ObjectMapper m = new ObjectMapper();
m.configure(MapperFeature.USE_STATIC_TYPING, true);
assertEquals(EXP, m.writeValueAsString(input));
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypedContainerSerialization.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypedContainerSerialization.java
index 92cc507..5c3bd75 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypedContainerSerialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestTypedContainerSerialization.java
@@ -9,12 +9,13 @@
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
+
import com.fasterxml.jackson.core.type.TypeReference;
+
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import com.fasterxml.jackson.databind.type.TypeFactory;
public class TestTypedContainerSerialization
extends BaseMapTest
@@ -123,13 +124,13 @@
public void testIssue329() throws Exception
{
- ArrayList<Animal> animals = new ArrayList<Animal>();
- animals.add(new Dog("Spot"));
- JavaType rootType = TypeFactory.defaultInstance().constructParametrizedType(Iterator.class, Iterator.class, Animal.class);
- String json = mapper.writerFor(rootType).writeValueAsString(animals.iterator());
- if (json.indexOf("\"object-type\":\"doggy\"") < 0) {
- fail("No polymorphic type retained, should be; JSON = '"+json+"'");
- }
+ ArrayList<Animal> animals = new ArrayList<Animal>();
+ animals.add(new Dog("Spot"));
+ JavaType rootType = mapper.getTypeFactory().constructParametricType(Iterator.class, Animal.class);
+ String json = mapper.writerFor(rootType).writeValueAsString(animals.iterator());
+ if (json.indexOf("\"object-type\":\"doggy\"") < 0) {
+ fail("No polymorphic type retained, should be; JSON = '"+json+"'");
+ }
}
public void testIssue508() throws Exception
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestVisibleTypeId.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestVisibleTypeId.java
index 3b9003e..c644bde 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestVisibleTypeId.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestVisibleTypeId.java
@@ -6,7 +6,7 @@
import com.fasterxml.jackson.databind.*;
/**
- * Tests to verify [JACKSON-437], [JACKSON-762]
+ * Tests to verify that Type Id may be exposed during deserialization,
*/
public class TestVisibleTypeId extends BaseMapTest
{
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestWithGenerics.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestWithGenerics.java
index b3529f0..bb4cd9b 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestWithGenerics.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestWithGenerics.java
@@ -156,7 +156,8 @@
{
Dog dog = new Dog("Fluffy", 3);
ContainerWithGetter<Animal> c2 = new ContainerWithGetter<Animal>(dog);
- String json = MAPPER.writerFor(MAPPER.getTypeFactory().constructParametrizedType(ContainerWithGetter.class, ContainerWithGetter.class, Animal.class)).writeValueAsString(c2);
+ String json = MAPPER.writerFor(MAPPER.getTypeFactory().constructParametricType(ContainerWithGetter.class,
+ Animal.class)).writeValueAsString(c2);
if (json.indexOf("\"object-type\":\"doggy\"") < 0) {
fail("polymorphic type not kept, result == "+json+"; should contain 'object-type':'...'");
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TypeDeserializerTest.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TypeDeserializerTest.java
new file mode 100644
index 0000000..2b1fa8e
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TypeDeserializerTest.java
@@ -0,0 +1,43 @@
+package com.fasterxml.jackson.databind.jsontype;
+
+import com.fasterxml.jackson.core.*;
+
+import com.fasterxml.jackson.databind.*;
+
+public class TypeDeserializerTest extends BaseMapTest
+{
+ public void testUtilMethods() throws Exception
+ {
+ final JsonFactory f = new JsonFactory();
+
+ JsonParser p = f.createParser("true");
+ assertNull(TypeDeserializer.deserializeIfNatural(p, null, Object.class));
+ p.nextToken();
+ assertEquals(Boolean.TRUE, TypeDeserializer.deserializeIfNatural(p, null, Object.class));
+ p.close();
+
+ p = f.createParser("false ");
+ p.nextToken();
+ assertEquals(Boolean.FALSE, TypeDeserializer.deserializeIfNatural(p, null, Object.class));
+ p.close();
+
+ p = f.createParser("1");
+ p.nextToken();
+ assertEquals(Integer.valueOf(1), TypeDeserializer.deserializeIfNatural(p, null, Object.class));
+ p.close();
+
+ p = f.createParser("0.5 ");
+ p.nextToken();
+ assertEquals(Double.valueOf(0.5), TypeDeserializer.deserializeIfNatural(p, null, Object.class));
+ p.close();
+
+ p = f.createParser("\"foo\" [ ] ");
+ p.nextToken();
+ assertEquals("foo", TypeDeserializer.deserializeIfNatural(p, null, Object.class));
+
+ p.nextToken();
+ assertNull(TypeDeserializer.deserializeIfNatural(p, null, Object.class));
+
+ p.close();
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/ext/MultipleExternalIds291Test.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/ext/MultipleExternalIds291Test.java
new file mode 100644
index 0000000..b015dd8
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/ext/MultipleExternalIds291Test.java
@@ -0,0 +1,111 @@
+package com.fasterxml.jackson.databind.jsontype.ext;
+
+import com.fasterxml.jackson.annotation.*;
+import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
+import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
+import com.fasterxml.jackson.databind.*;
+
+public class MultipleExternalIds291Test extends BaseMapTest
+{
+ // For [Issue#291]
+ interface F1 {}
+
+ static class A implements F1 {
+ public String a;
+ }
+
+ static class B implements F1 {
+ public String b;
+ }
+
+ static interface F2 {}
+
+ static class C implements F2 {
+ public String c;
+ }
+
+ static class D implements F2{
+ public String d;
+ }
+
+ static class Container {
+ @JsonTypeInfo(use = Id.NAME, property = "type", include = As.EXTERNAL_PROPERTY)
+ @JsonSubTypes({
+ @JsonSubTypes.Type(value = A.class, name = "1"),
+ @JsonSubTypes.Type(value = B.class, name = "2")})
+ public F1 field1;
+
+ @JsonTypeInfo(use = Id.NAME, property = "type", include = As.EXTERNAL_PROPERTY)
+ @JsonSubTypes({
+ @JsonSubTypes.Type(value = C.class, name = "1"),
+ @JsonSubTypes.Type(value = D.class, name = "2")})
+ public F2 field2;
+ }
+
+ static class ContainerWithExtra extends Container {
+ public String type;
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ final ObjectMapper MAPPER = objectMapper();
+
+ // [databind#291]
+ public void testMultipleValuesSingleExtId() throws Exception
+ {
+ // first with ext-id before values
+ _testMultipleValuesSingleExtId(
+"{'type' : '1',\n"
++"'field1' : { 'a' : 'AAA' },\n"
++"'field2' : { 'c' : 'CCC' }\n"
++"}"
+);
+
+ // then after
+ _testMultipleValuesSingleExtId(
+"{\n"
++"'field1' : { 'a' : 'AAA' },\n"
++"'field2' : { 'c' : 'CCC' },\n"
++"'type' : '1'\n"
++"}"
+);
+ // and then in-between
+ _testMultipleValuesSingleExtId(
+"{\n"
++"'field1' : { 'a' : 'AAA' },\n"
++"'type' : '1',\n"
++"'field2' : { 'c' : 'CCC' }\n"
++"}"
+);
+ }
+
+ public void _testMultipleValuesSingleExtId(String json) throws Exception
+ {
+ json = aposToQuotes(json);
+
+ // First, with base class, no type id field separately
+ {
+ Container c = MAPPER.readValue(json, Container.class);
+ assertNotNull(c);
+ assertTrue(c.field1 instanceof A);
+ assertEquals("AAA", ((A) c.field1).a);
+ assertTrue(c.field2 instanceof C);
+ assertEquals("CCC", ((C) c.field2).c);
+ }
+
+ // then with sub-class that does have similarly named property
+ {
+ ContainerWithExtra c = MAPPER.readValue(json, ContainerWithExtra.class);
+ assertNotNull(c);
+ assertEquals("1", c.type);
+ assertTrue(c.field1 instanceof A);
+ assertEquals("AAA", ((A) c.field1).a);
+ assertTrue(c.field2 instanceof C);
+ assertEquals("CCC", ((C) c.field2).c);
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/misc/BeanPropertyMapTest.java b/src/test/java/com/fasterxml/jackson/databind/misc/BeanPropertyMapTest.java
index eafa286..2240d5a 100644
--- a/src/test/java/com/fasterxml/jackson/databind/misc/BeanPropertyMapTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/misc/BeanPropertyMapTest.java
@@ -31,7 +31,8 @@
PropertyMetadata md = PropertyMetadata.STD_REQUIRED;
props.add(new ObjectIdValueProperty(new MyObjectIdReader("pk"), md));
props.add(new ObjectIdValueProperty(new MyObjectIdReader("firstName"), md));
- BeanPropertyMap propMap = new BeanPropertyMap(false, props);
+ BeanPropertyMap propMap = new BeanPropertyMap(false, props,
+ new HashMap<String,List<PropertyName>>());
propMap = propMap.withProperty(new ObjectIdValueProperty(new MyObjectIdReader("@id"), md));
assertNotNull(propMap);
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/mixins/TestMixinSerForMethods.java b/src/test/java/com/fasterxml/jackson/databind/mixins/TestMixinSerForMethods.java
index 3ab6ce1..a57cffe 100644
--- a/src/test/java/com/fasterxml/jackson/databind/mixins/TestMixinSerForMethods.java
+++ b/src/test/java/com/fasterxml/jackson/databind/mixins/TestMixinSerForMethods.java
@@ -62,13 +62,6 @@
@JsonIgnore
public String takeB() { return null; }
}
-
- interface ObjectMixIn
- {
- // and then ditto for hashCode..
- @Override
- @JsonProperty public int hashCode();
- }
static class EmptyBean { }
@@ -154,40 +147,6 @@
assertEquals(Integer.valueOf(42), result.get("x"));
}
- /**
- * Unit test for verifying that it is actually possible to attach
- * mix-in annotations to basic <code>Object.class</code>. This
- * will essentially apply to any and all Objects.
- */
- public void testObjectMixin() throws IOException
- {
- ObjectMapper mapper = new ObjectMapper();
- mapper.addMixIn(Object.class, ObjectMixIn.class);
-
- // First, with our bean...
- Map<String,Object> result = writeAndMap(mapper, new BaseClass("a", "b"));
-
- assertEquals(2, result.size());
- assertEquals("b", result.get("b"));
- Object ob = result.get("hashCode");
- assertNotNull(ob);
- assertEquals(Integer.class, ob.getClass());
-
- /* 15-Oct-2010, tatu: Actually, we now block serialization (attempts) of plain Objects, by default
- * (since generally that makes no sense -- may need to revisit). As such, need to comment out
- * this part of test
- */
- /* Hmmh. For plain Object.class... I suppose getClass() does
- * get serialized (and can't really be blocked either).
- * Fine.
- */
- result = writeAndMap(mapper, new BaseClass("a", "b"));
- assertEquals(2, result.size());
- ob = result.get("hashCode");
- assertNotNull(ob);
- assertEquals(Integer.class, ob.getClass());
- }
-
// [databind#688]
public void testCustomResolver() throws IOException
{
@@ -195,8 +154,8 @@
mapper.setMixInResolver(new ClassIntrospector.MixInResolver() {
@Override
public Class<?> findMixInClassFor(Class<?> target) {
- if (target == BaseClass.class) {
- return ObjectMixIn.class;
+ if (target == EmptyBean.class) {
+ return MixInForSimple.class;
}
return null;
}
@@ -206,10 +165,8 @@
return this;
}
});
- Map<String,Object> result = writeAndMap(mapper, new BaseClass("c", "d"));
- assertEquals(2, result.size());
- assertNotNull(result.get("hashCode"));
- assertTrue(result.containsKey("b"));
- assertFalse(result.containsKey("a"));
+ Map<String,Object> result = writeAndMap(mapper, new SimpleBean());
+ assertEquals(1, result.size());
+ assertEquals(Integer.valueOf(42), result.get("x"));
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleArgCheckTest.java b/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleArgCheckTest.java
new file mode 100644
index 0000000..f97e6e1
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleArgCheckTest.java
@@ -0,0 +1,154 @@
+package com.fasterxml.jackson.databind.module;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.core.Version;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.jsontype.NamedType;
+
+public class SimpleModuleArgCheckTest extends BaseMapTest
+{
+ /*
+ /**********************************************************
+ /* Unit tests for invalid deserializers
+ /**********************************************************
+ */
+
+ public void testInvalidForDeserializers() throws Exception
+ {
+ SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
+ (Map<Class<?>,JsonDeserializer<?>>) null);
+
+ try {
+ mod.addDeserializer(String.class, null);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as deserializer");
+ }
+
+ try {
+ mod.addKeyDeserializer(String.class, null);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as key deserializer");
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Unit tests for invalid serializers
+ /**********************************************************
+ */
+
+ public void testInvalidForSerializers() throws Exception
+ {
+ SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
+ (List<JsonSerializer<?>>) null);
+
+ try {
+ mod.addSerializer(String.class, null);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as serializer");
+ }
+
+ try {
+ mod.addSerializer((JsonSerializer<?>) null);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as serializer");
+ }
+
+ try {
+ mod.addKeySerializer(String.class, null);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as key serializer");
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Unit tests for invalid misc other
+ /**********************************************************
+ */
+
+ public void testInvalidAbstractTypeMapping() throws Exception
+ {
+ // just for funsies let's use more esoteric constructor
+ Map<Class<?>,JsonDeserializer<?>> desers = Collections.emptyMap();
+ List<JsonSerializer<?>> sers = Collections.emptyList();
+ SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
+ desers, sers);
+
+ try {
+ mod.addAbstractTypeMapping(null, String.class);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as abstract type to map");
+ }
+ try {
+ mod.addAbstractTypeMapping(String.class, null);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as concrete type to map to");
+ }
+ }
+
+ public void testInvalidSubtypeMappings() throws Exception
+ {
+ SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
+ null, null);
+ try {
+ mod.registerSubtypes(String.class, null);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as subtype to register");
+ }
+
+ try {
+ mod.registerSubtypes(new NamedType(Integer.class), (NamedType) null);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as subtype to register");
+ }
+ }
+
+ public void testInvalidValueInstantiator() throws Exception
+ {
+ SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
+
+ try {
+ mod.addValueInstantiator(null, null);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as class to register value instantiator for");
+ }
+ try {
+ mod.addValueInstantiator(CharSequence.class, null);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as value instantiator");
+ }
+ }
+
+ public void testInvalidMixIn() throws Exception
+ {
+ SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
+
+ try {
+ mod.setMixInAnnotation(null, String.class);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as target type");
+ }
+ try {
+ mod.setMixInAnnotation(String.class, null);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not pass `null` as mixin class");
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/module/TestSimpleModule.java b/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java
similarity index 93%
rename from src/test/java/com/fasterxml/jackson/databind/module/TestSimpleModule.java
rename to src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java
index 375b41c..bd30c67 100644
--- a/src/test/java/com/fasterxml/jackson/databind/module/TestSimpleModule.java
+++ b/src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleTest.java
@@ -11,18 +11,13 @@
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.module.SimpleSerializers;
+
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
@SuppressWarnings("serial")
-public class TestSimpleModule extends BaseMapTest
+public class SimpleModuleTest extends BaseMapTest
{
- /*
- /**********************************************************
- /* Helper classes; simple beans and their handlers
- /**********************************************************
- */
-
/**
* Trivial bean that requires custom serializer and deserializer
*/
@@ -210,7 +205,8 @@
mapper.readValue("{\"str\":\"ab\",\"num\":2}", CustomBean.class);
fail("Should have caused an exception");
} catch (IOException e) {
- verifyException(e, "No suitable constructor found");
+ verifyException(e, "Can not construct");
+ verifyException(e, "no creators");
}
}
@@ -234,17 +230,19 @@
ObjectMapper mapper = new ObjectMapper();
SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
mod.addSerializer(new SimpleEnumSerializer());
- mapper.registerModule(mod);
+ // for fun, call "multi-module" registration
+ mapper.registerModules(mod);
assertEquals(quote("b"), mapper.writeValueAsString(SimpleEnum.B));
}
- // for [JACKSON-550]
public void testSimpleInterfaceSerializer() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
mod.addSerializer(new BaseSerializer());
- mapper.registerModule(mod);
+ // and another variant here too
+ List<Module> mods = Arrays.asList((Module) mod);
+ mapper.registerModules(mods);
assertEquals(quote("Base:1"), mapper.writeValueAsString(new Impl1()));
assertEquals(quote("Base:2"), mapper.writeValueAsString(new Impl2()));
}
@@ -275,15 +273,17 @@
SimpleEnum result = mapper.readValue(quote("a"), SimpleEnum.class);
assertSame(SimpleEnum.A, result);
}
-
- // Simple verification of [JACKSON-455]
+
public void testMultipleModules() throws Exception
{
MySimpleModule mod1 = new MySimpleModule("test1", Version.unknownVersion());
SimpleModule mod2 = new SimpleModule("test2", Version.unknownVersion());
mod1.addSerializer(SimpleEnum.class, new SimpleEnumSerializer());
mod1.addDeserializer(CustomBean.class, new CustomBeanDeserializer());
- mod2.addDeserializer(SimpleEnum.class, new SimpleEnumDeserializer());
+
+ Map<Class<?>,JsonDeserializer<?>> desers = new HashMap<>();
+ desers.put(SimpleEnum.class, new SimpleEnumDeserializer());
+ mod2.setDeserializers(new SimpleDeserializers(desers));
mod2.addSerializer(CustomBean.class, new CustomBeanSerializer());
ObjectMapper mapper = new ObjectMapper();
@@ -307,8 +307,7 @@
/* Unit tests; other
/**********************************************************
*/
-
- // [JACKSON-644]: ability to register mix-ins
+
public void testMixIns() throws Exception
{
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
@@ -322,7 +321,6 @@
assertEquals(Integer.valueOf(2), props.get("b"));
}
- // [JACKSON-686]
public void testAccessToMapper() throws Exception
{
ContextVerifierModule module = new ContextVerifierModule();
@@ -339,4 +337,10 @@
Class<?> found = mapper.findMixInClassFor(Object.class);
assertEquals(String.class, found);
}
+
+ public void testAutoDiscovery() throws Exception
+ {
+ List<Module> mods = ObjectMapper.findModules();
+ assertEquals(0, mods.size());
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/module/TestCustomEnumKeyDeserializer.java b/src/test/java/com/fasterxml/jackson/databind/module/TestCustomEnumKeyDeserializer.java
index f99e320..bc5a53c 100644
--- a/src/test/java/com/fasterxml/jackson/databind/module/TestCustomEnumKeyDeserializer.java
+++ b/src/test/java/com/fasterxml/jackson/databind/module/TestCustomEnumKeyDeserializer.java
@@ -4,8 +4,6 @@
import java.io.IOException;
import java.util.*;
-import org.junit.Test;
-
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.TypeReference;
@@ -169,7 +167,6 @@
*/
// Test passing with the fix
- @Test
public void testWithEnumKeys() throws Exception {
ObjectMapper plainObjectMapper = new ObjectMapper();
JsonNode tree = plainObjectMapper.readTree(aposToQuotes("{'red' : [ 'a', 'b']}"));
diff --git a/src/test/java/com/fasterxml/jackson/databind/module/TestKeyDeserializers.java b/src/test/java/com/fasterxml/jackson/databind/module/TestKeyDeserializers.java
index a3f2227..a9bd0d8 100644
--- a/src/test/java/com/fasterxml/jackson/databind/module/TestKeyDeserializers.java
+++ b/src/test/java/com/fasterxml/jackson/databind/module/TestKeyDeserializers.java
@@ -4,10 +4,7 @@
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.BaseMapTest;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.KeyDeserializer;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.*;
public class TestKeyDeserializers extends BaseMapTest
{
@@ -25,7 +22,6 @@
public Foo(String v) { value = v; }
}
-
/*
/**********************************************************
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/ArrayNodeTest.java b/src/test/java/com/fasterxml/jackson/databind/node/ArrayNodeTest.java
new file mode 100644
index 0000000..62053c3
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/node/ArrayNodeTest.java
@@ -0,0 +1,346 @@
+package com.fasterxml.jackson.databind.node;
+
+import java.io.*;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayList;
+
+import com.fasterxml.jackson.core.*;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.TextNode;
+import com.fasterxml.jackson.databind.node.TreeTraversingParser;
+
+/**
+ * Additional tests for {@link ArrayNode} container class.
+ */
+public class ArrayNodeTest
+ extends BaseMapTest
+{
+ public void testDirectCreation() throws IOException
+ {
+ ArrayNode n = new ArrayNode(JsonNodeFactory.instance);
+ assertStandardEquals(n);
+ assertFalse(n.elements().hasNext());
+ assertFalse(n.fieldNames().hasNext());
+ TextNode text = TextNode.valueOf("x");
+ n.add(text);
+ assertEquals(1, n.size());
+ assertFalse(0 == n.hashCode());
+ assertTrue(n.elements().hasNext());
+ // no field names for arrays
+ assertFalse(n.fieldNames().hasNext());
+ assertNull(n.get("x")); // not used with arrays
+ assertTrue(n.path("x").isMissingNode());
+ assertSame(text, n.get(0));
+
+ // single element, so:
+ assertFalse(n.has("field"));
+ assertFalse(n.hasNonNull("field"));
+ assertTrue(n.has(0));
+ assertTrue(n.hasNonNull(0));
+ assertFalse(n.has(1));
+ assertFalse(n.hasNonNull(1));
+
+ // add null node too
+ n.add((JsonNode) null);
+ assertEquals(2, n.size());
+ assertTrue(n.get(1).isNull());
+ assertTrue(n.has(1));
+ assertFalse(n.hasNonNull(1));
+ // change to text
+ n.set(1, text);
+ assertSame(text, n.get(1));
+ n.set(0, null);
+ assertTrue(n.get(0).isNull());
+
+ // and finally, clear it all
+ ArrayNode n2 = new ArrayNode(JsonNodeFactory.instance);
+ n2.add("foobar");
+ assertFalse(n.equals(n2));
+ n.addAll(n2);
+ assertEquals(3, n.size());
+
+ assertFalse(n.get(0).isTextual());
+ assertNotNull(n.remove(0));
+ assertEquals(2, n.size());
+ assertTrue(n.get(0).isTextual());
+ assertNull(n.remove(-1));
+ assertNull(n.remove(100));
+ assertEquals(2, n.size());
+
+ ArrayList<JsonNode> nodes = new ArrayList<JsonNode>();
+ nodes.add(text);
+ n.addAll(nodes);
+ assertEquals(3, n.size());
+ assertNull(n.get(10000));
+ assertNull(n.remove(-4));
+
+ TextNode text2 = TextNode.valueOf("b");
+ n.insert(0, text2);
+ assertEquals(4, n.size());
+ assertSame(text2, n.get(0));
+
+ assertNotNull(n.addArray());
+ assertEquals(5, n.size());
+ n.addPOJO("foo");
+ assertEquals(6, n.size());
+
+ // Try serializing it for fun, too...
+ JsonGenerator g = objectMapper().getFactory().createGenerator(new StringWriter());
+ n.serialize(g, null);
+ g.close();
+
+ n.removeAll();
+ assertEquals(0, n.size());
+ }
+
+ public void testDirectCreation2() throws IOException
+ {
+ JsonNodeFactory f = objectMapper().getNodeFactory();
+ ArrayList<JsonNode> list = new ArrayList<>();
+ list.add(f.booleanNode(true));
+ list.add(f.textNode("foo"));
+ ArrayNode n = new ArrayNode(f, list);
+ assertEquals(2, n.size());
+ assertTrue(n.get(0).isBoolean());
+ assertTrue(n.get(1).isTextual());
+
+ // also, should fail with invalid set attempt
+ try {
+ n.set(2, f.nullNode());
+ fail("Should not pass");
+ } catch (IndexOutOfBoundsException e) {
+ verifyException(e, "illegal index");
+ }
+ n.insert(1, (String) null);
+ assertEquals(3, n.size());
+ assertTrue(n.get(0).isBoolean());
+ assertTrue(n.get(1).isNull());
+ assertTrue(n.get(2).isTextual());
+
+ n.removeAll();
+ n.insert(0, (JsonNode) null);
+ assertEquals(1, n.size());
+ assertTrue(n.get(0).isNull());
+ }
+
+ public void testArrayViaMapper() throws Exception
+ {
+ final String JSON = "[[[-0.027512,51.503221],[-0.008497,51.503221],[-0.008497,51.509744],[-0.027512,51.509744]]]";
+
+ JsonNode n = objectMapper().readTree(JSON);
+ assertNotNull(n);
+ assertTrue(n.isArray());
+ ArrayNode an = (ArrayNode) n;
+ assertEquals(1, an.size());
+ ArrayNode an2 = (ArrayNode) n.get(0);
+ assertTrue(an2.isArray());
+ assertEquals(4, an2.size());
+ }
+
+ public void testAdds()
+ {
+ ArrayNode n = new ArrayNode(JsonNodeFactory.instance);
+ assertNotNull(n.addArray());
+ assertNotNull(n.addObject());
+ n.addPOJO("foobar");
+ n.add(1);
+ n.add(1L);
+ n.add(0.5);
+ n.add(0.5f);
+ n.add(new BigDecimal("0.2"));
+ n.add(BigInteger.TEN);
+ assertEquals(9, n.size());
+
+ assertNotNull(n.insertArray(0));
+ assertNotNull(n.insertObject(0));
+ n.insertPOJO(2, "xxx");
+ assertEquals(12, n.size());
+
+ n.insert(0, BigInteger.ONE);
+ n.insert(0, new BigDecimal("0.1"));
+ assertEquals(14, n.size());
+ }
+
+ public void testNullAdds()
+ {
+ JsonNodeFactory f = objectMapper().getNodeFactory();
+ ArrayNode array = f.arrayNode(14);
+
+ array.add((BigDecimal) null);
+ array.add((BigInteger) null);
+ array.add((Boolean) null);
+ array.add((byte[]) null);
+ array.add((Double) null);
+ array.add((Float) null);
+ array.add((Integer) null);
+ array.add((JsonNode) null);
+ array.add((Long) null);
+ array.add((String) null);
+
+ assertEquals(10, array.size());
+
+ for (JsonNode node : array) {
+ assertTrue(node.isNull());
+ }
+ }
+
+ public void testNullInserts()
+ {
+ JsonNodeFactory f = objectMapper().getNodeFactory();
+ ArrayNode array = f.arrayNode(3);
+
+ array.insert(0, (BigDecimal) null);
+ array.insert(0, (BigInteger) null);
+ array.insert(0, (Boolean) null);
+ // Offsets out of the range are fine; negative become 0;
+ // super big just add at the end
+ array.insert(-56, (byte[]) null);
+ array.insert(0, (Double) null);
+ array.insert(200, (Float) null);
+ array.insert(0, (Integer) null);
+ array.insert(1, (JsonNode) null);
+ array.insert(array.size(), (Long) null);
+ array.insert(1, (String) null);
+
+ assertEquals(10, array.size());
+
+ for (JsonNode node : array) {
+ assertTrue(node.isNull());
+ }
+ }
+
+ public void testNullChecking()
+ {
+ ArrayNode a1 = JsonNodeFactory.instance.arrayNode();
+ ArrayNode a2 = JsonNodeFactory.instance.arrayNode();
+ // used to throw NPE before fix:
+ a1.addAll(a2);
+ assertEquals(0, a1.size());
+ assertEquals(0, a2.size());
+
+ a2.addAll(a1);
+ assertEquals(0, a1.size());
+ assertEquals(0, a2.size());
+ }
+
+ public void testNullChecking2()
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ ArrayNode src = mapper.createArrayNode();
+ ArrayNode dest = mapper.createArrayNode();
+ src.add("element");
+ dest.addAll(src);
+ }
+
+ public void testParser() throws Exception
+ {
+ ArrayNode n = new ArrayNode(JsonNodeFactory.instance);
+ n.add(123);
+ TreeTraversingParser p = new TreeTraversingParser(n, null);
+ p.setCodec(null);
+ assertNull(p.getCodec());
+ assertNotNull(p.getParsingContext());
+ assertNotNull(p.getTokenLocation());
+ assertNotNull(p.getCurrentLocation());
+ assertNull(p.getEmbeddedObject());
+ assertNull(p.currentNode());
+
+ //assertNull(p.getNumberType());
+
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
+ p.skipChildren();
+ assertToken(JsonToken.END_ARRAY, p.getCurrentToken());
+ p.close();
+
+ p = new TreeTraversingParser(n, null);
+ p.nextToken();
+ assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
+ assertEquals(JsonParser.NumberType.INT, p.getNumberType());
+ p.close();
+ }
+
+ public void testArrayNodeEquality()
+ {
+ ArrayNode n1 = new ArrayNode(null);
+ ArrayNode n2 = new ArrayNode(null);
+
+ assertTrue(n1.equals(n2));
+ assertTrue(n2.equals(n1));
+
+ n1.add(TextNode.valueOf("Test"));
+
+ assertFalse(n1.equals(n2));
+ assertFalse(n2.equals(n1));
+
+ n2.add(TextNode.valueOf("Test"));
+
+ assertTrue(n1.equals(n2));
+ assertTrue(n2.equals(n1));
+ }
+
+ public void testSimpleArray() throws Exception
+ {
+ ArrayNode result = objectMapper().createArrayNode();
+
+ assertTrue(result.isArray());
+ assertType(result, ArrayNode.class);
+
+ assertFalse(result.isObject());
+ assertFalse(result.isNumber());
+ assertFalse(result.isNull());
+ assertFalse(result.isTextual());
+
+ // and let's add stuff...
+ result.add(false);
+ result.insertNull(0);
+
+ // should be equal to itself no matter what
+ assertEquals(result, result);
+ assertFalse(result.equals(null)); // but not to null
+
+ // plus see that we can access stuff
+ assertEquals(NullNode.instance, result.path(0));
+ assertEquals(NullNode.instance, result.get(0));
+ assertEquals(BooleanNode.FALSE, result.path(1));
+ assertEquals(BooleanNode.FALSE, result.get(1));
+ assertEquals(2, result.size());
+
+ assertNull(result.get(-1));
+ assertNull(result.get(2));
+ JsonNode missing = result.path(2);
+ assertTrue(missing.isMissingNode());
+ assertTrue(result.path(-100).isMissingNode());
+
+ // then construct and compare
+ ArrayNode array2 = objectMapper().createArrayNode();
+ array2.addNull();
+ array2.add(false);
+ assertEquals(result, array2);
+
+ // plus remove entries
+ JsonNode rm1 = array2.remove(0);
+ assertEquals(NullNode.instance, rm1);
+ assertEquals(1, array2.size());
+ assertEquals(BooleanNode.FALSE, array2.get(0));
+ assertFalse(result.equals(array2));
+
+ JsonNode rm2 = array2.remove(0);
+ assertEquals(BooleanNode.FALSE, rm2);
+ assertEquals(0, array2.size());
+ }
+
+ public void testSimpleMismatch() throws Exception
+ {
+ ObjectMapper mapper = objectMapper();
+ try {
+ mapper.readValue(" 123 ", ArrayNode.class);
+ fail("Should not pass");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "out of VALUE_NUMBER_INT token");
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/JsonNodeFactoryTest.java b/src/test/java/com/fasterxml/jackson/databind/node/JsonNodeFactoryTest.java
new file mode 100644
index 0000000..3a646b4
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/node/JsonNodeFactoryTest.java
@@ -0,0 +1,37 @@
+package com.fasterxml.jackson.databind.node;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import com.fasterxml.jackson.databind.*;
+
+public class JsonNodeFactoryTest extends NodeTestBase
+{
+ private final ObjectMapper MAPPER = objectMapper();
+
+ public void testSimpleCreation()
+ {
+ JsonNodeFactory f = MAPPER.getNodeFactory();
+ JsonNode n;
+
+ n = f.numberNode((byte) 4);
+ assertTrue(n.isInt());
+ assertEquals(4, n.intValue());
+
+ assertTrue(f.numberNode((Byte) null).isNull());
+
+ assertTrue(f.numberNode((Short) null).isNull());
+
+ assertTrue(f.numberNode((Integer) null).isNull());
+
+ assertTrue(f.numberNode((Long) null).isNull());
+
+ assertTrue(f.numberNode((Float) null).isNull());
+
+ assertTrue(f.numberNode((Double) null).isNull());
+
+ assertTrue(f.numberNode((BigDecimal) null).isNull());
+
+ assertTrue(f.numberNode((BigInteger) null).isNull());
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java b/src/test/java/com/fasterxml/jackson/databind/node/NumberNodesTest.java
similarity index 76%
rename from src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java
rename to src/test/java/com/fasterxml/jackson/databind/node/NumberNodesTest.java
index f5b71c4..5f3b4cf 100644
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java
+++ b/src/test/java/com/fasterxml/jackson/databind/node/NumberNodesTest.java
@@ -12,8 +12,10 @@
* Basic tests for {@link JsonNode} implementations that
* contain numeric values.
*/
-public class TestNumberNodes extends NodeTestBase
+public class NumberNodesTest extends NodeTestBase
{
+ private final ObjectMapper MAPPER = objectMapper();
+
public void testShort()
{
ShortNode n = ShortNode.valueOf((short) 1);
@@ -37,8 +39,33 @@
assertTrue(ShortNode.valueOf(Short.MAX_VALUE).canConvertToLong());
assertTrue(ShortNode.valueOf(Short.MIN_VALUE).canConvertToLong());
}
-
- public void testInt()
+
+ public void testIntViaMapper() throws Exception
+ {
+ int value = -90184;
+ JsonNode result = MAPPER.readTree(String.valueOf(value));
+ assertTrue(result.isNumber());
+ assertTrue(result.isIntegralNumber());
+ assertTrue(result.isInt());
+ assertType(result, IntNode.class);
+ assertFalse(result.isLong());
+ assertFalse(result.isFloatingPointNumber());
+ assertFalse(result.isDouble());
+ assertFalse(result.isNull());
+ assertFalse(result.isTextual());
+ assertFalse(result.isMissingNode());
+
+ assertEquals(value, result.numberValue().intValue());
+ assertEquals(value, result.intValue());
+ assertEquals(String.valueOf(value), result.asText());
+ assertEquals((double) value, result.doubleValue());
+ assertEquals((long) value, result.longValue());
+
+ // also, equality should work ok
+ assertEquals(result, IntNode.valueOf(value));
+ }
+
+ public void testInt()
{
IntNode n = IntNode.valueOf(1);
assertStandardEquals(n);
@@ -62,6 +89,7 @@
assertTrue(IntNode.valueOf(0).canConvertToLong());
assertTrue(IntNode.valueOf(Integer.MAX_VALUE).canConvertToLong());
assertTrue(IntNode.valueOf(Integer.MIN_VALUE).canConvertToLong());
+
}
public void testLong()
@@ -92,6 +120,31 @@
assertTrue(LongNode.valueOf(Long.MIN_VALUE).canConvertToLong());
}
+ public void testLongViaMapper() throws Exception
+ {
+ // need to use something being 32-bit value space
+ long value = 12345678L << 32;
+ JsonNode result = MAPPER.readTree(String.valueOf(value));
+ assertTrue(result.isNumber());
+ assertTrue(result.isIntegralNumber());
+ assertTrue(result.isLong());
+ assertType(result, LongNode.class);
+ assertFalse(result.isInt());
+ assertFalse(result.isFloatingPointNumber());
+ assertFalse(result.isDouble());
+ assertFalse(result.isNull());
+ assertFalse(result.isTextual());
+ assertFalse(result.isMissingNode());
+
+ assertEquals(value, result.numberValue().longValue());
+ assertEquals(value, result.longValue());
+ assertEquals(String.valueOf(value), result.asText());
+ assertEquals((double) value, result.doubleValue());
+
+ // also, equality should work ok
+ assertEquals(result, LongNode.valueOf(value));
+ }
+
public void testDouble() throws Exception
{
DoubleNode n = DoubleNode.valueOf(0.25);
@@ -105,7 +158,6 @@
assertEquals(BigInteger.ZERO, n.bigIntegerValue());
assertEquals("0.25", n.asText());
- // 1.6:
assertNodeNumbers(DoubleNode.valueOf(4.5), 4, 4.5);
assertTrue(DoubleNode.valueOf(0).canConvertToInt());
@@ -125,6 +177,31 @@
assertEquals("-0.0", String.valueOf(n.doubleValue()));
}
+ public void testDoubleViaMapper() throws Exception
+ {
+ double value = 3.04;
+ JsonNode result = MAPPER.readTree(String.valueOf(value));
+ assertTrue(result.isNumber());
+ assertFalse(result.isNull());
+ assertType(result, DoubleNode.class);
+ assertTrue(result.isFloatingPointNumber());
+ assertTrue(result.isDouble());
+ assertFalse(result.isInt());
+ assertFalse(result.isLong());
+ assertFalse(result.isIntegralNumber());
+ assertFalse(result.isTextual());
+ assertFalse(result.isMissingNode());
+
+ assertEquals(value, result.doubleValue());
+ assertEquals(value, result.numberValue().doubleValue());
+ assertEquals((int) value, result.intValue());
+ assertEquals((long) value, result.longValue());
+ assertEquals(String.valueOf(value), result.asText());
+
+ // also, equality should work ok
+ assertEquals(result, DoubleNode.valueOf(value));
+ }
+
// @since 2.2
public void testFloat()
{
@@ -173,6 +250,7 @@
assertEquals(JsonParser.NumberType.BIG_DECIMAL, n.numberType());
assertTrue(n.isNumber());
assertFalse(n.isIntegralNumber());
+ assertFalse(n.isArray());
assertTrue(n.isBigDecimal());
assertEquals(BigDecimal.ONE, n.numberValue());
assertEquals(1, n.intValue());
@@ -180,7 +258,6 @@
assertEquals(BigDecimal.ONE, n.decimalValue());
assertEquals("1", n.asText());
- // 1.6:
assertNodeNumbers(n, 1, 1.0);
assertTrue(DecimalNode.valueOf(BigDecimal.ZERO).canConvertToInt());
@@ -192,8 +269,31 @@
assertTrue(DecimalNode.valueOf(BigDecimal.ZERO).canConvertToLong());
assertTrue(DecimalNode.valueOf(BigDecimal.valueOf(Long.MAX_VALUE)).canConvertToLong());
assertTrue(DecimalNode.valueOf(BigDecimal.valueOf(Long.MIN_VALUE)).canConvertToLong());
- }
+ // no "natural" way to get it, must construct
+ BigDecimal value = new BigDecimal("0.1");
+ JsonNode result = DecimalNode.valueOf(value);
+
+ assertFalse(result.isObject());
+ assertTrue(result.isNumber());
+ assertFalse(result.isIntegralNumber());
+ assertFalse(result.isLong());
+ assertType(result, DecimalNode.class);
+ assertFalse(result.isInt());
+ assertTrue(result.isFloatingPointNumber());
+ assertTrue(result.isBigDecimal());
+ assertFalse(result.isDouble());
+ assertFalse(result.isNull());
+ assertFalse(result.isTextual());
+ assertFalse(result.isMissingNode());
+
+ assertEquals(value, result.numberValue());
+ assertEquals(value.toString(), result.asText());
+
+ // also, equality should work ok
+ assertEquals(result, DecimalNode.valueOf(value));
+ }
+
public void testDecimalNodeEqualsHashCode()
{
/*
@@ -284,7 +384,6 @@
assertEquals("100", mapper.writeValueAsString(tree));
}
- // Related to [Issue#333]
public void testCanonicalNumbers() throws Exception
{
JsonNodeFactory f = new JsonNodeFactory();
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestObjectNode.java b/src/test/java/com/fasterxml/jackson/databind/node/ObjectNodeTest.java
similarity index 90%
rename from src/test/java/com/fasterxml/jackson/databind/node/TestObjectNode.java
rename to src/test/java/com/fasterxml/jackson/databind/node/ObjectNodeTest.java
index 719c109..b617d68 100644
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestObjectNode.java
+++ b/src/test/java/com/fasterxml/jackson/databind/node/ObjectNodeTest.java
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.databind.node;
import java.math.BigDecimal;
+import java.math.BigInteger;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -8,11 +9,12 @@
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
/**
* Additional tests for {@link ObjectNode} container class.
*/
-public class TestObjectNode
+public class ObjectNodeTest
extends BaseMapTest
{
@JsonDeserialize(as = DataImpl.class)
@@ -80,7 +82,6 @@
assertTrue(root.isObject());
assertEquals(2, root.size());
- // Related to [JACKSON-50]:
Iterator<JsonNode> it = root.iterator();
assertNotNull(it);
assertTrue(it.hasNext());
@@ -115,7 +116,7 @@
assertEquals(IntNode.valueOf(1), root.get("key"));
assertNull(root.get("b"));
}
- // for [Issue#346]
+ // for [databind#346]
public void testEmptyNodeAsValue() throws Exception
{
Data w = MAPPER.readValue("{}", Data.class);
@@ -178,6 +179,24 @@
assertEquals(0, n.size());
}
+ public void testBigNumbers()
+ {
+ ObjectNode n = new ObjectNode(JsonNodeFactory.instance);
+ assertStandardEquals(n);
+ BigInteger I = BigInteger.valueOf(3);
+ BigDecimal DEC = new BigDecimal("0.1");
+
+ n.put("a", DEC);
+ n.put("b", I);
+
+ assertEquals(2, n.size());
+
+ assertTrue(n.path("a").isBigDecimal());
+ assertEquals(DEC, n.get("a").decimalValue());
+ assertTrue(n.path("b").isBigInteger());
+ assertEquals(I, n.get("b").bigIntegerValue());
+ }
+
/**
* Verify null handling
*/
@@ -205,6 +224,13 @@
n = o1.get("d");
assertNotNull(n);
assertSame(n, NullNode.instance);
+
+ o1.put("3", (BigInteger) null);
+ n = o1.get("3");
+ assertNotNull(3);
+ assertSame(n, NullNode.instance);
+
+ assertEquals(4, o1.size());
}
/**
@@ -340,7 +366,7 @@
assertEquals(1, root3.path("a").intValue());
}
- // [Issue#237] (databind): support DeserializationFeature#FAIL_ON_READING_DUP_TREE_KEY
+ // [databind#237] (databind): support DeserializationFeature#FAIL_ON_READING_DUP_TREE_KEY
public void testFailOnDupKeys() throws Exception
{
final String DUP_JSON = "{ \"a\":1, \"a\":2 }";
@@ -413,4 +439,15 @@
// System.out.println("Deserialized to MyValue: "+de2);
assertNotNull(de2);
}
+
+ public void testSimpleMismatch() throws Exception
+ {
+ ObjectMapper mapper = objectMapper();
+ try {
+ mapper.readValue("[ 1, 2, 3 ]", ObjectNode.class);
+ fail("Should not pass");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "out of START_ARRAY token");
+ }
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestArrayNode.java b/src/test/java/com/fasterxml/jackson/databind/node/TestArrayNode.java
deleted file mode 100644
index 901910e..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestArrayNode.java
+++ /dev/null
@@ -1,168 +0,0 @@
-package com.fasterxml.jackson.databind.node;
-
-import java.io.*;
-import java.util.*;
-
-
-import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.*;
-import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.JsonNodeFactory;
-import com.fasterxml.jackson.databind.node.TextNode;
-import com.fasterxml.jackson.databind.node.TreeTraversingParser;
-
-/**
- * Additional tests for {@link ArrayNode} container class.
- */
-public class TestArrayNode
- extends BaseMapTest
-{
- public void testBasics() throws IOException
- {
- ArrayNode n = new ArrayNode(JsonNodeFactory.instance);
- assertStandardEquals(n);
- assertFalse(n.elements().hasNext());
- assertFalse(n.fieldNames().hasNext());
- TextNode text = TextNode.valueOf("x");
- n.add(text);
- assertEquals(1, n.size());
- assertFalse(0 == n.hashCode());
- assertTrue(n.elements().hasNext());
- // no field names for arrays
- assertFalse(n.fieldNames().hasNext());
- assertNull(n.get("x")); // not used with arrays
- assertTrue(n.path("x").isMissingNode());
- assertSame(text, n.get(0));
-
- // single element, so:
- assertFalse(n.has("field"));
- assertFalse(n.hasNonNull("field"));
- assertTrue(n.has(0));
- assertTrue(n.hasNonNull(0));
- assertFalse(n.has(1));
- assertFalse(n.hasNonNull(1));
-
- // add null node too
- n.add((JsonNode) null);
- assertEquals(2, n.size());
- assertTrue(n.get(1).isNull());
- assertTrue(n.has(1));
- assertFalse(n.hasNonNull(1));
- // change to text
- n.set(1, text);
- assertSame(text, n.get(1));
- n.set(0, null);
- assertTrue(n.get(0).isNull());
-
- // and finally, clear it all
- ArrayNode n2 = new ArrayNode(JsonNodeFactory.instance);
- n2.add("foobar");
- assertFalse(n.equals(n2));
- n.addAll(n2);
- assertEquals(3, n.size());
-
- assertFalse(n.get(0).isTextual());
- assertNotNull(n.remove(0));
- assertEquals(2, n.size());
- assertTrue(n.get(0).isTextual());
-
- ArrayList<JsonNode> nodes = new ArrayList<JsonNode>();
- nodes.add(text);
- n.addAll(nodes);
- assertEquals(3, n.size());
- assertNull(n.get(10000));
- assertNull(n.remove(-4));
-
- TextNode text2 = TextNode.valueOf("b");
- n.insert(0, text2);
- assertEquals(4, n.size());
- assertSame(text2, n.get(0));
-
- assertNotNull(n.addArray());
- assertEquals(5, n.size());
- n.addPOJO("foo");
- assertEquals(6, n.size());
-
- // Try serializing it for fun, too...
- JsonGenerator jg = new MappingJsonFactory().createGenerator(new StringWriter());
- n.serialize(jg, null);
-
- n.removeAll();
- assertEquals(0, n.size());
- jg.close();
- }
-
- public void testAdds()
- {
- ArrayNode n = new ArrayNode(JsonNodeFactory.instance);
- assertNotNull(n.addArray());
- assertNotNull(n.addObject());
- n.addPOJO("foobar");
- n.add(1);
- n.add(1L);
- n.add(0.5);
- n.add(0.5f);
- assertEquals(7, n.size());
-
- assertNotNull(n.insertArray(0));
- assertNotNull(n.insertObject(0));
- n.insertPOJO(2, "xxx");
- assertEquals(10, n.size());
- }
-
- /**
- * Test to verify [JACKSON-227]
- */
- public void testNullChecking()
- {
- ArrayNode a1 = JsonNodeFactory.instance.arrayNode();
- ArrayNode a2 = JsonNodeFactory.instance.arrayNode();
- // used to throw NPE before fix:
- a1.addAll(a2);
- assertEquals(0, a1.size());
- assertEquals(0, a2.size());
-
- a2.addAll(a1);
- assertEquals(0, a1.size());
- assertEquals(0, a2.size());
- }
-
- /**
- * Another test to verify [JACKSON-227]...
- */
- public void testNullChecking2()
- {
- ObjectMapper mapper = new ObjectMapper();
- ArrayNode src = mapper.createArrayNode();
- ArrayNode dest = mapper.createArrayNode();
- src.add("element");
- dest.addAll(src);
- }
-
- public void testParser() throws Exception
- {
- ArrayNode n = new ArrayNode(JsonNodeFactory.instance);
- n.add(123);
- TreeTraversingParser p = new TreeTraversingParser(n, null);
- p.setCodec(null);
- assertNull(p.getCodec());
- assertNotNull(p.getParsingContext());
- assertNotNull(p.getTokenLocation());
- assertNotNull(p.getCurrentLocation());
- assertNull(p.getEmbeddedObject());
- assertNull(p.currentNode());
-
- //assertNull(p.getNumberType());
-
- assertToken(JsonToken.START_ARRAY, p.nextToken());
- p.skipChildren();
- assertToken(JsonToken.END_ARRAY, p.getCurrentToken());
- p.close();
-
- p = new TreeTraversingParser(n, null);
- p.nextToken();
- assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
- assertEquals(JsonParser.NumberType.INT, p.getNumberType());
- p.close();
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestJsonNode.java b/src/test/java/com/fasterxml/jackson/databind/node/TestJsonNode.java
index 0dbb91f..0750fef 100644
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestJsonNode.java
+++ b/src/test/java/com/fasterxml/jackson/databind/node/TestJsonNode.java
@@ -15,36 +15,7 @@
{
private final ObjectMapper MAPPER = objectMapper();
- public void testText()
- {
- assertNull(TextNode.valueOf(null));
- TextNode empty = TextNode.valueOf("");
- assertStandardEquals(empty);
- assertSame(TextNode.EMPTY_STRING_NODE, empty);
-
- // 1.6:
- assertNodeNumbers(TextNode.valueOf("-3"), -3, -3.0);
- assertNodeNumbers(TextNode.valueOf("17.75"), 17, 17.75);
-
- // [JACKSON-587]
- long value = 127353264013893L;
- TextNode n = TextNode.valueOf(String.valueOf(value));
- assertEquals(value, n.asLong());
-
- // and then with non-numeric input
- n = TextNode.valueOf("foobar");
- assertNodeNumbersForNonNumeric(n);
-
- assertEquals("foobar", n.asText("barf"));
- assertEquals("", empty.asText("xyz"));
-
- assertTrue(TextNode.valueOf("true").asBoolean(true));
- assertTrue(TextNode.valueOf("true").asBoolean(false));
- assertFalse(TextNode.valueOf("false").asBoolean(true));
- assertFalse(TextNode.valueOf("false").asBoolean(false));
- }
-
- public void testBoolean()
+ public void testBoolean() throws Exception
{
BooleanNode f = BooleanNode.getFalse();
assertNotNull(f);
@@ -67,11 +38,23 @@
assertEquals("true", t.asText());
assertEquals(JsonToken.VALUE_TRUE, t.asToken());
- // 1.6:
assertNodeNumbers(f, 0, 0.0);
assertNodeNumbers(t, 1, 1.0);
- }
+
+ JsonNode result = objectMapper().readTree("true\n");
+ assertFalse(result.isNull());
+ assertFalse(result.isNumber());
+ assertFalse(result.isTextual());
+ assertTrue(result.isBoolean());
+ assertType(result, BooleanNode.class);
+ assertTrue(result.booleanValue());
+ assertEquals("true", result.asText());
+ assertFalse(result.isMissingNode());
+ // also, equality should work ok
+ assertEquals(result, BooleanNode.valueOf(true));
+ assertEquals(result, BooleanNode.getTrue());
+ }
public void testBinary() throws Exception
{
@@ -147,6 +130,10 @@
assertTrue(root1.equals(root1));
assertTrue(root2.equals(root2));
+ assertTrue(nestedArray1.equals(nestedArray1));
+ assertFalse(nestedArray1.equals(nestedArray2));
+ assertFalse(nestedArray2.equals(nestedArray1));
+
// but. Custom comparator can make all the difference
Comparator<JsonNode> cmp = new Comparator<JsonNode>() {
@@ -159,11 +146,15 @@
return 0;
}
if ((o1 instanceof NumericNode) && (o2 instanceof NumericNode)) {
- double d1 = ((NumericNode) o1).asDouble();
- double d2 = ((NumericNode) o2).asDouble();
+ int d1 = ((NumericNode) o1).asInt();
+ int d2 = ((NumericNode) o2).asInt();
if (d1 == d2) { // strictly equals because it's integral value
return 0;
}
+ if (d1 < d2) {
+ return -1;
+ }
+ return 1;
}
return 0;
}
@@ -172,6 +163,14 @@
assertTrue(root2.equals(cmp, root1));
assertTrue(root1.equals(cmp, root1));
assertTrue(root2.equals(cmp, root2));
+
+ ArrayNode array3 = MAPPER.createArrayNode();
+ array3.add(123);
+
+ assertFalse(root2.equals(cmp, nestedArray1));
+ assertTrue(nestedArray1.equals(cmp, nestedArray1));
+ assertFalse(nestedArray1.equals(cmp, root2));
+ assertFalse(nestedArray1.equals(cmp, array3));
}
// [databind#793]
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestMissingNode.java b/src/test/java/com/fasterxml/jackson/databind/node/TestMissingNode.java
index c09d619..45f0bac 100644
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestMissingNode.java
+++ b/src/test/java/com/fasterxml/jackson/databind/node/TestMissingNode.java
@@ -1,6 +1,10 @@
package com.fasterxml.jackson.databind.node;
+import java.io.StringReader;
+import java.util.Iterator;
+
import com.fasterxml.jackson.core.JsonToken;
+import com.fasterxml.jackson.databind.JsonNode;
public class TestMissingNode extends NodeTestBase
{
@@ -9,17 +13,12 @@
MissingNode n = MissingNode.getInstance();
assertTrue(n.isMissingNode());
assertEquals(JsonToken.NOT_AVAILABLE, n.asToken());
- // as per [JACKSON-775]
assertEquals("", n.asText());
assertStandardEquals(n);
assertEquals("", n.toString());
- /* As of 2.0, MissingNode is considered non-numeric, meaning
- * that default values are served.
- */
assertNodeNumbersForNonNumeric(n);
- // [JACKSON-823]
assertTrue(n.asBoolean(true));
assertEquals(4, n.asInt(4));
assertEquals(5L, n.asLong(5));
@@ -27,4 +26,69 @@
assertEquals("foo", n.asText("foo"));
}
+
+ /**
+ * Let's also verify behavior of "MissingNode" -- one needs to be able
+ * to traverse such bogus nodes with appropriate methods.
+ */
+ @SuppressWarnings("unused")
+ public void testMissingViaMapper() throws Exception
+ {
+ String JSON = "[ { }, [ ] ]";
+ JsonNode result = objectMapper().readTree(new StringReader(JSON));
+
+ assertTrue(result.isContainerNode());
+ assertTrue(result.isArray());
+ assertEquals(2, result.size());
+
+ int count = 0;
+ for (JsonNode node : result) {
+ ++count;
+ }
+ assertEquals(2, count);
+
+ Iterator<JsonNode> it = result.iterator();
+
+ JsonNode onode = it.next();
+ assertTrue(onode.isContainerNode());
+ assertTrue(onode.isObject());
+ assertEquals(0, onode.size());
+ assertFalse(onode.isMissingNode()); // real node
+ assertNull(onode.textValue());
+
+ // how about dereferencing?
+ assertNull(onode.get(0));
+ JsonNode dummyNode = onode.path(0);
+ assertNotNull(dummyNode);
+ assertTrue(dummyNode.isMissingNode());
+ assertNull(dummyNode.get(3));
+ assertNull(dummyNode.get("whatever"));
+ JsonNode dummyNode2 = dummyNode.path(98);
+ assertNotNull(dummyNode2);
+ assertTrue(dummyNode2.isMissingNode());
+ JsonNode dummyNode3 = dummyNode.path("field");
+ assertNotNull(dummyNode3);
+ assertTrue(dummyNode3.isMissingNode());
+
+ // and same for the array node
+
+ JsonNode anode = it.next();
+ assertTrue(anode.isContainerNode());
+ assertTrue(anode.isArray());
+ assertFalse(anode.isMissingNode()); // real node
+ assertEquals(0, anode.size());
+
+ assertNull(anode.get(0));
+ dummyNode = anode.path(0);
+ assertNotNull(dummyNode);
+ assertTrue(dummyNode.isMissingNode());
+ assertNull(dummyNode.get(0));
+ assertNull(dummyNode.get("myfield"));
+ dummyNode2 = dummyNode.path(98);
+ assertNotNull(dummyNode2);
+ assertTrue(dummyNode2.isMissingNode());
+ dummyNode3 = dummyNode.path("f");
+ assertNotNull(dummyNode3);
+ assertTrue(dummyNode3.isMissingNode());
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestNullNode.java b/src/test/java/com/fasterxml/jackson/databind/node/TestNullNode.java
index c807b03..99f6eab 100644
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestNullNode.java
+++ b/src/test/java/com/fasterxml/jackson/databind/node/TestNullNode.java
@@ -1,10 +1,22 @@
package com.fasterxml.jackson.databind.node;
+import java.io.StringWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
public class TestNullNode extends NodeTestBase
{
+ final static class CovarianceBean {
+ ObjectNode _object;
+ ArrayNode _array;
+
+ public void setObject(ObjectNode n) { _object = n; }
+ public void setArray(ArrayNode n) { _array = n; }
+ }
+
public void testBasicsWithNullNode() throws Exception
{
// Let's use something that doesn't add much beyond JsonNode base
@@ -42,4 +54,47 @@
// 2.4
assertEquals("foo", n.asText("foo"));
}
+
+ public void testNullHandling() throws Exception
+ {
+ // First, a stand-alone null
+ JsonNode n = objectReader().readTree("null");
+ assertNotNull(n);
+ assertTrue(n.isNull());
+ assertFalse(n.isNumber());
+ assertFalse(n.isTextual());
+ assertEquals("null", n.asText());
+ assertEquals(n, NullNode.instance);
+
+ n = objectMapper().readTree("null");
+ assertNotNull(n);
+ assertTrue(n.isNull());
+
+ // Then object property
+ ObjectNode root = (ObjectNode) objectReader().readTree("{\"x\":null}");
+ assertEquals(1, root.size());
+ n = root.get("x");
+ assertNotNull(n);
+ assertTrue(n.isNull());
+ }
+
+ public void testNullSerialization() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ StringWriter sw = new StringWriter();
+ mapper.writeValue(sw, NullNode.instance);
+ assertEquals("null", sw.toString());
+ }
+
+ public void testNullHandlingCovariance() throws Exception
+ {
+ String JSON = "{\"object\" : null, \"array\" : null }";
+ CovarianceBean bean = objectMapper().readValue(JSON, CovarianceBean.class);
+
+ ObjectNode on = bean._object;
+ assertNull(on);
+
+ ArrayNode an = bean._array;
+ assertNull(an);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeDeserialization.java
index 0c88cdc..5bf8d85 100644
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeDeserialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeDeserialization.java
@@ -3,7 +3,6 @@
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
-import java.io.IOException;
/**
* This unit test suite tries to verify that JsonNode-based trees
@@ -26,44 +25,6 @@
/**********************************************************
*/
- /**
- * This test checks that is possible to mix "regular" Java objects
- * and JsonNode.
- */
- public void testMixed() throws IOException
- {
- ObjectMapper om = new ObjectMapper();
- String JSON = "{\"node\" : { \"a\" : 3 }, \"x\" : 9 }";
- Bean bean = om.readValue(JSON, Bean.class);
-
- assertEquals(9, bean._x);
- JsonNode n = bean._node;
- assertNotNull(n);
- assertEquals(1, n.size());
- ObjectNode on = (ObjectNode) n;
- assertEquals(3, on.get("a").intValue());
- }
-
- /// Verifying [JACKSON-143]
- public void testArrayNodeEquality()
- {
- ArrayNode n1 = new ArrayNode(null);
- ArrayNode n2 = new ArrayNode(null);
-
- assertTrue(n1.equals(n2));
- assertTrue(n2.equals(n1));
-
- n1.add(TextNode.valueOf("Test"));
-
- assertFalse(n1.equals(n2));
- assertFalse(n2.equals(n1));
-
- n2.add(TextNode.valueOf("Test"));
-
- assertTrue(n1.equals(n2));
- assertTrue(n2.equals(n1));
- }
-
public void testObjectNodeEquality()
{
ObjectNode n1 = new ObjectNode(null);
@@ -96,44 +57,4 @@
String value = out.path("field").asText();
assertNotNull(value);
}
-
- // Issue#186
- public void testNullHandling() throws Exception
- {
- // First, a stand-alone null
- JsonNode n = objectReader().readTree("null");
- assertNotNull(n);
- assertTrue(n.isNull());
-
- n = objectMapper().readTree("null");
- assertNotNull(n);
- assertTrue(n.isNull());
-
- // Then object property
- ObjectNode root = (ObjectNode) objectReader().readTree("{\"x\":null}");
- assertEquals(1, root.size());
- n = root.get("x");
- assertNotNull(n);
- assertTrue(n.isNull());
- }
-
- final static class CovarianceBean {
- ObjectNode _object;
- ArrayNode _array;
-
- public void setObject(ObjectNode n) { _object = n; }
- public void setArray(ArrayNode n) { _array = n; }
- }
-
- public void testNullHandlingCovariance() throws Exception
- {
- String JSON = "{\"object\" : null, \"array\" : null }";
- CovarianceBean bean = objectMapper().readValue(JSON, CovarianceBean.class);
-
- ObjectNode on = bean._object;
- assertNull(on);
-
- ArrayNode an = bean._array;
- assertNull(an);
- }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeMapperDeserializer.java b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeMapperDeserializer.java
deleted file mode 100644
index 37410aa..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeMapperDeserializer.java
+++ /dev/null
@@ -1,425 +0,0 @@
-package com.fasterxml.jackson.databind.node;
-
-import java.io.*;
-import java.math.BigDecimal;
-import java.util.*;
-
-import com.fasterxml.jackson.core.*;
-import com.fasterxml.jackson.databind.*;
-
-/**
- * This unit test suite tries to verify that ObjectMapper
- * can properly parse JSON and bind contents into appropriate
- * JsonNode instances.
- */
-public class TestTreeMapperDeserializer extends BaseMapTest
-{
- public void testSimple()
- throws Exception
- {
- final String JSON = SAMPLE_DOC_JSON_SPEC;
-
- for (int type = 0; type < 2; ++type) {
- JsonNode result;
-
- if (type == 0) {
- result = objectMapper().readTree(new StringReader(JSON));
- } else {
- result = objectMapper().readTree(JSON);
- }
-
- assertType(result, ObjectNode.class);
- assertEquals(1, result.size());
- assertTrue(result.isObject());
-
- ObjectNode main = (ObjectNode) result;
- assertEquals("Image", main.fieldNames().next());
- JsonNode ob = main.elements().next();
- assertType(ob, ObjectNode.class);
- ObjectNode imageMap = (ObjectNode) ob;
-
- assertEquals(5, imageMap.size());
- ob = imageMap.get("Width");
- assertTrue(ob.isIntegralNumber());
- assertFalse(ob.isFloatingPointNumber());
- assertEquals(SAMPLE_SPEC_VALUE_WIDTH, ob.intValue());
- ob = imageMap.get("Height");
- assertTrue(ob.isIntegralNumber());
- assertEquals(SAMPLE_SPEC_VALUE_HEIGHT, ob.intValue());
-
- ob = imageMap.get("Title");
- assertTrue(ob.isTextual());
- assertEquals(SAMPLE_SPEC_VALUE_TITLE, ob.textValue());
-
- ob = imageMap.get("Thumbnail");
- assertType(ob, ObjectNode.class);
- ObjectNode tn = (ObjectNode) ob;
- ob = tn.get("Url");
- assertTrue(ob.isTextual());
- assertEquals(SAMPLE_SPEC_VALUE_TN_URL, ob.textValue());
- ob = tn.get("Height");
- assertTrue(ob.isIntegralNumber());
- assertEquals(SAMPLE_SPEC_VALUE_TN_HEIGHT, ob.intValue());
- ob = tn.get("Width");
- assertTrue(ob.isTextual());
- assertEquals(SAMPLE_SPEC_VALUE_TN_WIDTH, ob.textValue());
-
- ob = imageMap.get("IDs");
- assertTrue(ob.isArray());
- ArrayNode idList = (ArrayNode) ob;
- assertEquals(4, idList.size());
- assertEquals(4, calcLength(idList.elements()));
- assertEquals(4, calcLength(idList.iterator()));
- {
- int[] values = new int[] {
- SAMPLE_SPEC_VALUE_TN_ID1,
- SAMPLE_SPEC_VALUE_TN_ID2,
- SAMPLE_SPEC_VALUE_TN_ID3,
- SAMPLE_SPEC_VALUE_TN_ID4
- };
- for (int i = 0; i < values.length; ++i) {
- assertEquals(values[i], idList.get(i).intValue());
- }
- int i = 0;
- for (JsonNode n : idList) {
- assertEquals(values[i], n.intValue());
- ++i;
- }
- }
- }
- }
-
- public void testBoolean()
- throws Exception
- {
- JsonNode result = objectMapper().readTree("true\n");
- assertFalse(result.isNull());
- assertFalse(result.isNumber());
- assertFalse(result.isTextual());
- assertTrue(result.isBoolean());
- assertType(result, BooleanNode.class);
- assertTrue(result.booleanValue());
- assertEquals("true", result.asText());
- assertFalse(result.isMissingNode());
-
- // also, equality should work ok
- assertEquals(result, BooleanNode.valueOf(true));
- assertEquals(result, BooleanNode.getTrue());
- }
-
- public void testDouble()
- throws Exception
- {
- double value = 3.04;
- JsonNode result = objectMapper().readTree(String.valueOf(value));
- assertTrue(result.isNumber());
- assertFalse(result.isNull());
- assertType(result, DoubleNode.class);
- assertTrue(result.isFloatingPointNumber());
- assertTrue(result.isDouble());
- assertFalse(result.isInt());
- assertFalse(result.isLong());
- assertFalse(result.isIntegralNumber());
- assertFalse(result.isTextual());
- assertFalse(result.isMissingNode());
-
- assertEquals(value, result.doubleValue());
- assertEquals(value, result.numberValue().doubleValue());
- assertEquals((int) value, result.intValue());
- assertEquals((long) value, result.longValue());
- assertEquals(String.valueOf(value), result.asText());
-
- // also, equality should work ok
- assertEquals(result, DoubleNode.valueOf(value));
- }
-
- public void testInt()
- throws Exception
- {
- int value = -90184;
- JsonNode result = objectMapper().readTree(String.valueOf(value));
- assertTrue(result.isNumber());
- assertTrue(result.isIntegralNumber());
- assertTrue(result.isInt());
- assertType(result, IntNode.class);
- assertFalse(result.isLong());
- assertFalse(result.isFloatingPointNumber());
- assertFalse(result.isDouble());
- assertFalse(result.isNull());
- assertFalse(result.isTextual());
- assertFalse(result.isMissingNode());
-
- assertEquals(value, result.numberValue().intValue());
- assertEquals(value, result.intValue());
- assertEquals(String.valueOf(value), result.asText());
- assertEquals((double) value, result.doubleValue());
- assertEquals((long) value, result.longValue());
-
- // also, equality should work ok
- assertEquals(result, IntNode.valueOf(value));
- }
-
- public void testLong() throws Exception
- {
- // need to use something being 32-bit value space
- long value = 12345678L << 32;
- JsonNode result = objectMapper().readTree(String.valueOf(value));
- assertTrue(result.isNumber());
- assertTrue(result.isIntegralNumber());
- assertTrue(result.isLong());
- assertType(result, LongNode.class);
- assertFalse(result.isInt());
- assertFalse(result.isFloatingPointNumber());
- assertFalse(result.isDouble());
- assertFalse(result.isNull());
- assertFalse(result.isTextual());
- assertFalse(result.isMissingNode());
-
- assertEquals(value, result.numberValue().longValue());
- assertEquals(value, result.longValue());
- assertEquals(String.valueOf(value), result.asText());
- assertEquals((double) value, result.doubleValue());
-
- // also, equality should work ok
- assertEquals(result, LongNode.valueOf(value));
- }
-
- public void testNull() throws Exception
- {
- JsonNode result = objectMapper().readTree(" null ");
- // should not get java null, but NullNode...
- assertNotNull(result);
- assertTrue(result.isNull());
- assertFalse(result.isNumber());
- assertFalse(result.isTextual());
- assertEquals("null", result.asText());
-
- // also, equality should work ok
- assertEquals(result, NullNode.instance);
- }
-
- public void testDecimalNode()
- throws Exception
- {
- // no "natural" way to get it, must construct
- BigDecimal value = new BigDecimal("0.1");
- JsonNode result = DecimalNode.valueOf(value);
-
- assertFalse(result.isArray());
- assertFalse(result.isObject());
- assertTrue(result.isNumber());
- assertFalse(result.isIntegralNumber());
- assertFalse(result.isLong());
- assertType(result, DecimalNode.class);
- assertFalse(result.isInt());
- assertTrue(result.isFloatingPointNumber());
- assertTrue(result.isBigDecimal());
- assertFalse(result.isDouble());
- assertFalse(result.isNull());
- assertFalse(result.isTextual());
- assertFalse(result.isMissingNode());
-
- assertEquals(value, result.numberValue());
- assertEquals(value.toString(), result.asText());
-
- // also, equality should work ok
- assertEquals(result, DecimalNode.valueOf(value));
- }
-
- public void testSimpleArray() throws Exception
- {
- ArrayNode result = objectMapper().createArrayNode();
-
- assertTrue(result.isArray());
- assertType(result, ArrayNode.class);
-
- assertFalse(result.isObject());
- assertFalse(result.isNumber());
- assertFalse(result.isNull());
- assertFalse(result.isTextual());
-
- // and let's add stuff...
- result.add(false);
- result.insertNull(0);
-
- // should be equal to itself no matter what
- assertEquals(result, result);
- assertFalse(result.equals(null)); // but not to null
-
- // plus see that we can access stuff
- assertEquals(NullNode.instance, result.path(0));
- assertEquals(NullNode.instance, result.get(0));
- assertEquals(BooleanNode.FALSE, result.path(1));
- assertEquals(BooleanNode.FALSE, result.get(1));
- assertEquals(2, result.size());
-
- assertNull(result.get(-1));
- assertNull(result.get(2));
- JsonNode missing = result.path(2);
- assertTrue(missing.isMissingNode());
- assertTrue(result.path(-100).isMissingNode());
-
- // then construct and compare
- ArrayNode array2 = objectMapper().createArrayNode();
- array2.addNull();
- array2.add(false);
- assertEquals(result, array2);
-
- // plus remove entries
- JsonNode rm1 = array2.remove(0);
- assertEquals(NullNode.instance, rm1);
- assertEquals(1, array2.size());
- assertEquals(BooleanNode.FALSE, array2.get(0));
- assertFalse(result.equals(array2));
-
- JsonNode rm2 = array2.remove(0);
- assertEquals(BooleanNode.FALSE, rm2);
- assertEquals(0, array2.size());
- }
-
- /**
- * Type mappers should be able to gracefully deal with end of
- * input.
- */
- public void testEOF() throws Exception
- {
- String JSON =
- "{ \"key\": [ { \"a\" : { \"name\": \"foo\", \"type\": 1\n"
- +"}, \"type\": 3, \"url\": \"http://www.google.com\" } ],\n"
- +"\"name\": \"xyz\", \"type\": 1, \"url\" : null }\n "
- ;
- JsonFactory jf = new JsonFactory();
- JsonParser jp = jf.createParser(new StringReader(JSON));
- JsonNode result = objectMapper().readTree(jp);
-
- assertTrue(result.isObject());
- assertEquals(4, result.size());
-
- assertNull(objectMapper().readTree(jp));
- jp.close();
- }
-
- public void testMultiple() throws Exception
- {
- String JSON = "12 \"string\" [ 1, 2, 3 ]";
- JsonFactory jf = new JsonFactory();
- JsonParser jp = jf.createParser(new StringReader(JSON));
- final ObjectMapper mapper = objectMapper();
- JsonNode result = mapper.readTree(jp);
-
- assertTrue(result.isIntegralNumber());
- assertTrue(result.isInt());
- assertFalse(result.isTextual());
- assertEquals(12, result.intValue());
-
- result = mapper.readTree(jp);
- assertTrue(result.isTextual());
- assertFalse(result.isIntegralNumber());
- assertFalse(result.isInt());
- assertEquals("string", result.textValue());
-
- result = mapper.readTree(jp);
- assertTrue(result.isArray());
- assertEquals(3, result.size());
-
- assertNull(mapper.readTree(jp));
- jp.close();
- }
-
- /**
- * Let's also verify behavior of "MissingNode" -- one needs to be able
- * to traverse such bogus nodes with appropriate methods.
- */
- @SuppressWarnings("unused")
- public void testMissingNode()
- throws Exception
- {
- String JSON = "[ { }, [ ] ]";
- JsonNode result = objectMapper().readTree(new StringReader(JSON));
-
- assertTrue(result.isContainerNode());
- assertTrue(result.isArray());
- assertEquals(2, result.size());
-
- int count = 0;
- for (JsonNode node : result) {
- ++count;
- }
- assertEquals(2, count);
-
- Iterator<JsonNode> it = result.iterator();
-
- JsonNode onode = it.next();
- assertTrue(onode.isContainerNode());
- assertTrue(onode.isObject());
- assertEquals(0, onode.size());
- assertFalse(onode.isMissingNode()); // real node
- assertNull(onode.textValue());
-
- // how about dereferencing?
- assertNull(onode.get(0));
- JsonNode dummyNode = onode.path(0);
- assertNotNull(dummyNode);
- assertTrue(dummyNode.isMissingNode());
- assertNull(dummyNode.get(3));
- assertNull(dummyNode.get("whatever"));
- JsonNode dummyNode2 = dummyNode.path(98);
- assertNotNull(dummyNode2);
- assertTrue(dummyNode2.isMissingNode());
- JsonNode dummyNode3 = dummyNode.path("field");
- assertNotNull(dummyNode3);
- assertTrue(dummyNode3.isMissingNode());
-
- // and same for the array node
-
- JsonNode anode = it.next();
- assertTrue(anode.isContainerNode());
- assertTrue(anode.isArray());
- assertFalse(anode.isMissingNode()); // real node
- assertEquals(0, anode.size());
-
- assertNull(anode.get(0));
- dummyNode = anode.path(0);
- assertNotNull(dummyNode);
- assertTrue(dummyNode.isMissingNode());
- assertNull(dummyNode.get(0));
- assertNull(dummyNode.get("myfield"));
- dummyNode2 = dummyNode.path(98);
- assertNotNull(dummyNode2);
- assertTrue(dummyNode2.isMissingNode());
- dummyNode3 = dummyNode.path("f");
- assertNotNull(dummyNode3);
- assertTrue(dummyNode3.isMissingNode());
- }
-
- public void testArray() throws Exception
- {
- final String JSON = "[[[-0.027512,51.503221],[-0.008497,51.503221],[-0.008497,51.509744],[-0.027512,51.509744]]]";
-
- JsonNode n = objectMapper().readTree(JSON);
- assertNotNull(n);
- assertTrue(n.isArray());
- ArrayNode an = (ArrayNode) n;
- assertEquals(1, an.size());
- ArrayNode an2 = (ArrayNode) n.get(0);
- assertTrue(an2.isArray());
- assertEquals(4, an2.size());
- }
-
- /*
- /**********************************************
- /* Helper methods
- /**********************************************
- */
-
- private int calcLength(Iterator<JsonNode> it)
- {
- int count = 0;
- while (it.hasNext()) {
- it.next();
- ++count;
- }
- return count;
- }
-}
-
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeMapperSerializer.java b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeMapperSerializer.java
index 6114ed6..10f77a8 100644
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeMapperSerializer.java
+++ b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeMapperSerializer.java
@@ -11,8 +11,7 @@
* This unit test suite tries to verify that the trees ObjectMapper
* constructs can be serialized properly.
*/
-public class TestTreeMapperSerializer
- extends BaseMapTest
+public class TestTreeMapperSerializer extends NodeTestBase
{
final static String FIELD1 = "first";
final static String FIELD2 = "Second?";
@@ -24,8 +23,7 @@
final static double DOUBLE_VALUE = 9.25;
- public void testFromArray()
- throws Exception
+ public void testFromArray() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
ArrayNode root = mapper.createArrayNode();
@@ -113,29 +111,20 @@
}
String doc = sw.toString();
- JsonParser jp = new JsonFactory().createParser(new StringReader(doc));
+ JsonParser p = new JsonFactory().createParser(new StringReader(doc));
- assertEquals(JsonToken.START_ARRAY, jp.nextToken());
+ assertEquals(JsonToken.START_ARRAY, p.nextToken());
for (int i = -20; i <= 20; ++i) {
- assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
- assertEquals(i, jp.getIntValue());
- assertEquals(""+i, jp.getText());
+ assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
+ assertEquals(i, p.getIntValue());
+ assertEquals(""+i, p.getText());
}
- assertEquals(JsonToken.END_ARRAY, jp.nextToken());
- jp.close();
+ assertEquals(JsonToken.END_ARRAY, p.nextToken());
+ p.close();
}
}
- public void testNull() throws Exception
- {
- ObjectMapper mapper = new ObjectMapper();
- StringWriter sw = new StringWriter();
- mapper.writeValue(sw, NullNode.instance);
- assertEquals("null", sw.toString());
- }
-
- public void testBinary()
- throws Exception
+ public void testBinary() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
final int LENGTH = 13045;
@@ -146,11 +135,11 @@
StringWriter sw = new StringWriter();
mapper.writeValue(sw, BinaryNode.valueOf(data));
- JsonParser jp = new JsonFactory().createParser(sw.toString());
+ JsonParser p = new JsonFactory().createParser(sw.toString());
// note: can't determine it's binary from json alone:
- assertToken(JsonToken.VALUE_STRING, jp.nextToken());
- assertArrayEquals(data, jp.getBinaryValue());
- jp.close();
+ assertToken(JsonToken.VALUE_STRING, p.nextToken());
+ assertArrayEquals(data, p.getBinaryValue());
+ p.close();
}
/*
@@ -162,63 +151,63 @@
private void verifyFromArray(String input)
throws Exception
{
- JsonParser jp = new JsonFactory().createParser(new StringReader(input));
+ JsonParser p = new JsonFactory().createParser(new StringReader(input));
- assertEquals(JsonToken.START_ARRAY, jp.nextToken());
+ assertEquals(JsonToken.START_ARRAY, p.nextToken());
- assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
- assertEquals(TEXT1, getAndVerifyText(jp));
+ assertEquals(JsonToken.VALUE_STRING, p.nextToken());
+ assertEquals(TEXT1, getAndVerifyText(p));
- assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
- assertEquals(3, jp.getIntValue());
+ assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
+ assertEquals(3, p.getIntValue());
- assertEquals(JsonToken.START_OBJECT, jp.nextToken());
- assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
- assertEquals(FIELD1, getAndVerifyText(jp));
+ assertEquals(JsonToken.START_OBJECT, p.nextToken());
+ assertEquals(JsonToken.FIELD_NAME, p.nextToken());
+ assertEquals(FIELD1, getAndVerifyText(p));
- assertEquals(JsonToken.VALUE_TRUE, jp.nextToken());
- assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
- assertEquals(FIELD2, getAndVerifyText(jp));
+ assertEquals(JsonToken.VALUE_TRUE, p.nextToken());
+ assertEquals(JsonToken.FIELD_NAME, p.nextToken());
+ assertEquals(FIELD2, getAndVerifyText(p));
- assertEquals(JsonToken.START_ARRAY, jp.nextToken());
- assertEquals(JsonToken.END_ARRAY, jp.nextToken());
- assertEquals(JsonToken.END_OBJECT, jp.nextToken());
+ assertEquals(JsonToken.START_ARRAY, p.nextToken());
+ assertEquals(JsonToken.END_ARRAY, p.nextToken());
+ assertEquals(JsonToken.END_OBJECT, p.nextToken());
- assertEquals(JsonToken.VALUE_FALSE, jp.nextToken());
+ assertEquals(JsonToken.VALUE_FALSE, p.nextToken());
- assertEquals(JsonToken.END_ARRAY, jp.nextToken());
- assertNull(jp.nextToken());
- jp.close();
+ assertEquals(JsonToken.END_ARRAY, p.nextToken());
+ assertNull(p.nextToken());
+ p.close();
}
private void verifyFromMap(String input)
throws Exception
{
- JsonParser jp = new JsonFactory().createParser(new StringReader(input));
- assertEquals(JsonToken.START_OBJECT, jp.nextToken());
- assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
- assertEquals(FIELD4, getAndVerifyText(jp));
- assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
- assertEquals(TEXT2, getAndVerifyText(jp));
+ JsonParser p = new JsonFactory().createParser(input);
+ assertEquals(JsonToken.START_OBJECT, p.nextToken());
+ assertEquals(JsonToken.FIELD_NAME, p.nextToken());
+ assertEquals(FIELD4, getAndVerifyText(p));
+ assertEquals(JsonToken.VALUE_STRING, p.nextToken());
+ assertEquals(TEXT2, getAndVerifyText(p));
- assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
- assertEquals(FIELD3, getAndVerifyText(jp));
- assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
- assertEquals(-1, jp.getIntValue());
+ assertEquals(JsonToken.FIELD_NAME, p.nextToken());
+ assertEquals(FIELD3, getAndVerifyText(p));
+ assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
+ assertEquals(-1, p.getIntValue());
- assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
- assertEquals(FIELD2, getAndVerifyText(jp));
- assertEquals(JsonToken.START_ARRAY, jp.nextToken());
- assertEquals(JsonToken.END_ARRAY, jp.nextToken());
+ assertEquals(JsonToken.FIELD_NAME, p.nextToken());
+ assertEquals(FIELD2, getAndVerifyText(p));
+ assertEquals(JsonToken.START_ARRAY, p.nextToken());
+ assertEquals(JsonToken.END_ARRAY, p.nextToken());
- assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
- assertEquals(FIELD1, getAndVerifyText(jp));
- assertEquals(JsonToken.VALUE_NUMBER_FLOAT, jp.nextToken());
- assertEquals(DOUBLE_VALUE, jp.getDoubleValue(), 0);
+ assertEquals(JsonToken.FIELD_NAME, p.nextToken());
+ assertEquals(FIELD1, getAndVerifyText(p));
+ assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
+ assertEquals(DOUBLE_VALUE, p.getDoubleValue(), 0);
- assertEquals(JsonToken.END_OBJECT, jp.nextToken());
+ assertEquals(JsonToken.END_OBJECT, p.nextToken());
- assertNull(jp.nextToken());
- jp.close();
+ assertNull(p.nextToken());
+ p.close();
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeTraversingParser.java b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeTraversingParser.java
index f0aa428..0573696 100644
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeTraversingParser.java
+++ b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeTraversingParser.java
@@ -8,6 +8,7 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.node.BinaryNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.POJONode;
@@ -22,7 +23,6 @@
public List<String> kids;
}
- // Helper class for [JACKSON-370]
@JsonIgnoreProperties(ignoreUnknown=true)
public static class Jackson370Bean {
public Inner inner;
@@ -45,65 +45,65 @@
"{ \"a\" : 123, \"list\" : [ 12.25, null, true, { }, [ ] ] }";
ObjectMapper m = new ObjectMapper();
JsonNode tree = m.readTree(JSON);
- JsonParser jp = tree.traverse();
+ JsonParser p = tree.traverse();
- assertNull(jp.getCurrentToken());
- assertNull(jp.getCurrentName());
+ assertNull(p.getCurrentToken());
+ assertNull(p.getCurrentName());
- assertToken(JsonToken.START_OBJECT, jp.nextToken());
- assertNull(jp.getCurrentName());
- assertEquals("Expected START_OBJECT", JsonToken.START_OBJECT.asString(), jp.getText());
+ assertToken(JsonToken.START_OBJECT, p.nextToken());
+ assertNull(p.getCurrentName());
+ assertEquals("Expected START_OBJECT", JsonToken.START_OBJECT.asString(), p.getText());
- assertToken(JsonToken.FIELD_NAME, jp.nextToken());
- assertEquals("a", jp.getCurrentName());
- assertEquals("a", jp.getText());
+ assertToken(JsonToken.FIELD_NAME, p.nextToken());
+ assertEquals("a", p.getCurrentName());
+ assertEquals("a", p.getText());
- assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
- assertEquals("a", jp.getCurrentName());
- assertEquals(123, jp.getIntValue());
- assertEquals("123", jp.getText());
+ assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
+ assertEquals("a", p.getCurrentName());
+ assertEquals(123, p.getIntValue());
+ assertEquals("123", p.getText());
- assertToken(JsonToken.FIELD_NAME, jp.nextToken());
- assertEquals("list", jp.getCurrentName());
- assertEquals("list", jp.getText());
+ assertToken(JsonToken.FIELD_NAME, p.nextToken());
+ assertEquals("list", p.getCurrentName());
+ assertEquals("list", p.getText());
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
- assertEquals("list", jp.getCurrentName());
- assertEquals(JsonToken.START_ARRAY.asString(), jp.getText());
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
+ assertEquals("list", p.getCurrentName());
+ assertEquals(JsonToken.START_ARRAY.asString(), p.getText());
- assertToken(JsonToken.VALUE_NUMBER_FLOAT, jp.nextToken());
- assertNull(jp.getCurrentName());
- assertEquals(12.25, jp.getDoubleValue(), 0);
- assertEquals("12.25", jp.getText());
+ assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
+ assertNull(p.getCurrentName());
+ assertEquals(12.25, p.getDoubleValue(), 0);
+ assertEquals("12.25", p.getText());
- assertToken(JsonToken.VALUE_NULL, jp.nextToken());
- assertNull(jp.getCurrentName());
- assertEquals(JsonToken.VALUE_NULL.asString(), jp.getText());
+ assertToken(JsonToken.VALUE_NULL, p.nextToken());
+ assertNull(p.getCurrentName());
+ assertEquals(JsonToken.VALUE_NULL.asString(), p.getText());
- assertToken(JsonToken.VALUE_TRUE, jp.nextToken());
- assertNull(jp.getCurrentName());
- assertTrue(jp.getBooleanValue());
- assertEquals(JsonToken.VALUE_TRUE.asString(), jp.getText());
+ assertToken(JsonToken.VALUE_TRUE, p.nextToken());
+ assertNull(p.getCurrentName());
+ assertTrue(p.getBooleanValue());
+ assertEquals(JsonToken.VALUE_TRUE.asString(), p.getText());
- assertToken(JsonToken.START_OBJECT, jp.nextToken());
- assertNull(jp.getCurrentName());
- assertToken(JsonToken.END_OBJECT, jp.nextToken());
- assertNull(jp.getCurrentName());
+ assertToken(JsonToken.START_OBJECT, p.nextToken());
+ assertNull(p.getCurrentName());
+ assertToken(JsonToken.END_OBJECT, p.nextToken());
+ assertNull(p.getCurrentName());
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
- assertNull(jp.getCurrentName());
- assertToken(JsonToken.END_ARRAY, jp.nextToken());
- assertNull(jp.getCurrentName());
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
+ assertNull(p.getCurrentName());
+ assertToken(JsonToken.END_ARRAY, p.nextToken());
+ assertNull(p.getCurrentName());
- assertToken(JsonToken.END_ARRAY, jp.nextToken());
+ assertToken(JsonToken.END_ARRAY, p.nextToken());
- assertToken(JsonToken.END_OBJECT, jp.nextToken());
- assertNull(jp.getCurrentName());
+ assertToken(JsonToken.END_OBJECT, p.nextToken());
+ assertNull(p.getCurrentName());
- assertNull(jp.nextToken());
+ assertNull(p.nextToken());
- jp.close();
- assertTrue(jp.isClosed());
+ p.close();
+ assertTrue(p.isClosed());
}
public void testArray() throws Exception
@@ -111,25 +111,25 @@
// For convenience, parse tree from JSON first
ObjectMapper m = new ObjectMapper();
- JsonParser jp = m.readTree("[]").traverse();
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
- assertToken(JsonToken.END_ARRAY, jp.nextToken());
- jp.close();
+ JsonParser p = m.readTree("[]").traverse();
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
+ assertToken(JsonToken.END_ARRAY, p.nextToken());
+ p.close();
- jp = m.readTree("[[]]").traverse();
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
- assertToken(JsonToken.END_ARRAY, jp.nextToken());
- assertToken(JsonToken.END_ARRAY, jp.nextToken());
- jp.close();
+ p = m.readTree("[[]]").traverse();
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
+ assertToken(JsonToken.END_ARRAY, p.nextToken());
+ assertToken(JsonToken.END_ARRAY, p.nextToken());
+ p.close();
- jp = m.readTree("[[ 12.1 ]]").traverse();
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
- assertToken(JsonToken.VALUE_NUMBER_FLOAT, jp.nextToken());
- assertToken(JsonToken.END_ARRAY, jp.nextToken());
- assertToken(JsonToken.END_ARRAY, jp.nextToken());
- jp.close();
+ p = m.readTree("[[ 12.1 ]]").traverse();
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
+ assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
+ assertToken(JsonToken.END_ARRAY, p.nextToken());
+ assertToken(JsonToken.END_ARRAY, p.nextToken());
+ p.close();
}
public void testNested() throws Exception
@@ -140,28 +140,28 @@
;
ObjectMapper m = new ObjectMapper();
JsonNode tree = m.readTree(JSON);
- JsonParser jp = tree.traverse();
- assertToken(JsonToken.START_OBJECT, jp.nextToken());
- assertToken(JsonToken.FIELD_NAME, jp.nextToken());
+ JsonParser p = tree.traverse();
+ assertToken(JsonToken.START_OBJECT, p.nextToken());
+ assertToken(JsonToken.FIELD_NAME, p.nextToken());
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
- assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
- assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
- assertToken(JsonToken.END_ARRAY, jp.nextToken());
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
+ assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
+ assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
+ assertToken(JsonToken.END_ARRAY, p.nextToken());
- assertToken(JsonToken.START_ARRAY, jp.nextToken());
- assertToken(JsonToken.VALUE_NUMBER_FLOAT, jp.nextToken());
- assertToken(JsonToken.VALUE_NUMBER_FLOAT, jp.nextToken());
- assertToken(JsonToken.END_ARRAY, jp.nextToken());
+ assertToken(JsonToken.START_ARRAY, p.nextToken());
+ assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
+ assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
+ assertToken(JsonToken.END_ARRAY, p.nextToken());
- assertToken(JsonToken.END_ARRAY, jp.nextToken());
- assertToken(JsonToken.END_ARRAY, jp.nextToken());
+ assertToken(JsonToken.END_ARRAY, p.nextToken());
+ assertToken(JsonToken.END_ARRAY, p.nextToken());
- assertToken(JsonToken.END_OBJECT, jp.nextToken());
- jp.close();
+ assertToken(JsonToken.END_OBJECT, p.nextToken());
+ p.close();
}
/**
@@ -172,71 +172,71 @@
{
ObjectMapper m = new ObjectMapper();
JsonNode tree = m.readTree(SAMPLE_DOC_JSON_SPEC);
- JsonParser jp = tree.traverse();
- verifyJsonSpecSampleDoc(jp, true);
- jp.close();
+ JsonParser p = tree.traverse();
+ verifyJsonSpecSampleDoc(p, true);
+ p.close();
}
public void testBinaryPojo() throws Exception
{
byte[] inputBinary = new byte[] { 1, 2, 100 };
POJONode n = new POJONode(inputBinary);
- JsonParser jp = n.traverse();
+ JsonParser p = n.traverse();
- assertNull(jp.getCurrentToken());
- assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, jp.nextToken());
- byte[] data = jp.getBinaryValue();
+ assertNull(p.getCurrentToken());
+ assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
+ byte[] data = p.getBinaryValue();
assertNotNull(data);
assertArrayEquals(inputBinary, data);
- Object pojo = jp.getEmbeddedObject();
+ Object pojo = p.getEmbeddedObject();
assertSame(data, pojo);
- jp.close();
+ p.close();
}
public void testBinaryNode() throws Exception
{
byte[] inputBinary = new byte[] { 0, -5 };
BinaryNode n = new BinaryNode(inputBinary);
- JsonParser jp = n.traverse();
+ JsonParser p = n.traverse();
- assertNull(jp.getCurrentToken());
+ assertNull(p.getCurrentToken());
// exposed as POJO... not as VALUE_STRING
- assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, jp.nextToken());
- byte[] data = jp.getBinaryValue();
+ assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
+ byte[] data = p.getBinaryValue();
assertNotNull(data);
assertArrayEquals(inputBinary, data);
// but as importantly, can be viewed as base64 encoded thing:
- assertEquals("APs=", jp.getText());
+ assertEquals("APs=", p.getText());
- assertNull(jp.nextToken());
- jp.close();
+ assertNull(p.nextToken());
+ p.close();
}
public void testTextAsBinary() throws Exception
{
TextNode n = new TextNode(" APs=\n");
- JsonParser jp = n.traverse();
- assertNull(jp.getCurrentToken());
- assertToken(JsonToken.VALUE_STRING, jp.nextToken());
- byte[] data = jp.getBinaryValue();
+ JsonParser p = n.traverse();
+ assertNull(p.getCurrentToken());
+ assertToken(JsonToken.VALUE_STRING, p.nextToken());
+ byte[] data = p.getBinaryValue();
assertNotNull(data);
assertArrayEquals(new byte[] { 0, -5 }, data);
- assertNull(jp.nextToken());
- jp.close();
- assertTrue(jp.isClosed());
+ assertNull(p.nextToken());
+ p.close();
+ assertTrue(p.isClosed());
// Also: let's verify we get an exception for garbage...
n = new TextNode("?!??");
- jp = n.traverse();
- assertToken(JsonToken.VALUE_STRING, jp.nextToken());
+ p = n.traverse();
+ assertToken(JsonToken.VALUE_STRING, p.nextToken());
try {
- jp.getBinaryValue();
- } catch (JsonParseException e) {
+ p.getBinaryValue();
+ } catch (InvalidFormatException e) {
verifyException(e, "Illegal character");
}
- jp.close();
+ p.close();
}
/**
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java
index 959133e..8395627 100644
--- a/src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java
+++ b/src/test/java/com/fasterxml/jackson/databind/node/TestTreeWithType.java
@@ -20,7 +20,7 @@
}
}
- // [Issue#353]
+ // [databind#353]
public class SavedCookie {
public String name, value;
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TextNodeTest.java b/src/test/java/com/fasterxml/jackson/databind/node/TextNodeTest.java
new file mode 100644
index 0000000..ad04c8a
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/node/TextNodeTest.java
@@ -0,0 +1,31 @@
+package com.fasterxml.jackson.databind.node;
+
+public class TextNodeTest extends NodeTestBase
+{
+ public void testText()
+ {
+ assertNull(TextNode.valueOf(null));
+ TextNode empty = TextNode.valueOf("");
+ assertStandardEquals(empty);
+ assertSame(TextNode.EMPTY_STRING_NODE, empty);
+
+ assertNodeNumbers(TextNode.valueOf("-3"), -3, -3.0);
+ assertNodeNumbers(TextNode.valueOf("17.75"), 17, 17.75);
+
+ long value = 127353264013893L;
+ TextNode n = TextNode.valueOf(String.valueOf(value));
+ assertEquals(value, n.asLong());
+
+ // and then with non-numeric input
+ n = TextNode.valueOf("foobar");
+ assertNodeNumbersForNonNumeric(n);
+
+ assertEquals("foobar", n.asText("barf"));
+ assertEquals("", empty.asText("xyz"));
+
+ assertTrue(TextNode.valueOf("true").asBoolean(true));
+ assertTrue(TextNode.valueOf("true").asBoolean(false));
+ assertFalse(TextNode.valueOf("false").asBoolean(true));
+ assertFalse(TextNode.valueOf("false").asBoolean(false));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TreeReadViaMapperTest.java b/src/test/java/com/fasterxml/jackson/databind/node/TreeReadViaMapperTest.java
new file mode 100644
index 0000000..dbeed46
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/node/TreeReadViaMapperTest.java
@@ -0,0 +1,192 @@
+package com.fasterxml.jackson.databind.node;
+
+import java.io.*;
+import java.util.*;
+
+import com.fasterxml.jackson.core.*;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.node.TestTreeDeserialization.Bean;
+
+/**
+ * This unit test suite tries to verify that ObjectMapper
+ * can properly parse JSON and bind contents into appropriate
+ * JsonNode instances.
+ */
+public class TreeReadViaMapperTest extends BaseMapTest
+{
+ public void testSimple() throws Exception
+ {
+ final String JSON = SAMPLE_DOC_JSON_SPEC;
+
+ for (int type = 0; type < 2; ++type) {
+ JsonNode result;
+
+ if (type == 0) {
+ result = objectMapper().readTree(new StringReader(JSON));
+ } else {
+ result = objectMapper().readTree(JSON);
+ }
+
+ assertType(result, ObjectNode.class);
+ assertEquals(1, result.size());
+ assertTrue(result.isObject());
+
+ ObjectNode main = (ObjectNode) result;
+ assertEquals("Image", main.fieldNames().next());
+ JsonNode ob = main.elements().next();
+ assertType(ob, ObjectNode.class);
+ ObjectNode imageMap = (ObjectNode) ob;
+
+ assertEquals(5, imageMap.size());
+ ob = imageMap.get("Width");
+ assertTrue(ob.isIntegralNumber());
+ assertFalse(ob.isFloatingPointNumber());
+ assertEquals(SAMPLE_SPEC_VALUE_WIDTH, ob.intValue());
+ ob = imageMap.get("Height");
+ assertTrue(ob.isIntegralNumber());
+ assertEquals(SAMPLE_SPEC_VALUE_HEIGHT, ob.intValue());
+
+ ob = imageMap.get("Title");
+ assertTrue(ob.isTextual());
+ assertEquals(SAMPLE_SPEC_VALUE_TITLE, ob.textValue());
+
+ ob = imageMap.get("Thumbnail");
+ assertType(ob, ObjectNode.class);
+ ObjectNode tn = (ObjectNode) ob;
+ ob = tn.get("Url");
+ assertTrue(ob.isTextual());
+ assertEquals(SAMPLE_SPEC_VALUE_TN_URL, ob.textValue());
+ ob = tn.get("Height");
+ assertTrue(ob.isIntegralNumber());
+ assertEquals(SAMPLE_SPEC_VALUE_TN_HEIGHT, ob.intValue());
+ ob = tn.get("Width");
+ assertTrue(ob.isTextual());
+ assertEquals(SAMPLE_SPEC_VALUE_TN_WIDTH, ob.textValue());
+
+ ob = imageMap.get("IDs");
+ assertTrue(ob.isArray());
+ ArrayNode idList = (ArrayNode) ob;
+ assertEquals(4, idList.size());
+ assertEquals(4, calcLength(idList.elements()));
+ assertEquals(4, calcLength(idList.iterator()));
+ {
+ int[] values = new int[] {
+ SAMPLE_SPEC_VALUE_TN_ID1,
+ SAMPLE_SPEC_VALUE_TN_ID2,
+ SAMPLE_SPEC_VALUE_TN_ID3,
+ SAMPLE_SPEC_VALUE_TN_ID4
+ };
+ for (int i = 0; i < values.length; ++i) {
+ assertEquals(values[i], idList.get(i).intValue());
+ }
+ int i = 0;
+ for (JsonNode n : idList) {
+ assertEquals(values[i], n.intValue());
+ ++i;
+ }
+ }
+ }
+ }
+
+ public void testMixed() throws IOException
+ {
+ ObjectMapper om = new ObjectMapper();
+ String JSON = "{\"node\" : { \"a\" : 3 }, \"x\" : 9 }";
+ Bean bean = om.readValue(JSON, Bean.class);
+
+ assertEquals(9, bean._x);
+ JsonNode n = bean._node;
+ assertNotNull(n);
+ assertEquals(1, n.size());
+ ObjectNode on = (ObjectNode) n;
+ assertEquals(3, on.get("a").intValue());
+ }
+
+ /**
+ * Type mappers should be able to gracefully deal with end of
+ * input.
+ */
+ public void testEOF() throws Exception
+ {
+ String JSON =
+ "{ \"key\": [ { \"a\" : { \"name\": \"foo\", \"type\": 1\n"
+ +"}, \"type\": 3, \"url\": \"http://www.google.com\" } ],\n"
+ +"\"name\": \"xyz\", \"type\": 1, \"url\" : null }\n "
+ ;
+ JsonFactory jf = new JsonFactory();
+ JsonParser p = jf.createParser(new StringReader(JSON));
+ JsonNode result = objectMapper().readTree(p);
+
+ assertTrue(result.isObject());
+ assertEquals(4, result.size());
+
+ assertNull(objectMapper().readTree(p));
+ p.close();
+ }
+
+ public void testMultiple() throws Exception
+ {
+ String JSON = "12 \"string\" [ 1, 2, 3 ]";
+ JsonFactory jf = new JsonFactory();
+ JsonParser p = jf.createParser(new StringReader(JSON));
+ final ObjectMapper mapper = objectMapper();
+ JsonNode result = mapper.readTree(p);
+
+ assertTrue(result.isIntegralNumber());
+ assertTrue(result.isInt());
+ assertFalse(result.isTextual());
+ assertEquals(12, result.intValue());
+
+ result = mapper.readTree(p);
+ assertTrue(result.isTextual());
+ assertFalse(result.isIntegralNumber());
+ assertFalse(result.isInt());
+ assertEquals("string", result.textValue());
+
+ result = mapper.readTree(p);
+ assertTrue(result.isArray());
+ assertEquals(3, result.size());
+
+ assertNull(mapper.readTree(p));
+ p.close();
+ }
+
+ // [databind#1406]
+ public void testNullFromEOFViaMapper() throws Exception
+ {
+ final ObjectMapper mapper = objectMapper();
+
+ assertNull(mapper.readTree(new StringReader("")));
+ assertNull(mapper.readTree(new ByteArrayInputStream(new byte[0])));
+ }
+
+ // [databind#1406]
+ public void testNullFromEOFViaObjectReader() throws Exception
+ {
+ final ObjectMapper mapper = objectMapper();
+
+ assertNull(mapper.readTree(new StringReader("")));
+ assertNull(mapper.readTree(new ByteArrayInputStream(new byte[0])));
+ assertNull(mapper.readerFor(JsonNode.class)
+ .readTree(new StringReader("")));
+ assertNull(mapper.readerFor(JsonNode.class)
+ .readTree(new ByteArrayInputStream(new byte[0])));
+ }
+
+ /*
+ /**********************************************
+ /* Helper methods
+ /**********************************************
+ */
+
+ private int calcLength(Iterator<JsonNode> it)
+ {
+ int count = 0;
+ while (it.hasNext()) {
+ it.next();
+ ++count;
+ }
+ return count;
+ }
+}
+
diff --git a/src/test/java/com/fasterxml/jackson/databind/objectid/JSOGDeserialize622Test.java b/src/test/java/com/fasterxml/jackson/databind/objectid/JSOGDeserialize622Test.java
index 129872e..1adfd10 100644
--- a/src/test/java/com/fasterxml/jackson/databind/objectid/JSOGDeserialize622Test.java
+++ b/src/test/java/com/fasterxml/jackson/databind/objectid/JSOGDeserialize622Test.java
@@ -97,14 +97,14 @@
static class JSOGRefDeserializer extends JsonDeserializer<JSOGRef>
{
@Override
- public JSOGRef deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
- JsonNode node = jp.readValueAsTree();
+ public JSOGRef deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
+ JsonNode node = p.readValueAsTree();
if (node.isTextual()) {
return new JSOGRef(node.asInt());
}
JsonNode n = node.get(REF_KEY);
if (n == null) {
- throw JsonMappingException.from(jp, "Could not find key '"+REF_KEY
+ throw new JsonMappingException(p, "Could not find key '"+REF_KEY
+"' from ("+node.getClass().getName()+"): "+node);
}
return new JSOGRef(n.asInt());
diff --git a/src/test/java/com/fasterxml/jackson/failing/TestObjectId687.java b/src/test/java/com/fasterxml/jackson/databind/objectid/ObjectId687Test.java
similarity index 76%
rename from src/test/java/com/fasterxml/jackson/failing/TestObjectId687.java
rename to src/test/java/com/fasterxml/jackson/databind/objectid/ObjectId687Test.java
index fd8fabc..972a5fa 100644
--- a/src/test/java/com/fasterxml/jackson/failing/TestObjectId687.java
+++ b/src/test/java/com/fasterxml/jackson/databind/objectid/ObjectId687Test.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.failing;
+package com.fasterxml.jackson.databind.objectid;
import java.io.IOException;
import java.util.*;
@@ -6,8 +6,9 @@
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
-public class TestObjectId687 extends BaseMapTest
+public class ObjectId687Test extends BaseMapTest
{
+ // for [databind#687]
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="label")
static class ReferredWithCreator {
public String label;
@@ -63,7 +64,8 @@
*/
private final ObjectMapper MAPPER = objectMapper();
-
+
+ // for [databind#687]
public void testSerializeDeserializeWithCreator() throws IOException {
ReferredWithCreator base = new ReferredWithCreator("label1");
ReferringToObjWithCreator r = new ReferringToObjWithCreator();
@@ -72,10 +74,15 @@
e.baseRef = base;
e.nextRef = r;
- String jsonStr = MAPPER.writeValueAsString(e);
+ String json = MAPPER.writeValueAsString(e);
- EnclosingForRefsWithCreator deserialized = MAPPER.readValue(jsonStr, EnclosingForRefsWithCreator.class);
- assertNotNull(deserialized);
+ EnclosingForRefsWithCreator result = MAPPER.readValue(json,
+ EnclosingForRefsWithCreator.class);
+ assertNotNull(result);
+ assertEquals(result.label, e.label);
+
+ // also, compare by re-serializing:
+ assertEquals(json, MAPPER.writeValueAsString(result));
}
public void testSerializeDeserializeNoCreator() throws IOException {
@@ -86,9 +93,14 @@
e.baseRef = base;
e.nextRef = r;
- String jsonStr = MAPPER.writeValueAsString(e);
+ String json = MAPPER.writeValueAsString(e);
- EnclosingForRefWithNoCreator deserialized = MAPPER.readValue(jsonStr, EnclosingForRefWithNoCreator.class);
- assertNotNull(deserialized);
+ EnclosingForRefWithNoCreator result = MAPPER.readValue(json,
+ EnclosingForRefWithNoCreator.class);
+ assertNotNull(result);
+ assertEquals(result.label, e.label);
+
+ // also, compare by re-serializing:
+ assertEquals(json, MAPPER.writeValueAsString(result));
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/objectid/TestObjectIdWithPolymorphic.java b/src/test/java/com/fasterxml/jackson/databind/objectid/TestObjectIdWithPolymorphic.java
index 2ecca8e..486abf3 100644
--- a/src/test/java/com/fasterxml/jackson/databind/objectid/TestObjectIdWithPolymorphic.java
+++ b/src/test/java/com/fasterxml/jackson/databind/objectid/TestObjectIdWithPolymorphic.java
@@ -2,11 +2,9 @@
import java.util.*;
-import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
-import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
@@ -134,11 +132,6 @@
public void testIssue811() throws Exception
{
ObjectMapper om = new ObjectMapper();
- om.disable(MapperFeature.AUTO_DETECT_CREATORS);
- om.disable(MapperFeature.AUTO_DETECT_GETTERS);
- om.disable(MapperFeature.AUTO_DETECT_IS_GETTERS);
- om.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
-
om.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX);
om.enable(SerializationFeature.INDENT_OUTPUT);
om.enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL, "@class");
diff --git a/src/test/java/com/fasterxml/jackson/databind/seq/SequenceWriterTest.java b/src/test/java/com/fasterxml/jackson/databind/seq/SequenceWriterTest.java
index 676383f..c103a34 100644
--- a/src/test/java/com/fasterxml/jackson/databind/seq/SequenceWriterTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/seq/SequenceWriterTest.java
@@ -1,12 +1,17 @@
package com.fasterxml.jackson.databind.seq;
+import java.io.Closeable;
+import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
+
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.io.SerializedString;
+
import com.fasterxml.jackson.databind.*;
public class SequenceWriterTest extends BaseMapTest
@@ -43,6 +48,40 @@
public ImplB(int v) { b = v; }
}
+ static class BareBase {
+ public int a = 1;
+ }
+
+ @JsonPropertyOrder({ "a", "b" })
+ static class BareBaseExt extends BareBase {
+ public int b = 2;
+ }
+
+ static class BareBaseCloseable extends BareBase
+ implements Closeable
+ {
+ public int c = 3;
+
+ boolean closed = false;
+
+ @Override
+ public void close() throws IOException {
+ closed = true;
+ }
+ }
+
+ static class CloseableValue implements Closeable
+ {
+ public int x;
+
+ public boolean closed;
+
+ @Override
+ public void close() throws IOException {
+ closed = true;
+ }
+ }
+
/*
/**********************************************************
/* Test methods, simple writes
@@ -57,6 +96,7 @@
{
StringWriter strw = new StringWriter();
SequenceWriter w = WRITER
+ .forType(Bean.class)
.writeValues(strw);
w.write(new Bean(13))
.write(new Bean(-6))
@@ -136,7 +176,6 @@
strw.toString());
}
- @SuppressWarnings("resource")
public void testPolymorphicArrayWithType() throws Exception
{
StringWriter strw = new StringWriter();
@@ -145,9 +184,53 @@
.writeValuesAsArray(strw);
w.write(new ImplA(-1))
.write(new ImplB(3))
- .write(new ImplA(7))
- .close();
+ .write(new ImplA(7));
+ w.flush();
+ w.close();
assertEquals(aposToQuotes("[{'type':'A','value':-1},{'type':'B','b':3},{'type':'A','value':7}]"),
strw.toString());
}
+
+ @SuppressWarnings("resource")
+ public void testSimpleCloseable() throws Exception
+ {
+ ObjectWriter w = MAPPER.writer()
+ .with(SerializationFeature.CLOSE_CLOSEABLE);
+ CloseableValue input = new CloseableValue();
+ assertFalse(input.closed);
+ StringWriter out = new StringWriter();
+ SequenceWriter seq = w.writeValues(out);
+ input = new CloseableValue();
+ assertFalse(input.closed);
+ seq.write(input);
+ assertTrue(input.closed);
+ seq.close();
+ input.close();
+ assertEquals(aposToQuotes("{'x':0,'closed':false}"), out.toString());
+ }
+
+ public void testWithExplicitType() throws Exception
+ {
+ ObjectWriter w = MAPPER.writer()
+ // just for fun (and higher coverage):
+ .without(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)
+ .with(SerializationFeature.CLOSE_CLOSEABLE);
+ StringWriter out = new StringWriter();
+ SequenceWriter seq = w.writeValues(out);
+ // first full, as-is
+ seq.write(new BareBaseExt());
+ // but then just base type (no 'b' field)
+ seq.write(new BareBaseExt(), MAPPER.constructType(BareBase.class));
+
+ // one more. And note! Check for Closeable is for _value_, not type
+ // so it's fine to expect closing here
+ BareBaseCloseable cl = new BareBaseCloseable();
+ seq.write(cl, MAPPER.constructType(BareBase.class));
+ assertTrue(cl.closed);
+ cl.close();
+
+ seq.close();
+ seq.flush();
+ assertEquals(aposToQuotes("{'a':1,'b':2} {'a':1} {'a':1}"), out.toString());
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/failing/TestInnerClassReaderFor.java b/src/test/java/com/fasterxml/jackson/databind/seq/TestInnerClassReaderFor.java
similarity index 95%
rename from src/test/java/com/fasterxml/jackson/failing/TestInnerClassReaderFor.java
rename to src/test/java/com/fasterxml/jackson/databind/seq/TestInnerClassReaderFor.java
index 7fef656..6cf85e4 100644
--- a/src/test/java/com/fasterxml/jackson/failing/TestInnerClassReaderFor.java
+++ b/src/test/java/com/fasterxml/jackson/databind/seq/TestInnerClassReaderFor.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.failing;
+package com.fasterxml.jackson.databind.seq;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.BaseMapTest;
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/AnyGetterTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/AnyGetterTest.java
index 871f7b8..23746d4 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/AnyGetterTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/AnyGetterTest.java
@@ -35,6 +35,18 @@
}
}
+ // For [databind#1376]: allow disabling any-getter
+ static class NotEvenAnyBean extends AnyOnlyBean
+ {
+ @JsonAnyGetter(enabled=false)
+ @Override
+ public Map<String,Integer> any() {
+ throw new RuntimeException("Should not get called!)");
+ }
+
+ public int getValue() { return 42; }
+ }
+
static class MapAsAny
{
protected Map<String,Object> stuff = new LinkedHashMap<String,Object>();
@@ -121,13 +133,13 @@
/*
/**********************************************************
- /* Test cases
+ /* Test methods
/**********************************************************
*/
private final ObjectMapper MAPPER = new ObjectMapper();
- public void testSimpleJsonValue() throws Exception
+ public void testSimpleAnyBean() throws Exception
{
String json = MAPPER.writeValueAsString(new Bean());
Map<?,?> map = MAPPER.readValue(json, Map.class);
@@ -153,6 +165,12 @@
assertEquals("{\"a\":3}", json);
}
+ public void testAnyDisabling() throws Exception
+ {
+ String json = MAPPER.writeValueAsString(new NotEvenAnyBean());
+ assertEquals(aposToQuotes("{'value':42}"), json);
+ }
+
// Trying to repro [databind#577]
public void testAnyWithNull() throws Exception
{
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/BeanSerializerModifier1612Test.java b/src/test/java/com/fasterxml/jackson/databind/ser/BeanSerializerModifier1612Test.java
new file mode 100644
index 0000000..2be2594
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/BeanSerializerModifier1612Test.java
@@ -0,0 +1,66 @@
+package com.fasterxml.jackson.databind.ser;
+
+import java.util.*;
+
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
+import com.fasterxml.jackson.databind.ser.BeanSerializerBuilder;
+import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
+
+public class BeanSerializerModifier1612Test extends BaseMapTest
+{
+ @JsonPropertyOrder({ "a", "b", "c" })
+ static class Bean1612 {
+ public Integer a;
+ public Integer b;
+ public Double c;
+
+ public Bean1612(Integer a, Integer b, Double c) {
+ this.a = a;
+ this.b = b;
+ this.c = c;
+ }
+ }
+
+ static class Modifier1612 extends BeanSerializerModifier {
+ @Override
+ public BeanSerializerBuilder updateBuilder(SerializationConfig config, BeanDescription beanDesc,
+ BeanSerializerBuilder builder) {
+ List<BeanPropertyWriter> filtered = new ArrayList<BeanPropertyWriter>(2);
+ List<BeanPropertyWriter> properties = builder.getProperties();
+ //Make the filtered properties list bigger
+ builder.setFilteredProperties(new BeanPropertyWriter[] {properties.get(0), properties.get(1), properties.get(2)});
+
+ //The props will be shorter
+ filtered.add(properties.get(1));
+ filtered.add(properties.get(2));
+ builder.setProperties(filtered);
+ return builder;
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Construction and setter methods
+ /**********************************************************
+ */
+
+ public void testIssue1612() throws Exception
+ {
+ SimpleModule mod = new SimpleModule();
+ mod.setSerializerModifier(new Modifier1612());
+ ObjectMapper objectMapper = new ObjectMapper()
+ .registerModule(mod);
+ try {
+ objectMapper.writeValueAsString(new Bean1612(0, 1, 2d));
+ fail("Should not pass");
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "Failed to construct BeanSerializer");
+ verifyException(e, Bean1612.class.getName());
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestBeanSerializer.java b/src/test/java/com/fasterxml/jackson/databind/ser/BeanSerializerModifierTest.java
similarity index 97%
rename from src/test/java/com/fasterxml/jackson/databind/ser/TestBeanSerializer.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/BeanSerializerModifierTest.java
index 915a2ab..6f0e571 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestBeanSerializer.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/BeanSerializerModifierTest.java
@@ -23,7 +23,7 @@
* construction of {@link BeanSerializer} instances.
*/
@SuppressWarnings("serial")
-public class TestBeanSerializer extends BaseMapTest
+public class BeanSerializerModifierTest extends BaseMapTest
{
static class SerializerModifierModule extends SimpleModule
{
@@ -142,8 +142,6 @@
}
}
- // for [JACKSON-670]
-
static class EmptyBean {
@JsonIgnore
public String name = "foo";
@@ -163,7 +161,8 @@
beanProperties.add(new BeanPropertyWriter(prop, f, null,
strType,
null, null, strType,
- false, null));
+ false, null,
+ null));
} catch (NoSuchFieldException e) {
throw new IllegalStateException(e.getMessage());
}
@@ -187,7 +186,7 @@
return new BogusBeanSerializer(42);
}
}
- // [Issue#120], arrays, collections, maps
+ // [databind#120], arrays, collections, maps
static class ArraySerializerModifier extends BeanSerializerModifier {
@Override
@@ -248,9 +247,7 @@
};
}
}
-
- enum EnumABC { A, B, C };
-
+
/*
/********************************************************
/* Unit tests: success
@@ -304,7 +301,6 @@
assertEquals("{\"bogus\":\"foo\"}", json);
}
- // [Issue#539]
public void testEmptyBean539() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
@@ -320,7 +316,7 @@
assertEquals("42", json);
}
- // [Issue#121]
+ // [databind#121]
public void testModifyArraySerializer() throws Exception
{
@@ -351,7 +347,7 @@
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new SimpleModule("test")
.setSerializerModifier(new EnumSerializerModifier()));
- assertEquals("123", mapper.writeValueAsString(EnumABC.C));
+ assertEquals("123", mapper.writeValueAsString(ABC.C));
}
public void testModifyKeySerializer() throws Exception
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestJsonValue.java b/src/test/java/com/fasterxml/jackson/databind/ser/JsonValueTest.java
similarity index 81%
rename from src/test/java/com/fasterxml/jackson/databind/ser/TestJsonValue.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/JsonValueTest.java
index 4b5b4d6..ec1adb5 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestJsonValue.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/JsonValueTest.java
@@ -16,7 +16,7 @@
* annotation with bean serialization.
*/
@SuppressWarnings("serial")
-public class TestJsonValue
+public class JsonValueTest
extends BaseMapTest
{
static class ValueClass<T>
@@ -26,12 +26,16 @@
public ValueClass(T v) { _value = v; }
@JsonValue T value() { return _value; }
-
- // shouldn't need this, but may be useful for troubleshooting:
- @Override
- public String toString() { return "???"; }
}
+ static class FieldValueClass<T>
+ {
+ @JsonValue(true)
+ final T _value;
+
+ public FieldValueClass(T v) { _value = v; }
+ }
+
/**
* Another test class to check that it is also possible to
* force specific serializer to use with @JsonValue annotated
@@ -54,8 +58,7 @@
{
public ToStringValueClass2(String value) { super(value); }
- /* Simple as well, but let's ensure that other getters won't matter...
- */
+ // Simple as well, but let's ensure that other getters won't matter...
@JsonProperty int getFoobar() { return 4; }
@@ -87,6 +90,15 @@
}
}
+ static class MapFieldBean
+ {
+ @JsonValue
+ Map<String,String> stuff = new HashMap<>();
+ {
+ stuff.put("b", "2");
+ }
+ }
+
static class MapAsNumber extends HashMap<String,String>
{
@JsonValue
@@ -99,6 +111,17 @@
public int value() { return 13; }
}
+ // Just to ensure it's possible to disable annotation (usually
+ // via mix-ins, but here directly)
+ @JsonPropertyOrder({ "x", "y" })
+ static class DisabledJsonValue {
+ @JsonValue(false)
+ public int x = 1;
+
+ @JsonValue(false)
+ public int getY() { return 2; }
+ }
+
static class IntExtBean {
public List<Internal> values = new ArrayList<Internal>();
@@ -178,10 +201,16 @@
private final ObjectMapper MAPPER = new ObjectMapper();
- public void testSimpleJsonValue() throws Exception
+ public void testSimpleMethodJsonValue() throws Exception
{
- String result = MAPPER.writeValueAsString(new ValueClass<String>("abc"));
- assertEquals("\"abc\"", result);
+ assertEquals("\"abc\"", MAPPER.writeValueAsString(new ValueClass<String>("abc")));
+ assertEquals("null", MAPPER.writeValueAsString(new ValueClass<String>(null)));
+ }
+
+ public void testSimpleFieldJsonValue() throws Exception
+ {
+ assertEquals("\"abc\"", MAPPER.writeValueAsString(new FieldValueClass<String>("abc")));
+ assertEquals("null", MAPPER.writeValueAsString(new FieldValueClass<String>(null)));
}
public void testJsonValueWithUseSerializer() throws Exception
@@ -199,6 +228,12 @@
assertEquals("\"xyz\"", result);
}
+ public void testDisabling() throws Exception
+ {
+ assertEquals(aposToQuotes("{'x':1,'y':2}"),
+ MAPPER.writeValueAsString(new DisabledJsonValue()));
+ }
+
public void testValueWithStaticType() throws Exception
{
// Ok; first, with dynamic type:
@@ -211,12 +246,15 @@
}
public void testMapWithJsonValue() throws Exception {
+ // First via method
assertEquals("{\"a\":\"1\"}", MAPPER.writeValueAsString(new MapBean()));
+
+ // then field
+ assertEquals("{\"b\":\"2\"}", MAPPER.writeValueAsString(new MapFieldBean()));
}
public void testWithMap() throws Exception {
assertEquals("42", MAPPER.writeValueAsString(new MapAsNumber()));
-
}
public void testWithList() throws Exception {
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/SerializationFeaturesTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/SerializationFeaturesTest.java
index e408561..edaefcf 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/SerializationFeaturesTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/SerializationFeaturesTest.java
@@ -3,13 +3,9 @@
import java.io.*;
import java.util.*;
-import com.fasterxml.jackson.annotation.*;
-import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
-
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
-import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
/**
* Unit tests for checking handling of some of {@link MapperFeature}s
@@ -18,52 +14,6 @@
public class SerializationFeaturesTest
extends BaseMapTest
{
- /**
- * Class with one explicitly defined getter, one name-based
- * auto-detectable getter.
- */
- static class GetterClass
- {
- @JsonProperty("x") public int getX() { return -2; }
- public int getY() { return 1; }
- }
-
- /**
- * Another test-class that explicitly disables auto-detection
- */
- @JsonAutoDetect(getterVisibility=Visibility.NONE)
- static class DisabledGetterClass
- {
- @JsonProperty("x") public int getX() { return -2; }
- public int getY() { return 1; }
- }
-
- /**
- * Another test-class that explicitly enables auto-detection
- */
- @JsonAutoDetect(isGetterVisibility=Visibility.NONE)
- static class EnabledGetterClass
- {
- @JsonProperty("x") public int getX() { return -2; }
- public int getY() { return 1; }
-
- // not auto-detected, since "is getter" auto-detect disabled
- public boolean isOk() { return true; }
- }
-
- /**
- * One more: only detect "isXxx", not "getXXX"
- */
- @JsonAutoDetect(getterVisibility=Visibility.NONE)
- static class EnabledIsGetterClass
- {
- // Won't be auto-detected any more
- public int getY() { return 1; }
-
- // but this will be
- public boolean isOk() { return true; }
- }
-
static class CloseableBean implements Closeable
{
public int a = 3;
@@ -83,82 +33,12 @@
public StringListBean(Collection<String> v) { values = v; }
}
- static class TCls {
- @JsonProperty("groupname")
- private String groupname;
-
- public void setName(String str) {
- this.groupname = str;
- }
- public String getName() {
- return groupname;
- }
- }
-
/*
/**********************************************************
/* Test methods
/**********************************************************
*/
- public void testGlobalAutoDetection() throws IOException
- {
- // First: auto-detection enabled (default):
- ObjectMapper m = new ObjectMapper();
- Map<String,Object> result = writeAndMap(m, new GetterClass());
- assertEquals(2, result.size());
- assertEquals(Integer.valueOf(-2), result.get("x"));
- assertEquals(Integer.valueOf(1), result.get("y"));
-
- // Then auto-detection disabled. But note: we MUST create a new
- // mapper, since old version of serializer may be cached by now
- m = new ObjectMapper();
- m.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
- result = writeAndMap(m, new GetterClass());
- assertEquals(1, result.size());
- assertTrue(result.containsKey("x"));
- }
-
- public void testPerClassAutoDetection() throws IOException
- {
- // First: class-level auto-detection disabling
- ObjectMapper m = new ObjectMapper();
- Map<String,Object> result = writeAndMap(m, new DisabledGetterClass());
- assertEquals(1, result.size());
- assertTrue(result.containsKey("x"));
-
- // And then class-level auto-detection enabling, should override defaults
- m.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
- result = writeAndMap(m, new EnabledGetterClass());
- assertEquals(2, result.size());
- assertTrue(result.containsKey("x"));
- assertTrue(result.containsKey("y"));
- }
-
- public void testPerClassAutoDetectionForIsGetter() throws IOException
- {
- ObjectMapper m = new ObjectMapper();
- // class level should override
- m.configure(MapperFeature.AUTO_DETECT_GETTERS, true);
- m.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
- Map<String,Object> result = writeAndMap(m, new EnabledIsGetterClass());
- assertEquals(1, result.size());
- assertTrue(result.containsKey("ok"));
- assertEquals(Boolean.TRUE, result.get("ok"));
- }
-
- // Simple test verifying that chainable methods work ok...
- public void testConfigChainability()
- {
- ObjectMapper m = new ObjectMapper();
- assertTrue(m.isEnabled(MapperFeature.AUTO_DETECT_SETTERS));
- assertTrue(m.isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
- m.configure(MapperFeature.AUTO_DETECT_SETTERS, false)
- .configure(MapperFeature.AUTO_DETECT_GETTERS, false);
- assertFalse(m.isEnabled(MapperFeature.AUTO_DETECT_SETTERS));
- assertFalse(m.isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
- }
-
// Test for [JACKSON-282]
@SuppressWarnings("resource")
public void testCloseCloseable() throws IOException
@@ -258,7 +138,7 @@
HashSet<Long> longs = new HashSet<Long>();
longs.add(42L);
assertEquals("42", writer.writeValueAsString(longs));
- // [Issue#180]
+ // [databind#180]
final String EXP_STRINGS = "{\"values\":\"foo\"}";
assertEquals(EXP_STRINGS, writer.writeValueAsString(new StringListBean(Collections.singletonList("foo"))));
@@ -268,29 +148,24 @@
// arrays:
assertEquals("true", writer.writeValueAsString(new boolean[] { true }));
+ assertEquals("[true,false]", writer.writeValueAsString(new boolean[] { true, false }));
assertEquals("true", writer.writeValueAsString(new Boolean[] { Boolean.TRUE }));
+
+ assertEquals("3", writer.writeValueAsString(new short[] { 3 }));
+ assertEquals("[3,2]", writer.writeValueAsString(new short[] { 3, 2 }));
+
assertEquals("3", writer.writeValueAsString(new int[] { 3 }));
+ assertEquals("[3,2]", writer.writeValueAsString(new int[] { 3, 2 }));
+
+ assertEquals("1", writer.writeValueAsString(new long[] { 1L }));
+ assertEquals("[-1,4]", writer.writeValueAsString(new long[] { -1L, 4L }));
+
+ assertEquals("0.5", writer.writeValueAsString(new double[] { 0.5 }));
+ assertEquals("[0.5,2.5]", writer.writeValueAsString(new double[] { 0.5, 2.5 }));
+
+ assertEquals("0.5", writer.writeValueAsString(new float[] { 0.5f }));
+ assertEquals("[0.5,2.5]", writer.writeValueAsString(new float[] { 0.5f, 2.5f }));
+
assertEquals(quote("foo"), writer.writeValueAsString(new String[] { "foo" }));
}
-
- public void testVisibilityFeatures() throws Exception
- {
- ObjectMapper om = new ObjectMapper();
- // Only use explicitly specified values to be serialized/deserialized (i.e., JSONProperty).
- om.configure(MapperFeature.AUTO_DETECT_FIELDS, false);
- om.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
- om.configure(MapperFeature.AUTO_DETECT_SETTERS, false);
- om.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
- om.configure(MapperFeature.USE_GETTERS_AS_SETTERS, false);
- om.configure(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
- om.configure(MapperFeature.INFER_PROPERTY_MUTATORS, false);
- om.configure(MapperFeature.USE_ANNOTATIONS, true);
-
- JavaType javaType = om.getTypeFactory().constructType(TCls.class);
- BeanDescription desc = (BeanDescription) om.getSerializationConfig().introspect(javaType);
- List<BeanPropertyDefinition> props = desc.findProperties();
- if (props.size() != 1) {
- fail("Should find 1 property, not "+props.size()+"; properties = "+props);
- }
- }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestArraySerialization.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestArraySerialization.java
index ee47809..1aa8bd5 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestArraySerialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestArraySerialization.java
@@ -73,8 +73,10 @@
public void testStringArray() throws Exception
{
- String json = MAPPER.writeValueAsString(new String[] { "a", "\"foo\"", null });
- assertEquals("[\"a\",\"\\\"foo\\\"\",null]", json);
+ assertEquals("[\"a\",\"\\\"foo\\\"\",null]",
+ MAPPER.writeValueAsString(new String[] { "a", "\"foo\"", null }));
+ assertEquals("[]",
+ MAPPER.writeValueAsString(new String[] { }));
}
public void testDoubleArray() throws Exception
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestCustomSerializers.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestCustomSerializers.java
index 12e2219..1feeba6 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestCustomSerializers.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestCustomSerializers.java
@@ -22,19 +22,14 @@
/**
* Tests for verifying various issues with custom serializers.
*/
+@SuppressWarnings("serial")
public class TestCustomSerializers extends BaseMapTest
{
- /*
- /**********************************************************
- /* Helper beans
- /**********************************************************
- */
-
static class ElementSerializer extends JsonSerializer<Element>
{
@Override
- public void serialize(Element value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
- jgen.writeString("element");
+ public void serialize(Element value, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {
+ gen.writeString("element");
}
}
@@ -49,7 +44,6 @@
/**
* Trivial simple custom escape definition set.
*/
- @SuppressWarnings("serial")
static class CustomEscapes extends CharacterEscapes
{
private final int[] _asciiEscapes;
@@ -110,8 +104,7 @@
prop = o;
}
}
-
- @SuppressWarnings("serial")
+
static class ParentClassSerializer
extends StdScalarSerializer<Object>
{
@@ -127,7 +120,32 @@
gen.writeString(desc+"/"+value);
}
}
-
+
+ static class UCStringSerializer extends StdScalarSerializer<String>
+ {
+ public UCStringSerializer() { super(String.class); }
+
+ @Override
+ public void serialize(String value, JsonGenerator gen,
+ SerializerProvider provider) throws IOException {
+ gen.writeString(value.toUpperCase());
+ }
+ }
+
+ // IMPORTANT: must associate serializer via property annotations
+ protected static class StringListWrapper
+ {
+ @JsonSerialize(contentUsing=UCStringSerializer.class)
+ public List<String> list;
+
+ public StringListWrapper(String... values) {
+ list = new ArrayList<>();
+ for (String value : values) {
+ list.add(value);
+ }
+ }
+ }
+
/*
/**********************************************************
/* Unit tests
@@ -156,12 +174,13 @@
module.addSerializer(Collection.class, new JsonSerializer<Collection>() {
@Override
- public void serialize(Collection value, JsonGenerator jgen, SerializerProvider provider)
- throws IOException, JsonProcessingException {
+ public void serialize(Collection value, JsonGenerator gen, SerializerProvider provider)
+ throws IOException
+ {
if (value.size() != 0) {
- collectionSerializer.serialize(value, jgen, provider);
+ collectionSerializer.serialize(value, gen, provider);
} else {
- jgen.writeNull();
+ gen.writeNull();
}
}
});
@@ -169,7 +188,7 @@
assertEquals("null", mapper.writeValueAsString(new ArrayList<Object>()));
}
- // [Issue#87]: delegating serializer
+ // [databind#87]: delegating serializer
public void testDelegating() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
@@ -189,7 +208,7 @@
assertEquals("{\"x\":3,\"y\":7}", mapper.writeValueAsString(new Immutable()));
}
- // [Issue#215]: Allow registering CharacterEscapes via ObjectWriter
+ // [databind#215]: Allow registering CharacterEscapes via ObjectWriter
public void testCustomEscapes() throws Exception
{
assertEquals(quote("foo\\u0062\\Ar"),
@@ -207,4 +226,29 @@
assertEquals(aposToQuotes("{'prop':'Issue631Bean/42'}"),
MAPPER.writeValueAsString(new Issue631Bean(42)));
}
+
+ public void testWithCustomElements() throws Exception
+ {
+ // First variant that uses per-property override
+ StringListWrapper wr = new StringListWrapper("a", null, "b");
+ assertEquals(aposToQuotes("{'list':['A',null,'B']}"),
+ MAPPER.writeValueAsString(wr));
+
+ // and then per-type registration
+
+ SimpleModule module = new SimpleModule("test", Version.unknownVersion());
+ module.addSerializer(String.class, new UCStringSerializer());
+ ObjectMapper mapper = new ObjectMapper()
+ .registerModule(module);
+
+ assertEquals(quote("FOOBAR"), mapper.writeValueAsString("foobar"));
+ assertEquals(aposToQuotes("['FOO',null]"),
+ mapper.writeValueAsString(new String[] { "foo", null }));
+
+ List<String> list = Arrays.asList("foo", null);
+ assertEquals(aposToQuotes("['FOO',null]"), mapper.writeValueAsString(list));
+
+ Set<String> set = new LinkedHashSet<String>(Arrays.asList("foo", null));
+ assertEquals(aposToQuotes("['FOO',null]"), mapper.writeValueAsString(set));
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestIterable.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestIterable.java
index 48c2e30..aed6a0f 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestIterable.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestIterable.java
@@ -1,7 +1,6 @@
package com.fasterxml.jackson.databind.ser;
import java.io.IOException;
-import java.io.StringWriter;
import java.util.*;
import com.fasterxml.jackson.core.JsonGenerator;
@@ -27,7 +26,8 @@
return _ints.iterator();
}
}
- // [JACKSON-689]
+
+ @JsonSerialize(typing=JsonSerialize.Typing.STATIC)
static class BeanWithIterable {
private final ArrayList<String> values = new ArrayList<String>();
{
@@ -37,6 +37,15 @@
public Iterable<String> getValues() { return values; }
}
+ static class BeanWithIterator {
+ private final ArrayList<String> values = new ArrayList<String>();
+ {
+ values.add("itValue");
+ }
+
+ public Iterator<String> getValues() { return values.iterator(); }
+ }
+
static class IntIterable implements Iterable<Integer>
{
@Override
@@ -96,37 +105,46 @@
/**********************************************************
*/
- private final static ObjectMapper MAPPER = new ObjectMapper();
-
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ private final ObjectMapper STATIC_MAPPER = new ObjectMapper();
+ {
+ STATIC_MAPPER.enable(MapperFeature.USE_STATIC_TYPING);
+ }
+
public void testIterator() throws IOException
{
- StringWriter sw = new StringWriter();
ArrayList<Integer> l = new ArrayList<Integer>();
l.add(1);
+ l.add(null);
l.add(-9);
l.add(0);
- MAPPER.writeValue(sw, l.iterator());
- assertEquals("[1,-9,0]", sw.toString().trim());
+
+ assertEquals("[1,null,-9,0]", MAPPER.writeValueAsString(l.iterator()));
+ l.clear();
+ assertEquals("[]", MAPPER.writeValueAsString(l.iterator()));
}
public void testIterable() throws IOException
{
- StringWriter sw = new StringWriter();
- MAPPER.writeValue(sw, new IterableWrapper(new int[] { 1, 2, 3 }));
- assertEquals("[1,2,3]", sw.toString().trim());
+ assertEquals("[1,2,3]",
+ MAPPER.writeValueAsString(new IterableWrapper(new int[] { 1, 2, 3 })));
}
- // [JACKSON-689], [JACKSON-876]
public void testWithIterable() throws IOException
{
- // 689:
assertEquals("{\"values\":[\"value\"]}",
- MAPPER.writeValueAsString(new BeanWithIterable()));
- // 876:
+ STATIC_MAPPER.writeValueAsString(new BeanWithIterable()));
assertEquals("[1,2,3]",
- MAPPER.writeValueAsString(new IntIterable()));
+ STATIC_MAPPER.writeValueAsString(new IntIterable()));
}
+ public void testWithIterator() throws IOException
+ {
+ assertEquals("{\"values\":[\"itValue\"]}",
+ STATIC_MAPPER.writeValueAsString(new BeanWithIterator()));
+ }
+
// [databind#358]
public void testIterable358() throws Exception {
String json = MAPPER.writeValueAsString(new B());
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestJsonSerialize.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestJsonSerialize.java
index 01cabf2..483ccb2 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestJsonSerialize.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestJsonSerialize.java
@@ -148,6 +148,7 @@
{
try {
serializeAsString(MAPPER, new BrokenClass());
+ fail("Should not succeed");
} catch (Exception e) {
verifyException(e, "types not related");
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java
index ccd674b..45471d3 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java
@@ -21,6 +21,26 @@
}
}
+ public static class NullKeySerializer extends JsonSerializer<Object>
+ {
+ private String _null;
+ public NullKeySerializer(String s) { _null = s; }
+ @Override
+ public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
+ gen.writeFieldName(_null);
+ }
+ }
+
+ public static class NullValueSerializer extends JsonSerializer<Object>
+ {
+ private String _null;
+ public NullValueSerializer(String s) { _null = s; }
+ @Override
+ public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
+ gen.writeString(_null);
+ }
+ }
+
public static class NotKarlBean
{
public Map<String,Integer> map = new HashMap<String,Integer>();
@@ -148,6 +168,19 @@
assertEquals("{\"stuff\":{\"xxxB\":\"bar\"}}", json);
}
+ public void testCustomNullSerializers() throws IOException
+ {
+ final ObjectMapper mapper = new ObjectMapper();
+ mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer("NULL-KEY"));
+ mapper.getSerializerProvider().setNullValueSerializer(new NullValueSerializer("NULL"));
+ Map<String,Integer> input = new HashMap<>();
+ input.put(null, 3);
+ String json = mapper.writeValueAsString(input);
+ assertEquals("{\"NULL-KEY\":3}", json);
+ json = mapper.writeValueAsString(new Object[] { 1, null, true });
+ assertEquals("[1,\"NULL\",true]", json);
+ }
+
public void testCustomEnumInnerMapKey() throws Exception {
Map<Outer, Object> outerMap = new HashMap<Outer, Object>();
Map<ABC, Map<String, String>> map = new EnumMap<ABC, Map<String, String>>(ABC.class);
@@ -190,6 +223,7 @@
}
// [databind#838]
+ @SuppressWarnings("deprecation")
public void testUnWrappedMapWithKeySerializer() throws Exception{
SimpleModule mod = new SimpleModule("test");
mod.addKeySerializer(ABC.class, new ABCKeySerializer());
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestMapSerialization.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestMapSerialization.java
index a220237..26cabd6 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestMapSerialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestMapSerialization.java
@@ -15,7 +15,7 @@
@SuppressWarnings("serial")
public class TestMapSerialization extends BaseMapTest
{
- @JsonSerialize(using=MapSerializer.class)
+ @JsonSerialize(using=PseudoMapSerializer.class)
static class PseudoMap extends LinkedHashMap<String,String>
{
public PseudoMap(String... values) {
@@ -25,7 +25,7 @@
}
}
- static class MapSerializer extends JsonSerializer<Map<String,String>>
+ static class PseudoMapSerializer extends JsonSerializer<Map<String,String>>
{
@Override
public void serialize(Map<String,String> value,
@@ -36,16 +36,6 @@
}
}
- // For [JACKSON-574]
- static class DefaultKeySerializer extends JsonSerializer<Object>
- {
- @Override
- public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException
- {
- jgen.writeFieldName("DEFAULT:"+value);
- }
- }
-
// [databind#335]
static class MapOrderingBean {
@JsonPropertyOrder(alphabetic=true)
@@ -93,29 +83,6 @@
}
}
- // for [databind#47]
- public static class Wat
- {
- private final String wat;
-
- @JsonCreator
- Wat(String wat) {
- this.wat = wat;
- }
-
- @JsonValue
- public String getWat() {
- return wat;
- }
-
- @Override
- public String toString() {
- return "(String)[Wat: " + wat + "]";
- }
- }
-
- static class WatMap extends HashMap<Wat,Boolean> { }
-
// for [databind#691]
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME)
@JsonTypeName("mymap")
@@ -147,7 +114,7 @@
}
// problems with map entries, values
- public void testMapKeyValueSerialization() throws IOException
+ public void testMapKeySetValuesSerialization() throws IOException
{
Map<String,String> map = new HashMap<String,String>();
map.put("a", "b");
@@ -167,16 +134,6 @@
assertEquals("[\"f\"]", MAPPER.writeValueAsString(map.values()));
}
- // For [JACKSON-574]
- public void testDefaultKeySerializer() throws IOException
- {
- ObjectMapper m = new ObjectMapper();
- m.getSerializerProvider().setDefaultKeySerializer(new DefaultKeySerializer());
- Map<String,String> map = new HashMap<String,String>();
- map.put("a", "b");
- assertEquals("{\"DEFAULT:a\":\"b\"}", m.writeValueAsString(map));
- }
-
// sort Map entries by key
public void testOrderByKey() throws IOException
{
@@ -242,26 +199,6 @@
assertEquals(aposToQuotes("{'value':{'answer':42}}"), json);
}
- // [databind#47]
- public void testMapJsonValueKey47() throws Exception
- {
- WatMap input = new WatMap();
- input.put(new Wat("3"), true);
-
- ObjectMapper mapper = new ObjectMapper();
- String json = mapper.writeValueAsString(input);
- assertEquals(aposToQuotes("{'3':true}"), json);
- }
-
- // [databind#682]
- public void testClassKey() throws IOException
- {
- Map<Class<?>,Integer> map = new LinkedHashMap<Class<?>,Integer>();
- map.put(String.class, 2);
- String json = MAPPER.writeValueAsString(map);
- assertEquals(aposToQuotes("{'java.lang.String':2}"), json);
- }
-
// [databind#691]
public void testNullJsonMapping691() throws Exception
{
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestRootType.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestRootType.java
index 0795bfe..56af44d 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestRootType.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestRootType.java
@@ -56,7 +56,7 @@
public int a = 3;
}
- // [Issue#412]
+ // [databind#412]
static class TestCommandParent {
public String uuid;
public int type;
@@ -190,14 +190,13 @@
assertEquals("456", mapper.writerFor(Long.TYPE).writeValueAsString(Long.valueOf(456L)));
}
- // [JACKSON-630] also, allow annotation to define root name
public void testRootNameAnnotation() throws Exception
{
String json = WRAP_ROOT_MAPPER.writeValueAsString(new WithRootName());
assertEquals("{\"root\":{\"a\":3}}", json);
}
- // [Issue#412]
+ // [databind#412]
public void testRootNameWithExplicitType() throws Exception
{
TestCommandChild cmd = new TestCommandChild();
@@ -207,6 +206,6 @@
ObjectWriter writer = WRAP_ROOT_MAPPER.writerFor(TestCommandParent.class);
String json = writer.writeValueAsString(cmd);
- assertEquals(json, "{\"TestCommandParent\":{\"uuid\":\"1234\",\"type\":1}}");
+ assertEquals("{\"TestCommandParent\":{\"uuid\":\"1234\",\"type\":1}}", json);
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestSimpleAtomicTypes.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestSimpleAtomicTypes.java
deleted file mode 100644
index afce72b..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestSimpleAtomicTypes.java
+++ /dev/null
@@ -1,60 +0,0 @@
-package com.fasterxml.jackson.databind.ser;
-
-import java.util.concurrent.atomic.*;
-
-import com.fasterxml.jackson.databind.BaseMapTest;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-
-/**
- * Unit tests for verifying serialization of simple basic non-structured
- * types; primitives (and/or their wrappers), Strings.
- */
-public class TestSimpleAtomicTypes
- extends BaseMapTest
-{
- static class UCStringWrapper {
- @JsonSerialize(contentUsing=UpperCasingSerializer.class)
- public AtomicReference<String> value;
-
- public UCStringWrapper(String s) { value = new AtomicReference<String>(s); }
- }
-
- /*
- /**********************************************************
- /* Test methods
- /**********************************************************
- */
-
- private final ObjectMapper MAPPER = objectMapper();
-
- public void testAtomicBoolean() throws Exception
- {
- assertEquals("true", MAPPER.writeValueAsString(new AtomicBoolean(true)));
- assertEquals("false", MAPPER.writeValueAsString(new AtomicBoolean(false)));
- }
-
- public void testAtomicInteger() throws Exception
- {
- assertEquals("1", MAPPER.writeValueAsString(new AtomicInteger(1)));
- assertEquals("-9", MAPPER.writeValueAsString(new AtomicInteger(-9)));
- }
-
- public void testAtomicLong() throws Exception
- {
- assertEquals("0", MAPPER.writeValueAsString(new AtomicLong(0)));
- }
-
- public void testAtomicReference() throws Exception
- {
- String[] strs = new String[] { "abc" };
- assertEquals("[\"abc\"]", MAPPER.writeValueAsString(new AtomicReference<String[]>(strs)));
- }
-
- public void testCustomSerializer() throws Exception
- {
- final String VALUE = "fooBAR";
- String json = MAPPER.writeValueAsString(new UCStringWrapper(VALUE));
- assertEquals(json, aposToQuotes("{'value':'FOOBAR'}"));
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/IgnorePropsForSerTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/IgnorePropsForSerTest.java
similarity index 98%
rename from src/test/java/com/fasterxml/jackson/databind/filter/IgnorePropsForSerTest.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/filter/IgnorePropsForSerTest.java
index a8342bd..c9879f2 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/IgnorePropsForSerTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/IgnorePropsForSerTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.ser.filter;
import java.util.*;
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/JsonInclude1327Test.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonInclude1327Test.java
similarity index 96%
rename from src/test/java/com/fasterxml/jackson/databind/filter/JsonInclude1327Test.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonInclude1327Test.java
index cf11a4d..bac6df4 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/JsonInclude1327Test.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonInclude1327Test.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.ser.filter;
import java.util.*;
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeArrayTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeArrayTest.java
new file mode 100644
index 0000000..4a48cf7
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeArrayTest.java
@@ -0,0 +1,127 @@
+package com.fasterxml.jackson.databind.ser.filter;
+
+import java.io.IOException;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+public class JsonIncludeArrayTest extends BaseMapTest
+{
+ static class NonEmptyByteArray {
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ public byte[] value;
+
+ public NonEmptyByteArray(byte... v) { value = v; }
+ }
+
+ static class NonEmptyShortArray {
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ public short[] value;
+
+ public NonEmptyShortArray(short... v) { value = v; }
+ }
+
+ static class NonEmptyCharArray {
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ public char[] value;
+
+ public NonEmptyCharArray(char... v) { value = v; }
+ }
+
+ static class NonEmptyIntArray {
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ public int[] value;
+
+ public NonEmptyIntArray(int... v) { value = v; }
+ }
+
+ static class NonEmptyLongArray {
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ public long[] value;
+
+ public NonEmptyLongArray(long... v) { value = v; }
+ }
+
+ static class NonEmptyBooleanArray {
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ public boolean[] value;
+
+ public NonEmptyBooleanArray(boolean... v) { value = v; }
+ }
+
+ static class NonEmptyDoubleArray {
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ public double[] value;
+
+ public NonEmptyDoubleArray(double... v) { value = v; }
+ }
+
+ static class NonEmptyFloatArray {
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ public float[] value;
+
+ public NonEmptyFloatArray(float... v) { value = v; }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ final private ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testByteArray() throws IOException
+ {
+ assertEquals("{}", MAPPER.writeValueAsString(new NonEmptyByteArray()));
+ }
+
+ public void testShortArray() throws IOException
+ {
+ assertEquals("{}", MAPPER.writeValueAsString(new NonEmptyShortArray()));
+ assertEquals("{\"value\":[1]}", MAPPER.writeValueAsString(new NonEmptyShortArray((short) 1)));
+ }
+
+ public void testCharArray() throws IOException
+ {
+ assertEquals("{}", MAPPER.writeValueAsString(new NonEmptyCharArray()));
+ // by default considered to be serialized as String
+ assertEquals("{\"value\":\"ab\"}", MAPPER.writeValueAsString(new NonEmptyCharArray('a', 'b')));
+ // but can force as sparse (real) array too
+ assertEquals("{\"value\":[\"a\",\"b\"]}", MAPPER
+ .writer().with(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS)
+ .writeValueAsString(new NonEmptyCharArray('a', 'b')));
+ }
+
+ public void testIntArray() throws IOException
+ {
+ assertEquals("{}", MAPPER.writeValueAsString(new NonEmptyIntArray()));
+ assertEquals("{\"value\":[2]}", MAPPER.writeValueAsString(new NonEmptyIntArray(2)));
+ }
+
+ public void testLongArray() throws IOException
+ {
+ assertEquals("{}", MAPPER.writeValueAsString(new NonEmptyLongArray()));
+ assertEquals("{\"value\":[3,4]}", MAPPER.writeValueAsString(new NonEmptyLongArray(3, 4)));
+ }
+
+ public void testBooleanArray() throws IOException
+ {
+ assertEquals("{}", MAPPER.writeValueAsString(new NonEmptyBooleanArray()));
+ assertEquals("{\"value\":[true,false]}", MAPPER.writeValueAsString(new NonEmptyBooleanArray(true,false)));
+ }
+
+ public void testDoubleArray() throws IOException
+ {
+ assertEquals("{}", MAPPER.writeValueAsString(new NonEmptyDoubleArray()));
+ assertEquals("{\"value\":[0.25,-1.0]}", MAPPER.writeValueAsString(new NonEmptyDoubleArray(0.25,-1.0)));
+ }
+
+ public void testFloatArray() throws IOException
+ {
+ assertEquals("{}", MAPPER.writeValueAsString(new NonEmptyFloatArray()));
+ assertEquals("{\"value\":[0.5]}", MAPPER.writeValueAsString(new NonEmptyFloatArray(0.5f)));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeCollectionTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeCollectionTest.java
new file mode 100644
index 0000000..699a194
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeCollectionTest.java
@@ -0,0 +1,39 @@
+package com.fasterxml.jackson.databind.ser.filter;
+
+import java.util.Arrays;
+import java.util.EnumSet;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class JsonIncludeCollectionTest extends BaseMapTest
+{
+ static class NonEmptyEnumSet {
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ public EnumSet<ABC> v;
+
+ public NonEmptyEnumSet(ABC...values) {
+ if (values.length == 0) {
+ v = EnumSet.noneOf(ABC.class);
+ } else {
+ v = EnumSet.copyOf(Arrays.asList(values));
+ }
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ final private ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testEnumSet() throws Exception
+ {
+ assertEquals("{}", MAPPER.writeValueAsString(new NonEmptyEnumSet()));
+ assertEquals("{\"v\":[\"B\"]}", MAPPER.writeValueAsString(new NonEmptyEnumSet(ABC.B)));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeCustomTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeCustomTest.java
new file mode 100644
index 0000000..0e3b0f0
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeCustomTest.java
@@ -0,0 +1,102 @@
+package com.fasterxml.jackson.databind.ser.filter;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.*;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
+
+// Tests for [databind#888]
+public class JsonIncludeCustomTest extends BaseMapTest
+{
+ static class FooFilter {
+ @Override
+ public boolean equals(Object other) {
+ if (other == null) { // do NOT filter out nulls
+ return false;
+ }
+ // in fact, only filter out exact String "foo"
+ return "foo".equals(other);
+ }
+ }
+
+ // for testing prob with `equals(null)` which SHOULD be allowed
+ static class BrokenFilter {
+ @Override
+ public boolean equals(Object other) {
+ /*String str = */ other.toString();
+ return false;
+ }
+ }
+
+ static class FooBean {
+ @JsonInclude(value=JsonInclude.Include.CUSTOM,
+ valueFilter=FooFilter.class)
+ public String value;
+
+ public FooBean(String v) { value = v; }
+ }
+
+ static class FooMapBean {
+ @JsonInclude(content=JsonInclude.Include.CUSTOM,
+ contentFilter=FooFilter.class)
+ public Map<String,String> stuff = new LinkedHashMap<String,String>();
+
+ public FooMapBean add(String key, String value) {
+ stuff.put(key, value);
+ return this;
+ }
+ }
+
+ static class BrokenBean {
+ @JsonInclude(value=JsonInclude.Include.CUSTOM,
+ valueFilter=BrokenFilter.class)
+ public String value;
+
+ public BrokenBean(String v) { value = v; }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, success
+ /**********************************************************
+ */
+
+ final private ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testSimpleCustomFilter() throws Exception
+ {
+ assertEquals(aposToQuotes("{'value':'x'}"), MAPPER.writeValueAsString(new FooBean("x")));
+ assertEquals("{}", MAPPER.writeValueAsString(new FooBean("foo")));
+ }
+
+ public void testCustomFilterWithMap() throws Exception
+ {
+ FooMapBean input = new FooMapBean()
+ .add("a", "1")
+ .add("b", "foo")
+ .add("c", "2");
+
+ assertEquals(aposToQuotes("{'stuff':{'a':'1','c':'2'}}"), MAPPER.writeValueAsString(input));
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, fail handling
+ /**********************************************************
+ */
+
+ public void testBrokenFilter() throws Exception
+ {
+ try {
+ String json = MAPPER.writeValueAsString(new BrokenBean("foo"));
+ fail("Should not pass, produced: "+json);
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "Problem determining whether filter of type");
+ verifyException(e, "filter out `null`");
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeOverrideTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeOverrideTest.java
new file mode 100644
index 0000000..1a8dbc5
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeOverrideTest.java
@@ -0,0 +1,191 @@
+package com.fasterxml.jackson.databind.ser.filter;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * Unit tests for checking that overridden settings for
+ * {@link com.fasterxml.jackson.databind.annotation.JsonSerialize#include} annotation property work
+ * as expected.
+ */
+public class JsonIncludeOverrideTest
+ extends BaseMapTest
+{
+ @JsonPropertyOrder({"list", "map"})
+ static class EmptyListMapBean
+ {
+ public List<String> list = Collections.emptyList();
+
+ public Map<String,String> map = Collections.emptyMap();
+ }
+
+ @JsonInclude(JsonInclude.Include.ALWAYS)
+ @JsonPropertyOrder({"num", "annotated", "plain"})
+ static class MixedTypeAlwaysBean
+ {
+ @JsonInclude(JsonInclude.Include.USE_DEFAULTS)
+ public Integer num = null;
+
+ @JsonInclude(JsonInclude.Include.NON_NULL)
+ public String annotated = null;
+
+ public String plain = null;
+ }
+
+ @JsonInclude(JsonInclude.Include.NON_NULL)
+ @JsonPropertyOrder({"num", "annotated", "plain"})
+ static class MixedTypeNonNullBean
+ {
+ @JsonInclude(JsonInclude.Include.USE_DEFAULTS)
+ public Integer num = null;
+
+ @JsonInclude(JsonInclude.Include.ALWAYS)
+ public String annotated = null;
+
+ public String plain = null;
+ }
+
+ public void testPropConfigOverridesForInclude() throws IOException
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ // First, with defaults, both included:
+ JsonIncludeOverrideTest.EmptyListMapBean empty = new JsonIncludeOverrideTest.EmptyListMapBean();
+ assertEquals(aposToQuotes("{'list':[],'map':{}}"),
+ mapper.writeValueAsString(empty));
+
+ // and then change inclusion criteria for either
+ mapper = new ObjectMapper();
+ mapper.configOverride(Map.class)
+ .setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, null));
+ assertEquals(aposToQuotes("{'list':[]}"),
+ mapper.writeValueAsString(empty));
+
+ mapper = new ObjectMapper();
+ mapper.configOverride(List.class)
+ .setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, null));
+ assertEquals(aposToQuotes("{'map':{}}"),
+ mapper.writeValueAsString(empty));
+ }
+
+ public void testOverrideForIncludeAsPropertyNonNull() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ // First, with defaults, all but NON_NULL annotated included
+ JsonIncludeOverrideTest.MixedTypeAlwaysBean nullValues = new JsonIncludeOverrideTest.MixedTypeAlwaysBean();
+ assertEquals(aposToQuotes("{'num':null,'plain':null}"),
+ mapper.writeValueAsString(nullValues));
+
+ // and then change inclusion as property criteria for either
+ mapper = new ObjectMapper();
+ mapper.configOverride(String.class)
+ .setIncludeAsProperty(JsonInclude.Value
+ .construct(JsonInclude.Include.NON_NULL, null));
+ assertEquals("{\"num\":null}",
+ mapper.writeValueAsString(nullValues));
+
+ mapper = new ObjectMapper();
+ mapper.configOverride(Integer.class)
+ .setIncludeAsProperty(JsonInclude.Value
+ .construct(JsonInclude.Include.NON_NULL, null));
+ assertEquals("{\"plain\":null}",
+ mapper.writeValueAsString(nullValues));
+ }
+
+ public void testOverrideForIncludeAsPropertyAlways() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ // First, with defaults, only ALWAYS annotated included
+ JsonIncludeOverrideTest.MixedTypeNonNullBean nullValues = new JsonIncludeOverrideTest.MixedTypeNonNullBean();
+ assertEquals("{\"annotated\":null}",
+ mapper.writeValueAsString(nullValues));
+
+ // and then change inclusion as property criteria for either
+ mapper = new ObjectMapper();
+ mapper.configOverride(String.class)
+ .setIncludeAsProperty(JsonInclude.Value
+ .construct(JsonInclude.Include.ALWAYS, null));
+ assertEquals(aposToQuotes("{'annotated':null,'plain':null}"),
+ mapper.writeValueAsString(nullValues));
+
+ mapper = new ObjectMapper();
+ mapper.configOverride(Integer.class)
+ .setIncludeAsProperty(JsonInclude.Value
+ .construct(JsonInclude.Include.ALWAYS, null));
+ assertEquals(aposToQuotes("{'num':null,'annotated':null}"),
+ mapper.writeValueAsString(nullValues));
+ }
+
+ public void testOverridesForIncludeAndIncludeAsPropertyNonNull() throws Exception
+ {
+ // First, with ALWAYS override on containing bean, all included
+ JsonIncludeOverrideTest.MixedTypeNonNullBean nullValues = new JsonIncludeOverrideTest.MixedTypeNonNullBean();
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configOverride(JsonIncludeOverrideTest.MixedTypeNonNullBean.class)
+ .setInclude(JsonInclude.Value
+ .construct(JsonInclude.Include.ALWAYS, null));
+ assertEquals(aposToQuotes("{'num':null,'annotated':null,'plain':null}"),
+ mapper.writeValueAsString(nullValues));
+
+ // and then change inclusion as property criteria for either
+ mapper = new ObjectMapper();
+ mapper.configOverride(JsonIncludeOverrideTest.MixedTypeNonNullBean.class)
+ .setInclude(JsonInclude.Value
+ .construct(JsonInclude.Include.ALWAYS, null));
+ mapper.configOverride(String.class)
+ .setIncludeAsProperty(JsonInclude.Value
+ .construct(JsonInclude.Include.NON_NULL, null));
+ assertEquals(aposToQuotes("{'num':null,'annotated':null}"),
+ mapper.writeValueAsString(nullValues));
+
+ mapper = new ObjectMapper();
+ mapper.configOverride(JsonIncludeOverrideTest.MixedTypeNonNullBean.class)
+ .setInclude(JsonInclude.Value
+ .construct(JsonInclude.Include.ALWAYS, null));
+ mapper.configOverride(Integer.class)
+ .setIncludeAsProperty(JsonInclude.Value
+ .construct(JsonInclude.Include.NON_NULL, null));
+ assertEquals(aposToQuotes("{'annotated':null,'plain':null}"),
+ mapper.writeValueAsString(nullValues));
+ }
+
+ public void testOverridesForIncludeAndIncludeAsPropertyAlways() throws Exception
+ {
+ // First, with NON_NULL override on containing bean, empty
+ JsonIncludeOverrideTest.MixedTypeAlwaysBean nullValues = new JsonIncludeOverrideTest.MixedTypeAlwaysBean();
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configOverride(JsonIncludeOverrideTest.MixedTypeAlwaysBean.class)
+ .setInclude(JsonInclude.Value
+ .construct(JsonInclude.Include.NON_NULL, null));
+ assertEquals("{}",
+ mapper.writeValueAsString(nullValues));
+
+ // and then change inclusion as property criteria for either
+ mapper = new ObjectMapper();
+ mapper.configOverride(JsonIncludeOverrideTest.MixedTypeAlwaysBean.class)
+ .setInclude(JsonInclude.Value
+ .construct(JsonInclude.Include.NON_NULL, null));
+ mapper.configOverride(String.class)
+ .setIncludeAsProperty(JsonInclude.Value
+ .construct(JsonInclude.Include.ALWAYS, null));
+ assertEquals("{\"plain\":null}",
+ mapper.writeValueAsString(nullValues));
+
+ mapper = new ObjectMapper();
+ mapper.configOverride(JsonIncludeOverrideTest.MixedTypeAlwaysBean.class)
+ .setInclude(JsonInclude.Value
+ .construct(JsonInclude.Include.NON_NULL, null));
+ mapper.configOverride(Integer.class)
+ .setIncludeAsProperty(JsonInclude.Value
+ .construct(JsonInclude.Include.ALWAYS, null));
+ assertEquals("{\"num\":null}",
+ mapper.writeValueAsString(nullValues));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/JsonIncludeTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeTest.java
similarity index 84%
rename from src/test/java/com/fasterxml/jackson/databind/filter/JsonIncludeTest.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeTest.java
index a5aa82e..5d18dff 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/JsonIncludeTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.ser.filter;
import java.io.IOException;
import java.util.*;
@@ -121,12 +121,32 @@
public NonEmptyDouble(double v) { value = v; }
}
- @JsonPropertyOrder({"list", "map"})
- static class EmptyListMapBean
- {
- public List<String> list = Collections.emptyList();
+ static class NonEmpty<T> {
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ public T value;
- public Map<String,String> map = Collections.emptyMap();
+ public NonEmpty(T v) { value = v; }
+ }
+
+ static class NonEmptyDate extends NonEmpty<Date> {
+ public NonEmptyDate(Date v) { super(v); }
+ }
+ static class NonEmptyCalendar extends NonEmpty<Calendar> {
+ public NonEmptyCalendar(Calendar v) { super(v); }
+ }
+
+ static class NonDefault<T> {
+ @JsonInclude(JsonInclude.Include.NON_DEFAULT)
+ public T value;
+
+ public NonDefault(T v) { value = v; }
+ }
+
+ static class NonDefaultDate extends NonDefault<Date> {
+ public NonDefaultDate(Date v) { super(v); }
+ }
+ static class NonDefaultCalendar extends NonDefault<Calendar> {
+ public NonDefaultCalendar(Calendar v) { super(v); }
}
// [databind#1351]
@@ -176,7 +196,7 @@
/*
/**********************************************************
- /* Unit tests
+ /* Test methods
/**********************************************************
*/
@@ -281,34 +301,11 @@
assertEquals("{\"value\":1.25}", defMapper.writeValueAsString(new NonEmptyDouble(1.25)));
assertEquals("{\"value\":0.0}", defMapper.writeValueAsString(new NonEmptyDouble(0.0)));
-
IntWrapper zero = new IntWrapper(0);
assertEquals("{\"i\":0}", defMapper.writeValueAsString(zero));
assertEquals("{\"i\":0}", inclMapper.writeValueAsString(zero));
}
- public void testPropConfigOverridesForInclude() throws IOException
- {
- // First, with defaults, both included:
- EmptyListMapBean empty = new EmptyListMapBean();
- assertEquals(aposToQuotes("{'list':[],'map':{}}"),
- MAPPER.writeValueAsString(empty));
- ObjectMapper mapper;
-
- // and then change inclusion criteria for either
- mapper = new ObjectMapper();
- mapper.configOverride(Map.class)
- .setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, null));
- assertEquals(aposToQuotes("{'list':[]}"),
- mapper.writeValueAsString(empty));
-
- mapper = new ObjectMapper();
- mapper.configOverride(List.class)
- .setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, null));
- assertEquals(aposToQuotes("{'map':{}}"),
- mapper.writeValueAsString(empty));
- }
-
// [databind#1351], [databind#1417]
public void testIssue1351() throws Exception
{
@@ -320,4 +317,27 @@
assertEquals(aposToQuotes("{}"),
mapper.writeValueAsString(new Issue1351NonBean(0)));
}
+
+ // [databind#1550]
+ public void testInclusionOfDate() throws Exception
+ {
+ final Date input = new Date(0L);
+ assertEquals(aposToQuotes("{'value':0}"),
+ MAPPER.writeValueAsString(new NonEmptyDate(input)));
+ assertEquals("{}",
+ MAPPER.writeValueAsString(new NonDefaultDate(input)));
+
+
+ }
+
+ // [databind#1550]
+ public void testInclusionOfCalendar() throws Exception
+ {
+ final Calendar input = new GregorianCalendar();
+ input.setTimeInMillis(0L);
+ assertEquals(aposToQuotes("{'value':0}"),
+ MAPPER.writeValueAsString(new NonEmptyCalendar(input)));
+ assertEquals("{}",
+ MAPPER.writeValueAsString(new NonDefaultCalendar(input)));
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/filter/MapInclusionTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/MapInclusionTest.java
new file mode 100644
index 0000000..cb8eb09
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/MapInclusionTest.java
@@ -0,0 +1,84 @@
+package com.fasterxml.jackson.databind.ser.filter;
+
+import java.io.IOException;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.*;
+
+public class MapInclusionTest extends BaseMapTest
+{
+ static class NoEmptiesMapContainer {
+ @JsonInclude(value=JsonInclude.Include.NON_EMPTY,
+ content=JsonInclude.Include.NON_EMPTY)
+ public Map<String,String> stuff = new LinkedHashMap<String,String>();
+
+ public NoEmptiesMapContainer add(String key, String value) {
+ stuff.put(key, value);
+ return this;
+ }
+ }
+
+ static class NoNullsMapContainer {
+ @JsonInclude(value=JsonInclude.Include.NON_NULL,
+ content=JsonInclude.Include.NON_NULL)
+ public Map<String,String> stuff = new LinkedHashMap<String,String>();
+
+ public NoNullsMapContainer add(String key, String value) {
+ stuff.put(key, value);
+ return this;
+ }
+ }
+
+ static class NoNullsNotEmptyMapContainer {
+ @JsonInclude(value=JsonInclude.Include.NON_EMPTY,
+ content=JsonInclude.Include.NON_NULL)
+ public Map<String,String> stuff = new LinkedHashMap<String,String>();
+
+ public NoNullsNotEmptyMapContainer add(String key, String value) {
+ stuff.put(key, value);
+ return this;
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ final private ObjectMapper MAPPER = objectMapper();
+
+ // [databind#588]
+ public void testNonEmptyValueMapViaProp() throws IOException
+ {
+ String json = MAPPER.writeValueAsString(new NoEmptiesMapContainer()
+ .add("a", null)
+ .add("b", ""));
+ assertEquals(aposToQuotes("{}"), json);
+ }
+
+ public void testNoNullsMap() throws IOException
+ {
+ NoNullsMapContainer input = new NoNullsMapContainer()
+ .add("a", null)
+ .add("b", "");
+ String json = MAPPER.writeValueAsString(input);
+ assertEquals(aposToQuotes("{'stuff':{'b':''}}"), json);
+ }
+
+ public void testNonEmptyNoNullsMap() throws IOException
+ {
+ NoNullsNotEmptyMapContainer input = new NoNullsNotEmptyMapContainer()
+ .add("a", null)
+ .add("b", "");
+ String json = MAPPER.writeValueAsString(input);
+ assertEquals(aposToQuotes("{'stuff':{'b':''}}"), json);
+
+ json = MAPPER.writeValueAsString(new NoNullsNotEmptyMapContainer()
+ .add("a", null)
+ .add("b", null));
+ assertEquals(aposToQuotes("{}"), json);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/NullSerializationTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/NullSerializationTest.java
similarity index 91%
rename from src/test/java/com/fasterxml/jackson/databind/filter/NullSerializationTest.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/filter/NullSerializationTest.java
index dc74036..d1cf083 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/NullSerializationTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/NullSerializationTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.ser.filter;
import java.io.*;
@@ -118,17 +118,6 @@
assertEquals("{\"a\":\"foobar\"}", m.writeValueAsString(root));
}
- /* 14-Oct-2013, tatu: Support for annotating classes is not
- * implemented yet.
- */
-/*
- public void testNullSerializerViaClass() throws Exception
- {
- assertEquals("[\"foobar\"]",
- MAPPER.writeValueAsString(new NullValuedType[] { new NullValuedType() }));
- }
- */
-
public void testNullSerializerForProperty() throws Exception
{
assertEquals("{\"a\":\"foobar\"}", MAPPER.writeValueAsString(new BeanWithNullProps()));
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/ReadOnlyProperties95Test.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/ReadOnlyProperties95Test.java
similarity index 93%
rename from src/test/java/com/fasterxml/jackson/databind/filter/ReadOnlyProperties95Test.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/filter/ReadOnlyProperties95Test.java
index a00b5c6..371e051 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/ReadOnlyProperties95Test.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/ReadOnlyProperties95Test.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.ser.filter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.*;
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/TestAnyGetterFiltering.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/TestAnyGetterFiltering.java
similarity index 97%
rename from src/test/java/com/fasterxml/jackson/databind/filter/TestAnyGetterFiltering.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/filter/TestAnyGetterFiltering.java
index 6a7f8d7..b6d5f31 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/TestAnyGetterFiltering.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/TestAnyGetterFiltering.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.ser.filter;
import java.util.*;
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestIgnoredTypes.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/TestIgnoredTypes.java
similarity index 98%
rename from src/test/java/com/fasterxml/jackson/databind/deser/TestIgnoredTypes.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/filter/TestIgnoredTypes.java
index 01b5c51..3279c02 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestIgnoredTypes.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/TestIgnoredTypes.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.deser;
+package com.fasterxml.jackson.databind.ser.filter;
import java.util.ArrayList;
import java.util.List;
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/TestJsonFilter.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/TestJsonFilter.java
similarity index 99%
rename from src/test/java/com/fasterxml/jackson/databind/filter/TestJsonFilter.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/filter/TestJsonFilter.java
index b607af7..cd2560c 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/TestJsonFilter.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/TestJsonFilter.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.ser.filter;
import com.fasterxml.jackson.annotation.*;
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/TestMapFiltering.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/TestMapFiltering.java
similarity index 79%
rename from src/test/java/com/fasterxml/jackson/databind/filter/TestMapFiltering.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/filter/TestMapFiltering.java
index a7a3729..d78ad4a 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/TestMapFiltering.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/TestMapFiltering.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.ser.filter;
import java.io.IOException;
import java.lang.annotation.*;
@@ -27,7 +27,7 @@
@JsonFilter("filterForMaps")
static class FilteredBean extends LinkedHashMap<String,Integer> { }
-
+
static class MapBean {
@JsonFilter("filterX")
@CustomOffset(1)
@@ -41,14 +41,31 @@
}
}
+ static class MapBeanNoOffset {
+ @JsonFilter("filterX")
+ public Map<String,Integer> values;
+
+ public MapBeanNoOffset() {
+ values = new LinkedHashMap<String,Integer>();
+ values.put("a", 1);
+ values.put("b", 2);
+ values.put("c", 3);
+ }
+ }
+
static class TestMapFilter implements PropertyFilter
{
@Override
- public void serializeAsField(Object value, JsonGenerator jgen,
+ public void serializeAsField(Object value, JsonGenerator g,
SerializerProvider provider, PropertyWriter writer)
throws Exception
{
String name = writer.getName();
+
+ // sanity checks
+ assertNotNull(writer.getType());
+ assertEquals(name, writer.getFullName().getSimpleName());
+
if (!"a".equals(name)) {
return;
}
@@ -56,7 +73,7 @@
int offset = (n == null) ? 0 : n.value();
Integer I = offset + ((Integer) value).intValue();
- writer.serializeAsField(I, jgen, provider);
+ writer.serializeAsField(I, g, provider);
}
@Override
@@ -70,15 +87,12 @@
@Deprecated
public void depositSchemaProperty(PropertyWriter writer,
ObjectNode propertiesNode, SerializerProvider provider)
- throws JsonMappingException {
-
- }
+ throws JsonMappingException { }
@Override
public void depositSchemaProperty(PropertyWriter writer,
JsonObjectFormatVisitor objectVisitor,
- SerializerProvider provider) throws JsonMappingException {
- }
+ SerializerProvider provider) throws JsonMappingException { }
}
// [databind#527]
@@ -180,6 +194,10 @@
String json = MAPPER.writer(prov).writeValueAsString(new MapBean());
// a=1 should become a=2
assertEquals(aposToQuotes("{'values':{'a':2}}"), json);
+
+ // and then one without annotation as contrast
+ json = MAPPER.writer(prov).writeValueAsString(new MapBeanNoOffset());
+ assertEquals(aposToQuotes("{'values':{'a':1}}"), json);
}
// [databind#527]
@@ -212,6 +230,7 @@
assertEquals(aposToQuotes("{'a':'foo'}"), json);
}
+ @SuppressWarnings("deprecation")
public void testMapNullSerialization() throws IOException
{
ObjectMapper m = new ObjectMapper();
@@ -220,7 +239,9 @@
// by default, should output null-valued entries:
assertEquals("{\"a\":null}", m.writeValueAsString(map));
// but not if explicitly asked not to (note: config value is dynamic here)
- m.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
+
+ m = new ObjectMapper();
+ m.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
assertEquals("{}", m.writeValueAsString(map));
}
@@ -240,4 +261,31 @@
.add("b", null)));
assertEquals(aposToQuotes("{}"), json);
}
+
+ public void testMapViaGlobalNonEmpty() throws Exception
+ {
+ // basic Map<String,String> subclass:
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.setDefaultPropertyInclusion(JsonInclude.Value.empty()
+ .withContentInclusion(JsonInclude.Include.NON_EMPTY));
+ assertEquals(aposToQuotes("{'a':'b'}"), mapper.writeValueAsString(
+ new StringMap497()
+ .add("x", "")
+ .add("a", "b")
+ ));
+ }
+
+ public void testMapViaTypeOverride() throws Exception
+ {
+ // basic Map<String,String> subclass:
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configOverride(Map.class)
+ .setInclude(JsonInclude.Value.empty()
+ .withContentInclusion(JsonInclude.Include.NON_EMPTY));
+ assertEquals(aposToQuotes("{'a':'b'}"), mapper.writeValueAsString(
+ new StringMap497()
+ .add("foo", "")
+ .add("a", "b")
+ ));
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/filter/TestSimpleSerializationIgnore.java b/src/test/java/com/fasterxml/jackson/databind/ser/filter/TestSimpleSerializationIgnore.java
similarity index 98%
rename from src/test/java/com/fasterxml/jackson/databind/filter/TestSimpleSerializationIgnore.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/filter/TestSimpleSerializationIgnore.java
index 5a580f7..92c7054 100644
--- a/src/test/java/com/fasterxml/jackson/databind/filter/TestSimpleSerializationIgnore.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/filter/TestSimpleSerializationIgnore.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.filter;
+package com.fasterxml.jackson.databind.ser.filter;
import java.util.*;
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/jdk/AtomicTypeSerializationTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/AtomicTypeSerializationTest.java
new file mode 100644
index 0000000..6213972
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/AtomicTypeSerializationTest.java
@@ -0,0 +1,94 @@
+package com.fasterxml.jackson.databind.ser.jdk;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+import java.util.concurrent.atomic.*;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+
+/**
+ * Unit tests for verifying serialization of simple basic non-structured
+ * types; primitives (and/or their wrappers), Strings.
+ */
+public class AtomicTypeSerializationTest
+ extends BaseMapTest
+{
+ static class UCStringWrapper {
+ @JsonSerialize(contentUsing=UpperCasingSerializer.class)
+ public AtomicReference<String> value;
+
+ public UCStringWrapper(String s) { value = new AtomicReference<String>(s); }
+ }
+
+ // [datatypes-java8#17]
+ @JsonPropertyOrder({ "date1", "date2", "date" })
+ static class ContextualOptionals
+ {
+ public AtomicReference<Date> date;
+
+ @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy+MM+dd")
+ public AtomicReference<Date> date1;
+
+ @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy*MM*dd")
+ public AtomicReference<Date> date2;
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = objectMapper();
+
+ public void testAtomicBoolean() throws Exception
+ {
+ assertEquals("true", MAPPER.writeValueAsString(new AtomicBoolean(true)));
+ assertEquals("false", MAPPER.writeValueAsString(new AtomicBoolean(false)));
+ }
+
+ public void testAtomicInteger() throws Exception
+ {
+ assertEquals("1", MAPPER.writeValueAsString(new AtomicInteger(1)));
+ assertEquals("-9", MAPPER.writeValueAsString(new AtomicInteger(-9)));
+ }
+
+ public void testAtomicLong() throws Exception
+ {
+ assertEquals("0", MAPPER.writeValueAsString(new AtomicLong(0)));
+ }
+
+ public void testAtomicReference() throws Exception
+ {
+ String[] strs = new String[] { "abc" };
+ assertEquals("[\"abc\"]", MAPPER.writeValueAsString(new AtomicReference<String[]>(strs)));
+ }
+
+ public void testCustomSerializer() throws Exception
+ {
+ final String VALUE = "fooBAR";
+ String json = MAPPER.writeValueAsString(new UCStringWrapper(VALUE));
+ assertEquals(json, aposToQuotes("{'value':'FOOBAR'}"));
+ }
+
+ public void testContextualAtomicReference() throws Exception
+ {
+ SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
+ df.setTimeZone(TimeZone.getTimeZone("UTC"));
+ final ObjectMapper mapper = objectMapper();
+ mapper.setDateFormat(df);
+ ContextualOptionals input = new ContextualOptionals();
+ input.date = new AtomicReference<>(new Date(0L));
+ input.date1 = new AtomicReference<>(new Date(0L));
+ input.date2 = new AtomicReference<>(new Date(0L));
+ final String json = mapper.writeValueAsString(input);
+ assertEquals(aposToQuotes(
+ "{'date1':'1970+01+01','date2':'1970*01*01','date':'1970/01/01'}"),
+ json);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestCollectionSerialization.java b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/CollectionSerializationTest.java
similarity index 85%
rename from src/test/java/com/fasterxml/jackson/databind/ser/TestCollectionSerialization.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/jdk/CollectionSerializationTest.java
index 59267de..1d09d74 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestCollectionSerialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/CollectionSerializationTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.ser;
+package com.fasterxml.jackson.databind.ser.jdk;
import java.io.*;
import java.util.*;
@@ -6,9 +6,10 @@
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-public class TestCollectionSerialization
+public class CollectionSerializationTest
extends BaseMapTest
{
enum Key { A, B, C };
@@ -66,6 +67,18 @@
public String[] empty = new String[0];
}
+ static class StaticListWrapper {
+ protected List<String> list;
+
+ public StaticListWrapper(String ... v) {
+ list = new ArrayList<String>(Arrays.asList(v));
+ }
+ protected StaticListWrapper() { }
+
+ public List<String> getList( ) { return list; }
+ public void setList(List<String> l) { list = l; }
+ }
+
/*
/**********************************************************
/* Test methods
@@ -227,14 +240,15 @@
assertNull(result.get("map"));
}
- // Test [JACKSON-220]
public void testListSerializer() throws IOException
{
- assertEquals("\"[ab, cd, ef]\"",
+ assertEquals(quote("[ab, cd, ef]"),
MAPPER.writeValueAsString(new PseudoList("ab", "cd", "ef")));
+ assertEquals(quote("[]"),
+ MAPPER.writeValueAsString(new PseudoList()));
}
- // [JACKSON-254]
+ @SuppressWarnings("deprecation")
public void testEmptyListOrArray() throws IOException
{
// by default, empty lists serialized normally
@@ -250,4 +264,19 @@
assertEquals("{}", m.writeValueAsString(list));
assertEquals("{}", m.writeValueAsString(array));
}
+
+ public void testStaticList() throws IOException
+ {
+ // First: au naturel
+ StaticListWrapper w = new StaticListWrapper("a", "b", "c");
+ String json = MAPPER.writeValueAsString(w);
+ assertEquals(aposToQuotes("{'list':['a','b','c']}"), json);
+
+ // but then with default typing
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.enableDefaultTyping(DefaultTyping.NON_FINAL);
+ json = mapper.writeValueAsString(w);
+ assertEquals(aposToQuotes(String.format("['%s',{'list':['%s',['a','b','c']]}]",
+ w.getClass().getName(), w.list.getClass().getName())), json);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/DateSerializationTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/DateSerializationTest.java
similarity index 86%
rename from src/test/java/com/fasterxml/jackson/databind/ser/DateSerializationTest.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/jdk/DateSerializationTest.java
index 682da99..a8a591c 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/DateSerializationTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/DateSerializationTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.ser;
+package com.fasterxml.jackson.databind.ser.jdk;
import java.io.*;
import java.text.*;
@@ -26,17 +26,6 @@
public DateAsNumberBean(long l) { date = new java.util.Date(l); }
}
- static class SqlDateAsDefaultBean {
- public java.sql.Date date;
- public SqlDateAsDefaultBean(long l) { date = new java.sql.Date(l); }
- }
-
- static class SqlDateAsNumberBean {
- @JsonFormat(shape=JsonFormat.Shape.NUMBER)
- public java.sql.Date date;
- public SqlDateAsNumberBean(long l) { date = new java.sql.Date(l); }
- }
-
static class DateAsStringBean {
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
public Date date;
@@ -129,38 +118,6 @@
assertEquals(quote("1969-12-31X16:00:00"), mapper.writeValueAsString(new Date(0L)));
}
- @SuppressWarnings("deprecation")
- public void testSqlDate() throws IOException
- {
- // use date 1999-04-01 (note: months are 0-based, use constant)
- java.sql.Date date = new java.sql.Date(99, Calendar.APRIL, 1);
- assertEquals(quote("1999-04-01"), MAPPER.writeValueAsString(date));
-
- java.sql.Date date0 = new java.sql.Date(0L);
- assertEquals(aposToQuotes("{'date':'"+date0.toString()+"'}"),
- MAPPER.writeValueAsString(new SqlDateAsDefaultBean(0L)));
-
- // but may explicitly force timestamp too
- assertEquals(aposToQuotes("{'date':0}"), MAPPER.writeValueAsString(new SqlDateAsNumberBean(0L)));
- }
-
- public void testSqlTime() throws IOException
- {
- java.sql.Time time = new java.sql.Time(0L);
- // not 100% sure what we should expect wrt timezone, but what serializes
- // does use is quite simple:
- assertEquals(quote(time.toString()), MAPPER.writeValueAsString(time));
- }
-
- public void testSqlTimestamp() throws IOException
- {
- java.sql.Timestamp input = new java.sql.Timestamp(0L);
- // just should produce same output as standard `java.util.Date`:
- Date altTnput = new Date(0L);
- assertEquals(MAPPER.writeValueAsString(altTnput),
- MAPPER.writeValueAsString(input));
- }
-
public void testTimeZone() throws IOException
{
TimeZone input = TimeZone.getTimeZone("PST");
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestJdkTypes.java b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java
similarity index 84%
rename from src/test/java/com/fasterxml/jackson/databind/ser/TestJdkTypes.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java
index 9aea068..055fdf8 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestJdkTypes.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/JDKTypeSerializationTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.ser;
+package com.fasterxml.jackson.databind.ser.jdk;
import java.io.*;
import java.math.BigDecimal;
@@ -9,6 +9,7 @@
import java.util.*;
import java.util.regex.Pattern;
+import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
@@ -16,11 +17,17 @@
* Unit tests for JDK types not covered by other tests (i.e. things
* that are not Enums, Collections, Maps, or standard Date/Time types)
*/
-public class TestJdkTypes
+public class JDKTypeSerializationTest
extends com.fasterxml.jackson.databind.BaseMapTest
{
private final ObjectMapper MAPPER = objectMapper();
+ static class InetAddressBean {
+ public InetAddress value;
+
+ public InetAddressBean(InetAddress i) { value = i; }
+ }
+
public void testBigDecimal() throws Exception
{
Map<String, Object> map = new HashMap<String, Object>();
@@ -83,19 +90,26 @@
public void testInetAddress() throws IOException
{
assertEquals(quote("127.0.0.1"), MAPPER.writeValueAsString(InetAddress.getByName("127.0.0.1")));
- assertEquals(quote("ning.com"), MAPPER.writeValueAsString(InetAddress.getByName("ning.com")));
+ InetAddress input = InetAddress.getByName("google.com");
+ assertEquals(quote("google.com"), MAPPER.writeValueAsString(input));
+
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configOverride(InetAddress.class)
+ .setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));
+ String json = mapper.writeValueAsString(input);
+ assertEquals(quote(input.getHostAddress()), json);
+
+ assertEquals(String.format("{\"value\":\"%s\"}", input.getHostAddress()),
+ mapper.writeValueAsString(new InetAddressBean(input)));
}
public void testInetSocketAddress() throws IOException
{
- assertEquals(
- quote("127.0.0.1:8080"),
+ assertEquals(quote("127.0.0.1:8080"),
MAPPER.writeValueAsString(new InetSocketAddress("127.0.0.1", 8080)));
- assertEquals(
- quote("ning.com:6667"),
- MAPPER.writeValueAsString(new InetSocketAddress("ning.com", 6667)));
- assertEquals(
- quote("[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"),
+ assertEquals(quote("google.com:6667"),
+ MAPPER.writeValueAsString(new InetSocketAddress("google.com", 6667)));
+ assertEquals(quote("[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"),
MAPPER.writeValueAsString(new InetSocketAddress("2001:db8:85a3:8d3:1319:8a2e:370:7348", 443)));
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/jdk/MapKeySerializationTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/MapKeySerializationTest.java
new file mode 100644
index 0000000..8731deb
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/MapKeySerializationTest.java
@@ -0,0 +1,103 @@
+package com.fasterxml.jackson.databind.ser.jdk;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonValue;
+import com.fasterxml.jackson.core.Base64Variants;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.*;
+
+@SuppressWarnings("serial")
+public class MapKeySerializationTest extends BaseMapTest
+{
+ // for [databind#47]
+ public static class Wat
+ {
+ private final String wat;
+
+ @JsonCreator
+ Wat(String wat) {
+ this.wat = wat;
+ }
+
+ @JsonValue
+ public String getWat() {
+ return wat;
+ }
+
+ @Override
+ public String toString() {
+ return "(String)[Wat: " + wat + "]";
+ }
+ }
+
+ static class WatMap extends HashMap<Wat,Boolean> { }
+
+ static class DefaultKeySerializer extends JsonSerializer<Object>
+ {
+ @Override
+ public void serialize(Object value, JsonGenerator g, SerializerProvider provider) throws IOException
+ {
+ g.writeFieldName("DEFAULT:"+value);
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ final private ObjectMapper MAPPER = objectMapper();
+
+ // [databind#47]
+ public void testMapJsonValueKey47() throws Exception
+ {
+ WatMap input = new WatMap();
+ input.put(new Wat("3"), true);
+
+ String json = MAPPER.writeValueAsString(input);
+ assertEquals(aposToQuotes("{'3':true}"), json);
+ }
+
+ // [databind#682]
+ public void testClassKey() throws IOException
+ {
+ Map<Class<?>,Integer> map = new LinkedHashMap<Class<?>,Integer>();
+ map.put(String.class, 2);
+ String json = MAPPER.writeValueAsString(map);
+ assertEquals(aposToQuotes("{'java.lang.String':2}"), json);
+ }
+
+ public void testDefaultKeySerializer() throws IOException
+ {
+ ObjectMapper m = new ObjectMapper();
+ m.getSerializerProvider().setDefaultKeySerializer(new DefaultKeySerializer());
+ Map<String,String> map = new HashMap<String,String>();
+ map.put("a", "b");
+ assertEquals("{\"DEFAULT:a\":\"b\"}", m.writeValueAsString(map));
+ }
+
+ // [databind#1552]
+ public void testMapsWithBinaryKeys() throws Exception
+ {
+ byte[] binary = new byte[] { 1, 2, 3, 4, 5 };
+
+ // First, using wrapper
+ MapWrapper<byte[], String> input = new MapWrapper<>(binary, "stuff");
+ String expBase64 = Base64Variants.MIME.encode(binary);
+
+ assertEquals(aposToQuotes("{'map':{'"+expBase64+"':'stuff'}}"),
+ MAPPER.writeValueAsString(input));
+
+ // and then dynamically..
+ Map<byte[],String> map = new LinkedHashMap<>();
+ map.put(binary, "xyz");
+ assertEquals(aposToQuotes("{'"+expBase64+"':'xyz'}"),
+ MAPPER.writeValueAsString(map));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/NumberSerTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/NumberSerTest.java
similarity index 67%
rename from src/test/java/com/fasterxml/jackson/databind/ser/NumberSerTest.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/jdk/NumberSerTest.java
index 7d573d6..cc0d35d 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/NumberSerTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/NumberSerTest.java
@@ -1,11 +1,13 @@
-package com.fasterxml.jackson.databind.ser;
+package com.fasterxml.jackson.databind.ser.jdk;
+import java.math.BigDecimal;
import java.math.BigInteger;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
* Unit tests for verifying serialization of simple basic non-structured
@@ -37,6 +39,14 @@
public double value = -0.5;
}
+ static class NumberWrapper {
+ // ensure it will use `Number` as statically force type, when looking for serializer
+ @JsonSerialize(as=Number.class)
+ public Number value;
+
+ public NumberWrapper(Number v) { value = v; }
+ }
+
/*
/**********************************************************
/* Test methods
@@ -87,4 +97,16 @@
assertEquals(aposToQuotes("{'i':'3'}"),
mapper.writeValueAsString(new IntWrapper(3)));
}
+
+ public void testNumberType() throws Exception
+ {
+ assertEquals(aposToQuotes("{'value':1}"), MAPPER.writeValueAsString(new NumberWrapper(Byte.valueOf((byte) 1))));
+ assertEquals(aposToQuotes("{'value':2}"), MAPPER.writeValueAsString(new NumberWrapper(Short.valueOf((short) 2))));
+ assertEquals(aposToQuotes("{'value':3}"), MAPPER.writeValueAsString(new NumberWrapper(Integer.valueOf(3))));
+ assertEquals(aposToQuotes("{'value':4}"), MAPPER.writeValueAsString(new NumberWrapper(Long.valueOf(4L))));
+ assertEquals(aposToQuotes("{'value':0.5}"), MAPPER.writeValueAsString(new NumberWrapper(Float.valueOf(0.5f))));
+ assertEquals(aposToQuotes("{'value':0.05}"), MAPPER.writeValueAsString(new NumberWrapper(Double.valueOf(0.05))));
+ assertEquals(aposToQuotes("{'value':123}"), MAPPER.writeValueAsString(new NumberWrapper(BigInteger.valueOf(123))));
+ assertEquals(aposToQuotes("{'value':0.025}"), MAPPER.writeValueAsString(new NumberWrapper(BigDecimal.valueOf(0.025))));
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/jdk/SqlDateSerializationTest.java b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/SqlDateSerializationTest.java
new file mode 100644
index 0000000..2ae4ff8
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/SqlDateSerializationTest.java
@@ -0,0 +1,97 @@
+package com.fasterxml.jackson.databind.ser.jdk;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+
+import com.fasterxml.jackson.annotation.*;
+
+import com.fasterxml.jackson.databind.*;
+
+// Tests for `java.sql.Date`, `java.sql.Time` and `java.sql.Timestamp`
+public class SqlDateSerializationTest extends BaseMapTest
+{
+ static class SqlDateAsDefaultBean {
+ public java.sql.Date date;
+ public SqlDateAsDefaultBean(long l) { date = new java.sql.Date(l); }
+ }
+
+ static class SqlDateAsNumberBean {
+ @JsonFormat(shape=JsonFormat.Shape.NUMBER)
+ public java.sql.Date date;
+ public SqlDateAsNumberBean(long l) { date = new java.sql.Date(l); }
+ }
+
+ // for [databind#1407]
+ static class Person {
+ @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy.MM.dd")
+ public java.sql.Date dateOfBirth;
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ @SuppressWarnings("deprecation")
+ public void testSqlDate() throws IOException
+ {
+ // use date 1999-04-01 (note: months are 0-based, use constant)
+ final java.sql.Date date99 = new java.sql.Date(99, Calendar.APRIL, 1);
+ final java.sql.Date date0 = new java.sql.Date(0);
+
+ // 11-Oct-2016, tatu: As per [databind#219] we really should use global
+ // defaults in 2.9, even if this changes behavior.
+
+ assertEquals(String.valueOf(date99.getTime()),
+ MAPPER.writeValueAsString(date99));
+
+ assertEquals(aposToQuotes("{'date':0}"),
+ MAPPER.writeValueAsString(new SqlDateAsDefaultBean(0L)));
+
+ // but may explicitly force timestamp too
+ assertEquals(aposToQuotes("{'date':0}"),
+ MAPPER.writeValueAsString(new SqlDateAsNumberBean(0L)));
+
+ // And also should be able to use String output as need be:
+ ObjectWriter w = MAPPER.writer().without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+
+ assertEquals(quote("1999-04-01"), w.writeValueAsString(date99));
+ assertEquals(quote(date0.toString()), w.writeValueAsString(date0));
+ assertEquals(aposToQuotes("{'date':'"+date0.toString()+"'}"),
+ w.writeValueAsString(new SqlDateAsDefaultBean(0L)));
+ }
+
+ public void testSqlTime() throws IOException
+ {
+ java.sql.Time time = new java.sql.Time(0L);
+ // not 100% sure what we should expect wrt timezone, but what serializes
+ // does use is quite simple:
+ assertEquals(quote(time.toString()), MAPPER.writeValueAsString(time));
+ }
+
+ public void testSqlTimestamp() throws IOException
+ {
+ java.sql.Timestamp input = new java.sql.Timestamp(0L);
+ // just should produce same output as standard `java.util.Date`:
+ Date altTnput = new Date(0L);
+ assertEquals(MAPPER.writeValueAsString(altTnput),
+ MAPPER.writeValueAsString(input));
+ }
+
+ public void testPatternWithSqlDate() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ // `java.sql.Date` applies system default zone (and not UTC)
+ mapper.setTimeZone(TimeZone.getDefault());
+
+ Person i = new Person();
+ i.dateOfBirth = java.sql.Date.valueOf("1980-04-14");
+ assertEquals(aposToQuotes("{'dateOfBirth':'1980.04.14'}"),
+ mapper.writeValueAsString(i));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestUntypedSerialization.java b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/UntypedSerializationTest.java
similarity index 97%
rename from src/test/java/com/fasterxml/jackson/databind/ser/TestUntypedSerialization.java
rename to src/test/java/com/fasterxml/jackson/databind/ser/jdk/UntypedSerializationTest.java
index e62ed96..9117e79 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestUntypedSerialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/ser/jdk/UntypedSerializationTest.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.ser;
+package com.fasterxml.jackson.databind.ser.jdk;
import java.util.*;
@@ -11,7 +11,7 @@
* "Native" java type mapper; basically that is can properly serialize
* core JDK objects to JSON.
*/
-public class TestUntypedSerialization
+public class UntypedSerializationTest
extends BaseMapTest
{
public void testFromArray()
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/EmptyArrayAsNullTest.java b/src/test/java/com/fasterxml/jackson/databind/struct/EmptyArrayAsNullTest.java
index 5199545..71aba32 100644
--- a/src/test/java/com/fasterxml/jackson/databind/struct/EmptyArrayAsNullTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/EmptyArrayAsNullTest.java
@@ -2,11 +2,8 @@
import java.math.BigDecimal;
import java.math.BigInteger;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.EnumMap;
-import java.util.Map;
-import java.util.UUID;
+
+import java.util.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
@@ -105,14 +102,14 @@
public void testWrapperFromEmptyArray() throws Exception
{
-// _testNullWrapper(Boolean.class);
-// _testNullWrapper(Byte.class);
+ _testNullWrapper(Boolean.class);
+ _testNullWrapper(Byte.class);
_testNullWrapper(Character.class);
-// _testNullWrapper(Short.class);
-// _testNullWrapper(Integer.class);
-// _testNullWrapper(Long.class);
-// _testNullWrapper(Float.class);
-// _testNullWrapper(Double.class);
+ _testNullWrapper(Short.class);
+ _testNullWrapper(Integer.class);
+ _testNullWrapper(Long.class);
+ _testNullWrapper(Float.class);
+ _testNullWrapper(Double.class);
}
/*
@@ -136,10 +133,8 @@
_testNullWrapper(UUID.class);
- /*
_testNullWrapper(Date.class);
_testNullWrapper(Calendar.class);
- */
}
/*
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeatureAcceptSingleTest.java b/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeatureAcceptSingleTest.java
new file mode 100644
index 0000000..3aca91e
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeatureAcceptSingleTest.java
@@ -0,0 +1,179 @@
+package com.fasterxml.jackson.databind.struct;
+
+import java.util.EnumSet;
+import java.util.List;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class FormatFeatureAcceptSingleTest extends BaseMapTest
+{
+ static class StringArrayNotAnnoted {
+ public String[] values;
+
+ protected StringArrayNotAnnoted() { }
+ public StringArrayNotAnnoted(String ... v) { values = v; }
+ }
+
+ static class StringArrayWrapper {
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ public String[] values;
+ }
+
+ static class BooleanArrayWrapper {
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ public boolean[] values;
+ }
+
+ static class IntArrayWrapper {
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ public int[] values;
+ }
+
+ static class LongArrayWrapper {
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ public long[] values;
+ }
+
+ static class FloatArrayWrapper {
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ public float[] values;
+ }
+
+ static class DoubleArrayWrapper {
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ public double[] values;
+ }
+
+ static class StringListWrapper {
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ public List<String> values;
+ }
+
+ static class EnumSetWrapper {
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ public EnumSet<ABC> values;
+ }
+
+ static class RolesInArray {
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ public Role[] roles;
+ }
+
+ static class RolesInList {
+ @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
+ public List<Role> roles;
+ }
+
+ static class Role {
+ public String ID;
+ public String Name;
+ }
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ /*
+ /**********************************************************
+ /* Test methods, reading with single-element unwrapping
+ /**********************************************************
+ */
+
+ public void testSingleStringArrayRead() throws Exception {
+ String json = aposToQuotes(
+ "{ 'values': 'first' }");
+ StringArrayWrapper result = MAPPER.readValue(json, StringArrayWrapper.class);
+ assertNotNull(result.values);
+ assertEquals(1, result.values.length);
+ assertEquals("first", result.values[0]);
+
+ // and then without annotation, but with global override
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configOverride(String[].class).setFormat(JsonFormat.Value.empty()
+ .withFeature(JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY));
+ StringArrayNotAnnoted result2 = mapper.readValue(json, StringArrayNotAnnoted.class);
+ assertNotNull(result2.values);
+ assertEquals(1, result2.values.length);
+ assertEquals("first", result2.values[0]);
+ }
+
+ public void testSingleIntArrayRead() throws Exception {
+ String json = aposToQuotes(
+ "{ 'values': 123 }");
+ IntArrayWrapper result = MAPPER.readValue(json, IntArrayWrapper.class);
+ assertNotNull(result.values);
+ assertEquals(1, result.values.length);
+ assertEquals(123, result.values[0]);
+ }
+
+ public void testSingleLongArrayRead() throws Exception {
+ String json = aposToQuotes(
+ "{ 'values': -205 }");
+ LongArrayWrapper result = MAPPER.readValue(json, LongArrayWrapper.class);
+ assertNotNull(result.values);
+ assertEquals(1, result.values.length);
+ assertEquals(-205L, result.values[0]);
+ }
+
+ public void testSingleBooleanArrayRead() throws Exception {
+ String json = aposToQuotes(
+ "{ 'values': true }");
+ BooleanArrayWrapper result = MAPPER.readValue(json, BooleanArrayWrapper.class);
+ assertNotNull(result.values);
+ assertEquals(1, result.values.length);
+ assertEquals(true, result.values[0]);
+ }
+
+ public void testSingleDoubleArrayRead() throws Exception {
+ String json = aposToQuotes(
+ "{ 'values': -0.5 }");
+ DoubleArrayWrapper result = MAPPER.readValue(json, DoubleArrayWrapper.class);
+ assertNotNull(result.values);
+ assertEquals(1, result.values.length);
+ assertEquals(-0.5, result.values[0]);
+ }
+
+ public void testSingleFloatArrayRead() throws Exception {
+ String json = aposToQuotes(
+ "{ 'values': 0.25 }");
+ FloatArrayWrapper result = MAPPER.readValue(json, FloatArrayWrapper.class);
+ assertNotNull(result.values);
+ assertEquals(1, result.values.length);
+ assertEquals(0.25f, result.values[0]);
+ }
+
+ public void testSingleElementArrayRead() throws Exception {
+ String json = aposToQuotes(
+ "{ 'roles': { 'Name': 'User', 'ID': '333' } }");
+ RolesInArray response = MAPPER.readValue(json, RolesInArray.class);
+ assertNotNull(response.roles);
+ assertEquals(1, response.roles.length);
+ assertEquals("333", response.roles[0].ID);
+ }
+
+ public void testSingleStringListRead() throws Exception {
+ String json = aposToQuotes(
+ "{ 'values': 'first' }");
+ StringListWrapper result = MAPPER.readValue(json, StringListWrapper.class);
+ assertNotNull(result.values);
+ assertEquals(1, result.values.size());
+ assertEquals("first", result.values.get(0));
+ }
+
+ public void testSingleElementListRead() throws Exception {
+ String json = aposToQuotes(
+ "{ 'roles': { 'Name': 'User', 'ID': '333' } }");
+ RolesInList response = MAPPER.readValue(json, RolesInList.class);
+ assertNotNull(response.roles);
+ assertEquals(1, response.roles.size());
+ assertEquals("333", response.roles.get(0).ID);
+ }
+
+ public void testSingleEnumSetRead() throws Exception {
+ EnumSetWrapper result = MAPPER.readValue(aposToQuotes("{ 'values': 'B' }"),
+ EnumSetWrapper.class);
+ assertNotNull(result.values);
+ assertEquals(1, result.values.size());
+ assertEquals(ABC.B, result.values.iterator().next());
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeatureUnwrapSingleTest.java b/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeatureUnwrapSingleTest.java
new file mode 100644
index 0000000..ab8b38c
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeatureUnwrapSingleTest.java
@@ -0,0 +1,192 @@
+package com.fasterxml.jackson.databind.struct;
+
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+
+public class FormatFeatureUnwrapSingleTest extends BaseMapTest
+{
+ static class StringArrayNotAnnoted {
+ public String[] values;
+
+ protected StringArrayNotAnnoted() { }
+ public StringArrayNotAnnoted(String ... v) { values = v; }
+ }
+
+ @JsonPropertyOrder( { "strings", "ints", "bools" })
+ static class WrapWriteWithArrays
+ {
+ @JsonProperty("strings")
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public String[] _strings = new String[] {
+ "a"
+ };
+
+ @JsonFormat(without={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public int[] ints = new int[] { 1 };
+
+ public boolean[] bools = new boolean[] { true };
+ }
+
+ static class UnwrapShortArray {
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public short[] v = { (short) 7 };
+ }
+
+ static class UnwrapIntArray {
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public int[] v = { 3 };
+ }
+
+ static class UnwrapLongArray {
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public long[] v = { 1L };
+ }
+
+ static class UnwrapBooleanArray {
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public boolean[] v = { true };
+ }
+
+ static class UnwrapFloatArray {
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public float[] v = { 0.5f };
+ }
+
+ static class UnwrapDoubleArray {
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public double[] v = { 0.25 };
+ }
+
+ static class UnwrapIterable {
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ @JsonSerialize(as=Iterable.class)
+ public Iterable<String> v;
+
+ public UnwrapIterable() {
+ v = Collections.singletonList("foo");
+ }
+
+ public UnwrapIterable(String... values) {
+ v = Arrays.asList(values);
+ }
+ }
+
+ static class UnwrapCollection {
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ @JsonSerialize(as=Collection.class)
+ public Collection<String> v;
+
+ public UnwrapCollection() {
+ v = Collections.singletonList("foo");
+ }
+
+ public UnwrapCollection(String... values) {
+ v = new LinkedHashSet<String>(Arrays.asList(values));
+ }
+ }
+
+ static class UnwrapStringLike {
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public URI[] v = { URI.create("http://foo") };
+ }
+
+ @JsonPropertyOrder( { "strings", "ints", "bools", "enums" })
+ static class WrapWriteWithCollections
+ {
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public List<String> strings = Arrays.asList("a");
+
+ @JsonFormat(without={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public Collection<Integer> ints = Arrays.asList(Integer.valueOf(1));
+
+ public Set<Boolean> bools = Collections.singleton(true);
+
+ @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
+ public EnumSet<ABC> enums = EnumSet.of(ABC.B);
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods, writing with single-element unwrapping
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testWithArrayTypes() throws Exception
+ {
+ // default: strings unwrapped, ints wrapped
+ assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':[true]}"),
+ MAPPER.writeValueAsString(new WrapWriteWithArrays()));
+
+ // change global default to "yes, unwrap"; changes 'bools' only
+ assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':true}"),
+ MAPPER.writer().with(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
+ .writeValueAsString(new WrapWriteWithArrays()));
+
+ // change global default to "no, don't, unwrap", same as first case
+ assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':[true]}"),
+ MAPPER.writer().without(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
+ .writeValueAsString(new WrapWriteWithArrays()));
+
+ // And then without SerializationFeature but with config override:
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configOverride(String[].class).setFormat(JsonFormat.Value.empty()
+ .withFeature(JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED));
+ assertEquals(aposToQuotes("{'values':'a'}"),
+ mapper.writeValueAsString(new StringArrayNotAnnoted("a")));
+ }
+
+ public void testWithCollectionTypes() throws Exception
+ {
+ // default: strings unwrapped, ints wrapped
+ assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':[true],'enums':'B'}"),
+ MAPPER.writeValueAsString(new WrapWriteWithCollections()));
+
+ // change global default to "yes, unwrap"; changes 'bools' only
+ assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':true,'enums':'B'}"),
+ MAPPER.writer().with(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
+ .writeValueAsString(new WrapWriteWithCollections()));
+
+ // change global default to "no, don't, unwrap", same as first case
+ assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':[true],'enums':'B'}"),
+ MAPPER.writer().without(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
+ .writeValueAsString(new WrapWriteWithCollections()));
+ }
+
+ public void testUnwrapWithPrimitiveArraysEtc() throws Exception {
+ assertEquals("{\"v\":7}", MAPPER.writeValueAsString(new UnwrapShortArray()));
+ assertEquals("{\"v\":3}", MAPPER.writeValueAsString(new UnwrapIntArray()));
+ assertEquals("{\"v\":1}", MAPPER.writeValueAsString(new UnwrapLongArray()));
+ assertEquals("{\"v\":true}", MAPPER.writeValueAsString(new UnwrapBooleanArray()));
+
+ assertEquals("{\"v\":0.5}", MAPPER.writeValueAsString(new UnwrapFloatArray()));
+ assertEquals("{\"v\":0.25}", MAPPER.writeValueAsString(new UnwrapDoubleArray()));
+ assertEquals("0.5",
+ MAPPER.writer().with(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
+ .writeValueAsString(new double[] { 0.5 }));
+
+ assertEquals("{\"v\":\"foo\"}", MAPPER.writeValueAsString(new UnwrapIterable()));
+ assertEquals("{\"v\":\"x\"}", MAPPER.writeValueAsString(new UnwrapIterable("x")));
+ assertEquals("{\"v\":[\"x\",null]}", MAPPER.writeValueAsString(new UnwrapIterable("x", null)));
+
+ assertEquals("{\"v\":\"foo\"}", MAPPER.writeValueAsString(new UnwrapCollection()));
+ assertEquals("{\"v\":\"x\"}", MAPPER.writeValueAsString(new UnwrapCollection("x")));
+ assertEquals("{\"v\":[\"x\",null]}", MAPPER.writeValueAsString(new UnwrapCollection("x", null)));
+
+ assertEquals("{\"v\":\"http://foo\"}", MAPPER.writeValueAsString(new UnwrapStringLike()));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeaturesMiscTest.java b/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeaturesMiscTest.java
new file mode 100644
index 0000000..6ad26f5
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeaturesMiscTest.java
@@ -0,0 +1,57 @@
+package com.fasterxml.jackson.databind.struct;
+
+import java.util.*;
+
+import com.fasterxml.jackson.annotation.*;
+import com.fasterxml.jackson.databind.*;
+
+/**
+ * Tests for {@link JsonFormat} and specifically <code>JsonFormat.Feature</code>s.
+ */
+public class FormatFeaturesMiscTest extends BaseMapTest
+{
+ static class Role {
+ public String ID;
+ public String Name;
+ }
+
+ static class CaseInsensitiveRoleWrapper
+ {
+ @JsonFormat(with={ JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES })
+ public Role role;
+ }
+
+ static class SortedKeysMap {
+ @JsonFormat(with = JsonFormat.Feature.WRITE_SORTED_MAP_ENTRIES)
+ public Map<String,Integer> values = new LinkedHashMap<>();
+
+ protected SortedKeysMap() { }
+
+ public SortedKeysMap put(String key, int value) {
+ values.put(key, value);
+ return this;
+ }
+ }
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+
+ // [databind#1232]: allow per-property case-insensitivity
+ public void testCaseInsensitive() throws Exception {
+ CaseInsensitiveRoleWrapper w = MAPPER.readValue
+ (aposToQuotes("{'role':{'id':'12','name':'Foo'}}"),
+ CaseInsensitiveRoleWrapper.class);
+ assertNotNull(w);
+ assertEquals("12", w.role.ID);
+ assertEquals("Foo", w.role.Name);
+ }
+
+ // [databind#1232]: allow forcing sorting on Map keys
+ public void testOrderedMaps() throws Exception {
+ SortedKeysMap map = new SortedKeysMap()
+ .put("b", 2)
+ .put("a", 1);
+ assertEquals(aposToQuotes("{'values':{'a':1,'b':2}}"),
+ MAPPER.writeValueAsString(map));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeaturesTest.java b/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeaturesTest.java
deleted file mode 100644
index 87d5922..0000000
--- a/src/test/java/com/fasterxml/jackson/databind/struct/FormatFeaturesTest.java
+++ /dev/null
@@ -1,254 +0,0 @@
-package com.fasterxml.jackson.databind.struct;
-
-import java.util.*;
-
-import com.fasterxml.jackson.annotation.*;
-import com.fasterxml.jackson.databind.*;
-
-/**
- * Tests for {@link JsonFormat} and specifically <code>JsonFormat.Feature</code>s.
- */
-public class FormatFeaturesTest extends BaseMapTest
-{
- @JsonPropertyOrder( { "strings", "ints", "bools" })
- static class WrapWriteWithArrays
- {
- @JsonProperty("strings")
- @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
- public String[] _strings = new String[] {
- "a"
- };
-
- @JsonFormat(without={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
- public int[] ints = new int[] {
- 1
- };
-
- public boolean[] bools = new boolean[] { true };
- }
-
- @JsonPropertyOrder( { "strings", "ints", "bools", "enums" })
- static class WrapWriteWithCollections
- {
- @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
- public List<String> strings = Arrays.asList("a");
-
- @JsonFormat(without={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
- public Collection<Integer> ints = Arrays.asList(Integer.valueOf(1));
-
- public Set<Boolean> bools = Collections.singleton(true);
-
- @JsonFormat(with={ JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED })
- public EnumSet<ABC> enums = EnumSet.of(ABC.B);
- }
-
- static class StringArrayWrapper {
- @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
- public String[] values;
- }
-
- static class StringArrayNotAnnoted {
- public String[] values;
-
- protected StringArrayNotAnnoted() { }
- public StringArrayNotAnnoted(String ... v) { values = v; }
- }
-
- static class IntArrayWrapper {
- @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
- public int[] values;
- }
-
- static class LongArrayWrapper {
- @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
- public long[] values;
- }
-
- static class StringListWrapper {
- @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
- public List<String> values;
- }
-
- static class EnumSetWrapper {
- @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
- public EnumSet<ABC> values;
- }
-
- static class RolesInArray {
- @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
- public Role[] roles;
- }
-
- static class RolesInList {
- @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
- public List<Role> roles;
- }
-
- static class Role {
- public String ID;
- public String Name;
- }
-
- static class CaseInsensitiveRoleWrapper
- {
- @JsonFormat(with={ JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES })
- public Role role;
- }
-
- static class SortedKeysMap {
- @JsonFormat(with = JsonFormat.Feature.WRITE_SORTED_MAP_ENTRIES)
- public Map<String,Integer> values = new LinkedHashMap<>();
-
- protected SortedKeysMap() { }
-
- public SortedKeysMap put(String key, int value) {
- values.put(key, value);
- return this;
- }
- }
-
- /*
- /**********************************************************
- /* Test methods, writing with single-element unwrapping
- /**********************************************************
- */
-
- private final ObjectMapper MAPPER = new ObjectMapper();
-
- public void testWithArrayTypes() throws Exception
- {
- // default: strings unwrapped, ints wrapped
- assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':[true]}"),
- MAPPER.writeValueAsString(new WrapWriteWithArrays()));
-
- // change global default to "yes, unwrap"; changes 'bools' only
- assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':true}"),
- MAPPER.writer().with(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
- .writeValueAsString(new WrapWriteWithArrays()));
-
- // change global default to "no, don't, unwrap", same as first case
- assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':[true]}"),
- MAPPER.writer().without(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
- .writeValueAsString(new WrapWriteWithArrays()));
-
- // And then without SerializationFeature but with config override:
- ObjectMapper mapper = new ObjectMapper();
- mapper.configOverride(String[].class).setFormat(JsonFormat.Value.empty()
- .withFeature(JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED));
- assertEquals(aposToQuotes("{'values':'a'}"),
- mapper.writeValueAsString(new StringArrayNotAnnoted("a")));
- }
-
- public void testWithCollectionTypes() throws Exception
- {
- // default: strings unwrapped, ints wrapped
- assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':[true],'enums':'B'}"),
- MAPPER.writeValueAsString(new WrapWriteWithCollections()));
-
- // change global default to "yes, unwrap"; changes 'bools' only
- assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':true,'enums':'B'}"),
- MAPPER.writer().with(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
- .writeValueAsString(new WrapWriteWithCollections()));
-
- // change global default to "no, don't, unwrap", same as first case
- assertEquals(aposToQuotes("{'strings':'a','ints':[1],'bools':[true],'enums':'B'}"),
- MAPPER.writer().without(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
- .writeValueAsString(new WrapWriteWithCollections()));
- }
-
- /*
- /**********************************************************
- /* Test methods, reading with single-element unwrapping
- /**********************************************************
- */
-
- public void testSingleStringArrayRead() throws Exception {
- String json = aposToQuotes(
- "{ 'values': 'first' }");
- StringArrayWrapper result = MAPPER.readValue(json, StringArrayWrapper.class);
- assertNotNull(result.values);
- assertEquals(1, result.values.length);
- assertEquals("first", result.values[0]);
-
- // and then without annotation, but with global override
- ObjectMapper mapper = new ObjectMapper();
- mapper.configOverride(String[].class).setFormat(JsonFormat.Value.empty()
- .withFeature(JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY));
- StringArrayNotAnnoted result2 = mapper.readValue(json, StringArrayNotAnnoted.class);
- assertNotNull(result2.values);
- assertEquals(1, result2.values.length);
- assertEquals("first", result2.values[0]);
- }
-
- public void testSingleIntArrayRead() throws Exception {
- String json = aposToQuotes(
- "{ 'values': 123 }");
- IntArrayWrapper result = MAPPER.readValue(json, IntArrayWrapper.class);
- assertNotNull(result.values);
- assertEquals(1, result.values.length);
- assertEquals(123, result.values[0]);
- }
-
- public void testSingleLongArrayRead() throws Exception {
- String json = aposToQuotes(
- "{ 'values': -205 }");
- LongArrayWrapper result = MAPPER.readValue(json, LongArrayWrapper.class);
- assertNotNull(result.values);
- assertEquals(1, result.values.length);
- assertEquals(-205L, result.values[0]);
- }
-
- public void testSingleElementArrayRead() throws Exception {
- String json = aposToQuotes(
- "{ 'roles': { 'Name': 'User', 'ID': '333' } }");
- RolesInArray response = MAPPER.readValue(json, RolesInArray.class);
- assertNotNull(response.roles);
- assertEquals(1, response.roles.length);
- assertEquals("333", response.roles[0].ID);
- }
-
- public void testSingleStringListRead() throws Exception {
- String json = aposToQuotes(
- "{ 'values': 'first' }");
- StringListWrapper result = MAPPER.readValue(json, StringListWrapper.class);
- assertNotNull(result.values);
- assertEquals(1, result.values.size());
- assertEquals("first", result.values.get(0));
- }
-
- public void testSingleElementListRead() throws Exception {
- String json = aposToQuotes(
- "{ 'roles': { 'Name': 'User', 'ID': '333' } }");
- RolesInList response = MAPPER.readValue(json, RolesInList.class);
- assertNotNull(response.roles);
- assertEquals(1, response.roles.size());
- assertEquals("333", response.roles.get(0).ID);
- }
-
- public void testSingleEnumSetRead() throws Exception {
- EnumSetWrapper result = MAPPER.readValue(aposToQuotes("{ 'values': 'B' }"),
- EnumSetWrapper.class);
- assertNotNull(result.values);
- assertEquals(1, result.values.size());
- assertEquals(ABC.B, result.values.iterator().next());
- }
-
- // [databind#1232]: allow per-property case-insensitivity
- public void testCaseInsensitive() throws Exception {
- CaseInsensitiveRoleWrapper w = MAPPER.readValue
- (aposToQuotes("{'role':{'id':'12','name':'Foo'}}"),
- CaseInsensitiveRoleWrapper.class);
- assertNotNull(w);
- assertEquals("12", w.role.ID);
- assertEquals("Foo", w.role.Name);
- }
-
- // [databind#1232]: allow forcing sorting on Map keys
- public void testOrderedMaps() throws Exception {
- SortedKeysMap map = new SortedKeysMap()
- .put("b", 2)
- .put("a", 1);
- assertEquals(aposToQuotes("{'values':{'a':1,'b':2}}"),
- MAPPER.writeValueAsString(map));
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/ScalarCoercionTest.java b/src/test/java/com/fasterxml/jackson/databind/struct/ScalarCoercionTest.java
new file mode 100644
index 0000000..57f71b3
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/ScalarCoercionTest.java
@@ -0,0 +1,195 @@
+package com.fasterxml.jackson.databind.struct;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+
+// for [databind#1106]
+public class ScalarCoercionTest extends BaseMapTest
+{
+ private final ObjectMapper COERCING_MAPPER = new ObjectMapper(); {
+ COERCING_MAPPER.enable(MapperFeature.ALLOW_COERCION_OF_SCALARS);
+ }
+
+ private final ObjectMapper NOT_COERCING_MAPPER = new ObjectMapper(); {
+ NOT_COERCING_MAPPER.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS);
+ }
+
+ /*
+ /**********************************************************
+ /* Unit tests: coercion from empty String
+ /**********************************************************
+ */
+
+ public void testNullValueFromEmpty() throws Exception
+ {
+ // wrappers accept `null` fine
+ _verifyNullOkFromEmpty(Boolean.class, null);
+ // but primitives require non-null
+ _verifyNullOkFromEmpty(Boolean.TYPE, Boolean.FALSE);
+
+ _verifyNullOkFromEmpty(Byte.class, null);
+ _verifyNullOkFromEmpty(Byte.TYPE, Byte.valueOf((byte) 0));
+ _verifyNullOkFromEmpty(Short.class, null);
+ _verifyNullOkFromEmpty(Short.TYPE, Short.valueOf((short) 0));
+ _verifyNullOkFromEmpty(Character.class, null);
+ _verifyNullOkFromEmpty(Character.TYPE, Character.valueOf((char) 0));
+ _verifyNullOkFromEmpty(Integer.class, null);
+ _verifyNullOkFromEmpty(Integer.TYPE, Integer.valueOf(0));
+ _verifyNullOkFromEmpty(Long.class, null);
+ _verifyNullOkFromEmpty(Long.TYPE, Long.valueOf(0L));
+ _verifyNullOkFromEmpty(Float.class, null);
+ _verifyNullOkFromEmpty(Float.TYPE, Float.valueOf(0.0f));
+ _verifyNullOkFromEmpty(Double.class, null);
+ _verifyNullOkFromEmpty(Double.TYPE, Double.valueOf(0.0));
+
+ _verifyNullOkFromEmpty(BigInteger.class, null);
+ _verifyNullOkFromEmpty(BigDecimal.class, null);
+ }
+
+ private void _verifyNullOkFromEmpty(Class<?> type, Object exp) throws IOException
+ {
+ Object result = COERCING_MAPPER.readerFor(type)
+ .with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
+ .readValue("\"\"");
+ if (exp == null) {
+ assertNull(result);
+ } else {
+ assertEquals(exp, result);
+ }
+ }
+
+ public void testNullFailFromEmpty() throws Exception
+ {
+ _verifyNullFail(Boolean.class);
+ _verifyNullFail(Boolean.TYPE);
+
+ _verifyNullFail(Byte.class);
+ _verifyNullFail(Byte.TYPE);
+ _verifyNullFail(Short.class);
+ _verifyNullFail(Short.TYPE);
+ _verifyNullFail(Character.class);
+ _verifyNullFail(Character.TYPE);
+ _verifyNullFail(Integer.class);
+ _verifyNullFail(Integer.TYPE);
+ _verifyNullFail(Long.class);
+ _verifyNullFail(Long.TYPE);
+ _verifyNullFail(Float.class);
+ _verifyNullFail(Float.TYPE);
+ _verifyNullFail(Double.class);
+ _verifyNullFail(Double.TYPE);
+
+ _verifyNullFail(BigInteger.class);
+ _verifyNullFail(BigDecimal.class);
+ }
+
+ private void _verifyNullFail(Class<?> type) throws IOException
+ {
+ try {
+ NOT_COERCING_MAPPER.readerFor(type).readValue("\"\"");
+ fail("Should have failed for "+type);
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Can not coerce empty String");
+ verifyException(e, "Null value for");
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Unit tests: coercion from secondary representations
+ /**********************************************************
+ */
+
+ public void testStringCoercionOk() throws Exception
+ {
+ // first successful coercions. Boolean has a ton...
+ _verifyCoerceSuccess("1", Boolean.TYPE, Boolean.TRUE);
+ _verifyCoerceSuccess("1", Boolean.class, Boolean.TRUE);
+ _verifyCoerceSuccess(quote("true"), Boolean.TYPE, Boolean.TRUE);
+ _verifyCoerceSuccess(quote("true"), Boolean.class, Boolean.TRUE);
+ _verifyCoerceSuccess(quote("True"), Boolean.TYPE, Boolean.TRUE);
+ _verifyCoerceSuccess(quote("True"), Boolean.class, Boolean.TRUE);
+ _verifyCoerceSuccess("0", Boolean.TYPE, Boolean.FALSE);
+ _verifyCoerceSuccess("0", Boolean.class, Boolean.FALSE);
+ _verifyCoerceSuccess(quote("false"), Boolean.TYPE, Boolean.FALSE);
+ _verifyCoerceSuccess(quote("false"), Boolean.class, Boolean.FALSE);
+ _verifyCoerceSuccess(quote("False"), Boolean.TYPE, Boolean.FALSE);
+ _verifyCoerceSuccess(quote("False"), Boolean.class, Boolean.FALSE);
+
+ _verifyCoerceSuccess(quote("123"), Byte.TYPE, Byte.valueOf((byte) 123));
+ _verifyCoerceSuccess(quote("123"), Byte.class, Byte.valueOf((byte) 123));
+ _verifyCoerceSuccess(quote("123"), Short.TYPE, Short.valueOf((short) 123));
+ _verifyCoerceSuccess(quote("123"), Short.class, Short.valueOf((short) 123));
+ _verifyCoerceSuccess(quote("123"), Integer.TYPE, Integer.valueOf(123));
+ _verifyCoerceSuccess(quote("123"), Integer.class, Integer.valueOf(123));
+ _verifyCoerceSuccess(quote("123"), Long.TYPE, Long.valueOf(123));
+ _verifyCoerceSuccess(quote("123"), Long.class, Long.valueOf(123));
+ _verifyCoerceSuccess(quote("123.5"), Float.TYPE, Float.valueOf(123.5f));
+ _verifyCoerceSuccess(quote("123.5"), Float.class, Float.valueOf(123.5f));
+ _verifyCoerceSuccess(quote("123.5"), Double.TYPE, Double.valueOf(123.5));
+ _verifyCoerceSuccess(quote("123.5"), Double.class, Double.valueOf(123.5));
+
+ _verifyCoerceSuccess(quote("123"), BigInteger.class, BigInteger.valueOf(123));
+ _verifyCoerceSuccess(quote("123.0"), BigDecimal.class, new BigDecimal("123.0"));
+ }
+
+ public void testStringCoercionFail() throws Exception
+ {
+ _verifyCoerceFail(quote("true"), Boolean.TYPE);
+ _verifyCoerceFail(quote("true"), Boolean.class);
+ _verifyCoerceFail(quote("123"), Byte.TYPE);
+ _verifyCoerceFail(quote("123"), Byte.class);
+ _verifyCoerceFail(quote("123"), Short.TYPE);
+ _verifyCoerceFail(quote("123"), Short.class);
+ _verifyCoerceFail(quote("123"), Integer.TYPE);
+ _verifyCoerceFail(quote("123"), Integer.class);
+ _verifyCoerceFail(quote("123"), Long.TYPE);
+ _verifyCoerceFail(quote("123"), Long.class);
+ _verifyCoerceFail(quote("123.5"), Float.TYPE);
+ _verifyCoerceFail(quote("123.5"), Float.class);
+ _verifyCoerceFail(quote("123.5"), Double.TYPE);
+ _verifyCoerceFail(quote("123.5"), Double.class);
+
+ _verifyCoerceFail(quote("123"), BigInteger.class);
+ _verifyCoerceFail(quote("123.0"), BigDecimal.class);
+ }
+
+ public void testMiscCoercionFail() throws Exception
+ {
+ // And then we have coercions from more esoteric types too
+ _verifyCoerceFail("1", Boolean.TYPE);
+ _verifyCoerceFail("1", Boolean.class);
+
+ _verifyCoerceFail("65", Character.class);
+ _verifyCoerceFail("65", Character.TYPE);
+ }
+
+ /*
+ /**********************************************************
+ /* Helper methods
+ /**********************************************************
+ */
+
+ private void _verifyCoerceSuccess(String input, Class<?> type, Object exp) throws IOException
+ {
+ Object result = COERCING_MAPPER.readerFor(type)
+ .readValue(input);
+ assertEquals(exp, result);
+ }
+
+ private void _verifyCoerceFail(String input, Class<?> type) throws IOException
+ {
+ try {
+ NOT_COERCING_MAPPER.readerFor(type)
+ .readValue(input);
+ fail("Should not have allowed coercion");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Can not coerce ");
+ verifyException(e, " for type `");
+ verifyException(e, "enable `MapperFeature.ALLOW_COERCION_OF_SCALARS` to allow");
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/SingleValueAsArrayTest.java b/src/test/java/com/fasterxml/jackson/databind/struct/SingleValueAsArrayTest.java
index 1903b2e..f0f9354 100644
--- a/src/test/java/com/fasterxml/jackson/databind/struct/SingleValueAsArrayTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/SingleValueAsArrayTest.java
@@ -71,16 +71,33 @@
public void testSuccessfulDeserializationOfObjectWithChainedArrayCreators() throws IOException
{
- MAPPER.readValue(JSON, Bean1421A.class);
+ Bean1421A result = MAPPER.readValue(JSON, Bean1421A.class);
+ assertNotNull(result);
}
public void testWithSingleString() throws Exception {
- ObjectMapper objectMapper = new ObjectMapper();
- objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
- Bean1421B<List<String>> a = objectMapper.readValue(quote("test2"),
+ Bean1421B<List<String>> a = MAPPER.readValue(quote("test2"),
new TypeReference<Bean1421B<List<String>>>() {});
List<String> expected = new ArrayList<>();
expected.add("test2");
assertEquals(expected, a.value);
}
+
+ public void testPrimitives() throws Exception {
+ int[] i = MAPPER.readValue("16", int[].class);
+ assertEquals(1, i.length);
+ assertEquals(16, i[0]);
+
+ long[] l = MAPPER.readValue("1234", long[].class);
+ assertEquals(1, l.length);
+ assertEquals(1234L, l[0]);
+
+ double[] d = MAPPER.readValue("12.5", double[].class);
+ assertEquals(1, d.length);
+ assertEquals(12.5, d[0]);
+
+ boolean[] b = MAPPER.readValue("true", boolean[].class);
+ assertEquals(1, d.length);
+ assertEquals(true, b[0]);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArray.java b/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArray.java
index 7e8f091..391cd3c 100644
--- a/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArray.java
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArray.java
@@ -7,6 +7,7 @@
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
@@ -191,8 +192,7 @@
/* Compatibility with "single-elem as array" feature
/*****************************************************
*/
-
- // for [JACKSON-805]
+
public void testSerializeAsArrayWithSingleProperty() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
@@ -262,4 +262,32 @@
assertNotNull(result);
assertEquals(3, result.y);
}
+
+ /*
+ /*****************************************************
+ /* Failure tests
+ /*****************************************************
+ */
+
+ public void testUnknownExtraProp() throws Exception
+ {
+ String json = "{\"value\":[true,\"Foobar\",42,13, false]}";
+ try {
+ MAPPER.readValue(json, PojoAsArrayWrapper.class);
+ fail("should not pass with extra element");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Unexpected JSON values");
+ }
+
+ // but actually fine if skip-unknown set
+ PojoAsArrayWrapper v = MAPPER.readerFor(PojoAsArrayWrapper.class)
+ .without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
+ .readValue(json);
+ assertNotNull(v);
+ // note: +1 for both so
+ assertEquals(v.value.x, 42);
+ assertEquals(v.value.y, 13);
+ assertTrue(v.value.complete);
+ assertEquals("Foobar", v.value.name);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArrayAdvanced.java b/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArrayAdvanced.java
index 989669c..d2e6ddd 100644
--- a/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArrayAdvanced.java
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArrayAdvanced.java
@@ -56,6 +56,27 @@
public int c;
}
+ @JsonFormat(shape=JsonFormat.Shape.ARRAY)
+ @JsonPropertyOrder(alphabetic=true)
+ static class AsArrayWithViewAndCreator
+ {
+ @JsonView(ViewA.class)
+ public int a;
+ @JsonView(ViewB.class)
+ public int b;
+ public int c;
+
+ @JsonCreator
+ public AsArrayWithViewAndCreator(@JsonProperty("a") int a,
+ @JsonProperty("b") int b,
+ @JsonProperty("c") int c)
+ {
+ this.a = a;
+ this.b = b;
+ this.c = c;
+ }
+ }
+
/*
/*****************************************************
/* Basic tests
@@ -75,12 +96,23 @@
assertEquals("[1,null,3]", json);
// and then that conversely deserializer does something similar
- AsArrayWithView output = MAPPER.readerFor(AsArrayWithView.class).withView(ViewB.class)
+ AsArrayWithView result = MAPPER.readerFor(AsArrayWithView.class).withView(ViewB.class)
.readValue("[1,2,3]");
// should include 'c' (not view-able) and 'b' (include in ViewB) but not 'a'
- assertEquals(3, output.c);
- assertEquals(2, output.b);
- assertEquals(0, output.a);
+ assertEquals(3, result.c);
+ assertEquals(2, result.b);
+ assertEquals(0, result.a);
+ }
+
+ public void testWithViewAndCreator() throws Exception
+ {
+ AsArrayWithViewAndCreator result = MAPPER.readerFor(AsArrayWithViewAndCreator.class)
+ .withView(ViewB.class)
+ .readValue("[1,2,3]");
+ // should include 'c' (not view-able) and 'b' (include in ViewB) but not 'a'
+ assertEquals(3, result.c);
+ assertEquals(2, result.b);
+ assertEquals(0, result.a);
}
public void testWithCreatorsOrdered() throws Exception
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArrayWithBuilder.java b/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArrayWithBuilder.java
index 2f572a4..321c86c 100644
--- a/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArrayWithBuilder.java
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/TestPOJOAsArrayWithBuilder.java
@@ -1,9 +1,14 @@
package com.fasterxml.jackson.databind.struct;
import com.fasterxml.jackson.annotation.*;
+
import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
/**
* Unit tests for "POJO as array" feature using Builder-style
@@ -28,6 +33,12 @@
static class SimpleBuilderXY
{
public int x, y;
+
+ protected SimpleBuilderXY() { }
+ protected SimpleBuilderXY(int x0, int y0) {
+ x = x0;
+ y = y0;
+ }
public SimpleBuilderXY withX(int x0) {
this.x = x0;
@@ -43,7 +54,48 @@
return new ValueClassXY(x, y);
}
}
-
+
+ // Also, with creator:
+
+ @JsonDeserialize(builder=CreatorBuilder.class)
+ @JsonFormat(shape=JsonFormat.Shape.ARRAY)
+ @JsonPropertyOrder(alphabetic=true)
+ static class CreatorValue
+ {
+ final int a, b, c;
+
+ protected CreatorValue(int a, int b, int c) {
+ this.a = a;
+ this.b = b;
+ this.c = c;
+ }
+ }
+
+ @JsonFormat(shape=JsonFormat.Shape.ARRAY)
+ static class CreatorBuilder {
+ private final int a, b;
+
+ private int c;
+
+ @JsonCreator
+ public CreatorBuilder(@JsonProperty("a") int a,
+ @JsonProperty("b") int b)
+ {
+ this.a = a;
+ this.b = b;
+ }
+
+ @JsonView(String.class)
+ public CreatorBuilder withC(int v) {
+ c = v;
+ return this;
+ }
+
+ public CreatorValue build() {
+ return new CreatorValue(a, b, c);
+ }
+ }
+
/*
/*****************************************************
/* Basic tests
@@ -59,4 +111,95 @@
assertEquals(2, value._x);
assertEquals(3, value._y);
}
+
+ // Won't work, but verify exception
+ public void testBuilderWithUpdate() throws Exception
+ {
+ // Ok, first, simple case of all values being present
+ try {
+ /*value =*/ MAPPER.readerFor(ValueClassXY.class)
+ .withValueToUpdate(new ValueClassXY(6, 7))
+ .readValue("[1,2]");
+ fail("Should not pass");
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "Deserialization of");
+ verifyException(e, "by passing existing instance");
+ verifyException(e, "ValueClassXY");
+ }
+ }
+
+ /*
+ /*****************************************************
+ /* Creator test(s)
+ /*****************************************************
+ */
+
+ // test to ensure @JsonCreator also works
+ public void testWithCreator() throws Exception
+ {
+ CreatorValue value = MAPPER.readValue("[1,2,3]", CreatorValue.class);
+ assertEquals(1, value.a);
+ assertEquals(2, value.b);
+ assertEquals(3, value.c);
+
+ // and should be ok with partial too?
+ value = MAPPER.readValue("[1,2]", CreatorValue.class);
+ assertEquals(1, value.a);
+ assertEquals(2, value.b);
+ assertEquals(0, value.c);
+
+ value = MAPPER.readValue("[1]", CreatorValue.class);
+ assertEquals(1, value.a);
+ assertEquals(0, value.b);
+ assertEquals(0, value.c);
+
+ value = MAPPER.readValue("[]", CreatorValue.class);
+ assertEquals(0, value.a);
+ assertEquals(0, value.b);
+ assertEquals(0, value.c);
+ }
+
+ public void testWithCreatorAndView() throws Exception
+ {
+ ObjectReader reader = MAPPER.readerFor(CreatorValue.class);
+ CreatorValue value;
+
+ // First including values in view
+ value = reader.withView(String.class).readValue("[1,2,3]");
+ assertEquals(1, value.a);
+ assertEquals(2, value.b);
+ assertEquals(3, value.c);
+
+ // then not including view
+ value = reader.withView(Character.class).readValue("[1,2,3]");
+ assertEquals(1, value.a);
+ assertEquals(2, value.b);
+ assertEquals(0, value.c);
+ }
+
+ /*
+ /*****************************************************
+ /* Failure tests
+ /*****************************************************
+ */
+
+ public void testUnknownExtraProp() throws Exception
+ {
+ String json = "[1, 2, 3, 4]";
+ try {
+ MAPPER.readValue(json, ValueClassXY.class);
+ fail("should not pass with extra element");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Unexpected JSON values");
+ }
+
+ // but actually fine if skip-unknown set
+ ValueClassXY v = MAPPER.readerFor(ValueClassXY.class)
+ .without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
+ .readValue(json);
+ assertNotNull(v);
+ // note: +1 for both so
+ assertEquals(v._x, 2);
+ assertEquals(v._y, 3);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapped.java b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapped.java
index dd373f6..d6536f8 100644
--- a/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapped.java
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapped.java
@@ -23,6 +23,17 @@
}
}
+ final static class Location {
+ public int x;
+ public int y;
+
+ public Location() { }
+ public Location(int x, int y) {
+ this.x = x;
+ this.y = y;
+ }
+ }
+
static class DeepUnwrapping
{
@JsonUnwrapped
@@ -45,17 +56,6 @@
name = n;
}
}
-
- final static class Location {
- public int x;
- public int y;
-
- public Location() { }
- public Location(int x, int y) {
- this.x = x;
- this.y = y;
- }
- }
// Class with two unwrapped properties
static class TwoUnwrappedProperties {
diff --git a/src/test/java/com/fasterxml/jackson/failing/TestUnwrappedIssue383.java b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedRecursive383.java
similarity index 82%
rename from src/test/java/com/fasterxml/jackson/failing/TestUnwrappedIssue383.java
rename to src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedRecursive383.java
index a9a8746..5b41be6 100644
--- a/src/test/java/com/fasterxml/jackson/failing/TestUnwrappedIssue383.java
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedRecursive383.java
@@ -1,12 +1,13 @@
-package com.fasterxml.jackson.failing;
+package com.fasterxml.jackson.databind.struct;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
-public class TestUnwrappedIssue383 extends BaseMapTest
+// Problem with recursive definition of unwrapping
+public class TestUnwrappedRecursive383 extends BaseMapTest
{
- // [Issue#383]
+ // [databind#383]
static class RecursivePerson {
public String name;
public int age;
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithPrefix.java b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithPrefix.java
index 5d439c5..29e742f 100644
--- a/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithPrefix.java
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithPrefix.java
@@ -137,10 +137,6 @@
static class SubChild {
public String value;
}
-
- // // // Reuse mapper to keep tests bit faster
-
- private final ObjectMapper MAPPER = new ObjectMapper();
/*
/**********************************************************
@@ -148,6 +144,8 @@
/**********************************************************
*/
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
public void testPrefixedUnwrappingSerialize() throws Exception
{
assertEquals("{\"name\":\"Tatu\",\"_x\":1,\"_y\":2}",
diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestUnwrappedWithTypeInfo.java b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithTypeInfo.java
similarity index 97%
rename from src/test/java/com/fasterxml/jackson/databind/ser/TestUnwrappedWithTypeInfo.java
rename to src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithTypeInfo.java
index d199044..5a5f2cc 100644
--- a/src/test/java/com/fasterxml/jackson/databind/ser/TestUnwrappedWithTypeInfo.java
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithTypeInfo.java
@@ -1,4 +1,4 @@
-package com.fasterxml.jackson.databind.ser;
+package com.fasterxml.jackson.databind.struct;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/UnwrapSingleArrayScalarsTest.java b/src/test/java/com/fasterxml/jackson/databind/struct/UnwrapSingleArrayScalarsTest.java
new file mode 100644
index 0000000..ba9be1c
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/UnwrapSingleArrayScalarsTest.java
@@ -0,0 +1,408 @@
+package com.fasterxml.jackson.databind.struct;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.net.URI;
+import java.util.UUID;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectReader;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+
+public class UnwrapSingleArrayScalarsTest extends BaseMapTest
+{
+ static class BooleanBean {
+ boolean _v;
+ void setV(boolean v) { _v = v; }
+ }
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ private final ObjectReader NO_UNWRAPPING_READER = MAPPER.reader();
+ private final ObjectReader UNWRAPPING_READER = MAPPER.reader()
+ .with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+
+ /*
+ /**********************************************************
+ /* Tests for boolean
+ /**********************************************************
+ */
+
+ public void testBooleanPrimitiveArrayUnwrap() throws Exception
+ {
+ // [databind#381]
+ final ObjectMapper mapper = new ObjectMapper();
+ mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+ BooleanBean result = mapper.readValue(new StringReader("{\"v\":[true]}"), BooleanBean.class);
+ assertTrue(result._v);
+
+ _verifyMultiValueArrayFail("[{\"v\":[true,true]}]", BooleanBean.class);
+
+ result = mapper.readValue("{\"v\":[null]}", BooleanBean.class);
+ assertNotNull(result);
+ assertFalse(result._v);
+
+ result = mapper.readValue("[{\"v\":[null]}]", BooleanBean.class);
+ assertNotNull(result);
+ assertFalse(result._v);
+
+ boolean[] array = mapper.readValue(new StringReader("[ [ null ] ]"), boolean[].class);
+ assertNotNull(array);
+ assertEquals(1, array.length);
+ assertFalse(array[0]);
+ }
+
+ /*
+ /**********************************************************
+ /* Single-element as array tests, numbers
+ /**********************************************************
+ */
+
+ // [databind#381]
+ public void testSingleElementScalarArrays() throws Exception {
+ final int intTest = 932832;
+ final double doubleTest = 32.3234;
+ final long longTest = 2374237428374293423L;
+ final short shortTest = (short) intTest;
+ final float floatTest = 84.3743f;
+ final byte byteTest = (byte) 43;
+ final char charTest = 'c';
+
+ final ObjectMapper mapper = new ObjectMapper();
+ mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+
+ final int intValue = mapper.readValue(asArray(intTest), Integer.TYPE);
+ assertEquals(intTest, intValue);
+ final Integer integerWrapperValue = mapper.readValue(asArray(Integer.valueOf(intTest)), Integer.class);
+ assertEquals(Integer.valueOf(intTest), integerWrapperValue);
+
+ final double doubleValue = mapper.readValue(asArray(doubleTest), Double.class);
+ assertEquals(doubleTest, doubleValue);
+ final Double doubleWrapperValue = mapper.readValue(asArray(Double.valueOf(doubleTest)), Double.class);
+ assertEquals(Double.valueOf(doubleTest), doubleWrapperValue);
+
+ final long longValue = mapper.readValue(asArray(longTest), Long.TYPE);
+ assertEquals(longTest, longValue);
+ final Long longWrapperValue = mapper.readValue(asArray(Long.valueOf(longTest)), Long.class);
+ assertEquals(Long.valueOf(longTest), longWrapperValue);
+
+ final short shortValue = mapper.readValue(asArray(shortTest), Short.TYPE);
+ assertEquals(shortTest, shortValue);
+ final Short shortWrapperValue = mapper.readValue(asArray(Short.valueOf(shortTest)), Short.class);
+ assertEquals(Short.valueOf(shortTest), shortWrapperValue);
+
+ final float floatValue = mapper.readValue(asArray(floatTest), Float.TYPE);
+ assertEquals(floatTest, floatValue);
+ final Float floatWrapperValue = mapper.readValue(asArray(Float.valueOf(floatTest)), Float.class);
+ assertEquals(Float.valueOf(floatTest), floatWrapperValue);
+
+ final byte byteValue = mapper.readValue(asArray(byteTest), Byte.TYPE);
+ assertEquals(byteTest, byteValue);
+ final Byte byteWrapperValue = mapper.readValue(asArray(Byte.valueOf(byteTest)), Byte.class);
+ assertEquals(Byte.valueOf(byteTest), byteWrapperValue);
+
+ final char charValue = mapper.readValue(asArray(quote(String.valueOf(charTest))), Character.TYPE);
+ assertEquals(charTest, charValue);
+ final Character charWrapperValue = mapper.readValue(asArray(quote(String.valueOf(charTest))), Character.class);
+ assertEquals(Character.valueOf(charTest), charWrapperValue);
+
+ final boolean booleanTrueValue = mapper.readValue(asArray(true), Boolean.TYPE);
+ assertTrue(booleanTrueValue);
+
+ final boolean booleanFalseValue = mapper.readValue(asArray(false), Boolean.TYPE);
+ assertFalse(booleanFalseValue);
+
+ final Boolean booleanWrapperTrueValue = mapper.readValue(asArray(Boolean.valueOf(true)), Boolean.class);
+ assertEquals(Boolean.TRUE, booleanWrapperTrueValue);
+ }
+
+ public void testSingleElementArrayDisabled() throws Exception {
+ final ObjectMapper mapper = new ObjectMapper();
+ mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+ try {
+ mapper.readValue("[42]", Integer.class);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+ try {
+ mapper.readValue("[42]", Integer.TYPE);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+ try {
+ mapper.readValue("[42342342342342]", Long.class);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+ try {
+ mapper.readValue("[42342342342342342]", Long.TYPE);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+
+ try {
+ mapper.readValue("[42]", Short.class);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+ try {
+ mapper.readValue("[42]", Short.TYPE);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+
+ try {
+ mapper.readValue("[327.2323]", Float.class);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+ try {
+ mapper.readValue("[82.81902]", Float.TYPE);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+
+ try {
+ mapper.readValue("[22]", Byte.class);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+ try {
+ mapper.readValue("[22]", Byte.TYPE);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+
+ try {
+ mapper.readValue("['d']", Character.class);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+ try {
+ mapper.readValue("['d']", Character.TYPE);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+
+ try {
+ mapper.readValue("[true]", Boolean.class);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+ try {
+ mapper.readValue("[true]", Boolean.TYPE);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException exp) {
+ //Exception was thrown correctly
+ }
+ }
+
+ public void testMultiValueArrayException() throws IOException {
+ _verifyMultiValueArrayFail("[42,42]", Integer.class);
+ _verifyMultiValueArrayFail("[42,42]", Integer.TYPE);
+ _verifyMultiValueArrayFail("[42342342342342,42342342342342]", Long.class);
+ _verifyMultiValueArrayFail("[42342342342342342,42342342342342]", Long.TYPE);
+ _verifyMultiValueArrayFail("[42,42]", Short.class);
+ _verifyMultiValueArrayFail("[42,42]", Short.TYPE);
+ _verifyMultiValueArrayFail("[22,23]", Byte.class);
+ _verifyMultiValueArrayFail("[22,23]", Byte.TYPE);
+ _verifyMultiValueArrayFail("[327.2323,327.2323]", Float.class);
+ _verifyMultiValueArrayFail("[82.81902,327.2323]", Float.TYPE);
+ _verifyMultiValueArrayFail("[42.273,42.273]", Double.class);
+ _verifyMultiValueArrayFail("[42.2723,42.273]", Double.TYPE);
+ _verifyMultiValueArrayFail(asArray(quote("c") + "," + quote("d")), Character.class);
+ _verifyMultiValueArrayFail(asArray(quote("c") + "," + quote("d")), Character.TYPE);
+ _verifyMultiValueArrayFail("[true,false]", Boolean.class);
+ _verifyMultiValueArrayFail("[true,false]", Boolean.TYPE);
+ }
+
+ /*
+ /**********************************************************
+ /* Simple non-primitive types
+ /**********************************************************
+ */
+
+ public void testSingleString() throws Exception
+ {
+ String value = "FOO!";
+ String result = MAPPER.readValue("\""+value+"\"", String.class);
+ assertEquals(value, result);
+ }
+
+ public void testSingleStringWrapped() throws Exception
+ {
+ final ObjectMapper mapper = new ObjectMapper();
+ mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+
+ String value = "FOO!";
+ try {
+ mapper.readValue("[\""+value+"\"]", String.class);
+ fail("Exception not thrown when attempting to unwrap a single value 'String' array into a simple String");
+ } catch (MismatchedInputException exp) {
+ verifyException(exp, "Can not deserialize");
+ verifyException(exp, "out of START_ARRAY");
+ }
+
+ mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+
+ try {
+ mapper.readValue("[\""+value+"\",\""+value+"\"]", String.class);
+ fail("Exception not thrown when attempting to unwrap a single value 'String' array that contained more than one value into a simple String");
+ } catch (MismatchedInputException exp) {
+ verifyException(exp, "Attempted to unwrap");
+ }
+ String result = mapper.readValue("[\""+value+"\"]", String.class);
+ assertEquals(value, result);
+ }
+
+ public void testBigDecimal() throws Exception
+ {
+ final ObjectMapper mapper = objectMapper();
+ mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+
+ BigDecimal value = new BigDecimal("0.001");
+ BigDecimal result = mapper.readValue(value.toString(), BigDecimal.class);
+ assertEquals(value, result);
+ try {
+ mapper.readValue("[" + value.toString() + "]", BigDecimal.class);
+ fail("Exception was not thrown when attempting to read a single value array of BigDecimal when UNWRAP_SINGLE_VALUE_ARRAYS feature is disabled");
+ } catch (MismatchedInputException exp) {
+ verifyException(exp, "Can not deserialize");
+ verifyException(exp, "out of START_ARRAY");
+ }
+
+ mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+ result = mapper.readValue("[" + value.toString() + "]", BigDecimal.class);
+ assertEquals(value, result);
+
+ try {
+ mapper.readValue("[" + value.toString() + "," + value.toString() + "]", BigDecimal.class);
+ fail("Exception was not thrown when attempting to read a muti value array of BigDecimal when UNWRAP_SINGLE_VALUE_ARRAYS feature is enabled");
+ } catch (MismatchedInputException exp) {
+ verifyException(exp, "Attempted to unwrap");
+ }
+ }
+
+ public void testBigInteger() throws Exception
+ {
+ final ObjectMapper mapper = objectMapper();
+ mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+
+ BigInteger value = new BigInteger("-1234567890123456789012345567809");
+ BigInteger result = mapper.readValue(value.toString(), BigInteger.class);
+ assertEquals(value, result);
+
+ try {
+ mapper.readValue("[" + value.toString() + "]", BigInteger.class);
+ fail("Exception was not thrown when attempting to read a single value array of BigInteger when UNWRAP_SINGLE_VALUE_ARRAYS feature is disabled");
+ } catch (MismatchedInputException exp) {
+ verifyException(exp, "Can not deserialize");
+ verifyException(exp, "out of START_ARRAY");
+ }
+
+ mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
+ result = mapper.readValue("[" + value.toString() + "]", BigInteger.class);
+ assertEquals(value, result);
+
+ try {
+ mapper.readValue("[" + value.toString() + "," + value.toString() + "]", BigInteger.class);
+ fail("Exception was not thrown when attempting to read a multi-value array of BigInteger when UNWRAP_SINGLE_VALUE_ARRAYS feature is enabled");
+ } catch (MismatchedInputException exp) {
+ verifyException(exp, "Attempted to unwrap");
+ }
+ }
+
+ public void testClassAsArray() throws Exception
+ {
+ Class<?> result = MAPPER
+ .readerFor(Class.class)
+ .with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
+ .readValue(quote(String.class.getName()));
+ assertEquals(String.class, result);
+
+ try {
+ MAPPER.readerFor(Class.class)
+ .without(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
+ .readValue("[" + quote(String.class.getName()) + "]");
+ fail("Did not throw exception when UNWRAP_SINGLE_VALUE_ARRAYS feature was disabled and attempted to read a Class array containing one element");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "out of START_ARRAY token");
+ }
+
+ _verifyMultiValueArrayFail("[" + quote(Object.class.getName()) + "," + quote(Object.class.getName()) +"]",
+ Class.class);
+ result = MAPPER.readerFor(Class.class)
+ .with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
+ .readValue("[" + quote(String.class.getName()) + "]");
+ assertEquals(String.class, result);
+ }
+
+ public void testURIAsArray() throws Exception
+ {
+ final ObjectReader reader = MAPPER.readerFor(URI.class);
+ final URI value = new URI("http://foo.com");
+ try {
+ reader.without(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
+ .readValue("[\""+value.toString()+"\"]");
+ fail("Did not throw exception for single value array when UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "out of START_ARRAY token");
+ }
+
+ _verifyMultiValueArrayFail("[\""+value.toString()+"\",\""+value.toString()+"\"]", URI.class);
+ }
+
+ public void testUUIDAsArray() throws Exception
+ {
+ final ObjectReader reader = MAPPER.readerFor(UUID.class);
+ final String uuidStr = "76e6d183-5f68-4afa-b94a-922c1fdb83f8";
+ UUID uuid = UUID.fromString(uuidStr);
+ try {
+ NO_UNWRAPPING_READER.forType(UUID.class)
+ .readValue("[" + quote(uuidStr) + "]");
+ fail("Exception was not thrown when UNWRAP_SINGLE_VALUE_ARRAYS is disabled and attempted to read a single value array as a single element");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "out of START_ARRAY token");
+ }
+ assertEquals(uuid,
+ reader.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
+ .readValue("[" + quote(uuidStr) + "]"));
+ _verifyMultiValueArrayFail("[" + quote(uuidStr) + "," + quote(uuidStr) + "]", UUID.class);
+ }
+
+ /*
+ /**********************************************************
+ /* Helper methods
+ /**********************************************************
+ */
+
+ private void _verifyMultiValueArrayFail(String input, Class<?> type) throws IOException {
+ try {
+ UNWRAPPING_READER.forType(type).readValue(input);
+ fail("Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
+ } catch (MismatchedInputException e) {
+ verifyException(e, "Attempted to unwrap");
+ }
+ }
+
+ private static String asArray(Object value) {
+ return "["+value+"]";
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/UnwrappedCreatorParam265Test.java b/src/test/java/com/fasterxml/jackson/databind/struct/UnwrappedCreatorParam265Test.java
new file mode 100644
index 0000000..21aa669
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/UnwrappedCreatorParam265Test.java
@@ -0,0 +1,101 @@
+package com.fasterxml.jackson.databind.struct;
+
+import com.fasterxml.jackson.annotation.*;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
+
+public class UnwrappedCreatorParam265Test extends BaseMapTest
+{
+ static class JAddress {
+ public String address;
+ public String city;
+ public String state;
+
+ protected JAddress() { }
+
+ public JAddress(String address, String city, String state) {
+ this.address = address;
+ this.city = city;
+ this.state = state;
+ }
+ }
+
+ static class JPersonWithoutName
+ {
+ public String name;
+
+ protected JAddress _address;
+
+ @JsonCreator
+ public JPersonWithoutName(@JsonProperty("name") String name,
+ @JsonUnwrapped JAddress address)
+ {
+ this.name = name;
+ _address = address;
+ }
+
+ @JsonUnwrapped
+ public JAddress getAddress() { return _address; }
+ }
+
+ static class JPersonWithName
+ {
+ public String name;
+
+ protected JAddress _address;
+
+ @JsonCreator
+ public JPersonWithName(@JsonProperty("name") String name,
+ @JsonUnwrapped
+ @JsonProperty("address")
+ JAddress address)
+ {
+ this.name = name;
+ _address = address;
+ }
+
+ @JsonUnwrapped
+ public JAddress getAddress() { return _address; }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ // For [databind#265]: handle problem by throwing exception
+ public void testUnwrappedWithUnnamedCreatorParam() throws Exception
+ {
+ JPersonWithoutName person = new JPersonWithoutName("MyName", new JAddress("main street", "springfield", "WA"));
+ ObjectMapper mapper = new ObjectMapper();
+ // serialization should be fine as far as that goes
+ String json = mapper.writeValueAsString(person);
+
+ // but not deserialization:
+ try {
+ /*JPersonWithoutName result =*/ mapper.readValue(json, JPersonWithoutName.class);
+ fail("Should not pass");
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "Can not define Creator parameter");
+ verifyException(e, "@JsonUnwrapped");
+ }
+ }
+
+ // For [databind#265]: handle problem by throwing exception
+ public void testUnwrappedWithNamedCreatorParam() throws Exception
+ {
+ JPersonWithName person = new JPersonWithName("MyName", new JAddress("main street", "springfield", "WA"));
+ ObjectMapper mapper = new ObjectMapper();
+ // serialization should be fine as far as that goes
+ String json = mapper.writeValueAsString(person);
+ try {
+ /*JPersonWithName result =*/ mapper.readValue(json, JPersonWithName.class);
+ fail("Should not pass");
+ } catch (InvalidDefinitionException e) {
+ verifyException(e, "Can not define Creator property \"address\"");
+ verifyException(e, "@JsonUnwrapped");
+ }
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/struct/UnwrappedWithView1559Test.java b/src/test/java/com/fasterxml/jackson/databind/struct/UnwrappedWithView1559Test.java
new file mode 100644
index 0000000..b23cf71
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/struct/UnwrappedWithView1559Test.java
@@ -0,0 +1,36 @@
+package com.fasterxml.jackson.databind.struct;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonUnwrapped;
+import com.fasterxml.jackson.databind.*;
+
+public class UnwrappedWithView1559Test extends BaseMapTest
+{
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ static final class Health {
+ @JsonUnwrapped(prefix="xxx.")
+ public Status status;
+ }
+
+ // NOTE: `final` is required to trigger [databind#1559]
+ static final class Status {
+ public String code;
+ }
+
+ /*
+ /**********************************************************
+ /* Tests methods
+ /**********************************************************
+ */
+
+ // for [databind#1559]
+ public void testCanSerializeSimpleWithDefaultView() throws Exception {
+ String json = new ObjectMapper().configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false)
+ .writeValueAsString(new Health());
+ assertEquals(aposToQuotes("{}"), json);
+ // and just in case this, although won't matter wrt output
+ json = new ObjectMapper().configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true)
+ .writeValueAsString(new Health());
+ assertEquals(aposToQuotes("{}"), json);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/type/CollectionType1415Test.java b/src/test/java/com/fasterxml/jackson/databind/type/CollectionType1415Test.java
new file mode 100644
index 0000000..03f6ffb
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/type/CollectionType1415Test.java
@@ -0,0 +1,102 @@
+package com.fasterxml.jackson.databind.type;
+
+import java.util.*;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.CollectionType;
+import com.fasterxml.jackson.databind.type.MapType;
+
+// for [databind#1415]
+public class CollectionType1415Test extends BaseMapTest
+{
+ static abstract class LongList implements List<Long> { }
+
+ static abstract class StringLongMap implements Map<String,Long> { }
+
+ /*
+ /**********************************************************
+ /* Unit tests, success
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testExplicitCollectionType() throws Exception
+ {
+ JavaType t = MAPPER.getTypeFactory()
+ .constructCollectionType(LongList.class, Long.class);
+ assertEquals(LongList.class, t.getRawClass());
+ assertEquals(Long.class, t.getContentType().getRawClass());
+ }
+
+ public void testImplicitCollectionType() throws Exception
+ {
+ JavaType t = MAPPER.getTypeFactory()
+ .constructParametricType(List.class, Long.class);
+ assertEquals(CollectionType.class, t.getClass());
+ assertEquals(List.class, t.getRawClass());
+ assertEquals(Long.class, t.getContentType().getRawClass());
+ }
+
+ public void testExplicitMapType() throws Exception
+ {
+ JavaType t = MAPPER.getTypeFactory()
+ .constructMapType(StringLongMap.class,
+ String.class, Long.class);
+ assertEquals(StringLongMap.class, t.getRawClass());
+ assertEquals(String.class, t.getKeyType().getRawClass());
+ assertEquals(Long.class, t.getContentType().getRawClass());
+ }
+
+ public void testImplicitMapType() throws Exception
+ {
+ JavaType t = MAPPER.getTypeFactory()
+ .constructParametricType(Map.class, Long.class, Boolean.class);
+ assertEquals(MapType.class, t.getClass());
+ assertEquals(Long.class, t.getKeyType().getRawClass());
+ assertEquals(Boolean.class, t.getContentType().getRawClass());
+ }
+
+ /*
+ /**********************************************************
+ /* Unit tests, fails
+ /**********************************************************
+ */
+
+ public void testMismatchedCollectionType() throws Exception
+ {
+ try {
+ MAPPER.getTypeFactory()
+ .constructCollectionType(LongList.class, String.class);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "LongList did not resolve to something");
+ verifyException(e, "element type");
+ }
+ }
+
+ public void testMismatchedMapType() throws Exception
+ {
+ // first, mismatched key type
+ try {
+ MAPPER.getTypeFactory()
+ .constructMapType(StringLongMap.class, Boolean.class, Long.class);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "StringLongMap did not resolve to something");
+ verifyException(e, "key type");
+ }
+ // then, mismatched value type
+ try {
+ MAPPER.getTypeFactory()
+ .constructMapType(StringLongMap.class, String.class, Class.class);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "StringLongMap did not resolve to something");
+ verifyException(e, "value type");
+ }
+ }
+}
+
diff --git a/src/test/java/com/fasterxml/jackson/databind/type/TestAnnotatedClass.java b/src/test/java/com/fasterxml/jackson/databind/type/TestAnnotatedClass.java
index dd16220..ec803da 100644
--- a/src/test/java/com/fasterxml/jackson/databind/type/TestAnnotatedClass.java
+++ b/src/test/java/com/fasterxml/jackson/databind/type/TestAnnotatedClass.java
@@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
+import com.fasterxml.jackson.databind.introspect.AnnotatedClassResolver;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
/**
@@ -82,7 +83,7 @@
{
SerializationConfig config = MAPPER.getSerializationConfig();
JavaType t = MAPPER.constructType(FieldBean.class);
- AnnotatedClass ac = AnnotatedClass.construct(t, config);
+ AnnotatedClass ac = AnnotatedClassResolver.resolve(config, t, config);
// AnnotatedClass does not ignore non-visible fields, yet
assertEquals(2, ac.getFieldCount());
for (AnnotatedField f : ac.fields()) {
@@ -101,7 +102,7 @@
Bean1005 bean = new Bean1005(13);
SerializationConfig config = MAPPER.getSerializationConfig();
JavaType t = MAPPER.constructType(bean.getClass());
- AnnotatedClass ac = AnnotatedClass.construct(t, config);
+ AnnotatedClass ac = AnnotatedClassResolver.resolve(config, t, config);
assertEquals(1, ac.getConstructors().size());
}
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/type/TestJavaType.java b/src/test/java/com/fasterxml/jackson/databind/type/TestJavaType.java
index 91be3f7..ffe0b29 100644
--- a/src/test/java/com/fasterxml/jackson/databind/type/TestJavaType.java
+++ b/src/test/java/com/fasterxml/jackson/databind/type/TestJavaType.java
@@ -72,6 +72,7 @@
JavaType baseType = tf.constructType(BaseType.class);
assertSame(BaseType.class, baseType.getRawClass());
assertTrue(baseType.hasRawClass(BaseType.class));
+ assertFalse(baseType.isTypeOrSubTypeOf(SubType.class));
assertFalse(baseType.isArrayType());
assertFalse(baseType.isContainerType());
@@ -84,8 +85,28 @@
assertNull(baseType.getContentType());
assertNull(baseType.getKeyType());
assertNull(baseType.getValueHandler());
+
+ assertEquals("Lcom/fasterxml/jackson/databind/type/TestJavaType$BaseType;", baseType.getGenericSignature());
+ assertEquals("Lcom/fasterxml/jackson/databind/type/TestJavaType$BaseType;", baseType.getErasedSignature());
}
+ @SuppressWarnings("deprecation")
+ public void testDeprecated()
+ {
+ TypeFactory tf = TypeFactory.defaultInstance();
+ JavaType baseType = tf.constructType(BaseType.class);
+ assertTrue(baseType.hasRawClass(BaseType.class));
+ assertNull(baseType.getParameterSource());
+ assertNull(baseType.getContentTypeHandler());
+ assertNull(baseType.getContentValueHandler());
+ assertFalse(baseType.hasValueHandler());
+ assertFalse(baseType.hasHandlers());
+
+ assertSame(baseType, baseType.forcedNarrowBy(BaseType.class));
+ JavaType sub = baseType.forcedNarrowBy(SubType.class);
+ assertTrue(sub.hasRawClass(SubType.class));
+ }
+
public void testArrayType()
{
TypeFactory tf = TypeFactory.defaultInstance();
@@ -119,6 +140,9 @@
assertNotNull(mapT.getContentType());
assertNotNull(mapT.getKeyType());
+ assertEquals("Ljava/util/HashMap<Ljava/lang/Object;Ljava/lang/Object;>;", mapT.getGenericSignature());
+ assertEquals("Ljava/util/HashMap;", mapT.getErasedSignature());
+
assertTrue(mapT.equals(mapT));
assertFalse(mapT.equals(null));
assertFalse(mapT.equals("xyz"));
@@ -127,7 +151,17 @@
public void testEnumType()
{
TypeFactory tf = TypeFactory.defaultInstance();
- assertTrue(tf.constructType(MyEnum.class).isEnumType());
+ JavaType enumT = tf.constructType(MyEnum.class);
+ assertTrue(enumT.isEnumType());
+ assertFalse(enumT.hasHandlers());
+ assertTrue(enumT.isTypeOrSubTypeOf(MyEnum.class));
+ assertTrue(enumT.isTypeOrSubTypeOf(Object.class));
+ assertNull(enumT.containedType(3));
+ assertTrue(enumT.containedTypeOrUnknown(3).isJavaLangObject());
+
+ assertEquals("Lcom/fasterxml/jackson/databind/type/TestJavaType$MyEnum;", enumT.getGenericSignature());
+ assertEquals("Lcom/fasterxml/jackson/databind/type/TestJavaType$MyEnum;", enumT.getErasedSignature());
+
assertTrue(tf.constructType(MyEnum2.class).isEnumType());
assertTrue(tf.constructType(MyEnum.A.getClass()).isEnumType());
assertTrue(tf.constructType(MyEnum2.A.getClass()).isEnumType());
@@ -164,7 +198,8 @@
m = Generic1194.class.getMethod("getList");
t = tf.constructType(m.getGenericReturnType());
assertEquals("Ljava/util/List<Ljava/lang/String;>;", t.getGenericSignature());
-
+ assertEquals("Ljava/util/List;", t.getErasedSignature());
+
m = Generic1194.class.getMethod("getMap");
t = tf.constructType(m.getGenericReturnType());
assertEquals("Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;",
diff --git a/src/test/java/com/fasterxml/jackson/databind/type/TestTypeBindings.java b/src/test/java/com/fasterxml/jackson/databind/type/TestTypeBindings.java
index 7af37e8..3c21d20 100644
--- a/src/test/java/com/fasterxml/jackson/databind/type/TestTypeBindings.java
+++ b/src/test/java/com/fasterxml/jackson/databind/type/TestTypeBindings.java
@@ -40,10 +40,11 @@
/**********************************************************
*/
+ private final TypeFactory DEFAULT_TF = TypeFactory.defaultInstance();
+
public void testInnerType() throws Exception
{
- TypeFactory tf = TypeFactory.defaultInstance();
- JavaType type = tf.constructType(InnerGenericTyping.InnerClass.class);
+ JavaType type = DEFAULT_TF.constructType(InnerGenericTyping.InnerClass.class);
assertEquals(MapType.class, type.getClass());
JavaType keyType = type.getKeyType();
assertEquals(Object.class, keyType.getRawClass());
@@ -56,8 +57,34 @@
// for [databind#76]
public void testRecursiveType()
{
- TypeFactory tf = TypeFactory.defaultInstance();
- JavaType type = tf.constructType(HashTree.class);
+ JavaType type = DEFAULT_TF.constructType(HashTree.class);
assertNotNull(type);
}
+
+ public void testBindingsBasics()
+ {
+ TypeBindings b = TypeBindings.create(Collection.class,
+ TypeFactory.unknownType());
+ // let's just call it -- should probably try to inspect but...
+ assertNotNull(b.toString());
+ assertEquals(Object.class, b.getBoundType(0).getRawClass());
+ assertNull(b.getBoundName(-1));
+ assertNull(b.getBoundType(-1));
+ assertNull(b.getBoundName(1));
+ assertNull(b.getBoundType(1));
+
+ assertFalse(b.equals("foo"));
+ }
+
+ public void testInvalidBindings()
+ {
+ JavaType unknown = TypeFactory.unknownType();
+ try {
+ TypeBindings.create(AbstractType.class, unknown);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "Can not create TypeBindings");
+ verifyException(e, "class expects 2");
+ }
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/type/TestTypeFactory.java b/src/test/java/com/fasterxml/jackson/databind/type/TestTypeFactory.java
index dd075e5..274cdb3 100644
--- a/src/test/java/com/fasterxml/jackson/databind/type/TestTypeFactory.java
+++ b/src/test/java/com/fasterxml/jackson/databind/type/TestTypeFactory.java
@@ -354,12 +354,7 @@
assertEquals(Integer.class, subtype.getContentType().getContentType().getRawClass());
// but with refinement, should have non-null super class
- // 20-Oct-2015, tatu: For now refinement does not faithfully replicate the
- // structure, it only retains most important information. Here it means
- // that actually existing super-classes are skipped, and only original
- // type is linked as expected
- /*
JavaType superType = subtype.getSuperClass();
assertNotNull(superType);
assertEquals(HashMap.class, superType.getRawClass());
@@ -367,7 +362,25 @@
assertEquals(String.class, superType.getKeyType().getRawClass());
assertEquals(List.class, superType.getContentType().getRawClass());
assertEquals(Integer.class, superType.getContentType().getContentType().getRawClass());
- */
+ }
+
+ public void testTypeGeneralization()
+ {
+ TypeFactory tf = newTypeFactory();
+ MapType t = tf.constructMapType(HashMap.class, String.class, Long.class);
+ JavaType superT = tf.constructGeneralizedType(t, Map.class);
+ assertEquals(String.class, superT.getKeyType().getRawClass());
+ assertEquals(Long.class, superT.getContentType().getRawClass());
+
+ assertSame(t, tf.constructGeneralizedType(t, HashMap.class));
+
+ // plus check there is super/sub relationship
+ try {
+ tf.constructGeneralizedType(t, TreeMap.class);
+ fail("Should not pass");
+ } catch (IllegalArgumentException e) {
+ verifyException(e, "not a super-type of");
+ }
}
public void testMapTypesRaw()
diff --git a/src/test/java/com/fasterxml/jackson/databind/util/ArrayBuildersTest.java b/src/test/java/com/fasterxml/jackson/databind/util/ArrayBuildersTest.java
index a788e28..bae0285 100644
--- a/src/test/java/com/fasterxml/jackson/databind/util/ArrayBuildersTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/util/ArrayBuildersTest.java
@@ -1,12 +1,22 @@
package com.fasterxml.jackson.databind.util;
+import java.util.Arrays;
+import java.util.HashSet;
+
import org.junit.Assert;
import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.util.ArrayBuilders.BooleanBuilder;
+import com.fasterxml.jackson.databind.util.ArrayBuilders.ByteBuilder;
+import com.fasterxml.jackson.databind.util.ArrayBuilders.DoubleBuilder;
+import com.fasterxml.jackson.databind.util.ArrayBuilders.FloatBuilder;
+import com.fasterxml.jackson.databind.util.ArrayBuilders.IntBuilder;
+import com.fasterxml.jackson.databind.util.ArrayBuilders.LongBuilder;
+import com.fasterxml.jackson.databind.util.ArrayBuilders.ShortBuilder;
public class ArrayBuildersTest extends BaseMapTest
{
- // Test for [Issue#157]
+ // [databind#157]
public void testInsertInListNoDup()
{
String [] arr = new String[]{"me", "you", "him"};
@@ -24,4 +34,57 @@
newarr = ArrayBuilders.insertInListNoDup(arr, "foobar");
Assert.assertArrayEquals(new String[]{"foobar", "me", "you", "him"}, newarr);
}
+
+ public void testBuilderAccess()
+ {
+ ArrayBuilders builders = new ArrayBuilders();
+
+ BooleanBuilder bb = builders.getBooleanBuilder();
+ assertNotNull(bb);
+ assertSame(bb, builders.getBooleanBuilder());
+
+ ByteBuilder b2 = builders.getByteBuilder();
+ assertNotNull(b2);
+ assertSame(b2, builders.getByteBuilder());
+
+ ShortBuilder sb = builders.getShortBuilder();
+ assertNotNull(sb);
+ assertSame(sb, builders.getShortBuilder());
+
+ IntBuilder ib = builders.getIntBuilder();
+ assertNotNull(ib);
+ assertSame(ib, builders.getIntBuilder());
+
+ LongBuilder lb = builders.getLongBuilder();
+ assertNotNull(lb);
+ assertSame(lb, builders.getLongBuilder());
+
+ FloatBuilder fb = builders.getFloatBuilder();
+ assertNotNull(fb);
+ assertSame(fb, builders.getFloatBuilder());
+
+ DoubleBuilder db = builders.getDoubleBuilder();
+ assertNotNull(db);
+ assertSame(db, builders.getDoubleBuilder());
+ }
+
+ public void testArrayComparator()
+ {
+ final int[] INT3 = new int[] { 3, 4, 5 };
+ Object comp = ArrayBuilders.getArrayComparator(INT3);
+ assertFalse(comp.equals(null));
+ assertTrue(comp.equals(INT3));
+ assertTrue(comp.equals(new int[] { 3, 4, 5 }));
+ assertFalse(comp.equals(new int[] { 5 }));
+ assertFalse(comp.equals(new int[] { 3, 4 }));
+ assertFalse(comp.equals(new int[] { 3, 5, 4 }));
+ assertFalse(comp.equals(new int[] { 3, 4, 5, 6 }));
+ }
+
+ public void testArraySet()
+ {
+ HashSet<String> set = ArrayBuilders.arrayToSet(new String[] { "foo", "bar" });
+ assertEquals(2, set.size());
+ assertEquals(new HashSet<String>(Arrays.asList("bar", "foo")), set);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/util/BeanUtilTest.java b/src/test/java/com/fasterxml/jackson/databind/util/BeanUtilTest.java
new file mode 100644
index 0000000..afd1732
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/util/BeanUtilTest.java
@@ -0,0 +1,147 @@
+package com.fasterxml.jackson.databind.util;
+
+import java.util.*;
+import java.util.concurrent.atomic.AtomicReference;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+
+public class BeanUtilTest extends BaseMapTest
+{
+ static class IsGetters {
+ public boolean isPrimitive() { return false; }
+ public Boolean isWrapper() { return false; }
+ public String isNotGetter() { return null; }
+ public boolean is() { return false; }
+ }
+
+ static class Getters {
+ public String getCallbacks() { return null; }
+ public String getMetaClass() { return null; }
+ public boolean get() { return false; }
+ }
+
+ static class Setters {
+ public void setFoo() { }
+ public void notSetter() { }
+ public void set() { }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ public void testNameMangle()
+ {
+ assertEquals("foo", BeanUtil.legacyManglePropertyName("getFoo", 3));
+ assertEquals("foo", BeanUtil.stdManglePropertyName("getFoo", 3));
+
+ assertEquals("url", BeanUtil.legacyManglePropertyName("getURL", 3));
+ assertEquals("URL", BeanUtil.stdManglePropertyName("getURL", 3));
+ }
+
+ public void testGetDefaultValue()
+ {
+ TypeFactory tf = TypeFactory.defaultInstance();
+ // For collection/array/Map types, should give `NOT_EMPTY`:
+ assertEquals(JsonInclude.Include.NON_EMPTY,
+ BeanUtil.getDefaultValue(tf.constructType(Map.class)));
+ assertEquals(JsonInclude.Include.NON_EMPTY,
+ BeanUtil.getDefaultValue(tf.constructType(List.class)));
+ assertEquals(JsonInclude.Include.NON_EMPTY,
+ BeanUtil.getDefaultValue(tf.constructType(Object[].class)));
+ // as well as ReferenceTypes, String
+ assertEquals(JsonInclude.Include.NON_EMPTY,
+ BeanUtil.getDefaultValue(tf.constructType(AtomicReference.class)));
+ assertEquals("",
+ BeanUtil.getDefaultValue(tf.constructType(String.class)));
+ // primitive/wrappers have others
+ assertEquals(Integer.valueOf(0),
+ BeanUtil.getDefaultValue(tf.constructType(Integer.class)));
+
+
+ // but POJOs have no real default
+ assertNull(BeanUtil.getDefaultValue(tf.constructType(getClass())));
+ }
+
+ public void testIsGetter() throws Exception
+ {
+ _testIsGetter("isPrimitive", "primitive");
+ _testIsGetter("isWrapper", "wrapper");
+ _testIsGetter("isNotGetter", null);
+ _testIsGetter("is", null);
+ }
+
+ public void testOkNameForGetter() throws Exception
+ {
+ // mostly chosen to exercise groovy exclusion
+ _testOkNameForGetter("getCallbacks", "callbacks");
+ _testOkNameForGetter("getMetaClass", "metaClass");
+ _testOkNameForGetter("get", null);
+ }
+
+ public void testOkNameForSetter() throws Exception
+ {
+ _testOkNameForSetter("setFoo", "foo");
+ _testOkNameForSetter("notSetter", null);
+ _testOkNameForSetter("set", null);
+ }
+
+ /*
+ /**********************************************************
+ /* Helper methods
+ /**********************************************************
+ */
+
+ private void _testIsGetter(String name, String expName) throws Exception {
+ _testIsGetter(name, expName, true);
+ _testIsGetter(name, expName, false);
+ }
+
+ private void _testIsGetter(String name, String expName, boolean useStd) throws Exception
+ {
+ AnnotatedMethod m = _method(IsGetters.class, name);
+ if (expName == null) {
+ assertNull(BeanUtil.okNameForIsGetter(m, name, useStd));
+ } else {
+ assertEquals(expName, BeanUtil.okNameForIsGetter(m, name, useStd));
+ }
+ }
+
+ private void _testOkNameForGetter(String name, String expName) throws Exception {
+ _testOkNameForGetter(name, expName, true);
+ _testOkNameForGetter(name, expName, false);
+ }
+
+ private void _testOkNameForGetter(String name, String expName, boolean useStd) throws Exception {
+ AnnotatedMethod m = _method(Getters.class, name);
+ if (expName == null) {
+ assertNull(BeanUtil.okNameForGetter(m, useStd));
+ } else {
+ assertEquals(expName, BeanUtil.okNameForGetter(m, useStd));
+ }
+ }
+
+ private void _testOkNameForSetter(String name, String expName) throws Exception {
+ _testOkNameForSetter(name, expName, true);
+ _testOkNameForSetter(name, expName, false);
+ }
+
+ @SuppressWarnings("deprecation")
+ private void _testOkNameForSetter(String name, String expName, boolean useStd) throws Exception {
+ AnnotatedMethod m = _method(Setters.class, name);
+ if (expName == null) {
+ assertNull(BeanUtil.okNameForSetter(m, useStd));
+ } else {
+ assertEquals(expName, BeanUtil.okNameForSetter(m, useStd));
+ }
+ }
+
+ private AnnotatedMethod _method(Class<?> cls, String name, Class<?>...parameterTypes) throws Exception {
+ return new AnnotatedMethod(null, cls.getMethod(name, parameterTypes), null, null);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/util/ByteBufferUtilsTest.java b/src/test/java/com/fasterxml/jackson/databind/util/ByteBufferUtilsTest.java
new file mode 100644
index 0000000..da66aa4
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/util/ByteBufferUtilsTest.java
@@ -0,0 +1,28 @@
+package com.fasterxml.jackson.databind.util;
+
+import java.nio.ByteBuffer;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+
+public class ByteBufferUtilsTest extends BaseMapTest
+{
+ public void testByteBufferInput() throws Exception {
+ byte[] input = new byte[] { 1, 2, 3 };
+ ByteBufferBackedInputStream wrapped = new ByteBufferBackedInputStream(ByteBuffer.wrap(input));
+ assertEquals(3, wrapped.available());
+ assertEquals(1, wrapped.read());
+ byte[] buffer = new byte[10];
+ assertEquals(2, wrapped.read(buffer, 0, 5));
+ wrapped.close();
+ }
+
+ public void testByteBufferOutput() throws Exception {
+ ByteBuffer b = ByteBuffer.wrap(new byte[10]);
+ ByteBufferBackedOutputStream wrappedOut = new ByteBufferBackedOutputStream(b);
+ wrappedOut.write(1);
+ wrappedOut.write(new byte[] { 2, 3 });
+ assertEquals(3, b.position());
+ assertEquals(7, b.remaining());
+ wrappedOut.close();
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/util/TestClassUtil.java b/src/test/java/com/fasterxml/jackson/databind/util/ClassUtilTest.java
similarity index 82%
rename from src/test/java/com/fasterxml/jackson/databind/util/TestClassUtil.java
rename to src/test/java/com/fasterxml/jackson/databind/util/ClassUtilTest.java
index b02f6ac..b6e4c54 100644
--- a/src/test/java/com/fasterxml/jackson/databind/util/TestClassUtil.java
+++ b/src/test/java/com/fasterxml/jackson/databind/util/ClassUtilTest.java
@@ -5,8 +5,7 @@
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.util.ClassUtil;
-public class TestClassUtil
- extends BaseMapTest
+public class ClassUtilTest extends BaseMapTest
{
/*
/**********************************************************
@@ -91,13 +90,6 @@
assertSame(e, e2);
}
- try {
- ClassUtil.throwRootCause(e);
- fail("Shouldn't get this far");
- } catch (Exception eAct) {
- assertSame(e, eAct);
- }
-
Error err = new Error();
try {
ClassUtil.throwAsIAE(err);
@@ -137,7 +129,7 @@
}
}
- public void testPrimiteDefaultValue()
+ public void testPrimitiveDefaultValue()
{
assertEquals(Integer.valueOf(0), ClassUtil.defaultValue(Integer.TYPE));
assertEquals(Long.valueOf(0L), ClassUtil.defaultValue(Long.TYPE));
@@ -148,6 +140,8 @@
assertEquals(Double.valueOf(0.0), ClassUtil.defaultValue(Double.TYPE));
assertEquals(Float.valueOf(0.0f), ClassUtil.defaultValue(Float.TYPE));
+ assertEquals(Boolean.FALSE, ClassUtil.defaultValue(Boolean.TYPE));
+
try {
ClassUtil.defaultValue(String.class);
} catch (IllegalArgumentException e) {
@@ -155,28 +149,48 @@
}
}
- public void testPrimiteWrapperType()
+ public void testPrimitiveWrapperType()
{
+ assertEquals(Byte.class, ClassUtil.wrapperType(Byte.TYPE));
+ assertEquals(Short.class, ClassUtil.wrapperType(Short.TYPE));
+ assertEquals(Character.class, ClassUtil.wrapperType(Character.TYPE));
assertEquals(Integer.class, ClassUtil.wrapperType(Integer.TYPE));
assertEquals(Long.class, ClassUtil.wrapperType(Long.TYPE));
- assertEquals(Character.class, ClassUtil.wrapperType(Character.TYPE));
- assertEquals(Short.class, ClassUtil.wrapperType(Short.TYPE));
- assertEquals(Byte.class, ClassUtil.wrapperType(Byte.TYPE));
assertEquals(Double.class, ClassUtil.wrapperType(Double.TYPE));
assertEquals(Float.class, ClassUtil.wrapperType(Float.TYPE));
+ assertEquals(Boolean.class, ClassUtil.wrapperType(Boolean.TYPE));
+
try {
ClassUtil.wrapperType(String.class);
+ fail("Should not pass");
} catch (IllegalArgumentException e) {
verifyException(e, "String is not a primitive type");
}
}
+ public void testWrapperToPrimitiveType()
+ {
+ assertEquals(Integer.TYPE, ClassUtil.primitiveType(Integer.class));
+ assertEquals(Long.TYPE, ClassUtil.primitiveType(Long.class));
+ assertEquals(Character.TYPE, ClassUtil.primitiveType(Character.class));
+ assertEquals(Short.TYPE, ClassUtil.primitiveType(Short.class));
+ assertEquals(Byte.TYPE, ClassUtil.primitiveType(Byte.class));
+ assertEquals(Float.TYPE, ClassUtil.primitiveType(Float.class));
+ assertEquals(Double.TYPE, ClassUtil.primitiveType(Double.class));
+ assertEquals(Boolean.TYPE, ClassUtil.primitiveType(Boolean.class));
+
+ assertNull(ClassUtil.primitiveType(String.class));
+ }
+
public void testFindEnumType()
{
assertEquals(TestEnum.class, ClassUtil.findEnumType(TestEnum.A));
+ // different codepaths for empty and non-empty EnumSets...
assertEquals(TestEnum.class, ClassUtil.findEnumType(EnumSet.allOf(TestEnum.class)));
+ assertEquals(TestEnum.class, ClassUtil.findEnumType(EnumSet.noneOf(TestEnum.class)));
+
assertEquals(TestEnum.class, ClassUtil.findEnumType(new EnumMap<TestEnum,Integer>(TestEnum.class)));
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/util/CompactStringObjectMapTest.java b/src/test/java/com/fasterxml/jackson/databind/util/CompactStringObjectMapTest.java
new file mode 100644
index 0000000..325d2f2
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/util/CompactStringObjectMapTest.java
@@ -0,0 +1,28 @@
+package com.fasterxml.jackson.databind.util;
+
+import java.util.*;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+
+public class CompactStringObjectMapTest extends BaseMapTest
+{
+ public void testBig()
+ {
+ Map<String,String> all = new LinkedHashMap<>();
+ for (int i = 0; i < 1000; ++i) {
+ String key = "key"+i;
+ all.put(key, key);
+ }
+ CompactStringObjectMap map = CompactStringObjectMap.construct(all);
+ assertEquals(1000, map.keys().size());
+
+ for (String key : all.keySet()) {
+ assertEquals(key, map.find(key));
+ }
+
+ // and then bogus empty keys
+ assertNull(map.find("key1000"));
+ assertNull(map.find("keyXXX"));
+ assertNull(map.find(""));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/util/EnumValuesTest.java b/src/test/java/com/fasterxml/jackson/databind/util/EnumValuesTest.java
new file mode 100644
index 0000000..ccc3ea2
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/util/EnumValuesTest.java
@@ -0,0 +1,66 @@
+package com.fasterxml.jackson.databind.util;
+
+import java.util.List;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationConfig;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+public class EnumValuesTest extends BaseMapTest
+{
+ enum ABC {
+ A("A"),
+ B("b"),
+ C("C");
+
+ private final String desc;
+
+ private ABC(String d) { desc = d; }
+
+ @Override
+ public String toString() { return desc; }
+ }
+
+ final ObjectMapper MAPPER = new ObjectMapper();
+
+ @SuppressWarnings("unchecked")
+ public void testConstructFromName() {
+ SerializationConfig cfg = MAPPER.getSerializationConfig()
+ .without(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
+ Class<Enum<?>> enumClass = (Class<Enum<?>>)(Class<?>) ABC.class;
+ EnumValues values = EnumValues.construct(cfg, enumClass);
+ assertEquals("A", values.serializedValueFor(ABC.A).toString());
+ assertEquals("B", values.serializedValueFor(ABC.B).toString());
+ assertEquals("C", values.serializedValueFor(ABC.C).toString());
+ assertEquals(3, values.values().size());
+ assertEquals(3, values.internalMap().size());
+ }
+
+ @SuppressWarnings("unchecked")
+ public void testConstructWithToString() {
+ SerializationConfig cfg = MAPPER.getSerializationConfig()
+ .with(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
+ Class<Enum<?>> enumClass = (Class<Enum<?>>)(Class<?>) ABC.class;
+ EnumValues values = EnumValues.construct(cfg, enumClass);
+ assertEquals("A", values.serializedValueFor(ABC.A).toString());
+ assertEquals("b", values.serializedValueFor(ABC.B).toString());
+ assertEquals("C", values.serializedValueFor(ABC.C).toString());
+ assertEquals(3, values.values().size());
+ assertEquals(3, values.internalMap().size());
+ }
+
+ public void testEnumResolver()
+ {
+ EnumResolver enumRes = EnumResolver.constructUnsafeUsingToString(ABC.class, null);
+ assertEquals(ABC.B, enumRes.getEnum(1));
+ assertNull(enumRes.getEnum(-1));
+ assertNull(enumRes.getEnum(3));
+ assertEquals(2, enumRes.lastValidIndex());
+ List<Enum<?>> enums = enumRes.getEnums();
+ assertEquals(3, enums.size());
+ assertEquals(ABC.A, enums.get(0));
+ assertEquals(ABC.B, enums.get(1));
+ assertEquals(ABC.C, enums.get(2));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/util/JsonParserSequenceTest.java b/src/test/java/com/fasterxml/jackson/databind/util/JsonParserSequenceTest.java
new file mode 100644
index 0000000..b7b6228
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/util/JsonParserSequenceTest.java
@@ -0,0 +1,45 @@
+package com.fasterxml.jackson.databind.util;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonToken;
+import com.fasterxml.jackson.core.util.JsonParserSequence;
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class JsonParserSequenceTest extends BaseMapTest {
+
+ private final ObjectMapper MAPPER = objectMapper();
+
+ /**
+ * Verifies fix for [core#372]
+ */
+ @SuppressWarnings("resource")
+ public void testJsonParserSequenceOverridesSkipChildren() throws Exception
+ {
+ // Create parser from TokenBuffer containing an incomplete JSON object
+ TokenBuffer buf1 = new TokenBuffer(MAPPER, false);
+ buf1.writeStartObject();
+ buf1.writeFieldName("foo");
+ buf1.writeStartObject();
+ JsonParser parser1 = buf1.asParser();
+
+ // Create parser from second TokenBuffer that completes the object started by the first buffer
+ TokenBuffer buf2 = new TokenBuffer(MAPPER, false);
+ buf2.writeEndObject();
+ buf2.writeEndObject();
+ JsonParser parser2 = buf2.asParser();
+
+ // Create sequence of both parsers and verify tokens
+ JsonParser parserSequence = JsonParserSequence.createFlattened(false, parser1, parser2);
+ assertToken(JsonToken.START_OBJECT, parserSequence.nextToken());
+ assertToken(JsonToken.FIELD_NAME, parserSequence.nextToken());
+ assertToken(JsonToken.START_OBJECT, parserSequence.nextToken());
+
+ // Skip children of current token. JsonParserSequence's overridden version should switch to the next parser
+ // in the sequence
+ parserSequence.skipChildren();
+
+ // Verify last token
+ assertToken(JsonToken.END_OBJECT, parserSequence.nextToken());
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/util/NameTransformerTest.java b/src/test/java/com/fasterxml/jackson/databind/util/NameTransformerTest.java
new file mode 100644
index 0000000..648c8a9
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/util/NameTransformerTest.java
@@ -0,0 +1,23 @@
+package com.fasterxml.jackson.databind.util;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+
+public class NameTransformerTest extends BaseMapTest
+{
+ public void testSimpleTransformer() throws Exception
+ {
+ NameTransformer xfer;
+
+ xfer = NameTransformer.simpleTransformer("a", null);
+ assertEquals("aFoo", xfer.transform("Foo"));
+ assertEquals("Foo", xfer.reverse("aFoo"));
+
+ xfer = NameTransformer.simpleTransformer(null, "++");
+ assertEquals("foo++", xfer.transform("foo"));
+ assertEquals("foo", xfer.reverse("foo++"));
+
+ xfer = NameTransformer.simpleTransformer("(", ")");
+ assertEquals("(foo)", xfer.transform("foo"));
+ assertEquals("foo", xfer.reverse("(foo)"));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/util/RawValueTest.java b/src/test/java/com/fasterxml/jackson/databind/util/RawValueTest.java
new file mode 100644
index 0000000..4d652b0
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/util/RawValueTest.java
@@ -0,0 +1,24 @@
+package com.fasterxml.jackson.databind.util;
+
+import com.fasterxml.jackson.databind.BaseMapTest;
+import com.fasterxml.jackson.databind.JsonSerializable;
+
+public class RawValueTest extends BaseMapTest
+{
+ public void testEquality()
+ {
+ RawValue raw1 = new RawValue("foo");
+ RawValue raw1b = new RawValue("foo");
+ RawValue raw2 = new RawValue("bar");
+
+ assertTrue(raw1.equals(raw1));
+ assertTrue(raw1.equals(raw1b));
+
+ assertFalse(raw1.equals(raw2));
+ assertFalse(raw1.equals(null));
+
+ assertFalse(new RawValue((JsonSerializable) null).equals(raw1));
+
+ assertNotNull(raw1.toString());
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/util/TestTokenBuffer.java b/src/test/java/com/fasterxml/jackson/databind/util/TestTokenBuffer.java
index 477301e..58b914a 100644
--- a/src/test/java/com/fasterxml/jackson/databind/util/TestTokenBuffer.java
+++ b/src/test/java/com/fasterxml/jackson/databind/util/TestTokenBuffer.java
@@ -1,30 +1,56 @@
package com.fasterxml.jackson.databind.util;
import java.io.*;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import java.util.UUID;
import com.fasterxml.jackson.core.*;
+import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.core.util.JsonParserSequence;
-import com.fasterxml.jackson.databind.BaseMapTest;
-import com.fasterxml.jackson.databind.ObjectMapper;
+
+import com.fasterxml.jackson.databind.*;
public class TestTokenBuffer extends BaseMapTest
{
private final ObjectMapper MAPPER = objectMapper();
-
+
/*
/**********************************************************
/* Basic TokenBuffer tests
/**********************************************************
*/
-
+
+ public void testBasicConfig() throws IOException
+ {
+ TokenBuffer buf;
+
+ buf = new TokenBuffer(MAPPER, false);
+ assertEquals(MAPPER.version(), buf.version());
+ assertSame(MAPPER, buf.getCodec());
+ assertNotNull(buf.getOutputContext());
+ assertFalse(buf.isClosed());
+
+ buf.setCodec(null);
+ assertNull(buf.getCodec());
+
+ assertFalse(buf.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
+ buf.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
+ assertTrue(buf.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
+ buf.disable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
+ assertFalse(buf.isEnabled(JsonGenerator.Feature.ESCAPE_NON_ASCII));
+
+ buf.close();
+ assertTrue(buf.isClosed());
+ }
+
/**
* Test writing of individual simple values
*/
public void testSimpleWrites() throws IOException
{
TokenBuffer buf = new TokenBuffer(null, false); // no ObjectCodec
-
+
// First, with empty buffer
JsonParser p = buf.asParser();
assertNull(p.getCurrentToken());
@@ -34,7 +60,6 @@
// Then with simple text
buf.writeString("abc");
- // First, simple text
p = buf.asParser();
assertNull(p.getCurrentToken());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
@@ -54,6 +79,48 @@
buf.close();
}
+ // For 2.9, explicit "isNaN" check
+ public void testSimpleNumberWrites() throws IOException
+ {
+ TokenBuffer buf = new TokenBuffer(null, false);
+
+ double[] values1 = new double[] {
+ 0.25, Double.NaN, -2.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY
+ };
+ float[] values2 = new float[] {
+ Float.NEGATIVE_INFINITY,
+ 0.25f,
+ Float.POSITIVE_INFINITY
+ };
+
+ for (double v : values1) {
+ buf.writeNumber(v);
+ }
+ for (float v : values2) {
+ buf.writeNumber(v);
+ }
+
+ JsonParser p = buf.asParser();
+ assertNull(p.getCurrentToken());
+
+ for (double v : values1) {
+ assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
+ double actual = p.getDoubleValue();
+ boolean expNan = Double.isNaN(v) || Double.isInfinite(v);
+ assertEquals(expNan, p.isNaN());
+ assertEquals(0, Double.compare(v, actual));
+ }
+ for (float v : values2) {
+ assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
+ float actual = p.getFloatValue();
+ boolean expNan = Float.isNaN(v) || Float.isInfinite(v);
+ assertEquals(expNan, p.isNaN());
+ assertEquals(0, Float.compare(v, actual));
+ }
+ p.close();
+ buf.close();
+ }
+
public void testParentContext() throws IOException
{
TokenBuffer buf = new TokenBuffer(null, false); // no ObjectCodec
@@ -200,6 +267,11 @@
verifyJsonSpecSampleDoc(tb.asParser(), true);
tb.close();
p.close();
+
+
+ // 19-Oct-2016, tatu: Just for fun, trigger `toString()` for code coverage
+ String desc = tb.toString();
+ assertNotNull(desc);
}
public void testAppend() throws IOException
@@ -382,6 +454,39 @@
buf.close();
}
+ public void testBasicSerialize() throws IOException
+ {
+ TokenBuffer buf;
+
+ // let's see how empty works...
+ buf = new TokenBuffer(MAPPER, false);
+ assertEquals("", MAPPER.writeValueAsString(buf));
+ buf.close();
+
+ buf = new TokenBuffer(MAPPER, false);
+ buf.writeStartArray();
+ buf.writeBoolean(true);
+ buf.writeBoolean(false);
+ long l = 1L + Integer.MAX_VALUE;
+ buf.writeNumber(l);
+ buf.writeNumber((short) 4);
+ buf.writeNumber(0.5);
+ buf.writeEndArray();
+ assertEquals(aposToQuotes("[true,false,"+l+",4,0.5]"), MAPPER.writeValueAsString(buf));
+ buf.close();
+
+ buf = new TokenBuffer(MAPPER, false);
+ buf.writeStartObject();
+ buf.writeFieldName(new SerializedString("foo"));
+ buf.writeNull();
+ buf.writeFieldName("bar");
+ buf.writeNumber(BigInteger.valueOf(123));
+ buf.writeFieldName("dec");
+ buf.writeNumber(BigDecimal.valueOf(5).movePointLeft(2));
+ assertEquals(aposToQuotes("{'foo':null,'bar':123,'dec':0.05}"), MAPPER.writeValueAsString(buf));
+ buf.close();
+ }
+
/*
/**********************************************************
/* Tests to verify interaction of TokenBuffer and JsonParserSequence
diff --git a/src/test/java/com/fasterxml/jackson/databind/views/DefaultViewTest.java b/src/test/java/com/fasterxml/jackson/databind/views/DefaultViewTest.java
new file mode 100644
index 0000000..34e8c08
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/databind/views/DefaultViewTest.java
@@ -0,0 +1,71 @@
+package com.fasterxml.jackson.databind.views;
+
+import java.io.IOException;
+
+import com.fasterxml.jackson.annotation.*;
+
+import com.fasterxml.jackson.databind.*;
+
+// for [databind#507], supporting default views
+public class DefaultViewTest extends BaseMapTest
+{
+ // Classes that represent views
+ static class ViewA { }
+ static class ViewAA extends ViewA { }
+ static class ViewB { }
+ static class ViewBB extends ViewB { }
+
+ @JsonView(ViewA.class)
+ @JsonPropertyOrder({ "a", "b" })
+ static class Defaulting {
+ public int a = 3;
+
+ @JsonView(ViewB.class)
+ public int b = 5;
+ }
+
+ /*
+ /**********************************************************
+ /* Unit tests
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testDeserialization() throws IOException
+ {
+ final String JSON = aposToQuotes("{'a':1,'b':2}");
+
+ // first: no views:
+ Defaulting result = MAPPER.readerFor(Defaulting.class)
+ .readValue(JSON);
+ assertEquals(result.a, 1);
+ assertEquals(result.b, 2);
+
+ // Then views; first A, then B(B)
+ result = MAPPER.readerFor(Defaulting.class)
+ .withView(ViewA.class)
+ .readValue(JSON);
+ assertEquals(result.a, 1);
+ assertEquals(result.b, 5);
+
+ result = MAPPER.readerFor(Defaulting.class)
+ .withView(ViewBB.class)
+ .readValue(JSON);
+ assertEquals(result.a, 3);
+ assertEquals(result.b, 2);
+ }
+
+ public void testSerialization() throws IOException
+ {
+ assertEquals(aposToQuotes("{'a':3,'b':5}"),
+ MAPPER.writeValueAsString(new Defaulting()));
+
+ assertEquals(aposToQuotes("{'a':3}"),
+ MAPPER.writerWithView(ViewA.class)
+ .writeValueAsString(new Defaulting()));
+ assertEquals(aposToQuotes("{'b':5}"),
+ MAPPER.writerWithView(ViewB.class)
+ .writeValueAsString(new Defaulting()));
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/databind/views/TestViewDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/views/TestViewDeserialization.java
index 5c28728..fd81d13 100644
--- a/src/test/java/com/fasterxml/jackson/databind/views/TestViewDeserialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/views/TestViewDeserialization.java
@@ -1,17 +1,13 @@
package com.fasterxml.jackson.databind.views;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.*;
public class TestViewDeserialization extends BaseMapTest
{
- /*
- /**********************************************************
- /* Helper types
- /**********************************************************
- */
-
// Classes that represent views
static class ViewA { }
static class ViewAA extends ViewA { }
@@ -40,6 +36,23 @@
public int b;
}
+ static class ViewsAndCreatorBean
+ {
+ @JsonView(ViewA.class)
+ public int a;
+
+ @JsonView(ViewB.class)
+ public int b;
+
+ @JsonCreator
+ public ViewsAndCreatorBean(@JsonProperty("a") int a,
+ @JsonProperty("b") int b)
+ {
+ this.a = a;
+ this.b = b;
+ }
+ }
+
/*
/************************************************************************
/* Tests
@@ -101,4 +114,28 @@
assertEquals(0, bean.a);
assertEquals(2, bean.b);
}
+
+ public void testWithCreatorAndViews() throws Exception
+ {
+ ViewsAndCreatorBean result;
+
+ result = mapper.readerFor(ViewsAndCreatorBean.class)
+ .withView(ViewA.class)
+ .readValue(aposToQuotes("{'a':1,'b':2}"));
+ assertEquals(1, result.a);
+ assertEquals(0, result.b);
+
+ result = mapper.readerFor(ViewsAndCreatorBean.class)
+ .withView(ViewB.class)
+ .readValue(aposToQuotes("{'a':1,'b':2}"));
+ assertEquals(0, result.a);
+ assertEquals(2, result.b);
+
+ // and actually... fine to skip incompatible stuff too
+ result = mapper.readerFor(ViewsAndCreatorBean.class)
+ .withView(ViewB.class)
+ .readValue(aposToQuotes("{'a':[ 1, 23, { } ],'b':2}"));
+ assertEquals(0, result.a);
+ assertEquals(2, result.b);
+ }
}
diff --git a/src/test/java/com/fasterxml/jackson/databind/views/TestViewSerialization.java b/src/test/java/com/fasterxml/jackson/databind/views/TestViewSerialization.java
index 8c17034..79dfee0 100644
--- a/src/test/java/com/fasterxml/jackson/databind/views/TestViewSerialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/views/TestViewSerialization.java
@@ -14,12 +14,6 @@
public class TestViewSerialization
extends BaseMapTest
{
- /*
- /**********************************************************
- /* Helper types
- /**********************************************************
- */
-
// Classes that represent views
static class ViewA { }
static class ViewAA extends ViewA { }
@@ -68,58 +62,63 @@
public String value = "x";
}
- // [JACKSON-868]
public static class WebView { }
public static class OtherView { }
public static class Foo {
@JsonView(WebView.class)
public int getFoo() { return 3; }
}
-
+
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
-
+
+ private final ObjectMapper MAPPER = objectMapper();
+
@SuppressWarnings("unchecked")
public void testSimple() throws IOException
{
StringWriter sw = new StringWriter();
- ObjectMapper mapper = new ObjectMapper();
// Ok, first, using no view whatsoever; all 3
Bean bean = new Bean();
- Map<String,Object> map = writeAndMap(mapper, bean);
+ Map<String,Object> map = writeAndMap(MAPPER, bean);
assertEquals(3, map.size());
// Then with "ViewA", just one property
sw = new StringWriter();
- mapper.writerWithView(ViewA.class).writeValue(sw, bean);
- map = mapper.readValue(sw.toString(), Map.class);
+ MAPPER.writerWithView(ViewA.class).writeValue(sw, bean);
+ map = MAPPER.readValue(sw.toString(), Map.class);
assertEquals(1, map.size());
assertEquals("1", map.get("a"));
// "ViewAA", 2 properties
sw = new StringWriter();
- mapper.writerWithView(ViewAA.class).writeValue(sw, bean);
- map = mapper.readValue(sw.toString(), Map.class);
+ MAPPER.writerWithView(ViewAA.class).writeValue(sw, bean);
+ map = MAPPER.readValue(sw.toString(), Map.class);
assertEquals(2, map.size());
assertEquals("1", map.get("a"));
assertEquals("2", map.get("aa"));
// "ViewB", 2 prop2
- String json = mapper.writerWithView(ViewB.class).writeValueAsString(bean);
- map = mapper.readValue(json, Map.class);
+ String json = MAPPER.writerWithView(ViewB.class).writeValueAsString(bean);
+ map = MAPPER.readValue(json, Map.class);
assertEquals(2, map.size());
assertEquals("2", map.get("aa"));
assertEquals("3", map.get("b"));
// and "ViewBB", 2 as well
- json = mapper.writerWithView(ViewBB.class).writeValueAsString(bean);
- map = mapper.readValue(json, Map.class);
+ json = MAPPER.writerWithView(ViewBB.class).writeValueAsString(bean);
+ map = MAPPER.readValue(json, Map.class);
assertEquals(2, map.size());
assertEquals("2", map.get("aa"));
assertEquals("3", map.get("b"));
+
+ // and finally, without view.
+ json = MAPPER.writerWithView(null).writeValueAsString(bean);
+ map = MAPPER.readValue(json, Map.class);
+ assertEquals(3, map.size());
}
/**
@@ -132,25 +131,31 @@
public void testDefaultExclusion() throws IOException
{
MixedBean bean = new MixedBean();
- StringWriter sw = new StringWriter();
- ObjectMapper mapper = new ObjectMapper();
// default setting: both fields will get included
- mapper.writerWithView(ViewA.class).writeValue(sw, bean);
- Map<String,Object> map = mapper.readValue(sw.toString(), Map.class);
+ String json = MAPPER.writerWithView(ViewA.class).writeValueAsString(bean);
+ Map<String,Object> map = MAPPER.readValue(json, Map.class);
assertEquals(2, map.size());
assertEquals("1", map.get("a"));
assertEquals("2", map.get("b"));
// but can also change (but not necessarily on the fly...)
- mapper = new ObjectMapper();
+ ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
+
// with this setting, only explicit inclusions count:
- String json = mapper.writerWithView(ViewA.class).writeValueAsString(bean);
+ json = mapper.writerWithView(ViewA.class).writeValueAsString(bean);
map = mapper.readValue(json, Map.class);
assertEquals(1, map.size());
assertEquals("1", map.get("a"));
assertNull(map.get("b"));
+
+ // but without view, view processing disabled:
+ json = mapper.writer().withView(null).writeValueAsString(bean);
+ map = mapper.readValue(json, Map.class);
+ assertEquals(2, map.size());
+ assertEquals("1", map.get("a"));
+ assertEquals("2", map.get("b"));
}
/**
@@ -159,15 +164,15 @@
*/
public void testImplicitAutoDetection() throws Exception
{
- assertEquals("{\"a\":1}", serializeAsString(new ImplicitBean()));
+ assertEquals("{\"a\":1}",
+ MAPPER.writeValueAsString(new ImplicitBean()));
}
public void testVisibility() throws Exception
{
- ObjectMapper mapper = new ObjectMapper();
VisibilityBean bean = new VisibilityBean();
// Without view setting, should only see "id"
- String json = mapper.writerWithView(Object.class).writeValueAsString(bean);
+ String json = MAPPER.writerWithView(Object.class).writeValueAsString(bean);
//json = mapper.writeValueAsString(bean);
assertEquals("{\"id\":\"id\"}", json);
}
diff --git a/src/test/java/com/fasterxml/jackson/failing/AnyPropSorting518Test.java b/src/test/java/com/fasterxml/jackson/failing/AnyPropSorting518Test.java
new file mode 100644
index 0000000..dbef399
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/failing/AnyPropSorting518Test.java
@@ -0,0 +1,49 @@
+package com.fasterxml.jackson.failing;
+
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.fasterxml.jackson.databind.*;
+
+public class AnyPropSorting518Test extends BaseMapTest
+{
+ @JsonPropertyOrder(alphabetic = true)
+ static class Bean
+ {
+ public int b;
+
+ protected Map<String,Object> extra = new HashMap<>();
+
+ public int a;
+
+ public Bean(int a, int b, Map<String,Object> x) {
+ this.a = a;
+ this.b = b;
+ extra = x;
+ }
+
+ @JsonAnyGetter
+ public Map<String,Object> getExtra() { return extra; }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testAnyBeanWithSort() throws Exception
+ {
+ Map<String,Object> extra = new LinkedHashMap<>();
+ extra.put("y", 4);
+ extra.put("x", 3);
+ String json = MAPPER.writeValueAsString(new Bean(1, 2, extra));
+ assertEquals(aposToQuotes("{'a':1,'b':2,'x':3,'y':4}"),
+ json);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/failing/AnySetter1035Test.java b/src/test/java/com/fasterxml/jackson/failing/AnySetter1035Test.java
deleted file mode 100644
index 451cab3..0000000
--- a/src/test/java/com/fasterxml/jackson/failing/AnySetter1035Test.java
+++ /dev/null
@@ -1,94 +0,0 @@
-package com.fasterxml.jackson.failing;
-
-import java.util.*;
-
-import com.fasterxml.jackson.annotation.*;
-
-import com.fasterxml.jackson.databind.*;
-
-/**
- * Test for [databind#1035], wherein key type of a `Map` not used with `@JsonAnySetter`
- * (value type is).
- */
-public class AnySetter1035Test extends BaseMapTest
-{
- static class MyGeneric<T>
- {
- private String staticallyMappedProperty;
- private Map<T, Integer> dynamicallyMappedProperties = new HashMap<T, Integer>();
-
- public String getStaticallyMappedProperty() {
- return staticallyMappedProperty;
- }
-
- @JsonAnySetter
- public void addDynamicallyMappedProperty(T key, int value) {
- dynamicallyMappedProperties.put(key, value);
- }
-
- public void setStaticallyMappedProperty(String staticallyMappedProperty) {
- this.staticallyMappedProperty = staticallyMappedProperty;
- }
-
- @JsonAnyGetter
- public Map<T, Integer> getDynamicallyMappedProperties() {
- return dynamicallyMappedProperties;
- }
- }
-
- static class MyWrapper
- {
- private MyGeneric<String> myStringGeneric;
- private MyGeneric<Integer> myIntegerGeneric;
-
- public MyGeneric<String> getMyStringGeneric() {
- return myStringGeneric;
- }
-
- public void setMyStringGeneric(MyGeneric<String> myStringGeneric) {
- this.myStringGeneric = myStringGeneric;
- }
-
- public MyGeneric<Integer> getMyIntegerGeneric() {
- return myIntegerGeneric;
- }
-
- public void setMyIntegerGeneric(MyGeneric<Integer> myIntegerGeneric) {
- this.myIntegerGeneric = myIntegerGeneric;
- }
- }
-
- public void testGenericAnySetter() throws Exception
- {
- ObjectMapper mapper = new ObjectMapper();
-
- Map<String, Integer> stringGenericMap = new HashMap<String, Integer>();
- stringGenericMap.put("testStringKey", 5);
- Map<Integer, Integer> integerGenericMap = new HashMap<Integer, Integer>();
- integerGenericMap.put(111, 6);
-
- MyWrapper deserialized = mapper.readValue(aposToQuotes(
- "{'myStringGeneric':{'staticallyMappedProperty':'Test','testStringKey':5},'myIntegerGeneric':{'staticallyMappedProperty':'Test2','111':6}}"
- ), MyWrapper.class);
- MyGeneric<String> stringGeneric = deserialized.getMyStringGeneric();
- MyGeneric<Integer> integerGeneric = deserialized.getMyIntegerGeneric();
-
- assertNotNull(stringGeneric);
- assertEquals(stringGeneric.getStaticallyMappedProperty(), "Test");
- for(Map.Entry<String, Integer> entry : stringGeneric.getDynamicallyMappedProperties().entrySet()) {
- assertTrue("A key in MyGeneric<String> is not an String.", entry.getKey() instanceof String);
- assertTrue("A value in MyGeneric<Integer> is not an Integer.", entry.getValue() instanceof Integer);
- }
- assertEquals(stringGeneric.getDynamicallyMappedProperties(), stringGenericMap);
-
- assertNotNull(integerGeneric);
- assertEquals(integerGeneric.getStaticallyMappedProperty(), "Test2");
- for(Map.Entry<Integer, Integer> entry : integerGeneric.getDynamicallyMappedProperties().entrySet()) {
- Object key = entry.getKey();
- assertEquals("A key in MyGeneric<Integer> is not an Integer.", Integer.class, key.getClass());
- Object value = entry.getValue();
- assertEquals("A value in MyGeneric<Integer> is not an Integer.", Integer.class, value.getClass());
- }
- assertEquals(integerGeneric.getDynamicallyMappedProperties(), integerGenericMap);
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/failing/BackReference1516Test.java b/src/test/java/com/fasterxml/jackson/failing/BackReference1516Test.java
index 95c4de7..23e298b 100644
--- a/src/test/java/com/fasterxml/jackson/failing/BackReference1516Test.java
+++ b/src/test/java/com/fasterxml/jackson/failing/BackReference1516Test.java
@@ -74,7 +74,7 @@
private final String PARENT_CHILD_JSON = aposToQuotes(
"{ 'id': 'abc',\n"+
" 'name': 'Bob',\n"+
-" 'child': { 'id': 'def', 'title':'Bert' }\n"+
+" 'child': { 'id': 'def', 'name':'Bert' }\n"+
"}");
public void testWithParentCreator() throws Exception
@@ -82,6 +82,8 @@
ParentWithCreator result = MAPPER.readValue(PARENT_CHILD_JSON,
ParentWithCreator.class);
assertNotNull(result);
+ assertNotNull(result.child);
+ assertSame(result, result.child.parent);
}
public void testWithParentNoCreator() throws Exception
@@ -89,5 +91,7 @@
ParentWithoutCreator result = MAPPER.readValue(PARENT_CHILD_JSON,
ParentWithoutCreator.class);
assertNotNull(result);
+ assertNotNull(result.child);
+ assertSame(result, result.child.parent);
}
}
diff --git a/src/test/java/com/fasterxml/jackson/failing/CollectionType1415Test.java b/src/test/java/com/fasterxml/jackson/failing/CollectionType1415Test.java
deleted file mode 100644
index dcf8718..0000000
--- a/src/test/java/com/fasterxml/jackson/failing/CollectionType1415Test.java
+++ /dev/null
@@ -1,61 +0,0 @@
-package com.fasterxml.jackson.failing;
-
-import java.util.*;
-
-import com.fasterxml.jackson.databind.BaseMapTest;
-import com.fasterxml.jackson.databind.JavaType;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.type.CollectionType;
-import com.fasterxml.jackson.databind.type.MapType;
-
-// for [databind#1415]
-public class CollectionType1415Test extends BaseMapTest
-{
- static abstract class LongList implements List<Long> { }
-
- static abstract class StringLongMap implements Map<String,Long> { }
-
- /*
- /**********************************************************
- /* Unit tests
- /**********************************************************
- */
-
- private final ObjectMapper MAPPER = new ObjectMapper();
-
- public void testExplicitCollectionType() throws Exception
- {
- JavaType t = MAPPER.getTypeFactory()
- .constructCollectionType(LongList.class, Long.class);
- assertEquals(LongList.class, t.getRawClass());
- assertEquals(Long.class, t.getContentType().getRawClass());
- }
-
- public void testImplicitCollectionType() throws Exception
- {
- JavaType t = MAPPER.getTypeFactory()
- .constructParametricType(List.class, Long.class);
- assertEquals(CollectionType.class, t.getClass());
- assertEquals(List.class, t.getRawClass());
- assertEquals(Long.class, t.getContentType().getRawClass());
- }
-
- public void testExplicitMapType() throws Exception
- {
- JavaType t = MAPPER.getTypeFactory()
- .constructMapType(StringLongMap.class,
- String.class, Long.class);
- assertEquals(StringLongMap.class, t.getRawClass());
- assertEquals(String.class, t.getKeyType().getRawClass());
- assertEquals(Long.class, t.getContentType().getRawClass());
- }
-
- public void testImplicitMapType() throws Exception
- {
- JavaType t = MAPPER.getTypeFactory()
- .constructParametricType(Map.class, Long.class, Boolean.class);
- assertEquals(MapType.class, t.getClass());
- assertEquals(Long.class, t.getKeyType().getRawClass());
- assertEquals(Boolean.class, t.getContentType().getRawClass());
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/failing/CreatorProperties1401Test.java b/src/test/java/com/fasterxml/jackson/failing/CreatorProperties1401Test.java
index 7671519..7b06004 100644
--- a/src/test/java/com/fasterxml/jackson/failing/CreatorProperties1401Test.java
+++ b/src/test/java/com/fasterxml/jackson/failing/CreatorProperties1401Test.java
@@ -4,8 +4,8 @@
import com.fasterxml.jackson.databind.*;
-// for [databind#1401]: should allow "Any Setter" to back up otherwise problematic
-// Creator properties?
+// for [databind#1401]: should allow "Any Setter" to back up otherwise
+// problematic Creator properties?
public class CreatorProperties1401Test extends BaseMapTest
{
// for [databind#1401]
diff --git a/src/test/java/com/fasterxml/jackson/failing/DefaultTypingOverride1391Test.java b/src/test/java/com/fasterxml/jackson/failing/DefaultTypingOverride1391Test.java
new file mode 100644
index 0000000..40d4a31
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/failing/DefaultTypingOverride1391Test.java
@@ -0,0 +1,29 @@
+package com.fasterxml.jackson.failing;
+
+import java.util.*;
+
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.databind.*;
+
+// for [databind#1391]: should allow disabling of default typing
+// via explicit {@link JsonTypeInfo}
+public class DefaultTypingOverride1391Test extends BaseMapTest
+{
+ static class ListWrapper {
+ /* 03-Oct-2016, tatu: This doesn't work because it applies to contents
+ * (elements), NOT the container. But there is no current mechanism
+ * to change that; need to add a new feature or properties in 2.9
+ */
+ @JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
+ public Collection<String> stuff = Collections.emptyList();
+ }
+
+ public void testCollectionWithOverride() throws Exception
+ {
+ final ObjectMapper mapper = new ObjectMapper()
+ .enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE,
+ "$type");
+ String json = mapper.writeValueAsString(new ListWrapper());
+ assertEquals(aposToQuotes("{'stuff':[]}"), json);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/failing/EnumDeserialization1626Test.java b/src/test/java/com/fasterxml/jackson/failing/EnumDeserialization1626Test.java
new file mode 100644
index 0000000..ef7625d
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/failing/EnumDeserialization1626Test.java
@@ -0,0 +1,87 @@
+package com.fasterxml.jackson.failing;
+
+import java.util.List;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.*;
+
+/**
+ * NOTE: not assumed to be actual bug -- real numbers are not coerced into
+ * Strings, and are instead assumed to always mean index numbers.
+ * But test retained in case there might be ways to improve support
+ * here: as is, one MUST use Creator method to resolve from number to
+ * enum.
+ */
+public class EnumDeserialization1626Test extends BaseMapTest
+{
+ static class JsonResponseEnvelope<T> {
+ @JsonProperty("d")
+ public T data;
+ }
+
+ static class ShippingMethodInfo {
+ @JsonProperty("typeId")
+ public int typeId;
+
+ @JsonProperty("value")
+ private ShippingMethods value;
+
+ @JsonProperty("coverage")
+ public int coverage;
+ }
+
+ enum ShippingMethods {
+ @JsonProperty("0")
+ SHIPPING_METHODS_UNSPECIFIED(0),
+
+ @JsonProperty("10")
+ SHIPPING_METHODS_FED_EX_PRIORITY_OVERNIGHT(10),
+
+ @JsonProperty("17")
+ SHIPPING_METHODS_FED_EX_1DAY_FREIGHT(17),
+ ;
+
+ private final int shippingMethodId;
+
+ ShippingMethods(final int shippingMethodId) {
+ this.shippingMethodId = shippingMethodId;
+ }
+
+ public int getShippingMethodId() {
+ return shippingMethodId;
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ protected final ObjectMapper MAPPER = new ObjectMapper();
+
+ // [databind#1626]
+ public void testSparseNumericEnum626() throws Exception
+ {
+ String jsonResponse =
+ "{\n" +
+ " \"d\": [\n" +
+ " {\n" +
+ " \"typeId\": 0,\n" +
+ // NOTE! Only real number fails; quoted-as-String is bound as expected
+ " \"value\": 17,\n" +
+ " \"coverage\": 1"+
+ " }\n" +
+ " ]\n" +
+ "}";
+
+ JsonResponseEnvelope<List<ShippingMethodInfo>> mappedResponse =
+ MAPPER.readValue(jsonResponse,
+ new TypeReference<JsonResponseEnvelope<List<ShippingMethodInfo>>>() { });
+ List<ShippingMethodInfo> shippingMethods = mappedResponse.data;
+
+ assertEquals(ShippingMethods.SHIPPING_METHODS_FED_EX_1DAY_FREIGHT,
+ shippingMethods.get(0).value);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/failing/KevinFail1410Test.java b/src/test/java/com/fasterxml/jackson/failing/KevinFail1410Test.java
new file mode 100644
index 0000000..8cba59c
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/failing/KevinFail1410Test.java
@@ -0,0 +1,79 @@
+package com.fasterxml.jackson.failing;
+
+import com.fasterxml.jackson.annotation.*;
+
+import com.fasterxml.jackson.databind.*;
+
+public class KevinFail1410Test extends BaseMapTest
+{
+ enum EnvironmentEventSource { BACKEND; }
+
+ @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="source")
+ @JsonSubTypes({
+ @JsonSubTypes.Type(value = BackendEvent.class, name = "BACKEND")
+ })
+ static abstract class EnvironmentEvent {
+ private String environmentName;
+ private String message;
+
+ protected EnvironmentEvent() { } // for deserializer
+ protected EnvironmentEvent(String env, String msg) {
+ environmentName = env;
+ message = msg;
+ }
+ public String getEnvironmentName() { return environmentName; }
+ public abstract EnvironmentEventSource getSource();
+ public String getMessage() { return message; }
+ }
+
+ static class BackendEvent extends EnvironmentEvent {
+ private String status;
+
+ private Object resultData;
+
+ protected BackendEvent() {} // for deserializer
+
+ public BackendEvent(String envName, String message, String status, Object results)
+ {
+ super(envName, message);
+ this.status = status;
+ resultData = results;
+ }
+
+ public static BackendEvent create(String environmentName, String message,
+ String status, Object results)
+ {
+ return new BackendEvent(environmentName, message,
+ status, results);
+ }
+
+ @Override
+ public EnvironmentEventSource getSource() {
+ return EnvironmentEventSource.BACKEND;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public Object getResultData() {
+ return resultData;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("(%s): %s", status, getMessage());
+ }
+ }
+
+ public void testDupProps() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ EnvironmentEvent event = new BackendEvent("foo", "hello", "bar", null);
+ String ser = mapper
+ .writerWithDefaultPrettyPrinter()
+ .writeValueAsString(event);
+ mapper.readValue(ser, EnvironmentEvent.class);
+ assertNotNull(ser);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/failing/MapEntryFormat1419Test.java b/src/test/java/com/fasterxml/jackson/failing/MapEntryFormat1419Test.java
new file mode 100644
index 0000000..18a4616
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/failing/MapEntryFormat1419Test.java
@@ -0,0 +1,40 @@
+package com.fasterxml.jackson.failing;
+
+import java.util.*;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.databind.*;
+
+// for [databind#1419]
+public class MapEntryFormat1419Test extends BaseMapTest
+{
+ static class BeanWithMapEntryAsObject {
+ @JsonFormat(shape=JsonFormat.Shape.OBJECT)
+ public Map.Entry<String,String> entry;
+
+ protected BeanWithMapEntryAsObject() { }
+ public BeanWithMapEntryAsObject(String key, String value) {
+ Map<String,String> map = new HashMap<>();
+ map.put(key, value);
+ entry = map.entrySet().iterator().next();
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testWrappedAsObjectRoundtrip() throws Exception
+ {
+ BeanWithMapEntryAsObject input = new BeanWithMapEntryAsObject("foo" ,"bar");
+ String json = MAPPER.writeValueAsString(input);
+ assertEquals(aposToQuotes("{'entry':{'key':'foo','value':'bar'}}"), json);
+ BeanWithMapEntryAsObject result = MAPPER.readValue(json, BeanWithMapEntryAsObject.class);
+ assertEquals("foo", result.entry.getKey());
+ assertEquals("bar", result.entry.getValue());
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/failing/ObjectIdWithBuilder1496Test.java b/src/test/java/com/fasterxml/jackson/failing/ObjectIdWithBuilder1496Test.java
new file mode 100644
index 0000000..8338ed2
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/failing/ObjectIdWithBuilder1496Test.java
@@ -0,0 +1,69 @@
+package com.fasterxml.jackson.failing;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+import com.fasterxml.jackson.annotation.*;
+
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
+
+public class ObjectIdWithBuilder1496Test extends BaseMapTest
+{
+ @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
+ @JsonDeserialize(builder=POJOBuilder.class)
+ static class POJO
+ {
+ private long id;
+ public long getId() { return id; }
+ private int var;
+ public int getVar() { return var; }
+ private POJO (long id, int var) { this.id = id; this.var = var; }
+
+ @Override
+ public String toString() { return "id: " + id + ", var: " + var; }
+ }
+
+ @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
+ @JsonPOJOBuilder(withPrefix = "", buildMethodName="readFromCacheOrBuild")
+ static final class POJOBuilder {
+ // Standard builder stuff
+ private long id;
+ private int var;
+
+ public POJOBuilder id(long _id) { id = _id; return this; }
+ public POJOBuilder var(int _var) { var = _var; return this; }
+
+ public POJO build() { return new POJO(id, var); }
+
+ // Special build method for jackson deserializer that caches objects already deserialized
+ private final static ConcurrentHashMap<Long, POJO> cache = new ConcurrentHashMap<>();
+ public POJO readFromCacheOrBuild() {
+ POJO pojo = cache.get(id);
+ if (pojo == null) {
+ POJO newPojo = build();
+ pojo = cache.putIfAbsent(id, newPojo);
+ if (pojo == null) {
+ pojo = newPojo;
+ }
+ }
+ return pojo;
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final ObjectMapper MAPPER = newObjectMapper();
+
+ public void testBuilderId1496() throws Exception
+ {
+ POJO input = new POJOBuilder().id(123L).var(456).build();
+ String json = MAPPER.writeValueAsString(input);
+ POJO result = MAPPER.readValue(json, POJO.class);
+ assertNotNull(result);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/failing/PolymorphicWithObjectId1551Test.java b/src/test/java/com/fasterxml/jackson/failing/PolymorphicWithObjectId1551Test.java
index 6944644..cf0b0f4 100644
--- a/src/test/java/com/fasterxml/jackson/failing/PolymorphicWithObjectId1551Test.java
+++ b/src/test/java/com/fasterxml/jackson/failing/PolymorphicWithObjectId1551Test.java
@@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
public class PolymorphicWithObjectId1551Test extends BaseMapTest
{
@@ -24,7 +25,14 @@
public Vehicle ownedVehicle;
}
- public void testWithAbstractUsingProp() throws Exception {
+ static class VehicleOwnerBroken {
+ @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "bogus")
+ @JsonIdentityReference(alwaysAsId = false)
+ public Vehicle ownedVehicle;
+ }
+
+ public void testWithAbstractUsingProp() throws Exception
+ {
Car c = new Car();
c.vehicleId = "123";
c.numberOfDoors = 2;
@@ -37,17 +45,47 @@
ObjectMapper objectMapper = new ObjectMapper();
String serialized = objectMapper.writer()
.writeValueAsString(new VehicleOwnerViaProp[] { v1, v2 });
-
// 02-May-2017, tatu: Not possible to support as of Jackson 2.8 at least, so:
+ VehicleOwnerViaProp[] deserialized = objectMapper.readValue(serialized, VehicleOwnerViaProp[].class);
+ assertEquals(2, deserialized.length);
+ assertSame(deserialized[0].ownedVehicle, deserialized[1].ownedVehicle);
+ }
+
+ public void testFailingAbstractUsingProp() throws Exception
+ {
+ Car c = new Car();
+ c.vehicleId = "123";
+ c.numberOfDoors = 2;
+ // both owners own the same vehicle (car sharing ;-))
+ VehicleOwnerBroken v1 = new VehicleOwnerBroken();
+ v1.ownedVehicle = c;
+ VehicleOwnerBroken v2 = new VehicleOwnerBroken();
+ v2.ownedVehicle = c;
+
+ ObjectMapper objectMapper = new ObjectMapper();
try {
- /*VehicleOwnerViaProp[] deserialized = */
- objectMapper.readValue(serialized, VehicleOwnerViaProp[].class);
- fail("Should not pass");
- } catch (JsonMappingException e) {
- verifyException(e, "Invalid Object Id definition for abstract type");
+ objectMapper.writer()
+ .writeValueAsString(new VehicleOwnerBroken[] { v1, v2 });
+ } catch (InvalidDefinitionException e) {
+ // on serialization, reported for different type
+ assertEquals(Car.class, e.getType().getRawClass());
+ verifyException(e, "Invalid Object Id definition");
+ verifyException(e, "can not find property with name 'bogus'");
}
-// assertEquals(2, deserialized.length);
-// assertSame(deserialized[0].ownedVehicle, deserialized[1].ownedVehicle);
+
+ // and same for deser
+ final String JSON = aposToQuotes(
+"[{'ownedVehicle':{'@class':'com.fasterxml.jackson.failing.PolymorphicWithObjectId1551Test$Car','vehicleId':'123',"
++"'numberOfDoors':2}},{'ownedVehicle':'123'}]"
+ );
+ try {
+ objectMapper.readValue(JSON, VehicleOwnerBroken[].class);
+ fail("Should not pass");
+ } catch (InvalidDefinitionException e) {
+ assertEquals(Vehicle.class, e.getType().getRawClass());
+ verifyException(e, "Invalid Object Id definition");
+ verifyException(e, "can not find property with name 'bogus'");
+ }
}
}
diff --git a/src/test/java/com/fasterxml/jackson/failing/ReadOnlyDeser1382Test.java b/src/test/java/com/fasterxml/jackson/failing/ReadOnlyDeser1382Test.java
new file mode 100644
index 0000000..8b3abe8
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/failing/ReadOnlyDeser1382Test.java
@@ -0,0 +1,31 @@
+package com.fasterxml.jackson.failing;
+
+import java.util.*;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.databind.*;
+
+public class ReadOnlyDeser1382Test extends BaseMapTest
+{
+ static class Foo {
+ @JsonProperty(access = JsonProperty.Access.READ_ONLY)
+ private List<Long> list = new ArrayList<>();
+
+ List<Long> getList() {
+ return list;
+ }
+
+ public Foo setList(List<Long> list) {
+ this.list = list;
+ return this;
+ }
+ }
+
+ public void testReadOnly() throws Exception
+ {
+ final ObjectMapper mapper = new ObjectMapper();
+ String payload = "{\"list\":[1,2,3,4]}";
+ Foo foo = mapper.readValue(payload, Foo.class);
+ assertTrue("List should be empty", foo.getList().isEmpty());
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/failing/SkipInjectableIntrospection962Test.java b/src/test/java/com/fasterxml/jackson/failing/SkipInjectableIntrospection962Test.java
index cb37a5c..88f283a 100644
--- a/src/test/java/com/fasterxml/jackson/failing/SkipInjectableIntrospection962Test.java
+++ b/src/test/java/com/fasterxml/jackson/failing/SkipInjectableIntrospection962Test.java
@@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.OptBoolean;
import com.fasterxml.jackson.databind.*;
public class SkipInjectableIntrospection962Test extends BaseMapTest
@@ -30,8 +31,10 @@
{
private String b;
+ // Important! Prevent binding from data
@JsonCreator
- public Injectee(@JacksonInject InjectMe injectMe, @JsonProperty("b") String b) {
+ public Injectee(@JacksonInject(useInput=OptBoolean.FALSE) InjectMe injectMe,
+ @JsonProperty("b") String b) {
this.b = b;
}
diff --git a/src/test/java/com/fasterxml/jackson/failing/StaticTyping1515Test.java b/src/test/java/com/fasterxml/jackson/failing/StaticTyping1515Test.java
new file mode 100644
index 0000000..7a1a554
--- /dev/null
+++ b/src/test/java/com/fasterxml/jackson/failing/StaticTyping1515Test.java
@@ -0,0 +1,77 @@
+package com.fasterxml.jackson.failing;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+
+public class StaticTyping1515Test extends BaseMapTest
+{
+ static abstract class Base {
+ public int a = 1;
+ }
+
+ static class Derived extends Base {
+ public int b = 2;
+ }
+
+ @JsonSerialize(typing = JsonSerialize.Typing.DYNAMIC)
+ static abstract class BaseDynamic {
+ public int a = 3;
+ }
+
+ static class DerivedDynamic extends BaseDynamic {
+ public int b = 4;
+ }
+
+ @JsonPropertyOrder({ "value", "aValue", "dValue" })
+ static class Issue515Singles {
+ public Base value = new Derived();
+
+ @JsonSerialize(typing = JsonSerialize.Typing.DYNAMIC)
+ public Base aValue = new Derived();
+
+ public BaseDynamic dValue = new DerivedDynamic();
+ }
+
+ @JsonPropertyOrder({ "list", "aList", "dList" })
+ static class Issue515Lists {
+ public List<Base> list = new ArrayList<>(); {
+ list.add(new Derived());
+ }
+
+ @JsonSerialize(typing = JsonSerialize.Typing.DYNAMIC)
+ public List<Base> aList = new ArrayList<>(); {
+ aList.add(new Derived());
+ }
+
+ public List<BaseDynamic> dList = new ArrayList<>(); {
+ dList.add(new DerivedDynamic());
+ }
+ }
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ private final ObjectMapper STAT_MAPPER = newObjectMapper();
+ {
+ STAT_MAPPER.enable(MapperFeature.USE_STATIC_TYPING);
+ }
+
+ public void testStaticTypingForProperties() throws Exception
+ {
+ String json = STAT_MAPPER.writeValueAsString(new Issue515Singles());
+ assertEquals(aposToQuotes("{'value':{'a':1},'aValue':{'a':1,'b':2},'dValue':{'a':3,'b':4}}"), json);
+ }
+
+ public void testStaticTypingForLists() throws Exception
+ {
+ String json = STAT_MAPPER.writeValueAsString(new Issue515Lists());
+ assertEquals(aposToQuotes("{'list':[{'a':1}],'aList':[{'a':1,'b':2}],'dList:[{'a':3,'b':4}]}"), json);
+ }
+}
diff --git a/src/test/java/com/fasterxml/jackson/failing/TestConvertingSerializer357.java b/src/test/java/com/fasterxml/jackson/failing/TestConvertingSerializer357.java
deleted file mode 100644
index ef0d30e..0000000
--- a/src/test/java/com/fasterxml/jackson/failing/TestConvertingSerializer357.java
+++ /dev/null
@@ -1,38 +0,0 @@
-package com.fasterxml.jackson.failing;
-
-import java.util.*;
-
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-
-import com.fasterxml.jackson.databind.util.StdConverter;
-
-public class TestConvertingSerializer357
- extends com.fasterxml.jackson.databind.BaseMapTest
-{
- // [databind#357]
- static class Value { }
-
- static class ListWrapper {
- @JsonSerialize(contentConverter = ValueToStringListConverter.class)
- public List<Value> list = Arrays.asList(new Value());
- }
-
- static class ValueToStringListConverter extends StdConverter<Value, List<String>> {
- @Override
- public List<String> convert(Value value) {
- return Arrays.asList("Hello world!");
- }
- }
-
- /*
- /**********************************************************
- /* Test methods
- /**********************************************************
- */
-
- // [databind#357]
- public void testConverterForList357() throws Exception {
- String json = objectWriter().writeValueAsString(new ListWrapper());
- assertEquals("{\"list\":[[\"Hello world!\"]]}", json);
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/failing/TestMultipleExternalIds291.java b/src/test/java/com/fasterxml/jackson/failing/TestMultipleExternalIds291.java
deleted file mode 100644
index 75ef0c2..0000000
--- a/src/test/java/com/fasterxml/jackson/failing/TestMultipleExternalIds291.java
+++ /dev/null
@@ -1,71 +0,0 @@
-package com.fasterxml.jackson.failing;
-
-import com.fasterxml.jackson.annotation.*;
-import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
-import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
-import com.fasterxml.jackson.databind.*;
-
-public class TestMultipleExternalIds291 extends BaseMapTest
-{
- // For [Issue#291]
- interface F1 {}
-
- static class A implements F1 {
- public String a;
- }
-
- static class B implements F1 {
- public String b;
- }
-
- static interface F2 {}
-
- static class C implements F2 {
- public String c;
- }
-
- static class D implements F2{
- public String d;
- }
-
- static class Container {
- public String type;
-
- @JsonTypeInfo(use = Id.NAME, property = "type", include = As.EXTERNAL_PROPERTY)
- @JsonSubTypes({
- @JsonSubTypes.Type(value = A.class, name = "1"),
- @JsonSubTypes.Type(value = B.class, name = "2")})
- public F1 field1;
-
- @JsonTypeInfo(use = Id.NAME, property = "type", include = As.EXTERNAL_PROPERTY)
- @JsonSubTypes({
- @JsonSubTypes.Type(value = C.class, name = "1"),
- @JsonSubTypes.Type(value = D.class, name = "2")})
- public F2 field2;
- }
-
- /*
- /**********************************************************
- /* Test methods
- /**********************************************************
- */
-
- // [databind#291]
- public void testMultiple() throws Exception
- {
- final ObjectMapper mapper = objectMapper();
- final String JSON =
-"{\"type\" : \"1\",\n"
-+"\"field1\" : {\n"
-+" \"a\" : \"AAA\"\n"
-+"}, \"field2\" : {\n"
-+" \"c\" : \"CCC\"\n"
-+"}\n"
-+"}";
-
- Container c = mapper.readValue(JSON, Container.class);
- assertNotNull(c);
- assertTrue(c.field1 instanceof A);
- assertTrue(c.field2 instanceof C);
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/failing/TestObjectIdWithUnwrapping1298.java b/src/test/java/com/fasterxml/jackson/failing/TestObjectIdWithUnwrapping1298.java
index 8c4b97b..d039140 100644
--- a/src/test/java/com/fasterxml/jackson/failing/TestObjectIdWithUnwrapping1298.java
+++ b/src/test/java/com/fasterxml/jackson/failing/TestObjectIdWithUnwrapping1298.java
@@ -45,6 +45,7 @@
public void testObjectIdWithRepeatedChild() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
+ // to keep output faithful to original, prevent auto-closing...
mapper.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
// Equivalent to Spring _embedded for Bean w/ List property
@@ -58,9 +59,10 @@
// serialize parent1 and parent2
String json = mapper
-// .writerWithDefaultPrettyPrinter()
+ .writerWithDefaultPrettyPrinter()
.writeValueAsString(parents);
- System.out.println("This works: " + json);
+ assertNotNull(json);
+// System.out.println("This works: " + json);
// Add parent3 to create ObjectId reference
// Bean w/ repeated relationship from parent1, should generate ObjectId
@@ -74,10 +76,7 @@
// .writerWithDefaultPrettyPrinter()
.writeValue(sw, parents);
} catch (Exception e) {
- System.out.println("Failed output so far: " + sw);
- throw e;
+ fail("Failed with "+e.getClass().getName()+", output so far: " + sw);
}
-
- System.out.println("Also works: " + sw);
}
}
diff --git a/src/test/java/com/fasterxml/jackson/failing/TestPolymorphicDeserialization283.java b/src/test/java/com/fasterxml/jackson/failing/TestPolymorphicDeserialization283.java
deleted file mode 100644
index bd1c254..0000000
--- a/src/test/java/com/fasterxml/jackson/failing/TestPolymorphicDeserialization283.java
+++ /dev/null
@@ -1,73 +0,0 @@
-package com.fasterxml.jackson.failing;
-
-import com.fasterxml.jackson.annotation.JsonSubTypes;
-import com.fasterxml.jackson.annotation.JsonTypeInfo;
-import com.fasterxml.jackson.databind.BaseMapTest;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- * Reproduction of [https://github.com/FasterXML/jackson-databind/issues/283],
- * contributed by Eric T.
- *<p>
- * Problem here is that although explicit concrete class is indicated, polymorphic
- * deserializer comes to different conclusion (using default implementation class),
- * resulting in a <code>ClassCastException</code>.
- * Whether this is wrong, and if so, can we fix it, is unknown at this point
- * (2.6): quite possibly this can not be changed.
- */
-public class TestPolymorphicDeserialization283 extends BaseMapTest
-{
- @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = ClassA.class)
- @JsonSubTypes({
- @JsonSubTypes.Type(name = "a", value = ClassA.class),
- @JsonSubTypes.Type(name = "b", value = ClassB.class)
- })
- public static interface SomeInterface
- {
- public String get();
- }
-
- public static class ClassA implements SomeInterface
- {
- @Override
- public String get()
- {
- return "A";
- }
- }
-
- public static class ClassB implements SomeInterface
- {
- @Override
- public String get()
- {
- return "B";
- }
- }
-
- public static class ClassC implements SomeInterface
- {
- @Override
- public String get()
- {
- return "C";
- }
- }
-
- @Test
- public void testName() throws Exception
- {
- ObjectMapper mapper = objectMapper();
-
- Assert.assertEquals("A", mapper.readValue("{\"type\": \"a\"}", SomeInterface.class).get());
- Assert.assertEquals("A", mapper.readValue("{}", SomeInterface.class).get());
- Assert.assertEquals("B", mapper.readValue("{\"type\": \"b\"}", SomeInterface.class).get());
- Assert.assertEquals("A", mapper.readValue("{\"type\": \"c\"}", SomeInterface.class).get());
-
- Assert.assertEquals("A", mapper.readValue("{\"type\": \"a\"}", ClassA.class).get());
- Assert.assertEquals("B", mapper.readValue("{\"type\": \"b\"}", ClassB.class).get());
- Assert.assertEquals("C", mapper.readValue("{\"type\": \"c\"}", ClassC.class).get());
- }
-}
diff --git a/src/test/java/com/fasterxml/jackson/failing/TestUnwrappedWithCreator265.java b/src/test/java/com/fasterxml/jackson/failing/TestUnwrappedWithCreator265.java
deleted file mode 100644
index a65a678..0000000
--- a/src/test/java/com/fasterxml/jackson/failing/TestUnwrappedWithCreator265.java
+++ /dev/null
@@ -1,75 +0,0 @@
-package com.fasterxml.jackson.failing;
-
-import com.fasterxml.jackson.annotation.*;
-
-import com.fasterxml.jackson.databind.*;
-
-public class TestUnwrappedWithCreator265 extends BaseMapTest
-{
- static class JAddress {
- protected String address;
- protected String city;
- protected String state;
-
- @JsonCreator
- public JAddress( @JsonProperty("address") String address,
- @JsonProperty("city") String city,
- @JsonProperty("state") String state
- ){
- this.address = address;
- this.city = city;
- this.state = state;
- }
-
- public String getAddress1() { return address; }
- public String getCity() { return city; }
- public String getState() { return state; }
- }
-
- static class JPerson {
- protected String _name;
- protected JAddress _address;
- protected String _alias;
-
- @JsonCreator
- public JPerson(@JsonProperty("name") String name,
- @JsonUnwrapped JAddress address,
- @JsonProperty("alias") String alias) {
- _name = name;
- _address = address;
- _alias = alias;
- }
-
- public String getName() {
- return _name;
- }
-
- @JsonUnwrapped public JAddress getAddress() {
- return _address;
- }
-
- public String getAlias() { return _alias; }
- }
-
- /*
- /**********************************************************
- /* Test methods
- /**********************************************************
- */
-
- // For [Issue#265] / [Scala#90]
- public void testUnwrappedWithCreator() throws Exception
- {
- JPerson person = new JPerson("MyName", new JAddress("main street", "springfield", "WA"), "bubba");
- ObjectMapper mapper = new ObjectMapper();
- String json = mapper.writeValueAsString(person);
- JPerson result = mapper.readValue(json, JPerson.class);
- assertNotNull(result);
- assertEquals(person._name, result._name);
- assertNotNull(result._address);
- assertEquals(person._address.city, result._address.city);
-
- // and see that round-tripping works
- assertEquals(json, mapper.writeValueAsString(result));
- }
-}