Repository: camel
Updated Branches:
  refs/heads/camel-2.13.x 655ac47ff -> 09d34bd3a


CAMEL-8217: Fix typeHints option for camel-xmljson

Fixed camel-core model to set the option on the data format. Changed the
behaviour of YES and WITH_PREFIX to reflect documentation and naming.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0e65a755
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0e65a755
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0e65a755

Branch: refs/heads/camel-2.13.x
Commit: 0e65a75512e0c6ed57912bf1310cc861805eaf5f
Parents: 655ac47
Author: Toni Fadjukoff <lamp...@lamperi.name>
Authored: Thu Jan 8 01:36:54 2015 +0200
Committer: Willem Jiang <willem.ji...@gmail.com>
Committed: Thu Jan 8 15:16:44 2015 +0800

----------------------------------------------------------------------
 .../model/dataformat/XmlJsonDataFormat.java     |  4 +-
 .../dataformat/xmljson/XmlJsonDataFormat.java   |  2 +
 .../dataformat/xmljson/XmlJsonOptionsTest.java  | 69 ++++++++++++++++++++
 .../xmljson/testMessage5-typeHints.json         |  1 +
 .../xmljson/testMessage5-typeHints.xml          |  6 ++
 .../xmljson/testMessage6-prefixedTypeHints.xml  |  5 ++
 6 files changed, 85 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0e65a755/camel-core/src/main/java/org/apache/camel/model/dataformat/XmlJsonDataFormat.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/model/dataformat/XmlJsonDataFormat.java
 
b/camel-core/src/main/java/org/apache/camel/model/dataformat/XmlJsonDataFormat.java
index 31eb6e7..490cd54 100644
--- 
a/camel-core/src/main/java/org/apache/camel/model/dataformat/XmlJsonDataFormat.java
+++ 
b/camel-core/src/main/java/org/apache/camel/model/dataformat/XmlJsonDataFormat.java
@@ -169,7 +169,7 @@ public class XmlJsonDataFormat extends DataFormatDefinition 
{
 
         // will end up calling the setTypeHints(String s) which does the 
parsing from the Enum String key to the Enum value
         if (typeHints != null) {
-            setProperty(camelContext, typeHints, TYPE_HINTS, typeHints);
+            setProperty(camelContext, dataFormat, TYPE_HINTS, typeHints);
         }
 
         //TODO: xmljson: element-namespace mapping is not implemented in the 
XML DSL
@@ -274,4 +274,4 @@ public class XmlJsonDataFormat extends DataFormatDefinition 
{
         this.typeHints = typeHints;
     }
 
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/0e65a755/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
----------------------------------------------------------------------
diff --git 
a/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
 
b/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
index 99a91d7..81d9420 100644
--- 
a/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
+++ 
b/components/camel-xmljson/src/main/java/org/apache/camel/dataformat/xmljson/XmlJsonDataFormat.java
@@ -117,6 +117,8 @@ public class XmlJsonDataFormat extends ServiceSupport 
implements DataFormat {
         if (typeHints == TypeHintsEnum.YES || typeHints == 
TypeHintsEnum.WITH_PREFIX) {
             serializer.setTypeHintsEnabled(true);
             if (typeHints == TypeHintsEnum.WITH_PREFIX) {
+                serializer.setTypeHintsCompatibility(false);
+            } else {
                 serializer.setTypeHintsCompatibility(true);
             }
         } else {

http://git-wip-us.apache.org/repos/asf/camel/blob/0e65a755/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonOptionsTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonOptionsTest.java
 
b/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonOptionsTest.java
index 8c5b20a..5183c5e 100644
--- 
a/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonOptionsTest.java
+++ 
b/components/camel-xmljson/src/test/java/org/apache/camel/dataformat/xmljson/XmlJsonOptionsTest.java
@@ -139,6 +139,61 @@ public class XmlJsonOptionsTest extends CamelTestSupport {
         assertEquals("Root element must have namespace attributes", 2, 
document.getDocumentElement().getAttributes().getLength());
         mockXML.assertIsSatisfied();
     }
+       
+       @Test
+       public void testTypeHintsToJSON() throws Exception {
+        InputStream inStream = 
getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage5-typeHints.xml");
+        String in = context.getTypeConverter().convertTo(String.class, 
inStream);
+
+        MockEndpoint mockJSON = getMockEndpoint("mock:jsonTypeHints");
+        mockJSON.expectedMessageCount(1);
+        mockJSON.message(0).body().isInstanceOf(byte[].class);
+
+        Object json = template.requestBody("direct:marshalTypeHints", in);
+        String jsonString = context.getTypeConverter().convertTo(String.class, 
json);
+        JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
+        assertEquals("root.a must be number", Integer.valueOf(1), 
obj.getJSONObject("root").get("a"));
+        assertEquals("root.b must be boolean", Boolean.TRUE, 
obj.getJSONObject("root").get("b"));
+
+        mockJSON.assertIsSatisfied();
+       }
+       
+       @Test
+       public void testTypeHintsToXML() throws Exception {
+        InputStream inStream = 
getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage5-typeHints.json");
+        String in = context.getTypeConverter().convertTo(String.class, 
inStream);
+
+        MockEndpoint mockXML = getMockEndpoint("mock:xmlTypeHints");
+        mockXML.expectedMessageCount(1);
+        mockXML.message(0).body().isInstanceOf(String.class);
+
+        Object marshalled = template.requestBody("direct:unmarshalTypeHints", 
in);
+        Document document = 
context.getTypeConverter().convertTo(Document.class, marshalled);
+        assertEquals("Element a should exists", 1, 
document.getDocumentElement().getElementsByTagName("a").getLength());
+        assertNotNull("Element a should have attribute type", 
document.getDocumentElement().getElementsByTagName("a").item(0).getAttributes().getNamedItem("type"));
+        assertEquals("Element a should have attribute type with value number", 
"number", 
document.getDocumentElement().getElementsByTagName("a").item(0).getAttributes().getNamedItem("type").getTextContent());
+        assertEquals("Element b should have attribute type with value 
boolean", "boolean", 
document.getDocumentElement().getElementsByTagName("b").item(0).getAttributes().getNamedItem("type").getTextContent());
+        mockXML.assertIsSatisfied();
+       }
+       
+       @Test
+       public void testPrefixedTypeHintsToJSON() throws Exception {
+        InputStream inStream = 
getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage6-prefixedTypeHints.xml");
+        String in = context.getTypeConverter().convertTo(String.class, 
inStream);
+
+        MockEndpoint mockJSON = getMockEndpoint("mock:jsonPrefixedTypeHints");
+        mockJSON.expectedMessageCount(1);
+        mockJSON.message(0).body().isInstanceOf(byte[].class);
+
+        Object json = template.requestBody("direct:marshalPrefixedTypeHints", 
in);
+        String jsonString = context.getTypeConverter().convertTo(String.class, 
json);
+        JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
+        assertEquals("root.a must be number", Integer.valueOf(1), 
obj.getJSONObject("root").get("a"));
+        assertEquals("root.b must be boolean", Boolean.TRUE, 
obj.getJSONObject("root").get("b"));
+
+        mockJSON.assertIsSatisfied();
+       }
+
 
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
@@ -170,6 +225,20 @@ public class XmlJsonOptionsTest extends CamelTestSupport {
                 // from JSON to XML
                 
from("direct:unmarshalNS").unmarshal(namespacesFormat).to("mock:xmlNS");
 
+                               XmlJsonDataFormat typeHintsFormat = new 
XmlJsonDataFormat();
+                typeHintsFormat.setForceTopLevelObject(true);
+                               typeHintsFormat.setTypeHints("YES");
+                               // from XML to JSON
+                
from("direct:marshalTypeHints").marshal(typeHintsFormat).to("mock:jsonTypeHints");
+                // from JSON to XML
+                
from("direct:unmarshalTypeHints").unmarshal(typeHintsFormat).to("mock:xmlTypeHints");
+                               
+                               XmlJsonDataFormat prefixedTypeHintsFormat = new 
XmlJsonDataFormat();
+                prefixedTypeHintsFormat.setForceTopLevelObject(true);
+                               
prefixedTypeHintsFormat.setTypeHints("WITH_PREFIX");
+                               // from XML to JSON
+                
from("direct:marshalPrefixedTypeHints").marshal(prefixedTypeHintsFormat).to("mock:jsonPrefixedTypeHints");
+
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/0e65a755/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.json
----------------------------------------------------------------------
diff --git 
a/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.json
 
b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.json
new file mode 100644
index 0000000..528b8c3
--- /dev/null
+++ 
b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.json
@@ -0,0 +1 @@
+{ "a": 1, "b": true, "c": "foo" }

http://git-wip-us.apache.org/repos/asf/camel/blob/0e65a755/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.xml
----------------------------------------------------------------------
diff --git 
a/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.xml
 
b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.xml
new file mode 100644
index 0000000..3052ba8
--- /dev/null
+++ 
b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage5-typeHints.xml
@@ -0,0 +1,6 @@
+<!-- { "a": 1, "b": true, "c": "foo" } -->
+<root>
+       <a type="number">1</a>
+       <b type="boolean">true</b>
+       <c>foo</c>
+</root>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/0e65a755/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage6-prefixedTypeHints.xml
----------------------------------------------------------------------
diff --git 
a/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage6-prefixedTypeHints.xml
 
b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage6-prefixedTypeHints.xml
new file mode 100644
index 0000000..66ff295
--- /dev/null
+++ 
b/components/camel-xmljson/src/test/resources/org/apache/camel/dataformat/xmljson/testMessage6-prefixedTypeHints.xml
@@ -0,0 +1,5 @@
+<!-- { "a": 1, "b": true, "c": "foo" } -->
+<root>
+       <a json_type="number">1</a>
+       <b json_type="boolean">true</b>
+</root>
\ No newline at end of file

Reply via email to