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

Backslash '\' in value causes exception #1827

Closed
TQwan opened this issue Nov 4, 2019 · 7 comments
Closed

Backslash '\' in value causes exception #1827

TQwan opened this issue Nov 4, 2019 · 7 comments
Labels
kind: bug state: needs more info the author of the issue needs to provide more details

Comments

@TQwan
Copy link

TQwan commented Nov 4, 2019

  • What is the issue you have?

backslash '\' in field value of json file causes an exception.

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

#include <nlohmann/json.hpp>

using json = nlohmann::json;

#include <iostream>     // std::cout
#include <fstream>      // std::ifstream
#include <ostream>      // std::ofstream
#include <iomanip>
#include <string>

// Note: nlohmann '<<' operator currently doesn't work with VS2019, so read file manually.
std::ifstream i;
i.open("examplefile.json", std::ifstream::in);
std::string x = "";
char c = i.get();
while (i.good()) {
    x += c;
    c = i.get();
}
i.close();

// parse explicitly
auto j3 = json::parse(x.c_str());

examplefile.json


{
    "Models": [
        {
            "path": "thispath\NOTworking.dae",
            "X": 0,
            "Y": 0,
            "Z": 0
        },
        {
            "path": "other.dae",
            "X": 0,
            "Y": 0,
            "Z": 0
        }
    ]
}

Note that forward slash does work.
examplefile2.json

{
    "Models": [
        {
            "path": "thispath/working.dae",
            "X": 0,
            "Y": 0,
            "Z": 0
        },
        {
            "path": "other.dae",
            "X": 0,
            "Y": 0,
            "Z": 0
        }
    ]
}
  • What is the expected behavior?
    File should just parse like the example2 (but with an string escaped backslash).

  • And what is the actual behavior instead?
    Program crash due to thrown exception.

  • Which compiler and operating system are you using? Is it a supported compiler?
    Microsoft Visual C++ 2019

  • Did you use a released version of the library or the version from the develop branch?
    Tryed both 3.6.1 and 3.70

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

@TQwan TQwan added the kind: bug label Nov 4, 2019
@nlohmann
Copy link
Owner

nlohmann commented Nov 4, 2019

Backslashes need to be escaped. I guess the exception is a parse error telling this.

@nlohmann
Copy link
Owner

nlohmann commented Nov 4, 2019

What is the exact exception text?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Nov 4, 2019
@gregmarr
Copy link
Contributor

gregmarr commented Nov 4, 2019

image

@nlohmann
Copy link
Owner

nlohmann commented Nov 4, 2019

One more question:

// Note: nlohmann '<<' operator currently doesn't work with VS2019, so read file manually.

Does code with << actually not compile or is it just a warning in the editor?

@ghost
Copy link

ghost commented Nov 5, 2019

Does code with << actually not compile or is it just a warning in the editor?

For me, it compiles but generates a warning...

1>Z:\blah\console.cpp(38,1): warning C4996: 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::operator <<': Since 3.0.0
1>Z:\blah\console.cpp(38,1): warning C4996: 	j << i;
1>Z:\blah\console.cpp(38,1): warning C4996: ^
1>Z:\blah\json.hpp(20730): message : see declaration of 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::operator <<'
1>Z:\blah\json.hpp(20730): message :     friend std::istream& operator<<(basic_json& j, std::istream& i)

VS2019 16.3.7.

@nlohmann
Copy link
Owner

nlohmann commented Nov 5, 2019

That's fine. The function is indeed deprecated.

@nlohmann
Copy link
Owner

Do you need further assistance with this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

3 participants