diff options
author | Milo Yip <miloyip@gmail.com> | 2015-07-20 04:31:24 +0300 |
---|---|---|
committer | Milo Yip <miloyip@gmail.com> | 2015-07-20 04:31:24 +0300 |
commit | 9b2810701623e970e03c32510ea495266738900e (patch) | |
tree | 859e034cb0645f60a3441f00a44a5f2fb9ff4a57 /doc | |
parent | f431aaff9dd9fd2f167b2a68d3a1f2e9666b6265 (diff) | |
parent | 5ac04cb0123104c3cd5afc0bec6941ae149b2725 (diff) |
Merge pull request #373 from mloskot/faq-issue-366
Add to FAQ: How to insert a document node into another document?
Diffstat (limited to 'doc')
-rw-r--r-- | doc/faq.md | 43 |
1 files changed, 41 insertions, 2 deletions
@@ -104,7 +104,7 @@ 8. How to clear-and-minimize a document or value? -* Call one of the `SetXXX()` methods - they call destructor which deallocates DOM data: + Call one of the `SetXXX()` methods - they call destructor which deallocates DOM data: ``` Document d; @@ -112,7 +112,7 @@ d.SetObject(); // clear and minimize ``` -* Alternatively, use equivalent of the [C++ swap with temporary idiom](https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Clear-and-minimize): + Alternatively, use equivalent of the [C++ swap with temporary idiom](https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Clear-and-minimize): ``` Value(kObjectType).Swap(d); ``` @@ -121,6 +121,45 @@ d.Swap(Value(kObjectType).Move()); ``` +9. How to insert a document node into another document? + + Let's take the following two DOM trees represented as JSON documents: + ``` + Document person; + person.Parse("{\"person\":{\"name\":{\"first\":\"Adam\",\"last\":\"Thomas\"}}}"); + + Document address; + address.Parse("{\"address\":{\"city\":\"Moscow\",\"street\":\"Quiet\"}}"); + ``` + Let's assume we want to merge them in such way that the whole `address` document becomes a node of the `person`: + ``` + { "person": { + "name": { "first": "Adam", "last": "Thomas" }, + "address": { "city": "Moscow", "street": "Quiet" } + } + } + ``` + + The most important requirement to take care of document and value life-cycle as well as consistent memory managent using the right allocator during the value transfer. + + Simple yet most efficient way to achieve that is to modify the `address` definition above to initialize it with allocator of the `person` document, then we just add the root nenber of the value: + ``` + Documnet address(person.GetAllocator()); + ... + person["person"].AddMember("address", address["address"], person.GetAllocator()); + ``` +Alternatively, if we don't want to explicitly refer to the root value of `address` by name, we can refer to it via iterator: + ``` + auto addressRoot = address.MemberBegin(); + person["person"].AddMember(addressRoot->name, addressRoot->value, person.GetAllocator()); + ``` + + Second way is to deep-clone the value from the address document: + ``` + Value addressValue = Value(address["address"], person.GetAllocator()); + person["person"].AddMember("address", addressValue, person.GetAllocator()); + ``` + ## Document/Value (DOM) 1. What is move semantics? Why? |