🔨 simplified SAX-DOM parser

This commit is contained in:
Niels Lohmann 2018-03-06 07:19:05 +01:00
parent 5b9d03cfdb
commit faf2546a15
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
2 changed files with 14 additions and 10 deletions

View file

@ -176,7 +176,8 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
bool key(std::string&& val) override bool key(std::string&& val) override
{ {
last_key = val; // add null at given key and store the reference for later
object_element = &(ref_stack.back()->m_value.object->operator[](val));
return true; return true;
} }
@ -219,8 +220,8 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
BasicJsonType root; BasicJsonType root;
/// stack to model hierarchy of values /// stack to model hierarchy of values
std::vector<BasicJsonType*> ref_stack; std::vector<BasicJsonType*> ref_stack;
/// helper variable for object keys /// helper to hold the reference for the next object element
std::string last_key; BasicJsonType* object_element = nullptr;
/*! /*!
@invariant If the ref stack is empty, then the passed value will be the new @invariant If the ref stack is empty, then the passed value will be the new
@ -247,8 +248,9 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
} }
else else
{ {
BasicJsonType& r = ref_stack.back()->m_value.object->operator[](last_key) = BasicJsonType(std::forward<Value>(v)); assert(object_element);
return &r; *object_element = BasicJsonType(std::forward<Value>(v));
return object_element;
} }
} }
} }

View file

@ -3310,7 +3310,8 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
bool key(std::string&& val) override bool key(std::string&& val) override
{ {
last_key = val; // add null at given key and store the reference for later
object_element = &(ref_stack.back()->m_value.object->operator[](val));
return true; return true;
} }
@ -3353,8 +3354,8 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
BasicJsonType root; BasicJsonType root;
/// stack to model hierarchy of values /// stack to model hierarchy of values
std::vector<BasicJsonType*> ref_stack; std::vector<BasicJsonType*> ref_stack;
/// helper variable for object keys /// helper to hold the reference for the next object element
std::string last_key; BasicJsonType* object_element = nullptr;
/*! /*!
@invariant If the ref stack is empty, then the passed value will be the new @invariant If the ref stack is empty, then the passed value will be the new
@ -3381,8 +3382,9 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
} }
else else
{ {
BasicJsonType& r = ref_stack.back()->m_value.object->operator[](last_key) = BasicJsonType(std::forward<Value>(v)); assert(object_element);
return &r; *object_element = BasicJsonType(std::forward<Value>(v));
return object_element;
} }
} }
} }