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

Internal structure gets corrupted while parsing #569

Closed
sytelus opened this issue Apr 25, 2017 · 11 comments
Closed

Internal structure gets corrupted while parsing #569

sytelus opened this issue Apr 25, 2017 · 11 comments

Comments

@sytelus
Copy link

sytelus commented Apr 25, 2017

I've two json files. One is stored using ASCII encoding and other is stored using UTF-8 encoding without BOM (using Notepad++'s encoding feature). When I load the UTF-8 file using json.hpp, its internal structure looks corrupted (very large array sizes) and you will see crash trying to read any of elements. The ASCII encoding seems to work fine. However the thing is that there are non-ASCII chars in the content.

Attached is the very small json file I'm trying to read.

settings.zip

@nlohmann
Copy link
Owner

The library fully supports UTF-8, and I can parse the file without problems:

#include "json.hpp"
#include <fstream>

using json = nlohmann::json;

int main()
{
    std::ifstream f("settings.json");
    json j = json::parse(f);
    std::cout << std::setw(2) << j << std::endl;
}

output:

{
  "Pixhawk": {
    "LocalHostIp": "127.0.0.1",
    "LogViewerHostIp": "127.0.0.1",
    "LogViewerPort": 14388,
    "Model": "Flamewheel",
    "OffboardCompID": 1,
    "OffboardSysID": 134,
    "QgcHostIp": "127.0.0.1",
    "QgcPort": 14550,
    "SerialBaudRate": 115200,
    "SerialPort": "*",
    "SimCompID": 42,
    "SimSysID": 142,
    "SitlIp": "127.0.0.1",
    "SitlPort": 14556,
    "UdpIp": "127.0.0.1",
    "UdpPort": 14560,
    "UseSerial": true,
    "VehicleCompID": 1,
    "VehicleSysID": 135
  },
  "RpcEnabled": false
}

The file does not contain any non-ASCII byte.

Can you provide more information?

@sytelus
Copy link
Author

sytelus commented Apr 25, 2017

I'm not able to reproduce this now so closing the issue. If I see this again, I'll re-open.

@sytelus sytelus closed this as completed Apr 25, 2017
@nlohmann
Copy link
Owner

Ok. Thanks for checking back!

@sedapsfognik
Copy link

Hello, guys! Got exactly the same issue :)
Parses perfectly on Windows, but crashes on Ubuntu. After parsing an object, is_object() returns false, while is_array() returns true. No exception on parsing, but exception on getting access to parsed fields.
config.zip

@sedapsfognik
Copy link

sedapsfognik commented Jul 7, 2017

Spent an hour :)
Even this do not work on Ubuntu: json jsonObj{ json_t::parse("{"x": "y"}") };
auto value = jsonObj.value("x", std::string());

@sedapsfognik
Copy link

OMG...

json jsonObj{ json_t::parse("{"x": "y"}") }; - works fine on Windows with latest VS2017
json jsonObj = json_t::parse("{"x": "y"}"); - only such a variant works on Ubuntu with gcc -std=c++1y

Looks like different compilers chose different (copy?)constructors or something like that around initializer's list. Just WOW!

@nlohmann
Copy link
Owner

nlohmann commented Jul 7, 2017

I'll have a look.

@sedapsfognik
Copy link

Thank you! BTW: Amazing lib! ;)

@sedapsfognik
Copy link

sedapsfognik commented Jul 7, 2017

Sorry, for annoying, start from here:

class A
{
public:

  A()
  {
    std::cout << "A()" << std::endl;
  }

  A(std::initializer_list<A> list)
  {
    std::cout << "A(std::initializer_list<A> list)" << std::endl;
  }

  A(const A& a)
  {
    std::cout << "A(const A& a)" << std::endl;
  }
};

int main(int argc, char* argv[])
{
  A a1;
  A a2 = a1;
  A a3{ a1 };

  return 0;
}

Windows-VS2017:
A()
A(const A& a)
A(const A& a)

Ubuntu-GCC:
A()
A(const A& a)
A(const A& a)
A(std::initializer_list<A> list)

@tlanc007
Copy link

tlanc007 commented Jul 7, 2017

@sedapsfognik :

json jsonObj{ json_t::parse("{"x": "y"}") }; - works fine on Windows with latest VS2017
json jsonObj = json_t::parse("{"x": "y"}"); - only such a variant works on Ubuntu with gcc -std=c++1y

gcc has had problems with the unified initializer construct. Although I thought that with version 7, it properly handled most of the legal constructs that clang/llvm and MSVS have been able to handle for years.

Also for the fun of it, is there any difference when you specifically use g++ instead of gcc for the command line? I know that there are cases where clang will do something a little differently than clang++.

@nlohmann
Copy link
Owner

nlohmann commented Jul 7, 2017

I can confirm that Clang and GCC (I tried version 5 to 8) behave differently when it comes to these lines:

json j1{ json::parse("{\"x\": \"y\"}") };
json j2 = json::parse("{\"x\": \"y\"}");

In all cases, j1 is returned as an array containing {"x":"y"}, whereas j2 is just the object {"x":"y"}.

I think there is nothing I can do from the library side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants