summaryrefslogtreecommitdiffstats
path: root/sca-cpp
diff options
context:
space:
mode:
authorjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2012-02-27 03:26:51 +0000
committerjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2012-02-27 03:26:51 +0000
commita07d4945c1a301aeaf4b67411065b9adcfac1065 (patch)
treec605de13425e7ecdcd8ee4c941a8ce97a881731a /sca-cpp
parentedc083d8056557814691d49d45b56f8244558bae (diff)
Fix mapping between repeating XML elements, S-expression lists, and JSON array cells.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1294006 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp')
-rw-r--r--sca-cpp/trunk/kernel/element.hpp4
-rw-r--r--sca-cpp/trunk/kernel/xml-test.cpp47
-rw-r--r--sca-cpp/trunk/kernel/xml.hpp2
-rw-r--r--sca-cpp/trunk/modules/json/json-test.cpp18
-rw-r--r--sca-cpp/trunk/modules/json/json.hpp2
5 files changed, 69 insertions, 4 deletions
diff --git a/sca-cpp/trunk/kernel/element.hpp b/sca-cpp/trunk/kernel/element.hpp
index c69d3bbf07..27b5af8691 100644
--- a/sca-cpp/trunk/kernel/element.hpp
+++ b/sca-cpp/trunk/kernel/element.hpp
@@ -172,10 +172,10 @@ const list<value> elementToValueGroupValues(const value& v, const list<value>& l
if (car<value>(car(l)) != car<value>(v))
return cons(v, l);
if (!elementToValueIsList(cadr<value>(car(l)))) {
- const value g = mklist<value>(car<value>(v), mklist<value>(cdr<value>(v), cdr<value>(car(l))));
+ const value g = mklist<value>(car<value>(v), mklist<value>(isList(cadr<value>(v))? (value)cdr<value>(v) : cadr<value>(v), isList(cadr<value>(car(l)))? (value)cdr<value>(car(l)) : cadr<value>(car(l))));
return elementToValueGroupValues(g, cdr(l));
}
- const value g = mklist<value>(car<value>(v), cons<value>(cdr<value>(v), (list<value>)cadr<value>(car(l))));
+ const value g = mklist<value>(car<value>(v), cons<value>(isList(cadr<value>(v))? (value)cdr<value>(v) : cadr<value>(v), (list<value>)cadr<value>(car(l))));
return elementToValueGroupValues(g, cdr(l));
}
diff --git a/sca-cpp/trunk/kernel/xml-test.cpp b/sca-cpp/trunk/kernel/xml-test.cpp
index 7149d1bc41..c505cae9cf 100644
--- a/sca-cpp/trunk/kernel/xml-test.cpp
+++ b/sca-cpp/trunk/kernel/xml-test.cpp
@@ -73,6 +73,35 @@ const string customerXML =
" </account>\n"
"</customer>\n";
+const string abcXML =
+"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+"<a>\n"
+" <m>abc</m>\n"
+" <m>def</m>\n"
+" <m>xyz</m>\n"
+" <m>tuv</m>\n"
+"</a>\n";
+
+const string xyzXML =
+"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+"<a>\n"
+" <m>\n"
+" <id>123</id>\n"
+" <name>abc</name>\n"
+" </m>\n"
+" <m>\n"
+" <id>234</id>\n"
+" <name>def</name>\n"
+" </m>\n"
+" <m>\n"
+" <id>345</id>\n"
+" <name>xyz</name>\n"
+" </m>\n"
+" <m>\n"
+" <id>456</id>\n"
+" <name>tuv</name>\n"
+" </m>\n"
+"</a>\n";
const bool isName(const value& token) {
return isTaggedList(token, attribute) && attributeName(token) == "name";
@@ -145,6 +174,24 @@ bool testElements() {
}
}
{
+ istringstream is(abcXML);
+ const list<value> c = readXML(streamList(is));
+ const list<value> v = elementsToValues(c);
+ const list<value> e = valuesToElements(v);
+ ostringstream os;
+ writeXML<ostream*>(xmlWriter, &os, e);
+ assert(str(os) == abcXML);
+ }
+ {
+ istringstream is(xyzXML);
+ const list<value> c = readXML(streamList(is));
+ const list<value> v = elementsToValues(c);
+ const list<value> e = valuesToElements(v);
+ ostringstream os;
+ writeXML<ostream*>(xmlWriter, &os, e);
+ assert(str(os) == xyzXML);
+ }
+ {
istringstream is(customerXML);
const list<value> c = readXML(streamList(is));
const list<value> v = elementsToValues(c);
diff --git a/sca-cpp/trunk/kernel/xml.hpp b/sca-cpp/trunk/kernel/xml.hpp
index fb7c0a6cf6..1f1c664c2d 100644
--- a/sca-cpp/trunk/kernel/xml.hpp
+++ b/sca-cpp/trunk/kernel/xml.hpp
@@ -253,7 +253,7 @@ const char* encoding = "UTF-8";
const list<value> expandElementValues(const value& n, const list<value>& l) {
if (isNil(l))
return l;
- return cons<value>(value(cons<value>(element, cons<value>(n, (list<value>)car(l)))), expandElementValues(n, cdr(l)));
+ return cons<value>(value(cons<value>(element, cons<value>(n, isList(car(l))? (list<value>)car(l) : mklist(car(l))))), expandElementValues(n, cdr(l)));
}
const failable<bool> writeList(const list<value>& l, const xmlTextWriterPtr xml) {
diff --git a/sca-cpp/trunk/modules/json/json-test.cpp b/sca-cpp/trunk/modules/json/json-test.cpp
index 157e012b5a..ac68d7573d 100644
--- a/sca-cpp/trunk/modules/json/json-test.cpp
+++ b/sca-cpp/trunk/modules/json/json-test.cpp
@@ -66,6 +66,13 @@ const string jsecho("{\n"
" }\n"
"}");
+const string jsarray("{\n"
+ " \"fruit\": [\n"
+ " \"Apple\",\n"
+ " \"Orange\"\n"
+ " ]\n"
+ "}");
+
bool testJSON() {
const js::JSContext cx;
@@ -107,6 +114,17 @@ bool testJSON() {
const list<value> r = elementsToValues(content(readJSON(il, cx)));
assert(r == l);
}
+ {
+ const list<value> l = mklist<value>((list<value>() + "fruit" + string("Apple")), (list<value>() + "fruit" + string("Orange")));
+ ostringstream wos;
+ write(content(writeJSON(valuesToElements(l), cx)), wos);
+ assert(str(wos) == jsarray);
+
+ istringstream is(str(wos));
+ const list<string> il = streamList(is);
+ const list<value> r = elementsToValues(content(readJSON(il, cx)));
+ assert(r == l);
+ }
return true;
}
diff --git a/sca-cpp/trunk/modules/json/json.hpp b/sca-cpp/trunk/modules/json/json.hpp
index e222ef0486..39c56b6863 100644
--- a/sca-cpp/trunk/modules/json/json.hpp
+++ b/sca-cpp/trunk/modules/json/json.hpp
@@ -122,7 +122,7 @@ template<typename R> const failable<R> writeJSON(const lambda<R(const string&, c
* Convert a list of values to a list of strings representing a JSON document.
*/
const failable<list<string> > writeJSON(const list<value>& l, const js::JSContext& cx) {
- const failable<list<string> > ls = writeJSON<list<string>>(rcons<string>, list<string>(), l, cx);
+ const failable<list<string> > ls = writeJSON<list<string>>(rcons<string>, list<string>(), valuesToElements(elementsToValues(l)), cx);
if (!hasContent(ls))
return ls;
return reverse(list<string>(content(ls)));