2014-02-24 21:22:02 +01:00
|
|
|
/* Copyright (C) 2014 Wildfire Games.
|
2011-05-29 17:02:02 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AtlasObject.h"
|
|
|
|
#include "AtlasObjectImpl.h"
|
|
|
|
|
2014-02-24 21:22:02 +01:00
|
|
|
#include "JSONSpiritInclude.h"
|
2011-05-29 17:02:02 +02:00
|
|
|
|
2013-12-06 01:42:50 +01:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
# pragma warning(disable:4996) // deprecated CRT
|
|
|
|
#endif
|
2012-01-04 14:19:38 +01:00
|
|
|
|
2011-05-29 17:02:02 +02:00
|
|
|
#include "wx/log.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
2014-02-24 21:22:02 +01:00
|
|
|
static AtSmartPtr<AtNode> ConvertNode(json_spirit::Value node);
|
2011-05-29 17:02:02 +02:00
|
|
|
|
2014-02-24 21:22:02 +01:00
|
|
|
AtObj AtlasObject::LoadFromJSON(const std::string& json)
|
2011-05-29 17:02:02 +02:00
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
json_spirit::Value rootnode;
|
|
|
|
json_spirit::read_string(json, rootnode);
|
|
|
|
|
|
|
|
AtObj obj;
|
|
|
|
obj.p = ConvertNode(rootnode);
|
|
|
|
return obj;
|
|
|
|
}
|
2011-05-29 17:02:02 +02:00
|
|
|
|
2014-02-24 21:22:02 +01:00
|
|
|
// Convert from a JSON to an AtNode
|
|
|
|
static AtSmartPtr<AtNode> ConvertNode(json_spirit::Value node)
|
|
|
|
{
|
|
|
|
AtSmartPtr<AtNode> obj (new AtNode());
|
|
|
|
|
|
|
|
if (node.type() == json_spirit::str_type)
|
2011-05-29 17:02:02 +02:00
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
obj->value = std::wstring(node.get_str().begin(),node.get_str().end());
|
2011-05-29 17:02:02 +02:00
|
|
|
}
|
2014-02-24 21:22:02 +01:00
|
|
|
else if (node.type() == json_spirit::int_type || node.type() == json_spirit::real_type)
|
2011-05-29 17:02:02 +02:00
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
std::wstringstream stream;
|
|
|
|
if (node.type() == json_spirit::int_type)
|
|
|
|
stream << node.get_int();
|
|
|
|
if (node.type() == json_spirit::real_type)
|
|
|
|
stream << node.get_real();
|
|
|
|
|
|
|
|
obj->value = stream.str().c_str();
|
|
|
|
obj->children.insert(AtNode::child_pairtype(
|
|
|
|
"@number", AtSmartPtr<AtNode>(new AtNode())
|
|
|
|
));
|
2011-05-29 17:02:02 +02:00
|
|
|
}
|
2014-02-24 21:22:02 +01:00
|
|
|
else if (node.type() == json_spirit::bool_type)
|
2011-05-29 17:02:02 +02:00
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
if (node.get_bool())
|
|
|
|
obj->value = L"true";
|
|
|
|
else
|
|
|
|
obj->value = L"false";
|
|
|
|
|
|
|
|
obj->children.insert(AtNode::child_pairtype(
|
|
|
|
"@boolean", AtSmartPtr<AtNode>(new AtNode())
|
|
|
|
));
|
2011-05-29 17:02:02 +02:00
|
|
|
}
|
2014-02-24 21:22:02 +01:00
|
|
|
else if (node.type() == json_spirit::array_type)
|
2011-05-29 17:02:02 +02:00
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
obj->children.insert(AtNode::child_pairtype(
|
|
|
|
"@array", AtSmartPtr<AtNode>(new AtNode())
|
|
|
|
));
|
|
|
|
|
|
|
|
json_spirit::Array nodeChildren = node.get_array();
|
|
|
|
json_spirit::Array::iterator itr = nodeChildren.begin();
|
|
|
|
|
|
|
|
for (; itr != nodeChildren.end(); itr++)
|
2011-05-29 17:02:02 +02:00
|
|
|
{
|
|
|
|
obj->children.insert(AtNode::child_pairtype(
|
2014-02-24 21:22:02 +01:00
|
|
|
"item", ConvertNode(*itr)
|
2011-05-29 17:02:02 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2014-02-24 21:22:02 +01:00
|
|
|
else if (node.type() == json_spirit::obj_type)
|
2011-05-29 17:02:02 +02:00
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
json_spirit::Object objectProperties = node.get_obj();
|
|
|
|
json_spirit::Object::iterator itr = objectProperties.begin();
|
|
|
|
for (; itr != objectProperties.end(); itr++)
|
2011-05-29 17:02:02 +02:00
|
|
|
{
|
|
|
|
obj->children.insert(AtNode::child_pairtype(
|
2014-02-24 21:22:02 +01:00
|
|
|
itr->name_, ConvertNode(itr->value_)
|
2011-05-29 17:02:02 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2014-03-09 18:55:54 +01:00
|
|
|
else if (node.type() == json_spirit::null_type)
|
|
|
|
{
|
|
|
|
return obj;
|
|
|
|
}
|
2014-02-24 21:22:02 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(! "Unimplemented type found when parsing JSON!");
|
|
|
|
}
|
|
|
|
|
2011-05-29 17:02:02 +02:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-24 21:22:02 +01:00
|
|
|
json_spirit::Value BuildJSONNode(AtNode::Ptr p)
|
2011-05-29 17:02:02 +02:00
|
|
|
{
|
|
|
|
if (!p)
|
2014-02-24 21:22:02 +01:00
|
|
|
{
|
|
|
|
json_spirit::Value rval;
|
|
|
|
return rval;
|
|
|
|
}
|
2011-05-29 17:02:02 +02:00
|
|
|
|
|
|
|
// Special case for numbers/booleans to allow round-tripping
|
|
|
|
if (p->children.count("@number"))
|
|
|
|
{
|
|
|
|
// Convert to double
|
|
|
|
std::wstringstream str;
|
|
|
|
str << p->value;
|
|
|
|
double val = 0;
|
|
|
|
str >> val;
|
|
|
|
|
2014-02-24 21:22:02 +01:00
|
|
|
json_spirit::Value rval(val);
|
2011-05-29 17:02:02 +02:00
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
else if (p->children.count("@boolean"))
|
|
|
|
{
|
|
|
|
bool val = false;
|
|
|
|
if (p->value == L"true")
|
|
|
|
val = true;
|
2014-02-24 21:22:02 +01:00
|
|
|
|
|
|
|
json_spirit::Value rval(val);
|
|
|
|
return rval;
|
2011-05-29 17:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If no children, then use the value string instead
|
|
|
|
if (p->children.empty())
|
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
json_spirit::Value rval(std::string(p->value.begin(), p->value.end()));
|
|
|
|
return rval;
|
2011-05-29 17:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p->children.find("@array") != p->children.end())
|
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
json_spirit::Array rval;
|
2011-05-29 17:02:02 +02:00
|
|
|
|
|
|
|
// Find the <item> children
|
|
|
|
AtNode::child_maptype::const_iterator lower = p->children.lower_bound("item");
|
|
|
|
AtNode::child_maptype::const_iterator upper = p->children.upper_bound("item");
|
|
|
|
|
2014-03-01 15:41:32 +01:00
|
|
|
unsigned int idx = 0;
|
2011-05-29 17:02:02 +02:00
|
|
|
for (AtNode::child_maptype::const_iterator it = lower; it != upper; ++it)
|
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
json_spirit::Value child = BuildJSONNode(it->second);
|
|
|
|
rval.push_back(child);
|
2011-05-29 17:02:02 +02:00
|
|
|
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
|
2014-02-24 21:22:02 +01:00
|
|
|
return rval;
|
2011-05-29 17:02:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
json_spirit::Object rval;
|
|
|
|
|
2011-05-29 17:02:02 +02:00
|
|
|
for (AtNode::child_maptype::const_iterator it = p->children.begin(); it != p->children.end(); ++it)
|
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
json_spirit::Value child = BuildJSONNode(it->second);
|
2014-03-09 18:55:54 +01:00
|
|
|
// We don't serialize childs with null value.
|
|
|
|
// Instead of something like this we omit the whole property: "StartingCamera": null
|
|
|
|
// There's no special reason for that, it's just the same behaviour the previous implementations had.
|
|
|
|
if (child.type() != json_spirit::null_type)
|
|
|
|
rval.push_back(json_spirit::Pair(it->first.c_str(), child));
|
2011-05-29 17:02:02 +02:00
|
|
|
}
|
|
|
|
|
2014-02-24 21:22:02 +01:00
|
|
|
return rval;
|
2011-05-29 17:02:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-24 21:22:02 +01:00
|
|
|
std::string AtlasObject::SaveToJSON(AtObj& obj)
|
2011-05-29 17:02:02 +02:00
|
|
|
{
|
2014-02-24 21:22:02 +01:00
|
|
|
json_spirit::Value root = BuildJSONNode(obj.p);
|
|
|
|
std::string ret = json_spirit::write_string(root, 0);
|
|
|
|
return ret;
|
2011-05-29 17:02:02 +02:00
|
|
|
}
|