summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/unmaintained/modules/json
diff options
context:
space:
mode:
Diffstat (limited to 'sca-cpp/trunk/unmaintained/modules/json')
-rw-r--r--sca-cpp/trunk/unmaintained/modules/json/Makefile.am29
-rw-r--r--sca-cpp/trunk/unmaintained/modules/json/json-test.cpp337
-rw-r--r--sca-cpp/trunk/unmaintained/modules/json/json.hpp197
3 files changed, 563 insertions, 0 deletions
diff --git a/sca-cpp/trunk/unmaintained/modules/json/Makefile.am b/sca-cpp/trunk/unmaintained/modules/json/Makefile.am
new file mode 100644
index 0000000000..70352f047b
--- /dev/null
+++ b/sca-cpp/trunk/unmaintained/modules/json/Makefile.am
@@ -0,0 +1,29 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+if WANT_JS
+
+incl_HEADERS = *.hpp
+incldir = $(prefix)/include/modules/json
+
+json_test_SOURCES = json-test.cpp
+json_test_LDFLAGS = -lmozjs
+
+noinst_PROGRAMS = json-test
+TESTS = json-test
+
+endif
diff --git a/sca-cpp/trunk/unmaintained/modules/json/json-test.cpp b/sca-cpp/trunk/unmaintained/modules/json/json-test.cpp
new file mode 100644
index 0000000000..23cd32682c
--- /dev/null
+++ b/sca-cpp/trunk/unmaintained/modules/json/json-test.cpp
@@ -0,0 +1,337 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/* $Rev$ $Date$ */
+
+/**
+ * Test JSON data conversion functions.
+ */
+
+#include <assert.h>
+#include "stream.hpp"
+#include "string.hpp"
+#include "json.hpp"
+#include "parallel.hpp"
+#include "perf.hpp"
+
+namespace tuscany {
+namespace json {
+
+ostream* jsonWriter(const string& s, ostream* os) {
+ (*os) << s;
+ return os;
+}
+
+const string jscustomer("{\n"
+ " \"customer\": {\n"
+ " \"@name\": \"jdoe\",\n"
+ " \"address\": {\n"
+ " \"@city\": \"san francisco\",\n"
+ " \"@state\": \"ca\"\n"
+ " },\n"
+ " \"account\": {\n"
+ " \"id\": \"1234\",\n"
+ " \"@balance\": 1000\n"
+ " }\n"
+ " }\n"
+ "}");
+
+const string jsphones("{\n"
+ " \"phones\": [\n"
+ " \"408-1234\",\n"
+ " \"650-1234\"\n"
+ " ],\n"
+ " \"lastName\": \"test\\u0009tab\",\n"
+ " \"@firstName\": \"test1\"\n"
+ "}");
+
+const string jsecho("{\n"
+ " \"ns1:echoString\": {\n"
+ " \"@xmlns:ns1\": \"http://ws.apache.org/axis2/services/echo\",\n"
+ " \"text\": \"Hello World!\"\n"
+ " }\n"
+ "}");
+
+const string jsarray("{\n"
+ " \"fruit\": [\n"
+ " \"Apple\",\n"
+ " \"Orange\"\n"
+ " ]\n"
+ "}");
+
+bool testJSON() {
+ gc_scoped_pool pool;
+ const js::JSContext cx;
+
+ {
+ const list<value> ad = mklist<value>(mklist<value>(attribute, "city", string("san francisco")), mklist<value>(attribute, "state", string("ca")));
+ const list<value> ac = mklist<value>(mklist<value>(element, "id", string("1234")), mklist<value>(attribute, "balance", 1000));
+ const list<value> cr = mklist<value>(mklist<value> (attribute, "name", string("jdoe")), cons<value>(element, cons<value>("address", ad)), cons<value>(element, cons<value>("account", ac)));
+ const list<value> c = mklist<value>(cons<value>(element, cons<value>("customer", cr)));
+
+ ostringstream os;
+ writeJSON<ostream*>(jsonWriter, &os, c, cx);
+ assert(str(os) == jscustomer);
+ }
+ {
+ const list<value> phones = mklist<value> (string("408-1234"), string("650-1234"));
+ const list<value> l = mklist<value> (mklist<value> (element, "phones", phones), mklist<value> (element, "lastName", string("test\ttab")), mklist<value> (attribute, "firstName", string("test1")));
+
+ ostringstream os;
+ writeJSON<ostream*>(jsonWriter, &os, l, cx);
+ assert(str(os) == jsphones);
+
+ istringstream is(str(os));
+ const list<string> il = streamList(is);
+ const list<value> r = content(readJSON(il, cx));
+ assert(r == l);
+
+ ostringstream wos;
+ write(content(writeJSON(r, cx)), wos);
+ assert(str(wos) == str(os));
+ }
+ {
+ const list<value> l = mklist<value>(list<value>() + "ns1:echoString" + (list<value>() + "@xmlns:ns1" + string("http://ws.apache.org/axis2/services/echo")) + (list<value>() + "text" + string("Hello World!")));
+ ostringstream wos;
+ write(content(writeJSON(valuesToElements(l), cx)), wos);
+ assert(str(wos) == jsecho);
+
+ istringstream is(str(wos));
+ const list<string> il = streamList(is);
+ 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)));
+ const list<value> l2 = mklist<value>(list<value>() + "fruit" + (list<value>() + string("Apple") + string("Orange")));
+ assert(r == l2);
+ }
+ return true;
+}
+
+const string jsitem("{\n"
+ " \"id\": 3,\n"
+ " \"result\": [\n"
+ " {\n"
+ " \"price\": \"$2.99\",\n"
+ " \"name\": \"Apple\"\n"
+ " },\n"
+ " {\n"
+ " \"price\": \"$3.55\",\n"
+ " \"name\": \"Orange\"\n"
+ " },\n"
+ " {\n"
+ " \"price\": \"$1.55\",\n"
+ " \"name\": \"Pear\"\n"
+ " }\n"
+ " ]\n"
+ "}");
+
+const string jsresult("{\n"
+ " \"id\": 1,\n"
+ " \"result\": [\n"
+ " \"Service.get\",\n"
+ " \"Service.getTotal\"\n"
+ " ]\n"
+ "}");
+
+const string jsfeed("{\n"
+ " \"id\": 1,\n"
+ " \"result\": [\n"
+ " \"Sample Feed\",\n"
+ " \"123456789\",\n"
+ " [\n"
+ " \"Item\",\n"
+ " \"111\",\n"
+ " {\n"
+ " \"name\": \"Apple\",\n"
+ " \"currencyCode\": \"USD\",\n"
+ " \"currencySymbol\": \"$\",\n"
+ " \"price\": 2.99\n"
+ " }\n"
+ " ],\n"
+ " [\n"
+ " \"Item\",\n"
+ " \"222\",\n"
+ " {\n"
+ " \"name\": \"Orange\",\n"
+ " \"currencyCode\": \"USD\",\n"
+ " \"currencySymbol\": \"$\",\n"
+ " \"price\": 3.55\n"
+ " }\n"
+ " ],\n"
+ " [\n"
+ " \"Item\",\n"
+ " \"333\",\n"
+ " {\n"
+ " \"name\": \"Pear\",\n"
+ " \"currencyCode\": \"USD\",\n"
+ " \"currencySymbol\": \"$\",\n"
+ " \"price\": 1.55\n"
+ " }\n"
+ " ]\n"
+ " ]\n"
+ "}");
+
+const string jsechoreq("{\n"
+ " \"id\": 1,\n"
+ " \"method\": \"echo\",\n"
+ " \"params\": [\n"
+ " {\n"
+ " \"ns1:echoString\": {\n"
+ " \"@xmlns:ns1\": \"http://ws.apache.org/axis2/services/echo\",\n"
+ " \"text\": \"Hello World!\"\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ "}");
+
+const string jsechores("{\n"
+ " \"id\": 1,\n"
+ " \"result\": {\n"
+ " \"ns1:echoString\": {\n"
+ " \"@xmlns:ns1\": \"http://ws.apache.org/axis2/c/samples\",\n"
+ " \"text\": \"Hello World!\"\n"
+ " }\n"
+ " }\n"
+ "}");
+
+bool testJSONRPC() {
+ gc_scoped_pool pool;
+ js::JSContext cx;
+ {
+ const string lm("{\"id\": 1, \"method\": \"test\", \"params\": []}");
+ const list<value> e = content(readJSON(mklist(lm), cx));
+ const list<value> v = elementsToValues(e);
+ assert(assoc<value>("id", v) == mklist<value>("id", 1));
+ assert(assoc<value>("method", v) == mklist<value>("method", string("test")));
+ assert(assoc<value>("params", v) == mklist<value>("params", list<value>()));
+ }
+ {
+ const string i2 = "{\"id\": 3,\"result\": {\"0\": {\"price\": \"$2.99\",\"name\": \"Apple\"},\"1\": {\"price\": \"$3.55\",\"name\": \"Orange\"},\"2\": {\"price\": \"$1.55\",\"name\": \"Pear\"}}}";
+ const list<value> e = content(readJSON(mklist(jsitem), cx));
+ const list<value> e2 = content(readJSON(mklist(i2), cx));
+ assert(e == e2);
+ }
+ {
+ const list<value> e = content(readJSON(mklist(jsitem), cx));
+ ostringstream os;
+ write(content(writeJSON(e, cx)), os);
+ assert(str(os) == jsitem);
+ const list<value> v = elementsToValues(e);
+ const list<value> r = valuesToElements(v);
+ assert(r == e);
+ }
+ {
+ const list<value> r = mklist<value>(mklist<value>("id", 1), mklist<value>("result", mklist<value>(string("Service.get"), string("Service.getTotal"))));
+ const list<value> e = valuesToElements(r);
+ ostringstream os;
+ write(content(writeJSON(e, cx)), os);
+ assert(str(os) == jsresult);
+ }
+ {
+ const list<value> r = content(readJSON(mklist(jsfeed), cx));
+ const list<value> v = elementsToValues(r);
+ const list<value> e = valuesToElements(v);
+ ostringstream os;
+ write(content(writeJSON(e, cx)), os);
+ assert(str(os) == jsfeed);
+ }
+ {
+ const list<value> arg = mklist<value>(list<value>() + "ns1:echoString" + (list<value>() + "@xmlns:ns1" + string("http://ws.apache.org/axis2/services/echo")) + (list<value>() + "text" + string("Hello World!")));
+ const failable<list<string> > r = jsonRequest(1, "echo", mklist<value>(arg), cx);
+ ostringstream os;
+ write(content(r), os);
+ assert(str(os) == jsechoreq);
+
+ istringstream is(str(os));
+ const list<string> il = streamList(is);
+ const list<value> ir = elementsToValues(content(readJSON(il, cx)));
+ assert(car<value>(cadr<value>(caddr<value>(ir))) == arg);
+ }
+ {
+ const list<value> res = mklist<value>(list<value>() + "ns1:echoString" + (list<value>() + "@xmlns:ns1" + string("http://ws.apache.org/axis2/c/samples")) + (list<value>() + "text" + string("Hello World!")));
+ const failable<list<string> > r = jsonResult(1, res, cx);
+ ostringstream os;
+ write(content(r), os);
+ assert(str(os) == jsechores);
+
+ istringstream is(str(os));
+ const list<string> il = streamList(is);
+ const list<value> ir = elementsToValues(content(readJSON(il, cx)));
+ assert(cdr<value>(cadr<value>(ir)) == res);
+ }
+ return true;
+}
+
+struct testReadWrite {
+ testReadWrite() {
+ }
+ const bool operator()() const {
+ gc_scoped_pool pool;
+ js::JSContext cx;
+
+ const list<value> ad = mklist<value>(mklist<value>(attribute, "city", string("san francisco")), mklist<value>(attribute, "state", string("ca")));
+ const list<value> ac = mklist<value>(mklist<value>(element, "id", string("1234")), mklist<value>(attribute, "balance", 1000));
+ const list<value> cr = mklist<value>(mklist<value> (attribute, "name", string("jdoe")), cons<value>(element, cons<value>("address", ad)), cons<value>(element, cons<value>("account", ac)));
+ const list<value> c = mklist<value>(cons<value>(element, cons<value>("customer", cr)));
+
+ ostringstream os;
+ writeJSON<ostream*>(jsonWriter, &os, c, cx);
+ assert(str(os) == jscustomer);
+
+ istringstream is(jscustomer);
+ const list<string> il = streamList(is);
+ const list<value> r = content(readJSON(il, cx));
+ assert(r == c);
+ return true;
+ }
+};
+
+bool testJSONPerf() {
+ gc_scoped_pool pool;
+
+ const lambda<bool()> rwl = lambda<bool()>(testReadWrite());
+ cout << "JSON read + write test " << time(rwl, 5, 200) << " ms" << endl;
+
+ return true;
+}
+
+}
+}
+
+int main() {
+ tuscany::gc_scoped_pool p;
+ tuscany::cout << "Testing..." << tuscany::endl;
+
+ tuscany::json::testJSON();
+ tuscany::json::testJSONRPC();
+ tuscany::json::testJSONPerf();
+
+ tuscany::cout << "OK" << tuscany::endl;
+
+ return 0;
+}
diff --git a/sca-cpp/trunk/unmaintained/modules/json/json.hpp b/sca-cpp/trunk/unmaintained/modules/json/json.hpp
new file mode 100644
index 0000000000..85579c1b28
--- /dev/null
+++ b/sca-cpp/trunk/unmaintained/modules/json/json.hpp
@@ -0,0 +1,197 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/* $Rev$ $Date$ */
+
+#ifndef tuscany_json_hpp
+#define tuscany_json_hpp
+
+/**
+ * JSON data conversion functions.
+ */
+
+#include "string.hpp"
+#include "list.hpp"
+#include "value.hpp"
+#include "element.hpp"
+#include "monad.hpp"
+#include "../js/eval.hpp"
+
+namespace tuscany {
+namespace json {
+
+/**
+ * Return true if a list of strings contains a JSON document.
+ */
+const bool isJSON(const list<string>& ls) {
+ if (isNil(ls))
+ return false;
+ const string s = substr(car(ls), 0, 1);
+ return s == "[" || s == "{";
+}
+
+/**
+ * Consumes JSON strings and populates a JS object.
+ */
+failable<bool> consume(JSONParser* parser, const list<string>& ilist, const js::JSContext& cx) {
+ if (isNil(ilist))
+ return true;
+ JSString* jstr = JS_NewStringCopyZ(cx, c_str(car(ilist)));
+ if(!JS_ConsumeJSONText(cx, parser, JS_GetStringCharsZ(cx, jstr), (uint32)JS_GetStringLength(jstr)))
+ return mkfailure<bool>("JS_ConsumeJSONText failed");
+ return consume(parser, cdr(ilist), cx);
+}
+
+/**
+ * Convert a list of strings representing a JSON document to a list of values.
+ */
+const failable<list<value> > readJSON(const list<string>& ilist, const js::JSContext& cx) {
+ jsval val;
+ JSONParser* parser = JS_BeginJSONParse(cx, &val);
+ if(parser == NULL)
+ return mkfailure<list<value> >("JS_BeginJSONParse failed");
+
+ const failable<bool> consumed = consume(parser, ilist, cx);
+
+ if(!JS_FinishJSONParse(cx, parser, JSVAL_NULL))
+ return mkfailure<list<value> >("JS_FinishJSONParse failed");
+ if(!hasContent(consumed))
+ return mkfailure<list<value> >(consumed);
+
+ return list<value>(js::jsValToValue(val, cx));
+}
+
+/**
+ * Context passed to the JSON write callback function.
+ */
+template<typename R> class WriteContext {
+public:
+ WriteContext(const lambda<R(const string&, const R)>& reduce, const R& accum, const js::JSContext& cx) : cx(cx), reduce(reduce), accum(accum) {
+ }
+ const js::JSContext& cx;
+ const lambda<R(const string&, const R)> reduce;
+ R accum;
+};
+
+/**
+ * Called by JS_Stringify to write JSON out.
+ */
+template<typename R> JSBool writeCallback(const jschar *buf, uint32 len, void *data) {
+ WriteContext<R>& wcx = *(static_cast<WriteContext<R>*> (data));
+ JSString* jstr = JS_NewUCStringCopyN(wcx.cx, buf, len);
+ char* cstr = JS_EncodeString(wcx.cx, jstr);
+ const string str(cstr, JS_GetStringLength(jstr));
+ JS_free(wcx.cx, cstr);
+ wcx.accum = wcx.reduce(str, wcx.accum);
+ return JS_TRUE;
+}
+
+/**
+ * Convert a list of values to a JSON document.
+ */
+template<typename R> const failable<R> writeJSON(const lambda<R(const string&, const R)>& reduce, const R& initial, const list<value>& l, const js::JSContext& cx) {
+ jsval val;
+ if (js::isJSArray(l))
+ val = OBJECT_TO_JSVAL(valuesToJSElements(JS_NewArrayObject(cx, 0, NULL), l, 0, cx));
+ else
+ val = OBJECT_TO_JSVAL(valuesToJSProperties(JS_NewObject(cx, NULL, NULL, NULL), l, cx));
+
+ WriteContext<R> wcx(reduce, initial, cx);
+ if (!JS_Stringify(cx, &val, NULL, INT_TO_JSVAL(1), writeCallback<R>, &wcx))
+ return mkfailure<R>("JS_Stringify failed");
+ return wcx.accum;
+}
+
+/**
+ * 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>(), valuesToElements(elementsToValues(l)), cx);
+ if (!hasContent(ls))
+ return ls;
+ return reverse(list<string>(content(ls)));
+}
+
+/**
+ * Convert a list of function + params to a JSON-RPC request.
+ */
+const failable<list<string> > jsonRequest(const value& id, const value& func, const value& params, js::JSContext& cx) {
+ const list<value> r = mklist<value>(mklist<value>("id", id), mklist<value>("method", string(func)), mklist<value>("params", params));
+ return writeJSON(valuesToElements(r), cx);
+}
+
+/**
+ * Convert a value to a JSON-RPC result.
+ */
+const failable<list<string> > jsonResult(const value& id, const value& val, js::JSContext& cx) {
+ return writeJSON(valuesToElements(mklist<value>(mklist<value>("id", id), mklist<value>("result", val))), cx);
+}
+
+/**
+ * Convert a JSON-RPC result to a value.
+ */
+const failable<value> jsonResultValue(const list<string>& s, js::JSContext& cx) {
+ const failable<list<value> > jsres = json::readJSON(s, cx);
+ if (!hasContent(jsres))
+ return mkfailure<value>(jsres);
+ const list<value> rval(cadr<value>(elementsToValues(content(jsres))));
+ const value val = cadr(rval);
+ if (isList(val) && !js::isJSArray(val))
+ return value(mklist<value>(val));
+ return val;
+}
+
+/**
+ * Convert a JSON payload to a list of values.
+ */
+const list<value> jsonValues(const list<value>& e) {
+ return elementsToValues(e);
+}
+
+/**
+ * Return a portable function name from a JSON-RPC function name.
+ * Strip the ".", "system." and "Service." prefixes added by some JSON-RPC clients.
+ */
+const string funcName(const string& f) {
+ if (length(f) > 1 && find(f, ".", 0) == 0)
+ return c_str(f) + 1;
+ if (length(f) > 7 && find(f, "system.", 0) == 0)
+ return c_str(f) + 7;
+ if (length(f) > 8 && find(f, "Service.", 0) == 0)
+ return c_str(f) + 8;
+ return f;
+}
+
+/**
+ * Returns a list of param values other than the id and method args from a list
+ * of key value pairs.
+ */
+const list<value> queryParams(const list<list<value> >& a) {
+ if (isNil(a))
+ return list<value>();
+ const list<value> p = car(a);
+ if (car(p) == value("id") || car(p) == value("method"))
+ return queryParams(cdr(a));
+ return cons(cadr(p), queryParams(cdr(a)));
+}
+
+}
+}
+
+#endif /* tuscany_json_hpp */