/* **************************************************************************** * * Entity::render - * * The rendering of JSON in APIv2 depends on the URI param 'options' * Rendering methods: * o 'normalized' (default) * o 'keyValues' (less verbose, only name and values shown for attributes - no type, no metadatas) * o 'values' (only the values of the attributes are printed, in a vector) */ std::string Entity::render(ConnectionInfo* ciP, RequestType requestType, bool comma) { std::string renderMode = RENDER_MODE_NORMALIZED; if (ciP->uriParamOptions[OPT_KEY_VALUES] == true) { renderMode = RENDER_MODE_KEY_VALUES; } else if (ciP->uriParamOptions[OPT_VALUES] == true) { renderMode = RENDER_MODE_VALUES; } else if (ciP->uriParamOptions[OPT_UNIQUE_VALUES] == true) { renderMode = RENDER_MODE_UNIQUE_VALUES; } if ((errorCode.description == "") && ((errorCode.error == "OK") || (errorCode.error == ""))) { std::string out; if ((renderMode == RENDER_MODE_VALUES) || (renderMode == RENDER_MODE_UNIQUE_VALUES)) { out = "["; if (attributeVector.size() != 0) { out += attributeVector.toJson(true, false, renderMode, ciP->uriParam["attrs"]); } out += "]"; } else { out = "{"; out += JSON_VALUE("id", id); out += ","; /* This is needed for entities coming from NGSIv1 (which allows empty or missing types) */ out += JSON_STR("type") + ":" + ((type != "")? JSON_STR(type) : JSON_STR(DEFAULT_TYPE)); if (attributeVector.size() != 0) { out += ","; out += attributeVector.toJson(true, false, renderMode, ciP->uriParam["attrs"]); } out += "}"; } if (comma) { out += ","; } return out; } return errorCode.toJson(true); }
/* **************************************************************************** * * EntityTypeAttributesResponse::toJson - */ std::string EntityTypeAttributesResponse::toJson(ConnectionInfo* ciP) { std::string out = "{"; char countV[16]; snprintf(countV, sizeof(countV), "%lld", entityType.count); out += JSON_STR("attrs") + ":"; out += "{"; out += entityType.contextAttributeVector.toJson(false, true); out += "}"; out += "," + JSON_STR("count") + ":" + countV; out += "}"; return out; }
/* **************************************************************************** * * EntityType::toJson - */ std::string EntityType::toJson(ConnectionInfo* ciP, bool includeType) { std::string out = "{"; char countV[STRING_SIZE_FOR_INT]; snprintf(countV, sizeof(countV), "%lld", count); if (includeType) { out += JSON_VALUE("type", type) + ","; } out += JSON_STR("attrs") + ":"; out += "{"; out += contextAttributeVector.toJsonTypes(); out += "}"; out += "," + JSON_STR("count") + ":" + countV; out += "}"; return out; }
/* **************************************************************************** * * toJson - */ std::string CompoundValueNode::toJson(bool isLastElement) { std::string out = ""; bool jsonComma = siblingNo < (int) container->childV.size() - 1; std::string tagName = (container->valueType == orion::ValueTypeVector)? "item" : name; // No "comma after" if toplevel if (container == this) { jsonComma = false; } if (valueType == orion::ValueTypeString) { LM_T(LmtCompoundValueRender, ("I am a String (%s)", name.c_str())); if (container->valueType == orion::ValueTypeVector) { out = JSON_STR(stringValue); } else { out = JSON_STR(tagName) + ":" + JSON_STR(stringValue); } } else if (valueType == orion::ValueTypeNumber) { char num[32]; LM_T(LmtCompoundValueRender, ("I am a Number (%s)", name.c_str())); snprintf(num, sizeof(num), "%f", numberValue); if (container->valueType == orion::ValueTypeVector) { out = JSON_NUMBER(num); } else { out = JSON_STR(tagName) + ":" + JSON_NUMBER(num); } } else if (valueType == orion::ValueTypeBoolean) { LM_T(LmtCompoundValueRender, ("I am a Bool (%s)", name.c_str())); if (container->valueType == orion::ValueTypeVector) { out = JSON_BOOL(boolValue); } else { out = JSON_STR(tagName) + ":" + JSON_BOOL(boolValue); } } else if ((valueType == orion::ValueTypeVector) && (container == this)) { // // NOTE: Here, the '[]' are already added in the calling function // LM_T(LmtCompoundValueRender, ("I am a Vector (%s) and my container is TOPLEVEL", name.c_str())); for (uint64_t ix = 0; ix < childV.size(); ++ix) { out += childV[ix]->toJson(ix == childV.size() - 1); } } else if ((valueType == orion::ValueTypeVector) && (container->valueType == orion::ValueTypeVector)) { out += "["; for (uint64_t ix = 0; ix < childV.size(); ++ix) { out += childV[ix]->toJson(false); } out += "]"; } else if (valueType == orion::ValueTypeVector) { LM_T(LmtCompoundValueRender, ("I am a Vector (%s)", name.c_str())); out += JSON_STR(name) + ":["; for (uint64_t ix = 0; ix < childV.size(); ++ix) { out += childV[ix]->toJson(false); } out += "]"; } else if ((valueType == orion::ValueTypeObject) && (container->valueType == orion::ValueTypeVector)) { LM_T(LmtCompoundValueRender, ("I am an Object (%s) and my container is a Vector", name.c_str())); out += "{"; for (uint64_t ix = 0; ix < childV.size(); ++ix) { out += childV[ix]->toJson(ix == childV.size() - 1); } out += "}"; } else if (valueType == orion::ValueTypeObject) { if (rootP != this) { LM_T(LmtCompoundValueRender, ("I am an Object (%s) and my container is NOT a Vector", name.c_str())); out += JSON_STR(name) + ":{"; for (uint64_t ix = 0; ix < childV.size(); ++ix) { out += childV[ix]->toJson(ix == childV.size() - 1); } out += "}"; } else { LM_T(LmtCompoundValueRender, ("I am the TREE ROOT (%s: %d children)", name.c_str(), childV.size())); for (uint64_t ix = 0; ix < childV.size(); ++ix) { out += childV[ix]->toJson(true); } } } out += jsonComma? "," : ""; return out; }
/* **************************************************************************** * * toJson - */ std::string Metadata::toJson(bool isLastElement) { std::string out; if (type == "") { if (valueType == orion::ValueTypeNumber) { char num[32]; snprintf(num, sizeof(num), "%f", numberValue); out = JSON_VALUE_NUMBER(name, num); } else if (valueType == orion::ValueTypeBoolean) { out = JSON_VALUE_BOOL(name, boolValue); } else if (valueType == orion::ValueTypeString) { out = JSON_VALUE(name, stringValue); } else { LM_E(("Runtime Error (invalid type for metadata %s)", name.c_str())); out = JSON_VALUE(name, stringValue); } } else { out = JSON_STR(name) + ":{"; out += JSON_VALUE("type", type) + ","; if (valueType == orion::ValueTypeString) { out += JSON_VALUE("value", stringValue); } else if (valueType == orion::ValueTypeNumber) { char num[32]; snprintf(num, sizeof(num), "%f", numberValue); out += JSON_VALUE_NUMBER("value", num); } else if (valueType == orion::ValueTypeBoolean) { out += JSON_VALUE_BOOL("value", boolValue); } else { LM_E(("Runtime Error (invalid value type for metadata %s)", name.c_str())); out += JSON_VALUE("value", stringValue); } out += "}"; } if (!isLastElement) { out += ","; } return out; }
/* **************************************************************************** * * Entity::render - * * The rendering of JSON in APIv2 depends on the URI param 'options' * Rendering methods: * o 'normalized' (default) * o 'keyValues' (less verbose, only name and values shown for attributes - no type, no metadatas) * o 'values' (only the values of the attributes are printed, in a vector) */ std::string Entity::render(ConnectionInfo* ciP, RequestType requestType, bool comma) { RenderFormat renderFormat = NGSI_V2_NORMALIZED; if (ciP->uriParamOptions[OPT_KEY_VALUES] == true) { renderFormat = NGSI_V2_KEYVALUES; } else if (ciP->uriParamOptions[OPT_VALUES] == true) { renderFormat = NGSI_V2_VALUES; } else if (ciP->uriParamOptions[OPT_UNIQUE_VALUES] == true) { renderFormat = NGSI_V2_UNIQUE_VALUES; } if ((oe.details == "") && ((oe.reasonPhrase == "OK") || (oe.reasonPhrase == ""))) { std::string out; std::vector<std::string> metadataFilter; std::vector<std::string> attrsFilter; if (ciP->uriParam[URI_PARAM_METADATA] != "") { stringSplit(ciP->uriParam[URI_PARAM_METADATA], ',', metadataFilter); } if (ciP->uriParam[URI_PARAM_ATTRIBUTES] != "") { stringSplit(ciP->uriParam[URI_PARAM_ATTRIBUTES], ',', attrsFilter); } // Add special attributes representing entity dates if ((creDate != 0) && (ciP->uriParamOptions[DATE_CREATED] || (std::find(attrsFilter.begin(), attrsFilter.end(), DATE_CREATED) != attrsFilter.end()))) { ContextAttribute* caP = new ContextAttribute(DATE_CREATED, DATE_TYPE, creDate); attributeVector.push_back(caP); } if ((modDate != 0) && (ciP->uriParamOptions[DATE_MODIFIED] || (std::find(attrsFilter.begin(), attrsFilter.end(), DATE_MODIFIED) != attrsFilter.end()))) { ContextAttribute* caP = new ContextAttribute(DATE_MODIFIED, DATE_TYPE, modDate); attributeVector.push_back(caP); } if ((renderFormat == NGSI_V2_VALUES) || (renderFormat == NGSI_V2_UNIQUE_VALUES)) { out = "["; if (attributeVector.size() != 0) { out += attributeVector.toJson(renderFormat, attrsFilter, metadataFilter, false); } out += "]"; } else { out = "{"; if (renderId) { out += JSON_VALUE("id", id); out += ","; /* This is needed for entities coming from NGSIv1 (which allows empty or missing types) */ out += JSON_STR("type") + ":" + ((type != "")? JSON_STR(type) : JSON_STR(DEFAULT_ENTITY_TYPE)); } std::string attrsOut; if (attributeVector.size() != 0) { attrsOut += attributeVector.toJson(renderFormat, attrsFilter, metadataFilter, false); } // // Note that just attributeVector.size() != 0 (used in previous versions) cannot be used // as ciP->uriParam["attrs"] filter could remove all the attributes // if (attrsOut != "") { if (renderId) { out += "," + attrsOut; } else { out += attrsOut; } } out += "}"; } if (comma) { out += ","; } return out; } return oe.toJson(); }