旅の途中
你知道阿基米德原理吗?

导航

 

概要:json11是一个基于c++11的json解析库,dropbox出品。

使用

直接举自带单元测试test.cpp中的例子:

    const string simple_test =
        R"({"k1":"v1", "k2":42, "k3":["a",123,true,false,null]})";

    string err;
    const auto json = Json::parse(simple_test, err);

    std::cout << "k1: " << json["k1"].string_value() << "\n";
    std::cout << "k3: " << json["k3"].dump() << "\n";

    for (auto &k : json["k3"].array_items()) {
        std::cout << "    - " << k.dump() << "\n";
    }

Json::parse可以解析string字符串生成Json对象,dump()则将Json对象格式化为字符串形式。

Json类提供了多种构造函数,用来构造Json对象:

    const Json obj = Json::object({
        { "k1", "v1" },
        { "k2", 42.0 },
        { "k3", Json::array({ "a", 123.0, true, false, nullptr }) },
    });

设计

主要由以下几个类构成

  • class Json
  • class JsonValue
  • class Value
  • class JsonInt JsonDouble ...

Json类为对外开放的接口,有多种构造方式。

class Json final { 
public
    // Types
    enum Type {
        NUL, NUMBER, BOOL, STRING, ARRAY, OBJECT
    };
    typedef std::vector<Json> array; //array、object直接使用STL容器实现
    typedef std::map<std::string, Json> object;

    Json() noexcept;                // NUL
    Json(std::nullptr_t) noexcept;  // NUL 
    Json(double value);             // NUMBER
    Json(int value);                // NUMBER
    Json(bool value);               // BOOL
    Json(const std::string &value); // STRING
    Json(std::string &&value);      // STRING
    Json(const char * value);       // STRING
    Json(const array &values);      // ARRAY
    Json(array &&values);           // ARRAY
    Json(const object &values);     // OBJECT
    Json(object &&values);          // OBJECT

    template <class T, class = decltype(&T::to_json)>
    Json(const T & t) : Json(t.to_json()) {}

    template <class M, typename std::enable_if<
        std::is_constructible<std::string, decltype(std::declval<M>().begin()->first)>::value
        && std::is_constructible<Json, decltype(std::declval<M>().begin()->second)>::value,
            int>::type = 0>
    Json(const M & m) : Json(object(m.begin(), m.end())) {}
    template <class V, typename std::enable_if<
        std::is_constructible<Json, decltype(*std::declval<V>().begin())>::value,
            int>::type = 0>
    Json(const V & v) : Json(array(v.begin(), v.end())) {}
    Json(void *) = delete;

...

private:
    std::shared_ptr<JsonValue> m_ptr;

其中有几个构造函数比较复杂,详见:

这些数据的实体被存放在m_ptr所指向的对象之中。

class JsonValue {
protected:
    friend class Json;
    friend class JsonInt;
    friend class JsonDouble;
    virtual Json::Type type() const = 0;
    virtual bool equals(const JsonValue * other) const = 0;
    virtual bool less(const JsonValue * other) const = 0;
    virtual void dump(std::string &out) const = 0;
    virtual double number_value() const;
    virtual int int_value() const;
    virtual bool bool_value() const;
    virtual const std::string &string_value() const;
    virtual const Json::array &array_items() const;
    virtual const Json &operator[](size_t i) const;
    virtual const Json::object &object_items() const;
    virtual const Json &operator[](const std::string &key) const;
    virtual ~JsonValue() {}
};

JsonValue是一个抽象基类,定义了一些api,用来操作访问json对象。这些api并不向用户开放,用于内部使用。用户借由Json类中提供的函数来操作和访问。

接下来是其派生类Value:

template <Json::Type tag, typename T>
class Value : public JsonValue {
protected:

    // Constructors
    explicit Value(const T &value) : m_value(value) {}
    explicit Value(T &&value)      : m_value(move(value)) {}

    // Get type tag
    Json::Type type() const override {
        return tag;
    }

    // Comparisons
    bool equals(const JsonValue * other) const override {
        return m_value == static_cast<const Value<tag, T> *>(other)->m_value;
    }
    bool less(const JsonValue * other) const override {
        return m_value < static_cast<const Value<tag, T> *>(other)->m_value;
    }

    const T m_value;
    void dump(string &out) const override { json11::dump(m_value, out); }
};

Value作为一个类模板,实例化出不同的类保存着json对象的数据实体。
而JsonInt,JsonDouble等等类可以直接通过继承对应的实例化类来保存数据。
以JsonInt为例:

class JsonInt final : public Value<Json::NUMBER, int> {
    double number_value() const override { return m_value; }
    int int_value() const override { return m_value; }
    bool equals(const JsonValue * other) const override { return m_value == other->number_value(); }
    bool less(const JsonValue * other)   const override { return m_value <  other->number_value(); }
public:
    explicit JsonInt(int value) : Value(value) {}
};

继承的Value为int版本,因此可以直接通过父类构造函数来保存value。同时实现了专属于该类的一些操作,例如int_value()用来获取value的值。

posted on 2017-10-23 22:12  CknightX  阅读(1476)  评论(0编辑  收藏  举报