JSON for Modern C++的dump函数的一个小修改:纯数值内容放在同一行

2023-05-16

对于[1,2,3...]这样的纯数值json,使用dump(4)输出会占用非常多的行:

[
    1,
    2,
    3,
。。。
]


我个人喜欢对这样的纯数值,只用一行,所以小改了一下源代码:

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(']');
。。。

现在,对于 [[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]],
会输出

[
    [
        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
    ]
]

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JSON for Modern C++的dump函数的一个小修改:纯数值内容放在同一行 的相关文章

随机推荐