You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Mark Webb <el...@gmail.com> on 2014/10/17 20:50:29 UTC
custom converter to string (json, xml, csv)
I have a custom object that I will be passing through Camel routes and
there may be times where I want to convert the object to different formats,
which means that I need to write custom converters. My question is, how do
I write a converter for the different String-based formats such as JSON,
XML and CSV and have Camel be smart enough to choose the correct one?
If I have the following methods
@Converter
public String myObjectToXml( MyObject event ){
// do stuff here
return "<xml>junk</xml>";
}
@Converter
public String myObjectToJson( MyObject event ){
// do stuff here
return "{stuff:junk}";
}
@Converter
public String myObjectToCsv( MyObject event ){
// do stuff here
return "junk,blah";
}
So I would marshal the object to a String-based format, but how does Camel
know which one I want?
Re: custom converter to string (json, xml, csv)
Posted by Willem Jiang <wi...@gmail.com>.
Please take a look at this DataFormat page[1]
you can use those marshal DSL to turn the object to the String with the formate you want.
[1]http://camel.apache.org/data-format.html
--
Willem Jiang
Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.iteye.com (Chinese)
Twitter: willemjiang
Weibo: 姜宁willem
On October 20, 2014 at 11:22:03 AM, Mark Webb (elihusmails@gmail.com) wrote:
> OK. So how does one develop Camel support for a custom object getting
> converted to a specified format?
>
> On Fri, Oct 17, 2014 at 6:40 PM, Christian Müller <
> christian.mueller@gmail.com> wrote:
>
> > You are looking for different data formats, not for different type
> > converters.
> >
> > Best,
> > Christian
> > Am 17.10.2014 20:50 schrieb "Mark Webb" :
> >
> > > I have a custom object that I will be passing through Camel routes and
> > > there may be times where I want to convert the object to different
> > formats,
> > > which means that I need to write custom converters. My question is, how
> > do
> > > I write a converter for the different String-based formats such as JSON,
> > > XML and CSV and have Camel be smart enough to choose the correct one?
> > >
> > > If I have the following methods
> > >
> > > @Converter
> > > public String myObjectToXml( MyObject event ){
> > > // do stuff here
> > > return "junk";
> > > }
> > >
> > > @Converter
> > > public String myObjectToJson( MyObject event ){
> > > // do stuff here
> > > return "{stuff:junk}";
> > > }
> > >
> > > @Converter
> > > public String myObjectToCsv( MyObject event ){
> > > // do stuff here
> > > return "junk,blah";
> > > }
> > >
> > > So I would marshal the object to a String-based format, but how does
> > Camel
> > > know which one I want?
> > >
> >
>
Re: custom converter to string (json, xml, csv)
Posted by Mark Webb <el...@gmail.com>.
OK. So how does one develop Camel support for a custom object getting
converted to a specified format?
On Fri, Oct 17, 2014 at 6:40 PM, Christian Müller <
christian.mueller@gmail.com> wrote:
> You are looking for different data formats, not for different type
> converters.
>
> Best,
> Christian
> Am 17.10.2014 20:50 schrieb "Mark Webb" <el...@gmail.com>:
>
> > I have a custom object that I will be passing through Camel routes and
> > there may be times where I want to convert the object to different
> formats,
> > which means that I need to write custom converters. My question is, how
> do
> > I write a converter for the different String-based formats such as JSON,
> > XML and CSV and have Camel be smart enough to choose the correct one?
> >
> > If I have the following methods
> >
> > @Converter
> > public String myObjectToXml( MyObject event ){
> > // do stuff here
> > return "<xml>junk</xml>";
> > }
> >
> > @Converter
> > public String myObjectToJson( MyObject event ){
> > // do stuff here
> > return "{stuff:junk}";
> > }
> >
> > @Converter
> > public String myObjectToCsv( MyObject event ){
> > // do stuff here
> > return "junk,blah";
> > }
> >
> > So I would marshal the object to a String-based format, but how does
> Camel
> > know which one I want?
> >
>
Re: custom converter to string (json, xml, csv)
Posted by Christian Müller <ch...@gmail.com>.
You are looking for different data formats, not for different type
converters.
Best,
Christian
Am 17.10.2014 20:50 schrieb "Mark Webb" <el...@gmail.com>:
> I have a custom object that I will be passing through Camel routes and
> there may be times where I want to convert the object to different formats,
> which means that I need to write custom converters. My question is, how do
> I write a converter for the different String-based formats such as JSON,
> XML and CSV and have Camel be smart enough to choose the correct one?
>
> If I have the following methods
>
> @Converter
> public String myObjectToXml( MyObject event ){
> // do stuff here
> return "<xml>junk</xml>";
> }
>
> @Converter
> public String myObjectToJson( MyObject event ){
> // do stuff here
> return "{stuff:junk}";
> }
>
> @Converter
> public String myObjectToCsv( MyObject event ){
> // do stuff here
> return "junk,blah";
> }
>
> So I would marshal the object to a String-based format, but how does Camel
> know which one I want?
>