12#include <json/value.h>
23template <
typename T>
using ValueAsTypeMemFn = T (Json::Value::*)()
const;
33 template <
typename T>
explicit FromJson(ValueAsTypeMemFn<T> ) :
m_self{
std::make_unique<ModelT<T>>()} {}
35 std::unique_ptr<Property> createProperty(
const std::string &name,
const Json::Value &
value,
bool createArray)
const {
44 virtual ~ConceptT() =
default;
45 virtual std::unique_ptr<Property> singleValueProperty(
const std::string &name,
const Json::Value &
value)
const = 0;
46 virtual std::unique_ptr<Property> arrayValueProperty(
const std::string &name,
const Json::Value &
value)
const = 0;
49 template <
typename T>
struct ModelT : ConceptT {
50 std::unique_ptr<Property> singleValueProperty(
const std::string &name,
51 const Json::Value &
value)
const override final {
52 using ToCppT = pwvjdetail::ToCpp<T>;
53 return std::make_unique<PropertyWithValue<T>>(name, ToCppT()(
value));
56 std::unique_ptr<Property> arrayValueProperty(
const std::string &name,
57 const Json::Value &
value)
const override final {
58 using ToCppVectorT = pwvjdetail::ToCpp<std::vector<T>>;
59 return std::make_unique<ArrayProperty<T>>(name, ToCppVectorT()(
value));
67using FromJsonConverters = std::map<Json::ValueType, FromJson>;
71const FromJsonConverters &converters() {
72 static FromJsonConverters converters;
73 if (converters.empty()) {
75 converters.insert(std::make_pair(Json::booleanValue, FromJson(&Json::Value::asBool)));
76 converters.insert(std::make_pair(Json::intValue, FromJson(&Json::Value::asInt)));
77 converters.insert(std::make_pair(Json::realValue, FromJson(&Json::Value::asDouble)));
78 converters.insert(std::make_pair(Json::stringValue, FromJson(&Json::Value::asString)));
93std::unique_ptr<Property> createSingleTypeProperty(
const std::string &name,
const Json::Value &
value) {
94 const auto isArray =
value.isArray();
95 FromJsonConverters::const_iterator conversionFnIter;
99 conversionFnIter = converters().find(
value[0].type());
101 conversionFnIter = converters().find(
value.type());
103 if (conversionFnIter == converters().end()) {
104 throw std::invalid_argument(
"Cannot create property with name " + name +
105 ". Unable to find converter "
106 "for Json::ValueType to C++ "
109 return conversionFnIter->second.createProperty(name,
value,
value.isArray());
118std::unique_ptr<Property> createKeyValueProperty(
const std::string &name,
const Json::Value &keyValues) {
131 auto propMgr = std::make_shared<PropertyManager>();
132 auto members = keyValues.getMemberNames();
133 for (
const auto &key : members) {
134 const auto &
value = keyValues[key];
135 if (
value.isObject())
136 propMgr->declareProperty(createKeyValueProperty(key,
value));
138 propMgr->declareProperty(createSingleTypeProperty(key,
value));
152 if (
value.isNull()) {
153 throw std::invalid_argument(
"decodeAsProperty(): Found null Json value.");
156 if (!
value.isObject()) {
157 return createSingleTypeProperty(name,
value);
159 return createKeyValueProperty(name,
value);
double value
The value of the point.
std::unique_ptr< ConceptT > m_self
MANTID_KERNEL_DLL std::unique_ptr< Property > decodeAsProperty(const std::string &name, const Json::Value &value)
Attempt to create a Property of the most appropriate type from a string name and Json value object.
MANTID_KERNEL_DLL PropertyManager_sptr createPropertyManager(const Json::Value &keyValues)
Attempt to create a PropertyManager from the Json::Value.
std::shared_ptr< PropertyManager > PropertyManager_sptr
Typedef for a shared pointer to a PropertyManager.