Skip to content

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

Closed

Description

@timmcd69

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;

Activity

  1. self-assigned this
    on May 7, 2017
  2. added this to the Release 3.0.0 milestone on May 7, 2017
  3. nlohmann commented on May 7, 2017

    @nlohmann
    Owner

    Done.

  4. pedro-vicente commented on Apr 21, 2021

    @pedro-vicente

    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

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions