Add JsonObject constructor.

This commit is contained in:
levlam 2023-07-31 19:47:55 +03:00
parent a7b6f55d7a
commit dd68528ae4
2 changed files with 10 additions and 5 deletions

View File

@ -394,10 +394,10 @@ Result<JsonValue> do_json_decode(Parser &parser, int32 max_depth) {
case '{': { case '{': {
parser.skip('{'); parser.skip('{');
parser.skip_whitespaces(); parser.skip_whitespaces();
JsonObject res;
if (parser.try_skip('}')) { if (parser.try_skip('}')) {
return JsonValue::make_object(std::move(res)); return JsonValue::make_object(JsonObject());
} }
vector<std::pair<Slice, JsonValue>> field_values;
while (true) { while (true) {
if (parser.empty()) { if (parser.empty()) {
return Status::Error("Unexpected string end"); return Status::Error("Unexpected string end");
@ -408,7 +408,7 @@ Result<JsonValue> do_json_decode(Parser &parser, int32 max_depth) {
return Status::Error("':' expected"); return Status::Error("':' expected");
} }
TRY_RESULT(value, do_json_decode(parser, max_depth - 1)); TRY_RESULT(value, do_json_decode(parser, max_depth - 1));
res.field_values_.emplace_back(field, std::move(value)); field_values.emplace_back(field, std::move(value));
parser.skip_whitespaces(); parser.skip_whitespaces();
if (parser.try_skip('}')) { if (parser.try_skip('}')) {
@ -423,7 +423,7 @@ Result<JsonValue> do_json_decode(Parser &parser, int32 max_depth) {
} }
return Status::Error("Unexpected symbol while parsing JSON Object"); return Status::Error("Unexpected symbol while parsing JSON Object");
} }
return JsonValue::make_object(std::move(res)); return JsonValue::make_object(JsonObject(std::move(field_values)));
} }
case '-': case '-':
case '+': case '+':

View File

@ -460,7 +460,12 @@ class JsonObject {
const JsonValue *get_field(Slice name) const; const JsonValue *get_field(Slice name) const;
public: public:
vector<std::pair<MutableSlice, JsonValue>> field_values_; vector<std::pair<Slice, JsonValue>> field_values_;
JsonObject() = default;
explicit JsonObject(vector<std::pair<Slice, JsonValue>> &&field_values) : field_values_(std::move(field_values)) {
}
size_t field_count() const { size_t field_count() const {
return field_values_.size(); return field_values_.size();