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); }
LONGBOW_TEST_CASE(parc_JSONArray, parcJSONArray_GetValue) { PARCJSONArray *array = parcJSONArray_Create(); PARCJSONValue *expected = parcJSONValue_CreateFromInteger(10); parcJSONArray_AddValue(array, expected); PARCJSONValue *actual = parcJSONArray_GetValue(array, 0); assertTrue(expected == actual, "Expected different value"); parcJSONValue_Release(&expected); parcJSONArray_Release(&array); }
static int _athenactl_ListFIB(PARCIdentity *identity, int argc, char **argv) { CCNxName *name = ccnxName_CreateFromURI(CCNxNameAthenaCommand_FIBList); CCNxInterest *interest = ccnxInterest_CreateSimple(name); ccnxName_Release(&name); const char *result = _athenactl_SendInterestControl(identity, interest); if (result) { PARCJSON *jsonContent = parcJSON_ParseString(result); if (jsonContent != NULL) { PARCJSONValue *resultValue = parcJSON_GetValueByName(jsonContent, JSON_KEY_RESULT); PARCJSONArray *fibEntryList = parcJSONValue_GetArray(resultValue); size_t fibEntryListLength = parcJSONArray_GetLength(fibEntryList); printf("Routes (%d):\n", (int) fibEntryListLength); if (fibEntryListLength == 0) { printf(" No Entries\n"); } for (size_t i = 0; i < fibEntryListLength; ++i) { PARCJSONValue *elementValue = parcJSONArray_GetValue(fibEntryList, i); PARCJSON *valueObj = parcJSONValue_GetJSON(elementValue); PARCJSONValue *value = parcJSON_GetValueByName(valueObj, JSON_KEY_NAME); char *prefixString = parcBuffer_ToString(parcJSONValue_GetString(value)); value = parcJSON_GetValueByName(valueObj, JSON_KEY_LINK); char *linkString = parcBuffer_ToString(parcJSONValue_GetString(value)); printf(" %s -> %s\n", prefixString, linkString); parcMemory_Deallocate(&prefixString); parcMemory_Deallocate(&linkString); } parcJSON_Release(&jsonContent); } else { printf("Returned value is not JSON: %s\n", result); } parcMemory_Deallocate(&result); } else { printf("NULL result recieved from route List request\n"); } ccnxMetaMessage_Release(&interest); return 0; }
CPIConnectionList * cpiConnectionList_FromJson(PARCJSON *json) { assertNotNull(json, "Parameter must be non-null"); PARCJSONValue *value = parcJSON_GetValueByName(json, cpi_ConnectionList); assertNotNull(value, "JSON key not found %s: %s", cpi_ConnectionList, parcJSON_ToString(json)); PARCJSONArray *tunnelListJson = parcJSONValue_GetArray(value); CPIConnectionList *list = cpiConnectionList_Create(); size_t length = parcJSONArray_GetLength(tunnelListJson); for (size_t i = 0; i < length; i++) { value = parcJSONArray_GetValue(tunnelListJson, i); PARCJSON *tunnelJson = parcJSONValue_GetJSON(value); CPIConnection *tunnel = cpiConnection_CreateFromJson(tunnelJson); cpiConnectionList_Append(list, tunnel); } return list; }
CPIInterfaceSet * cpiInterfaceSet_FromJson(PARCJSON *json) { assertNotNull(json, "Parameter must be non-null"); PARCJSONValue *value = parcJSON_GetValueByName(json, cpi_InterfaceList); assertNotNull(value, "JSON key not found %s: %s", cpi_InterfaceList, parcJSON_ToString(json)); PARCJSONArray *ifaceSetJson = parcJSONValue_GetArray(value); CPIInterfaceSet *set = cpiInterfaceSet_Create(); size_t length = parcJSONArray_GetLength(ifaceSetJson); for (size_t i = 0; i < length; i++) { value = parcJSONArray_GetValue(ifaceSetJson, i); PARCJSON *ifaceJson = parcJSONValue_GetJSON(value); CPIInterface *iface = cpiInterface_FromJson(ifaceJson); cpiInterfaceSet_Add(set, iface); } return set; }
CCNxManifestHashGroup * ccnxManifestHashGroup_CreateFromJson(const PARCJSON *json) { CCNxManifestHashGroup *group = ccnxManifestHashGroup_Create(); PARCJSONValue *ptrListValue = parcJSON_GetValueByName(json, "HashGroup"); PARCJSONArray *ptrList = parcJSONValue_GetArray(ptrListValue); size_t numberOfPointers = parcJSONArray_GetLength(ptrList); for (size_t i = 0; i < numberOfPointers; i++) { PARCJSONValue *pointerValue = parcJSONArray_GetValue(ptrList, i); PARCJSON *typeJson = parcJSONValue_GetJSON(pointerValue); PARCJSONValue *typeValue = parcJSON_GetValueByName(typeJson, "type"); CCNxManifestHashGroupPointerType type; if (parcJSONValue_GetInteger(typeValue) == 0) { type = CCNxManifestHashGroupPointerType_Data; } else { type = CCNxManifestHashGroupPointerType_Manifest; } PARCJSON *digestJson = parcJSONValue_GetJSON(pointerValue); PARCJSONValue *digestValue = parcJSON_GetValueByName(digestJson, "digest"); PARCBuffer *digestHex = parcJSONValue_GetString(digestValue); char *hexString = parcBuffer_ToString(digestHex); PARCBuffer *digest = parcBuffer_Flip(parcBuffer_ParseHexString(hexString)); parcMemory_Deallocate(&hexString); ccnxManifestHashGroup_AppendPointer(group, type, digest); parcBuffer_Release(&digest); } if (parcJSON_GetPairByName(json, "overallDataDigest") != NULL) { PARCJSONValue *overallDataDigestValue = parcJSON_GetValueByName(json, "overallDataDigest"); PARCBuffer *digestHex = parcJSONValue_GetString(overallDataDigestValue); char *hexString = parcBuffer_ToString(digestHex); group->overallDataDigest = parcBuffer_Flip(parcBuffer_ParseHexString(hexString)); parcMemory_Deallocate(&hexString); } if (parcJSON_GetPairByName(json, "locator") != NULL) { PARCJSONValue *locatorValue = parcJSON_GetValueByName(json, "locator"); PARCBuffer *buffer = parcJSONValue_GetString(locatorValue); char *locator = parcBuffer_ToString(buffer); group->locator = ccnxName_CreateFromCString(locator); parcMemory_Deallocate(&locator); } if (parcJSON_GetPairByName(json, "entrySize") != NULL) { PARCJSONValue *childBlockNodeSizeValue = parcJSON_GetValueByName(json, "entrySize"); group->entrySize = parcJSONValue_GetInteger(childBlockNodeSizeValue); } if (parcJSON_GetPairByName(json, "dataSize") != NULL) { PARCJSONValue *totalSizeValue = parcJSON_GetValueByName(json, "dataSize"); group->dataSize = parcJSONValue_GetInteger(totalSizeValue); } if (parcJSON_GetPairByName(json, "blockSize") != NULL) { PARCJSONValue *blockSizeValue = parcJSON_GetValueByName(json, "blockSize"); group->blockSize = parcJSONValue_GetInteger(blockSizeValue); } if (parcJSON_GetPairByName(json, "treeHeight") != NULL) { PARCJSONValue *treeHeightValue = parcJSON_GetValueByName(json, "treeHeight"); group->treeHeight = parcJSONValue_GetInteger(treeHeightValue); } return group; }
static int _athenactl_ListLinks(PARCIdentity *identity, int argc, char **argv) { CCNxName *name = ccnxName_CreateFromURI(CCNxNameAthenaCommand_LinkList); CCNxInterest *interest = ccnxInterest_CreateSimple(name); ccnxName_Release(&name); const char *result = _athenactl_SendInterestControl(identity, interest); printf("Link: Interface list"); if (result) { PARCBuffer *buffer = parcBuffer_WrapCString((char *) result); PARCJSONParser *parser = parcJSONParser_Create(buffer); PARCJSONValue *value = parcJSONValue_Parser(parser); if (value == NULL) { printf("\n\tCould not parse forwarder list response"); } else { PARCJSONArray *array = parcJSONValue_GetArray(value); for (int i = 0; i < parcJSONArray_GetLength(array); i++) { PARCJSONValue *getvalue = parcJSONArray_GetValue(array, i); PARCJSON *json = parcJSONValue_GetJSON(getvalue); PARCJSONValue *pairValue = parcJSON_GetValueByName(json, "linkName"); PARCBuffer *bufferString = parcJSONValue_GetString(pairValue); char *linkName = parcBuffer_ToString(bufferString); pairValue = parcJSON_GetValueByName(json, "index"); int64_t index = parcJSONValue_GetInteger(pairValue); pairValue = parcJSON_GetValueByName(json, "notLocal"); bool notLocal = parcJSONValue_GetBoolean(pairValue); pairValue = parcJSON_GetValueByName(json, "localForced"); bool localForced = parcJSONValue_GetBoolean(pairValue); if (index < 0) { if (notLocal) { printf("\n Link listener%s: %s", localForced ? " (forced remote)" : "", linkName); } else { printf("\n Link listener%s: %s", localForced ? " (forced local)" : "", linkName); } } else { if (notLocal) { printf("\n Link instance [%" PRId64 "] %s: %s", index, localForced ? "(forced remote)" : "(remote)", linkName); } else { printf("\n Link instance [%" PRId64 "] %s: %s", index, localForced ? "(forced local)" : "(local)", linkName); } } parcMemory_Deallocate(&linkName); } parcJSONValue_Release(&value); } parcJSONParser_Release(&parser); parcBuffer_Release(&buffer); parcMemory_Deallocate(&result); } printf("\nDone.\n"); ccnxMetaMessage_Release(&interest); return 0; }