/** * Outputs device tag */ void cmd_getdevicetag(char *line) { const char *devicetag = flashstorage_keyval_get("DEVICETAG"); JSONNODE *n = json_new(JSON_NODE); if(devicetag != 0) { json_push_back(n, json_new_a("devicetag", devicetag)); } else { json_push_back(n, json_new_a("devicetag", "No device tag set")); } json_char *jc = json_write_formatted(n); serial_write_string(jc); json_free(jc); json_delete(n); }
void JSON_Append_text(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; C_TEXT node; C_TEXT value; C_TEXT returnValue; json.fromParamAtIndex(pParams, 1); node.fromParamAtIndex(pParams, 2); value.fromParamAtIndex(pParams, 3); JSONNODE *n = _fromHex(json); std::wstring nodeName; std::wstring valueText; _copyString(node, nodeName); _copyString(value, valueText); JSONNODE *_node = json_new_a(nodeName.c_str(), valueText.c_str()); nodeName.clear(); valueText.clear(); json_push_back(n, _node); _toHex(_node, returnValue); returnValue.setReturn(pResult); }
JSONNODE* tsload_get_dispatchers(void) { JSONNODE* disps = json_new(JSON_ARRAY); json_push_back(disps, json_new_a("simple", "simple")); return disps; }
JSONNODE* json_wlparam_format(wlp_descr_t* wlp) { JSONNODE* wlp_node = json_new(JSON_NODE); json_set_name(wlp_node, wlp->name); switch(wlp->type) { case WLP_BOOL: json_push_back(wlp_node, json_new_a("type", "bool")); if(wlp->defval.enabled) json_push_back(wlp_node, json_new_b("default", wlp->defval.b)); break; case WLP_INTEGER: json_push_back(wlp_node, json_new_a("type", "integer")); if(wlp->range.range) { json_push_back(wlp_node, json_new_i("min", wlp->range.i_min)); json_push_back(wlp_node, json_new_i("max", wlp->range.i_max)); } if(wlp->defval.enabled) json_push_back(wlp_node, json_new_i("default", wlp->defval.i)); break; case WLP_FLOAT: json_push_back(wlp_node, json_new_a("type", "float")); if(wlp->range.range) { json_push_back(wlp_node, json_new_f("min", wlp->range.d_min)); json_push_back(wlp_node, json_new_f("max", wlp->range.d_max)); } if(wlp->defval.enabled) json_push_back(wlp_node, json_new_f("default", wlp->defval.f)); break; case WLP_SIZE: json_push_back(wlp_node, json_new_a("type", "size")); if(wlp->range.range) { json_push_back(wlp_node, json_new_i("min", wlp->range.sz_min)); json_push_back(wlp_node, json_new_i("max", wlp->range.sz_max)); } if(wlp->defval.enabled) json_push_back(wlp_node, json_new_i("default", wlp->defval.sz)); break; case WLP_RAW_STRING: json_push_back(wlp_node, json_new_a("type", "string")); json_push_back(wlp_node, json_new_i("len", wlp->range.str_length)); if(wlp->defval.enabled) json_push_back(wlp_node, json_new_a("default", wlp->defval.s)); break; case WLP_STRING_SET: json_push_back(wlp_node, json_new_a("type", "strset")); json_push_back(wlp_node, json_wlparam_strset_format(wlp)); if(wlp->defval.enabled) json_push_back(wlp_node, json_new_i("default", wlp->defval.ssi)); break; } json_push_back(wlp_node, json_new_i("flags", wlp->flags)); json_push_back(wlp_node, json_new_a("description", wlp->description)); return wlp_node; }
/** * hello: a simple "ping" command to check that the * device is responsive */ void cmd_hello(char *line) { JSONNODE *n = json_new(JSON_NODE); json_push_back(n, json_new_a("hello", "Greetings professor Falken")); json_char *jc = json_write_formatted(n); serial_write_string(jc); json_free(jc); json_delete(n); }
/** * Outputs firmware version */ void cmd_version(char *line) { JSONNODE *n = json_new(JSON_NODE); json_push_back(n, json_new_a("version", OS100VERSION)); json_char *jc = json_write_formatted(n); serial_write_string(jc); json_free(jc); json_delete(n); }
void Json::SetValue(JSONNODE* node, const string& key, const vector<string>& array) { if(node == NULL) node = mpRootNode; JSONNODE* c = json_new(JSON_ARRAY); json_set_name(c, key.c_str()); for(size_t i = 0; i < array.size(); i++) { json_push_back(c, json_new_a(NULL, array[i].c_str())); } json_push_back(node, c); }
void JSON_Append_text_array(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; C_TEXT node; ARRAY_TEXT values; C_TEXT returnValue; json.fromParamAtIndex(pParams, 1); node.fromParamAtIndex(pParams, 2); values.fromParamAtIndex(pParams, 3); JSONNODE *n = _fromHex(json); if(n){ std::wstring nodeName; _copyString(node, nodeName); JSONNODE *_node = json_new(JSON_ARRAY); json_set_name(_node, nodeName.c_str()); for (unsigned int i = 1; i < values.getSize(); ++i) { CUTF16String s; values.copyUTF16StringAtIndex(&s, i); C_TEXT t; t.setUTF16String(&s); std::wstring valueText; _copyString(t, valueText); json_push_back(_node, json_new_a(L"", valueText.c_str())); } json_push_back(n, _node); _toHex(_node, returnValue); } returnValue.setReturn(pResult); }
void Json::SetValue(JSONNODE* node, const string& key, const string& str) { if(node == NULL) node = mpRootNode; json_push_back(node, json_new_a(key.c_str(), str.c_str())); }
void TestSuite::TestFunctions(void){ UnitTest::SetPrefix("TestFunctions.cpp - Swap"); #ifdef JSON_LIBRARY JSONNODE * test1 = json_new(JSON_NODE); JSONNODE * test2 = json_new(JSON_NODE); json_set_i(test1, 14); json_set_i(test2, 35); json_swap(test1, test2); assertEquals_Primitive(json_as_int(test1), 35); assertEquals_Primitive(json_as_int(test2), 14); UnitTest::SetPrefix("TestFunctions.cpp - Duplicate"); json_delete(test1); test1 = json_duplicate(test2); #ifdef JSON_UNIT_TEST assertNotEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #endif assertTrue(json_equal(test1, test2)); UnitTest::SetPrefix("TestFunctions.cpp - Duplicate with children"); JSONNODE * node = json_new(JSON_NODE); json_push_back(node, json_new_i(JSON_TEXT(""), 15)); json_push_back(node, json_new_f(JSON_TEXT(""), 27.4f)); json_push_back(node, json_new_b(JSON_TEXT(""), true)); TestSuite::testParsingItself(node); JSONNODE * dup = json_duplicate(node); assertEquals(json_size(dup), 3); #ifdef JSON_UNIT_TEST assertNotEquals(((JSONNode*)node) -> internal, ((JSONNode*)dup) -> internal); #endif assertEquals(json_type(dup), JSON_NODE); TestSuite::testParsingItself(node); TestSuite::testParsingItself(dup); assertEquals_Primitive(json_as_int(json_at(dup, 0)), 15); assertEquals_Primitive(json_as_float(json_at(dup, 1)), 27.4f); assertEquals(json_as_bool(json_at(dup, 2)), true); assertTrue(json_equal(json_at(dup, 0), json_at(node, 0))); assertTrue(json_equal(json_at(dup, 1), json_at(node, 1))); assertTrue(json_equal(json_at(dup, 2), json_at(node, 2))); TestSuite::testParsingItself(dup); #ifdef JSON_ITERATORS for(JSONNODE_ITERATOR it = json_begin(node), end = json_end(node), dup_it = json_begin(dup); it != end; ++it, ++dup_it){ assertTrue(json_equal(*it, *dup_it)); #ifdef JSON_UNIT_TEST assertNotEquals(((JSONNode*)(*it)) -> internal, ((JSONNode*)(*dup_it)) -> internal); #endif } #endif UnitTest::SetPrefix("TestFunctions.cpp - Nullify"); json_nullify(test1); assertEquals(json_type(test1), JSON_NULL); json_char * res = json_name(test1); assertCStringSame(res, JSON_TEXT("")); json_free(res); #ifdef JSON_CASTABLE UnitTest::SetPrefix("TestFunctions.cpp - Cast"); json_cast(test1, JSON_NULL); json_set_i(test2, 1); json_cast(test2, JSON_BOOL); assertEquals(json_type(test1), JSON_NULL); assertEquals(json_type(test2), JSON_BOOL); assertEquals(json_as_bool(test2), true); json_set_b(test2, true); assertEquals(json_as_bool(test2), true); json_cast(test2, JSON_NUMBER); assertEquals_Primitive(json_as_float(test2), 1.0f); json_set_f(test2, 0.0f); assertEquals_Primitive(json_as_float(test2), 0.0f); json_cast(test2, JSON_BOOL); assertEquals(json_as_bool(test2), false); #endif UnitTest::SetPrefix("TestFunctions.cpp - Merge"); json_set_a(test1, JSON_TEXT("hello")); json_set_a(test2, JSON_TEXT("hello")); #ifdef JSON_UNIT_TEST assertNotEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #endif assertTrue(json_equal(test1, test2)); json_merge(test1, test2); #ifdef JSON_UNIT_TEST #ifdef JSON_REF_COUNT assertEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #else assertNotEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #endif #endif #ifdef JSON_CASTABLE json_cast(test1, JSON_NODE); json_cast(test2, JSON_NODE); assertEquals(json_type(test1), JSON_NODE); assertEquals(json_type(test2), JSON_NODE); json_push_back(test1, json_new_a(JSON_TEXT("hi"), JSON_TEXT("world"))); json_push_back(test2, json_new_a(JSON_TEXT("hi"), JSON_TEXT("world"))); TestSuite::testParsingItself(test1); TestSuite::testParsingItself(test2); json_merge(test1, test2); #ifdef JSON_UNIT_TEST #ifdef JSON_REF_COUNT assertEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #else assertNotEquals(((JSONNode*)test1) -> internal, ((JSONNode*)test2) -> internal); #endif #endif TestSuite::testParsingItself(test1); TestSuite::testParsingItself(test2); #endif json_delete(test1); json_delete(test2); json_delete(node); json_delete(dup); #else JSONNode test1; JSONNode test2; test1 = JSON_TEXT("hello"); test2 = JSON_TEXT("world"); test1.swap(test2); assertEquals(test1, JSON_TEXT("world")); assertEquals(test2, JSON_TEXT("hello")); UnitTest::SetPrefix("TestFunctions.cpp - Duplicate"); test1 = test2.duplicate(); #ifdef JSON_UNIT_TEST assertNotEquals(test1.internal, test2.internal); #endif assertEquals(test1, test2); UnitTest::SetPrefix("TestFunctions.cpp - Duplicate with children"); JSONNode node = JSONNode(JSON_NODE); node.push_back(JSONNode(JSON_TEXT(""), 15)); node.push_back(JSONNode(JSON_TEXT(""), JSON_TEXT("hello world"))); node.push_back(JSONNode(JSON_TEXT(""), true)); TestSuite::testParsingItself(node); JSONNode dup = node.duplicate(); assertEquals(dup.size(), 3); #ifdef JSON_UNIT_TEST assertNotEquals(node.internal, dup.internal); #endif assertEquals(dup.type(), JSON_NODE); TestSuite::testParsingItself(node); TestSuite::testParsingItself(dup); try { assertEquals(dup.at(0), 15); assertEquals(dup.at(1), JSON_TEXT("hello world")); assertEquals(dup.at(2), true); assertEquals(dup.at(0), node.at(0)); assertEquals(dup.at(1), node.at(1)); assertEquals(dup.at(2), node.at(2)); } catch (std::out_of_range){ FAIL("exception caught"); } TestSuite::testParsingItself(dup); #ifdef JSON_ITERATORS for(JSONNode::iterator it = node.begin(), end = node.end(), dup_it = dup.begin(); it != end; ++it, ++dup_it){ assertEquals(*it, *dup_it); #ifdef JSON_UNIT_TEST assertNotEquals((*it).internal, (*dup_it).internal); #endif } #endif UnitTest::SetPrefix("TestFunctions.cpp - Nullify"); test1.nullify(); assertEquals(test1.type(), JSON_NULL); assertEquals(test1.name(), JSON_TEXT("")); #ifdef JSON_CASTABLE UnitTest::SetPrefix("TestFunctions.cpp - Cast"); test1.cast(JSON_NULL); test2 = 1; test2.cast(JSON_BOOL); assertEquals(test1.type(), JSON_NULL); assertEquals(test2.type(), JSON_BOOL); assertEquals(test2, true); test2 = true; assertEquals(test2, true); test2.cast(JSON_NUMBER); assertEquals(test2, 1.0f); test2 = 0.0f; assertEquals(test2, 0.0f); test2.cast(JSON_BOOL); assertEquals(test2, false); #endif UnitTest::SetPrefix("TestFunctions.cpp - Merge"); test1 = JSON_TEXT("hello"); test2 = JSON_TEXT("hello"); #ifdef JSON_UNIT_TEST assertNotEquals(test1.internal, test2.internal); #endif assertEquals(test1, test2); test1.merge(test2); #ifdef JSON_UNIT_TEST #ifdef JSON_REF_COUNT assertEquals(test1.internal, test2.internal); #else assertNotEquals(test1.internal, test2.internal); #endif #endif #ifdef JSON_CASTABLE test1.cast(JSON_NODE); test2.cast(JSON_NODE); #else test1 = JSONNode(JSON_NODE); test2 = JSONNode(JSON_NODE); #endif assertEquals(test1.type(), JSON_NODE); assertEquals(test2.type(), JSON_NODE); test1.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world"))); test2.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world"))); TestSuite::testParsingItself(test1); TestSuite::testParsingItself(test2); test1.merge(test2); #ifdef JSON_UNIT_TEST #ifdef JSON_REF_COUNT assertEquals(test1.internal, test2.internal); #else assertNotEquals(test1.internal, test2.internal); #endif #endif TestSuite::testParsingItself(test1); TestSuite::testParsingItself(test2); #endif }
bool admJson::addString(const char *key,const char *value) { json_push_back(COOKIE, json_new_a(key,value)); return true; }
void TestSuite::TestInspectors(void){ UnitTest::SetPrefix("TestInspectors.cpp - Inspectors"); #ifdef JSON_LIBRARY JSONNODE * test = json_new(JSON_NULL); assertEquals(json_type(test), JSON_NULL); json_char * res = json_as_string(test); assertCStringSame(res, JSON_TEXT("")); json_free(res); assertEquals_Primitive(json_as_int(test), 0); assertEquals_Primitive(json_as_float(test), 0.0f); assertEquals(json_as_bool(test), false); json_set_f(test, 15.5f); assertEquals(json_type(test), JSON_NUMBER); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("15.5")); json_free(res); #endif assertEquals_Primitive(json_as_int(test), 15); assertEquals_Primitive(json_as_float(test), 15.5f); #ifdef JSON_CASTABLE assertEquals(json_as_bool(test), true); #endif json_set_f(test, 0.0f); assertEquals(json_type(test), JSON_NUMBER); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("0")); json_free(res); #endif assertEquals_Primitive(json_as_int(test), 0); assertEquals_Primitive(json_as_float(test), 0.0f); #ifdef JSON_CASTABLE assertEquals(json_as_bool(test), false); #endif json_set_b(test, true); assertEquals(json_type(test), JSON_BOOL); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("true")); json_free(res); assertEquals_Primitive(json_as_int(test), 1); assertEquals_Primitive(json_as_float(test), 1.0f); #endif assertEquals(json_as_bool(test), true); json_set_b(test, false); assertEquals(json_type(test), JSON_BOOL); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("false")); json_free(res); assertEquals_Primitive(json_as_int(test), 0); assertEquals_Primitive(json_as_float(test), 0.0f); #endif assertEquals(json_as_bool(test), false); #ifdef JSON_CASTABLE json_cast(test, JSON_NODE); assertEquals(json_type(test), JSON_NODE); assertEquals(json_size(test), 0); json_push_back(test, json_new_a(JSON_TEXT("hi"), JSON_TEXT("world"))); json_push_back(test, json_new_a(JSON_TEXT("hello"), JSON_TEXT("mars"))); json_push_back(test, json_new_a(JSON_TEXT("salut"), JSON_TEXT("france"))); assertEquals(json_size(test), 3); TestSuite::testParsingItself(test); JSONNODE * casted = json_as_array(test); #ifdef JSON_UNIT_TEST assertNotEquals(((JSONNode*)casted) -> internal, ((JSONNode*)test) -> internal); #endif assertEquals(json_type(casted), JSON_ARRAY); assertEquals(json_type(test), JSON_NODE); assertEquals(json_size(test), 3); assertEquals(json_size(casted), 3); TestSuite::testParsingItself(casted); #endif UnitTest::SetPrefix("TestInspectors.cpp - Location"); #ifdef JSON_CASTABLE #define CheckAt(parent, locale, text)\ if(JSONNODE * temp = json_at(parent, locale)){\ json_char * _res = json_as_string(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckAt: ") + #parent + "[" + #locale + "]");\ } #define CheckNameAt(parent, locale, text)\ if(JSONNODE * temp = json_at(parent, locale)){\ json_char * _res = json_name(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckNameAt: ") + #parent + "[" + #locale + "]");\ } CheckAt(casted, 0, JSON_TEXT("world")); CheckAt(casted, 1, JSON_TEXT("mars")); CheckAt(casted, 2, JSON_TEXT("france")); CheckNameAt(casted, 0, JSON_TEXT("")); CheckNameAt(casted, 1, JSON_TEXT("")); CheckNameAt(casted, 2, JSON_TEXT("")); CheckAt(test, 0, JSON_TEXT("world")); CheckAt(test, 1, JSON_TEXT("mars")); CheckAt(test, 2, JSON_TEXT("france")); CheckNameAt(test, 0, JSON_TEXT("hi")); CheckNameAt(test, 1, JSON_TEXT("hello")); CheckNameAt(test, 2, JSON_TEXT("salut")); #define CheckGet(parent, locale, text)\ if(JSONNODE * temp = json_get(parent, locale)){\ json_char * _res = json_as_string(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckGet: ") + #parent + "[" + #locale + "]");\ } #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS #define CheckGetNoCase(parent, locale, text)\ if(JSONNODE * temp = json_get_nocase(parent, locale)){\ json_char * _res = json_as_string(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckGetNoCase: ") + #parent + "[" + #locale + "]");\ } #else #define CheckGetNoCase(parent, locale, text) #endif CheckGet(test, JSON_TEXT("hi"), JSON_TEXT("world")); CheckGetNoCase(test, JSON_TEXT("HI"), JSON_TEXT("world")); CheckGet(test, JSON_TEXT("hello"), JSON_TEXT("mars")); CheckGetNoCase(test, JSON_TEXT("HELLO"), JSON_TEXT("mars")); CheckGet(test, JSON_TEXT("salut"), JSON_TEXT("france")); CheckGetNoCase(test, JSON_TEXT("SALUT"), JSON_TEXT("france")); assertNull(json_get(test, JSON_TEXT("meh"))); #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS assertNull(json_get_nocase(test, JSON_TEXT("meh"))); #endif #endif #ifdef JSON_ITERATORS #ifdef JSON_CASTABLE UnitTest::SetPrefix("TestInspectors.cpp - Iterators"); for(JSONNODE_ITERATOR it = json_begin(casted), end = json_end(casted); it != end; ++it){ json_char * _res = json_name(*it); assertCStringSame(_res, JSON_TEXT("")); json_free(_res); } #endif #endif #ifdef JSON_BINARY UnitTest::SetPrefix("TestInspectors.cpp - Binary"); json_set_binary(test, (const unsigned char *)"Hello World", 11); assertEquals(json_type(test), JSON_STRING); json_char * _res = json_as_string(test); assertCStringSame(_res, JSON_TEXT("SGVsbG8gV29ybGQ=")); json_free(_res); unsigned long i; if(char * bin = (char*)json_as_binary(test, &i)){ assertEquals(i, 11); char * terminated = (char*)std::memcpy(std::malloc(i + 1), bin, i); terminated[i] = '\0'; assertCStringEquals(terminated, "Hello World"); json_free(bin); std::free(terminated); } else { FAIL("as_binary failed"); } json_set_a(test, JSON_TEXT("Hello World")); assertEquals(json_type(test), JSON_STRING); _res = json_as_string(test); assertCStringSame(_res, JSON_TEXT("Hello World")); json_free(_res); #ifdef JSON_SAFE assertEquals(json_as_binary(test, &i), 0); assertEquals(i, 0); #endif #endif json_delete(test); #ifdef JSON_CASTABLE json_delete(casted); #endif #else JSONNode test = JSONNode(JSON_NULL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("")); assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); assertEquals(test.as_bool(), false); #endif test = 15.5f; assertEquals(test.type(), JSON_NUMBER); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("15.5")); #endif assertEquals(test.as_int(), 15); assertEquals(test.as_float(), 15.5f); #ifdef JSON_CASTABLE assertEquals(test.as_bool(), true); #endif test = 0.0f; assertEquals(test.type(), JSON_NUMBER); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("0")); #endif assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); #ifdef JSON_CASTABLE assertEquals(test.as_bool(), false); #endif test = true; assertEquals(test.type(), JSON_BOOL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("true")); assertEquals(test.as_int(), 1); assertEquals(test.as_float(), 1.0f); #endif assertEquals(test.as_bool(), true); test = false; assertEquals(test.type(), JSON_BOOL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("false")); assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); #endif assertEquals(test.as_bool(), false); #ifdef JSON_CASTABLE test.cast(JSON_NODE); #else test = JSONNode(JSON_NODE); #endif assertEquals(test.type(), JSON_NODE); assertEquals(test.size(), 0); test.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world"))); test.push_back(JSONNode(JSON_TEXT("hello"), JSON_TEXT("mars"))); test.push_back(JSONNode(JSON_TEXT("salut"), JSON_TEXT("france"))); assertEquals(test.size(), 3); TestSuite::testParsingItself(test); #ifdef JSON_CASTABLE JSONNode casted = test.as_array(); #ifdef JSON_UNIT_TEST assertNotEquals(casted.internal, test.internal); #endif assertEquals(casted.type(), JSON_ARRAY); assertEquals(test.type(), JSON_NODE); assertEquals(test.size(), 3); assertEquals(casted.size(), 3); TestSuite::testParsingItself(casted); #endif UnitTest::SetPrefix("TestInspectors.cpp - Location"); try { #ifdef JSON_CASTABLE assertEquals(casted.at(0), JSON_TEXT("world")); assertEquals(casted.at(1), JSON_TEXT("mars")); assertEquals(casted.at(2), JSON_TEXT("france")); assertEquals(casted.at(0).name(), JSON_TEXT("")); assertEquals(casted.at(1).name(), JSON_TEXT("")); assertEquals(casted.at(2).name(), JSON_TEXT("")); #endif assertEquals(test.at(0), JSON_TEXT("world")); assertEquals(test.at(1), JSON_TEXT("mars")); assertEquals(test.at(2), JSON_TEXT("france")); assertEquals(test.at(0).name(), JSON_TEXT("hi")); assertEquals(test.at(1).name(), JSON_TEXT("hello")); assertEquals(test.at(2).name(), JSON_TEXT("salut")); } catch (std::out_of_range){ FAIL("exception caught"); } try { assertEquals(test.at(JSON_TEXT("hi")), JSON_TEXT("world")); assertEquals(test.at(JSON_TEXT("hello")), JSON_TEXT("mars")); assertEquals(test.at(JSON_TEXT("salut")), JSON_TEXT("france")); #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS assertEquals(test.at_nocase(JSON_TEXT("SALUT")), JSON_TEXT("france")); assertEquals(test.at_nocase(JSON_TEXT("HELLO")), JSON_TEXT("mars")); assertEquals(test.at_nocase(JSON_TEXT("HI")), JSON_TEXT("world")); #endif } catch (std::out_of_range){ FAIL("exception caught"); } assertException(test.at(JSON_TEXT("meh")), std::out_of_range); #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS assertException(test.at_nocase(JSON_TEXT("meh")), std::out_of_range); #endif assertEquals(test[JSON_TEXT("hi")], json_string(JSON_TEXT("world"))); assertEquals(test[JSON_TEXT("hello")], json_string(JSON_TEXT("mars"))); assertEquals(test[JSON_TEXT("salut")], json_string(JSON_TEXT("france"))); assertEquals(test[0], JSON_TEXT("world")); assertEquals(test[1], JSON_TEXT("mars")); assertEquals(test[2], JSON_TEXT("france")); #ifdef JSON_ITERATORS #ifdef JSON_CASTABLE UnitTest::SetPrefix("TestInspectors.cpp - Iterators"); for(JSONNode::iterator it = casted.begin(), end = casted.end(); it != end; ++it){ assertEquals((*it).name(), JSON_TEXT("")); } #endif #endif #ifdef JSON_BINARY UnitTest::SetPrefix("TestInspectors.cpp - Binary"); test.set_binary((const unsigned char *)"Hello World", 11); assertEquals(test.type(), JSON_STRING); assertEquals(test.as_string(), JSON_TEXT("SGVsbG8gV29ybGQ=")); assertEquals(test.as_binary(), "Hello World"); assertEquals(test.as_binary().size(), 11); test = JSON_TEXT("Hello World"); assertEquals(test.type(), JSON_STRING); assertEquals(test.as_string(), JSON_TEXT("Hello World")); #ifdef JSON_SAFE assertEquals(test.as_binary(), ""); #endif #endif #ifdef JSON_READ_PRIORITY //This is a regression test for a bug in at() json_string buffer(JSON_TEXT("{ \"myValue1\" : \"foo\", \"myValue2\" : \"bar\"}")); JSONNode current = libjson::parse(buffer); try { JSONNode & value1 = current[JSON_TEXT("myValue1")]; assertEquals(value1.as_string(), JSON_TEXT("foo")); JSONNode & value2 = current[JSON_TEXT("myValue2")]; assertEquals(value2.as_string(), JSON_TEXT("bar")); } catch (...){ assertTrue(false); } #endif #endif }
void TestSuite::TestWriter(void){ UnitTest::SetPrefix("Writing"); #ifdef JSON_LIBRARY #define assertWrite(node, func, expected)\ {\ json_char * _temp = func(node);\ assertCStringSame(_temp, expected);\ json_free(_temp);\ } JSONNODE * test1 = json_new(JSON_NODE); assertWrite(test1, json_write, JSON_TEXT("{}")); json_push_back(test1, json_new_a(JSON_TEXT("Hello"), JSON_TEXT("World"))); json_push_back(test1, json_new_b(JSON_TEXT("libjson"), true)); assertWrite(test1, json_write, JSON_TEXT("{\"Hello\":\"World\",\"libjson\":true}")); #ifdef JSON_NEWLINE assertEquals(JSON_NEWLINE, JSON_TEXT("\r\n")); #ifdef JSON_INDENT assertEquals(JSON_INDENT, JSON_TEXT(" ")) assertWrite(test1, json_write_formatted, JSON_TEXT("{\r\n \"Hello\" : \"World\",\r\n \"libjson\" : true\r\n}")); #else assertWrite(test1, json_write_formatted, JSON_TEXT("{\r\n\t\"Hello\" : \"World\",\r\n\t\"libjson\" : true\r\n}")); #endif #else #ifdef JSON_INDENT assertEquals(JSON_INDENT, JSON_TEXT(" ")) assertWrite(test1, json_write_formatted, JSON_TEXT("{\n \"Hello\" : \"World\",\n \"libjson\" : true\n}")); #else assertWrite(test1, json_write_formatted, JSON_TEXT("{\n\t\"Hello\" : \"World\",\n\t\"libjson\" : true\n}")); #endif #endif json_delete(test1); JSONNODE * test2 = json_new(JSON_ARRAY); assertWrite(test2, json_write, JSON_TEXT("[]")); json_delete(test2); JSONNODE * card = json_new(JSON_ARRAY); JSONNODE *c = json_new(JSON_ARRAY); json_push_back(c, json_new_a(JSON_TEXT("name"), JSON_TEXT("Entrée Audio Intégrée 1"))); json_push_back(c, json_new_i(NULL, 0)); json_push_back(card, c); #ifdef JSON_UNICODE assertWrite(card, json_write, JSON_TEXT("[[\"Entr\\u00E9e Audio Int\\u00E9gr\\u00E9e 1\",0]]")) JSONNODE * ass = json_parse(JSON_TEXT("[[\"Entr\\u00E9e Audio Int\\u00E9gr\\u00E9e 1\",0]]")); JSONNODE * item = json_at(json_at(ass, 0), 0); assertWrite(item, json_as_string, JSON_TEXT("Entrée Audio Intégrée 1")); #else assertWrite(card, json_write, JSON_TEXT("[[\"Entr\\u00C3\\u00A9e Audio Int\\u00C3\\u00A9gr\\u00C3\\u00A9e 1\",0]]")) JSONNODE * ass = json_parse(JSON_TEXT("[[\"Entr\\u00C3\\u00A9e Audio Int\\u00C3\\u00A9gr\\u00C3\\u00A9e 1\",0]]")); JSONNODE * item = json_at(json_at(ass, 0), 0); assertWrite(item, json_as_string, JSON_TEXT("Entrée Audio Intégrée 1")); #endif json_delete(card); json_delete(ass); #ifdef JSON_COMMENTS JSONNODE * test3 = json_new(JSON_NODE); json_push_back(test3, json_new_a(JSON_TEXT("Hi"), JSON_TEXT("There"))); json_push_back(test3, json_new_a(JSON_TEXT("Hello"), JSON_TEXT("World"))); json_set_comment(json_at(test3, 0), JSON_TEXT("Testing stuff")); json_set_comment(json_at(test3, 1), JSON_TEXT("Multi\r\nLine\nUnix and Windows")); assertWrite(test3, json_write, JSON_TEXT("{\"Hi\":\"There\",\"Hello\":\"World\"}")); #if !defined( JSON_INDENT) && !defined(JSON_NEWLINE) #ifdef JSON_WRITE_BASH_COMMENTS assertWrite(test3, json_write_formatted, JSON_TEXT("{\n\t\n\t#Testing stuff\n\t\"Hi\" : \"There\",\n\t\n\t#Multi\n\t#Line\n\t#Unix and Windows\n\t\"Hello\" : \"World\"\n}")); #elif defined(JSON_WRITE_SINGLE_LINE_COMMENTS) assertWrite(test3, json_write_formatted, JSON_TEXT("{\n\t\n\t//Testing stuff\n\t\"Hi\" : \"There\",\n\t\n\t//Multi\n\t//Line\n\t//Unix and Windows\n\t\"Hello\" : \"World\"\n}")); #else assertWrite(test3, json_write_formatted, JSON_TEXT("{\n\t\n\t//Testing stuff\n\t\"Hi\" : \"There\",\n\t\n\t/*\n\t\tMulti\n\t\tLine\n\t\tUnix and Windows\n\t*/\n\t\"Hello\" : \"World\"\n}")); #endif #endif json_delete(test3); #endif #else JSONNode test1(JSON_NODE); assertEquals(test1.write(), JSON_TEXT("{}")); test1.push_back(JSONNode(JSON_TEXT("Hello"), JSON_TEXT("World"))); test1.push_back(JSONNode(JSON_TEXT("libjson"), true)); assertEquals(test1.write(), JSON_TEXT("{\"Hello\":\"World\",\"libjson\":true}")); #ifdef JSON_NEWLINE assertEquals(JSON_NEWLINE, JSON_TEXT("\r\n")); #ifdef JSON_INDENT assertEquals(JSON_INDENT, JSON_TEXT(" ")) assertEquals(test1.write_formatted(), JSON_TEXT("{\r\n \"Hello\" : \"World\",\r\n \"libjson\" : true\r\n}")); #else assertEquals(test1.write_formatted(), JSON_TEXT("{\r\n\t\"Hello\" : \"World\",\r\n\t\"libjson\" : true\r\n}")); #endif #else #ifdef JSON_INDENT assertEquals(JSON_INDENT, JSON_TEXT(" ")) assertEquals(test1.write_formatted(), JSON_TEXT("{\n \"Hello\" : \"World\",\n \"libjson\" : true\n}")); #else assertEquals(test1.write_formatted(), JSON_TEXT("{\n\t\"Hello\" : \"World\",\n\t\"libjson\" : true\n}")); #endif #endif JSONNode test2(JSON_ARRAY); assertEquals(test2.write(), JSON_TEXT("[]")); #ifdef JSON_COMMENTS JSONNode test3(JSON_NODE); test3.push_back(JSONNode(JSON_TEXT("Hi"), JSON_TEXT("There"))); test3.push_back(JSONNode(JSON_TEXT("Hello"), JSON_TEXT("World"))); test3[0].set_comment(JSON_TEXT("Testing stuff")); test3[1].set_comment(JSON_TEXT("Multi\r\nLine\nUnix and Windows")); assertEquals(test3.write(), JSON_TEXT("{\"Hi\":\"There\",\"Hello\":\"World\"}")); #if !defined( JSON_INDENT) && !defined(JSON_NEWLINE) #ifdef JSON_WRITE_BASH_COMMENTS assertEquals(test3.write_formatted(), JSON_TEXT("{\n\t\n\t#Testing stuff\n\t\"Hi\" : \"There\",\n\t\n\t#Multi\n\t#Line\n\t#Unix and Windows\n\t\"Hello\" : \"World\"\n}")); #elif defined(JSON_WRITE_SINGLE_LINE_COMMENTS) assertEquals(test3.write_formatted(), JSON_TEXT("{\n\t\n\t//Testing stuff\n\t\"Hi\" : \"There\",\n\t\n\t//Multi\n\t//Line\n\t//Unix and Windows\n\t\"Hello\" : \"World\"\n}")); #else assertEquals(test3.write_formatted(), JSON_TEXT("{\n\t\n\t//Testing stuff\n\t\"Hi\" : \"There\",\n\t\n\t/*\n\t\tMulti\n\t\tLine\n\t\tUnix and Windows\n\t*/\n\t\"Hello\" : \"World\"\n}")); #endif #endif #endif #endif }