From e982b4ef38fd043c15e89bdd60763b10434a087e Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Sun, 28 Feb 2010 19:40:06 +0000 Subject: Moving old inactive code to a branch as it's confusing code assist, searches and indexing in trunk. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@917273 13f79535-47bb-0310-9956-ffa450edef68 --- .../branches/cpp-contrib/modules/json/Makefile.am | 23 ++ .../cpp-contrib/modules/json/json-test.cpp | 176 +++++++++ sca-cpp/branches/cpp-contrib/modules/json/json.hpp | 403 +++++++++++++++++++++ 3 files changed, 602 insertions(+) create mode 100644 sca-cpp/branches/cpp-contrib/modules/json/Makefile.am create mode 100644 sca-cpp/branches/cpp-contrib/modules/json/json-test.cpp create mode 100644 sca-cpp/branches/cpp-contrib/modules/json/json.hpp (limited to 'sca-cpp/branches/cpp-contrib/modules/json') diff --git a/sca-cpp/branches/cpp-contrib/modules/json/Makefile.am b/sca-cpp/branches/cpp-contrib/modules/json/Makefile.am new file mode 100644 index 0000000000..5bd435fe99 --- /dev/null +++ b/sca-cpp/branches/cpp-contrib/modules/json/Makefile.am @@ -0,0 +1,23 @@ +# 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. + +noinst_PROGRAMS = json-test + +json_test_SOURCES = json-test.cpp +json_test_LDFLAGS = -lmozjs + +TESTS = json-test diff --git a/sca-cpp/branches/cpp-contrib/modules/json/json-test.cpp b/sca-cpp/branches/cpp-contrib/modules/json/json-test.cpp new file mode 100644 index 0000000000..b74f068710 --- /dev/null +++ b/sca-cpp/branches/cpp-contrib/modules/json/json-test.cpp @@ -0,0 +1,176 @@ +/* + * 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 +#include "stream.hpp" +#include "string.hpp" +#include "json.hpp" + +namespace tuscany { +namespace json { + +bool testJSEval() { + JSONContext cx; + const string script("(function testJSON(n){ return JSON.parse(JSON.stringify(n)) })(5)"); + jsval 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; +} + +ostream* jsonWriter(const string& s, ostream* os) { + (*os) << s; + return os; +} + +bool testJSON() { + const JSONContext cx; + + { + const list ad = mklist(mklist(attribute, "city", string("san francisco")), mklist(attribute, "state", string("ca"))); + const list ac = mklist(mklist(element, "id", string("1234")), mklist(attribute, "balance", 1000)); + const list cr = mklist(mklist (attribute, "name", string("jdoe")), cons(element, cons("address", ad)), cons(element, cons("account", ac))); + const list c = mklist(cons(element, cons("customer", cr))); + + ostringstream os; + writeJSON(jsonWriter, &os, c, cx); + assert(str(os) == "{\"customer\":{\"@name\":\"jdoe\",\"address\":{\"@city\":\"san francisco\",\"@state\":\"ca\"},\"account\":{\"id\":\"1234\",\"@balance\":1000}}}"); + } + { + const list phones = mklist (string("408-1234"), string("650-1234")); + const list l = mklist (mklist (element, "phones", phones), mklist (element, "lastName", string("test\ttab")), mklist (attribute, "firstName", string("test1"))); + + ostringstream os; + writeJSON(jsonWriter, &os, l, cx); + assert(str(os) == "{\"phones\":[\"408-1234\",\"650-1234\"],\"lastName\":\"test\\u0009tab\",\"@firstName\":\"test1\"}"); + + istringstream is(str(os)); + const list il = streamList(is); + const list r = content(readJSON(il, cx)); + assert(r == l); + + ostringstream wos; + write(content(writeJSON(r, cx)), wos); + assert(str(wos) == str(os)); + } + { + const list l = mklist(list() + "ns1:echoString" + (list() + "@xmlns:ns1" + string("http://ws.apache.org/axis2/services/echo")) + (list() + "text" + string("Hello World!"))); + ostringstream wos; + write(content(writeJSON(valuesToElements(l), cx)), wos); + assert(str(wos) == "{\"ns1:echoString\":{\"@xmlns:ns1\":\"http://ws.apache.org/axis2/services/echo\",\"text\":\"Hello World!\"}}"); + + istringstream is(str(wos)); + const list il = streamList(is); + const list r = elementsToValues(content(readJSON(il, cx))); + assert(r == l); + } + return true; +} + +bool testJSONRPC() { + JSONContext cx; + { + const string lm("{\"id\": 1, \"method\": \"system.listMethods\", \"params\": []}"); + const list e = content(readJSON(mklist(lm), cx)); + const list v = elementsToValues(e); + assert(assoc("id", v) == mklist("id", 1)); + assert(assoc("method", v) == mklist("method", string("system.listMethods"))); + assert(assoc("params", v) == mklist("params", list())); + } + { + const string i("{\"id\":3,\"result\":[{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"},{\"price\":\"$1.55\",\"name\":\"Pear\"}]}"); + const list e = content(readJSON(mklist(i), cx)); + 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 e2 = content(readJSON(mklist(i), cx)); + assert(e == e2); + } + { + const string i("{\"id\":3,\"result\":[{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"},{\"price\":\"$1.55\",\"name\":\"Pear\"}]}"); + const list e = content(readJSON(mklist(i), cx)); + ostringstream os; + write(content(writeJSON(e, cx)), os); + assert(str(os) == i); + const list v = elementsToValues(e); + const list r = valuesToElements(v); + assert(r == e); + } + { + const list r = mklist(mklist("id", 1), mklist("result", mklist(string("Service.get"), string("Service.getTotal")))); + const list e = valuesToElements(r); + ostringstream os; + write(content(writeJSON(e, cx)), os); + assert(str(os) == "{\"id\":1,\"result\":[\"Service.get\",\"Service.getTotal\"]}"); + } + { + 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 r = content(readJSON(mklist(f), cx)); + const list v = elementsToValues(r); + const list e = valuesToElements(v); + ostringstream os; + write(content(writeJSON(e, cx)), os); + assert(str(os) == f); + } + { + const list arg = mklist(list() + "ns1:echoString" + (list() + "@xmlns:ns1" + string("http://ws.apache.org/axis2/services/echo")) + (list() + "text" + string("Hello World!"))); + const failable > r = jsonRequest(1, "echo", mklist(arg), cx); + ostringstream os; + write(content(r), os); + assert(str(os) == "{\"id\":1,\"method\":\"echo\",\"params\":[{\"ns1:echoString\":{\"@xmlns:ns1\":\"http://ws.apache.org/axis2/services/echo\",\"text\":\"Hello World!\"}}]}"); + + istringstream is(str(os)); + const list il = streamList(is); + const list ir = elementsToValues(content(readJSON(il, cx))); + assert(car(cadr(caddr(ir))) == arg); + } + { + const list res = mklist(list() + "ns1:echoString" + (list() + "@xmlns:ns1" + string("http://ws.apache.org/axis2/c/samples")) + (list() + "text" + string("Hello World!"))); + const failable > r = jsonResult(1, res, cx); + ostringstream os; + write(content(r), os); + assert(str(os) == "{\"id\":1,\"result\":{\"ns1:echoString\":{\"@xmlns:ns1\":\"http://ws.apache.org/axis2/c/samples\",\"text\":\"Hello World!\"}}}"); + + istringstream is(str(os)); + const list il = streamList(is); + const list ir = elementsToValues(content(readJSON(il, cx))); + assert(cdr(cadr(ir)) == res); + } + return true; +} + +} +} + +int main() { + tuscany::cout << "Testing..." << tuscany::endl; + + tuscany::json::testJSEval(); + tuscany::json::testJSON(); + tuscany::json::testJSONRPC(); + + tuscany::cout << "OK" << tuscany::endl; + + return 0; +} diff --git a/sca-cpp/branches/cpp-contrib/modules/json/json.hpp b/sca-cpp/branches/cpp-contrib/modules/json/json.hpp new file mode 100644 index 0000000000..1d966d3f67 --- /dev/null +++ b/sca-cpp/branches/cpp-contrib/modules/json/json.hpp @@ -0,0 +1,403 @@ +/* + * 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. + */ + +#define XP_UNIX +#include +#include "string.hpp" +#include "list.hpp" +#include "value.hpp" +#include "element.hpp" +#include "monad.hpp" + +namespace tuscany { +namespace json { + +/** + * Report JSON errors. + */ +void reportError(unused JSContext *cx, const char *message, JSErrorReport *report) { + cerr << (const char*)(report->filename? report->filename : "") << ":" + << (int)report->lineno << ":" << message << endl; +} + +/** + * Encapsulates a JavaScript runtime. Shared by multiple threads in + * a process. + */ +class JSONRuntime { +public: + JSONRuntime() { + // Create JS runtime + rt = JS_NewRuntime(8L * 1024L * 1024L); + if(rt == NULL) + cleanup(); + } + + operator JSRuntime*() const { + return rt; + } +private: + bool cleanup() { + if(rt != NULL) { + JS_DestroyRuntime(rt); + rt = NULL; + } + JS_ShutDown(); + return true; + } + + JSRuntime* rt; +} jsRuntime; + +JSClass jsGlobalClass = { "global", JSCLASS_GLOBAL_FLAGS, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, + JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, JSCLASS_NO_OPTIONAL_MEMBERS}; + +/** + * Represents a JavaScript context. Create one per thread. + */ +class JSONContext { +public: + JSONContext() { + // Create JS context + cx = JS_NewContext(jsRuntime, 8192); + if(cx == NULL) + return; + JS_SetOptions(cx, JSOPTION_VAROBJFIX); + JS_SetVersion(cx, JSVERSION_DEFAULT); + JS_SetErrorReporter(cx, reportError); + + // Create global JS object + global = JS_NewObject(cx, &jsGlobalClass, NULL, NULL); + if(global == NULL) { + cleanup(); + return; + } + + // Populate global object with the standard globals, like Object and Array + if(!JS_InitStandardClasses(cx, global)) { + cleanup(); + return; + } + } + + ~JSONContext() { + cleanup(); + } + + operator JSContext*() const { + return cx; + } + + JSObject* getGlobal() const { + return global; + } + +private: + bool cleanup() { + if(cx != NULL) { + JS_DestroyContext(cx); + cx = NULL; + } + return true; + } + + JSContext* cx; + JSObject* global; +}; + +/** + * Returns true if a list represents a JS array. + */ +const bool isJSArray(const list& l) { + if(isNil(l)) + return true; + const value v = car(l); + if (isSymbol(v)) + return false; + if(isList(v)) { + if(!isNil((list)v) && isSymbol(car(v))) + return false; + } + return true; +} + +/** + * Converts JS properties to values. + */ +const list jsPropertiesToValues(const list& propertiesSoFar, JSObject* o, JSObject* i, const JSONContext& cx) { + + const value jsValToValue(const jsval& jsv, const JSONContext& cx); + + jsid id; + if(!JS_NextProperty(cx, i, &id) || id == JSVAL_VOID) + return propertiesSoFar; + jsval jsv; + if(!JS_GetPropertyById(cx, o, id, &jsv)) + return propertiesSoFar; + const value val = jsValToValue(jsv, cx); + + jsval idv; + JS_IdToValue(cx, id, &idv); + if(JSVAL_IS_STRING(idv)) { + const string name = JS_GetStringBytes(JSVAL_TO_STRING(idv)); + if (substr(name, 0, 1) == atsign) + return jsPropertiesToValues(cons(mklist(attribute, c_str(substr(name, 1)), val), propertiesSoFar), o, i, cx); + if (isList(val) && !isJSArray(val)) + return jsPropertiesToValues(cons(cons(element, cons(c_str(name), list(val))), propertiesSoFar), o, i, cx); + return jsPropertiesToValues(cons (mklist (element, c_str(name), val), propertiesSoFar), o, i, cx); + } + return jsPropertiesToValues(cons(val, propertiesSoFar), o, i, cx); +} + +/** + * Converts a JS val to a value. + */ +const value jsValToValue(const jsval& jsv, const JSONContext& cx) { + switch(JS_TypeOfValue(cx, jsv)) { + case JSTYPE_STRING: { + return value(string(JS_GetStringBytes(JSVAL_TO_STRING(jsv)))); + } + case JSTYPE_BOOLEAN: { + return value((bool)JSVAL_TO_BOOLEAN(jsv)); + } + case JSTYPE_NUMBER: { + jsdouble jsd; + JS_ValueToNumber(cx, jsv, &jsd); + return value((double)jsd); + } + case JSTYPE_OBJECT: { + JSObject* o = JSVAL_TO_OBJECT(jsv); + JSObject* i = JS_NewPropertyIterator(cx, o); + if(i == NULL) + return value(list ()); + const value pv = jsPropertiesToValues(list (), o, i, cx); + return pv; + } + default: { + return value(); + } + } +} + +/** + * Consumes JSON strings and populates a JS object. + */ +failable consume(JSONParser* parser, const list& ilist, const JSONContext& cx) { + if (isNil(ilist)) + return true; + JSString* jstr = JS_NewStringCopyZ(cx, c_str(car(ilist))); + if(!JS_ConsumeJSONText(cx, parser, JS_GetStringChars(jstr), JS_GetStringLength(jstr))) + return mkfailure("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 > readJSON(const list& ilist, const JSONContext& cx) { + jsval val; + JSONParser* parser = JS_BeginJSONParse(cx, &val); + if(parser == NULL) + return mkfailure >("JS_BeginJSONParse failed"); + + const failable consumed = consume(parser, ilist, cx); + + if(!JS_FinishJSONParse(cx, parser, JSVAL_NULL)) + return mkfailure >("JS_FinishJSONParse failed"); + if(!hasContent(consumed)) + return mkfailure >(reason(consumed)); + + return list(jsValToValue(val, cx)); +} + +/** + * Converts a list of values to JS array elements. + */ +JSObject* valuesToJSElements(JSObject* a, const list& l, int i, const JSONContext& cx) { + const jsval valueToJSVal(const value& val, const JSONContext& cx); + if (isNil(l)) + return a; + jsval pv = valueToJSVal(car(l), cx); + JS_SetElement(cx, a, i, &pv); + return valuesToJSElements(a, cdr(l), ++i, cx); +} + +/** + * Converts a value to a JS val. + */ +const jsval valueToJSVal(const value& val, const JSONContext& cx) { + JSObject* valuesToJSProperties(JSObject* o, const list& l, const JSONContext& cx); + + switch(type(val)) { + case value::String: + case value::Symbol: { + return STRING_TO_JSVAL(JS_NewStringCopyZ(cx, c_str((string)val))); + } + case value::Bool: { + return BOOLEAN_TO_JSVAL((bool)val); + } + case value::Number: { + return DOUBLE_TO_JSVAL(JS_NewDouble(cx, (double)val)); + } + case value::List: { + if (isJSArray(val)) + return OBJECT_TO_JSVAL(valuesToJSElements(JS_NewArrayObject(cx, 0, NULL), val, 0, cx)); + return OBJECT_TO_JSVAL(valuesToJSProperties(JS_NewObject(cx, NULL, NULL, NULL), val, cx)); + } + default: { + return JSVAL_VOID; + } + } +} + +/** + * Converts a list of values to JS properties. + */ +JSObject* valuesToJSProperties(JSObject* o, const list& l, const JSONContext& cx) { + if (isNil(l)) + return o; + + // Write an attribute + const value token(car(l)); + + if (isTaggedList(token, attribute)) { + jsval pv = valueToJSVal(attributeValue(token), cx); + JS_SetProperty(cx, o, c_str(atsign + 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, 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, c_str(string(elementName(token))), &pv); + + // Write its children + valuesToJSProperties(child, elementChildren(token), cx); + } + } + + // Go on + return valuesToJSProperties(o, cdr(l), cx); +} + +/** + * Context passed to the JSON write callback function. + */ +template class WriteContext { +public: + WriteContext(const lambda& reduce, const R& accum, const JSONContext& cx) : cx(cx), reduce(reduce), accum(accum) { + } + const JSONContext& cx; + const lambda reduce; + R accum; +}; + +/** + * Called by JS_Stringify to write JSON out. + */ +template JSBool writeCallback(const jschar *buf, uint32 len, void *data) { + WriteContext& wcx = *(static_cast*> (data)); + JSString* jstr = JS_NewUCStringCopyN(wcx.cx, buf, len); + 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 const failable writeJSON(const lambda& reduce, const R& initial, const list& l, const JSONContext& cx) { + jsval val = OBJECT_TO_JSVAL(valuesToJSProperties(JS_NewObject(cx, NULL, NULL, NULL), l, cx)); + + WriteContext wcx(reduce, initial, cx); + if (!JS_Stringify(cx, &val, NULL, JSVAL_NULL, writeCallback, &wcx)) + return mkfailure("JS_Stringify failed"); + return wcx.accum; +} + +/** + * Convert a list of values to a list of strings representing a JSON document. + */ +const failable > writeJSON(const list& l, const JSONContext& cx) { + const failable > ls = writeJSON>(rcons, list(), l, cx); + if (!hasContent(ls)) + return ls; + return reverse(list(content(ls))); +} + +/** + * Convert a list of function + params to a JSON-RPC request. + */ +const failable > jsonRequest(const value& id, const value& func, const value& params, json::JSONContext& cx) { + const list r = mklist(mklist("id", id), mklist("method", string(func)), mklist("params", params)); + return writeJSON(valuesToElements(r), cx); +} + +/** + * Convert a value to a JSON-RPC result. + */ +const failable > jsonResult(const value& id, const value& val, JSONContext& cx) { + return writeJSON(valuesToElements(mklist(mklist("id", id), mklist("result", val))), cx); +} + +/** + * Convert a JSON-RPC result to a value. + */ +const failable jsonResultValue(const list& s, JSONContext& cx) { + const failable > jsres = json::readJSON(s, cx); + if (!hasContent(jsres)) + return mkfailure(reason(jsres)); + const list rval(cadr(elementsToValues(content(jsres)))); + const value val = cadr(rval); + if (isList(val) && !isJSArray(val)) + return value(mklist(val)); + return val; +} + +/** + * 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) > 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; +} + +} +} + +#endif /* tuscany_json_hpp */ -- cgit v1.2.3