Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.dump() not allowing compact form #2678

Closed
3 tasks
qikdauie opened this issue Mar 17, 2021 · 5 comments
Closed
3 tasks

.dump() not allowing compact form #2678

qikdauie opened this issue Mar 17, 2021 · 5 comments
Labels
kind: bug state: needs more info the author of the issue needs to provide more details

Comments

@qikdauie
Copy link

qikdauie commented Mar 17, 2021

When trying to use dump(-1) or dump(), it is producing the effects of dump(0)

Please describe the steps to reproduce the issue.

use dump(-1) or dump(), it will add newlines.

Can you provide a small but working code example?

json obj { {"a", "ayyy"}, {"b", "beee"} } std::cout << obj.dump(-1);
output:
{\n "a": "ayyy",\n "b": "beee"\n }\n

What is the expected behavior?

{"a": "ayyy","b": "beee"}

In json.hpp starting a line 2236, the conditions here do not allow for -1 mode.
if (indent >= 0) { s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent)); } else { s.dump(*this, false, ensure_ascii, 0); }

  • Compiler: cl
  • Operating system: mac

Which version of the library did you use?

  • [x ] latest release version 3.9.1
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • [ x] no - please copy/paste the error message below
@qikdauie
Copy link
Author

Im just going to alter my copy of the lib, but, was this disabled for some reason?

@qikdauie
Copy link
Author

if anyone is having this issue, a workaround is a compact function using regex. Here's mine if you're interested:

std::string CompactJsonString(std::string& json) { std::regex no_space("\\s(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); std::regex no_newline("\\s\n(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); // write the results to an output iterator json = std::regex_replace(json, no_space, ""); json = std::regex_replace(json, no_newline, ""); return json; }

@t-b
Copy link
Contributor

t-b commented Mar 19, 2021

@qikdauie Do the tests pass on your platform?

From the test code at

SECTION("no indent / indent=-1")
{
CHECK(j.dump() ==
"{\"array\":[1,2,3,4],\"boolean\":false,\"null\":null,\"number\":42,\"object\":{},\"string\":\"Hello world\"}");
CHECK(j.dump() == j.dump(-1));
}
it looks like dump does what you expect.

@qikdauie
Copy link
Author

@t-b on a deadline at the moment. Ill test asap and get back to the thread. Thanks for the response.

@nlohmann
Copy link
Owner

I cannot reproduce the issue. The code for serialization is unchanged for months and part of the unit tests. Please provide a small working example. Please also check if the test suite passes.

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Mar 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

3 participants