/* * 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 #include #include #include "slist.hpp" #include "json.hpp" namespace tuscany { namespace json { bool testJSEval() { JSONContext cx; 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))); assert(r == "5"); return true; } std::ostringstream* jsonWriter(const std::string& s, std::ostringstream* os) { (*os) << s; return os; } bool testJSON() { const JSONContext cx; { const list ad = mklist(mklist(attribute, "city", std::string("san francisco")), mklist(attribute, "state", std::string("ca"))); const list ac = mklist(mklist(element, "id", std::string("1234")), mklist(attribute, "balance", 1000)); const list cr = mklist(mklist (attribute, "name", std::string("jdoe")), cons(element, cons("address", ad)), cons(element, cons("account", ac))); const list c = mklist(cons(element, cons("customer", cr))); std::ostringstream os; writeJSON(jsonWriter, &os, c, cx); assert(os.str() == "{\"customer\":{\"name\":\"jdoe\",\"address\":{\"city\":\"san francisco\",\"state\":\"ca\"},\"account\":{\"id\":\"1234\",\"balance\":1000}}}"); } { const list phones = mklist (std::string("408-1234"), std::string("650-1234")); const list l = mklist (mklist (element, "phones", phones), mklist (element, "lastName", std::string("test\ttab")), mklist (element, "firstName", std::string("test1"))); std::ostringstream os; writeJSON(jsonWriter, &os, l, cx); assert(os.str() == "{\"phones\":[\"408-1234\",\"650-1234\"],\"lastName\":\"test\\u0009tab\",\"firstName\":\"test1\"}"); std::istringstream is(os.str()); const list il = streamList(is); const list r = readJSON(il, cx); assert(r == l); std::ostringstream wos; write(writeJSON(r, cx), wos); assert(wos.str() == os.str()); } return true; } bool testJSONRPC() { JSONContext cx; { const std::string lm("{\"id\": 1, \"method\": \"system.listMethods\", \"params\": []}"); const list e = readJSON(mklist(lm), cx); const list v = elementsToValues(e); assert(assoc("id", v) == mklist("id", 1)); assert(assoc("method", v) == mklist("method", std::string("system.listMethods"))); assert(assoc("params", v) == mklist("params", list())); } { const std::string i("{\"id\":3,\"result\":[{\"price\":\"$2.99\",\"name\":\"Apple\"},{\"price\":\"$3.55\",\"name\":\"Orange\"},{\"price\":\"$1.55\",\"name\":\"Pear\"}]}"); const list e = 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 list e2 = 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 list e = readJSON(mklist(i), cx); std::ostringstream os; write(writeJSON(e, cx), os); assert(os.str() == i); const list v = elementsToValues(e); const list r = valuesToElements(v); assert(r == e); } { const list r = mklist(mklist("id", 1), mklist("result", mklist(std::string("Service.get"), std::string("Service.getTotal")))); const list e = valuesToElements(r); std::ostringstream os; write(writeJSON(e, cx), os); assert(os.str() == "{\"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 list r = readJSON(mklist(f), cx); const list v = elementsToValues(r); const list e = valuesToElements(v); std::ostringstream os; write(writeJSON(e, cx), os); assert(os.str() == f); } return true; } } } int main() { std::cout << "Testing..." << std::endl; tuscany::json::testJSEval(); tuscany::json::testJSON(); tuscany::json::testJSONRPC(); std::cout << "OK" << std::endl; return 0; }