static StructuredData::ObjectSP ParseJSONObject(JSONParser &json_parser) { // The "JSONParser::Token::ObjectStart" token should have already been // consumed by the time this function is called auto dict_up = llvm::make_unique<StructuredData::Dictionary>(); std::string value; std::string key; while (1) { JSONParser::Token token = json_parser.GetToken(value); if (token == JSONParser::Token::String) { key.swap(value); token = json_parser.GetToken(value); if (token == JSONParser::Token::Colon) { StructuredData::ObjectSP value_sp = ParseJSONValue(json_parser); if (value_sp) dict_up->AddItem(key, value_sp); else break; } } else if (token == JSONParser::Token::ObjectEnd) { return StructuredData::ObjectSP(dict_up.release()); } else if (token == JSONParser::Token::Comma) { continue; } else { break; } } return StructuredData::ObjectSP(); }
static StructuredData::ObjectSP ParseJSONArray (JSONParser &json_parser) { // The "JSONParser::Token::ObjectStart" token should have already been consumed // by the time this function is called std::unique_ptr<StructuredData::Array> array_up(new StructuredData::Array()); std::string value; std::string key; while (1) { StructuredData::ObjectSP value_sp = ParseJSONValue(json_parser); if (value_sp) array_up->AddItem(value_sp); else break; JSONParser::Token token = json_parser.GetToken(value); if (token == JSONParser::Token::Comma) { continue; } else if (token == JSONParser::Token::ArrayEnd) { return StructuredData::ObjectSP(array_up.release()); } else { break; } } return StructuredData::ObjectSP(); }
static StructuredData::ObjectSP ParseJSONValue (JSONParser &json_parser) { std::string value; const JSONParser::Token token = json_parser.GetToken(value); switch (token) { case JSONParser::Token::ObjectStart: return ParseJSONObject(json_parser); case JSONParser::Token::ArrayStart: return ParseJSONArray(json_parser); case JSONParser::Token::Integer: { bool success = false; uint64_t uval = StringConvert::ToUInt64(value.c_str(), 0, 0, &success); if (success) return StructuredData::ObjectSP(new StructuredData::Integer(uval)); } break; case JSONParser::Token::Float: { bool success = false; double val = StringConvert::ToDouble(value.c_str(), 0.0, &success); if (success) return StructuredData::ObjectSP(new StructuredData::Float(val)); } break; case JSONParser::Token::String: return StructuredData::ObjectSP(new StructuredData::String(value)); case JSONParser::Token::True: case JSONParser::Token::False: return StructuredData::ObjectSP(new StructuredData::Boolean(token == JSONParser::Token::True)); case JSONParser::Token::Null: return StructuredData::ObjectSP(new StructuredData::Null()); default: break; } return StructuredData::ObjectSP(); }
static StructuredData::ObjectSP ParseJSONValue(JSONParser &json_parser) { std::string value; const JSONParser::Token token = json_parser.GetToken(value); switch (token) { case JSONParser::Token::ObjectStart: return ParseJSONObject(json_parser); case JSONParser::Token::ArrayStart: return ParseJSONArray(json_parser); case JSONParser::Token::Integer: { uint64_t uval; if (llvm::to_integer(value, uval, 0)) return std::make_shared<StructuredData::Integer>(uval); } break; case JSONParser::Token::Float: { double val; if (llvm::to_float(value, val)) return std::make_shared<StructuredData::Float>(val); } break; case JSONParser::Token::String: return std::make_shared<StructuredData::String>(value); case JSONParser::Token::True: case JSONParser::Token::False: return std::make_shared<StructuredData::Boolean>(token == JSONParser::Token::True); case JSONParser::Token::Null: return std::make_shared<StructuredData::Null>(); default: break; } return StructuredData::ObjectSP(); }