JSONElement* JSONArray::find(std::string path) const{ auto item = JSONElement::find(path); if (!item || !*item){ int idx = path.find_first_of(".["); if (idx > 0){ auto key = path.substr(0, idx); if (key.compare("this") == 0){ return (JSONElement*)this; } else if (isPositiveInteger(key)){ JSONElement* elem; auto index = atoi(key.c_str()); elem = get(index); unsigned int nIdx = idx + (path[idx] == '.' ? 1 : 0); if (nIdx < path.length()){ return elem->find(path.substr(nIdx)); } return elem; } } else if (idx == 0 && path[0] == '.'){ return find(path.substr(1)); } else if (idx == 0 && path[0] == '['){ unsigned int eIdx = path.find_first_of(']', 1); if (eIdx > 0){ auto key = path.substr(1, eIdx - 1); if (isPositiveInteger(key)){ auto index = atoi(key.c_str()); auto elem = get(index); if (eIdx + 1 < path.length()){ return elem->find(path.substr(eIdx + 1)); } return elem; } } } else{ if (path.compare("this") == 0){ return (JSONElement*)this; } else{ if (isPositiveInteger(path)){ auto index = atoi(path.c_str()); return get(index); } } } } return item; }
std::string CborValue::inspect() const { if( isNull() ) { static std::string null = "(null)"; return null; } else if( isUndefined() ) { static std::string undefined = "(undefined)"; return undefined; } else if( isBool() ) { return boost::lexical_cast<std::string>(toBool()); } else if( isPositiveInteger() ) { return boost::lexical_cast<std::string>(toPositiveInteger()); } else if( isNegativeInteger() ) { std::string result = "-"; const char specialValue[] = "18446744073709551616"; // 0x10000000000000000 uint64_t value = toNegativeInteger(); if( value == 0 ) result += specialValue; else result += boost::lexical_cast<std::string>(value); return result; } else if( isDouble() ) { return boost::lexical_cast<std::string>(toDouble()); } else if( isString() ) { return toString(); } else if( isByteString() ) { std::vector<char> byteString = toByteString(); std::string result = "(0x"; static const char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; for(size_t i = 0; i < byteString.size(); ++i) { unsigned char c = static_cast<unsigned char>(byteString[i]); result += hex[c / sizeof(hex)]; result += hex[c % sizeof(hex)]; } result += ')'; return result; } else if( isArray() ) { std::vector<CborValue> values = toArray(); std::string result = "["; if( values.empty() == false ) { for(size_t i = 0; i < values.size(); ++i) { result += values[i].inspect(); result += ", "; } result.resize(result.size() - 1); result[result.size() - 1] = ']'; } else { result += ']'; } return result; } else if( isMap() ) { std::map<CborValue, CborValue> values = toMap(); std::string result = "{"; if( values.empty() == false ) { std::map<CborValue, CborValue>::iterator it = values.begin(); std::map<CborValue, CborValue>::iterator end = values.end(); for(; it != end; ++it) { result += it->first.inspect(); result += ": "; result += it->second.inspect(); result += ", "; } result.resize(result.size() - 1); result[result.size() - 1] = '}'; } else { result += '}'; } return result; } else if( isBigInteger() ) { BigInteger bigInteger = toBigInteger(); std::string result; if( bigInteger.positive ) result = "(big integer: 0x"; else result = "(negative big integer: 0x"; static const char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; if( bigInteger.bigint.empty() == false ) { for(size_t i = 0; i < bigInteger.bigint.size(); ++i) { unsigned char c = static_cast<unsigned char >(bigInteger.bigint[i]); result += hex[c / sizeof(hex)]; result += hex[c % sizeof(hex)]; } result.resize(result.size() - 1); result[result.size() - 1] = ')'; } else { result += ')'; } return result; } assert(false); std::string invalidType = "(invalid type)"; return invalidType; }