Hi,
Does cocos2dx offer any cross platform solution for parsing JSON documents or should I go to a third party library for this part?
Thanks.
JB
Hi,
Does cocos2dx offer any cross platform solution for parsing JSON documents or should I go to a third party library for this part?
Thanks.
JB
You can use for that rapidjson, i’m use it for my game.
thanks for the super fast answer Alex
If problems occur, write here. maybe I can help
Emmm with this simple code (Document instance) I get the error: “interface type cannot be statically allocated”. Do you know what could the reason be?
PS: I tried also to init it as a pointer, and then I get the error “Unknown type name ‘Document’, did you mean UIDocument?”. It seems like even I have copied the header files, it is not detecting rapidjson…
Thanks!
#include "rapidjson.h"
#include "prettywriter.h"
#include "filestream.h"
#include
using namespace rapidjson;
void FriendsList::onEnter()
{
Document document;
}
sorry, it worked doing this:
#include “…/…/libs/rapidjson/document.h” // rapidjson’s DOM-style API
#include “…/…/libs/rapidjson/prettywriter.h” // for stringify JSON
#include “…/…/libs/rapidjson/filestream.h” // wrapper of C stream for prettywriter as output
#include
#include “rapidjson/document.h”
thanks!
emmm sorry again, now it looks like document.h throws a compiler error when I just do this:
const char json[] = " { ello : orld : true , : false, null, 123, i 3.1416, [1, 2, 3, 4] } “;
printf(”Original JSON:%s\n", json);
Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
document.Parse<0>(json).HasParseError();
The error is in Document.h, at the line: RawAssign(stack.template Pop);
template <unsigned parseFlags, typename Stream>
GenericDocument& ParseStream {
ValueType::SetNull; // Remove existing root if exist
GenericReader reader;
if ) {
RAPIDJSON_ASSERT == sizeof); // Got one and only one root object
RawAssign);
parseError = 0;
errorOffset_ = 0;
}
else {
parseError_ = reader.GetParseError;
errorOffset_ = reader.GetErrorOffset;
ClearStack;
}
return**this;
}
In order to help others, I’ve found the solution here: http://code.google.com/p/rapidjson/issues/detail?id=13
Thanks
uff now I get a new error while trying to parse the JSON:
Nothing should follow the root object or array
The JSON is this, and it is valid according to http://jsonlint.com:
[{id
Do you have any idea? Thanks!
This snippet can help you:
std::string fullPath = CCFileUtils::fullPathFromRelativePath("figures.json");
unsigned long bufferSize = 0;
const char* mFileData = (const char*)CCFileUtils::getFileData(fullPath.c_str(), "r", &bufferSize);
std::string clearData(mFileData);
size_t pos = clearData.rfind("}");
clearData = clearData.substr(0, pos+1);
document.Parse<0>(clearData.c_str());
This error occurs because when you get to the end of the buffer is added to the garbage (I do not know why), so I use crutches to trim the buffer Replace clearData.rfind(“}”); on clearData.rfind(“]”); in your caseю
It worked thanks! Now document.Parse<0>(data).HasParseError() returns always false, but unfortunately…
this always return false document.IsObject()
Maybe the framework doesn’t support multiple elements between “{}”, since in the example I have a simpler json, with just different variables and their values, could it be?
[{id
ok, got it:
Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
if(data document.Parse<0>(data).HasParseError())
{
for (SizeType i = 0; i < document.Size(); i++) // rapidjson uses SizeType instead of size_t.
{
CCLog(document[i][“id”].GetString());
}
}
else {
CCLog(document.GetParseError());
}
congratulate you!
Thanks Alex
I’ve written a small article in my blog to help others in similar situations: http://www.jesusbosch.com/2012/08/parsing-json-from-c-and-maybe-from.html
Jesus Bosch wrote:
Thanks Alex
>
I’ve written a small article in my blog to help others in similar situations: http://www.jesusbosch.com/2012/08/parsing-json-from-c-and-maybe-from.html
Oh, you ahead me, I wanted to do the same on the weekend
oh you have a blog? please share it, I’ll follow you
Jesus Bosch wrote:
oh you have a blog? please share it, I’ll follow you
I recently made it, so there are only two articles. But I will fill with the development of using Socos2d-x. And I write in Russian
arrrg russian is too hard for me… :-S if you write in English (even with bad english like mine) you will have more visits