Commit 26326f2b authored by Floris Berendsen's avatar Floris Berendsen
Browse files

ENH: added json reader

parent fdbcc529
......@@ -27,6 +27,7 @@ public:
typedef Blueprint::Pointer BlueprintPointerType;
static BlueprintPointerType FromXML(const std::string &filename);
static BlueprintPointerType FromJson(const std::string &filename);
private:
......
......@@ -34,7 +34,7 @@ ConfigurationReader::BlueprintPointerType ConfigurationReader::FromXML(const std
// Create empty property tree object
using boost::property_tree::ptree;
ptree pt;
read_xml(filename, pt, boost::property_tree::xml_parser::trim_whitespace);
BlueprintPointerType blueprint = Blueprint::New();
......@@ -106,6 +106,85 @@ ConfigurationReader::BlueprintPointerType ConfigurationReader::FromXML(const std
return blueprint;
}
ConfigurationReader::BlueprintPointerType ConfigurationReader::FromJson(const std::string &filename)
{
// Create empty property tree object
using boost::property_tree::ptree;
ptree pt;
read_json(filename, pt);
BlueprintPointerType blueprint = Blueprint::New();
BOOST_FOREACH(ptree::value_type &v, pt.equal_range("Component"))
{
std::string componentName;
ParameterMapType componentPropertyMap;
for (auto const & elm : v.second)
{
const std::string & componentKey = elm.first;
if (componentKey == "Name")
{
componentName = elm.second.data();
continue;
}
ParameterValueType propertyMultiValue = VectorizeValues(elm.second);
std::string propertyKey = elm.first;
componentPropertyMap[propertyKey] = propertyMultiValue;
}
blueprint->AddComponent(componentName, componentPropertyMap);
}
BOOST_FOREACH(ptree::value_type &v, pt.equal_range("Connection"))
{
std::string connectionName = v.second.data();
if (connectionName != "")
{
std::cout << "warning connectionName """ << connectionName << """ is ignored" << std::endl;
}
std::string outName;
std::string inName;
ParameterMapType componentPropertyMap;
for (auto const & elm : v.second)
{
const std::string & connectionKey = elm.first;
if (connectionKey == "Out")
{
outName = elm.second.data();
continue;
}
else if (connectionKey == "In")
{
inName = elm.second.data();
continue;
}
else if (connectionKey == "Name")
{
std::cout << "Warning: connection 'Name'-s are ignored." << std::endl;
continue;
}
else
{
ParameterValueType propertyMultiValue = VectorizeValues(elm.second);
std::string propertyKey = elm.first;
componentPropertyMap[propertyKey] = propertyMultiValue;
}
}
blueprint->AddConnection(outName, inName, componentPropertyMap);
}
return blueprint;
}
} // namespace selx
#endif // ConfigurationReader_cxx
......@@ -20,3 +20,12 @@ TEST_F( ConfigurationReaderTest, ReadXML )
}
TEST_F(ConfigurationReaderTest, ReadJson)
{
ConfigurationReader reader;
ConfigurationReader::BlueprintPointerType blueprint;
EXPECT_NO_THROW(blueprint = reader.FromJson("configurationReaderTest.json"));
blueprint->WriteBlueprint("configurationReaderTest.json.dot");
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment