OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
PathTreeSerialization.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
30 #include <osvr/Common/PathNode.h>
32 #include <osvr/Util/Verbosity.h>
33 
34 // Library/third-party includes
35 #include <json/value.h>
36 
37 // Standard includes
38 // - none
39 
40 namespace osvr {
41 namespace common {
42  namespace {
45  class PathNodeToJsonVisitor
46  : public boost::static_visitor<Json::Value> {
47  public:
48  PathNodeToJsonVisitor() : boost::static_visitor<Json::Value>() {}
49 
52  void addNodeData(PathNode const &node, Json::Value &val) {
53  val["path"] = getFullPath(node);
54  val["type"] = getTypeName(node);
55  }
56 
57  template <typename T>
58  Json::Value operator()(PathNode const &node, T const &elt) {
59  auto ret = pathElementToJson(elt);
60  addNodeData(node, ret);
61  return ret;
62  }
63  };
64  Json::Value pathNodeToJson(PathNode const &node) {
65  PathNodeToJsonVisitor visitor;
66  return applyPathNodeVisitor(visitor, node);
67  }
70  class PathTreeToJsonVisitor {
71  public:
72  PathTreeToJsonVisitor(bool keepNulls)
73  : m_ret(Json::arrayValue), m_keepNulls(keepNulls) {}
74 
75  Json::Value getResult() { return m_ret; }
76 
77  void operator()(PathNode const &node) {
78  if (m_keepNulls || !elements::isNull(node.value())) {
79  // If we're keeping nulls or this isn't a null...
80  m_ret.append(pathNodeToJson(node));
81  }
82  // Recurse on children
83  node.visitConstChildren(*this);
84  }
85 
86  private:
87  Json::Value m_ret;
88  bool m_keepNulls;
89  };
90  } // namespace
91 
92  Json::Value pathTreeToJson(PathTree const &tree, bool keepNulls) {
93  auto visitor = PathTreeToJsonVisitor{keepNulls};
94  tree.visitConstTree(visitor);
95  return visitor.getResult();
96  }
97 
98  void jsonToPathTree(PathTree &tree, Json::Value nodes) {
99  for (auto const &node : nodes) {
100  elements::PathElement elt = jsonToPathElement(node);
101  tree.getNodeByPath(node["path"].asString()).value() = elt;
102  }
103  }
104 } // namespace common
105 } // namespace osvr
A tree representation, with path/url syntax, of the known OSVR system.
Definition: PathTree.h:43
::osvr::util::TreeNode< PathElement > PathNode
The specific tree node type that contains a path element.
Definition: PathNode_fwd.h:42
Header containing the workings of turning PathElements to/from JSON (serialization).
Header including PathTree.h and all additional headers needed to define related types.
Json::Value pathElementToJson(T const &element)
Returns a JSON object with any element-type-specific data for the given PathElement-holdable type...
void jsonToPathTree(PathTree &tree, Json::Value nodes)
Deserialize a path tree from a JSON array of objects.
std::string getFullPath(PathNode const &node)
Gets the absolute path for the given node.
Definition: PathNode.cpp:42
PathNode & getNodeByPath(std::string const &path)
Returns the node indicated by the path, which must be absolute (begin with a /). Any non-existent nod...
Definition: PathTree.cpp:47
bool isNull(PathElement const &elt)
Returns true if the path element provided is a NullElement.
Definition: Server.h:44
void visitConstTree(F &functor) const
Visit the tree, with const nodes, starting at the root, with the given functor.
Definition: PathTree.h:55
Visitor::result_type applyPathNodeVisitor(Visitor &v, PathNode &node)
Visit a node's element's contained type, similar to boost::apply_visitor, but passing both the PathNo...
const char * getTypeName(PathNode const &node)
Gets an identifying string for the node value type.
Definition: PathNode.cpp:39
Internal, configured header file for verbosity macros.
Json::Value pathTreeToJson(PathTree const &tree, bool keepNulls=false)
Serialize a path tree to a JSON array of objects, one for each node.
boost::variant< NullElement, AliasElement, SensorElement, InterfaceElement, DeviceElement, PluginElement, StringElement > PathElement
The variant type containing a particular kind of path element.
Header.
Json::Value pathNodeToJson(PathNode const &node)
Serialize a path node to a JSON object.