Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
usModuleManifest.cpp
Go to the documentation of this file.
1 /*=============================================================================
2 
3  Library: CppMicroServices
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 =============================================================================*/
21 
22 #include "usModuleManifest_p.h"
23 
24 #include "jsoncpp.h"
25 
26 #include <stdexcept>
27 
28 US_BEGIN_NAMESPACE
29 
30 namespace {
31 
32  typedef std::map<std::string, Any> AnyMap;
33  typedef std::vector<Any> AnyVector;
34 
35  void ParseJsonObject(const Json::Value& jsonObject, AnyMap& anyMap);
36  void ParseJsonArray(const Json::Value& jsonArray, AnyVector& anyVector);
37 
38  Any ParseJsonValue(const Json::Value& jsonValue)
39  {
40  if (jsonValue.isObject())
41  {
42  Any any = AnyMap();
43  ParseJsonObject(jsonValue, ref_any_cast<AnyMap>(any));
44  return any;
45  }
46  else if (jsonValue.isArray())
47  {
48  Any any = AnyVector();
49  ParseJsonArray(jsonValue, ref_any_cast<AnyVector>(any));
50  return any;
51  }
52  else if (jsonValue.isString())
53  {
54  return Any(jsonValue.asString());
55  }
56  else if (jsonValue.isBool())
57  {
58  return Any(jsonValue.asBool());
59  }
60  else if (jsonValue.isDouble())
61  {
62  return Any(jsonValue.asDouble());
63  }
64  else if (jsonValue.isIntegral())
65  {
66  return Any(jsonValue.asInt());
67  }
68 
69  return Any();
70  }
71 
72  void ParseJsonObject(const Json::Value& jsonObject, AnyMap& anyMap)
73  {
74  for (Json::Value::const_iterator it = jsonObject.begin();
75  it != jsonObject.end(); ++it)
76  {
77  const Json::Value& jsonValue = *it;
78  Any anyValue = ParseJsonValue(jsonValue);
79  if (!anyValue.Empty())
80  {
81  anyMap.insert(std::make_pair(it.memberName(), anyValue));
82  }
83  }
84  }
85 
86  void ParseJsonArray(const Json::Value& jsonArray, AnyVector& anyVector)
87  {
88  for (Json::Value::const_iterator it = jsonArray.begin();
89  it != jsonArray.end(); ++it)
90  {
91  const Json::Value& jsonValue = *it;
92  Any anyValue = ParseJsonValue(jsonValue);
93  if (!anyValue.Empty())
94  {
95  anyVector.push_back(anyValue);
96  }
97  }
98  }
99 
100 }
101 
102 ModuleManifest::ModuleManifest()
103 {
104 }
105 
106 void ModuleManifest::Parse(std::istream& is)
107 {
108  Json::Value root;
110  if (!jsonReader.parse(is, root, false))
111  {
112  throw std::runtime_error(jsonReader.getFormattedErrorMessages());
113  }
114 
115  if (!root.isObject())
116  {
117  throw std::runtime_error("The Json root element must be an object.");
118  }
119 
120  ParseJsonObject(root, m_Properties);
121 }
122 
123 bool ModuleManifest::Contains(const std::string& key) const
124 {
125  return m_Properties.count(key) > 0;
126 }
127 
128 Any ModuleManifest::GetValue(const std::string& key) const
129 {
130  AnyMap::const_iterator iter = m_Properties.find(key);
131  if (iter != m_Properties.end())
132  {
133  return iter->second;
134  }
135  return Any();
136 }
137 
138 std::vector<std::string> ModuleManifest::GetKeys() const
139 {
140  std::vector<std::string> keys;
141  for (AnyMap::const_iterator iter = m_Properties.begin();
142  iter != m_Properties.end(); ++iter)
143  {
144  keys.push_back(iter->first);
145  }
146  return keys;
147 }
148 
149 void ModuleManifest::SetValue(const std::string& key, const Any& value)
150 {
151  m_Properties[key] = value;
152 }
153 
154 US_END_NAMESPACE
bool asBool() const
Definition: jsoncpp.cpp:2428
const_iterator begin() const
Definition: jsoncpp.cpp:3019
const_iterator end() const
Definition: jsoncpp.cpp:3054
bool isBool() const
Definition: jsoncpp.cpp:2913
bool isObject() const
Definition: jsoncpp.cpp:2971
Represents a JSON value.
Definition: jsoncpp.h:433
bool isDouble() const
Definition: jsoncpp.cpp:2943
bool isArray() const
Definition: jsoncpp.cpp:2964
double asDouble() const
Definition: jsoncpp.cpp:2370
bool isIntegral() const
Definition: jsoncpp.cpp:2934
std::string asString() const
Definition: jsoncpp.cpp:2198
static Features strictMode()
A configuration that is strictly compatible with the JSON specification.
Definition: jsoncpp.cpp:231
Unserialize a JSON document into a Value.
Definition: jsoncpp.h:1455
Int asInt() const
Definition: jsoncpp.cpp:2230
bool isString() const
Definition: jsoncpp.cpp:2957
const iterator for object and array value.
Definition: jsoncpp.h:1298