1

我决定用 C++ 编写一个 json 解释器来练习。理想情况下,我希望能够将它设置在一个包含地图、向量和相关值类型的树状容器中,以便我可以按照 json 的实际结构方式进行访问。例如,给出以下 JSON 示例(来自 json.org):

JSON
{
"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}


C++
jsonobject["menu"]["id"] // returns std::string "file"
jsonobject["menu"]["popup"]["menuitem"] // returns std::vector of std::maps
jsonobject["menu"]["popup"]["menuitem"][0]["value"] // returns std::string "New"

在上面的示例中,我的第一个问题来自容器内的混合类型。例如,在上面的 json 中,“menu”将是一个 std::map,但我不能拥有“id”和“popup”键,因为一个返回一个字符串,另一个返回一个向量。

为了解决这个问题,我决定创建从无类型基类继承的包装模板类。假设这将提供对值的多态访问。问题是我做不到。这是一些代码来显示我到目前为止所拥有的内容:

#include <string>
#include <map>
#include <vector>

class NodeBase {};

template <typename T>
class Node : public BaseNode {};

typedef std::map<std::string, BaseNode*> JSONObject;
template <>
class Node<JSONObject> : public BaseNode {
  public:
    JSONObject value;
    BaseNode* operator[](const std::string key){(value.find(key) != value.end) ? return value[key] : return nullptr}
};

typedef std::vector<BaseNode*> JSONArray;
template <>
class Node<JSONArray> : public BaseNode {
  public:
    JSONArray value;
    BaseNode* operator[](const uint index) {(index < value.size()) ? return value[index] : return nullptr}
};

template <typename T>
class Node : public BaseNode {
  public:
    T value;
};

class RootNode {
  Node<JSONObject> value;
};

int main(void) {
  RootNode root;
  root.insert(std::pair<std::string, BaseNode*>("menu", new Node<JSONObject>)
  // problem!
  // cannot use following code, because BaseNode* does not have access to value :'<
  root["menu"].insert(..)
}

所以我想我的问题是,我该如何进行这项工作?我是在正确的道路上,但无法从经验中看到解决方案,还是这种设计与 C++ 根本不兼容?

4

1 回答 1

3

有趣的课程,但我最近使用 boost::property_three 来解析 json 并且很高兴我避免重新发明轮子 xD 你的库可能作为轻量级解析器工作,所以我对这项研究感兴趣。

我觉得这个设计不是很好,使用库的人可能不知道某些值包含什么:哈希或数组。这可能导致过载,dynamic_cast这是相对繁重的操作。结果,您的库将失去其“轻量级”属性。

我建议使用一种对象类型表示法,就像所有Get方法 return一样Node*,但Node*可以回答问题isArray()isHash()而不需要像动态强制转换这样的繁重操作。

接下来,Node<JSONObject>并且Node<JSONArray>应该具有从BaseNode:继承的相同接口BaseNode* operator[](const std::string key),但Node<JSONArray>能够通过重载接收int密钥。如果用户知道它是数组,operator[]这会缩小 API 并允许用户使用索引。int但这不是万能的,你应该做所谓的“图书馆设计”。

Globally, i recommend not to write code right now. Write some class hierarchy schemas, write some use cases; look how these use cases can fit into schema. Generally writing such librarys in not a trivial task and "design" mistakes may make your library unusable in future. So, choose wise=) Goodluck!

于 2012-09-25T06:48:06.250 回答