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

Json dump #1768

Closed
dibet opened this issue Sep 30, 2019 · 5 comments
Closed

Json dump #1768

dibet opened this issue Sep 30, 2019 · 5 comments
Labels
kind: enhancement/improvement solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)

Comments

@dibet
Copy link

dibet commented Sep 30, 2019

  • Describe the feature in as much detail as possible.
    Any way that dump method dont show empty objects
  • Include sample usage where appropriate.
    { "a": { "b": 1}, "c": {} }
    o result should be
    { "a": { "b": 1} }
@jaredgrubb
Copy link
Contributor

You are asking for an option to suppress printing a key/value pair if the value is {}? What if it's an empty array [], or the value null, and so forth?

@dibet
Copy link
Author

dibet commented Oct 1, 2019

Yes, thanks, it is the idea...

@nlohmann
Copy link
Owner

nlohmann commented Oct 1, 2019

No, this is currently not possible. You would need to remove unwanted values before serialization.

@dibet
Copy link
Author

dibet commented Oct 1, 2019

I try this approach but steel have some problems

case value_t::object:
{
if (val.m_value.object->empty())
{
o->write_characters("{}", 2);
return;
}

            if (pretty_print)
            {
                o->write_characters("{\n", 2);

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

                // first n-1 elements
                auto i = val.m_value.object->cbegin();
                for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
                {
                    o->write_characters(indent_string.c_str(), new_indent);
                    o->write_character('\"');
                    dump_escaped(i->first, ensure_ascii);
                    o->write_characters("\": ", 3);
                    dump(i->second, true, ensure_ascii, indent_step, new_indent);
                    o->write_characters(",\n", 2);
                }

                // last element
                assert(i != val.m_value.object->cend());
                assert(std::next(i) == val.m_value.object->cend());
                o->write_characters(indent_string.c_str(), new_indent);
                o->write_character('\"');
                dump_escaped(i->first, ensure_ascii);
                o->write_characters("\": ", 3);
                dump(i->second, true, ensure_ascii, indent_step, new_indent);

                o->write_character('\n');
                o->write_characters(indent_string.c_str(), current_indent);
                o->write_character('}');
            }
            else
            {
                o->write_character('{');

                // first n-1 elements
                auto i = val.m_value.object->cbegin();
                for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
                {
                    
                    if (i->second.m_type == value_t::object) {
                        
                        if (i->second.m_value.object->empty()) {
                            
                        } else {
                            o->write_character('\"');
                            dump_escaped(i->first, ensure_ascii);
                            o->write_characters("\":", 2);
                            dump(i->second, false, ensure_ascii, indent_step, current_indent);
                            o->write_character(',');
                        }
                    } else {
                        o->write_character('\"');
                        dump_escaped(i->first, ensure_ascii);
                        o->write_characters("\":", 2);
                        dump(i->second, false, ensure_ascii, indent_step, current_indent);
                        o->write_character(',');
                    }
                    
                    
                   
                }

                // last element
                assert(i != val.m_value.object->cend());
                assert(std::next(i) == val.m_value.object->cend());
                
                if (i->second.m_type == value_t::object) {
                        
                        if (i->second.m_value.object->empty()) {
                            
                            o->removeLastChar();
                            
                        } else {
                            o->write_character('\"');
                            dump_escaped(i->first, ensure_ascii);
                            o->write_characters("\":", 2);
                            dump(i->second, false, ensure_ascii, indent_step, current_indent);   
                        }
                    } else {
                        o->write_character('\"');
                        dump_escaped(i->first, ensure_ascii);
                        o->write_characters("\":", 2);
                        dump(i->second, false, ensure_ascii, indent_step, current_indent);   
                    }
                
                
               
                

                o->write_character('}');
            }

            return;
        }

@stale
Copy link

stale bot commented Oct 31, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Oct 31, 2019
@nlohmann nlohmann added solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) and removed state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated labels Nov 5, 2019
@nlohmann nlohmann closed this as completed Nov 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope)
Projects
None yet
Development

No branches or pull requests

3 participants