/** * Builds Native Libjson Node tree recusively * \param pANode A SettingsAPI's Node. * \return libjson's equivalent to SettingNode including children. * If parentAbstractNode is null, then empty native node is returned */ JSONNODE* buildNativeJSONTreeNode(SNI* pANode) { if (pANode == NULL) { return json_new(identifyNodeType(SNI::TYPE_OBJECT)); } JSONNODE* pNNode = json_new(identifyNodeType(pANode->getType())); json_set_name(pNNode, pANode->getKey().c_str()); switch (pANode->getType()) { case SNI::TYPE_VALUE: { json_set_a(pNNode, pANode->readString().c_str()); break; } case SNI::TYPE_OBJECT: case SNI::TYPE_ARRAY: { std::vector<SNI*> children = pANode->getChildren(); std::vector<SNI*>::iterator it = children.begin(); while (it != children.end()) { SNI* cANode = *it; JSONNODE* cNChild = buildNativeJSONTreeNode(cANode); json_insert(pNNode, json_end(pNNode), cNChild); ++it; } break; } default: // Node is undefined type. break; } return pNNode; }
/* Build an object from the text. */ static const char* parse_object (json *item, const char* value) { json *child; #if SPINE_JSON_DEBUG /* unnecessary, only callsite (parse_value) verifies this */ if (*value != '{') { ep = value; return 0; } /* not an object! */ #endif item->type = json_Object; value = skip(value + 1); if (*value == '}') return value + 1; /* empty array. */ item->child = child = json_new(); if (!item->child) return 0; value = skip(parse_string(child, skip(value))); if (!value) return 0; child->name = child->valueString; child->valueString = 0; if (*value != ':') { ep = value; return 0; } /* fail! */ value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */ if (!value) return 0; item->size = 1; while (*value == ',') { json *new_item = json_new(); if (!new_item) return 0; /* memory fail */ child->next = new_item; #if SPINE_JSON_HAVE_PREV new_item->prev = child; #endif child = new_item; value = skip(parse_string(child, skip(value + 1))); if (!value) return 0; child->name = child->valueString; child->valueString = 0; if (*value != ':') { ep = value; return 0; } /* fail! */ value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */ if (!value) return 0; item->size++; } if (*value == '}') return value + 1; /* end of array */ ep = value; return 0; /* malformed. */ }
JSONNODE* tsload_get_dispatchers(void) { JSONNODE* disps = json_new(JSON_ARRAY); json_push_back(disps, json_new_a("simple", "simple")); return disps; }
/** * Gets the current CPM reading as * { "cpm": { * "value": val, // Actual value * "valid": boolean, // Valid flag * "raw": val, // Uncompensated value * "cpm30": val // 30 second window * } * } * * See Geiger.cpp for a more in-depth explanation of * raw and compensated CPM values. */ void cmd_cpm(char *line) { JSONNODE *n = json_new(JSON_NODE); JSONNODE *reading = json_new(JSON_NODE); json_set_name(reading, "cpm"); json_push_back(reading, json_new_f("value", system_geiger->get_cpm_deadtime_compensated())); json_push_back(reading, json_new_b("valid", system_geiger->is_cpm_valid())); json_push_back(reading, json_new_f("raw", system_geiger->get_cpm())); json_push_back(reading, json_new_f("cpm30", system_geiger->get_cpm30())); json_push_back(n, reading); json_char *jc = json_write_formatted(n); serial_write_string(jc); json_free(jc); json_delete(n); }
void JSON_Append_array(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; C_TEXT node; C_TEXT returnValue; json.fromParamAtIndex(pParams, 1); node.fromParamAtIndex(pParams, 2); JSONNODE *n = _fromHex(json); if(n){ std::wstring nodeName; _copyString(node, nodeName); JSONNODE *_node = json_new(JSON_NODE); json_set_name(_node, nodeName.c_str()); json_cast(_node, JSON_ARRAY); json_push_back(n, _node); _toHex(_node, returnValue); } returnValue.setReturn(pResult); }
void JSON_Append_bool_array(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; C_TEXT node; ARRAY_BOOLEAN 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) { json_push_back(_node, json_new_b(L"", values.getBooleanValueAtIndex(i))); } json_push_back(n, _node); _toHex(_node, returnValue); } returnValue.setReturn(pResult); }
u_char * dbrelay_exec_query(dbrelay_connection_t *conn, char *database, char *sql, unsigned long flags) { json_t *json = json_new(); u_char *ret; if (flags & DBRELAY_FLAG_PP) json_pretty_print(json, 1); if (flags & DBRELAY_FLAG_EMBEDCSV) json_set_mode(json, DBRELAY_JSON_MODE_CSV); api->change_db(conn->db, database); if (flags & DBRELAY_FLAG_XACT) api->exec(conn->db, api->catalogsql(DBRELAY_DBCMD_BEGIN, NULL)); if (api->exec(conn->db, sql)) { dbrelay_db_fill_data(json, conn); if (flags & DBRELAY_FLAG_XACT) api->exec(conn->db, api->catalogsql(DBRELAY_DBCMD_COMMIT, NULL)); } else { if (flags & DBRELAY_FLAG_XACT) api->exec(conn->db, api->catalogsql(DBRELAY_DBCMD_ROLLBACK, NULL)); return NULL; } ret = (u_char *) json_to_string(json); json_free(json); return ret; }
void Json::Reset() { json_free(mJsonStr); json_delete(mpRootNode); mpRootNode = json_new(JSON_NODE); mJsonStr = NULL; }
bool admJson::addNode(const char *nodeName) { JSONNODE *n = json_new(JSON_NODE); cookies.push_back((void *)n); json_set_name(n,nodeName); cookie=(void *)n; return true; }
/** * 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); }
/** * Gets current time on the device */ void cmd_getrtc() { JSONNODE *n = json_new(JSON_NODE); json_push_back(n, json_new_i("rtc", realtime_get_unixtime())); json_char *jc = json_write_formatted(n); serial_write_string(jc); json_free(jc); json_delete(n); }
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; }
/** * 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); }
static JSONNODE* json_wlparam_strset_format(wlp_descr_t* wlp) { JSONNODE* node = json_new(JSON_ARRAY); JSONNODE* el; int i; char* str; json_set_name(node, "strset"); for(i = 0; i < wlp->range.ss_num; ++i) { str = wlp->range.ss_strings[i]; el = json_new(JSON_STRING); json_set_a(el, str); json_push_back(node, el); } return node; }
int main (void) { json_value_t *obj = json_new(JSON_OBJECT, "[object]"); json_value_t *name = json_new(JSON_STRING, "joseph"); assert(obj); ok("json_new"); char *src = json_stringify(obj); ok("json_stringify"); assert(src); json_destroy(obj); assert(0 == obj->size); ok("json_destroy"); obj = NULL; ok_done(); return 0; }
void dbrelay_db_restart_json(dbrelay_request_t *request, json_t **json) { if (IS_SET(request->js_error)) { // free json handle and start over json_free(*json); *json = json_new(); json_add_callback(*json, request->js_error); json_new_object(*json); dbrelay_append_request_json(*json, request); } }
/** * Displays status of log area: records used, total records * and logging interval (in seconds) */ void cmd_logstatus(char *line) { JSONNODE *n = json_new(JSON_NODE); JSONNODE *n2 = json_new(JSON_NODE); json_set_name(n2, "logstatus"); json_push_back(n2, json_new_i("used", flashstorage_log_currentrecords())); json_push_back(n2, json_new_i("total", flashstorage_log_maxrecords())); const char *sloginter = flashstorage_keyval_get("LOGINTERVAL"); uint32_t c = 0; if(sloginter != 0) { sscanf(sloginter, "%"PRIu32"", &c); } else { c = 30 * 60; } json_push_back(n2, json_new_i("interval", c)); json_push_back(n, n2); json_char *jc = json_write_formatted(n); serial_write_string(jc); json_free(jc); json_delete(n); }
void JSON_New(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT returnValue; JSONNODE *n = json_new(JSON_NODE); _addJsonRootToList(n); _toHex(n, returnValue); returnValue.setReturn(pResult); }
/** * 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::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); }
/* Parse an object - create a new root, and populate. */ json *json_create (const char* value) { json *c; ep = 0; if (!value) return 0; /* only place we check for NULL other than skip() */ c = json_new(); if (!c) return 0; /* memory fail */ value = parse_value(c, skip(value)); if (!value) { json_dispose(c); return 0; } /* parse failure. ep is set. */ return c; }
JSONNODE* json_wlparam_format_all(wlp_descr_t* wlp) { JSONNODE* node = json_new(JSON_NODE); JSONNODE* wlp_node = NULL; json_set_name(node, "params"); while(wlp->type != WLP_NULL) { wlp_node = json_wlparam_format(wlp); json_push_back(node, wlp_node); wlp++; } return node; }
int main() { int i = 0; int rc; while (i++ < 1) { json_t *jso = json_new(); json_add_string(jso, "string1", "string1_val"); json_add_string(jso, "string2", "string2_val"); json_add_int(jso, "integer1", 12345); printf("%s\n", json_get_string(jso, "string1")); printf("%s\n%d\n", json_get_string(jso, "string2"), json_get_int(jso, "integer1")); json_padd_string(jso, "sub-object/sub-string", "sub-string-val"); json_padd_string(jso, "object1/sub-object/sub-string", "sub-string-val"); char *path = "object1/sub-object1/sub-sub-object1//sub-sub-string1"; json_padd_string(jso, path, "sub-sub-string1-val "); printf("sub-sub-string1=[%s]\n", json_pget_string(jso, path)); json_add_string(jso, "string3", "string3_val"); json_padd_string(jso, "string4", "string4_val"); // LOG("\n%s", json_to_string(jso)); array_t *arr = json_new_array( jso, "array0" ); rc = json_array_add_string( arr, 0, "array-item", "array-item0-val" ); rc = json_array_add_int( arr, 0, "array-item-int", 1234 ); rc = json_array_add_string( arr, 1, "array-item", "array-item1-val" ); rc = json_array_add_int( arr, 1, "array-item-int", 2234 ); rc = json_array_add_string( arr, 2, "array-item", "array-item2-val" ); rc = json_array_add_int( arr, 2, "array-item-int", 3234 ); /* LOG( "rc = [%d][%s][%s][%s][%d]", rc, json_array_get_string( arr, 0, "array-item"), json_array_get_string( arr, 1, "array-item" ), json_array_get_string( arr, 2, "array-item" ), json_array_get_int( arr, 2, "array-item-int") ); */ //printf( "====[%d]===[%s]-=-----\n", json_array_getlen(arr),json_array_get_string( arr, 0, "array-item" ) ); json_t *tmp1 = json_array_get_item( arr, 1 ); //printf( "-----------\n%s\n--------\n", json_to_string(tmp1) ); //printf("\n%s\n", json_to_string(jso)); LOG( "\n%s\n", json_to_string(jso) ); json_free(&jso); } //system("pause"); }
void JSON_Append_node(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; C_TEXT node; C_TEXT nodeText; C_TEXT returnValue; json.fromParamAtIndex(pParams, 1); node.fromParamAtIndex(pParams, 2); nodeText.fromParamAtIndex(pParams, 3); JSONNODE *n = _fromHex(json); if(n){ std::wstring nodeName; _copyString(node, nodeName); JSONNODE *_node; if(!nodeText.getUTF16Length()){ _node = json_new(JSON_NODE); }else{ std::wstring w; _copyString(nodeText, w); _node = json_parse(w.c_str()); } if(_node){ json_set_name(_node, nodeName.c_str()); json_push_back(n, _node); } _toHex(_node, returnValue); } returnValue.setReturn(pResult); }
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); }
u_char *dbrelay_db_status(dbrelay_request_t *request) { dbrelay_connection_t *connections; dbrelay_connection_t *conn; json_t *json = json_new(); int i; char tmpstr[100]; u_char *json_output; struct tm *ts; json_new_object(json); json_add_key(json, "status"); json_new_object(json); json_add_key(json, "info"); json_new_object(json); json_add_string(json, "build", DBRELAY_BUILD); sprintf(tmpstr, "0x%08x", dbrelay_get_ipc_key()); json_add_string(json, "ipckey", tmpstr); json_end_object(json); json_add_key(json, "connections"); json_new_array(json); connections = dbrelay_time_get_shmem(request); for (i=0; i<DBRELAY_MAX_CONN; i++) { conn = &connections[i]; if (connections[i].pid!=0) { json_new_object(json); sprintf(tmpstr, "%u", conn->slot); json_add_number(json, "slot", tmpstr); sprintf(tmpstr, "%u", conn->pid); json_add_number(json, "pid", tmpstr); json_add_string(json, "name", conn->connection_name ? conn->connection_name : ""); ts = localtime(&conn->tm_create); strftime(tmpstr, sizeof(tmpstr), "%Y-%m-%d %H:%M:%S", ts); json_add_string(json, "tm_created", tmpstr); ts = localtime(&conn->tm_accessed); strftime(tmpstr, sizeof(tmpstr), "%Y-%m-%d %H:%M:%S", ts); json_add_string(json, "tm_accessed", tmpstr); json_add_string(json, "sql_server", conn->sql_server ? conn->sql_server : ""); json_add_string(json, "sql_port", conn->sql_port ? conn->sql_port : ""); json_add_string(json, "sql_database", conn->sql_database ? conn->sql_database : ""); json_add_string(json, "sql_user", conn->sql_user ? conn->sql_user : ""); sprintf(tmpstr, "%ld", conn->connection_timeout); json_add_number(json, "connection_timeout", tmpstr); sprintf(tmpstr, "%u", conn->in_use); json_add_number(json, "in_use", tmpstr); json_add_string(json, "sock_path", conn->sock_path); sprintf(tmpstr, "%u", conn->helper_pid); json_add_number(json, "helper_pid", tmpstr); json_end_object(json); } } dbrelay_time_release_shmem(request, connections); json_end_array(json); json_end_object(json); json_end_object(json); json_output = (u_char *) json_to_string(json); json_free(json); return json_output; }
Json::Json() { mpRootNode = json_new(JSON_NODE); mJsonStr = NULL; }
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 }
void TestSuite::TestInequality(void){ UnitTest::SetPrefix("TestInequality.cpp - Inequality"); #ifdef JSON_LIBRARY JSONNODE * test1 = json_new(JSON_NODE); JSONNODE * test2 = json_new(JSON_NODE); json_set_a(test1, JSON_TEXT("hello")); json_set_a(test2, JSON_TEXT("world")); assertFalse(json_equal(test1, test2)); json_set_i(test2,13); assertFalse(json_equal(test1, test2)); json_set_f(test2, 13.5f); assertFalse(json_equal(test1, test2)); json_set_b(test2, true); assertFalse(json_equal(test1, test2)); json_set_b(test2, false); assertFalse(json_equal(test1, test2)); json_nullify(test2); assertFalse(json_equal(test1, test2)); json_delete(test1); json_delete(test2); #else JSONNode test1; JSONNode test2; test1 = JSON_TEXT("hello"); test2 = JSON_TEXT("world"); assertNotEquals(test1, test2); assertNotEquals(test1, JSON_TEXT("hi")); assertNotEquals(test2, 13.5f); assertNotEquals(test2, 14); assertNotEquals(test2, true); assertNotEquals(test2, false); test2 = 13; assertNotEquals(test1, test2); assertNotEquals(test2, 13.5f); assertNotEquals(test2, 14); assertNotEquals(test2, true); assertNotEquals(test2, false); assertNotEquals(test2, JSON_TEXT("13")); //not the same type test2 = 13.5f; assertNotEquals(test1, test2); assertNotEquals(test2, 13); assertNotEquals(test2, 14); assertNotEquals(test2, true); assertNotEquals(test2, false); assertNotEquals(test2, JSON_TEXT("13.5")); //not the same type test2 = true; assertNotEquals(test1, test2); assertNotEquals(test2, 13.5f); assertNotEquals(test2, 14); assertNotEquals(test2, false); assertNotEquals(test2, JSON_TEXT("true")); //not the same type test2 = false; assertNotEquals(test1, test2); assertNotEquals(test2, 13.5f); assertNotEquals(test2, 14); assertNotEquals(test2, true); assertNotEquals(test2, JSON_TEXT("false")); //not the same type test2.nullify(); assertNotEquals(test1, test2); assertNotEquals(test2, 13.5f); assertNotEquals(test2, 14); assertNotEquals(test2, true); assertNotEquals(test2, false); assertNotEquals(test2, "null"); //not the same type #endif }
u_char *dbrelay_db_run_query(dbrelay_request_t *request) { /* FIX ME */ char error_string[500]; json_t *json = json_new(); u_char *ret; dbrelay_connection_t *conn; dbrelay_connection_t *connections; int s = 0; int slot = -1; char *newsql; int have_error = 0; pid_t helper_pid = 0; error_string[0]='\0'; dbrelay_log_info(request, "run_query called"); if (request->flags & DBRELAY_FLAG_PP) json_pretty_print(json, 1); if (IS_SET(request->js_callback)) { json_add_callback(json, request->js_callback); } json_new_object(json); dbrelay_append_request_json(json, request); if (!dbrelay_check_request(request)) { dbrelay_db_restart_json(request, &json); dbrelay_log_info(request, "check_request failed."); dbrelay_write_json_log(json, request, "Not all required parameters submitted."); if (IS_SET(request->js_callback) || IS_SET(request->js_error)) { json_end_callback(json); } ret = (u_char *) json_to_string(json); json_free(json); return ret; } newsql = dbrelay_resolve_params(request, request->sql); conn = dbrelay_wait_for_connection(request, &s); if (conn == NULL) { dbrelay_db_restart_json(request, &json); dbrelay_write_json_log(json, request, "Couldn't allocate new connection"); if (IS_SET(request->js_callback) || IS_SET(request->js_error)) json_end_callback(json); ret = (u_char *) json_to_string(json); json_free(json); return ret; } slot = conn->slot; dbrelay_log_debug(request, "Allocated connection for query"); error_string[0]='\0'; if (IS_SET(request->connection_name)) { if ((helper_pid = dbrelay_conn_initialize(s, request))==-1) { dbrelay_db_restart_json(request, &json); strcpy(error_string, "Couldn't initialize connector"); dbrelay_db_restart_json(request, &json); dbrelay_write_json_log(json, request, "Couldn't initialize connector"); if (IS_SET(request->js_callback) || IS_SET(request->js_error)) json_end_callback(json); free(newsql); ret = (u_char *) json_to_string(json); json_free(json); return ret; } else if (helper_pid) { // write the connectors pid into shared memory connections = dbrelay_time_get_shmem(request); connections[slot].helper_pid = helper_pid; dbrelay_time_release_shmem(request, connections); } // else we didn't get a pid but didn't fail, shouldn't happen dbrelay_log_info(request, "sending request"); ret = (u_char *) dbrelay_conn_send_request(s, request, &have_error); dbrelay_log_debug(request, "back"); // internal error if (have_error==2) { dbrelay_log_error(request, "Error occurred on socket %s (PID: %u)", conn->sock_path, conn->helper_pid); // socket error of some sort, kill the connector to be safe and let it restart on its own dbrelay_conn_kill(s); } if (have_error) { dbrelay_db_restart_json(request, &json); dbrelay_log_debug(request, "have error %s\n", ret); dbrelay_copy_string(error_string, (char *)ret, sizeof(error_string)); } else if (!IS_SET((char *)ret)) { dbrelay_log_warn(request, "Connector returned no information"); dbrelay_log_info(request, "Query was: %s", newsql); } else { json_add_json(json, ", "); json_add_json(json, (char *) ret); free(ret); } dbrelay_log_debug(request, "closing"); dbrelay_conn_close(s); dbrelay_log_debug(request, "after close"); } else { if (!api->connected(conn->db)) { //strcpy(error_string, "Failed to login"); //if (login_msgno == 18452 && IS_EMPTY(request->sql_password)) { if (IS_EMPTY(request->sql_password)) { strcpy(error_string, "Login failed and no password was set, please check.\n"); dbrelay_copy_string(&error_string[strlen(error_string)], api->error(conn->db), sizeof(error_string) - strlen(error_string)); } else if (!strlen(api->error(conn->db))) { strcpy(error_string, "Connection failed.\n"); } else { dbrelay_copy_string(error_string, api->error(conn->db), sizeof(error_string)); } dbrelay_db_restart_json(request, &json); } else { dbrelay_log_debug(request, "Sending sql query"); ret = dbrelay_exec_query(conn, request->sql_database, newsql, request->flags); if (ret==NULL) { dbrelay_db_restart_json(request, &json); dbrelay_log_debug(request, "error"); //strcpy(error_string, request->error_message); strcpy(error_string, api->error(conn->db)); } else { json_add_json(json, ", "); json_add_json(json, (char *) ret); free(ret); } dbrelay_log_debug(request, "Done filling JSON output"); } } // !named connection free(conn); free(newsql); dbrelay_log_debug(request, "error = %s\n", error_string); dbrelay_append_log_json(json, request, error_string); if (IS_SET(request->js_callback) || IS_SET(request->js_error)) { json_end_callback(json); } ret = (u_char *) json_to_string(json); json_free(json); dbrelay_log_debug(request, "Query completed, freeing connection."); connections = dbrelay_time_get_shmem(request); /* set time accessed at end of processing so that long queries do not * become eligible for being timed out immediately. */ conn = &connections[slot]; conn->tm_accessed = time(NULL); dbrelay_db_free_connection(conn, request); dbrelay_time_release_shmem(request, connections); return ret; }