nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.26k stars 6.57k forks source link

'\' char problem in strings #261

Closed tanisman closed 8 years ago

tanisman commented 8 years ago

I am trying to parse this json:

"window": { "texture": "interface\ifcommon\slibifwnd.ddj", "width":330, "height":366, "position": [0, 0], "rect": [0, 0, 330, 366], "moveable": true, "moverect": [0, 0, 330, 30] },

but parser gives me unexpected \ error for string.

IDE: Visual Studio 2015

nlohmann commented 8 years ago

On first sight, there are too many backslashes: there should be two instead of three, because \i and \s are not allowed according to RFC 7159, Section 7. The correct JSON would be:

{
  "window": {
    "texture": "interface\\ifcommon\\slibifwnd.ddj",
    "width": 330,
    "height": 366,
    "position": [0, 0],
    "rect": [0, 0, 330, 366],
    "moveable": true,
    "moverect": [0, 0, 330, 30]
  }
}
tanisman commented 8 years ago

it has 2 backslashes but my web browser showed one backslash after posting that, than I have edited my post.Here is picture of json: image

nlohmann commented 8 years ago

Can you then please paste the complete JSON as is? The current one begins with "window" and closes with , which both would be noncompliant.

tanisman commented 8 years ago
{
  "window": {
    "texture": "interface\\ifcommon\\slibifwnd.ddj",
    "width":330,
    "height":366,
    "position": [0, 0],
    "rect": [0, 0, 330, 366],
    "moveable": true,
    "moverect": [0, 0, 330, 30]
    },

    "elements": {
      "count":2,
      "1": {
        "class": "button",
        "textures": [
          "interface\\ifcommon\\com_button.ddj",
          "interface\\ifcommon\\com_button_focus.ddj",
          "interface\\ifcommon\\com_button_press.ddj",
          "interface\\ifcommon\\com_button_disable.ddj"
          ],
          "width": 76,
          "height": 24,
          "text": "Button1",
          "disabled": false,
          "position": [150, 50],
          "rect": [0, 0, 76, 24]
      },
      "2": {
        "class": "edit",
        "textures": [
          "interface\\ifcommon\\slib_editbox.ddj"
          ],
          "width": 107,
          "height": 21,
          "text": "EditBox1",
          "disabled": false,
          "position": [30, 50],
          "rect": [5, 0, 102, 19]
      }
    }
}
nlohmann commented 8 years ago

Thanks. This file is valid JSON, and I can parse it without problems with

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

using nlohmann::json;

int main()
{
    // just parse the string
    json s = R"("interface\\ifcommon\\slibifwnd.ddj")"_json;
    std::cout << std::setw(2) << s << std::endl;

    // parse the whole file
    std::ifstream f("issue261.json");
    json j;
    j << f;
    std::cout << std::setw(2) << j << std::endl;
}

Could you please try this code? Especially, parsing just the string would be interesting.

nlohmann commented 8 years ago

And just to make sure:

tanisman commented 8 years ago

I figured out the problem;

there is no problem when I use R prefix

json s = R"("interface\\ifcommon\\slibifwnd.ddj")"_json;
    std::cout << std::setw(2) << s << std::endl;

but the problem is backslashes, json required format: path\\to\\directory as you know its presented in C as path\\\\to\\\\directory I was trying to load from a variable not a file that was my mistake Thanks for your care!