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

Suggestion: Support tabs for indentation when serializing to stream. #520

Closed
timmcd69 opened this issue Mar 17, 2017 · 2 comments
Closed
Assignees
Labels
kind: enhancement/improvement solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@timmcd69
Copy link

With very slight modification to json.hpp, it can support setting the fill char on the output stream to '\t', and then using this fill char as the indent char instead of space. It works well, for example:

nlohman::json jsonval(...);
std::ofstream ofs(path);
ofs << std::setfill('\t') << std::setw(1) << jsonval;

@nlohmann nlohmann self-assigned this May 7, 2017
@nlohmann nlohmann added this to the Release 3.0.0 milestone May 7, 2017
nlohmann added a commit that referenced this issue May 7, 2017
An optional parameter for dump() allows to set the character to use for
indentation (default: space). In case a JSON value is serialized to an
output stream, its fill character is used (and can be set with
std::setfill).
@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label May 7, 2017
@nlohmann
Copy link
Owner

nlohmann commented May 7, 2017

Done.

@nlohmann nlohmann closed this as completed May 7, 2017
@pedro-vicente
Copy link

you can also use this method for space indentation , same as saving the string in dump(2)

#include "nlohmann/json.hpp"
#include <fstream>
#include <iostream>
#include <iomanip>
using json = nlohmann::json;

void main()
{
  json j;
  j["pi"] = 3.141;
  std::string s = j.dump(2);
  std::ofstream file("key.json");
  file << std::setfill(' ') << std::setw(2) << j;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants