c@5: /* json11
c@5:  *
c@5:  * json11 is a tiny JSON library for C++11, providing JSON parsing and serialization.
c@5:  *
c@5:  * The core object provided by the library is json11::Json. A Json object represents any JSON
c@5:  * value: null, bool, number (int or double), string (std::string), array (std::vector), or
c@5:  * object (std::map).
c@5:  *
c@5:  * Json objects act like values: they can be assigned, copied, moved, compared for equality or
c@5:  * order, etc. There are also helper methods Json::dump, to serialize a Json to a string, and
c@5:  * Json::parse (static) to parse a std::string as a Json object.
c@5:  *
c@5:  * Internally, the various types of Json object are represented by the JsonValue class
c@5:  * hierarchy.
c@5:  *
c@5:  * A note on numbers - JSON specifies the syntax of number formatting but not its semantics,
c@5:  * so some JSON implementations distinguish between integers and floating-point numbers, while
c@5:  * some don't. In json11, we choose the latter. Because some JSON implementations (namely
c@5:  * Javascript itself) treat all numbers as the same type, distinguishing the two leads
c@5:  * to JSON that will be *silently* changed by a round-trip through those implementations.
c@5:  * Dangerous! To avoid that risk, json11 stores all numbers as double internally, but also
c@5:  * provides integer helpers.
c@5:  *
c@5:  * Fortunately, double-precision IEEE754 ('double') can precisely store any integer in the
c@5:  * range +/-2^53, which includes every 'int' on most systems. (Timestamps often use int64
c@5:  * or long long to avoid the Y2038K problem; a double storing microseconds since some epoch
c@5:  * will be exact for +/- 275 years.)
c@5:  */
c@5: 
c@5: /* Copyright (c) 2013 Dropbox, Inc.
c@5:  *
c@5:  * Permission is hereby granted, free of charge, to any person obtaining a copy
c@5:  * of this software and associated documentation files (the "Software"), to deal
c@5:  * in the Software without restriction, including without limitation the rights
c@5:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
c@5:  * copies of the Software, and to permit persons to whom the Software is
c@5:  * furnished to do so, subject to the following conditions:
c@5:  *
c@5:  * The above copyright notice and this permission notice shall be included in
c@5:  * all copies or substantial portions of the Software.
c@5:  *
c@5:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c@5:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c@5:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
c@5:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
c@5:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
c@5:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
c@5:  * THE SOFTWARE.
c@5:  */
c@5: 
c@5: #pragma once
c@5: 
c@5: #include <string>
c@5: #include <vector>
c@5: #include <map>
c@5: #include <memory>
c@5: #include <initializer_list>
c@5: 
c@5: namespace json11 {
c@5: 
c@5: enum JsonParse {
c@5:     STANDARD, COMMENTS
c@5: };
c@5: 
c@5: class JsonValue;
c@5: 
c@5: class Json final {
c@5: public:
c@5:     // Types
c@5:     enum Type {
c@5:         NUL, NUMBER, BOOL, STRING, ARRAY, OBJECT
c@5:     };
c@5: 
c@5:     // Array and object typedefs
c@5:     typedef std::vector<Json> array;
c@5:     typedef std::map<std::string, Json> object;
c@5: 
c@5:     // Constructors for the various types of JSON value.
c@5:     Json() noexcept;                // NUL
c@5:     Json(std::nullptr_t) noexcept;  // NUL
c@5:     Json(double value);             // NUMBER
c@5:     Json(int value);                // NUMBER
c@5:     Json(bool value);               // BOOL
c@5:     Json(const std::string &value); // STRING
c@5:     Json(std::string &&value);      // STRING
c@5:     Json(const char * value);       // STRING
c@5:     Json(const array &values);      // ARRAY
c@5:     Json(array &&values);           // ARRAY
c@5:     Json(const object &values);     // OBJECT
c@5:     Json(object &&values);          // OBJECT
c@5: 
c@5:     // Implicit constructor: anything with a to_json() function.
c@5:     template <class T, class = decltype(&T::to_json)>
c@5:     Json(const T & t) : Json(t.to_json()) {}
c@5: 
c@5:     // Implicit constructor: map-like objects (std::map, std::unordered_map, etc)
c@5:     template <class M, typename std::enable_if<
c@5:         std::is_constructible<std::string, typename M::key_type>::value
c@5:         && std::is_constructible<Json, typename M::mapped_type>::value,
c@5:             int>::type = 0>
c@5:     Json(const M & m) : Json(object(m.begin(), m.end())) {}
c@5: 
c@5:     // Implicit constructor: vector-like objects (std::list, std::vector, std::set, etc)
c@5:     template <class V, typename std::enable_if<
c@5:         std::is_constructible<Json, typename V::value_type>::value,
c@5:             int>::type = 0>
c@5:     Json(const V & v) : Json(array(v.begin(), v.end())) {}
c@5: 
c@5:     // This prevents Json(some_pointer) from accidentally producing a bool. Use
c@5:     // Json(bool(some_pointer)) if that behavior is desired.
c@5:     Json(void *) = delete;
c@5: 
c@5:     // Accessors
c@5:     Type type() const;
c@5: 
c@5:     bool is_null()   const { return type() == NUL; }
c@5:     bool is_number() const { return type() == NUMBER; }
c@5:     bool is_bool()   const { return type() == BOOL; }
c@5:     bool is_string() const { return type() == STRING; }
c@5:     bool is_array()  const { return type() == ARRAY; }
c@5:     bool is_object() const { return type() == OBJECT; }
c@5: 
c@5:     // Return the enclosed value if this is a number, 0 otherwise. Note that json11 does not
c@5:     // distinguish between integer and non-integer numbers - number_value() and int_value()
c@5:     // can both be applied to a NUMBER-typed object.
c@5:     double number_value() const;
c@5:     int int_value() const;
c@5: 
c@5:     // Return the enclosed value if this is a boolean, false otherwise.
c@5:     bool bool_value() const;
c@5:     // Return the enclosed string if this is a string, "" otherwise.
c@5:     const std::string &string_value() const;
c@5:     // Return the enclosed std::vector if this is an array, or an empty vector otherwise.
c@5:     const array &array_items() const;
c@5:     // Return the enclosed std::map if this is an object, or an empty map otherwise.
c@5:     const object &object_items() const;
c@5: 
c@5:     // Return a reference to arr[i] if this is an array, Json() otherwise.
c@5:     const Json & operator[](size_t i) const;
c@5:     // Return a reference to obj[key] if this is an object, Json() otherwise.
c@5:     const Json & operator[](const std::string &key) const;
c@5: 
c@5:     // Serialize.
c@5:     void dump(std::string &out) const;
c@5:     std::string dump() const {
c@5:         std::string out;
c@5:         dump(out);
c@5:         return out;
c@5:     }
c@5: 
c@5:     // Parse. If parse fails, return Json() and assign an error message to err.
c@5:     static Json parse(const std::string & in,
c@5:                       std::string & err,
c@5:                       JsonParse strategy = JsonParse::STANDARD);
c@5:     static Json parse(const char * in,
c@5:                       std::string & err,
c@5:                       JsonParse strategy = JsonParse::STANDARD) {
c@5:         if (in) {
c@5:             return parse(std::string(in), err, strategy);
c@5:         } else {
c@5:             err = "null input";
c@5:             return nullptr;
c@5:         }
c@5:     }
c@5:     // Parse multiple objects, concatenated or separated by whitespace
c@5:     static std::vector<Json> parse_multi(
c@5:         const std::string & in,
c@5:         std::string & err,
c@5:         JsonParse strategy = JsonParse::STANDARD);
c@5: 
c@5:     bool operator== (const Json &rhs) const;
c@5:     bool operator<  (const Json &rhs) const;
c@5:     bool operator!= (const Json &rhs) const { return !(*this == rhs); }
c@5:     bool operator<= (const Json &rhs) const { return !(rhs < *this); }
c@5:     bool operator>  (const Json &rhs) const { return  (rhs < *this); }
c@5:     bool operator>= (const Json &rhs) const { return !(*this < rhs); }
c@5: 
c@5:     /* has_shape(types, err)
c@5:      *
c@5:      * Return true if this is a JSON object and, for each item in types, has a field of
c@5:      * the given type. If not, return false and set err to a descriptive message.
c@5:      */
c@5:     typedef std::initializer_list<std::pair<std::string, Type>> shape;
c@5:     bool has_shape(const shape & types, std::string & err) const;
c@5: 
c@5: private:
c@5:     std::shared_ptr<JsonValue> m_ptr;
c@5: };
c@5: 
c@5: // Internal class hierarchy - JsonValue objects are not exposed to users of this API.
c@5: class JsonValue {
c@5: protected:
c@5:     friend class Json;
c@5:     friend class JsonInt;
c@5:     friend class JsonDouble;
c@5:     virtual Json::Type type() const = 0;
c@5:     virtual bool equals(const JsonValue * other) const = 0;
c@5:     virtual bool less(const JsonValue * other) const = 0;
c@5:     virtual void dump(std::string &out) const = 0;
c@5:     virtual double number_value() const;
c@5:     virtual int int_value() const;
c@5:     virtual bool bool_value() const;
c@5:     virtual const std::string &string_value() const;
c@5:     virtual const Json::array &array_items() const;
c@5:     virtual const Json &operator[](size_t i) const;
c@5:     virtual const Json::object &object_items() const;
c@5:     virtual const Json &operator[](const std::string &key) const;
c@5:     virtual ~JsonValue() {}
c@5: };
c@5: 
c@5: } // namespace json11