LONGBOW_TEST_CASE(JSON, parcJSON_AddArray) { PARCJSON *json = parcJSON_Create(); char *expectedName = "array"; PARCJSONArray *array = parcJSONArray_Create(); PARCJSONValue *value = parcJSONValue_CreateFromCString("Some Pig"); parcJSONArray_AddValue(array, value); parcJSON_AddArray(json, expectedName, array); parcJSONArray_Release(&array); const PARCJSONPair *pair = parcJSON_GetPairByName(json, expectedName); PARCBuffer *actualName = parcJSONPair_GetName(pair); PARCJSONValue *actualValue = parcJSONPair_GetValue(pair); assertTrue(strcmp(expectedName, parcBuffer_Overlay(actualName, 0)) == 0, "Expected name %s, actual %s", expectedName, (char *) parcBuffer_ToString(actualName)); assertTrue(parcJSONValue_IsArray(actualValue), "Expect value to be type PARCJSONArray"); array = parcJSONValue_GetArray(actualValue); PARCJSONValue *result = parcJSONArray_GetValue(array, 0); assertTrue(parcBuffer_Equals(parcJSONValue_GetString(result), parcJSONValue_GetString(value)), "Expected %s actual %s", parcJSONValue_ToString(value), parcJSONValue_ToString(result)); parcJSONValue_Release(&value); parcJSON_Release(&json); }
PARCJSON * ccnxManifest_ToJSON(const CCNxManifest *manifest) { PARCJSON *root = parcJSON_Create(); char *nameString = ccnxName_ToString(ccnxManifest_GetName(manifest)); parcJSON_AddString(root, "locator", nameString); parcMemory_Deallocate(&nameString); PARCJSONArray *array = parcJSONArray_Create(); for (size_t i = 0; i < ccnxManifest_GetNumberOfHashGroups(manifest); i++) { CCNxManifestHashGroup *group = ccnxManifest_GetHashGroupByIndex(manifest, i); PARCJSON *groupJson = ccnxManifestHashGroup_ToJson(group); PARCJSONValue *jsonValue = parcJSONValue_CreateFromJSON(groupJson); parcJSONArray_AddValue(array, jsonValue); parcJSONValue_Release(&jsonValue); parcJSON_Release(&groupJson); ccnxManifestHashGroup_Release(&group); } parcJSON_AddArray(root, "HashGroups", array); parcJSONArray_Release(&array); return root; }
PARCJSON * ccnxManifestHashGroup_ToJson(const CCNxManifestHashGroup *group) { PARCJSON *root = parcJSON_Create(); PARCJSONArray *ptrList = parcJSONArray_Create(); for (size_t i = 0; i < parcLinkedList_Size(group->pointers); i++) { CCNxManifestHashGroupPointer *ptr = (CCNxManifestHashGroupPointer *) parcLinkedList_GetAtIndex(group->pointers, i); PARCJSON *ptrJson = parcJSON_Create(); // Type. parcJSON_AddInteger(ptrJson, "type", ccnxManifestHashGroupPointer_GetType(ptr)); // Digest. char *digestString = parcBuffer_ToHexString(ptr->digest); parcJSON_AddString(ptrJson, "digest", digestString); parcMemory_Deallocate(&digestString); // Add the tuple to the list. PARCJSONValue *val = parcJSONValue_CreateFromJSON(ptrJson); parcJSONArray_AddValue(ptrList, val); // Cleanup parcJSONValue_Release(&val); parcJSON_Release(&ptrJson); } root = parcJSON_AddArray(root, "HashGroup", ptrList); parcJSONArray_Release(&ptrList); if (group->overallDataDigest != NULL) { char *digestString = parcBuffer_ToHexString(group->overallDataDigest); root = parcJSON_AddString(root, "overallDataDigest", digestString); parcMemory_Deallocate((void **) &digestString); } if (group->locator != NULL) { char *locatorString = ccnxName_ToString(group->locator); root = parcJSON_AddString(root, "locator", locatorString); parcMemory_Deallocate((void **) &locatorString); } if (group->entrySize > 0) { root = parcJSON_AddInteger(root, "entrySize", group->entrySize); } if (group->dataSize > 0) { root = parcJSON_AddInteger(root, "dataSize", group->dataSize); } if (group->blockSize > 0) { root = parcJSON_AddInteger(root, "blockSize", group->blockSize); } if (group->treeHeight > 0) { root = parcJSON_AddInteger(root, "treeHeight", group->treeHeight); } return root; }
PARCJSON * cpiConnectionList_ToJson(const CPIConnectionList *list) { assertNotNull(list, "Parameter must be non-null"); PARCJSONArray *inner_json = parcJSONArray_Create(); size_t length = parcArrayList_Size(list->listOfConnections); for (size_t i = 0; i < length; i++) { CPIConnection *tunnel = (CPIConnection *) parcArrayList_Get(list->listOfConnections, i); PARCJSON *json = cpiConnection_ToJson(tunnel); PARCJSONValue *value = parcJSONValue_CreateFromJSON(json); parcJSON_Release(&json); parcJSONArray_AddValue(inner_json, value); parcJSONValue_Release(&value); } PARCJSON *outter_json = parcJSON_Create(); parcJSON_AddArray(outter_json, cpi_ConnectionList, inner_json); parcJSONArray_Release(&inner_json); return outter_json; }
PARCJSON * cpiInterfaceSet_ToJson(CPIInterfaceSet *set) { assertNotNull(set, "Parameter must be non-null"); PARCJSONArray *interfaceList = parcJSONArray_Create(); size_t length = parcArrayList_Size(set->listOfInterfaces); for (size_t i = 0; i < length; i++) { CPIInterface *iface = (CPIInterface *) parcArrayList_Get(set->listOfInterfaces, i); PARCJSON *json = cpiInterface_ToJson(iface); PARCJSONValue *value = parcJSONValue_CreateFromJSON(json); parcJSON_Release(&json); parcJSONArray_AddValue(interfaceList, value); parcJSONValue_Release(&value); } PARCJSON *result = parcJSON_Create(); parcJSON_AddArray(result, cpi_InterfaceList, interfaceList); parcJSONArray_Release(&interfaceList); return result; }
static CCNxMetaMessage * _create_FIBList_response(Athena *athena, CCNxName *ccnxName, PARCList *fibEntryList) { PARCJSON *jsonPayload = parcJSON_Create(); PARCJSONArray *jsonEntryList = parcJSONArray_Create(); parcJSON_AddArray(jsonPayload, JSON_KEY_RESULT, jsonEntryList); for (size_t i = 0; i < parcList_Size(fibEntryList); ++i) { AthenaFIBListEntry *entry = parcList_GetAtIndex(fibEntryList, i); if (entry != NULL) { CCNxName *prefixName = athenaFIBListEntry_GetName(entry); if (prefixName) { char *prefix = ccnxName_ToString(prefixName); int linkId = athenaFIBListEntry_GetLinkId(entry); const char *linkName = athenaTransportLinkAdapter_LinkIdToName(athena->athenaTransportLinkAdapter, linkId); parcLog_Debug(athena->log, " Route: %s->%s", prefix, linkName); PARCJSON *jsonItem = parcJSON_Create(); parcJSON_AddString(jsonItem, JSON_KEY_NAME, prefix); parcJSON_AddString(jsonItem, JSON_KEY_LINK, linkName); PARCJSONValue *jsonItemValue = parcJSONValue_CreateFromJSON(jsonItem); parcJSON_Release(&jsonItem); parcJSONArray_AddValue(jsonEntryList, jsonItemValue); parcJSONValue_Release(&jsonItemValue); parcMemory_Deallocate(&prefix); } else { int linkId = athenaFIBListEntry_GetLinkId(entry); const char *linkName = athenaTransportLinkAdapter_LinkIdToName(athena->athenaTransportLinkAdapter, linkId); parcLog_Debug(athena->log, " Route: <empty>->%s", linkName); PARCJSON *jsonItem = parcJSON_Create(); parcJSON_AddString(jsonItem, JSON_KEY_NAME, ""); parcJSON_AddString(jsonItem, JSON_KEY_LINK, linkName); PARCJSONValue *jsonItemValue = parcJSONValue_CreateFromJSON(jsonItem); parcJSON_Release(&jsonItem); parcJSONArray_AddValue(jsonEntryList, jsonItemValue); parcJSONValue_Release(&jsonItemValue); } } } char *jsonString = parcJSON_ToString(jsonPayload); parcJSONArray_Release(&jsonEntryList); parcJSON_Release(&jsonPayload); PARCBuffer *payload = parcBuffer_CreateFromArray(jsonString, strlen(jsonString)); CCNxContentObject *contentObject = ccnxContentObject_CreateWithNameAndPayload(ccnxName, parcBuffer_Flip(payload)); struct timeval tv; gettimeofday(&tv, NULL); uint64_t nowInMillis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); ccnxContentObject_SetExpiryTime(contentObject, nowInMillis + 100); // this response is good for 100 millis CCNxMetaMessage *result = ccnxMetaMessage_CreateFromContentObject(contentObject); ccnxContentObject_Release(&contentObject); parcBuffer_Release(&payload); parcMemory_Deallocate(&jsonString); athena_EncodeMessage(result); return result; }