Same question posted on SO
http://stackoverflow.com/questions/19389696/how-do-i-marshall-nested-key-value-pairs-into-json-with-camel-and-jackson-librar



On Tue, Oct 15, 2013 at 9:47 PM, erj2code <p...@tetraconcepts.com> wrote:
> I have a Java project that currently returns a map to Camel without any
> nested key,value pairs, and the Jackson library marshalls it just fine into
> JSON using the Jackson library.
>
> For example if I put the following two key,values into a demoMap:
>
>     Map<String,String> demoMap = new TreeMap<String,String>
>     demoMap.put("key1","5");
>     demoMap.put("key2","10");
>
> I get the following JSON:
>
>     {"key1":"5","key2":"10"}
>
> However, now some of my key,value entries will have an optional visibility
> that I need to put as a nested key value in my map.  Any key,values that
> don't have an optional visibility will use the default.  So, for example if
> I specify visibilities for key1, and key2, but not key3 I want to get JSON
> out that looks like this:
>
>     {"key1":"5",
>      "key2":"10",
>      "key3":"17",
>      "visibility" : { "key1": "a&b&!c", "key2": "a&b", "_default": "a" }
>     }
>
> How can I get Camel to marshall a Java object with nested key,value pairs?
> I'm a visual learner, so a simple example would be helpful.
>
> I tried changing my Map to have a value as an object i.e.,:
>
>     Map<String,Object> demoMap = new TreeMap<String,Object>
>
> and then tried adding nested key,values for some keys with an ArrayList
> using
> http://examples.javacodegeeks.com/core-java/json/jackson/convert-java-map-to-from-json-using-jackson-example/
> for reference, but realized that this just gives me a bunch of nested values
> under a key, not a bunch of nested key,value pairs.
>
> Even when I tried it for grins, I got an error from the Camel processor with
> a java.lang.ClassCastException stating java.util.ArrayList cannot be cast to
> java.lang.String
>
> And similarly when I tried to nest a Map inside my demoMap I got this
> ClassCastException:
>
>     3244 [hello.world.request.timer] ERROR
> org.apache.camel.processor.DefaultErrorHandler  - Failed delivery for
> exchangeId: e6518e39-89b7-435e-96d9-ce26811ac67e. Exhausted after delivery
> attempt: 1 caught: java.lang.ClassCastException: java.util.HashMap cannot be
> cast to java.lang.String
>
> So I know how NOT to do it. :-/
>
> I re-read the Camel JSON documentation at http://camel.apache.org/json.html
> but as of this writing it doesn't specify an example with nested key,value
> pairs.
>
>
>
> --
> View this message in context: 
> http://camel.465427.n5.nabble.com/How-do-I-marshall-nested-key-value-pairs-into-JSON-with-Camel-and-Jackson-library-tp5741618.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cib...@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Reply via email to