migrate from dynamic::Value to dv::value & total erase namespace 'dynamic'
This commit is contained in:
+12
-271
@@ -6,29 +6,17 @@
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
|
||||
#include "data/dynamic.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "commons.hpp"
|
||||
|
||||
using namespace json;
|
||||
using namespace dynamic;
|
||||
|
||||
class Parser : BasicParser {
|
||||
std::unique_ptr<dynamic::List> parseList();
|
||||
std::unique_ptr<dynamic::Map> parseObject();
|
||||
dynamic::Value parseValue();
|
||||
public:
|
||||
Parser(std::string_view filename, std::string_view source);
|
||||
|
||||
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);
|
||||
Parser(std::string_view filename, std::string_view source);
|
||||
|
||||
dv::value parse();
|
||||
};
|
||||
@@ -46,22 +34,6 @@ inline void newline(
|
||||
}
|
||||
}
|
||||
|
||||
void stringifyObj(
|
||||
const Map* obj,
|
||||
std::stringstream& ss,
|
||||
int indent,
|
||||
const std::string& indentstr,
|
||||
bool nice
|
||||
);
|
||||
|
||||
void stringifyArr(
|
||||
const List* list,
|
||||
std::stringstream& ss,
|
||||
int indent,
|
||||
const std::string& indentstr,
|
||||
bool nice
|
||||
);
|
||||
|
||||
void stringifyObj(
|
||||
const dv::value& obj,
|
||||
std::stringstream& ss,
|
||||
@@ -78,34 +50,6 @@ void stringifyList(
|
||||
bool nice
|
||||
);
|
||||
|
||||
void stringifyValue(
|
||||
const Value& value,
|
||||
std::stringstream& ss,
|
||||
int indent,
|
||||
const std::string& indentstr,
|
||||
bool nice
|
||||
) {
|
||||
if (auto map = std::get_if<Map_sptr>(&value)) {
|
||||
stringifyObj(map->get(), ss, indent, indentstr, nice);
|
||||
} else if (auto listptr = std::get_if<List_sptr>(&value)) {
|
||||
stringifyArr(listptr->get(), ss, indent, indentstr, nice);
|
||||
} else if (auto bytesptr = std::get_if<ByteBuffer_sptr>(&value)) {
|
||||
auto bytes = bytesptr->get();
|
||||
ss << "\"" << util::base64_encode(bytes->data(), bytes->size());
|
||||
ss << "\"";
|
||||
} else if (auto flag = std::get_if<bool>(&value)) {
|
||||
ss << (*flag ? "true" : "false");
|
||||
} else if (auto num = std::get_if<number_t>(&value)) {
|
||||
ss << std::setprecision(15) << *num;
|
||||
} else if (auto num = std::get_if<integer_t>(&value)) {
|
||||
ss << *num;
|
||||
} else if (auto str = std::get_if<std::string>(&value)) {
|
||||
ss << util::escape(*str);
|
||||
} else {
|
||||
ss << "null";
|
||||
}
|
||||
}
|
||||
|
||||
void stringifyValue(
|
||||
const dv::value& value,
|
||||
std::stringstream& ss,
|
||||
@@ -146,74 +90,6 @@ void stringifyValue(
|
||||
}
|
||||
}
|
||||
|
||||
void stringifyArr(
|
||||
const List* list,
|
||||
std::stringstream& ss,
|
||||
int indent,
|
||||
const std::string& indentstr,
|
||||
bool nice
|
||||
) {
|
||||
if (list == nullptr) {
|
||||
ss << "nullptr";
|
||||
return;
|
||||
}
|
||||
if (list->values.empty()) {
|
||||
ss << "[]";
|
||||
return;
|
||||
}
|
||||
ss << "[";
|
||||
for (size_t i = 0; i < list->size(); i++) {
|
||||
if (i > 0 || nice) {
|
||||
newline(ss, nice, indent, indentstr);
|
||||
}
|
||||
const Value& value = list->values[i];
|
||||
stringifyValue(value, ss, indent + 1, indentstr, nice);
|
||||
if (i + 1 < list->size()) {
|
||||
ss << ',';
|
||||
}
|
||||
}
|
||||
if (nice) {
|
||||
newline(ss, true, indent - 1, indentstr);
|
||||
}
|
||||
ss << ']';
|
||||
}
|
||||
|
||||
void stringifyObj(
|
||||
const Map* obj,
|
||||
std::stringstream& ss,
|
||||
int indent,
|
||||
const std::string& indentstr,
|
||||
bool nice
|
||||
) {
|
||||
if (obj == nullptr) {
|
||||
ss << "nullptr";
|
||||
return;
|
||||
}
|
||||
if (obj->values.empty()) {
|
||||
ss << "{}";
|
||||
return;
|
||||
}
|
||||
ss << "{";
|
||||
size_t index = 0;
|
||||
for (auto& entry : obj->values) {
|
||||
const std::string& key = entry.first;
|
||||
if (index > 0 || nice) {
|
||||
newline(ss, nice, indent, indentstr);
|
||||
}
|
||||
const Value& value = entry.second;
|
||||
ss << util::escape(key) << ": ";
|
||||
stringifyValue(value, ss, indent + 1, indentstr, nice);
|
||||
index++;
|
||||
if (index < obj->values.size()) {
|
||||
ss << ',';
|
||||
}
|
||||
}
|
||||
if (nice) {
|
||||
newline(ss, true, indent - 1, indentstr);
|
||||
}
|
||||
ss << '}';
|
||||
}
|
||||
|
||||
void stringifyList(
|
||||
const dv::value& list,
|
||||
std::stringstream& ss,
|
||||
@@ -273,30 +149,6 @@ void stringifyObj(
|
||||
}
|
||||
|
||||
std::string json::stringify(
|
||||
const Map* obj, bool nice, const std::string& indent
|
||||
) {
|
||||
std::stringstream ss;
|
||||
stringifyObj(obj, ss, 1, indent, nice);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string json::stringify(
|
||||
const dynamic::List* arr, bool nice, const std::string& indent
|
||||
) {
|
||||
std::stringstream ss;
|
||||
stringifyArr(arr, ss, 1, indent, nice);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string json::stringify(
|
||||
const dynamic::Value& value, bool nice, const std::string& indent
|
||||
) {
|
||||
std::stringstream ss;
|
||||
stringifyValue(value, ss, 1, indent, nice);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string json::stringifyDV(
|
||||
const dv::value& value, bool nice, const std::string& indent
|
||||
) {
|
||||
std::stringstream ss;
|
||||
@@ -308,7 +160,7 @@ Parser::Parser(std::string_view filename, std::string_view source)
|
||||
: BasicParser(filename, source) {
|
||||
}
|
||||
|
||||
std::unique_ptr<Map> Parser::parse() {
|
||||
dv::value Parser::parse() {
|
||||
char next = peek();
|
||||
if (next != '{') {
|
||||
throw error("'{' expected");
|
||||
@@ -316,118 +168,7 @@ std::unique_ptr<Map> Parser::parse() {
|
||||
return parseObject();
|
||||
}
|
||||
|
||||
std::unique_ptr<Map> Parser::parseObject() {
|
||||
expect('{');
|
||||
auto obj = std::make_unique<Map>();
|
||||
auto& map = obj->values;
|
||||
while (peek() != '}') {
|
||||
if (peek() == '#') {
|
||||
skipLine();
|
||||
continue;
|
||||
}
|
||||
std::string key = parseName();
|
||||
char next = peek();
|
||||
if (next != ':') {
|
||||
throw error("':' expected");
|
||||
}
|
||||
pos++;
|
||||
map.insert(std::make_pair(key, parseValue()));
|
||||
next = peek();
|
||||
if (next == ',') {
|
||||
pos++;
|
||||
} else if (next == '}') {
|
||||
break;
|
||||
} else {
|
||||
throw error("',' expected");
|
||||
}
|
||||
}
|
||||
pos++;
|
||||
return obj;
|
||||
}
|
||||
|
||||
std::unique_ptr<List> Parser::parseList() {
|
||||
expect('[');
|
||||
auto arr = std::make_unique<List>();
|
||||
auto& values = arr->values;
|
||||
while (peek() != ']') {
|
||||
if (peek() == '#') {
|
||||
skipLine();
|
||||
continue;
|
||||
}
|
||||
values.push_back(parseValue());
|
||||
|
||||
char next = peek();
|
||||
if (next == ',') {
|
||||
pos++;
|
||||
} else if (next == ']') {
|
||||
break;
|
||||
} else {
|
||||
throw error("',' expected");
|
||||
}
|
||||
}
|
||||
pos++;
|
||||
return arr;
|
||||
}
|
||||
|
||||
Value Parser::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 Map_sptr(parseObject().release());
|
||||
}
|
||||
if (next == '[') {
|
||||
return List_sptr(parseList().release());
|
||||
}
|
||||
if (next == '"' || next == '\'') {
|
||||
pos++;
|
||||
return parseString(next);
|
||||
}
|
||||
throw error("unexpected character '" + std::string({next}) + "'");
|
||||
}
|
||||
|
||||
dynamic::Map_sptr json::parse(
|
||||
std::string_view filename, std::string_view source
|
||||
) {
|
||||
Parser parser(filename, source);
|
||||
return parser.parse();
|
||||
}
|
||||
|
||||
dynamic::Map_sptr json::parse(std::string_view 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() {
|
||||
dv::value Parser::parseObject() {
|
||||
expect('{');
|
||||
auto object = dv::object();
|
||||
while (peek() != '}') {
|
||||
@@ -455,7 +196,7 @@ dv::value ParserDV::parseObject() {
|
||||
return object;
|
||||
}
|
||||
|
||||
dv::value ParserDV::parseList() {
|
||||
dv::value Parser::parseList() {
|
||||
expect('[');
|
||||
auto list = dv::list();
|
||||
while (peek() != ']') {
|
||||
@@ -478,14 +219,14 @@ dv::value ParserDV::parseList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
dv::value ParserDV::parseValue() {
|
||||
dv::value Parser::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);
|
||||
if (numeric.isInteger()) {
|
||||
return numeric.asInteger();
|
||||
}
|
||||
return std::get<number_t>(numeric);
|
||||
return numeric.asNumber();
|
||||
}
|
||||
if (is_identifier_start(next)) {
|
||||
std::string literal = parseName();
|
||||
@@ -513,13 +254,13 @@ dv::value ParserDV::parseValue() {
|
||||
throw error("unexpected character '" + std::string({next}) + "'");
|
||||
}
|
||||
|
||||
dv::value json::parseDV(
|
||||
dv::value json::parse(
|
||||
std::string_view filename, std::string_view source
|
||||
) {
|
||||
ParserDV parser(filename, source);
|
||||
Parser parser(filename, source);
|
||||
return parser.parse();
|
||||
}
|
||||
|
||||
dv::value json::parseDV(std::string_view source) {
|
||||
return parseDV("<string>", source);
|
||||
dv::value json::parse(std::string_view source) {
|
||||
return parse("<string>", source);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user