You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Charles Hudak <me...@gmail.com> on 2011/12/09 03:29:55 UTC
Custom Jackson ObjectMapper
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
Re: Custom Jackson ObjectMapper
Posted by Claus Ibsen <cl...@gmail.com>.
Hi
I have logged a ticket
https://issues.apache.org/jira/browse/CAMEL-4992
On Wed, Feb 1, 2012 at 6:11 PM, Tim <ch...@gmail.com> wrote:
> I'm running into a similar situation when trying to enable Jackson
> SmileFactory.
> This requires you to create an ObjectMapper with a SmileFactory as the
> argument to it's constructor.
> The way to do this seems to me kind of convoluted.
> It would be more clean if there was a way to register a dataformat provider.
>
> On Wed, Feb 1, 2012 at 2:25 AM, Claus Ibsen <cl...@gmail.com> wrote:
>
>> Hi
>>
>> If Jackson has a number of good configurations options and whatnot,
>> then we should consider having a <jackson> data format, where you can
>> configure that.
>>
>> What other kind of options can you configure, that you would need?
>>
>>
>> In Camel 3.0 we are talking about allowing the data formats to be
>> configured using uri notations just like endpoints.
>> That makes them more flexible.
>>
>>
>> On Mon, Jan 30, 2012 at 7:14 PM, GPRSLyons <GP...@gmail.com> wrote:
>> > Is there a currently implemented way in Camel to apply a custom naming
>> > strategy to Jackson (such as LowerCaseWithUnderscoresStrategy
>> >
>> http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.html
>> )?
>> > Or will this only be possible when we get access to the ObjectMapper in
>> 3.0
>> > via
>> >
>> > ObjectMapper mapper = new ObjectMapper();
>> > mapper.setPropertyNamingStrategy(new LowerCaseWithUnderscoresStrategy());
>> >
>> > Thanks!
>> >
>> > --
>> > View this message in context:
>> http://camel.465427.n5.nabble.com/Custom-Jackson-ObjectMapper-tp5060579p5442178.html
>> > Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> FuseSource
>> Email: cibsen@fusesource.com
>> 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: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Re: Custom Jackson ObjectMapper
Posted by Tim <ch...@gmail.com>.
I'm running into a similar situation when trying to enable Jackson
SmileFactory.
This requires you to create an ObjectMapper with a SmileFactory as the
argument to it's constructor.
The way to do this seems to me kind of convoluted.
It would be more clean if there was a way to register a dataformat provider.
On Wed, Feb 1, 2012 at 2:25 AM, Claus Ibsen <cl...@gmail.com> wrote:
> Hi
>
> If Jackson has a number of good configurations options and whatnot,
> then we should consider having a <jackson> data format, where you can
> configure that.
>
> What other kind of options can you configure, that you would need?
>
>
> In Camel 3.0 we are talking about allowing the data formats to be
> configured using uri notations just like endpoints.
> That makes them more flexible.
>
>
> On Mon, Jan 30, 2012 at 7:14 PM, GPRSLyons <GP...@gmail.com> wrote:
> > Is there a currently implemented way in Camel to apply a custom naming
> > strategy to Jackson (such as LowerCaseWithUnderscoresStrategy
> >
> http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.html
> )?
> > Or will this only be possible when we get access to the ObjectMapper in
> 3.0
> > via
> >
> > ObjectMapper mapper = new ObjectMapper();
> > mapper.setPropertyNamingStrategy(new LowerCaseWithUnderscoresStrategy());
> >
> > Thanks!
> >
> > --
> > View this message in context:
> http://camel.465427.n5.nabble.com/Custom-Jackson-ObjectMapper-tp5060579p5442178.html
> > Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>
Re: Custom Jackson ObjectMapper
Posted by Claus Ibsen <cl...@gmail.com>.
Hi
If Jackson has a number of good configurations options and whatnot,
then we should consider having a <jackson> data format, where you can
configure that.
What other kind of options can you configure, that you would need?
In Camel 3.0 we are talking about allowing the data formats to be
configured using uri notations just like endpoints.
That makes them more flexible.
On Mon, Jan 30, 2012 at 7:14 PM, GPRSLyons <GP...@gmail.com> wrote:
> Is there a currently implemented way in Camel to apply a custom naming
> strategy to Jackson (such as LowerCaseWithUnderscoresStrategy
> http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.html)?
> Or will this only be possible when we get access to the ObjectMapper in 3.0
> via
>
> ObjectMapper mapper = new ObjectMapper();
> mapper.setPropertyNamingStrategy(new LowerCaseWithUnderscoresStrategy());
>
> Thanks!
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Custom-Jackson-ObjectMapper-tp5060579p5442178.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
--
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Re: Custom Jackson ObjectMapper
Posted by GPRSLyons <GP...@gmail.com>.
Is there a currently implemented way in Camel to apply a custom naming
strategy to Jackson (such as LowerCaseWithUnderscoresStrategy
http://jackson.codehaus.org/1.9.4/javadoc/org/codehaus/jackson/map/PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.html)?
Or will this only be possible when we get access to the ObjectMapper in 3.0
via
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new LowerCaseWithUnderscoresStrategy());
Thanks!
--
View this message in context: http://camel.465427.n5.nabble.com/Custom-Jackson-ObjectMapper-tp5060579p5442178.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Custom Jackson ObjectMapper
Posted by Claus Ibsen <cl...@gmail.com>.
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 <me...@gmail.com> 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 <cl...@gmail.com> 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 <me...@gmail.com>
>> 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: cibsen@fusesource.com
>> 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: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Re: Custom Jackson ObjectMapper
Posted by Charles Hudak <me...@gmail.com>.
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 <cl...@gmail.com> 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 <me...@gmail.com>
> 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: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>
Re: Custom Jackson ObjectMapper
Posted by Claus Ibsen <cl...@gmail.com>.
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 <me...@gmail.com> 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: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/