jsoncpp icon indicating copy to clipboard operation
jsoncpp copied to clipboard

why change the default output format

Open batobolg opened this issue 11 years ago • 9 comments

Hi ~

I found writer output is changed.
before array output like this. ["aaa","bbb","ccc","ddd"] in one line. but now it like this. [ "aaa", "bbb", "ccc", "ddd" ] why you change it ?. I like one line format, It not take so much place. if array is too long you can new line by 30 item.

thank you.

batobolg avatar Feb 20 '15 16:02 batobolg

Please tell us which commit you are using, and if possible, which commit or version you were using. Also, which Writer are you using?

cdunn2001 avatar Feb 20 '15 16:02 cdunn2001

the version is 1.4.4 and my code like this:

int main(int argc, const char *argv[])
{
        Json::Value root;
        std::string example = "{\"array\":[\"item1\", \"item2\", \"item2\", \"item2\"], \"foo\":\"asdf\"}";

        Json::Reader reader;

        bool parsed = reader.parse(example, root, false);

        std::cout << root << std::endl;

        return 0;
}

+++++++++++++++++++++++ result is:

{
    "array" : 
    [
        "item1",
        "item2",
        "item2",
        "item2"
    ],
    "foo" : "asdf"
}

batobolg avatar Feb 20 '15 16:02 batobolg

What commit/version were you using before, when things worked correctly?

cdunn2001 avatar Feb 20 '15 17:02 cdunn2001

I assume you were using the 0.6.0-rc2 version, or something fairly old, right?

Instead of

std::cout << root << std::endl;

try

   Json::StreamWriterBuilder builder;
   builder.settings_["commentStyle"] = "None";  // default is "All"
   cout << Json::writeString(builder, root);

The reason for the change is that // this kind of comment cannot be embedded on a single line. Test-cases were failing with the old, but they had been suppressed instead of fixed. The choices were:

  • A) Keep the old, weird, inconsistent behavior for embedded comments.
    • Very hard to maintain, let alone document and explain.
  • B) Leave comments off by default.
    • Likely to break lots of users.
  • C) Leave comments on by default.
    • Might upset a few users, but it should not actually break anyone. And the work-around is simple.

Sorry. I hope the work-around is ok.

cdunn2001 avatar Feb 20 '15 17:02 cdunn2001

the header file shows #define JSONCPP_VERSION_STRING "1.4.4"

work-around is ok , yes. I like it. thank you, thank you very much.

batobolg avatar Feb 21 '15 16:02 batobolg

Json::FastWriter can be used to get output in a std::string form.

ya1gaurav avatar Sep 18 '15 04:09 ya1gaurav

thank you.

batobolg avatar Sep 18 '15 07:09 batobolg

I wouldn't recommend FastWriter. Because its internals are exposed, we cannot alter that class and maintain binary-compatibility. That makes it hard to support.

I suggest StreamWriterBuilder plus Json::writeString(), as shown under Advanced Usage here:

  • http://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html

cdunn2001 avatar Sep 19 '15 03:09 cdunn2001

I am using StreamWriterBuilder plus Json::writeString() , thank you. :)

batobolg avatar Sep 21 '15 04:09 batobolg