No trailing space for BuiltStyledStreamWriter#1154
No trailing space for BuiltStyledStreamWriter#1154innerlee wants to merge 9 commits intoopen-source-parsers:masterfrom
Conversation
Signed-off-by: lizz <lizz@sensetime.com>
|
The |
BillyDonahue
left a comment
There was a problem hiding this comment.
I've made some suggestions in this patch.
You should be able to merge this change into your PR branch.
src/lib_json/json_writer.cpp
Outdated
| name.data(), static_cast<unsigned>(name.length()), emitUTF8_)); | ||
| *sout_ << colonSymbol_; | ||
| if ((childValue.type() == objectValue && | ||
| !childValue.getMemberNames().empty()) || |
There was a problem hiding this comment.
getMemberNames() returns a full std::vector. It's expensive.
All we do is see if it's empty and throw it away. There's a better way to test this condition.
you can test childValue.empty() directly.
Can you explain the rest of this conditional expression though?
I'm a little concerned about how it interacts with comments.
Let's add a test for that.
There was a problem hiding this comment.
The first part deals with objects and the second part deals with list.
The conditions are copied from
https://github.com/open-source-parsers/jsoncpp/blob/master/src/lib_json/json_writer.cpp#L697-L699
and
https://github.com/open-source-parsers/jsoncpp/blob/master/src/lib_json/json_writer.cpp#L1002-L1006
, respectively.
There was a problem hiding this comment.
Regarding the interaction with comments, I'm not very clear. Could you please add them if appropriate?
src/lib_json/json_writer.cpp
Outdated
| ((cs_ == CommentStyle::All) || isMultilineArray(childValue)))) | ||
| *sout_ << colonSymbolNoTrailingSpace_; | ||
| else | ||
| *sout_ << colonSymbol_; |
There was a problem hiding this comment.
We should be able to just write a part of the colonSymbol_ range here instead of having another member variable to hold a copy with the space cut off.
src/lib_json/json_writer.cpp
Outdated
| useSpecialFloats_(useSpecialFloats), emitUTF8_(emitUTF8), | ||
| precision_(precision), precisionType_(precisionType) {} | ||
| precision_(precision), precisionType_(precisionType) { | ||
| if (colonSymbol_[colonSymbol_.size() - 1] == ' ') { |
There was a problem hiding this comment.
if colonSymbol_ is empty this is undefined behavior. Shouldn't need this new member anyway.
Can use find_last_not_of(' ') or something.
|
Good idea, but there is a much simpler way to do this. Let's treat the "trailing colon" and "embedded colon" as different symbols, and let's inject Features add UI complexity, but they are transparent. As a user, I would prefer transparency and control. |
|
The trailing space is a kind of bug imho. It does not related to yaml-mode or indentation. The extra trailing spaces does not have any functionality and they are not part of any standard. Removing them is purely for aesthetic reasons so I'm not sure if exposing them as new "features" is worth the mental effort. |
First, I don't think the JSON spec calls that a bug. I respect your opinion, but we need to concentrate on engineering here. Let's just consider this a reasonable preference that you have. Second, there are good coders in the world who write actual regression tests, and I respect them as well. It is reasonable for those people to record the exact output in their test. You're giving yourself the right to change their output. I have seen Issues filed over changes in the past. Third, to the extent that you are right, we should not even support indentation or spacing. You're treating JSON as a human-readable format, but that is not part of the spec either. My life would be easier if we compressed whitespace. I honestly do not understand your point of view anymore. You think the whitespace is significant, but you don't want a user to have control over it? How is that logically consistent? I don't know the right thing to do here. Certainly, there are always trade-offs, but that's not what you're saying at all. You assert that this is a bug of a sort, but what about its prevalence? Are you familiar with the standard Python
Like you, I have always been bothered by that. I use that all the time for work. When a co-worker used What would you think of the Python solution? In other words, expose the |
|
Hi I haven't used python's json very much. Could you give an example that python's json.dump produce trailing spaces? My tries: In [7]: json.dumps(json.loads(a), indent=2, separators=(',', ': '))
Out[7]: '[\n {\n "headshoulders_info": [\n {\n "det_score": 0.731445,\n "rect": {\n "bottom": 205,\n "left": 200,\n "right": 309,\n "top": 102\n }\n }\n ],\n "image": {\n "height": 368,\n "id": 0,\n "uuid": "/workspace/face/data/shot_0001/img_00000.jpg",\n "width": 640\n },\n "status": 0\n }\n]'
In [8]: json.dumps(json.loads(a), indent=2, separators=(',', ' : '))
Out[8]: '[\n {\n "headshoulders_info" : [\n {\n "det_score" : 0.731445,\n "rect" : {\n "bottom" : 205,\n "left" : 200,\n "right" : 309,\n "top" : 102\n }\n }\n ],\n "image" : {\n "height" : 368,\n "id" : 0,\n "uuid" : "/workspace/face/data/shot_0001/img_00000.jpg",\n "width" : 640\n },\n "status" : 0\n }\n]'Note that there are no spaces before any of |
|
Oh, you're right, because Python puts Here is why I had a problem at work: My co-worker specified Now that I recall, my problem at work with was trailing whitespace after commas, which happens if you want the whitespace for short lists. It's essentially the same problem. And oddly, when I try it now, I find that there is a difference between python2 and python3.... ... Aha!
So this was "fixed" in python3. Note the major-version bump. And I'm not kidding about that. Guido threw a ton of changes into python3, knowing that a major-version bump does not happen often. But you are not arguing for a major-version bump of jsoncpp, right? And yet you also argue against adding a "feature". Nobody likes the "features". Everybody thinks they add too much complexity. But I observe that everybody also thinks backward-compatibility is somebody else's problem. |
|
Anyway, maybe the best solution is to add a feature to keep |
|
I understand the argument for compatibility. If changing test case is considered breaking, then it is better wait for the next major revision. Some background info: For me, it is purely for aesthetic reasons. I accidentally used jsoncpp for some output and noticed this little annoyance. My wish is to let it have a clean output :) |
|
Yes, we should re-open this when we bump the major version someday. Thanks for the suggestions. |
|
Hi this is also an issue to us. For now we have a script that removes them but it would be nice to get rid of that script. Please add a version of this when you bump to version 2. |
…9836) The maintainers of jsoncpp chose to decline open-source-parsers/jsoncpp#1154 until they make a new major release. We are not burdened by that requirement. Closes #10887 Refs #19834


Before pr, the positions with
<-HEREannotation has a trailing space:[ { "headshoulders_info" : <-HERE [ { "det_score" : 0.73144500000000001, "rect" : <-HERE { "bottom" : 205, "left" : 200, "right" : 309, "top" : 102 } } ], "image" : <-HERE { "height" : 368, "id" : 0, "uuid" : "/workspace/face/data/shot_0001/img_00000.jpg", "width" : 640 }, "status" : 0 } ]After:
[ { "headshoulders_info" : [ { "det_score" : 0.73144500000000001, "rect" : { "bottom" : 205, "left" : 200, "right" : 309, "top" : 102 } } ], "image" : { "height" : 368, "id" : 0, "uuid" : "/workspace/face/data/shot_0001/img_00000.jpg", "width" : 640 }, "status" : 0 } ]code: