Extra characters loaded from FileUtils getFileData

Extra characters loaded from FileUtils getFileData
0.0 0

#1

I am using the code below to load in a JSON file.

long filesize = 0;
string content;
string fullPath = "res/docs/chapters.json";

auto fileData = FileUtils::getInstance()->getFileData(fullPath.c_str(), "r", &filesize);
content.append((char*)fileData);
delete[] fileData;

It sometimes loads normal and then randomly it loads with extra characters at the end of the data that look like this:

{"myjson":{...}}\366\317l\310Z\345\223

Is there something odd about how I am loading the data or is this a bug in the FileUtils?

This happens when debugging on an iPad Mini 2. Also fails on an iPod Touch.


#2

I’ve added this code as a temporary fix:

auto found = content.find_last_of("}");
content = content.substr(0, found + 1);

#3

In another thread I got the answer I needed. I had to change methods to this:

auto fileData = FileUtils::getInstance()->getDataFromFile(fullPath.c_str());
string content((const char*)fileData.getBytes(), fileData.getSize());

because I was calling

getFileData(fullPath.c_str(), "r", &filesize)

which was giving the extra data.


#4

One less line of code:

string content = FileUtils::getInstance()->getStringFromFile(fullPath.c_str());

#5

Hello,

I have used following code to get string from .json file

ssize_t size = 0;
unsigned char* pBuffer = FileUtils::getInstance()->getFileData(filePath.c_str(), “r”, &size);
__String* ccstringVal = __String::createWithData(pBuffer, size);

Regards


#6

That works well because it terminates the string in createWithData().

I think I prefer getStringFromFile() because the responsibility for terminating the string and handing you the “correct” data from the file is contained in the FileUtils.


#7

Though this topic is a bit old, I’ve been facing unknown issues reading local JSON files as well. Rapidjson failed to parse certain JSON files most of the time, without any specific pattern, when I was useing:

FileUtils::getInstance()->getStringFromFile("Filename.json");

After lot of head scratching, I found this thread which mentioned about using the following, but with the problem of unwanted additional characters in the return string:

FileUtils::getInstance()->getFileData("Filename.json", "r", &size);

I used getFileData() method and added a flag to the RapidJSON parse method to solve this issue, and it seems to be working fine now. I parsed the returned string as below:

rapidjson::Document d;
d.Parse<rapidjson::ParseFlag::kParseStopWhenDoneFlag>((char *)json);

Please note the rapidjson::ParseFlag::kParseStopWhenDoneFlag, which is being used in place of the normally used 0 (zero). This allows proper parsing of the file, even when it has additional content beyond the last closing brace in the JSON string.

Perhaps this may help someone who may be facing similar issues that I had faced.


#8

That’s interesting, @abhimanyu I haven’t experienced a problem with getStringFromFile and I have used on WP8 and iOS. Are you running this on Android? Are the JSON files really large? Mine are relatively small.

Any other data you can give might help someone else down the line, as you mentioned.


#9

Yeah, I faced the issue in Android. Didn’t test the game on WP or iOS yet. The files were less than 5 KB when I was facing the issue.

Randomly the parse used to fail. Initially I thought there was an issue with RapidJSON or my JSON file wasn’t formatted properly, but it turned out that wasn’t the case. After going through this thread, I tried the above mentioned solution, and things have been working fine till now. My files are still small, maybe I’ll have to test it out with larger JSON files as well.


#10

My json file which is about 500 kb gets loaded with getStringFromFile incomplete. So rapidjson can not parse it. The json is valid by the way. And Android :slight_smile:


#11

I haven’t run it on Android, but I would guess the file you are using is in the wrong format so that when opened in Android it doesn’t understand EOF or something of that nature. Try and ensure your file is UTF-8. Did you make the file on Mac or Windows?


#12

I tried both Mac & Windows.


#13

Try the version below and then look at the size of the file to see if it looks right. I honestly don’t know much more than that. My best guess is that there are some odd characters in it.

auto fileData = FileUtils::getInstance()->getDataFromFile(fullPath.c_str());
string content((const char*)fileData.getBytes(), fileData.getSize());