Mantid
Loading...
Searching...
No Matches
PropertyWithValueJSON.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include "MantidKernel/DllConfig.h"
11#include "MantidKernel/Matrix.h"
12#include "PropertyManager_fwd.h"
13
14#include <json/value.h>
15#include <memory>
16
17#include <memory>
18#include <vector>
19
20namespace Mantid {
21namespace Kernel {
22template <typename T> class Matrix;
23class Property;
24
25namespace pwvjdetail {
26
28template <typename T> struct ToCpp {
29 T operator()(const Json::Value &) {
30 throw Exception::NotImplementedError("Unknown conversion from Json to C++ type");
31 }
32};
33
35template <> struct ToCpp<int> {
36 int operator()(const Json::Value &value) { return value.asInt(); }
37};
39template <> struct ToCpp<long long> {
40 long long operator()(const Json::Value &value) { return value.asInt64(); }
41};
43template <> struct ToCpp<long> {
44 long operator()(const Json::Value &value) { return value.asInt(); }
45};
47template <> struct ToCpp<unsigned int> {
48 unsigned int operator()(const Json::Value &value) { return value.asUInt(); }
49};
51template <> struct ToCpp<unsigned long long int> {
52 unsigned long long int operator()(const Json::Value &value) { return value.asUInt64(); }
53};
55template <> struct ToCpp<bool> {
56 bool operator()(const Json::Value &value) { return value.asBool(); }
57};
59template <> struct ToCpp<float> {
60 float operator()(const Json::Value &value) { return value.asFloat(); }
61};
63template <> struct ToCpp<double> {
64 double operator()(const Json::Value &value) { return value.asDouble(); }
65};
67template <> struct ToCpp<std::string> {
68 std::string operator()(const Json::Value &value) { return value.asString(); }
69};
70
72template <typename T> struct ToCpp<std::vector<T>> {
73 std::vector<T> operator()(const Json::Value &value) {
74 std::vector<T> arrayValues;
75 arrayValues.reserve(value.size());
76 auto toCpp = ToCpp<T>();
77 for (const auto &elem : value) {
78 try {
79 arrayValues.emplace_back(toCpp(elem));
80 } catch (Json::Exception &exc) {
81 throw std::invalid_argument("Mixed-type JSON array values not supported:" + std::string(exc.what()));
82 }
83 }
84 return arrayValues;
85 }
86};
87
88} // namespace pwvjdetail
89
91template <typename Type> Type decode(const Json::Value &value) { return pwvjdetail::ToCpp<Type>()(value); }
92
94MANTID_KERNEL_DLL
95PropertyManager_sptr createPropertyManager(const Json::Value &keyValues);
96
99MANTID_KERNEL_DLL std::unique_ptr<Property> decodeAsProperty(const std::string &name, const Json::Value &value);
100
101namespace pwvjdetail {
102// There is a known isssue with jsoncpp and ambiguous overloads when called
103// with long/unsigned long. We disambigate this by defining a generic
104// template that passes through its type and provide specializations
105// for the integer types to map these to appropriate Json integer types
106template <typename ValueType> struct JsonType {
107 using Type = ValueType;
108};
109template <> struct JsonType<int> {
110 using Type = Json::Int;
111};
112template <> struct JsonType<long> {
113 using Type = Json::Int64;
114};
115template <> struct JsonType<long long> {
116 using Type = Json::Int64;
117};
118template <> struct JsonType<unsigned int> {
119 using Type = Json::UInt;
120};
121template <> struct JsonType<unsigned long> {
122 using Type = Json::UInt64;
123};
124template <> struct JsonType<unsigned long long> {
125 using Type = Json::UInt64;
126};
127} // namespace pwvjdetail
128
134template <typename ValueType> Json::Value encodeAsJson(const ValueType &value) {
135 using JsonValueType = typename pwvjdetail::JsonType<ValueType>::Type;
136 return Json::Value(static_cast<JsonValueType>(value));
137}
138
144template <typename ValueType> Json::Value encodeAsJson(const std::vector<ValueType> &vectorValue) {
145 Json::Value jsonArray(Json::arrayValue);
146 for (const auto &element : vectorValue) {
147 jsonArray.append(encodeAsJson(element));
148 }
149 return jsonArray;
150}
151
158template <> inline Json::Value encodeAsJson(const std::vector<bool> &vectorValue) {
159 Json::Value jsonArray(Json::arrayValue);
160 for (const auto element : vectorValue) {
161 jsonArray.append(encodeAsJson(element));
162 }
163 return jsonArray;
164}
165
171template <typename ValueType> Json::Value encodeAsJson(const std::shared_ptr<ValueType> &) {
172 throw std::runtime_error("Unable to encode shared_ptr<T> as Json::Value.");
173}
174
180template <typename ValueType> Json::Value encodeAsJson(const Kernel::Matrix<ValueType> &) {
181 throw Exception::NotImplementedError("encodeAsJson not implemented for matrix-value type");
182}
183
184} // namespace Kernel
185} // namespace Mantid
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
Marks code as not implemented yet.
Definition Exception.h:138
Numerical Matrix class.
Definition Matrix.h:42
Type decode(const Json::Value &value)
Attempt to decode the given Json::Value as the given Type.
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::Json::Value encodeAsJson(const OptionalBool &)
Encode an OptionalBool as a Json::Value.
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.
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
double operator()(const Json::Value &value)
long long operator()(const Json::Value &value)
std::string operator()(const Json::Value &value)
std::vector< T > operator()(const Json::Value &value)
unsigned int operator()(const Json::Value &value)
unsigned long long int operator()(const Json::Value &value)
General type to convert a Json::Value to a set C++ type.