[jackson-annotations] 34/207: add yet more meat

Timo Aaltonen tjaalton at moszumanska.debian.org
Sat Sep 6 13:55:36 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 b0577af92527d79a04e8047487ac1fdacc0d3f07
Author: Tatu Saloranta <tsaloranta at gmail.com>
Date:   Fri Mar 2 21:01:56 2012 -0800

    add yet more meat
---
 README.md | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 97 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index f74ede5..e3a0ba6 100644
--- a/README.md
+++ b/README.md
@@ -17,11 +17,25 @@ In addition to regular usage (see below), there are couple of noteworthy improve
 * [Mix-in annotations](jackson-annotations/wiki/MixinAnnotations) allow associating annotations on third-party classes ''without modifying classes''.
 * Jackson annotations support full inheritance: meaning that you can ''override annotation definitions'', and not just class annotations but also method/field annotations!
 
-### Renaming properties
+### Maven, Java package
+
+All annotations are in Java package `com.fasterxml.core.annotation`.
+To use annotations, you need to use Maven dependency:
+
+    <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-annotations</artifactId>
+      <version>2.0.0</version>
+    </dependency>
+
+
+or download jars from Maven repository or [Download page](wiki.fasterxml.com/JacksonDownload)
+
+### Usage: renaming properties
 
 One of most common tasks is to change JSON name used for a property: for example:
 
-    public class POJO {
+    public class Name {
       @JsonProperty("firstName")
       public String _first_name;
     }
@@ -34,10 +48,90 @@ instead of
 
     { "_first_name" : "Bob"
 
-### Ignoring properties
+### Usage: Ignoring properties
+
+Sometimes POJOs contain properties that you do not want to write out, so you can do:
+
+    public class Value {
+      public int value;
+      @JsonIgnore public int internalValue;
+    }
+
+and get JSON like:
+
+    { "value" : 42 }
+
+or, you may get properties in JSON that you just want to skip: if so, you can use:
+
+    @JsonIgnoreProperties({ "extra", "uselessValue" })
+    public class Value {
+      public int value;
+    }
+
+which would be able to handle JSON like:
+
+    { "value" : 42, "extra" : "fluffy", "uselessValue" : -13 }
+
+### Usage: Changing property auto-detection
+
+The default Jackson property detection rules will find:
+
+* All ''public'' fields
+* All ''public'' getters ('getXxx()' methods)
+* All setters ('setXxx(value)' methods), ''regardless of visibility'')
+
+But if this does not work, you can change visibility levels by using annotation `@JsonAutoDetect`.
+If you wanted, for example, to auto-detect ALL fields (similar to how packages like GSON work), you could do:
+
+    @JsonAutoDetect(fieldVisiblity=JsonAutoDetect.Visibility.ANY)
+    public class POJOWithFields {
+      private int value;
+    }
+
+or, to disable auto-detection of fields altogether:
+
+    @JsonAutoDetect(fieldVisiblity=JsonAutoDetect.Visibility.NONE)
+    public class POJOWithNoFields {
+      // will NOT be included, unless there is access 'getValue()'
+      public int value;
+    }
+
 
 ### Handling polymorphic types
 
+If you need to read and write values of Objects where there are multiple possible subtypes (i.e. ones that exhibit polymorphism), you may need to enable inclusion of type information. This is needed so that Jackson can read back correct Object type when deserializing (reading JSON into Objects).
+This can be done by adding `@JsonTypeInfo` annotation on ''base class'':
+
+    // Include Java class name ("com.myempl.ImplClass") as JSON property "class"
+    @JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="class")
+    public abstract class BaseClass {
+    }
+
+    public class Impl1 extends BaseClass {
+      public int x;
+    }
+    public class Impl2 extends BaseClass {
+      public String name;
+    }
+
+    public class PojoWithTypedObjects {
+      public List<BaseClass> items;
+    }
+
+and this could result in serialized JSON like:
+
+    { "items" : [
+      { "class":"Impl2", "name":"Bob" },
+      { "class":"Impl1", "x":13 }
+    ])
+
+
+Note that this annotation has lots of configuration possibilities: for more information check out:
+
+* [JavaDocs](http://wiki.fasterxml.com/JacksonJavaDocs)
+* [Intro to polymorphic type handling](http://www.cowtowncoder.com/blog/archives/2010/03/entry_372.html)
+
+
 # Further reading
 
 * [Jackson Project Home](http://wiki.fasterxml.com/JacksonHome)

-- 
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