add dv::value support to json::parse
This commit is contained in:
parent
ceaa676a3a
commit
271db9a6f1
@ -23,6 +23,16 @@ public:
|
|||||||
std::unique_ptr<dynamic::Map> parse();
|
std::unique_ptr<dynamic::Map> parse();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ParserDV : BasicParser {
|
||||||
|
dv::value parseList();
|
||||||
|
dv::value parseObject();
|
||||||
|
dv::value parseValue();
|
||||||
|
public:
|
||||||
|
ParserDV(std::string_view filename, std::string_view source);
|
||||||
|
|
||||||
|
dv::value parse();
|
||||||
|
};
|
||||||
|
|
||||||
inline void newline(
|
inline void newline(
|
||||||
std::stringstream& ss, bool nice, uint indent, const std::string& indentstr
|
std::stringstream& ss, bool nice, uint indent, const std::string& indentstr
|
||||||
) {
|
) {
|
||||||
@ -362,7 +372,11 @@ std::unique_ptr<List> Parser::parseList() {
|
|||||||
Value Parser::parseValue() {
|
Value Parser::parseValue() {
|
||||||
char next = peek();
|
char next = peek();
|
||||||
if (next == '-' || next == '+' || is_digit(next)) {
|
if (next == '-' || next == '+' || is_digit(next)) {
|
||||||
return parseNumber();
|
auto numeric = parseNumber();
|
||||||
|
if (std::holds_alternative<integer_t>(numeric)) {
|
||||||
|
return std::get<integer_t>(numeric);
|
||||||
|
}
|
||||||
|
return std::get<number_t>(numeric);
|
||||||
}
|
}
|
||||||
if (is_identifier_start(next)) {
|
if (is_identifier_start(next)) {
|
||||||
std::string literal = parseName();
|
std::string literal = parseName();
|
||||||
@ -400,3 +414,112 @@ dynamic::Map_sptr json::parse(
|
|||||||
dynamic::Map_sptr json::parse(std::string_view source) {
|
dynamic::Map_sptr json::parse(std::string_view source) {
|
||||||
return parse("<string>", source);
|
return parse("<string>", source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ParserDV::ParserDV(std::string_view filename, std::string_view source)
|
||||||
|
: BasicParser(filename, source) {
|
||||||
|
}
|
||||||
|
|
||||||
|
dv::value ParserDV::parse() {
|
||||||
|
char next = peek();
|
||||||
|
if (next != '{') {
|
||||||
|
throw error("'{' expected");
|
||||||
|
}
|
||||||
|
return parseObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
dv::value ParserDV::parseObject() {
|
||||||
|
expect('{');
|
||||||
|
auto object = dv::object();
|
||||||
|
while (peek() != '}') {
|
||||||
|
if (peek() == '#') {
|
||||||
|
skipLine();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
std::string key = parseName();
|
||||||
|
char next = peek();
|
||||||
|
if (next != ':') {
|
||||||
|
throw error("':' expected");
|
||||||
|
}
|
||||||
|
pos++;
|
||||||
|
object[key] = parseValue();
|
||||||
|
next = peek();
|
||||||
|
if (next == ',') {
|
||||||
|
pos++;
|
||||||
|
} else if (next == '}') {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
throw error("',' expected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pos++;
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
dv::value ParserDV::parseList() {
|
||||||
|
expect('[');
|
||||||
|
auto list = dv::list();
|
||||||
|
while (peek() != ']') {
|
||||||
|
if (peek() == '#') {
|
||||||
|
skipLine();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
list.add(parseValue());
|
||||||
|
|
||||||
|
char next = peek();
|
||||||
|
if (next == ',') {
|
||||||
|
pos++;
|
||||||
|
} else if (next == ']') {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
throw error("',' expected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pos++;
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
dv::value ParserDV::parseValue() {
|
||||||
|
char next = peek();
|
||||||
|
if (next == '-' || next == '+' || is_digit(next)) {
|
||||||
|
auto numeric = parseNumber();
|
||||||
|
if (std::holds_alternative<integer_t>(numeric)) {
|
||||||
|
return std::get<integer_t>(numeric);
|
||||||
|
}
|
||||||
|
return std::get<number_t>(numeric);
|
||||||
|
}
|
||||||
|
if (is_identifier_start(next)) {
|
||||||
|
std::string literal = parseName();
|
||||||
|
if (literal == "true") {
|
||||||
|
return true;
|
||||||
|
} else if (literal == "false") {
|
||||||
|
return false;
|
||||||
|
} else if (literal == "inf") {
|
||||||
|
return INFINITY;
|
||||||
|
} else if (literal == "nan") {
|
||||||
|
return NAN;
|
||||||
|
}
|
||||||
|
throw error("invalid literal ");
|
||||||
|
}
|
||||||
|
if (next == '{') {
|
||||||
|
return parseObject();
|
||||||
|
}
|
||||||
|
if (next == '[') {
|
||||||
|
return parseList();
|
||||||
|
}
|
||||||
|
if (next == '"' || next == '\'') {
|
||||||
|
pos++;
|
||||||
|
return parseString(next);
|
||||||
|
}
|
||||||
|
throw error("unexpected character '" + std::string({next}) + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
dv::value json::parseDV(
|
||||||
|
std::string_view filename, std::string_view source
|
||||||
|
) {
|
||||||
|
ParserDV parser(filename, source);
|
||||||
|
return parser.parse();
|
||||||
|
}
|
||||||
|
|
||||||
|
dv::value json::parseDV(std::string_view source) {
|
||||||
|
return parseDV("<string>", source);
|
||||||
|
}
|
||||||
|
|||||||
@ -11,6 +11,9 @@ namespace json {
|
|||||||
dynamic::Map_sptr parse(std::string_view filename, std::string_view source);
|
dynamic::Map_sptr parse(std::string_view filename, std::string_view source);
|
||||||
dynamic::Map_sptr parse(std::string_view source);
|
dynamic::Map_sptr parse(std::string_view source);
|
||||||
|
|
||||||
|
dv::value parseDV(std::string_view filename, std::string_view source);
|
||||||
|
dv::value parseDV(std::string_view source);
|
||||||
|
|
||||||
std::string stringify(
|
std::string stringify(
|
||||||
const dynamic::Map* obj, bool nice, const std::string& indent
|
const dynamic::Map* obj, bool nice, const std::string& indent
|
||||||
);
|
);
|
||||||
|
|||||||
@ -50,20 +50,20 @@ TEST(JSON, EncodeDecodeDV) {
|
|||||||
|
|
||||||
std::string text;
|
std::string text;
|
||||||
{
|
{
|
||||||
auto map = dv::object();
|
auto object = dv::object();
|
||||||
map["name"] = name;
|
object["name"] = name;
|
||||||
map["year"] = year;
|
object["year"] = year;
|
||||||
map["score"] = score;
|
object["score"] = score;
|
||||||
map["data"] = srcBytes;
|
object["data"] = srcBytes;
|
||||||
|
|
||||||
text = json::stringifyDV(map, false, "");
|
text = json::stringifyDV(object, false, "");
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
auto map = json::parse(text);
|
auto object = json::parseDV(text);
|
||||||
EXPECT_EQ(map->get<std::string>("name"), name);
|
EXPECT_EQ(object["name"].asString(), name);
|
||||||
EXPECT_EQ(map->get<integer_t>("year"), year);
|
EXPECT_EQ(object["year"].asInteger(), year);
|
||||||
EXPECT_FLOAT_EQ(map->get<number_t>("score"), score);
|
EXPECT_FLOAT_EQ(object["score"].asNumber(), score);
|
||||||
auto b64string = map->get<std::string>("data");
|
auto b64string = object["data"].asString();
|
||||||
|
|
||||||
auto bytes = util::base64_decode(b64string);
|
auto bytes = util::base64_decode(b64string);
|
||||||
EXPECT_EQ(bytes.size(), bytesSize);
|
EXPECT_EQ(bytes.size(), bytesSize);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user