13#include <json/value.h>
24template <
typename T>
using ValueAsTypeMemFn = T (Json::Value::*)() const;
34 template <
typename T>
explicit FromJson(ValueAsTypeMemFn<T> ) :
m_self{
std::make_unique<ModelT<T>>()} {}
36 std::unique_ptr<Property> createProperty(
const std::string &
name,
const Json::Value &
value,
bool createArray)
const {
45 virtual ~ConceptT() =
default;
46 virtual std::unique_ptr<Property> singleValueProperty(
const std::string &
name,
const Json::Value &
value)
const = 0;
47 virtual std::unique_ptr<Property> arrayValueProperty(
const std::string &
name,
const Json::Value &
value)
const = 0;
50 template <
typename T>
struct ModelT : ConceptT {
51 std::unique_ptr<Property> singleValueProperty(
const std::string &
name,
52 const Json::Value &
value)
const override final {
53 using ToCppT = pwvjdetail::ToCpp<T>;
54 return std::make_unique<PropertyWithValue<T>>(
name, ToCppT()(
value));
57 std::unique_ptr<Property> arrayValueProperty(
const std::string &
name,
58 const Json::Value &
value)
const override final {
59 using ToCppVectorT = pwvjdetail::ToCpp<std::vector<T>>;
60 return std::make_unique<ArrayProperty<T>>(
name, ToCppVectorT()(
value));
68using FromJsonConverters = std::map<Json::ValueType, FromJson>;
72const FromJsonConverters &converters() {
73 static FromJsonConverters converters;
74 if (converters.empty()) {
76 converters.insert(std::make_pair(Json::booleanValue, FromJson(&Json::Value::asBool)));
77 converters.insert(std::make_pair(Json::intValue, FromJson(&Json::Value::asInt)));
78 converters.insert(std::make_pair(Json::realValue, FromJson(&Json::Value::asDouble)));
79 converters.insert(std::make_pair(Json::stringValue, FromJson(&Json::Value::asString)));
94std::unique_ptr<Property> createSingleTypeProperty(
const std::string &
name,
const Json::Value &
value) {
95 const auto isArray =
value.isArray();
96 FromJsonConverters::const_iterator conversionFnIter;
100 conversionFnIter = converters().find(
value[0].type());
102 conversionFnIter = converters().find(
value.type());
104 if (conversionFnIter == converters().end()) {
105 throw std::invalid_argument(
"Cannot create property with name " +
name +
106 ". Unable to find converter "
107 "for Json::ValueType to C++ "
110 return conversionFnIter->second.createProperty(
name,
value,
value.isArray());
119std::unique_ptr<Property> createKeyValueProperty(
const std::string &
name,
const Json::Value &keyValues) {
132 auto propMgr = std::make_shared<PropertyManager>();
133 auto members = keyValues.getMemberNames();
134 for (
const auto &key : members) {
135 const auto &
value = keyValues[key];
136 if (
value.isObject())
137 propMgr->declareProperty(createKeyValueProperty(key,
value));
139 propMgr->declareProperty(createSingleTypeProperty(key,
value));
153 if (
value.isNull()) {
154 throw std::invalid_argument(
"decodeAsProperty(): Found null Json value.");
157 if (!
value.isObject()) {
158 return createSingleTypeProperty(
name,
value);
160 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.