[Practicalxml-commits] SF.net SVN: practicalxml:[159] branches/dev-1.1/src
Brought to you by:
kdgregory
From: Auto-Generated S. C. M. <pra...@li...> - 2009-09-24 17:52:15
|
Revision: 159 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=159&view=rev Author: kdgregory Date: 2009-09-24 17:52:00 +0000 (Thu, 24 Sep 2009) Log Message: ----------- xml -> json: make quoted fieldnames the default Modified Paths: -------------- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Json2XmlConverter.java branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonConverter.java branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonOptions.java branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJson2XmlConverter.java branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJsonConverter.java branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestXml2JsonConverter.java Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Json2XmlConverter.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Json2XmlConverter.java 2009-09-24 17:05:17 UTC (rev 158) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Json2XmlConverter.java 2009-09-24 17:52:00 UTC (rev 159) @@ -142,11 +142,12 @@ String next = nextToken(); if (atEndOfSequence(next, "}", false)) return; - if (next.equals("\"")) - next = parseString(); while (true) { + if (next.equals("\"")) + next = parseString(); + Element child = appendChild(parent, next); expect(":"); Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonConverter.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonConverter.java 2009-09-24 17:05:17 UTC (rev 158) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonConverter.java 2009-09-24 17:52:00 UTC (rev 159) @@ -167,12 +167,16 @@ private StringBuilder appendFieldName(StringBuilder buf, String name) { - if (_options.contains(Xml2JsonOptions.QUOTE_FIELD_NAMES)) + if (_options.contains(Xml2JsonOptions.UNQUOTED_FIELD_NAMES)) + { + buf.append(name); + } + else + { buf.append('"') .append(name) .append('"'); - else - buf.append(name); + } buf.append(": "); return buf; Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonOptions.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonOptions.java 2009-09-24 17:05:17 UTC (rev 158) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonOptions.java 2009-09-24 17:52:00 UTC (rev 159) @@ -21,12 +21,15 @@ public enum Xml2JsonOptions { /** - * If enabled, field names will be quoted. This is required by the - * <a href="http://www.json.org/">JSON spec</a>, so should be used - * if you expect to use the result anywhere but in a browser. It is - * not required by <code>eval()</code>, nor for explicit scripting. + * If enabled, field names will not be quoted. This violates the + * <a href="http://www.json.org/">JSON spec</a>, which defines the + * production "pair" as "string : value" (and "string" is quoted). + * However, literal Java objects do not quote the field names, so + * if you use this converter to create explicit scripts, you won't + * want to follow the spec (and, not surprisingly, <code>eval()</code> + * doesn't require quoted names either). */ - QUOTE_FIELD_NAMES, + UNQUOTED_FIELD_NAMES, /** * If enabled, the entire string is wrapped by parentheses. This is Modified: branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJson2XmlConverter.java =================================================================== --- branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJson2XmlConverter.java 2009-09-24 17:05:17 UTC (rev 158) +++ branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJson2XmlConverter.java 2009-09-24 17:52:00 UTC (rev 159) @@ -255,6 +255,27 @@ } + // regression test! + public void testConvertTwoElementWithQuotedFieldNames() throws Exception + { + String src = "{\"foo\": 123, \"bar\": 456}"; + + Element root = new Json2XmlConverter(src).convert(); + assertEquals("data", root.getNodeName()); + assertEquals(2, root.getChildNodes().getLength()); + + Element child1 = (Element)root.getFirstChild(); + assertEquals("foo", child1.getNodeName()); + assertEquals("123", DomUtil.getText(child1)); + assertEquals(0, DomUtil.getChildren(child1).size()); + + Element child2 = (Element)child1.getNextSibling(); + assertEquals("bar", child2.getNodeName()); + assertEquals("456", DomUtil.getText(child2)); + assertEquals(0, DomUtil.getChildren(child2).size()); + } + + public void testFailObjectMissingCommaBetweenTerms() throws Exception { String src = "{foo: 123 bar: 456}"; Modified: branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJsonConverter.java =================================================================== --- branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJsonConverter.java 2009-09-24 17:05:17 UTC (rev 158) +++ branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJsonConverter.java 2009-09-24 17:52:00 UTC (rev 159) @@ -111,6 +111,20 @@ } + public void testUnquotedFieldnames() throws Exception + { + Element src = element("data", + element("foo", text("bar")), + element("argle", text("bargle"))) + .toDOM().getDocumentElement(); + String json = JsonConverter.convertToJson(src, Xml2JsonOptions.UNQUOTED_FIELD_NAMES); + Element dst = JsonConverter.convertToXml(json, "test") + .getDocumentElement(); + + assertChildren(src, dst); + } + + public void testRepeatedElements() throws Exception { Element src = element("data", Modified: branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestXml2JsonConverter.java =================================================================== --- branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestXml2JsonConverter.java 2009-09-24 17:05:17 UTC (rev 158) +++ branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestXml2JsonConverter.java 2009-09-24 17:52:00 UTC (rev 159) @@ -59,7 +59,7 @@ public void testSingleChild() throws Exception { convertAndAssert( - "{foo: \"bar\"}", + "{\"foo\": \"bar\"}", element("data", element("foo", text("bar")))); } @@ -68,7 +68,7 @@ public void testTwoChildren() throws Exception { convertAndAssert( - "{foo: \"bar\", argle: \"bargle\"}", + "{\"foo\": \"bar\", \"argle\": \"bargle\"}", element("data", element("foo", text("bar")), element("argle", text("bargle")))); @@ -78,7 +78,7 @@ public void testChildAndGrandchildren() throws Exception { convertAndAssert( - "{foo: \"bar\", argle: {biz: \"baz\", fizz: \"buzz\"}}", + "{\"foo\": \"bar\", \"argle\": {\"biz\": \"baz\", \"fizz\": \"buzz\"}}", element("data", element("foo", text("bar")), element("argle", @@ -91,7 +91,7 @@ { // note that "argle" elements are not adjacent, must become adjacent convertAndAssert( - "{foo: \"bar\", argle: [\"bargle\", \"wargle\"], baz: \"bar\"}", + "{\"foo\": \"bar\", \"argle\": [\"bargle\", \"wargle\"], \"baz\": \"bar\"}", element("data", element("foo", text("bar")), element("argle", text("bargle")), @@ -103,7 +103,7 @@ public void testArrayWithNestedObject() throws Exception { convertAndAssert( - "{foo: \"bar\", argle: [\"bargle\", {foo: \"bar\", bar: \"baz\"}]}", + "{\"foo\": \"bar\", \"argle\": [\"bargle\", {\"foo\": \"bar\", \"bar\": \"baz\"}]}", element("data", element("foo", text("bar")), element("argle", text("bargle")), @@ -117,7 +117,7 @@ public void testMixedContentWithWhitespace() throws Exception { convertAndAssert( - "{foo: \"bar\"}", + "{\"foo\": \"bar\"}", element("data", text(" "), element("foo", text("bar")), @@ -128,26 +128,26 @@ public void testWhitespace() throws Exception { convertAndAssert( - "{foo: \" \"}", + "{\"foo\": \" \"}", element("data", element("foo", text(" ")))); } - public void testQuotedFieldnames() throws Exception + public void testUnquotedFieldnames() throws Exception { convertAndAssert( - "{\"foo\": \"bar\"}", + "{foo: \"bar\"}", element("data", element("foo", text("bar"))), - Xml2JsonOptions.QUOTE_FIELD_NAMES); + Xml2JsonOptions.UNQUOTED_FIELD_NAMES); } public void testWrapWithParens() throws Exception { convertAndAssert( - "({foo: \"bar\"})", + "({\"foo\": \"bar\"})", element("data", element("foo", text("bar"))), Xml2JsonOptions.WRAP_WITH_PARENS); @@ -156,6 +156,8 @@ public void testStringEscaping() throws Exception { + // I'm using unquoted field names here because there are already + // far too many escape sequences for the test to be readable convertAndAssert( "{backslash: \"\\\\\", " + "quote: \"\\\"\", " @@ -165,6 +167,7 @@ element("backslash", text("\\")), element("quote", text("\"")), element("nonprint", text("\b\f\n\r\t")), - element("unicode", text("b\u00e4r")))); + element("unicode", text("b\u00e4r"))), + Xml2JsonOptions.UNQUOTED_FIELD_NAMES); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |