diff options
Diffstat (limited to 'sca-cpp/trunk/modules/json')
-rw-r--r-- | sca-cpp/trunk/modules/json/json-test.cpp | 74 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/json/json.hpp | 62 |
2 files changed, 67 insertions, 69 deletions
diff --git a/sca-cpp/trunk/modules/json/json-test.cpp b/sca-cpp/trunk/modules/json/json-test.cpp index 4d1bbffd5a..b4a6ba8746 100644 --- a/sca-cpp/trunk/modules/json/json-test.cpp +++ b/sca-cpp/trunk/modules/json/json-test.cpp @@ -24,10 +24,8 @@ */ #include <assert.h> -#include <iostream> -#include <sstream> -#include <string> -#include "slist.hpp" +#include "stream.hpp" +#include "string.hpp" #include "json.hpp" namespace tuscany { @@ -35,15 +33,15 @@ namespace json { bool testJSEval() { JSONContext cx; - const std::string script("(function testJSON(n){ return JSON.parse(JSON.stringify(n)) })(5)"); + const 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))); + assert(JS_EvaluateScript(cx, cx.getGlobal(), c_str(script), length(script), "testJSON.js", 1, &rval)); + const string r(JS_GetStringBytes(JS_ValueToString(cx, rval))); assert(r == "5"); return true; } -std::ostringstream* jsonWriter(const std::string& s, std::ostringstream* os) { +ostream* jsonWriter(const string& s, ostream* os) { (*os) << s; return os; } @@ -52,30 +50,30 @@ bool testJSON() { const JSONContext cx; { - const list<value> ad = mklist<value>(mklist<value>(attribute, "city", std::string("san francisco")), mklist<value>(attribute, "state", std::string("ca"))); - const list<value> ac = mklist<value>(mklist<value>(element, "id", std::string("1234")), mklist<value>(attribute, "balance", 1000)); - const list<value> cr = mklist<value>(mklist<value> (attribute, "name", std::string("jdoe")), cons<value>(element, cons<value>("address", ad)), cons<value>(element, cons<value>("account", ac))); + 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))); - std::ostringstream os; - writeJSON<std::ostringstream*>(jsonWriter, &os, c, cx); - assert(os.str() == "{\"customer\":{\"name\":\"jdoe\",\"address\":{\"city\":\"san francisco\",\"state\":\"ca\"},\"account\":{\"id\":\"1234\",\"balance\":1000}}}"); + ostringstream os; + writeJSON<ostream*>(jsonWriter, &os, c, cx); + assert(str(os) == "{\"customer\":{\"name\":\"jdoe\",\"address\":{\"city\":\"san francisco\",\"state\":\"ca\"},\"account\":{\"id\":\"1234\",\"balance\":1000}}}"); } { - const list<value> phones = mklist<value> (std::string("408-1234"), std::string("650-1234")); - const list<value> l = mklist<value> (mklist<value> (element, "phones", phones), mklist<value> (element, "lastName", std::string("test\ttab")), mklist<value> (element, "firstName", std::string("test1"))); + 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> (element, "firstName", string("test1"))); - std::ostringstream os; - writeJSON<std::ostringstream*>(jsonWriter, &os, l, cx); - assert(os.str() == "{\"phones\":[\"408-1234\",\"650-1234\"],\"lastName\":\"test\\u0009tab\",\"firstName\":\"test1\"}"); + ostringstream os; + writeJSON<ostream*>(jsonWriter, &os, l, cx); + assert(str(os) == "{\"phones\":[\"408-1234\",\"650-1234\"],\"lastName\":\"test\\u0009tab\",\"firstName\":\"test1\"}"); - std::istringstream is(os.str()); - const list<std::string> il = streamList(is); + istringstream is(str(os)); + const list<string> il = streamList(is); const list<value> r = content(readJSON(il, cx)); assert(r == l); - std::ostringstream wos; + ostringstream wos; write(content(writeJSON(r, cx)), wos); - assert(wos.str() == os.str()); + assert(str(wos) == str(os)); } return true; } @@ -83,45 +81,45 @@ bool testJSON() { bool testJSONRPC() { JSONContext cx; { - const std::string lm("{\"id\": 1, \"method\": \"system.listMethods\", \"params\": []}"); + const string lm("{\"id\": 1, \"method\": \"system.listMethods\", \"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", std::string("system.listMethods"))); + assert(assoc<value>("method", v) == mklist<value>("method", string("system.listMethods"))); assert(assoc<value>("params", v) == mklist<value>("params", list<value>())); } { - const std::string i("{\"id\":3,\"result\":[{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"},{\"price\":\"$1.55\",\"name\":\"Pear\"}]}"); + const string i("{\"id\":3,\"result\":[{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"},{\"price\":\"$1.55\",\"name\":\"Pear\"}]}"); const list<value> e = content(readJSON(mklist(i), cx)); - const std::string i2("{\"id\":3,\"result\":{\"0\":{\"price\":\"$2.99\",\"name\":\"Apple\"},\"1\":{\"price\":\"$3.55\",\"name\":\"Orange\"},\"2\":{\"price\":\"$1.55\",\"name\":\"Pear\"}}}"); + 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> e2 = content(readJSON(mklist(i), cx)); assert(e == e2); } { - const std::string i("{\"id\":3,\"result\":[{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"},{\"price\":\"$1.55\",\"name\":\"Pear\"}]}"); + const string i("{\"id\":3,\"result\":[{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"},{\"price\":\"$1.55\",\"name\":\"Pear\"}]}"); const list<value> e = content(readJSON(mklist(i), cx)); - std::ostringstream os; + ostringstream os; write(content(writeJSON(e, cx)), os); - assert(os.str() == i); + assert(str(os) == i); 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>(std::string("Service.get"), std::string("Service.getTotal")))); + 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); - std::ostringstream os; + ostringstream os; write(content(writeJSON(e, cx)), os); - assert(os.str() == "{\"id\":1,\"result\":[\"Service.get\",\"Service.getTotal\"]}"); + assert(str(os) == "{\"id\":1,\"result\":[\"Service.get\",\"Service.getTotal\"]}"); } { - const std::string f("{\"id\":1,\"result\":[\"Sample Feed\",\"123456789\",[\"Item\",\"111\",{\"javaClass\":\"services.Item\",\"name\":\"Apple\",\"currencyCode\":\"USD\",\"currencySymbol\":\"$\",\"price\":2.99}],[\"Item\",\"222\",{\"javaClass\":\"services.Item\",\"name\":\"Orange\",\"currencyCode\":\"USD\",\"currencySymbol\":\"$\",\"price\":3.55}],[\"Item\",\"333\",{\"javaClass\":\"services.Item\",\"name\":\"Pear\",\"currencyCode\":\"USD\",\"currencySymbol\":\"$\",\"price\":1.55}]]}"); + const string f("{\"id\":1,\"result\":[\"Sample Feed\",\"123456789\",[\"Item\",\"111\",{\"javaClass\":\"services.Item\",\"name\":\"Apple\",\"currencyCode\":\"USD\",\"currencySymbol\":\"$\",\"price\":2.99}],[\"Item\",\"222\",{\"javaClass\":\"services.Item\",\"name\":\"Orange\",\"currencyCode\":\"USD\",\"currencySymbol\":\"$\",\"price\":3.55}],[\"Item\",\"333\",{\"javaClass\":\"services.Item\",\"name\":\"Pear\",\"currencyCode\":\"USD\",\"currencySymbol\":\"$\",\"price\":1.55}]]}"); const list<value> r = content(readJSON(mklist(f), cx)); const list<value> v = elementsToValues(r); const list<value> e = valuesToElements(v); - std::ostringstream os; + ostringstream os; write(content(writeJSON(e, cx)), os); - assert(os.str() == f); + assert(str(os) == f); } return true; } @@ -130,13 +128,13 @@ bool testJSONRPC() { } int main() { - std::cout << "Testing..." << std::endl; + tuscany::cout << "Testing..." << tuscany::endl; tuscany::json::testJSEval(); tuscany::json::testJSON(); tuscany::json::testJSONRPC(); - std::cout << "OK" << std::endl; + tuscany::cout << "OK" << tuscany::endl; return 0; } diff --git a/sca-cpp/trunk/modules/json/json.hpp b/sca-cpp/trunk/modules/json/json.hpp index 6ce4e3a9a1..9124c5143c 100644 --- a/sca-cpp/trunk/modules/json/json.hpp +++ b/sca-cpp/trunk/modules/json/json.hpp @@ -28,7 +28,7 @@ #define XP_UNIX #include <jsapi.h> -#include <string> +#include "string.hpp" #include "list.hpp" #include "value.hpp" #include "element.hpp" @@ -41,8 +41,8 @@ namespace json { * Report JSON errors. */ void reportError(unused JSContext *cx, const char *message, JSErrorReport *report) { - std::cerr << (const char*)(report->filename? report->filename : "<no filename>") << ":" - << (unsigned int)report->lineno << ":" << message << std::endl; + cerr << (const char*)(report->filename? report->filename : "<no filename>") << ":" + << (int)report->lineno << ":" << message << endl; } /** @@ -168,7 +168,7 @@ const list<value> jsPropertiesToValues(const list<value>& propertiesSoFar, JSObj const value jsValToValue(const jsval& jsv, const JSONContext& cx) { switch(JS_TypeOfValue(cx, jsv)) { case JSTYPE_STRING: { - return value(std::string(JS_GetStringBytes(JSVAL_TO_STRING(jsv)))); + return value(string(JS_GetStringBytes(JSVAL_TO_STRING(jsv)))); } case JSTYPE_BOOLEAN: { return value((bool)JSVAL_TO_BOOLEAN(jsv)); @@ -195,30 +195,30 @@ const value jsValToValue(const jsval& jsv, const JSONContext& cx) { /** * Consumes JSON strings and populates a JS object. */ -failable<bool, std::string> consume(JSONParser* parser, const list<std::string>& ilist, const JSONContext& cx) { +failable<bool> consume(JSONParser* parser, const list<string>& ilist, const JSONContext& cx) { if (isNil(ilist)) return true; - JSString* jstr = JS_NewStringCopyZ(cx, car(ilist).c_str()); + JSString* jstr = JS_NewStringCopyZ(cx, c_str(car(ilist))); if(!JS_ConsumeJSONText(cx, parser, JS_GetStringChars(jstr), JS_GetStringLength(jstr))) - return mkfailure<bool, std::string>("JS_ConsumeJSONText failed"); + 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>, std::string> readJSON(const list<std::string>& ilist, const JSONContext& cx) { +const failable<list<value> > readJSON(const list<string>& ilist, const JSONContext& cx) { jsval val; JSONParser* parser = JS_BeginJSONParse(cx, &val); if(parser == NULL) - return mkfailure<list<value>, std::string>("JS_BeginJSONParse failed"); + return mkfailure<list<value> >("JS_BeginJSONParse failed"); - const failable<bool, std::string> consumed = consume(parser, ilist, cx); + const failable<bool> consumed = consume(parser, ilist, cx); if(!JS_FinishJSONParse(cx, parser, JSVAL_NULL)) - return mkfailure<list<value>, std::string>("JS_FinishJSONParse failed"); + return mkfailure<list<value> >("JS_FinishJSONParse failed"); if(!hasContent(consumed)) - return mkfailure<list<value>, std::string>(reason(consumed)); + return mkfailure<list<value> >(reason(consumed)); return list<value>(jsValToValue(val, cx)); } @@ -261,7 +261,7 @@ JSObject* valuesToJSProperties(JSObject* o, const list<value>& l, const JSONCont return o; const list<value> p = car(l); jsval pv = valueToJSVal(caddr(p), cx); - JS_SetProperty(cx, o, ((std::string)cadr(p)).c_str(), &pv); + JS_SetProperty(cx, o, c_str((string)cadr(p)), &pv); return valuesToJSProperties(o, cdr(l), cx); } @@ -272,7 +272,7 @@ const jsval valueToJSVal(const value& val, const JSONContext& cx) { switch(type(val)) { case value::String: case value::Symbol: { - return STRING_TO_JSVAL(JS_NewStringCopyZ(cx, ((std::string)val).c_str())); + return STRING_TO_JSVAL(JS_NewStringCopyZ(cx, c_str((string)val))); } case value::Bool: { return BOOLEAN_TO_JSVAL((bool)val); @@ -291,7 +291,7 @@ const jsval valueToJSVal(const value& val, const JSONContext& cx) { } } -const failable<bool, std::string> writeList(const list<value>& l, JSObject* o, const JSONContext& cx) { +const failable<bool> writeList(const list<value>& l, JSObject* o, const JSONContext& cx) { if (isNil(l)) return true; @@ -300,24 +300,24 @@ const failable<bool, std::string> writeList(const list<value>& l, JSObject* o, c if (isTaggedList(token, attribute)) { jsval pv = valueToJSVal(attributeValue(token), cx); - JS_SetProperty(cx, o, std::string(attributeName(token)).c_str(), &pv); + JS_SetProperty(cx, o, c_str(string(attributeName(token))), &pv); } else if (isTaggedList(token, element)) { // Write the value of an element if (elementHasValue(token)) { jsval pv = valueToJSVal(elementValue(token), cx); - JS_SetProperty(cx, o, std::string(elementName(token)).c_str(), &pv); + JS_SetProperty(cx, o, c_str(string(elementName(token))), &pv); } else { // Write a parent element JSObject* child = JS_NewObject(cx, NULL, NULL, NULL); jsval pv = OBJECT_TO_JSVAL(child); - JS_SetProperty(cx, o, std::string(elementName(token)).c_str(), &pv); + JS_SetProperty(cx, o, c_str(string(elementName(token))), &pv); // Write its children - const failable<bool, std::string> w = writeList(elementChildren(token), child, cx); + const failable<bool> w = writeList(elementChildren(token), child, cx); if (!hasContent(w)) return w; } @@ -332,10 +332,10 @@ const failable<bool, std::string> writeList(const list<value>& l, JSObject* o, c */ template<typename R> class WriteContext { public: - WriteContext(const lambda<R(const std::string&, const R)>& reduce, const R& accum, const JSONContext& cx) : cx(cx), reduce(reduce), accum(accum) { + WriteContext(const lambda<R(const string&, const R)>& reduce, const R& accum, const JSONContext& cx) : cx(cx), reduce(reduce), accum(accum) { } const JSONContext& cx; - const lambda<R(const std::string&, const R)> reduce; + const lambda<R(const string&, const R)> reduce; R accum; }; @@ -345,40 +345,40 @@ public: 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); - wcx.accum = wcx.reduce(std::string(JS_GetStringBytes(jstr), JS_GetStringLength(jstr)), wcx.accum); + wcx.accum = wcx.reduce(string(JS_GetStringBytes(jstr), JS_GetStringLength(jstr)), wcx.accum); return JS_TRUE; } /** * Convert a list of values to a JSON document. */ -template<typename R> const failable<R, std::string> writeJSON(const lambda<R(const std::string&, const R)>& reduce, const R& initial, const list<value>& l, const JSONContext& cx) { +template<typename R> const failable<R> writeJSON(const lambda<R(const string&, const R)>& reduce, const R& initial, const list<value>& l, const JSONContext& cx) { JSObject* o = JS_NewObject(cx, NULL, NULL, NULL); jsval val = OBJECT_TO_JSVAL(o); - const failable<bool, std::string> w = writeList(l, o, cx); + const failable<bool> w = writeList(l, o, cx); if (!hasContent(w)) - return mkfailure<R, std::string>(reason(w)); + return mkfailure<R>(reason(w)); WriteContext<R> wcx(reduce, initial, cx); if (!JS_Stringify(cx, &val, NULL, JSVAL_NULL, writeCallback<R>, &wcx)) - return mkfailure<R, std::string>("JS_Stringify failed"); + 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<std::string>, std::string> writeJSON(const list<value>& l, const JSONContext& cx) { - const failable<list<std::string>, std::string> ls = writeJSON<list<std::string> >(rcons<std::string>, list<std::string>(), l, cx); +const failable<list<string> > writeJSON(const list<value>& l, const JSONContext& cx) { + const failable<list<string> > ls = writeJSON<list<string>>(rcons<string>, list<string>(), l, cx); if (!hasContent(ls)) return ls; - return reverse(list<std::string>(content(ls))); + return reverse(list<string>(content(ls))); } /** * Convert a function + params to a JSON request. */ -const failable<list<std::string>, std::string> jsonRequest(const value& id, const value& func, const value& params, json::JSONContext& cx) { +const failable<list<string> > jsonRequest(const value& id, const value& func, const value& params, json::JSONContext& cx) { const list<value> r = mklist<value>(mklist<value>("id", id), mklist<value>("method", func), mklist<value>("params", params)); return writeJSON(valuesToElements(r), cx); } @@ -386,7 +386,7 @@ const failable<list<std::string>, std::string> jsonRequest(const value& id, cons /** * Convert a value to a JSON result. */ -const failable<list<std::string>, std::string> jsonResult(const value& id, const value& val, JSONContext& cx) { +const failable<list<string> > jsonResult(const value& id, const value& val, JSONContext& cx) { return writeJSON(valuesToElements(mklist<value>(mklist<value>("id", id), mklist<value>("result", val))), cx); } |