Parse JSON formatted file from SD card

Hello,

I’ve been in trouble to parse a JSON formatted data on SD card.

I wrote a file, ‘option.txt’ like below.

{ "option" : { "ip":"127.0.0.1", "port":3000, "id":"01", "mac": ["0xDE", "0xAD", "0xBE", "0xEF", "0xFE", "0xED"] } }

And I wrote codes following below steps.

First, I read a data from SD card.
Second, I convert this data to String. And then, convert this String to charArray using toCharArray()
Third, I convert this charArray to aJsonObject using aJson.parse() method.

I think that there is a problem on third step. but I couldn’t find the cause.

Is there anyone who has experienced similar case?

#include <aJSON.h>
#include <string.h>
#include <SdFat.h>

SdFat sd;
SdFile myFile;

#define SD_CS 4  //CS pin
#define SD_SS 10 //SS pin, 10 on Uno, 53 on Mega

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))

void setup()
{
  Serial.begin(9600);
  while (!Serial) {}  // wait for Leonardo
  
  setSD(SD_CS);
}

boolean setSD(int _cs)
{
  if (!sd.begin(_cs, SPI_HALF_SPEED)) sd.initErrorHalt();

  // re-open the file for reading:
  if (!myFile.open("option.txt", O_READ)) {
    sd.errorHalt("opening test.txt for read failed");
  }
 
  String temp;
  char data;
  // make data to String;
  while ((data = myFile.read()) >= 0) 
  {
   temp = temp + data;
  }
  
  // close the file:
  myFile.close();
  Serial.println("option.txt:");
  Serial.println(temp);
  
  //make String to char array;
  char option[temp.length()];
  temp.toCharArray(option, temp.length()+1);
  
  Serial.println("toCharArray");
  Serial.println(option);
  
  //make char array to aJson Object
   aJsonObject* root = aJson.parse(option);
   processMessage(root);
   aJson.deleteItem(root);
}

/* Process message { "option" : { "ip":"127.0.0.1", "port":3000, "id":"01", "mac": ["0xDE", "0xAD", "0xBE", "0xEF", "0xFE", "0xED"] } }*/
void processMessage(aJsonObject *msg)
{
  aJsonObject *option = aJson.getObjectItem(msg, "option");
  
  char *json_String = aJson.print(msg);
  Serial.println(*json_String);
  
  if (!option) {
    Serial.println("no option data");
    return;
  }else{
    Serial.println("option available");
  }

  const static char* options[] = { "ip", "port", "id", "mac" };
  const static int option_n = 4;
  for (int i = 0; i < option_n; i++) {
    char* value;
     
    aJsonObject *optionVal = aJson.getObjectItem(option, options[i]);
    
    if (!optionVal){ 
      continue; // Value not provided, ok.
    }else{
      if(options[i] != "mac")
      {
        value = aJson.print(optionVal);
        Serial.print(options[i]);
        Serial.print(" : ");
        Serial.println(value);
      }else{
        Serial.print(options[i]);
        unsigned char numMAC = aJson.getArraySize( optionVal );
        Serial.print(" : ");
        
        for (byte i = 0; i < numMAC; i++)
        {
            aJsonObject *item = aJson.getArrayItem( optionVal, i );
            Serial.print(item->valuestring);
            if(i+1 < numMAC)
            {
            Serial.print(",");
            }
        }
        
        Serial.println();
      }
    }
  }
}
  char option[temp.length()];
  temp.toCharArray(option, temp.length()+1);

Suppose that temp contains 14 characters. You are allocating space, then, for 14 characters, and then telling the toCharArray() to write up to 15 characters in the array. You have the +1 in the wrong place.

I think that there is a problem on third step. but I couldn't find the cause.

What do you think the problem is? What have you done to verify that steps one and two are successful?

I also doubt the third step.

But, the toCharArray() method omit the last character

as I removed last '+1' at the method "toCharArray()".

So, I should add '+1' on that argument as buffer length.

Recently, I found that this code works fine without SdFat library.

I think there is some bugs between SdFat and aJson library.

Is there anyone who know clean SD card library which are not occurred problem with aJson library?