[jackson-annotations] 158/207: API for custom Object Identifier resolution.
Timo Aaltonen
tjaalton at moszumanska.debian.org
Sat Sep 6 13:55:50 UTC 2014
This is an automated email from the git hooks/post-receive script.
tjaalton pushed a commit to branch master
in repository jackson-annotations.
commit f3cbf27e7176fd71b5e06ed07dc1ef6a963fb2c8
Author: Pascal Gélinas <pascal.gelinas at nuecho.com>
Date: Wed Feb 5 17:29:00 2014 -0500
API for custom Object Identifier resolution.
---
.../jackson/annotation/JsonIdentityInfo.java | 9 ++++
.../jackson/annotation/ObjectIdGenerator.java | 6 +--
.../jackson/annotation/ObjectIdResolver.java | 61 ++++++++++++++++++++++
.../jackson/annotation/SimpleObjectIdResolver.java | 43 +++++++++++++++
4 files changed, 116 insertions(+), 3 deletions(-)
diff --git a/src/main/java/com/fasterxml/jackson/annotation/JsonIdentityInfo.java b/src/main/java/com/fasterxml/jackson/annotation/JsonIdentityInfo.java
index 9ccde66..aa37216 100644
--- a/src/main/java/com/fasterxml/jackson/annotation/JsonIdentityInfo.java
+++ b/src/main/java/com/fasterxml/jackson/annotation/JsonIdentityInfo.java
@@ -60,6 +60,15 @@ public @interface JsonIdentityInfo
public Class<? extends ObjectIdGenerator<?>> generator();
/**
+ * Resolver to use for producing POJO from Object Identifier.
+ * <p>
+ * Default value is {@link SimpleObjectIdResolver}
+ *
+ * @since 2.4
+ */
+ public Class<? extends ObjectIdResolver> resolver() default SimpleObjectIdResolver.class;
+
+ /**
* Scope is used to define applicability of an Object Id: all ids
* must be unique within their scope; where scope is defined
* as combination of this value and generator type.
diff --git a/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java
index ce7ec69..b426562 100644
--- a/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java
+++ b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdGenerator.java
@@ -100,17 +100,17 @@ public abstract class ObjectIdGenerator<T>
/**
* Type of {@link ObjectIdGenerator} used for generating Object Id
*/
- private final Class<?> type;
+ public final Class<?> type;
/**
* Scope of the Object Id (may be null, to denote global)
*/
- private final Class<?> scope;
+ public final Class<?> scope;
/**
* Object for which Object Id was generated: can NOT be null.
*/
- private final Object key;
+ public final Object key;
/**
* Hash code
diff --git a/src/main/java/com/fasterxml/jackson/annotation/ObjectIdResolver.java b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdResolver.java
new file mode 100644
index 0000000..afbb0b6
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/annotation/ObjectIdResolver.java
@@ -0,0 +1,61 @@
+package com.fasterxml.jackson.annotation;
+
+import com.fasterxml.jackson.annotation.ObjectIdGenerator.IdKey;
+
+/**
+ * Definition of API used for constructing POJO from Object Identifiers (as
+ * annotated using {@link JsonIdentityInfo}).
+ *
+ * @since 2.4
+ */
+public interface ObjectIdResolver {
+ /**
+ * Method called when a POJO is deserialized and has an Object Identifier.
+ * Method exists so that implementation can keep track of existing object in
+ * json stream that could be useful for further resolution.
+ *
+ * @param id
+ * The Object Identifer
+ * @param ob
+ * The POJO associated to that Identifier
+ */
+ void bindItem(IdKey id, Object pojo);
+
+ /**
+ * Method called when deserialization encounters the given Object Identifier
+ * and requires the POJO associated with it.
+ *
+ * @param id
+ * The Object Identifier
+ * @return The POJO, or null if unable to resolve.
+ */
+ Object resolveId(IdKey id);
+
+ /**
+ * Factory method called to create a new instance to use for
+ * deserialization: needed since resolvers may have state (a pool of
+ * objects).
+ * <p>
+ * Note that actual type of 'context' is
+ * <code>com.fasterxml.jackson.databind.DeserializationContext</code>, but
+ * can not be declared here as type itself (as well as call to this object)
+ * comes from databind package.
+ *
+ * @param context
+ * Deserialization context object used (of type
+ * <code>com.fasterxml.jackson.databind.DeserializationContext</code>
+ * ; may be needed by more complex resolvers to access contextual
+ * information such as configuration.
+ */
+ ObjectIdResolver newForDeserialization(Object context);
+
+ /**
+ * Method called to check whether this resolver instance can be used for
+ * Object Ids of specific resolver type; determination is based by passing a
+ * configured "blueprint" (prototype) instance; from which the actual
+ * instances are created (using {@link #newForDeserialization}).
+ *
+ * @return True if this instance can be used as-is; false if not
+ */
+ boolean canUseFor(ObjectIdResolver resolverType);
+}
diff --git a/src/main/java/com/fasterxml/jackson/annotation/SimpleObjectIdResolver.java b/src/main/java/com/fasterxml/jackson/annotation/SimpleObjectIdResolver.java
new file mode 100644
index 0000000..b383ddd
--- /dev/null
+++ b/src/main/java/com/fasterxml/jackson/annotation/SimpleObjectIdResolver.java
@@ -0,0 +1,43 @@
+package com.fasterxml.jackson.annotation;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.ObjectIdGenerator.IdKey;
+
+/**
+ * Simple implementation of {@link ObjectIdResolver}
+ *
+ * @author Pascal Gélinas
+ */
+public class SimpleObjectIdResolver implements ObjectIdResolver {
+ private Map<IdKey,Object> _items = new HashMap<ObjectIdGenerator.IdKey,Object>();
+
+ @Override
+ public void bindItem(IdKey id, Object ob)
+ {
+ if (_items.containsKey(id)) {
+ throw new IllegalStateException("Already had POJO for id (" + id.key.getClass().getName() + ") [" + id
+ + "]");
+ }
+ _items.put(id, ob);
+ }
+
+ @Override
+ public Object resolveId(IdKey id)
+ {
+ return _items.get(id);
+ }
+
+ @Override
+ public boolean canUseFor(ObjectIdResolver resolverType)
+ {
+ return resolverType.getClass() == getClass();
+ }
+
+ @Override
+ public ObjectIdResolver newForDeserialization(Object context)
+ {
+ return this;
+ }
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jackson-annotations.git
More information about the pkg-java-commits
mailing list