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 { using Type = ValueType; };
107template <> struct JsonType<int> { using Type = Json::Int; };
108template <> struct JsonType<long> { using Type = Json::Int64; };
109template <> struct JsonType<long long> { using Type = Json::Int64; };
110template <> struct JsonType<unsigned int> { using Type = Json::UInt; };
111template <> struct JsonType<unsigned long> { using Type = Json::UInt64; };
112template <> struct JsonType<unsigned long long> { using Type = Json::UInt64; };
113} // namespace pwvjdetail
114
120template <typename ValueType> Json::Value encodeAsJson(const ValueType &value) {
121 using JsonValueType = typename pwvjdetail::JsonType<ValueType>::Type;
122 return Json::Value(static_cast<JsonValueType>(value));
123}
124
130template <typename ValueType> Json::Value encodeAsJson(const std::vector<ValueType> &vectorValue) {
131 Json::Value jsonArray(Json::arrayValue);
132 for (const auto &element : vectorValue) {
133 jsonArray.append(encodeAsJson(element));
134 }
135 return jsonArray;
136}
137
144template <> inline Json::Value encodeAsJson(const std::vector<bool> &vectorValue) {
145 Json::Value jsonArray(Json::arrayValue);
146 for (const auto element : vectorValue) {
147 jsonArray.append(encodeAsJson(element));
148 }
149 return jsonArray;
150}
151
157template <typename ValueType> Json::Value encodeAsJson(const std::shared_ptr<ValueType> &) {
158 throw std::runtime_error("Unable to encode shared_ptr<T> as Json::Value.");
159}
160
166template <typename ValueType> Json::Value encodeAsJson(const Kernel::Matrix<ValueType> &) {
167 throw Exception::NotImplementedError("encodeAsJson not implemented for matrix-value type");
168}
169
170} // namespace Kernel
171} // namespace Mantid
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.
bool operator()(const Json::Value &value)
double operator()(const Json::Value &value)
float operator()(const Json::Value &value)
int operator()(const Json::Value &value)
long 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.