Comments (10)
So assigning a node/branch of a tree nullifies the whole tree? That's going to
surprise a lot of people. Maybe there should be an option to disallow
assignment with move?
Original comment by [email protected]
on 9 Feb 2012 at 3:53
from rapidjson.
So let's assume you have a json document object A, and you assign B = A, where
B is another json document object.
Well, in the source at line 160 of document.h,
//! Assignment with move semantics.
/*! \param rhs Source of the assignment. It will become a null value after assignment.
*/
GenericValue& operator=(GenericValue& rhs) {
RAPIDJSON_ASSERT(this != &rhs);
this->~GenericValue();
memcpy(this, &rhs, sizeof(GenericValue));
rhs.flags_ = kNullFlag; //!! makes A null, in assignment B = A
return *this;
}
So, in my source, I commented out the line
rhs.flags_ = kNullFlag;
And that actually works _for now_. It doesn't seem to have caused any
problems, but I can't be sure if there are negative side effects.
And yes, if you have a tree like:
FILE: objects.json:
{
"fruits":{
"oranges":4,
"bananas":2,
"apples":3
},
"vegs":{
"beans":8,
"peas":20
}
}
And you do something like:
rapidjson::Document jsonDoc = load( "objects.json" ) ; // assume it loads the file
Then you later do:
rapidjson::Value fruits = jsonDoc[ "fruits" ] ;
// jsonDoc now NULL, SURPRISE!
rapidjson::Value vegs = jsonDoc[ "veg" ] ; // ERROR
Problem is, api doesn't seem to expose _any_ easy way to walk the document tree
when there are nested objects.
Original comment by [email protected]
on 9 Feb 2012 at 7:09
from rapidjson.
When document/value are traversed, it should be probably use references instead
of new objects, as in tutorial.cpp:
const Value& a = document["a"]; // Using a reference for consecutive access is handy and faster.
For your change:
rhs.flags_ = kNullFlag;
This will not work appropriately if rhs is an array or an object. This makes
two values shares the same array/object. And it will even crash if the
allocator is not MemoryPoolAllocator (the array/object will be destroy twice).
The rationale behind is that cloning nodes can be expensive (a deep clone of a
DOM tree/sub-tree) and this operation should not be common in
traversing/modifying a json DOM, so that the behavior of assignment uses move
semantics by design.
I think more examples can be added to help user understand this behavior at the
mean time.
In future, a Clone() function can be added to let user explicitly cloning a DOM
tree/sub-tree.
Thank you for your comment.
Original comment by [email protected]
on 19 Feb 2012 at 4:00
from rapidjson.
So I think it's a hole in the API. You shouldn't allow programmers to shoot
themselves in the foot so easily.
Perhaps return a pointer from operator[] instead?
Original comment by [email protected]
on 19 Feb 2012 at 4:38
from rapidjson.
Need to revise this API. Planned for 0.2 version.
Original comment by [email protected]
on 14 Nov 2012 at 3:45
- Changed state: Accepted
- Added labels: Priority-High
- Removed labels: Priority-Medium
from rapidjson.
It seems like supporting swap would be a good way of dealing with not
supporting assignment. Prior to C++11, the moral equivalent of assignment with
move (WAT!) is swap.
With that in mind, can swap be implemented outside of document?
Original comment by [email protected]
on 8 Aug 2013 at 8:57
from rapidjson.
For the record: There is an implementation proposal for adding an explicit
"copy constructor" and a "CopyFrom" function in my personal fork a Github [1].
As an answer to #6: Yes, a swap function can already be implemented easily
outside of GenericValue/GenericDocument:
template<typename Encoding, typename Allocator>
void swap(GenericValue<Encoding,Allocator> & v1, GenericValue<Encoding,Allocator> & v2) {
GenericValue<Encoding,Allocator> tmp;
tmp = v2;
v2 = v1;
v1 = tmp;
}
Description of the pull-request:
To allow deep copying from an existing GenericValue, an explicit "copy
constructor" (with required Allocator param) and a CopyFrom assignment function
are added.
Document d; Document::AllocatorType& a = d.GetAllocator();
Value v1("foo");
// Value v2(v1); // not allowed
Value v2(v1,a); // make a copy
RAPIDJSON_ASSERT(v1.IsString()); // v1 untouched
d.SetArray().PushBack(v1,a).PushBack(v2,a);
RAPIDJSON_ASSERT(v1.Empty() && v2.Empty());
v2.CopyFrom(d,a); // copy whole document
RAPIDJSON_ASSERT(d.IsArray() && d.Size()); // d untouched
v1.SetObject().AddMember( "array", v2, a );
d.PushBack(v1,a);
The strings in the source value are copied, if and only if they have been
allocated as a copy during their construction (determined by kCopyFlag). This
is needed to avoid double free()s or problems with differing lifetimes of the
allocated string storage.
Additionally, the Handler implementation in GenericDocument is made private
again, restricting access to GenericReader and GenericValue.
Changes: https://github.com/pah/rapidjson/compare/svn/trunk...value-copy-from
[1]: GenericValue: add copy constructor and CopyFrom
https://github.com/pah/rapidjson/pull/2
Original comment by [email protected]
on 7 Apr 2014 at 12:14
from rapidjson.
Original comment by [email protected]
on 24 Jun 2014 at 2:15
- Added labels: Type-Enhancement
- Removed labels: Type-Defect
from rapidjson.
Milo, please give me some time to open official pull-requests at GitHub before
adding the patches pasted here (and to the other issues). It would be nice to
keep the correct author attribution in the revision history. Thanks!
Original comment by [email protected]
on 24 Jun 2014 at 2:21
from rapidjson.
https://github.com/miloyip/rapidjson/pull/20
Original comment by [email protected]
on 26 Jun 2014 at 2:39
- Changed state: Fixed
from rapidjson.
Related Issues (20)
- Fix of some clang warnings HOT 3
- Parsing crash HOT 2
- SIMD instruction can cause seg-fault/UMR HOT 6
- Adding elements to an array HOT 5
- Patches from GitHub HOT 4
- GenericDocument::ParseStream: make SourceEncoding optional HOT 1
- rapidjson accessing memory it doesn't own HOT 4
- Memory access error due to 'memcmp' HOT 4
- Document leaks memory when stored as Value. HOT 4
- gcc-4.9.0 compile warnings HOT 1
- 'Convert JSON document to string' PROBLEM solved... HOT 1
- Error compiling on OS X 10.6 HOT 4
- Compilation error on gcc 3.4.6 HOT 1
- [address alignment issue]tutorial.cpp isn't working on HP-UX 11.23 ia64 HOT 5
- Blaireaux HOT 1
- Cannot parse min normal positive double
- reader.h compiler errors clang (use 'template' keyword to treat 'Stack' as a dependent template name) HOT 2
- Failed to parse nested json buffer HOT 1
- lacks ending quotation before the end of string HOT 1
- Problem with FindMeber HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rapidjson.