Sleds/cppjson/testjson/testjson.cpp

509 lines
21 KiB
C++
Raw Normal View History

2025-03-13 21:28:38 +00:00
// testjson.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <omp.h>
#include "../jsonobject.h"
namespace
{
const char TEST_JSON_OBJ2[] = "{\"testObj1\":\"This \\n has \\n a \\n linefeed\"}";
const char TEST_JSON_NULL[] = "{\"testNull\":null, \"desc\":\"This object has a NULL!\"}";
const char TEST_JSON_OBJ[] = "{"
" \"collab\": ["
" {"
" \"_id\": \"urn:ionu:000000:7E62D46D::\","
" \"publicKey\": \"\","
" \"status\": \"active\","
" \"updated\": 1381533272020,"
" \"messageIdentifier\": \"\","
" \"enabled\": true,"
" \"cghk\": \"vwo/eEZ6u9CoVn1Wy8Gyz/Qb2YWluTU6YBL3Sv5GoAMKU+OyejhL7CENjXiMcuV1N+x/Oira4E1lCuH38F86Ow==\","
" \"phone\": {"
" \"primary\": \"\""
" },"
" \"saved\": 1381867316232,"
" \"k2\": \"offline\","
" \"version\": 376,"
" \"urn\": \"urn:ionu:000000:7E62D46D::\","
" \"cr\": ["
" \"favbook::\","
" \"flame::\","
" \"kiss::\","
" \"password::Xci+zrr5ZimK+55HRMo5wZhogbONABC6asqAQTmOTN0=\""
" ],"
" \"prefs\": {"
" \"msgTBR\": false,"
" \"shareFullname\": true,"
" \"searchEmail\": true,"
" \"receiveOptInNews\": false,"
" \"shareAvatar\": true,"
" \"searchPhone\": false,"
" \"msgTTL\": 0,"
" \"userType\": \"SNT_UNKNOWN\","
" \"searchUsername\": true,"
" \"shareEmail\": true,"
" \"shareUsername\": true,"
" \"msgRR\": false,"
" \"sharePhone\": false,"
" \"msgDR\": true"
" },"
" \"email\": {"
" \"primary\": \"viggy@ionu.com\""
" },"
" \"user\": \"viggy\","
" \"fullname\": \"Mike Vigorita\""
" },"
" {"
" \"_id\": \"urn:ionu:000000:00000006::\","
" \"publicKey\": \"\","
" \"status\": \"active\","
" \"updated\": 1381533278616,"
" \"messageIdentifier\": \"94b42a57-6f50-4630-91d9-85b81a83f5e3\","
" \"enabled\": true,"
" \"k2\": \"online\","
" \"urn\": \"urn:ionu:000000:00000006::\","
" \"prefs\": {"
" \"msgTBR\": false,"
" \"searchUsername\": true,"
" \"shareFullname\": true,"
" \"searchEmail\": true,"
" \"shareEmail\": true,"
" \"shareAvatar\": true,"
" \"shareUsername\": true,"
" \"msgRR\": false,"
" \"avatar\": \"\","
" \"msgTTL\": 0,"
" \"userType\": \"SNT_ROBOT\","
" \"msgDR\": false"
" },"
" \"email\": {"
" \"primary\": \"broadcastbot@ionu.com\""
" },"
" \"user\": \"broadcastbot\","
" \"fullname\": \"broadcastbot robot\""
" },"
" {"
" \"_id\": \"urn:ionu:000000:00000005::\","
" \"publicKey\": \"\","
" \"status\": \"active\","
" \"updated\": 1381533278762,"
" \"messageIdentifier\": \"cb07013f-fda4-4f9f-928e-ac26a12e9eb6\","
" \"enabled\": true,"
" \"k2\": \"online\","
" \"urn\": \"urn:ionu:000000:00000005::\","
" \"prefs\": {"
" \"msgTBR\": false,"
" \"shareFullname\": true,"
" \"searchUsername\": true,"
" \"shareEmail\": true,"
" \"searchEmail\": true,"
" \"shareUsername\": true,"
" \"shareAvatar\": true,"
" \"msgRR\": false,"
" \"avatar\": \"\","
" \"msgTTL\": 0,"
" \"msgDR\": false,"
" \"userType\": \"SNT_ROBOT\""
" },"
" \"email\": {"
" \"primary\": \"bugbot@ionu.com\""
" },"
" \"user\": \"bugbot\","
" \"fullname\": \"bugbot robot\""
" },"
" {"
" \"_id\": \"urn:ionu:000000:00000003::\","
" \"publicKey\": \"\","
" \"status\": \"active\","
" \"updated\": 1381533288848,"
" \"messageIdentifier\": \"48735e19-18d3-47c4-945b-7b7573cf313a\","
" \"enabled\": true,"
" \"k2\": \"online\","
" \"urn\": \"urn:ionu:000000:00000003::\","
" \"prefs\": {"
" \"msgTBR\": false,"
" \"shareFullname\": true,"
" \"searchUsername\": true,"
" \"shareEmail\": true,"
" \"searchEmail\": true,"
" \"shareUsername\": true,"
" \"shareAvatar\": true,"
" \"msgRR\": false,"
" \"avatar\": \"\","
" \"msgTTL\": 0,"
" \"msgDR\": false,"
" \"userType\": \"SNT_ROBOT\""
" },"
" \"email\": {"
" \"primary\": \"nikki@ionu.com\""
" },"
" \"user\": \"nikki\","
" \"fullname\": \"nikki robot\""
" }"
" ]"
"}";
const char TEST_JSON_ARRAY[] = "["
" {"
" \"chat\": \"Hi!\","
" \"filename\": \"C:/Users/viggy/AppData/Roaming/ionu/Messages/e640498d970d/cacae6c8-88ab-4959-ac02-c5f691259605.ionu\","
" \"metadata\": {"
" \"ver\": \"0.1\","
" \"magic\": \"IOnU Encrypted Archive\","
" \"platform\": \"Windows\","
" \"status\": \"modified\","
" \"urn\": \"urn:ionu:000000:7E62D46D:A7A6:CF3F7392FDCE\","
" \"modified_by\": \"urn:ionu:000000:7E62D46D:A7A6:\","
" \"modified_date\": \"2013-10-15T15:29:33.045Z\","
" \"cipher\": \"aes256cbc\","
" \"digest\": \"sha256\","
" \"tgisig\": \"e148a3c72ccf\","
" \"contents\": ["
" {"
" \"metatype\": \"msg\","
" \"mimetype\": \"text/plain\","
" \"size\": 4,"
" \"time\": \"2013-10-15T15:29:33Z\""
" }"
" ],"
" \"seq\": 1,"
" \"fnum\": 1,"
" \"utime\": \"2013-10-15T15:29:19.959Z\""
" }"
" },"
" {"
" \"chat\": \"I'm not going to buy my kids an encyclopedia. Let them walk to school like I did.\","
" \"filename\": \"C:/Users/viggy/AppData/Roaming/ionu/Messages/e640498d970d/74693e01-402c-4e06-8c77-2e8cf93005ea.ionu\","
" \"metadata\": {"
" \"utime\": \"2013-10-15T15:29:20.783Z\","
" \"platform\": \"Linux\","
" \"perms\": {"
" \"stream\": [],"
" \"doorway\": [],"
" \"writeAssign\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"streamAssign\": [],"
" \"admin\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"write\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"owner\": \"00000003\","
" \"read\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"readAssign\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"adminAssign\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"doorwayAssign\": []"
" },"
" \"contents\": ["
" {"
" \"mimetype\": \"text/plain\","
" \"time\": \"2013-10-15T15:29:20Z\","
" \"metatype\": \"msg\","
" \"size\": 81"
" },"
" {"
" \"mimetype\": \"text/plain\","
" \"time\": \"2013-10-15T15:29:20Z\","
" \"metatype\": \"msg\","
" \"size\": 81"
" }"
" ],"
" \"modified_date\": \"2013-10-15T15:29:20.004Z\","
" \"status\": \"modified\","
" \"digest\": \"sha256\","
" \"ver\": \"0.1\","
" \"drr\": true,"
" \"tgisig\": \"6ddcd374470a\","
" \"fnum\": 2,"
" \"urn\": \"urn:ionu:000000:00000003:0001:5F2CFA37D4A5\","
" \"magic\": \"IOnU Encrypted Archive\","
" \"modified_by\": \"urn:ionu:000000:00000003:0001:\","
" \"seq\": 1,"
" \"rr\": [],"
" \"cipher\": \"aes256cbc\","
" \"tbr\": false,"
" \"dr\": ["
" \"urn:ionu:000000:7E62D46D::\""
" ],"
" \"rrr\": false"
" }"
" },"
" {"
" \"chat\": \"Okay\","
" \"filename\": \"C:/Users/viggy/AppData/Roaming/ionu/Messages/e640498d970d/ba4f8e77-8644-4b07-901b-03b86e6c4adf.ionu\","
" \"metadata\": {"
" \"ver\": \"0.1\","
" \"magic\": \"IOnU Encrypted Archive\","
" \"platform\": \"Windows\","
" \"status\": \"modified\","
" \"urn\": \"urn:ionu:000000:7E62D46D:A7A6:4B024B43ED6B\","
" \"modified_by\": \"urn:ionu:000000:7E62D46D:A7A6:\","
" \"modified_date\": \"2013-10-15T15:30:16.062Z\","
" \"cipher\": \"aes256cbc\","
" \"digest\": \"sha256\","
" \"tgisig\": \"e148a3c72ccf\","
" \"contents\": ["
" {"
" \"metatype\": \"msg\","
" \"mimetype\": \"text/plain\","
" \"size\": 5,"
" \"time\": \"2013-10-15T15:30:16Z\""
" }"
" ],"
" \"seq\": 1,"
" \"fnum\": 3,"
" \"utime\": \"2013-10-15T15:30:02.597Z\""
" }"
" },"
" {"
" \"chat\": \"You can observe a lot just by watching.\","
" \"filename\": \"C:/Users/viggy/AppData/Roaming/ionu/Messages/e640498d970d/c9415533-a811-4602-bd3b-a65c7b4e7e47.ionu\","
" \"metadata\": {"
" \"utime\": \"2013-10-15T15:30:03.367Z\","
" \"platform\": \"Linux\","
" \"perms\": {"
" \"stream\": [],"
" \"doorway\": [],"
" \"writeAssign\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"streamAssign\": [],"
" \"admin\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"write\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"owner\": \"00000003\","
" \"read\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"readAssign\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"adminAssign\": ["
" \"00000003\","
" \"7E62D46D\","
" \"00000003\","
" \"7E62D46D\""
" ],"
" \"doorwayAssign\": []"
" },"
" \"contents\": ["
" {"
" \"mimetype\": \"text/plain\","
" \"time\": \"2013-10-15T15:30:03Z\","
" \"metatype\": \"msg\","
" \"size\": 39"
" },"
" {"
" \"mimetype\": \"text/plain\","
" \"time\": \"2013-10-15T15:30:03Z\","
" \"metatype\": \"msg\","
" \"size\": 39"
" }"
" ],"
" \"modified_date\": \"2013-10-15T15:30:03.031Z\","
" \"status\": \"modified\","
" \"digest\": \"sha256\","
" \"ver\": \"0.1\","
" \"drr\": true,"
" \"tgisig\": \"6ddcd374470a\","
" \"fnum\": 4,"
" \"urn\": \"urn:ionu:000000:00000003:0001:FED9948DB130\","
" \"magic\": \"IOnU Encrypted Archive\","
" \"modified_by\": \"urn:ionu:000000:00000003:0001:\","
" \"seq\": 1,"
" \"rr\": [],"
" \"cipher\": \"aes256cbc\","
" \"tbr\": false,"
" \"dr\": ["
" \"urn:ionu:000000:7E62D46D::\""
" ],"
" \"rrr\": false"
" }"
" }"
"]";
}
int main(int argc, char* argv[])
{
if (argc >= 2)
{
sequencelogic::JSONObject tmpObj;
tmpObj.LoadFromFile(argv[1]);
std::cout << "Parsed:\n" << tmpObj << std::endl;
}
//omp_set_num_threads(30);
//#pragma omp parallel
{
//#pragma omp for
//for (int i = 0; i < 50; ++i)
{
sequencelogic::JSONObject tempObj (TEST_JSON_OBJ2);
std:: cout << "[INFO] Object with linefeeds:\n" << tempObj << std::endl;
sequencelogic::JSONObject emptyObj;
if (!emptyObj.empty())
std::cout << "[ERROR] Empty object is not empty.\n";
else
std::cout << "[INFO] Empty object is empty.\n";
emptyObj.setJSONValue("val", "no longer empty");
if (emptyObj.empty())
std::cout << "[ERROR] No longer empty object is empty.\n";
else
std::cout << "[INFO] Empty object is no longer empty: " << emptyObj << "\n";
// Simple JSON test...
sequencelogic::JSONObject tmpObject (TEST_JSON_OBJ);
//sequencelogic::JSONObject tmpObject ("{\"test1\":42}");
std::cout << "[INFO] Created JSON object:\n" << tmpObject << "\n";
// Copy it!
sequencelogic::JSONObjectPtr pNewObject(new sequencelogic::JSONObject(tmpObject));
std::cout << "[INFO] Copied JSON object:\n" << *pNewObject << "\n";
// Get the array...
sequencelogic::JSONArray &collabArray = pNewObject->getJSONArray("collab");
std::cout << "[INFO] Got JSON array:\n";
for (int i = 0; i < collabArray.getnumelements(); ++i)
{
std::cout << "[INFO] Array member:\n" << reinterpret_cast<sequencelogic::JSONObject &>(*collabArray.get(i)) << "\n";
}
sequencelogic::JSONObject tmpObjArray(TEST_JSON_ARRAY);
sequencelogic::JSONArray &jsonArray = reinterpret_cast<sequencelogic::JSONArray &>(tmpObjArray);
std::cout << "[INFO] Created JSON array:\n" << jsonArray << "\n";
sequencelogic::JSONArray newArray(jsonArray);
std::cout << "[INFO] Copied JSON array:\n" << newArray << "\n";
std::cout << "[INFO] JSON array has:\n";
for (int i = 0; i < newArray.getnumelements(); ++i)
{
sequencelogic::JSONObject &msg = reinterpret_cast<sequencelogic::JSONObject&>(*newArray.get(i));
sequencelogic::JSONObject tmpObj(msg);
std::cout << "[INFO] Array member:\n" << msg << "\n";
#ifdef WIN32
std::cout << "[INFO] metadata.platform: " << msg.getJSONStringPath("metadata.platform") << "\n";
msg.setJSONValuePath("metadata.platform", std::string("Linux"));
std::cout << "[INFO] metadata.platform is now: " << msg.getJSONStringPath("metadata.platform") << "\n";
#else
std::cout << "[INFO] metadata.platform: " << msg.getJSONObject("metadata").getJSONString("platform") << "\n";
msg.getJSONObject("metadata").setJSONValue("platform", "Linux");
std::cout << "[INFO] metadata.platform is now: " << msg.getJSONObject("metadata").getJSONString("platform") << "\n";
#endif
tmpObj.setJSONValue("testing", "Viggy");
msg = tmpObj;
std::cout << "[INFO] Testing value is: " << msg.getJSONString("testing") << std::endl;
}
#ifdef WIN32
std::cout << "[INFO] Testing array path access: \n";
sequencelogic::JSONObject &tmpMsg = reinterpret_cast<sequencelogic::JSONObject&>(*newArray.get(1));
std::cout << "[INFO] " << tmpMsg.getJSONStringPath("metadata.perms.writeAssign[1]") << std::endl;
tmpMsg.setJSONValuePath("metadata.perms.writeAssign[1]", std::string("newVal"));
std::cout << "[INFO] " << tmpMsg.getJSONStringPath("metadata.perms.writeAssign[1]") << std::endl;
std::cout << "[INFO] Testing array set...\n";
tmpMsg.setJSONValuePath("testKey.testArray[1].someKey", std::string("Viggy!"));
std::cout << "[INFO] " << tmpMsg.getJSONStringPath("testKey.testArray[1].someKey") << std::endl;
#endif
sequencelogic::JSONObject parentObj;
{
parentObj.setJSONValue("foo", "bar");
sequencelogic::JSONObject childObject;
childObject.setJSONValue("theAnswer", 42);
childObject.setJSONArray("TestArray");
sequencelogic::JSONArray &tmpArray = childObject.getJSONArray("TestArray");
tmpArray.addElement("one");
tmpArray.addElement("two");
tmpArray.addElement("three");
parentObj.setJSONValue("child", childObject);
}
std::cout << "[INFO] Parent is:\n" << parentObj << std::endl;
parentObj.erase("child");
std::cout << "[INFO] Parent is now:\n" << parentObj << std::endl;
sequencelogic::JSONArray newArray2;
newArray2.addElement("ONE");
newArray2.addElement("TWO");
newArray2.addElement("THREE");
sequencelogic::JSONObject newChild;
newChild.setJSONValue("newChild", newArray2);
parentObj.mergeWith(newChild);
std::cout << "[INFO] Parent is NOW:\n" << parentObj << std::endl;
sequencelogic::JSONArray emptyArray;
std::cout << "[INFO] Empty array is: " << emptyArray << std::endl;
sequencelogic::JSONObject emptyObj2;
std::cout << "[INFO] Empty obj is: " << emptyObj2 << std::endl;
sequencelogic::JSONObject quoteObj;
std::string testStr = "This string has \"quotes\"!\n";
quoteObj.setJSONValue(std::string("newKey"), testStr.c_str());
std::cout << "[INFO] Quoted object:\n" << quoteObj << std::endl;
sequencelogic::JSONObject specCharObj;
testStr = "Oh stick \xe\x8\x9em up";
specCharObj.setJSONValue("somethingNikkiSays", testStr.c_str());
std::cout << "[INFO] Special object:\n" << specCharObj << std::endl;
sequencelogic::JSONObject nullEntry(TEST_JSON_NULL);
std::cout << "[INFO] This is the NULL entry object:\n" << nullEntry << std::endl;
}
}
return 0;
}