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

Parsing error with comment on json file #421

Closed
hallard opened this issue Jan 16, 2017 · 2 comments
Closed

Parsing error with comment on json file #421

hallard opened this issue Jan 16, 2017 · 2 comments
Labels
bug v5 ArduinoJson 5

Comments

@hallard
Copy link

hallard commented Jan 16, 2017

Docs mention that comments are allowed in 2 formats

// comment

or

/* Comments */

but as soon as I put comment I can't parse file (from ESP8266 SPIFFS), when I remove them, all is fine, I tried

{
  "gateway": {
    "rgb_luminosity": 25,
// 1=GRBW, 2=RGBW, 3=GRB, 4=RGB
    "rgb_led_type": 4,    
    "oled": 1,
  }
}
{
  "gateway": {
    "rgb_luminosity": 25,
/* 1=GRBW, 2=RGBW, 3=GRB, 4=RGB */
    "rgb_led_type": 4,    
    "oled": 1,
  }
}
{
  "gateway": {
    "rgb_luminosity": 25,
    "rgb_led_type": 4,    // 1=GRBW, 2=RGBW, 3=GRB, 4=RGB
    "oled": 1,
  }
}
{
  "gateway": {
    "rgb_luminosity": 25,
    "rgb_led_type": 4,    /* 1=GRBW, 2=RGBW, 3=GRB, 4=RGB */
    "oled": 1,
  }
}

none of my implementation is working, what I'm doing wrong ?

And here below my reading implementation on ESP8266 SPIFFS

/* ======================================================================
Function: read_config_gateway
Purpose : read json gateway config file
Input   : -
Output  : true if config ok, false otherwise
Comments: -
====================================================================== */
bool read_config_gateway(void) 
{
  const char cfg_file[] = "/cfg_gateway.json";
  bool ret = false;

  size_t size = check_config_file(cfg_file);

  if ( size ) {

    File configFile = SPIFFS.open(cfg_file, "r");

    // Allocate a buffer to store contents of the file.
    std::unique_ptr<char[]> buf(new char[size]);

    // We don't use String here because ArduinoJson library requires the input
    // buffer to be mutable. If you don't use ArduinoJson, you may as well
    // use configFile.readString instead.
    if ( configFile.readBytes(buf.get(), size) == size ) {
      DynamicJsonBuffer jsonBuffer;
      JsonObject& json = jsonBuffer.parseObject(buf.get());

      if (json.success()) {
        const char* data ;

        uint16_t mode = 0;
        mode = json["gateway"]["rgb_luminosity"];
        if (mode) {
          Debugf("JSON set RGB Luminosity to %d%%\r\n", mode);
          rgb_luminosity = mode;
        }

        mode = json["gateway"]["rgb_led_type"];
        if (mode) {
          Debugf("JSON set RGB LED Type to %d\r\n", mode);
          rgb_led_type = (RgbLedType_e) mode;
        }

        // All is fine
        ret = true;
      } else {
        DebuglnF("Failed to parse file");
      }
    } else {
      DebuglnF("Failed to read file content");
    }

    if (configFile) {
      configFile.close();
    }

  }
  return ret;
}

Each time I got this output

SPIFFS Mount succesfull
FS File: /cfg_wifi.json, size: 111
FS File: /cfg_module.json, size: 808
FS File: /index.htm, size: 4072
FS File: /edit.htm, size: 17179
FS File: /cfg_gateway.json, size: 191
file /cfg_wifi.json looks good (111 bytes)
JSON override SSID '' to 'CH2I-HOTSPOT'
JSON override PSK '' to '********'
file /cfg_gateway.json looks good (191 bytes)
Failed to parse file
@hallard hallard changed the title Parsing error with comment Parsing error with comment on json file Jan 16, 2017
bblanchon added a commit that referenced this issue Jan 16, 2017
bblanchon added a commit that referenced this issue Jan 17, 2017
@bblanchon bblanchon added the bug label Jan 17, 2017
@bblanchon
Copy link
Owner

Hello Charles,

The bug has been fixed in the master branch and will be available in v5.8.2 in a few days.

Thank you very much for reporting this bug.

Regards,
Benoit

@hallard
Copy link
Author

hallard commented Jan 18, 2017

Benoit,

Nickel, just pulled latest, will try it tomorrow

Charles

Repository owner locked and limited conversation to collaborators Sep 21, 2018
@bblanchon bblanchon added the v5 ArduinoJson 5 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug v5 ArduinoJson 5
Projects
None yet
Development

No branches or pull requests

2 participants