From 4a4c32fea2f0cc36b01ee1382964b10936e1d7d0 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Sun, 11 Oct 2009 00:01:09 +0000 Subject: 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 --- cpp/sca/modules/json/json.hpp | 52 ++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 25 deletions(-) (limited to 'cpp/sca/modules/json/json.hpp') 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 #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& ilist) { +failable consumeJSON(const JSONContext& cx, JSONParser* parser, const list& 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 readJSON(const JSONContext& cx, const list& ilist) { +const failable, std::string> readJSON(const JSONContext& cx, const list& ilist) { jsval val; JSONParser* parser = JS_BeginJSONParse(cx, &val); if(parser == NULL) - return list (); + return std::string("JS_BeginJSONParse failed"); - bool ok = consumeJSON(cx, parser, ilist); + const failable consumed = consumeJSON(cx, parser, ilist); if(!JS_FinishJSONParse(cx, parser, JSVAL_NULL)) - return list (); - if(!ok) - return list (); + return std::string("JS_FinishJSONParse failed"); + if(!hasValue(consumed)) + return std::string(consumed); - return jsValToValue(cx, val); + return list(jsValToValue(cx, val)); } /** * Returns true if a list represents a JS array. */ const bool isJSArray(const list& l) { - if(l == list ()) + if(isNil(l)) return false; value v = car(l); if(isList(v)) { @@ -240,8 +242,7 @@ const bool isJSArray(const list& l) { */ JSObject* valuesToJSElements(const JSONContext& cx, JSObject* a, const list& l, int i) { const jsval valueToJSVal(const JSONContext& cx, const value& val); - - if (l == list()) + 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& l) { const jsval valueToJSVal(const JSONContext& cx, const value& val); - - if(l == list ()) + if(isNil(l)) return o; const list 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 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 const R writeJSON(const JSONContext& cx, const lambda& reduce, const R& initial, const list& l) { +template const failable writeJSON(const JSONContext& cx, const lambda& reduce, const R& initial, const list& l) { jsval val = valueToJSVal(cx, l); JSONWriteContext wcx(cx, reduce, initial); - JS_Stringify(cx, &val, NULL, JSVAL_NULL, writeCallback, &wcx); + if (!JS_Stringify(cx, &val, NULL, JSVAL_NULL, writeCallback, &wcx)) + return std::string("JS_Stringify failed"); return wcx.accum; } @@ -326,11 +326,13 @@ const list writeJSONList(const list& 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 writeJSON(const JSONContext& cx, const list& l) { - lambda(list, std::string)> writer(writeJSONList); - return reverse(writeJSON(cx, writer, list(), l)); +const failable, std::string> writeJSON(const JSONContext& cx, const list& l) { + const failable, std::string> ls = writeJSON >(cx, writeJSONList, list(), l); + if (!hasValue(ls)) + return ls; + return reverse(list(ls)); } } -- cgit v1.2.3