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

I tried some change to dump() for [1,2,3...] #2584

Closed
taodm opened this issue Jan 13, 2021 · 3 comments
Closed

I tried some change to dump() for [1,2,3...] #2584

taodm opened this issue Jan 13, 2021 · 3 comments

Comments

@taodm
Copy link

taodm commented Jan 13, 2021

function dump under pretty print maybe not good enough,it will use too many lines.
I tried some change to dump(),maybe can be accepted.

when json j is [1,2,3...],use j.dump(4),get:
[
1,
2,
3,
...
]
I want get [1,2,3..] in one line
so, I changed here:
void dump(const BasicJsonType& val, const bool pretty_print,
const bool ensure_ascii,
const unsigned int indent_step,
const unsigned int current_indent = 0)
...
case value_t::array:
{
if (val.m_value.array->empty())
{
o->write_characters("[]", 2);
return;
}

            if (pretty_print)
            {
                auto subitem_type = val.m_value.array->cbegin()->m_type;
                bool full_pretty_print = subitem_type == value_t::array  ||
                                         subitem_type == value_t::object ||
                                         std::any_of(val.m_value.array->cbegin(), val.m_value.array->cend(),
                                                     [subitem_type](auto & subItem){return subItem.m_type != subitem_type;});

                size_t   elements_per_line  =  (subitem_type == value_t::string) ? 5 : 10;
                bool     more_than_one_line =  val.m_value.array->size() > elements_per_line;

                o->write_character('[');

                // variable to hold indentation for recursive calls
                const auto new_indent = current_indent + indent_step;
                if (JSON_UNLIKELY(indent_string.size() < new_indent))
                {
                    indent_string.resize(indent_string.size() * 2, ' ');
                }

                // first n-1 elements
                size_t element_count = 0;
                for (auto i = val.m_value.array->cbegin();
                        i != val.m_value.array->cend() - 1; ++i)
                {
                    if (full_pretty_print || (more_than_one_line && ((element_count++ % elements_per_line) == 0)))
                    {
                        o->write_character('\n');
                        o->write_characters(indent_string.c_str(), new_indent);
                    }

                    dump(*i, true, ensure_ascii, indent_step, new_indent);
                    o->write_character(',');
                }

                // last element
                assert(not val.m_value.array->empty());
                if (full_pretty_print || (more_than_one_line && ((element_count++ % elements_per_line) == 0)))
                {
                    o->write_character('\n');
                    o->write_characters(indent_string.c_str(), new_indent);
                }
                dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);

                if (full_pretty_print || more_than_one_line)
                {
                    o->write_character('\n');
                    o->write_characters(indent_string.c_str(), current_indent);
                }
                o->write_character(']');

...
now, when json j is [[1,2,3,4,5,6,7,8,9,10,11],[1,2,3,4,5,6,7,8,9,0],[1,2,3,4,5,6,7,8,9],["1",1]],
j.dump(4) will get:
[
[
1,2,3,4,5,6,7,8,9,10,
11
],
[1,2,3,4,5,6,7,8,9,0],
[1,2,3,4,5,6,7,8,9],
[
"1",
1
]
]

@nlohmann
Copy link
Owner

In order to asses your issue, we need the following information:

  • What is the issue you have?

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

  • What is the expected behavior?

  • And what is the actual behavior instead?

  • Which compiler and operating system are you using? Is it a supported compiler?

  • Did you use a released version of the library or the version from the develop branch?

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

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Jan 14, 2021
@taodm
Copy link
Author

taodm commented Jan 15, 2021

I think function dump(4), pretty print not good enougf, it will use too many lines for arry of int.
I want print 10 int_value in one line,like
[
1,2,3,4,5,6,7,8,9,10,
11
],
It will use less lines,and we can quickly count how many elements.
I've tried change the code.
May it can be accepted?

@nlohmann
Copy link
Owner

I am not sure that such a fix is helpful for everybody, and we have no real way to support all kinds of configurations at the moment. Best would be a way to implement user-defined serializers (maybe using the SAX interface, see #2275).

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

No branches or pull requests

2 participants