Use gd::String in Serializer

This commit is contained in:
Florian Rival
2015-09-29 22:04:51 +02:00
parent ef3e145f42
commit 656b3bf3eb
6 changed files with 25 additions and 25 deletions

View File

@@ -67,7 +67,7 @@ void AnalyticsSender::SendData(gd::String collection, SerializerElement & data)
request.setMethod(sf::Http::Request::Post);
request.setField("Content-Type", "application/json");
request.setUri("/3.0/projects/"+projectId.ToLocale()+"/events/"+collection.ToLocale()+"?api_key="+writeKey.ToLocale());
request.setBody(Serializer::ToJSON(data));
request.setBody(Serializer::ToJSON(data).ToSfString());
// Send the request
sf::Http::Response response = Http.sendRequest(request, sf::seconds(2));

View File

@@ -123,7 +123,7 @@ bool ProjectFileWriter::SaveToJSONFile(const gd::Project & project, const gd::St
project.SerializeTo(rootElement);
//Write JSON to file
std::string str = gd::Serializer::ToJSON(rootElement);
gd::String str = gd::Serializer::ToJSON(rootElement);
std::ofstream ofs(filename.ToLocale().c_str());
if (!ofs.is_open())
{

View File

@@ -131,13 +131,13 @@ namespace
* a JSON file.
* Adapted from public domain library "jsoncpp" (http://sourceforge.net/projects/jsoncpp/).
*/
std::string StringToQuotedJSONString( const char *value )
gd::String StringToQuotedJSONString( const char *value )
{
if (value == NULL)
return "";
// Not sure how to handle unicode...
if (strpbrk(value, "\"\\\b\f\n\r\t") == NULL && !containsControlCharacter( value ))
return std::string("\"") + value + "\"";
return gd::String("\"") + value + "\"";
// We have to walk value and escape any special characters.
// Appending to std::string is not efficient, but this should be rare.
// (Note: forward slashes are *not* rare, but I am not escaping them.)
@@ -193,30 +193,30 @@ namespace
}
}
result += "\"";
return result;
return gd::String::FromUTF8(result);
}
std::string ValueToJSON(const SerializerValue & val)
gd::String ValueToJSON(const SerializerValue & val)
{
if (val.IsBoolean())
return val.GetBool() ? "true" : "false";
else if (val.IsInt())
return gd::String::From(val.GetInt()).ToUTF8();
return gd::String::From(val.GetInt());
else if (val.IsDouble())
return gd::String::From(val.GetDouble()).ToUTF8();
return gd::String::From(val.GetDouble());
else
return StringToQuotedJSONString(val.GetString().c_str());
}
}
std::string Serializer::ToJSON(const SerializerElement & element)
gd::String Serializer::ToJSON(const SerializerElement & element)
{
if (element.IsValueUndefined())
{
if ( !element.ConsideredAsArrayOf().empty() )
{
//Store the element as an array in JSON:
std::string str = "[";
gd::String str = "[";
bool firstChild = true;
if ( element.GetAllAttributes().size() > 0 )
@@ -248,7 +248,7 @@ std::string Serializer::ToJSON(const SerializerElement & element)
}
else
{
std::string str = "{";
gd::String str = "{";
bool firstChild = true;
const std::map<gd::String, SerializerValue> & attributes = element.GetAllAttributes();

View File

@@ -47,7 +47,7 @@ public:
* Serialize a SerializerElement from/to JSON.
*/
///@{
static std::string ToJSON(const SerializerElement & element);
static gd::String ToJSON(const SerializerElement & element);
static SerializerElement FromJSON(const std::string & json);
static SerializerElement FromJSON(const gd::String & json)
{

View File

@@ -25,23 +25,23 @@ using namespace gd;
TEST_CASE( "Serializer", "[common]" ) {
SECTION("JSON basics") {
std::string originalJSON = "{\"ok\": true,\"hello\": \"world\"}";
gd::String originalJSON = "{\"ok\": true,\"hello\": \"world\"}";
SerializerElement element = Serializer::FromJSON(originalJSON);
REQUIRE(element.GetChild("ok").GetValue().GetBool() == true);
REQUIRE(element.GetChild("hello").GetValue().GetString() == "world");
std::string json = Serializer::ToJSON(element);
gd::String json = Serializer::ToJSON(element);
REQUIRE(json == originalJSON);
}
SECTION("Quotes and special characters") {
std::string originalJSON = "{\"\\\"hello\\\"\": \" \\\"quote\\\" \",\"caret-prop\": 1,\"special-\\b\\f\\n\\r\\t\\\"\": \"\\b\\f\\n\\r\\t\"}";
gd::String originalJSON = "{\"\\\"hello\\\"\": \" \\\"quote\\\" \",\"caret-prop\": 1,\"special-\\b\\f\\n\\r\\t\\\"\": \"\\b\\f\\n\\r\\t\"}";
SerializerElement element = Serializer::FromJSON(originalJSON);
REQUIRE(element.GetChild("caret-prop").GetValue().GetBool() == true);
REQUIRE(element.GetChild("\"hello\"").GetValue().GetString() == " \"quote\" ");
REQUIRE(element.GetChild("special-\b\f\n\r\t\"").GetValue().GetString() == "\b\f\n\r\t");
std::string json = Serializer::ToJSON(element);
gd::String json = Serializer::ToJSON(element);
REQUIRE(json == originalJSON);
}
@@ -79,7 +79,7 @@ TEST_CASE( "Serializer", "[common]" ) {
}
SECTION("Unsplit elements") {
//Get a JSON with elements being reference to split elements
std::string originalJSON = "{\"a\": {\"a1\": {\"name\": \"\",\"referenceTo\": \"/a/a1\"}},\"b\": {\"b1\": \"world\"},\"c\": {\"c1\": 3},\"layouts\": [{\"name\": \"layout0\",\"referenceTo\": \"/layouts/layout\"},{\"name\": \"layout1\",\"referenceTo\": \"/layouts/layout\"},{\"name\": \"layout2\",\"referenceTo\": \"/layouts/layout\"},{\"name\": \"layout3\",\"referenceTo\": \"/layouts/layout\"},{\"name\": \"layout4\",\"referenceTo\": \"/layouts/layout\"}]}";
gd::String originalJSON = "{\"a\": {\"a1\": {\"name\": \"\",\"referenceTo\": \"/a/a1\"}},\"b\": {\"b1\": \"world\"},\"c\": {\"c1\": 3},\"layouts\": [{\"name\": \"layout0\",\"referenceTo\": \"/layouts/layout\"},{\"name\": \"layout1\",\"referenceTo\": \"/layouts/layout\"},{\"name\": \"layout2\",\"referenceTo\": \"/layouts/layout\"},{\"name\": \"layout3\",\"referenceTo\": \"/layouts/layout\"},{\"name\": \"layout4\",\"referenceTo\": \"/layouts/layout\"}]}";
SerializerElement root = Serializer::FromJSON(originalJSON);
gd::Splitter splitter;

View File

@@ -106,7 +106,7 @@ bool Exporter::ExportLayoutForPreview(gd::Project & project, gd::Layout & layout
ExportIncludesAndLibs(includesFiles, exportDir, false);
//Create the index file
if (!ExportIndexFile("./JsPlatform/Runtime/index.html", exportDir, includesFiles))
if (!ExportIndexFile("./JsPlatform/Runtime/index.html", exportDir, includesFiles))
return false;
return true;
@@ -121,7 +121,7 @@ gd::String Exporter::ExportToJSON(gd::AbstractFileSystem &fs, const gd::Project
gd::SerializerElement rootElement;
project.SerializeTo(rootElement);
gd::String output = gd::String::FromUTF8(gd::Serializer::ToJSON(rootElement));
gd::String output = gd::Serializer::ToJSON(rootElement);
if (!wrapIntoVariable.empty()) output = wrapIntoVariable + " = " + output + ";";
if (!fs.WriteToFile(filename, output))
@@ -159,7 +159,7 @@ bool Exporter::ExportCordovaConfigFile(const gd::Project & project, gd::String e
.FindAndReplace("GDJS_PROJECTNAME", project.GetName())
.FindAndReplace("GDJS_PACKAGENAME", project.GetPackageName())
.FindAndReplace("GDJS_ORIENTATION", "default");
if (!fs.WriteToFile(exportDir + "/config.xml", str))
{
lastError = "Unable to write configuration file.";
@@ -400,7 +400,7 @@ void Exporter::ShowProjectExportDialog(gd::Project & project)
bool Exporter::ExportWholeProject(gd::Project & project, gd::String exportDir,
bool minify, bool exportForCocoonJS, bool exportForCordova)
{
auto exportProject = [this, &project, &minify,
auto exportProject = [this, &project, &minify,
&exportForCocoonJS, &exportForCordova](gd::String exportDir)
{
#if !defined(GD_NO_WX_GUI)
@@ -471,11 +471,11 @@ bool Exporter::ExportWholeProject(gd::Project & project, gd::String exportDir,
//Copy all dependencies and the index (or metadata) file.
gd::String additionalSpec = exportForCocoonJS ? "{forceFullscreen:true}" : "";
ExportIncludesAndLibs(includesFiles, exportDir, minify);
gd::String source = exportForCordova ?
gd::String source = exportForCordova ?
"./JsPlatform/Runtime/Cordova/www/index.html" :
"./JsPlatform/Runtime/index.html";
if (!ExportIndexFile(source, exportDir, includesFiles, additionalSpec))
{
gd::LogError(_("Error during export:\n") + lastError);
@@ -525,7 +525,7 @@ bool Exporter::ExportWholeProject(gd::Project & project, gd::String exportDir,
return true;
};
if (exportForCordova)
if (exportForCordova)
{
//Prepare the export directory
fs.MkDir(exportDir);