Hi Charles

Thanks for sharing this.

I think you have a good solution so far.

We may want to improve the Camel <json> data format to allow to
specify a custom ObjectMapper.
Unfortunately the Camel json data format is a dual library that
supports xstream and jackson.
So if we add this option, then its specific to jackson only.

We should possible separate the 2 json libraries in Camel 3.0, so
there is a pure jackson and another for xstream.


On Fri, Dec 9, 2011 at 4:37 PM, Charles Hudak <[email protected]> wrote:
> In order to handle the configuration we built a wrapper around the object
> mapper that handles it all during construction:
>
> public class XxxObjectMapper extends ObjectMapper
> {
>    public XxxObjectMapper()
>    {
>        super();
>        configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
>
> getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
>
>        SimpleModule module = new SimpleModule("xxx-module", new Version(2,
> 0, 0, "RELEASE"));
>        module.addSerializer(XxxDateSerializer.instance);
>
>        registerModule(module);
>    }
> }
>
> We are exchanging JSON data with android devices so we are using Jackson on
> both ends. We had to implement a custom serializer
> as a workaround for a problem we were having on the device side as well so
> we just use the same ObjectMapper on both ends:
>
> /**
>  * Custom das date serializer that uses JODA time to get around a bug in
> SimpleDateFormat
>  * in Harmony:
>  *
>  * http://code.google.com/p/android/issues/detail?id=8258
>  *
>  * @author Charles Hudak
>  * @since Feb 16, 2011
>  *
>  */
> public class XxxDateSerializer extends ScalarSerializerBase<java.util.Date>
> {
>    public final static XxxDateSerializer instance = new
> XxxDateSerializer();
>
>    public XxxDateSerializer() { super(java.util.Date.class); }
>
>    @Override
>    public void serialize(java.util.Date value, JsonGenerator jgen,
> SerializerProvider provider)
>        throws IOException, JsonGenerationException
>    {
>        DateTimeFormatter dtf =
> ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
>        jgen.writeString(dtf.print(new DateTime(value)));
>    }
>
>    @Override
>    public JsonNode getSchema(SerializerProvider provider, Type typeHint)
>        throws JsonMappingException
>    {
>        return createSchemaNode("string", true);
>    }
> }
>
> For what it is worth, I found that while I can't use the <dataFormats>
> configuration element to automatically wire up all my json configurations,
> I can do it this way:
>
> Data Format configuration:
>
>    <!-- Custom dataformat for json -->
>    <bean id="purchaseRequestDataFormat"
> class="org.apache.camel.component.jackson.JacksonDataFormat">
>      <constructor-arg ref="jsonObjectMapper"/><!-- Our custom object
> mapper -->
>      <constructor-arg value="com.xxx.common.json.model.PurchaseRequest"/>
>    </bean>
>
> In my route:
>
>     <!-- convert from json to object -->
>     <camel:unmarshal ref="purchaseRequestDataFormat"/>
>
> On Thu, Dec 8, 2011 at 11:27 PM, Claus Ibsen <[email protected]> wrote:
>
>> Hi
>>
>> Do you mind posting how you configure Jackson currently to fx marshal
>> dates as you do etc.
>>
>> We may need to add some configuration options to the Jackson JSON data
>> format, so you can configure this as well easily.
>>
>>
>> On Fri, Dec 9, 2011 at 3:29 AM, Charles Hudak <[email protected]>
>> wrote:
>> > Hi,
>> >
>> > I'm new to Camel and am setting up some routes to do some JSON
>> marshalling
>> > and unmarshalling. We use Jackson and have been doing this with spring
>> > integration and spring mvc for building RESTful web applications that
>> > consume and produce JSON.
>> >
>> > We use a custom configuration of the Jackson object mapper that marshals
>> > dates as ISO-8601 strings (among other things) rather than ms. I can't
>> seem
>> > to find anyway to get camel to use my ObjectMapper; there doesn't appear
>> to
>> > be any injection point when the DataFormat is configured. Is this
>> possible?
>> > I'm using xml configuration not java DSL.
>> >
>> > Thanks,
>> >
>> > Charles
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> FuseSource
>> Email: [email protected]
>> Web: http://fusesource.com
>> Twitter: davsclaus, fusenews
>> Blog: http://davsclaus.blogspot.com/
>> Author of Camel in Action: http://www.manning.com/ibsen/
>>



-- 
Claus Ibsen
-----------------
FuseSource
Email: [email protected]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Reply via email to