Java 类org.apache.camel.dataformat.xmljson.XmlJsonDataFormat 实例源码

项目:Camel    文件:XmlJsonDataFormatAutoConfiguration.java   
@Bean
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(XmlJsonDataFormat.class)
public XmlJsonDataFormat configureXmlJsonDataFormat(
        CamelContext camelContext,
        XmlJsonDataFormatConfiguration configuration) throws Exception {
    XmlJsonDataFormat dataformat = new XmlJsonDataFormat();
    if (dataformat instanceof CamelContextAware) {
        ((CamelContextAware) dataformat).setCamelContext(camelContext);
    }
    Map<String, Object> parameters = new HashMap<>();
    IntrospectionSupport.getProperties(configuration, parameters, null,
            false);
    IntrospectionSupport.setProperties(camelContext,
            camelContext.getTypeConverter(), dataformat, parameters);
    return dataformat;
}
项目:camel-cookbook-examples    文件:XmlJsonRoute.java   
@Override
public void configure() throws Exception {
    from("direct:marshal")
        .marshal().xmljson()
        .to("mock:marshalResult");

    from("direct:unmarshal")
        .unmarshal().xmljson()
        .to("mock:unmarshalResult");

    XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
    xmlJsonFormat.setRootName("bookstore");
    xmlJsonFormat.setElementName("book");
    xmlJsonFormat.setExpandableProperties(Arrays.asList("author", "author"));

    from("direct:unmarshalBookstore")
        .unmarshal(xmlJsonFormat)
        .to("mock:unmarshalBookstoreResult");
}
项目:camel-cookbook-examples    文件:NormalizerRoute.java   
@Override
public void configure() throws Exception {
    final DataFormat bindy = new BindyCsvDataFormat(org.camelcookbook.transformation.csv.model.BookModel.class);
    final DataFormat jaxb = new JaxbDataFormat("org.camelcookbook.transformation.myschema");

    final XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
    xmlJsonFormat.setRootName("bookstore");
    xmlJsonFormat.setElementName("book");
    xmlJsonFormat.setExpandableProperties(Arrays.asList("author", "author"));

    from("direct:start")
        .choice()
        .when(header(Exchange.FILE_NAME).endsWith(".csv"))
            .unmarshal(bindy)
            .bean(MyNormalizer.class, "bookModelToJaxb")
            .to("mock:csv")
        .when(header(Exchange.FILE_NAME).endsWith(".json"))
            .unmarshal(xmlJsonFormat)
            .to("mock:json")
        .when(header(Exchange.FILE_NAME).endsWith(".xml"))
            .unmarshal(jaxb)
            .to("mock:xml")
        .otherwise()
            .to("mock:unknown")
            .stop()
        .end()
        .to("mock:normalized");
}
项目:wildfly-camel    文件:XmlJsonIntegrationTest.java   
@Test
public void testMarshalAndUnmarshal() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            XmlJsonDataFormat format = new XmlJsonDataFormat();
            from("direct:marshal").marshal(format).to("mock:json");
            from("direct:unmarshal").unmarshal(format).to("mock:xml");
        }
    });

    camelctx.start();
    try {
        MockEndpoint mockJSON = camelctx.getEndpoint("mock:json", MockEndpoint.class);
        mockJSON.expectedMessageCount(1);
        mockJSON.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/json");
        mockJSON.message(0).body().isInstanceOf(byte[].class);

        MockEndpoint mockXML = camelctx.getEndpoint("mock:xml", MockEndpoint.class);
        mockXML.expectedMessageCount(1);
        mockXML.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/xml");
        mockXML.message(0).body().isInstanceOf(String.class);

        InputStream inStream = getClass().getResourceAsStream("/xmljson/testMessage1.xml");
        String in = camelctx.getTypeConverter().convertTo(String.class, inStream);

        ProducerTemplate template = camelctx.createProducerTemplate();
        Object json = template.requestBody("direct:marshal", in);
        String jsonString = camelctx.getTypeConverter().convertTo(String.class, json);

        String expString = "{'a':'1','b':'2','c':{'a':'c.a.1','b':'c.b.2'},'d':['a','b','c'],'e':['1','2','3'],'f':'true','g':[]}";
        Assert.assertEquals(expString, jsonString.replace('"', '\''));

        template.sendBody("direct:unmarshal", jsonString);

        mockJSON.assertIsSatisfied();
        mockXML.assertIsSatisfied();
    } finally {
        camelctx.stop();
    }
}