<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>CowTalk</title>
<link>http://www.cowtowncoder.com/blog/blog.html</link>
<description>Moo-able Type for Cowtowncoder.com</description>
<language>en-US</language>
<copyright>Copyright 2011</copyright>
<lastBuildDate>Sat, 10 Dec 2011 19:28:24 -0800</lastBuildDate>
<pubDate>Sat, 10 Dec 2011 19:28:24 -0800</pubDate>
<generator>http://thingamablog.sf.net</generator>
<docs>http://en.wikipedia.org/wiki/Rss</docs>

<item>
<title>Sorting large data sets in Java using Java-merge-sort</title>
<description>&lt;p&gt;
      When sorting data sets in Java, life is easy if amount of data to 
      process is not huge: JDK has the basic sorting covered well. But if your 
      data is big enough not to fit in memory you are on your own.
    &lt;/p&gt;
    &lt;p&gt;
      This often means that developers use basic Unix 'sort' command line 
      tool. But while it is a good package for basic textual sort -- and when 
      combined with other Unix pipeline tools, on whole range of column-based 
      alternatives -- it is limited in two sometimes crucial aspects:
    &lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;
        Defining custom sorting (collation) order is difficult
      &lt;/li&gt;
      &lt;li&gt;
        Interacting with external tools (including 'sort') from within JVM is 
        inherently difficult
      &lt;/li&gt;
    &lt;/ol&gt;
    &lt;p&gt;
      But there is one less well-known alternative available: a relatively new 
      Java Open Source library available from Github: &lt;a href=&quot;https://github.com/cowtowncoder/java-merge-sort&quot;&gt;java-merge-sort&lt;/a&gt;.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;1. What is java-merge-sort&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Java-merge-sort library implements basic external &lt;a href=&quot;http://en.wikipedia.org/wiki/Merge_sort&quot;&gt;merge 
      sort&lt;/a&gt;, sorting algorithm typically used for disk-backed sorting. 
      Input and output are not limited to files; any &lt;i&gt;java.io.InputStream&lt;/i&gt; 
      / &lt;i&gt;java.io.OutputStream&lt;/i&gt; implementation will work just fine.
    &lt;/p&gt;
    &lt;p&gt;
      Sorting library is designed to work as an ad-hoc tool (in fact, Jar 
      itself can be used as 'sort' tool) as well as a component of bigger data 
      processing systems.
    &lt;/p&gt;
    &lt;p&gt;
      Notable features include:
    &lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;
        Fully customizable input and output handlers, used for reading 
        external data into objects to be sorted and writing them back out 
        (handlers defined by providing factories that create instances)
      &lt;/li&gt;
      &lt;li&gt;
        Optional custom comparators (if items read do not implement Comparable)
      &lt;/li&gt;
      &lt;li&gt;
        Configurable merge factor (number of inputs merged in each pass); max 
        memory usage (which limits length of pre-sort segments -- more memory 
        used, fewer rounds needed)
      &lt;/li&gt;
      &lt;li&gt;
        Configurable temporary file handling (defaults to using JDK default 
        temp files, deletions)
      &lt;/li&gt;
      &lt;li&gt;
        Ability to cancel sorting jobs asynchronously
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
      &lt;b&gt;2. Using as command-line tool&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      A simple way to use the library is as a stand-alone command tool; while 
      there are no specific benefits over standard 'sort' command (assuming 
      one is available), it can be used to test functionality. Usage is as 
      simple as:
    &lt;/p&gt;
    &lt;pre&gt;  java -jar java-merge-sort-[VERSION].jar [input-file]&lt;/pre&gt;
    &lt;p&gt;
      where 'input-file' is optional (if it is missing, will read from 
      standard input); and sorted output will be displayed to standard output.&lt;br&gt;Commonly 
      one will then redirect output to a file:
    &lt;/p&gt;
    &lt;pre&gt;  java -jar java-merge-sort-[VERSION].jar unsorted.txt &amp;gt; sorted.txt&lt;/pre&gt;
    &lt;p&gt;
      Under the hood, this will run code from class &lt;i&gt;com.fasterxml.sort.std.TextFileSorter&lt;/i&gt;
    &lt;/p&gt;
    &lt;p&gt;
      which is both a concrete sorter implementation, and defines main() 
      method to act as a command-line tool.&lt;br&gt;Sort will be done line-by-line, 
      using basic lexicographic (~= alphabetic) sort which works for common 
      encodings like ASCII, Latin-1 and UTF-8.&lt;br&gt;Command will limit memory 
      usage to 50% of maximum heap.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;3. Simple programmatic usage: textual file sort &lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      More commonly java-merge-sort is used as a component of bigger 
      processing system. So let's have a look at basic usage as 'sort' 
      replacement, i.e. sorting text files.
    &lt;/p&gt;
    &lt;p&gt;
      Code to sort an input file into output file is:
    &lt;/p&gt;
    &lt;pre&gt;  public void sort(InputFile in, OutputFile out) throws IOException&lt;br&gt;  {&lt;br&gt;    TextFileSorter sorter = new TextFileSorter(new SortConfig().withMaxMemoryUsage(20 * 1000 * 1000)); // use up to 20 megs&lt;br&gt;    sorter.sort(new FileInputStream(in), new FileOutputStream(out));&lt;br&gt;    // note: sort() will close InputStream, OutputStream after sorting&lt;br&gt;  }&lt;br&gt;&lt;/pre&gt;
    &lt;p&gt;
      which uses default configuration except for maximum memory usage 
      (default is 40 megs: which often works just fine)
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;4. Advanced usage: sort JSON files&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Above example showed one benefit -- easy integration from Java code -- 
      but the real power comes from the fact that we can change input and 
      output handlers to deal with all kinds of data, to support advanced 
      sorting behavior. To demonstrate this, let's consider case where input 
      is a file that contains JSON entries: each line contains a JSON Object 
      like:
    &lt;/p&gt;
    &lt;p&gt;
      { &amp;quot;firstName&amp;quot; : &amp;quot;Joe&amp;quot;, &amp;quot;lastName&amp;quot; : &amp;quot;Plumber&amp;quot;, &amp;quot;age&amp;quot;:58 }
    &lt;/p&gt;
    &lt;p&gt;
      and which we want to sort primary by age, from lowest to highest, and 
      than by name, alphabetic, first by last name, then by first name.&lt;br&gt;We 
      can bind this to a Java class like:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;  public class Person implements Comparable&amp;lt;Person&amp;gt;
  {
    public int age;
    public String firstName, lastName;

    public int compareTo(Person other) {
     int diff = age - other.age;
     if (diff == 0) {
      diff = lastName.compareTo(other.lastName);
      if (diff == 0) {
       diff = firstName.compareTo(other.firstName);
      }
     }
     return diff;
    }
  }&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      using Jackson JSON processor, and then sort entries using 
      java-merge-sort.
    &lt;/p&gt;
    &lt;p&gt;
      Code to do this is bit more complicated; let's start with Sorter 
      implementation:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;import java.io.*;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.*;
import org.codehaus.jackson.type.JavaType;

import com.fasterxml.sort.std.StdComparator;

public class JsonPersonSorter extends Sorter&amp;lt;Person&amp;gt;
{
  public JsonFileSorter() throws IOException {
    this(entryType, new SortConfig(), new ObjectMapper());
  }

  public JsonFileSorter(SortConfig config, ObjectMapper mapper) throws IOException {
    this(mapper.constructType(Person.class), config, mapper);
  }

  public JsonFileSorter(JavaType entryType, SortConfig config, ObjectMapper mapper) throws IOException {
    super(config, new ReaderFactory(mapper.reader(entryType)),
      new WriterFactory(mapper),
      new StdComparator&amp;lt;Person&amp;gt;());
  }
}&lt;/pre&gt;
    &lt;hr&gt;
    and supporting reading-related classes are:&lt;hr&gt;

    &lt;pre&gt;public class ReaderFactory extends DataReaderFactory&amp;lt;Person&amp;gt;
{
  private final ObjectReader _reader;
  public ReaderFactory(ObjectReader r) {
    _reader = r;
  }

  @Override
  public DataReader&amp;lt;Person&amp;gt; constructReader(InputStream in) throws IOException {
    MappingIterator&amp;lt;Person&amp;gt; it = _reader.readValues(in);
    return new Reader&amp;lt;Person&amp;gt;(it);
  }
}

public class Reader&amp;lt;E&amp;gt; extends DataReader&amp;lt;E&amp;gt;
{
  protected final MappingIterator&amp;lt;E&amp;gt; _iterator;
 
  public Reader(MappingIterator&amp;lt;E&amp;gt; it) {_i terator = it; }

  @Override
  public E readNext() throws IOException {
    if (_iterator.hasNext()) {
      return _iterator.nextValue();
    }
    return null;
  }
&lt;br&gt;  // not a good estimation, has to do for now (should count String lengths, estimate)
  @Override public int estimateSizeInBytes(E item) { return 100; }
  @Overridepu blic void close() throws IOException { } // auto-closes when we reach end
}&lt;/pre&gt;
    &lt;p&gt;
      
    &lt;/p&gt;
    &lt;hr&gt;
    and writing-related classes:&lt;hr&gt;

    &lt;pre&gt;static class WriterFactory&amp;lt;W&amp;gt; extends DataWriterFactory&amp;lt;W&amp;gt;
{
  protected final ObjectMapper _mapper;

  public WriterFactory(ObjectMapper m) {
    _mapper = m;
  }

  @Override
  public DataWriter&amp;lt;W&amp;gt; constructWriter(OutputStream out) throws IOException {
    return new Writer&amp;lt;W&amp;gt;(_mapper, out);
  }
}

static class Writer&amp;lt;E&amp;gt; extends DataWriter&amp;lt;E&amp;gt;
{
  protected final ObjectMapper _mapper;
  protected final JsonGenerator _generator;

  public Writer(ObjectMapper mapper, OutputStream out) throws IOException {
    _mapper = mapper;
    _generator = _mapper.getJsonFactory().createJsonGenerator(out);
  }

  @Override
  public void writeEntry(E item) throws IOException {
    _mapper.writeValue(_generator, item);
    // not 100% necesary, but for readability, add linefeeds
    _generator.writeRaw('\n');
  }

  @Override
  public void close() throws IOException {
    _generator.close();
  }
}&lt;br&gt;&lt;br&gt;So with all of above, we could sort a file using:

   JsonFileSorter sorter = new JsonFileSorter();
   sorter.sort(inputFile, outputFile);&lt;/pre&gt;
    &lt;p&gt;
      Which is pretty much identical to earlier code to sort a File; just with 
      different reader+writer configuration.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;5. Even more advanced: compress intermediate files?&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      There are many ways to customize processing; and one interesting idea is 
      to actually compress intermediate files (results of pre-sort, inputs to 
      later merge rounds); preferably using ultra-fast Java compressor like &lt;a href=&quot;https://github.com/ning/compress&quot;&gt;Ning 
      LZF&lt;/a&gt;.
    &lt;/p&gt;
    &lt;p&gt;
      Code to do this would not be long -- it's just matter of changing 
      DataReaderFactory and DataWriterFactory to read/write files -- but I 
      will leave this up as an exercise to reader. :-)
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;6. More speed: configurations&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      There are two main configuration switches that can be used to improve 
      speed:
    &lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;
        Amount of memory used for pre-sorting: more memory to use, fewer 
        sorted segments are needed -- in fact, it may be possible to do the 
        whole sort in memory. Default memory to use is 40 megabytes (to 
        accomodate for default JDK max heap size of 64 megs)
      &lt;/li&gt;
      &lt;li&gt;
        Number of inputs merged per round: default is 16 inputs, which should 
        be enough; but you can increase this to reduce number of merge rounds 
        needed (or reduce if you want to minimize number of open files, in 
        case you encounter problems)
      &lt;/li&gt;
    &lt;/ol&gt;
    &lt;p&gt;
      &lt;b&gt;7. Future ideas&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Looking at JSON sorting code, I realize that it would be easy to create 
      a generic sorter that uses Jackson. And not only would this support 
      sorting JSON files, but also files that use any other format Jackson 
      supports, such as Smile (out of the box, with 'SmileFactory'), &lt;a href=&quot;https://github.com/FasterXML/jackson-xml-databind&quot;&gt;XML&lt;/a&gt;, 
      &lt;a href=&quot;https://github.com/FasterXML/jackson-dataformat-csv&quot;&gt;CSV&lt;/a&gt; 
      and &lt;a href=&quot;https://github.com/michel-kraemer/bson4jackson&quot;&gt;BSON&lt;/a&gt;!
    &lt;/p&gt;</description>
<link>http://www.cowtowncoder.com/blog/archives/12-01-2011_12-31-2011.html#465</link>
<guid>http://www.cowtowncoder.com/blog/archives/12-01-2011_12-31-2011.html#465</guid>

<category>Java</category>

<category>Open Source</category>

<pubDate>Sat, 10 Dec 2011 19:23:16 -0800</pubDate>
</item>

<item>
<title>On prioritizing my Open Source projects, retrospect #3</title>
<description>&lt;p&gt;
  (note: continuing story, see the &lt;a href=&quot;/blog/archives/2011/02/entry_444.html&quot;&gt;previous 
  installment&lt;/a&gt;)
&lt;/p&gt;
&lt;p&gt;
  &lt;b&gt;1. What was the plan again?&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
  Ok, it has been almost 8 months since the previous priorization overview 
  (plan was to check after 4, but time flies when you are having fun!)&lt;br&gt;High-level 
  priority list back then had these entries:
&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;
Aalto 1.0 (complete async API, impl)
  &lt;/li&gt;
  &lt;li&gt;
ClassMate 1.0
  &lt;/li&gt;
  &lt;li&gt;
Java CacheMate, ideally 1.0
  &lt;/li&gt;
  &lt;li&gt;
Tr13 1.0
  &lt;/li&gt;
  &lt;li&gt;
Externalized Mr Bean (depending on interest)
  &lt;/li&gt;
  &lt;li&gt;
Jackson 1.8
  &lt;/li&gt;
  &lt;li&gt;
Jackson-xml-databinding 1.0
  &lt;/li&gt;
  &lt;li&gt;
Work on Smile format
  &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
  &lt;b&gt;2. And how have we done?&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
  This time hit rate was even bit lower (than previous one at 50%), 
  although there was some progress. In fact, had I checked things after 4 
  months, only one entry would have been completed (Jackson 1.8).
&lt;/p&gt;
&lt;p&gt;
  Item by item, we have:
&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;
Aalto: modest progress (did write a blog entry on how to use async 
parsing at least); still need async SAX implementation, no 1.0 
(although 0.9.7 was released right after blog entry)
  &lt;/li&gt;
  &lt;li&gt;
ClassMate: minor fixes, but no 1.0 yet
  &lt;/li&gt;
  &lt;li&gt;
CacheMate: significant progress (secondary indexes); I now have 1.0 
design (for &amp;quot;raw&amp;quot; in-memory), but not yet implemented -- so kind of 
half-done
  &lt;/li&gt;
  &lt;li&gt;
Tr13: no progress
  &lt;/li&gt;
  &lt;li&gt;
Externalized mr Bean: no demand, no progress
  &lt;/li&gt;
  &lt;li&gt;
Jackson: 1.8 released (and even more, see below)
  &lt;/li&gt;
  &lt;li&gt;
Deferred: Externalized Mr Bean -- no work done (only some preliminary 
scoping)
  &lt;/li&gt;
  &lt;li&gt;
Jackson-xml-databinding: bug fixes, but no 1.0
  &lt;/li&gt;
  &lt;li&gt;
Smile format: actual progress -- Pierre from Ning implemented libsmile 
(C), contributed Smile-detection for unix/linux 'file' command
  &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
  So it's mostly modest progress and misses this time; plan was not really 
  aligned with what was needed. Only 3 entries had significant progress.
&lt;/p&gt;
&lt;p&gt;
  What went wrong? Partially it's just that huge popularity of Jackson 
  swept away many of the plans; and conversely, lack of interest in many 
  of the entries held them back.&lt;br&gt;But additionally, many other things 
  got implemented. So let's look at that aspect next.
&lt;/p&gt;
&lt;p&gt;
  &lt;b&gt;3. What was done instead?&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
  Here are things I can remember, in loose work order:
&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
&lt;a href=&quot;https://github.com/ning/compress&quot;&gt;LZF compression&lt;/a&gt; (&amp;quot;Ning 
LZF&amp;quot;) -- much progress, quite close to 1.0
  &lt;/li&gt;
  &lt;li&gt;
Jackson modules, such as &lt;a href=&quot;https://github.com/FasterXML/jackson-module-afterburner&quot;&gt;Afterburner&lt;/a&gt; 
and improvements to already existing ones (&lt;a href=&quot;https://github.com/FasterXML/jackson-module-scala&quot;&gt;scala&lt;/a&gt;, 
&lt;a href=&quot;https://github.com/FasterXML/jackson-module-hibernate&quot;&gt;hibernate&lt;/a&gt;) 
-- although not yet for CSV or Joda modules (which exist in skeletal 
form)
  &lt;/li&gt;
  &lt;li&gt;
&lt;a href=&quot;https://github.com/ning/jvm-compressor-benchmark&quot;&gt;JVM-compressor-benchmark&lt;/a&gt; 
for comparing space/time efficiency of various compressors on JVM, 
core done (can always add codecs)
  &lt;/li&gt;
  &lt;li&gt;
&lt;a href=&quot;https://github.com/cowtowncoder/low-gc-membuffers&quot;&gt;Low-gc-membuffers&lt;/a&gt;, 
an experimental FIFO for byte[], with native memory buffers
  &lt;/li&gt;
  &lt;li&gt;
&lt;a href=&quot;https://github.com/cowtowncoder/java-merge-sort&quot;&gt;Java merge 
sort&lt;/a&gt; (file-backed configurable efficient merge sort) -- mostly 
done, although not declared 1.0
  &lt;/li&gt;
  &lt;li&gt;
&lt;a href=&quot;https://github.com/ning/lzf4hadoop&quot;&gt;Lzf4Hadoop&lt;/a&gt;, Hadoop 
integration for LZF compression -- basically done
  &lt;/li&gt;
  &lt;li&gt;
New mode for &lt;a href=&quot;https://github.com/eishay/jvm-serializers/wiki&quot;&gt;JVM-serializers&lt;/a&gt; 
benchmark, data streams, for more balanced evaluations; implemented 
most common codecs
  &lt;/li&gt;
  &lt;li&gt;
Jackson 1.9
  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
  Quite a list eh? One completely new &amp;quot;branch&amp;quot; of development was related 
  to LZF compression codec. And continue huge demand for all things 
  Jackson also meant that majority of my time was spent on Jackson and its 
  extensions.
&lt;/p&gt;
&lt;p&gt;
  &lt;b&gt;3. Updated list&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
  Given recent developments, popular demand, and on-going plans, here is 
  my current thinking of main priorities:
&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
Jackson CSV module: I want to add proper Jackson support for CSV, 
since it it still a very common (and pretty functional!) input data 
format, and de facto default export format for lots of data sources. 
And best of all, this can be done without any work on Jackson core
  &lt;/li&gt;
  &lt;li&gt;
CacheMate: I really want to implement secondary caches, and have a 
reasonable design (in many ways similar to persistence used by 
Cassandra/BigTable/HBase) on how to go about it
  &lt;/li&gt;
  &lt;li&gt;
Jackson 2.0: move to github, refactor, redesign, remove deprecated 
things -- major renovation, to lay foundation for longer term 2.x 
development
  &lt;/li&gt;
  &lt;li&gt;
ClassMate: getting to official 1.0 would be good, as well as writing 
blog entry or two on actual usage
  &lt;/li&gt;
  &lt;li&gt;
Jackson XML data binding: fix bugs, declare 1.0, easier to market that 
way. And of course document
  &lt;/li&gt;
  &lt;li&gt;
Ning-compress (LZF) 1.0: already functional, and feature-wise as good 
as 1.0, but there are couple of optimization tricks (by mr Dain S who 
ported Snappy to Java) that I'd still like to investigate, before 
declaring things 1.0
  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
  Other interesting things that might get included are:
&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
Aalto 1.0: it would be good to sort of declare it done by implementing 
Async SAX, announcing the first non-beta release
  &lt;/li&gt;
  &lt;li&gt;
Externalized mr Bean (BeanMate?) still looks like a potentially useful 
thing that others would want to use (this above and beyond basic 
refactoring that Jackson 2.0 would dictate, i.e. splitting of the jar 
as first-level new module)
  &lt;/li&gt;
  &lt;li&gt;
Standardization work for Smile?
  &lt;/li&gt;
  &lt;li&gt;
Maybe even design a splittable variant of LZF (Splitty? Splitz?) -- 
with improved usage of length indicators (VInts), designed so 
implementation can be even faster than LZF (on par with Snappy java), 
yet allow splittability which would be very valuable for Map/Reduce 
tasks
  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
  I expect above list to of course have at most 50% success rate, and for 
  other good stuff to be worked on instead. Especially with likely changes 
  to my daytime job, with possibly changing roles at day-to-day work, 
  changes that will likely boost priority of some other open source 
  efforts, reduce that of others.
&lt;/p&gt;</description>
<link>http://www.cowtowncoder.com/blog/archives/10-01-2011_10-31-2011.html#464</link>
<guid>http://www.cowtowncoder.com/blog/archives/10-01-2011_10-31-2011.html#464</guid>

<category></category>

<pubDate>Sat, 22 Oct 2011 21:51:27 -0700</pubDate>
</item>

<item>
<title>Jackson 1.9 new feature overview</title>
<description>&lt;p&gt;
      Jackson 1.9 was just released. As usual, it can downloaded from the &lt;a href=&quot;http://wiki.fasterxml.com/JacksonDownload&quot;&gt;Download 
      page&lt;/a&gt;, and detailed release information can be found from &lt;a href=&quot;http://wiki.fasterxml.com/JacksonRelease19&quot;&gt;1.9 
      release page&lt;/a&gt;.
    &lt;/p&gt;
    &lt;p&gt;
      Let's have a look into contents of this release.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;1. Overview&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      One of focus areas on this release was once again to tackle oldest 
      significant issues and improvement ideas; and two of major new features 
      are long-standing issues (ability to inline/unwrap JSON values; unify 
      annotation handling for getters/setters/fields). Another big goal was to 
      improve ergonomics: to simplify configuration, shorten commonly used 
      usage patterns and so on. And finally there was also intent to try to 
      &amp;quot;2.0 proof&amp;quot; things, by trying to figure out things that need to be 
      deprecated to allow removal of obsolete methods as well as indicate 
      cases where improved functionality is available.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;2. Major features&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      (note: classification of features into major, medium and minor 
      categories is not exact science, and different users might consider 
      different things more important than others -- here we simply use 
      categorization that the release page uses)
    &lt;/p&gt;
    &lt;p&gt;
      Major features included in 1.9 are:
    &lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;
        Allow inlining/unwrapping of child objects using @JsonUnwrapped
      &lt;/li&gt;
      &lt;li&gt;
        Rewrite property introspection part of framework to combine 
        getter/setter/field annotations
      &lt;/li&gt;
      &lt;li&gt;
        Allow injection of values during deserialization
      &lt;/li&gt;
      &lt;li&gt;
        Support for 'external type id' by adding 
        @JsonTypeInfo.As.EXTERNAL_PROPERTY
      &lt;/li&gt;
      &lt;li&gt;
        Allow registering instantiators (ValueInstantiator) for types
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
      &lt;b&gt;2.1 @JsonUnwrapped&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Ability to map JSON like
    &lt;/p&gt;
    &lt;pre&gt;  {
    &amp;quot;name&amp;quot; : &amp;quot;home&amp;quot;,
    &amp;quot;latitude&amp;quot; : 127,
    &amp;quot;longitude&amp;quot; : 345
  }&lt;/pre&gt;
    &lt;p&gt;
      to classes defined as:
    &lt;/p&gt;
    &lt;pre&gt;  class Place {
    public String name;&lt;br&gt;&lt;br&gt;    @JsonUnwrapped
    public Location location;
  }
&lt;br&gt;  class Location {
   public int latitude, longitude;
  }&lt;/pre&gt;
    &lt;p&gt;
      has been on many users' wish list for a while now; and with addition of 
      @JsonUnwrapped (used as shown above) this simple structural 
      transformation can now be achieved without custom handling
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;2.2 &amp;quot;Unified&amp;quot; properties, merging (&amp;quot;sharing&amp;quot;) of annotations of 
      getters/setters/fields&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Another long-standing issue has been that of isolation between 
      annotations used by getters, setters and fields. Basically annotation 
      added to a getter was only ever used for serialization, and would never 
      have any effect on deserialization; similarly setter never affected 
      deserialization. While this is not a problem for many annotation use 
      cases, it would make following use case work quite different from what 
      users intuitively expect:
    &lt;/p&gt;
    &lt;pre&gt;  class Point {&lt;br&gt;    @JsonProperty(&amp;quot;width&amp;quot;)&lt;br&gt;    public int getW();&lt;br&gt;    public void setW(int w); // must be separately renamed&lt;br&gt;  }&lt;/pre&gt;
    &lt;p&gt;
      which would actually lead to there being two separate properties: 
      &amp;quot;width&amp;quot; that is written out during serialization; and &amp;quot;w&amp;quot; that is 
      expected to be received when deserializing. Many users would intuitively 
      expect annotation to be &amp;quot;shared&amp;quot; between two parts of logically related 
      accessors. Same issue also affects annotations like @JsonIgnore and 
      @JsonTypeInfo, requiring use of seemingly redundant annotations.
    &lt;/p&gt;
    &lt;p&gt;
      Jackson 1.9 solves this by adding new internal representation of logical 
      property, and merging resulting annotations using expected priorities 
      (meaning that annotations on a getter have precedence over setter when 
      serializing, and vice versa).
    &lt;/p&gt;
    &lt;p&gt;
      There are also other more subtle changes, related to these changes. For 
      example, class like:
    &lt;/p&gt;
    &lt;pre&gt;  class ValueBean {
    private int value;

    public int getValue() { return value; }
  }&lt;/pre&gt;
    &lt;p&gt;
      can now be deserialized succesfully, even without field &amp;quot;value&amp;quot; being 
      visible or annotated: since it is joined with getter (&amp;quot;getValue()&amp;quot;), and 
      getter is explicitly annotated, field is included as the accessor to use 
      for assigning value for the property.
    &lt;/p&gt;
    &lt;p&gt;
      The last important benefit of this feature is that now handling of 
      Jackson and JAXB annotations is much more similar, which should make 
      JAXB annotations works better as a result (code was simplified 
      significantly) -- this because JAXB had always considered annotations to 
      be shared in this way.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;2.3 Value Injection for Deserialization&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Value injection here means ability to insert (&amp;quot;inject&amp;quot;) values into 
      POJOs outside of general data binding: that is, values that do not come 
      from JSON input. Instead, values to inject are specified during 
      configuration of ObjectMapper or ObjectReader used for data binding.
    &lt;/p&gt;
    &lt;p&gt;
      Why is this needed? Some Java types require additional context 
      information to be able to construct POJO instances, for example. And in 
      other cases, you may want to pre-populate values of some fields; and 
      while there are other mechanims (for example, you can pass an existing 
      POJO instance for &amp;quot;updateValue()&amp;quot;) method) they are quite limited.
    &lt;/p&gt;
    &lt;p&gt;
      Only two things are needed for value injection:
    &lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;
        Means to indicate properties for which values are to be injected, and
      &lt;/li&gt;
      &lt;li&gt;
        Definition of values to inject
      &lt;/li&gt;
    &lt;/ol&gt;
    &lt;p&gt;
      Default mechanism is to handle first part by using new annotation, &lt;i&gt;@JacksonInject&lt;/i&gt;, 
      so that we could have:
    &lt;/p&gt;
    &lt;pre&gt;  public class InjectableBean
  {
    @JacksonInject(&amp;quot;seq&amp;quot;) private int sequenceNumber;
    public String name;
  }&lt;/pre&gt;
    &lt;p&gt;
      and second part is handled by allowing configuration of ObjectMapper or 
      ObjectWriter instance with &lt;i&gt;InjectableValues&lt;/i&gt;, object that can find 
      values to inject given value id. Value ids can be specified as either 
      Strings, or as Classes; if Class is used, Class.getName() is used to get 
      actual String id to use. For above POJO, we could handle deserialization 
      as follows:
    &lt;/p&gt;
    &lt;pre&gt;  ObjectMapper mapper = new ObjectMapper();
  Integer sequenceNumber = SequenceGenerator.next(); // or whatever
  InjectableValues inject = new InjectableValues.Std()
   .addValue(&amp;quot;seq&amp;quot;, id)
  final String json = &amp;quot;{\&amp;quot;name\&amp;quot;:\&amp;quot;Lucifer\&amp;quot;}&amp;quot;;
  InjectableBean value = mapper.reader(InjectableBean.class).withInjectableValues(inject).readValue(json);&lt;/pre&gt;
    &lt;p&gt;
      For more on this feature, check out FasterXML Wiki's entry on &lt;a href=&quot;http://wiki.fasterxml.com/JacksonFeatureValueInjection&quot;&gt;Value 
      Injection&lt;/a&gt;.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;2.4 External Type Id&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Jackson has had support for full polymorphic type handling since 1.5, 
      allowing configuration of both type identifier in use (usually either a 
      class name, or logical type name) and type inclusion mechanism (as 
      property, as wrapper array, as single-element wrapper object).&lt;br&gt;This 
      covers wide range of usage scenarios, but there is one inclusion 
      mechanism that is sometimes used but could not be supported by Jackson: 
      that of using &amp;quot;external type identifier&amp;quot;. This style of type inclusion 
      is used by some data formats, most notably geoJSON.
    &lt;/p&gt;
    &lt;p&gt;
      By external type identifier we mean case such as this:
    &lt;/p&gt;
    &lt;pre&gt; {
  &amp;quot;type&amp;quot; : &amp;quot;rectangle&amp;quot;,
  &amp;quot;shape&amp;quot; :  {
   &amp;quot;width&amp;quot;: 20.0,
   &amp;quot;height&amp;quot; : 40.0
  }
 }&lt;/pre&gt;
    &lt;p&gt;
      where type is included as a property (&amp;quot;type&amp;quot;) that is outside of JSON 
      Object being typed.
    &lt;/p&gt;
    &lt;p&gt;
      With 1.9 we can support such use case by using @JsonTypeInfo with a new 
      inclusion value:
    &lt;/p&gt;
    &lt;pre&gt;  public class ShapeContainer
  {
    @JsonTypeInfo(use=Id.NAME, include=As.EXTERNAL_PROPERTY, property=&amp;quot;type&amp;quot;)
    public Shape shape;    
  }
 &lt;br&gt;  static class Shape { }
&lt;br&gt;  @JsonTypeName(&amp;quot;rectangle&amp;quot;) // or rely on class name, Rectangle
  static class Rectangle extends Shape {
    public double width, height;
  }&lt;/pre&gt;
    &lt;p&gt;
      One thing to note here is that this inclusion mechanism should only be 
      used with properties; annotating classes with @JsonTypeInfo that 
      indicates external type identifiers can cause conflicts.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;2.5 Value instantiators&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      And last but not least, 1.9 also allows much more control over mechanism 
      used to create actual POJO value instances. While Jackson 1.2 added 
      support for @JsonCreator annotation, there has not been a way to add 
      custom creator objects.
    &lt;/p&gt;
    &lt;p&gt;
      With 1.9, we get following pieces:
    &lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;
        &lt;i&gt;ValueInstantiator&lt;/i&gt; (abstract class), extended by objects used to 
        create value instances
      &lt;/li&gt;
      &lt;li&gt;
        &lt;i&gt;ValueInstantiators&lt;/i&gt; (interface), provider for per-type 
        ValueInstantor instances (as well as ValueInstantiators.Base abstract 
        class for actual implementations)
      &lt;/li&gt;
      &lt;li&gt;
        &lt;i&gt;Module.setupContext&lt;/i&gt; method &lt;i&gt;addValueInstantiators&lt;/i&gt;(); as 
        well as &lt;i&gt;SimpleModule&lt;/i&gt; method &lt;i&gt;addValueInstantiator&lt;/i&gt;(), for 
        adding provider(s), so modules can easily provide instantiators for 
        types they support
      &lt;/li&gt;
      &lt;li&gt;
        &lt;i&gt;@JsonValueInstantiator&lt;/i&gt; annotation that can be used as an 
        alternative to specify instantiator used for annotated type.
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
      Above pieces are basically enough to support all three modes of 
      construction @JsonCreator allows (so basically @JsonCreator could be 
      implemented as module, if we wanted!):
    &lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;
        &amp;quot;Default&amp;quot; construction that takes no arguments and uses no-argument 
        constructor or factory method
      &lt;/li&gt;
      &lt;li&gt;
        &amp;quot;Delegate-based&amp;quot; construction, in which JSON value is first bound to 
        an intermediate type (such as java.util.Map or Jackson JsonNode), and 
        this instance is passed to single-argument creator method
      &lt;/li&gt;
      &lt;li&gt;
        &amp;quot;Property-based&amp;quot; construction, in which one or more named values (JSON 
        properties) are bound to specified types that match creator arguments, 
        and these are passed to creator method.
      &lt;/li&gt;
    &lt;/ol&gt;
    &lt;p&gt;
      Mapping of above construction methods to ValueInstantiator methods is 
      fairly straight-forward:
    &lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;
        Simple no-arguments construction (&lt;i&gt;ValueInstantiator.createUsingDefault()&lt;/i&gt;): 
        used if the other construction mechanisms are not available: consumes 
        no JSON properties.
      &lt;/li&gt;
      &lt;li&gt;
        Delegate-based construction (&lt;i&gt;ValueInstantiator.createUsingDelegate&lt;/i&gt;(Object)): 
        similar to annotating a single-argument constructor or factory method 
        with @JsonCreator, but NOT specifying argument name with 
        @JsonProperty. If specified (i.e. value instantiator indicates it 
        supports this), JSON value for property is first bound into 
        intermediate (delegate) type, and then this value is passed to 
        delegate creator method. Jackson mapper will handle all the details of 
        initial binding, passing delegate object as the argument.
      &lt;/li&gt;
      &lt;li&gt;
        Property-based construction (&lt;i&gt;ValueInstantiator.createFromObjectWith&lt;/i&gt;(Object[] 
        args)): similar to using @JsonCreator with arguments that all have 
        @JsonProperty annotation to specify JSON property name to bind.
      &lt;/li&gt;
    &lt;/ol&gt;
    &lt;p&gt;
      It is worth noting that order in which availability of different modes 
      is checked is reverse of above: first a check is made to see if 
      property-based method is available; if not, then delegate-based, and 
      finally default construction.
    &lt;/p&gt;
    &lt;p&gt;
      Since this is possibly the most complicated new feature, I will need to 
      defer a full example to another blog post. But let's consider a very 
      simple ValueInstantiator implementation that just supports the default 
      (no-argument) instantiation:
    &lt;/p&gt;
    &lt;pre&gt;  class SimpleInstantiator extends ValueInstantiator
  {
    @Override public String getValueTypeDesc() { // only needed for error messages
      return MyType.class.getName();
    }

    @Override // yes, this creation method is available
    public boolean canCreateUsingDefault() { return true; }

    @Override
    public MyType createUsingDefault() {
      return new MyType(true);
    }
  }&lt;/pre&gt;
    &lt;p&gt;
      and similarly you can add support for delegate- or property-based 
      methods.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;3. Other notable features&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Aside from above-mentioned major features, there are many other useful 
      improvements:
    &lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;
        &amp;quot;mini-core&amp;quot; jar (jackson-mini-1.9.0.jar)
      &lt;/li&gt;
      &lt;li&gt;
        &lt;i&gt;DeserializationConfig.Feature.UNWRAP_ROOT_VALUE&lt;/i&gt;
      &lt;/li&gt;
      &lt;li&gt;
        @JsonView for JAX-RS methods to return a specific &lt;i&gt;JsonView&lt;/i&gt;
      &lt;/li&gt;
      &lt;li&gt;
        Terse(r) Visibility: &lt;i&gt;ObjectMapper.setVisibility()&lt;/i&gt;, &lt;i&gt;VisibilityChecker.with(Visibility)&lt;/i&gt;
      &lt;/li&gt;
      &lt;li&gt;
        Add standard naming-strategy implementation(s)
      &lt;/li&gt;
      &lt;li&gt;
        Add &lt;i&gt;JsonTypeInfo.defaultSubType&lt;/i&gt; property to indicate type to 
        use if class id/name missing
      &lt;/li&gt;
      &lt;li&gt;
        Add &lt;i&gt;SimpleFilterProvider.setFailOnUnknownId()&lt;/i&gt; to disable 
        throwing exception on missing filter id
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
      &amp;quot;Mini core&amp;quot;: as name suggests, there is now a new jar 
      (jackson-mini-1.9.0.jar) that is about 40% smaller than the default one 
      -- about 136kB or so. Size reduction is achieved by leaving out text 
      files (LICENSE), as well as annotations, but otherwise functionality is 
      equivalent to standard core package, i.e. supports streaming API 
      (JsonParser/JsonGenerator, JsonFactory).
    &lt;/p&gt;
    &lt;p&gt;
      &lt;i&gt;DeserializationConfig.Feature.UNWRAP_ROOT_VALUE&lt;/i&gt; is counterpart to &lt;i&gt;SerializationConfig.Feature.WRAP_ROOT_VALUE&lt;/i&gt;; 
      and there is also now a new annotation -- &lt;i&gt;@JsonRootName &lt;/i&gt;-- that 
      can be used to use custom wrapper name instead of the simple class name. 
      This is useful with interoperability, as some frameworks insist on 
      adding such wrappers.
    &lt;/p&gt;
    &lt;p&gt;
      One of few improvements to JAX-RS provider is that now you can add 
      @JsonView annotation to JAX-RS resource methods, and if one is found, it 
      will be set as the active Serialization View during serialization of the 
      result value.
    &lt;/p&gt;
    &lt;p&gt;
      One nice ergonomic improvement is the ability to use much more compact 
      configuration methods for changing default introspection visibility 
      levels.&lt;br&gt;For example, you can use:
    &lt;/p&gt;
    &lt;pre&gt;  objectMapper.setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);&lt;/pre&gt;
    &lt;p&gt;
      to make all fields auto-detectable, regardless of their visibility. Or, 
      to prevent all auto-detection, you could use:
    &lt;/p&gt;
    &lt;pre&gt;  objectMapper.setVisibilityChecker(m.getVisibilityChecker()
  	.with(JsonAutoDetect.Visibility.NONE));&lt;/pre&gt;
    &lt;p&gt;
      An improvement to naming strategy support is inclusion of one &amp;quot;standard&amp;quot; 
      naming strategy -- CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES -- which 
      converts between standard Java Bean names (that setters and getters 
      use), and C-style names (like used by Twitter). You can enable this 
      converter by:
    &lt;/p&gt;
    &lt;pre&gt;  mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);&lt;/pre&gt;
    &lt;p&gt;
      and from there on, can consume JSON like:
    &lt;/p&gt;
    &lt;pre&gt; { &amp;quot;first_name&amp;quot; : &amp;quot;Joe&amp;quot; }&lt;br&gt;&lt;/pre&gt;
    &lt;p&gt;
      to bind to class like:
    &lt;/p&gt;
    &lt;p&gt;
      public class Name { public String firstName; }
    &lt;/p&gt;
    &lt;p&gt;
      without having to use @JsonProperty to fix name mismatch.
    &lt;/p&gt;
    &lt;p&gt;
      As to sub-typing, you can now use new @JsonTypeInfo property 
      defaultSubType to indicate, as name suggests, default sub-type to use in 
      case where type name was missing or could not be resolved: use it like:
    &lt;/p&gt;
    &lt;pre&gt;  @JsonSubType(use=Id.NAME, include=As.PROPERTY, defaultSubType=GenericImpl.class)
  public abstract class BaseType { }&lt;/pre&gt;
    &lt;p&gt;
      And finally, one improvement to Json Filter functionality is ability to 
      specify that it is ok to use a filter id that does not refer to an 
      actual filter (i.e. can not be resolved by the currently configured 
      filter provider) -- use 'SimpleFilterProvider.setFailOnUnknownId(false)' 
      to make this the default behavior. Missing filter is then assumed to 
      mean &amp;quot;no filtering&amp;quot;, that is, serialization is handled as if no filter 
      was specified.
    &lt;/p&gt;</description>
<link>http://www.cowtowncoder.com/blog/archives/10-01-2011_10-31-2011.html#463</link>
<guid>http://www.cowtowncoder.com/blog/archives/10-01-2011_10-31-2011.html#463</guid>

<category>Java</category>

<category>JSON</category>

<pubDate>Tue, 11 Oct 2011 20:21:19 -0700</pubDate>
</item>

<item>
<title>Advanced filtering with Jackson, Json Filters</title>
<description>&lt;p&gt;
      I wrote a bit earlier on &amp;quot;&lt;a href=&quot;/blog/archives/2011/02/entry_443.html&quot;&gt;filtering 
      properties with Jackson&lt;/a&gt;&amp;quot;. While it was comprehensive in that all 
      main methods of filtering were covered, there wasn't much depth. 
      Specifically, only very basic usage of Json Filters (&lt;i&gt;@JsonFilter&lt;/i&gt; 
      annotation, &lt;i&gt;SimpleFilterProvider&lt;/i&gt; as provider) was considered. 
      This approach does allow more dynamic filtering than, say, &lt;i&gt;@JsonView&lt;/i&gt;, 
      but it is still somewhat limited. So let's consider more advanced 
      customizability.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;1. Refresher on Json Filters&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Ok, so the basic idea with Json Filters is that:
    &lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;
        Classes can have an associated Filter Id, which defines logical filter 
        to use.
      &lt;/li&gt;
      &lt;li&gt;
        A provider is needed to get the actual filter instance to use, given 
        id: this will be configured by assigning a FilterProvider (such as 
        'SimpleFilterProvider') to ObjectMapper or ObjectWriter.
      &lt;/li&gt;
      &lt;li&gt;
        Jackson will dynamically (and efficiently) resolve filter given class 
        uses, dynamically, allowing per-call reconfiguration of filtering.
      &lt;/li&gt;
    &lt;/ol&gt;
    &lt;p&gt;
      From this it is clear that there are 2 main things you can configure: 
      mechanism that is used to find Filter id of a given class, and mechanism 
      used for mapping this id to actual filter used (implementation of which 
      can be as complicated as you want).
    &lt;/p&gt;
    &lt;p&gt;
      So let's have a look at both parts.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;2. Configuring mapping from id to filter instance&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Of mechanisms, latter one may be easier to understand and use: one just 
      has to implement '&lt;i&gt;FilterProvider&lt;/i&gt;', which has but one method to 
      implement:
    &lt;/p&gt;
    &lt;pre&gt;  public abstract class FilterProvider {
    public abstract BeanPropertyFilter findFilter(Object filterId);
  }&lt;/pre&gt;
    &lt;p&gt;
      given this, 'SimpleFilterProvider' is little more than a &lt;i&gt;Map&amp;lt;String,BeanPropertyFilter&amp;gt;&lt;/i&gt;, 
      except for adding couple of convenience factory methods that build 
      'SimpleBeanPropertyFilter' instances given property names, so you 
      typically just instantiate one with calls like:
    &lt;/p&gt;
    &lt;pre&gt;  SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.filterOutAllExcept(&amp;quot;a&amp;quot;));&lt;/pre&gt;
    &lt;p&gt;
      which would out all properties except for one named &amp;quot;a&amp;quot;. This filter is 
      then configured with ObjectMapper like so:
    &lt;/p&gt;
    &lt;pre&gt;  FilterProvider fp = new SimpleFilterProvider().addFilter(&amp;quot;onlyAFilter&amp;quot;, filter);
  objectMapper.writer(fp).writeValueAsString(pojo);&lt;/pre&gt;
    &lt;p&gt;
      which would, then, apply to any Java type configured to use filter with 
      id &amp;quot;onlyAFilter&amp;quot;.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;3. Configuring discovery of filter id&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      From above example we know we need to indicate classes that are to use 
      our &amp;quot;onlyAFilter&amp;quot;. The default mechanism is to use:
    &lt;/p&gt;
    &lt;pre&gt;  @JsonFilter(&amp;quot;onlyAFilter&amp;quot;)
  public class FilteredPOJO {
    //...
  }&lt;/pre&gt;
    &lt;p&gt;
      But this is just the default. How so? The way Jackson figures out its 
      annotation-based configuration is actually indirect, and fully 
      customizable: all interaction is through configured '&lt;i&gt;AnnotationIntrospector'&lt;/i&gt; 
      object, which amongst other things defines this method:
    &lt;/p&gt;
    &lt;pre&gt;  public Object findFilterId(AnnotatedClass ac);&lt;/pre&gt;
    &lt;p&gt;
      which is called when serializer needs to determine id of the filter to 
      apply (if any) for given class. Since the default implementation 
      (org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector) has 
      everything else working fine, what we can do is to sub-class it and 
      override this method.&lt;br&gt;For example:
    &lt;/p&gt;
    &lt;pre&gt;  public class MyFilteringIntrospector extends JacksonAnnotationIntrospector
  {
    @Override
    public Object findFilterId(AnnotatedClass ac) {
      // First, let's consider @JsonFilter by calling superclass
      Object id = super.findFilterId(ac);
      // but if not found, use our own heuristic; say, just use class name as filter id, if there's &amp;quot;Filter&amp;quot; in name:
      if (id == null) {
        String name = ac.getName();
        if (name.indexOf(&amp;quot;Filter&amp;quot;) &amp;gt;= 0) {
          id = name;
        }
      }
      return id;
    }
  }&lt;/pre&gt;
    &lt;p&gt;
      Above functionality is just to show what is possible, not that it makes 
      sense. Alternatively you could of course define your own annotations to 
      check; or have List of known class names, check class definition or 
      interfaces type implements. The main point is just that you are not 
      limited to using &lt;i&gt;@JsonFilter&lt;/i&gt; annotation, but can use pretty much 
      any logic you want, within limits of your coding skills.
    &lt;/p&gt;
    &lt;p&gt;
      The only caveat is that the resolution from Class to matching id is only 
      guaranteed to be called once per ObjectMapper; so any variation in 
      filtering of specific class needs to happen at either mapping of id to 
      filter, or within filter itself.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;4. Don't be afraid of sub-classing (Jackson)AnnotationIntrospector&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Actually, the key take away might as well be the fact that 
      AnnotationIntrospector is designed to be customizable. It was initially 
      created to allow easy reuse of JAXB annotations (via &lt;i&gt;JAXBAnnotationIntrospector&lt;/i&gt;; 
      combining things with &lt;i&gt;AnnotationIntrospector.Pair&lt;/i&gt;); but it is 
      also a very powerful general-purpose customization mechanism. But at 
      this point quite underused one at that.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;5. Addendum&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Some additional notes based on feedback I received:
    &lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;
        Custom &lt;i&gt;BeanPropertyFilter&lt;/i&gt; implementations are obviously 
        powerful too: not only can they completely change what (if anything) 
        gets written for property, they can base this on all configuration 
        accessible via &lt;i&gt;SerializerProvider&lt;/i&gt; which is passed to 
        serializeAsField(): for example, it can check to see what 
        serialization view is available by calling '&lt;i&gt;provider.getSerializationView()&lt;/i&gt;'.
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
      
    &lt;/p&gt;</description>
<link>http://www.cowtowncoder.com/blog/archives/09-01-2011_09-30-2011.html#461</link>
<guid>http://www.cowtowncoder.com/blog/archives/09-01-2011_09-30-2011.html#461</guid>

<category>Java</category>

<category>JSON</category>

<category>Open Source</category>

<pubDate>Wed, 28 Sep 2011 19:38:34 -0700</pubDate>
</item>

<item>
<title>Traversing JSON trees with Jackson</title>
<description>&lt;p&gt;
      &lt;b&gt;1. Three models to rule the...&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      One of &lt;a href=&quot;/blog/archives/2009/01/entry_131.html&quot;&gt;three canonical 
      JSON processing models&lt;/a&gt;, tree model, may look a bit like a red-headed 
      stepchild. The amount of effort so far spent on both developing and 
      documenting Jackson data-binding functionality is an order of magnitude 
      higher than all the work for tree model functionality. And considering 
      how much more effort using stream-based processing takes, surprisingly 
      many developers choose it over tree handling.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;2. Why I never really liked tree model that much&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      I confess to having slight aversion to using JSON trees as well; but I 
      have a reasonable excuse: I grow to hate tree-based models with XML. 
      Having survived bad experiences of XML DOM processing (which is both 
      cumbersome and inefficient at same time) tends to inoculate one against 
      further infections. I know this is bit of unjustified bias, considering 
      that most problems with DOM had nothing to do with the basic idea of an 
      in-memory tree model (and not all even due to it being XML...)
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;3. ... even though I perhaps should have&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      But Jackson actually does provide reasonable support for JSON trees with 
      its JsonNode based model, and many brave developers have put it to good 
      use. And due to Jackson's extensive support for efficient conversions 
      between models (that is, ability to both combine approaches and to 
      convert data as needed), you don't have to pick and choose just one 
      model but can combine strengths of each model. Tree model's expressive 
      power is actually very useful when doing pre- or post-processing of data 
      binding; or when building quick prototype systems.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;4. Basics&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      A &amp;quot;JSON tree&amp;quot; is defined by one simple thing: &lt;b&gt;&lt;i&gt;org.codehaus.jackson.JsonNode&lt;/i&gt;&lt;/b&gt; 
      object that acts as the tree of the logical tree. The root node is 
      usually of type 'ObjectNode' (and represents JSON Object), but most 
      operations (all read-operations, specifically) are exposed through basic 
      JsonNode interface.
    &lt;/p&gt;
    &lt;p&gt;
      There are three basic options for creating a JSON tree instance, all 
      accessible via ObjectMapper:
    &lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;
        Parse from a JSON source: &lt;b&gt;JsonNode root = mapper.readTree(json);&lt;/b&gt;
      &lt;/li&gt;
      &lt;li&gt;
        Convert from a POJO: &lt;b&gt;JsonNode root = mapper.valueToTree(pojo);&lt;/b&gt; 
        // special case of 'ObjectMapper.convertValue()'
      &lt;/li&gt;
      &lt;li&gt;
        Construct from scratch: &lt;b&gt;ObjectNode root = mapper.createObjectNode();&lt;/b&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
    &lt;p&gt;
      The choice largely depends on use case, that is, what do you have to 
      work with; whether you generating new tree from scratch, or modify an 
      existing JSON structure.
    &lt;/p&gt;
    &lt;p&gt;
      After you have the root node you can traverse it modify structure, and 
      convert to other representations (serialize as JSON, convert to a POJO).
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;5. Back &amp;amp; Forth&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Aside from the ability to convert a POJO to a tree, you can easily do 
      the reverse using &amp;quot;ObjectMapper.treeToValue()&amp;quot;. Or, if you happen to 
      need a &lt;i&gt;JsonParser&lt;/i&gt;, use &amp;quot;ObjectMapper.treeAsTokens()&amp;quot;. And to 
      create actual textual JSON, the regular &amp;quot;ObjectMapper.writeValue()&amp;quot; 
      works as expected.
    &lt;/p&gt;
    &lt;p&gt;
      In fact, from ObjectMapper's perspective, JsonNode is just another Java 
      type and is handled using serializers, deserializers which can be 
      overridden if you want to customize handling. You can even replace &lt;i&gt;JsonNodeFactory&lt;/i&gt; 
      that &lt;i&gt;ObjectMapper&lt;/i&gt; uses, if you want to provide custom JsonNode 
      implementation classes!
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;6. More convenient traversal&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      One of things that has quietly improved over time has been traversal. 
      Earliest Jackson versions just supported basic traversal like so:
    &lt;/p&gt;
    &lt;pre&gt;  JsonNode root = mapper.readTree(&amp;quot;{\&amp;quot;address\&amp;quot;:{\&amp;quot;zip\&amp;quot;:98040, \&amp;quot;city\&amp;quot;:\&amp;quot;Mercer Island\&amp;quot;}}&amp;quot;);
  JsonNode address = root.get(&amp;quot;address&amp;quot;);
  if (address != null &amp;amp;&amp;amp; address.has(&amp;quot;zip&amp;quot;)) {
    int zip = address.get(&amp;quot;zip&amp;quot;).getIntValue();
  }&lt;/pre&gt;
    &lt;p&gt;
      but it soon became apparent that null checks are a worthless hassle, so 
      alternative access, &amp;quot;path()&amp;quot; was quickly added. It allows for traversing 
      over virtual paths, without worrying whether a node exists: if one does 
      not exist, it will just be evaluate as &amp;quot;missing node&amp;quot; when trying to 
      access actual leaf value:
    &lt;/p&gt;
    &lt;pre&gt;  JsonNode root = ...;
  int zip = root.path(&amp;quot;address&amp;quot;).path(&amp;quot;zip&amp;quot;).getValueAsInt(); // if no such path, returns 0
  // could also do:
  JsonNode zipNode = root.path(&amp;quot;address&amp;quot;).path(&amp;quot;zip&amp;quot;);
  if (zipNode.isMissingNode()) { // true if no such path exists
  }&lt;/pre&gt;
    &lt;p&gt;
      This is fine and dandy for read-only use cases, but it does not help 
      when trying to add things -- while you can traverse path that does not 
      really exist, you can not add anything to it. To address this 
      shortcoming, Jackson 1.8 comes equipped with &amp;quot;with()&amp;quot; method, which will 
      actually create the path if it does not exist. So you can finally write 
      something like this:
    &lt;/p&gt;
    &lt;pre&gt;  JsonNode root = ObjectMapper.createObjectNode();
  // note: JsonNode.with() returns 'JsonNode'; but ObjectNode.with() 'ObjectNode' -- go contra-variance!
  root.with(&amp;quot;address&amp;quot;).put(&amp;quot;zip&amp;quot;, 98040);&lt;/pre&gt;
    &lt;p&gt;
      which actually makes Jackson Tree usage almost as convenient as I would 
      like it to be. It is especially useful when materializing full trees 
      from scratch: you can implicitly build the tree structure just by 
      traversing it!
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;7. More?&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Jackson tree model is still somewhat spartan, especially compared to 
      features galore of data binding. Going forward it would be nice to add 
      support for things like:
    &lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;
        Simple path language (JsonPath, JsonQuery?) support, to be able to 
        evaluate expressions to locate nodes.
      &lt;/li&gt;
      &lt;li&gt;
        Filtering during construction, to create trimmed/pruned trees, 
        sub-trees
      &lt;/li&gt;
      &lt;li&gt;
        More advanced find methods? There already exists a few &amp;quot;findXxx()&amp;quot; 
        methods in JsonNode, but more would make sense, esp. with configurable 
        matchers or filters
      &lt;/li&gt;
      &lt;li&gt;
        Method names are bit too verbose (mostly due to historical reasons -- 
        I didn't realize early enough that long method names can hurt when 
        chained calls are used)
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
      But as usual, much of Jackson development work is feedback-driven -- 
      features that get used also get more likely further improved. So if you 
      do find Tree Model useful, let development team know that!
    &lt;/p&gt;</description>
<link>http://www.cowtowncoder.com/blog/archives/08-01-2011_08-31-2011.html#460</link>
<guid>http://www.cowtowncoder.com/blog/archives/08-01-2011_08-31-2011.html#460</guid>

<category>Java</category>

<category>JSON</category>

<pubDate>Fri, 12 Aug 2011 22:55:28 -0700</pubDate>
</item>

<item>
<title>One of coolest, least well-known Jackson features: Mr Bean, aka &quot;abstract type materialization&quot;</title>
<description>&lt;p&gt;
      &lt;b&gt;1. Quest for simplest JSON processing, eliminating monkey code: 
      &amp;quot;struct classes&amp;quot;&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      I have found myself using &amp;quot;Java structs&amp;quot; quite often, when accessing 
      JSON services from Java. By this I mean simple public-field-only classes 
      like:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;public class RequestDTO {
 public long requestId;
 public String callerId;
}&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      While many Java newbies think there is something wrong in using public 
      fields, there is actually very little harm in using such classes for 
      simple data transfer, if no actual business logic is needed for classes 
      themselves.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;2. But sometimes &amp;quot;real&amp;quot; classes would be nice&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Then again, sometimes it would be nice to use more full-featured 
      Bean(-like) POJOs. Perhaps we want to add some input validation for 
      setters; or add convenience accessors, or even just occasional 
      'toString()' implementation.
    &lt;/p&gt;
    &lt;p&gt;
      For above example, we might want to get something like:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;public class RequestImpl&lt;br&gt;{
  private long requestId;
  private String callerId;

  public RequstImpl() { }

  public long getRequestId() { return requestId; }
  public String getCallerId() { return callerId; }

  public void setRequestId(long l) { requestId = l;
  public void setCallerId(String s) { callerId = s; }

  @Override
  public String toString() { return String.format(&amp;quot;[request: id %d, caller %s]&amp;quot;, requestId, callerId); }
}&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      But ideally we would usually just define something like
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;public interface Request {
  public long getRequestId();
  public String getCallerId();

  public void setRequestId(long l);
  public void setCallerId(String s);
}&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      and somehow get an implementation; alas, that usually means writing 
      boiler-plate implementation for that interface (and if we are 
      masochists, sometimes even intermediate abstract classes...)
    &lt;/p&gt;
    &lt;p&gt;
      So what's the problem here? I don't particular like writing monkey code 
      to declare basic setters, getters, and fields; especially when there is 
      nothing interesting going on there, just mechanical typing. And while 
      one can use IDEs to generate sources, this only helps with 
      bootstrapping: you still get more source code to maintain, which 
      translates to more place where bugs may hide when definitions are 
      edited. Similarly various annotation-based post-processors seem alien to 
      me if they just produce more source code to compile.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;3. So why not just like... get implementations &amp;quot;materialize&amp;quot;?&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      But while I don't like the idea of getting yet more source code 
      generated to be compiled, maintained, I do like the idea of getting 
      actual implementation classes dynamically.
    &lt;/p&gt;
    &lt;p&gt;
      And this is where entry #6 of &amp;quot;&lt;a href=&quot;/blog/archives/2010/11/entry_434.html&quot;&gt;7 
      Jackson killer features&lt;/a&gt;&amp;quot; comes in: enter mr. Bean! When enabled, it 
      can actually materialize concrete implementations as needed.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;4. Mr Bean: basics&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      (from FasterXML &lt;a href=&quot;http://wiki.fasterxml.com/JacksonFeatureMaterializedBeans&quot;&gt;Mr 
      Bean Wiki page&lt;/a&gt;)
    &lt;/p&gt;
    &lt;p&gt;
      Basic usage is simple: you need jackson mrbean jar (included in Jackson 
      distribution), and need to enable functionality with:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;  ObjectMapper mapper = new ObjectMapper();
  mapper.registerModule(new MrBeanModule());&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      and then just watch interfaces appear: for example, with above example:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;  Request request = objectMapper.readValue(jsonInput, Request.class); // where Request is an interface&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      What happens here is that mr Bean extension hooks with ObjectMapper, and 
      whenever an abstract type is encountered and there is no concrete class 
      available (no abstract type mapping; no annotation to indicate concrete 
      type; no @JsonTypeInfo to provide subtype information), it is asked to 
      &amp;quot;materialize&amp;quot; concrete type.
    &lt;/p&gt;
    &lt;p&gt;
      Materialization simply means generating bytecode using ASM, based on 
      getters and/or setters; adding necessary internal fields, loading class 
      and returning it to caller. After this, core Jackson mapper can 
      introspect all information it needs, and what you get is an instance of 
      this implementation. Implementations are cached for later use, and 
      performance-wise they behave similarly to manually implemented ones 
      would.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;5. Mr Bean: but wait! There's more!&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Ok: so we can get monkey code materialized: getters and setters are 
      implemented, and internal fields added to store values. But this is just 
      the beginning.
    &lt;/p&gt;
    &lt;p&gt;
      First: if you do not need to use setters yourself you can freely omit 
      them from interface definition.&lt;br&gt;Mr Bean is smart enough to figure out 
      that setters are typically needed to set values (or public fields) if 
      there are getters materialized.&lt;br&gt;So you can simplify your 
      interfaces/abstract classes to look something like:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;  public interface RequestWithoutSetters {
    public long getRequestId();
    public String getCallerId();
  }&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      and things will still work just fine; you can't access setters (which 
      actually may be a good thing), but Jackson data binder can populate 
      values just fine (internally either setters get generated; or public 
      fields added to implementation, this is an implementation detail).
    &lt;/p&gt;
    &lt;p&gt;
      Aside from simplistic get/set Bean it is more commont to want a partial 
      implementation; an abstract class where you provide some methods and/or 
      fields, but can leave implementation of trivial properties to Mr Bean. 
      This it can do just fine: mr Bean can materialize abstract classes, just 
      &amp;quot;filling in the blanks&amp;quot;.
    &lt;/p&gt;
    &lt;p&gt;
      So you can ask for a class like:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;  public abstract class RequestBase {
    public long getRequestId();
    public String getCallerId();
    
    @Override public String toString() {
      return String.format(&amp;quot;[request: id %d, caller %s]&amp;quot;, requestId, callerId); }
    }
  }&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      and things work, well, as expected. Note, too, that you can implement 
      setters and getters, not just &amp;quot;other&amp;quot; methods.
    &lt;/p&gt;
    &lt;p&gt;
      And finally: you can use annotations normally as well, adding them to 
      your interface/abstract class definition. Thanks to Jackson's powerful 
      and versatile annotation handling (including annotation &amp;quot;inheritance&amp;quot; 
      for methods), you can do something like:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;  // JSON we get has weird names; need to annotate
  public abstract class RequestBase {
    @JsonProperty(&amp;quot;REQID&amp;quot;)
    public long getRequestId();
    @JsonProperty(&amp;quot;CALLERID&amp;quot;)
    public String getCallerId();
  }&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      and get things configured as per annotations.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;6. Known issues?&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Mr Bean seems to work to degree I need it to work. But there are some 
      potential concerns you may need to be aware of:
    &lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;
        Jackson has multiple ways of dealing with abstract types: do you want 
        bean materialized or not? As mentioned above, mr Bean does not try to 
        materialize abstract types that seem to expect different kind of 
        handling; for example, if interface has @JsonTypeInfo annotation, 
        assumption is that polymorphic handling can figure out actual type. 
        But it is possible that there are corner cases (esp. when using 
        &amp;quot;default typing&amp;quot;) there might be conflicts. So polymorphic types may 
        not mix well with mr Bean materialization
      &lt;/li&gt;
      &lt;li&gt;
        Generic signatures may not be added as expected. Although you can 
        declared generic types for abstract methods just fine, and Jackson 
        mapper should fine declarations, there are some issues due to 
        complexities in getting generic declarations work with ASM. You may 
        need to use additional annotations (@JsonDeserialize(contentAs=...)) 
        in some cases.
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
      Above is just a list of potential concerns -- as far as I know, they 
      haven't been found to be much of a problem in actual use so far.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;7. What Next?&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Usage, usage, usage! It would be great to get more Jackson users use 
      this potentially hugely work-saving feature. And if you find the feature 
      useful, make sure to let your friends know! (if you hate it, just let me 
      know :-) ).
    &lt;/p&gt;</description>
<link>http://www.cowtowncoder.com/blog/archives/08-01-2011_08-31-2011.html#459</link>
<guid>http://www.cowtowncoder.com/blog/archives/08-01-2011_08-31-2011.html#459</guid>

<category>Java</category>

<category>JSON</category>

<pubDate>Thu, 11 Aug 2011 19:07:41 -0700</pubDate>
</item>

<item>
<title>Jackson tips: using @JsonAnyGetter/@JsonAnySetter to create &quot;dyna beans&quot;</title>
<description>&lt;p&gt;
      One relatively common &amp;quot;special&amp;quot; POJO is so-called dynamic bean 
      (&amp;quot;dyna-bean&amp;quot;), which is sort of combination of regular bean and basic 
      Java Map; with zero or more properties with known name, and extensible 
      set of 'other' key/value pairs.
    &lt;/p&gt;
    &lt;p&gt;
      Here's what such a POJO might look like:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;public class DynaBean
{
    // Two mandatory properties
    protected final int id;
    protected final String name;

    // and then &amp;quot;other&amp;quot; stuff:
    protected Map&amp;lt;String,Object&amp;gt; other = new HashMap&amp;lt;String,Object&amp;gt;();

    public DynaBean(int id, String name)
    {
        this.id = id;
        this.name = name;
    }

    public int getId() { return id; }
    public String getName() { return name; }

    public Object get(String name) {
        return other.get(name);
    }

    public void set(String name, Object value) {
        other.put(name, value);
    }
}&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      Since Jackson can serialize Bean as well as Maps, what is the problem? 
      As presented, bean would not serialize and deserialize as expected, 
      although it could be modified to just return Map of &amp;quot;other&amp;quot; properties, 
      and deserialize them back. This would work, but would result in an 
      additional level of wrapping, so that secondary properties would be 
      within a separate JSON Object.
    &lt;/p&gt;
    &lt;p&gt;
      But Jackson can actually be made to work with such POJOs: here is one 
      way to do it:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;public class DynaBean
{
    // Two mandatory properties
    protected final int id;
    protected final String name;

    // and then &amp;quot;other&amp;quot; stuff:
    protected Map&amp;lt;String,Object&amp;gt; other = new HashMap&amp;lt;String,Object&amp;gt;();

    // Could alternatively add setters, but since these are mandatory
    @JsonCreator
    public DynaBean(@JsonProperty(&amp;quot;id&amp;quot;) int id, @JsonProperty(&amp;quot;name&amp;quot;) String name)
    {
        this.id = id;
        this.name = name;
    }

    public int getId() { return id; }
    public String getName() { return name; }

    public Object get(String name) {
        return other.get(name);
    }

    // &amp;quot;any getter&amp;quot; needed for serialization    
    @JsonAnyGetter
    public Map&amp;lt;String,Object&amp;gt; any() {
        return other;
    }

    @JsonAnySetter
    public void set(String name, Object value) {
        other.put(name, value);
    }
}&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      And there we have it: serializes and deserializes nicely.
    &lt;/p&gt;
    &lt;p&gt;
      Share and enjoy...
    &lt;/p&gt;</description>
<link>http://www.cowtowncoder.com/blog/archives/07-01-2011_07-31-2011.html#458</link>
<guid>http://www.cowtowncoder.com/blog/archives/07-01-2011_07-31-2011.html#458</guid>

<category>Java</category>

<category>JSON</category>

<pubDate>Tue, 26 Jul 2011 22:29:46 -0700</pubDate>
</item>

<item>
<title>Jackson Annotations: @JsonCreator demystified</title>
<description>&lt;p&gt;
      One of more powerful features of Jackson is its ability to use arbitrary 
      constructors for creating POJO instances, by indicating constructor to 
      use with @JsonCreator annotation. A simple &lt;a href=&quot;http://wiki.fasterxml.com/JacksonFeatureCreators&quot;&gt;explanation&lt;/a&gt; 
      of this feature can be found from FasterXML wiki; but it only scratches 
      surface of all the power this annotation exposes. This article will 
      expand on what can be done with this annotation.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;1. What are Creators in Jackson?&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &amp;quot;Creator&amp;quot; refers to two kinds of things: constructors, and static 
      factory methods: both can be used to construct new instances. Term 
      &amp;quot;creator&amp;quot; is used for convenience, to avoid repeating &amp;quot;constructor or 
      static factory method&amp;quot;.
    &lt;/p&gt;
    &lt;p&gt;
      In some places term &amp;quot;creator method&amp;quot; may be used, although this is not 
      preferred (since constructors are not regular methods). In case of 
      static factory methods, method's return type must be same as declaring 
      classes type, or its subtype.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;2. Two kinds of creators: property-based, delegate-based&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      There are two kinds of creators that one can denote using @JsonCreator: 
      property- and delegate-based creators:
    &lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;
        Property-based creators take one or more arguments; all of which MUST 
        be annotated with @JsonProperty, to specify JSON name used for 
        property. They can only be used to bind data from JSON Objects; and 
        each parameter represents one property of the JSON Object; type of 
        property being used for binding data to be passed as that parameter 
        when calling creator.
      &lt;/li&gt;
      &lt;li&gt;
        Delegate-based creators take just one argument, which is NOT annotated 
        with @JsonProperty. Type of that property is used by Jackson to bind 
        the whole JSON value (JSON Object, array or scalar value), to be 
        passed as value of that one argument.
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
      In addition, sometimes distinction is made between two kinds of 
      delegate-based creators: those that take scalar value 
      (int/Integer/long/Long, string or boolean/Boolean) and other delegates. 
      There is only one important distinction from user perspective: there is 
      no creator overloading, so only one creator of each type is alllowed. 
      More on this later on.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;3. Property-based creators&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Property-based creators are typically used to pass one or more 
      obligatory parameters into constructor (either directly or via factory 
      method). If a property is not found from JSON, null is passed instead 
      (or, in case of primitives, so-called default value; 0 for ints and so 
      on).
    &lt;/p&gt;
    &lt;p&gt;
      A typical use case could look like this:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;  public class NonDefaultBean {
    private final String name;
    private final int age;

    private String type;
  
    @JsonCreator
    public NonDefaultBean(@JsonProperty(&amp;quot;name&amp;quot;) String name, @JsonProperty(&amp;quot;age&amp;quot;) int age)
    {
      this.name = name;
      this.age = age;
    }

    public void setType(String type) {
      this.type = type;
    }
  }&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      where two properties are passed via constructor creator; and then a 
      third one through regular setter. Note that we could have as well used a 
      static factory method as creator.
    &lt;/p&gt;
    &lt;p&gt;
      One thing to note about these creators is that it is possible to create 
      a sub-type of class: this allows implementing polymorphic handling 
      manually if necessary, although with limitation that creator itself must 
      be in base class. But all other properties can be passed to sub-classes, 
      as Jackson is smart enough to check type and properties of the actual 
      instance, not just declared nominal type.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;4. Delegate-based creators&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Whereas you can use multiple arguments with properties-based creators 
      (but must explicitly name them), delegate-based creator takes just one 
      argument. Jackson will then use type of that argument for data-binding, 
      and bind JSON to that type before calling creator. Creator is then free 
      to construct instance any way it wants to. A typical usage looks like:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;  public class ObjectDelegateBean
  {
    private final String name;
    private final int age;
    private final String type;

    @JsonCreator
    public ObjectDelegateBean(Map&amp;lt;String,Object&amp;gt; props)
    {
      name = (String) props.get(&amp;quot;name&amp;quot;);
      age = (Integer) props.get(&amp;quot;age&amp;quot;);
      type = (String) props.get(&amp;quot;type&amp;quot;);
    }
  }  &lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      In this case we just manually extract properties from a Map (where JSON 
      data has been bound to &amp;quot;natural&amp;quot; types; Maps, Lists, String, Numbers and 
      Booleans). Other common delegate types used are JsonNode (to use JSON 
      tree as intermediate form), TokenBuffer (stream of exact JsonTokens, 
      which can be used to create a JsonParser) and basic java.lang.Object 
      (which would map to natural types mentioned earlier).
    &lt;/p&gt;
    &lt;p&gt;
      But as I mentioned earlier, it is also possible to use variants to 
      handle scalar types: specifically ints (or Integers), long (or Longs) 
      and Strings (in future support will also be added for double/Double).
    &lt;/p&gt;
    &lt;p&gt;
      For example:
    &lt;/p&gt;
    &lt;hr&gt;
    

    &lt;pre&gt;  public class DateBean
  {
    private Date date;

    private DateBean(Date date) {
      this.date = date;
    }

    @JsonCreator
    public static DateBean factory(long timestamp) {
      Date d = new Date(timestamp);
      return new DateBean(d);
    }&lt;br&gt;  }&lt;/pre&gt;
    &lt;hr&gt;
    

    &lt;p&gt;
      which is bit contrived example, as Jackson can easily bind 
      java.util.Date directly. However, some JSON formats support overloading, 
      or try to minimize size; and JSON Strings are sometimes used to bundle 
      semi-structured data
    &lt;/p&gt;
    &lt;p&gt;
      One point to note about &amp;quot;scalar delegates&amp;quot; is that Jackson allows 
      overload of creators, as long as there is only one creator type per JSON 
      type: so you can have only one properties-based creator (or 
      delegate-based creator that takes something other than match of JSON 
      scalar type) , but there can be an additional string-delegate creator as 
      well as an int-delegate creator. Limitation is conceptually simple: 
      there must only be one applicable delegate to choose from, knowing type 
      of JSON value being bound -- and this is why delegates that take String, 
      Integer/int or Long/long are special cases of delegate-based creators.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;5. More to know?&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      In near future (Jackson 1.9) there will be significant improvements in 
      this area: new things called &amp;quot;value instantiators&amp;quot; will be allowed to 
      handle feature set similar to that of @JsonCreator; but that need not be 
      bound to existing Java constructors or factory methods.
    &lt;/p&gt;
    &lt;p&gt;
      
    &lt;/p&gt;
    &lt;p&gt;
      
    &lt;/p&gt;</description>
<link>http://www.cowtowncoder.com/blog/archives/07-01-2011_07-31-2011.html#457</link>
<guid>http://www.cowtowncoder.com/blog/archives/07-01-2011_07-31-2011.html#457</guid>

<category>Java</category>

<category>JSON</category>

<pubDate>Mon, 04 Jul 2011 16:56:25 -0700</pubDate>
</item>

<item>
<title>Oh yes, Jackson 1.8 was released a while ago... :-)</title>
<description>&lt;p&gt;
      Whoa. Looks like I forgot to blog about something rather big; the 
      release of Jackson 1.8.0. This is mostly because I did that shortly 
      before going for a nice long vacation, and by the time I came back, I 
      had forgotten most of it. Although was reminded by the release in form 
      of a nice long list of new bug reports. :-)
    &lt;/p&gt;
    &lt;p&gt;
      At any rate, I did write something about the release; so as a 
      background, here are contemporary accounts of the event:
    &lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;
        &lt;a href=&quot;http://jackson-users.ning.com/profiles/blogs/jackson-180-released&quot;&gt;Jackson 
        1.8.0 released!&lt;/a&gt; (at &lt;a href=&quot;http://jackson-users.ning.com/&quot;&gt;Jackson 
        User Group&lt;/a&gt;)
      &lt;/li&gt;
      &lt;li&gt;
        &lt;a href=&quot;http://wiki.fasterxml.com/JacksonRelease18&quot;&gt;JacksonRelease18&lt;/a&gt; 
        - list of all included features (at FasterXML &lt;a href=&quot;http://wiki.fasterxml.com/JacksonHome&quot;&gt;Jackson 
        Wiki&lt;/a&gt;)
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
      Actually, both of above enumerate all the new features, so I can't add 
      much more at detailed level.
    &lt;/p&gt;
    &lt;p&gt;
      But one thing that may not be obvious is that 1.8 was a huge step 
      forward to allow full power of configurability for Jackson Modules; 
      concept which was added in 1.7, but that really became useful enough for 
      major extensions in 1.8. As a result all the exciting modules (esp. ones 
      at &lt;a href=&quot;https://github.com/FasterXML&quot;&gt;FasterXML GitHub&lt;/a&gt;) -- 
      including &amp;quot;Jackson XML module&amp;quot; for XML-based serialization, 
      deserialization; Hibernate Jackson module, and even Jackson Scala module 
      -- require 1.8 as their baseline. And at least XML and Scala modules 
      were driving some of improvements made to module interface.
    &lt;/p&gt;
    &lt;p&gt;
      Another main goal was to focus on paying down sort of &amp;quot;feature debt&amp;quot; -- 
      something similar to technical debt, but relating to the fact that 
      sometimes oldest feature-requests tend to be forgotten, and development 
      focuses more on latest ideas. In 1.8, then, we saw completion of many 
      long-standing (and sometimes, long-ignored) feature requests.
    &lt;/p&gt;
    &lt;p&gt;
      Anyway -- it has been a while since 1.8.0 was released; and at this 
      point I am focused on starting work on 1.9.0. While only one new feature 
      has been completed, there is lots of work behind the scenes; much of 
      which is aimed at helping modules (or working on specific modules). But 
      more on these ventures in future blog posts; stay tuned!
    &lt;/p&gt;</description>
<link>http://www.cowtowncoder.com/blog/archives/07-01-2011_07-31-2011.html#456</link>
<guid>http://www.cowtowncoder.com/blog/archives/07-01-2011_07-31-2011.html#456</guid>

<category>Java</category>

<category>JSON</category>

<pubDate>Fri, 01 Jul 2011 19:32:42 -0700</pubDate>
</item>

<item>
<title>On proper performance testing of Java JSON processing</title>
<description>&lt;p&gt;
      Lately I have spent little time writing or worrying about the 
      performance of JSON processing on Java platform. As has been repeatedly 
      pointed out, JSON processing is typically NOT amongst biggest 
      bottlenecks, compared to aspects like database access or HTTP request 
      handling overhead.
    &lt;/p&gt;
    &lt;p&gt;
      But lately there seems to have been bit of renaissance on writing simple 
      Java JSON parsers, and typically these new projects also provide 
      performance tests that seek to prove the superior performance of the 
      offering. Alas, while writing performance tests is not exactly rocket 
      surgery, there are many pitfalls that can trip new performance engineers 
      and testers, rendering many of initial reports misleading at best. And 
      while the results are usually corrected over time, based on feedback, 
      first impressions tend to stick (&amp;quot;but wasn't XYZ the fastest things 
      ever?&amp;quot;).
    &lt;/p&gt;
    &lt;p&gt;
      So I figured that since I often end up pointing out issues with these 
      tests, I might as well summarize a list of typical problems that plague 
      new performance benchmarj. Maybe this helps in making the whole process 
      more efficient. At the very least I can just send a URL to point to this 
      entry, as a sort of starting point or FAQ.
    &lt;/p&gt;
    &lt;p&gt;
      With that, here is a collection of common problems with Java JSON 
      processing test suites. Many of these problems are also applicable for 
      related test suites, such as those testing other data formats (XML, 
      binary data formats) to large-scale data processing (map/reduce and 
      other &amp;quot;big data&amp;quot;).
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;1. Missing JVM warmup&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      For developers on non-managed languages (C/C++ for example), testing is 
      relatively simple: after binary code is loaded in (and perhaps test data 
      from disk), system is ready to measured, and there is little variance 
      between test runs, or need to repeat tests for large number of 
      repetitions.
    &lt;/p&gt;
    &lt;p&gt;
      This is very different for Java, since JVM is very much an adaptive 
      system: while program code is loaded as somewhat abstract bytecode, it 
      will be converted to native code (to have anything close to optimal 
      speed), and this occurs based on measurements that JVM itself does to 
      figure out parts that need to be optimized, on-the-fly. In addition, 
      garbage collection will have impact on performance. To complicate things 
      further the standard class library (JDK classes) is a big thing so that 
      its initialization takes much more time than that of native libraries 
      like libc for C. Trying to test Java libraries same way as C libraries 
      is a recipe for disaster.
    &lt;/p&gt;
    &lt;p&gt;
      What this means is that unless care is taken, measurements that do not 
      account for initial startup overhead may well be just measuring 
      efficiency of JVM at initializing itself, and to some degree complexity 
      of the library being tested (since one-time overhead of more complex, or 
      just bigger, libraries is higher than those of simplest libraries). But 
      what is not being tested is eventual steady-state performance of the 
      library. And since this steady-state is what actually matters most for 
      server-side processes, results will be irrelevant.
    &lt;/p&gt;
    &lt;p&gt;
      So unless you really want to just measure the startup time of an 
      application or library, make sure to run test code for a non-trivial 
      amount of time (at minimum, multiple seconds) first before starting 
      actual measurements. Ideally you should also run tests long enough to 
      get stable measurements. Ideally this steady state would be 
      statistically validated, which is one reason why performance test 
      frameworks are very useful for writing performance tests; typically 
      their authors have solved many of the obvious issues.
    &lt;/p&gt;
    &lt;p&gt;
      Another slightly subtles issue is that the order in which code is loaded 
      (and, over time, dynamically optimized) matters as well: often the test 
      that is run first is best optimized by JVM. This means that optimally 
      different tests (tests for different libraries) should be run on 
      separate JVMs, all &amp;quot;warmed up&amp;quot; running specific test case. This is 
      something that most performance benchmarking frameworks can also help 
      with (I am most familiar with &lt;a href=&quot;http://japex.java.net/&quot;&gt;Japex&lt;/a&gt;, 
      which already runs separate tests on separate JVMs).
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;2. Trivial payloads&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Another common mistake is that of using trivial data: something that is 
      so small that it:
    &lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;
        Is unlikely to be used as data for real production systems, and
      &lt;/li&gt;
      &lt;li&gt;
        Is so light-weight to process that test mostly checks how much 
        per-invocation overhead library has
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;p&gt;
      In case of JSON, for example, some tests give tiniest data snippest 
      (single String; array with one integer element). Unless your actual use 
      case revolved around such tiny data you probably should not be testing 
      such cases; or at least use wide set of likely input data, to emphasize 
      more common cases.
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;3. Incorrect input modeling&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      When testing processing of data formats, data usually comes from outside 
      JVM -- it may read from a storage device or received or sent over 
      network to/from external services. If so, data will arrive as a byte 
      stream of some kind, so the most natural representation is usually &lt;i&gt;java.io.InputSource&lt;/i&gt;. 
      Or, if data is length-prefixed, it may be processed by reading it all in 
      a byte array (or ByteBuffer), and offered to library using such 
      abstraction.
    &lt;/p&gt;
    &lt;p&gt;
      But (too) many performance tests start assume that input comes as a &lt;i&gt;java.lang.String&lt;/i&gt;. 
      This is peculiar, given that Strings are really things that only live 
      within JVM, and must always be constructed from a byte stream or buffer, 
      decoded from external encoding such UTF-8 or ISO-8859-1. About the only 
      case where input actually arrives as a String are unit tests; or 
      sometimes when another processing components has handled reading and 
      decoding of content.
    &lt;/p&gt;
    &lt;p&gt;
      Now: given that reading and character decoding are generally mandatory 
      steps, how is it actually achieved? Many libraries punt this issue by 
      just declaring that what they accept is just a String (or, sometimes, 
      Reader). This is functionally acceptable as JDK provides simple ways to 
      handle decoding (for example by using &lt;i&gt;java.io.InputStreamReader&lt;/i&gt;, 
      or constructing String instances by specifying encoding). But this also 
      happens to be one area where more advanced parsers can optimize 
      processing significantly, by making good use of specific properties of 
      encoding (for example, fact that JSON MUST be encoded using one of only 
      3 allowed Unicode-based encodings).
    &lt;/p&gt;
    &lt;p&gt;
      So the specific problem of using &amp;quot;too refined input&amp;quot; is two-fold:
    &lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;
        It underestimates real overhead of (JSON) processing, by omitting a 
        mandatory decoding step, and
      &lt;/li&gt;
      &lt;li&gt;
        It eliminates performance comparison of one important part of the 
        process, essentially punishing libraries that can do encoding step 
        more efficiently than others.
      &lt;/li&gt;
    &lt;/ol&gt;
    &lt;p&gt;
      Effects of decoding overhead are non-trivial: for JSON, it is common 
      that UTF-8 decoding can take nearly as much time as actual tokenization 
      (~= &amp;quot;parsing&amp;quot;) of decoded input; which is also why quite a bit of effort 
      has been spent to make parsers more efficient at decoding than 
      general-purpose UTF-8 decoders (such as one that JDK comes equipped 
      with).
    &lt;/p&gt;
    &lt;p&gt;
      &lt;b&gt;4. What am I testing again?&lt;/b&gt;
    &lt;/p&gt;
    &lt;p&gt;
      Another common mistake is that of vaguely (or not at all) defining of 
      what exactly is being measured. This actually starts even earlier, by 
      using incorrect terminology for the library: most JSON &amp;quot;parsers&amp;quot; are 
      much more than parsers. In fact, I can not think of a single Java JSON 
      library that was just a parser (or perhaps most accurately, tokenizer): 
      most new JSON processing libraries implement or embed a low-level parser 
      but also bundle a higher-level abstraction (either tree model or data 
      binding -- see &amp;quot;&lt;a href=&quot;/blog/archives/2009/01/entry_131.html&quot;&gt;three 
      ways to process JSON&lt;/a&gt;&amp;quot; for longer discussion on available processing 
      modes). This is not limited to JSON, by the way; with some other data 
      formats like XML things are even worse: many things called parsers (such 
      as DOM or JDOM) do not even include parser themselves! (instead, they 
      use actual low-level (SAX or Stax) XML parser and then just implement 
      tree model on top of actual parser.
    &lt;/p&gt;
    &lt;p&gt;
      But why does it matter? The basic issue is that sometimes comparison is 
      apples to oranges: for example, comparing a simple streaming parser to a 
      data-binding processor (or, one that provides tree model) is not a fair 
      comparison, given that functionality provided is very different, from 
      user perspective.
    &lt;/p&gt;
    &lt;p&gt;
      Going back to &amp;quot;JSON parser&amp;quot; misnomer: some of the tests choose to test 
      performance for specific processing model -- often Tree Model, probably 
      because the original &amp;quot;org.json parser&amp;quot; only offers this abstraction -- 
      but yet claim it as proof of &amp;quot;parser XXX is the Fastest Java JSON 
      parser!&amp;quot;. This is incorrect since it bundles together both low-level 
      parsing (which is most efficient to do with a minimal incremental 
      streaming parser) and building (and possibly manipulation) of a Tree 
      model on top. And to give some idea of relative performance: building of 
      a tree model can take more time than parsing (tokenizing) JSON content 
      -- this is similar to XML processing, where building of a DOM tree 
      typically does take more time (often 2x) than low-level parsing, 
      although JSON tree models are usually much simpler than XML tree models.
    &lt;/p&gt;
    &lt;p&gt;
      The important thing here is that test should clearly explain what is 
      being measured: and in cases where differing approaches are compared, 
      what are the trade-offs.
    &lt;/p&gt;</description>
<link>http://www.cowtowncoder.com/blog/archives/05-01-2011_05-31-2011.html#455</link>
<guid>http://www.cowtowncoder.com/blog/archives/05-01-2011_05-31-2011.html#455</guid>

<category></category>

<pubDate>Tue, 31 May 2011 22:10:37 -0700</pubDate>
</item>

</channel>
</rss>

