summaryrefslogtreecommitdiffstats
path: root/cpp/sca/modules/json
diff options
context:
space:
mode:
authorjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2009-10-11 00:01:09 +0000
committerjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2009-10-11 00:01:09 +0000
commit4a4c32fea2f0cc36b01ee1382964b10936e1d7d0 (patch)
treef9059ef059f45a28c7379a7f101035f7b20557a1 /cpp/sca/modules/json
parenta64c1ec2a50f62d63872eac6bc740966602e87bd (diff)
Added support for JSON-RPC to httpd module. Fixed issues with double numbers in json.hpp. Added store .html and .js files to store test case.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@823982 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r--cpp/sca/modules/json/json-test.cpp24
-rw-r--r--cpp/sca/modules/json/json.hpp52
2 files changed, 43 insertions, 33 deletions
diff --git a/cpp/sca/modules/json/json-test.cpp b/cpp/sca/modules/json/json-test.cpp
index 5a1dcc5d51..1ebece647b 100644
--- a/cpp/sca/modules/json/json-test.cpp
+++ b/cpp/sca/modules/json/json-test.cpp
@@ -34,7 +34,7 @@ namespace tuscany {
bool testJSEval() {
JSONContext cx;
- std::string script("(function testJSON(n){ return JSON.parse(JSON.stringify(n)) })(5)");
+ const std::string script("(function testJSON(n){ return JSON.parse(JSON.stringify(n)) })(5)");
jsval rval;
assert(JS_EvaluateScript(cx, cx.getGlobal(), script.c_str(), script.length(), "testJSON.js", 1, &rval));
const std::string r(JS_GetStringBytes(JS_ValueToString(cx, rval)));
@@ -48,21 +48,20 @@ std::ostringstream* jsonWriter(std::ostringstream* os, const std::string& s) {
}
bool testJSON() {
- JSONContext cx;
+ const JSONContext cx;
- list<value> phones = makeList<value> (std::string("408-1234"), std::string("650-1234"));
- list<value> l = makeList<value> (makeList<value> ("phones", phones), makeList<value> ("lastName", std::string("test\ttab")), makeList<value> ("firstName", std::string("test1")));
+ const list<value> phones = makeList<value> (std::string("408-1234"), std::string("650-1234"));
+ const list<value> l = makeList<value> (makeList<value> ("phones", phones), makeList<value> ("lastName", std::string("test\ttab")), makeList<value> ("firstName", std::string("test1")));
print(l, std::cout);
std::cout << std::endl;
std::ostringstream os;
- lambda<std::ostringstream*(std::ostringstream*, std::string)> writer(jsonWriter);
- writeJSON(cx, writer, &os, l);
+ writeJSON<std::ostringstream*>(cx, jsonWriter, &os, l);
std::cout << os.str() << std::endl;
std::istringstream is(os.str());
- list<std::string> il = makeStreamList(is);
- list<value> r = readJSON(cx, il);
+ const list<std::string> il = makeStreamList(is);
+ const list<value> r = readJSON(cx, il);
print(r, std::cout);
std::cout << std::endl;
assert(r == l);
@@ -74,6 +73,14 @@ bool testJSON() {
return true;
}
+bool testJSONRPC() {
+ const std::string lm("{\"id\": 1, \"method\": \"system.listMethods\", \"params\": []}");
+ JSONContext cx;
+ const list<value> v = readJSON(cx, makeList(lm));
+ std::cout << v << std::endl;
+ return true;
+}
+
}
int main() {
@@ -81,6 +88,7 @@ int main() {
tuscany::testJSEval();
tuscany::testJSON();
+ tuscany::testJSONRPC();
std::cout << "OK" << std::endl;
diff --git a/cpp/sca/modules/json/json.hpp b/cpp/sca/modules/json/json.hpp
index e9938672c5..f6ed9202c3 100644
--- a/cpp/sca/modules/json/json.hpp
+++ b/cpp/sca/modules/json/json.hpp
@@ -32,6 +32,7 @@
#include <string>
#include "list.hpp"
#include "value.hpp"
+#include "monad.hpp"
namespace tuscany {
@@ -172,8 +173,9 @@ const value jsValToValue(const JSONContext& cx, const jsval& jsv) {
return value((bool)JSVAL_TO_BOOLEAN(jsv));
}
case JSTYPE_NUMBER: {
- jsdouble* jsd = JSVAL_TO_DOUBLE(jsv);
- return value((double)*jsd);
+ jsdouble jsd;
+ JS_ValueToNumber(cx, jsv, &jsd);
+ return value((double)jsd);
}
case JSTYPE_OBJECT: {
JSObject* o = JSVAL_TO_OBJECT(jsv);
@@ -191,39 +193,39 @@ const value jsValToValue(const JSONContext& cx, const jsval& jsv) {
/**
* Consumes JSON strings and populates a JS object.
*/
-bool consumeJSON(const JSONContext& cx, JSONParser* parser, const list<std::string>& ilist) {
+failable<bool, std::string> consumeJSON(const JSONContext& cx, JSONParser* parser, const list<std::string>& ilist) {
if (isNil(ilist))
return true;
JSString* jstr = JS_NewStringCopyZ(cx, car(ilist).c_str());
if(!JS_ConsumeJSONText(cx, parser, JS_GetStringChars(jstr), JS_GetStringLength(jstr)))
- return false;
+ return "JS_ConsumeJSONText failed";
return consumeJSON(cx, parser, cdr(ilist));
}
/**
* Read JSON tokens from list of strings.
*/
-const list<value> readJSON(const JSONContext& cx, const list<std::string>& ilist) {
+const failable<list<value>, std::string> readJSON(const JSONContext& cx, const list<std::string>& ilist) {
jsval val;
JSONParser* parser = JS_BeginJSONParse(cx, &val);
if(parser == NULL)
- return list<value> ();
+ return std::string("JS_BeginJSONParse failed");
- bool ok = consumeJSON(cx, parser, ilist);
+ const failable<bool, std::string> consumed = consumeJSON(cx, parser, ilist);
if(!JS_FinishJSONParse(cx, parser, JSVAL_NULL))
- return list<value> ();
- if(!ok)
- return list<value> ();
+ return std::string("JS_FinishJSONParse failed");
+ if(!hasValue(consumed))
+ return std::string(consumed);
- return jsValToValue(cx, val);
+ return list<value>(jsValToValue(cx, val));
}
/**
* Returns true if a list represents a JS array.
*/
const bool isJSArray(const list<value>& l) {
- if(l == list<value> ())
+ if(isNil(l))
return false;
value v = car(l);
if(isList(v)) {
@@ -240,8 +242,7 @@ const bool isJSArray(const list<value>& l) {
*/
JSObject* valuesToJSElements(const JSONContext& cx, JSObject* a, const list<value>& l, int i) {
const jsval valueToJSVal(const JSONContext& cx, const value& val);
-
- if (l == list<value>())
+ if (isNil(l))
return a;
jsval pv = valueToJSVal(cx, car(l));
JS_SetElement(cx, a, i, &pv);
@@ -253,8 +254,7 @@ JSObject* valuesToJSElements(const JSONContext& cx, JSObject* a, const list<valu
*/
JSObject* valuesToJSProperties(const JSONContext& cx, JSObject* o, const list<value>& l) {
const jsval valueToJSVal(const JSONContext& cx, const value& val);
-
- if(l == list<value> ())
+ if(isNil(l))
return o;
const list<value> p = car(l);
jsval pv = valueToJSVal(cx, cadr(p));
@@ -274,8 +274,7 @@ const jsval valueToJSVal(const JSONContext& cx, const value& val) {
return BOOLEAN_TO_JSVAL((bool)val);
}
case value::Number: {
- jsdouble d = (double)val;
- return DOUBLE_TO_JSVAL(&d);
+ return DOUBLE_TO_JSVAL(JS_NewDouble(cx, (double)val));
}
case value::List: {
if (isJSArray(val)) {
@@ -312,12 +311,13 @@ template<typename R> JSBool writeCallback(const jschar *buf, uint32 len, void *d
}
/**
- * Write a list of values as a JSON document.
+ * Convert a list of values to a JSON document.
*/
-template<typename R> const R writeJSON(const JSONContext& cx, const lambda<R(R, std::string)>& reduce, const R& initial, const list<value>& l) {
+template<typename R> const failable<R, std::string> writeJSON(const JSONContext& cx, const lambda<R(R, std::string)>& reduce, const R& initial, const list<value>& l) {
jsval val = valueToJSVal(cx, l);
JSONWriteContext<R> wcx(cx, reduce, initial);
- JS_Stringify(cx, &val, NULL, JSVAL_NULL, writeCallback<R>, &wcx);
+ if (!JS_Stringify(cx, &val, NULL, JSVAL_NULL, writeCallback<R>, &wcx))
+ return std::string("JS_Stringify failed");
return wcx.accum;
}
@@ -326,11 +326,13 @@ const list<std::string> writeJSONList(const list<std::string>& listSoFar, const
}
/**
- * Write a list of values as a JSON document represented as a list of strings.
+ * Convert a list of values to a JSON document represented as a list of strings.
*/
-const list<std::string> writeJSON(const JSONContext& cx, const list<value>& l) {
- lambda<list<std::string>(list<std::string>, std::string)> writer(writeJSONList);
- return reverse(writeJSON(cx, writer, list<std::string>(), l));
+const failable<list<std::string>, std::string> writeJSON(const JSONContext& cx, const list<value>& l) {
+ const failable<list<std::string>, std::string> ls = writeJSON<list<std::string> >(cx, writeJSONList, list<std::string>(), l);
+ if (!hasValue(ls))
+ return ls;
+ return reverse(list<std::string>(ls));
}
}