in iothub_service_client/src/iothub_registrymanager.c [432:546]
static BUFFER_HANDLE constructDeviceOrModuleJson(const IOTHUB_DEVICE_OR_MODULE* deviceOrModuleInfo)
{
BUFFER_HANDLE result;
JSON_Value* root_value = NULL;
JSON_Object* root_object = NULL;
JSON_Status jsonStatus;
const char *authTypeForJson;
int iotEdge_capable = (deviceOrModuleInfo->iotEdge_capable == true) ? 1 : 0;
if (deviceOrModuleInfo == NULL)
{
LogError("deviceOrModuleInfo cannot be null");
result = NULL;
}
else if (deviceOrModuleInfo->deviceId == NULL)
{
LogError("Device id cannot be NULL");
result = NULL;
}
if ((root_value = json_value_init_object()) == NULL)
{
LogError("json_value_init_object failed");
result = NULL;
}
else if ((root_object = json_value_get_object(root_value)) == NULL)
{
LogError("json_value_get_object failed");
result = NULL;
}
else if ((json_object_set_string(root_object, DEVICE_JSON_KEY_DEVICE_NAME, deviceOrModuleInfo->deviceId)) != JSONSuccess)
{
LogError("json_object_set_string failed for deviceId");
result = NULL;
}
else if ((deviceOrModuleInfo->moduleId != NULL) && ((json_object_set_string(root_object, DEVICE_JSON_KEY_MODULE_NAME, deviceOrModuleInfo->moduleId)) != JSONSuccess))
{
LogError("json_object_set_string failed for moduleId");
result = NULL;
}
else if ((deviceOrModuleInfo->managedBy != NULL) && ((json_object_set_string(root_object, DEVICE_JSON_KEY_MANAGED_BY, deviceOrModuleInfo->managedBy)) != JSONSuccess))
{
LogError("json_object_set_string failed for managedBy");
result = NULL;
}
else if (json_object_dotset_string(root_object, DEVICE_JSON_KEY_DEVICE_STATUS, getStatusStringForJson(deviceOrModuleInfo->status)) != JSONSuccess)
{
LogError("json_object_dotset_string failed for status");
result = NULL;
}
else if ((NULL == (authTypeForJson = getAuthTypeStringForJson(deviceOrModuleInfo->authMethod))) || ((json_object_dotset_string(root_object, DEVICE_JSON_KEY_DEVICE_AUTH_TYPE, authTypeForJson)) != JSONSuccess))
{
LogError("json_object_dotset_string failed for authType");
result = NULL;
}
//
// Static function here. We make the assumption that the auth method has been validated by the caller of this function.
//
else if ((deviceOrModuleInfo->authMethod == IOTHUB_REGISTRYMANAGER_AUTH_SPK) && ((json_object_dotset_string(root_object, DEVICE_JSON_KEY_DEVICE_PRIMARY_KEY, deviceOrModuleInfo->primaryKey)) != JSONSuccess))
{
LogError("json_object_dotset_string failed for primarykey");
result = NULL;
}
else if ((deviceOrModuleInfo->authMethod == IOTHUB_REGISTRYMANAGER_AUTH_SPK) && ((json_object_dotset_string(root_object, DEVICE_JSON_KEY_DEVICE_SECONDARY_KEY, deviceOrModuleInfo->secondaryKey)) != JSONSuccess))
{
LogError("json_object_dotset_string failed for secondaryKey");
result = NULL;
}
else if ((deviceOrModuleInfo->authMethod == IOTHUB_REGISTRYMANAGER_AUTH_X509_THUMBPRINT) && ((json_object_dotset_string(root_object, DEVICE_JSON_KEY_DEVICE_PRIMARY_THUMBPRINT, deviceOrModuleInfo->primaryKey)) != JSONSuccess))
{
LogError("json_object_dotset_string failed for primaryThumbprint");
result = NULL;
}
else if ((deviceOrModuleInfo->authMethod == IOTHUB_REGISTRYMANAGER_AUTH_X509_THUMBPRINT) && ((json_object_dotset_string(root_object, DEVICE_JSON_KEY_DEVICE_SECONDARY_THUMBPRINT, deviceOrModuleInfo->secondaryKey)) != JSONSuccess))
{
LogError("json_object_dotset_string failed for secondaryThumbprint");
result = NULL;
}
else if ((deviceOrModuleInfo->moduleId == NULL) && (json_object_dotset_boolean(root_object, DEVICE_JSON_KEY_CAPABILITIES_IOTEDGE, iotEdge_capable)) != JSONSuccess)
{
LogError("json_object_dotset_string failed for iotEdge capable");
result = NULL;
}
else
{
char* serialized_string;
if ((serialized_string = json_serialize_to_string(root_value)) == NULL)
{
LogError("json_serialize_to_string failed");
result = NULL;
}
else
{
if ((result = BUFFER_create((const unsigned char*)serialized_string, strlen(serialized_string))) == NULL)
{
LogError("Buffer_Create failed");
result = NULL;
}
json_free_serialized_string(serialized_string);
}
}
if ((jsonStatus = json_object_clear(root_object)) != JSONSuccess)
{
LogError("json_object_clear failed");
BUFFER_delete(result);
result = NULL;
}
if(root_value != NULL)
json_value_free(root_value);
return result;
}