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

2 backslash cause problem #2652

Closed
1 of 4 tasks
yazi1297 opened this issue Feb 20, 2021 · 4 comments
Closed
1 of 4 tasks

2 backslash cause problem #2652

yazi1297 opened this issue Feb 20, 2021 · 4 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@yazi1297
Copy link

yazi1297 commented Feb 20, 2021

Code like this:

try
{
    std::string strJson = "{\"file_path\":\"C\\123.txt\"}";
    nlohmann::json jsonMsg = nlohmann::json::parse(strJson);
    std::string strJsonDump = jsonMsg.dump();
    std::cout << strJsonDump << std::endl;
}
catch (const std::exception& e)
{
    const char* szErrInfo = e.what();
    printf("%s \n", szErrInfo);
}

It will crash, and error log is

[json.exception.parse_error.101] parse error at line 1, column 17: syntax error while parsing value - invalid string: forbidden character after backslash; last read: '"C\1'

What is the issue you have?

When I analyze json code which have 2 backslash, my proejct will crash.

Please describe the steps to reproduce the issue.

  1. Create a console project
  2. Include Json.hpp in your project
  3. Copy the code above into your main function, and run.

Can you provide a small but working code example?

#include "pch.h"
#include <iostream>
#include <string>
#include "json/json.hpp"
int main()
{
    try
    {
        std::string strJson = "{\"file_path\":\"C:\\123.txt\"}";
        nlohmann::json jsonMsg = nlohmann::json::parse(strJson);
        std::string strJsonDump = jsonMsg.dump();
        std::cout << strJsonDump << std::endl;
    }
    catch (const std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
    std::cout << "Hello World!\n";
}

What is the expected behavior?

It should print {"file_path":"C:\123.txt"} on the screen

And what is the actual behavior instead?

It crashed.

Which compiler and operating system are you using?

  • Compiler: vs2019 + Visual Studio 2017 - Windows XP (v141_xp)
  • Operating system: Windows

Which version of the library did you use?

  • [] latest release version 3.9.1
  • other release - please state the version: ___
  • the develop branch
    develop + 3.9.1

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

  • yes
  • no - please copy/paste the error message below
@nlohmann
Copy link
Owner

The literal "{\"file_path\":\"C\\123.txt\"}" after escaping is "{"file_path":"C\123.txt"}". In JSON, a 1 is not allowed after a backslash - this is what the exception message is saying.

@gregmarr
Copy link
Contributor

To get your desired result, you need to escape twice, once for JSON and one for std::string.

std::string strJson = "{\"file_path\":\"C:\\\\123.txt\"}";

or you can use a raw string literal and only escape once

std::string strJson = R"({ "file_path": "C:\\123.txt" })";

https://en.cppreference.com/w/cpp/language/string_literal

@nlohmann
Copy link
Owner

@yazi1297 Do you need further assistance with this issue?

@AronZhi
Copy link

AronZhi commented Nov 23, 2022

use a raw string literal seems not work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants