2015-02-03 00:07:09 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-02-03 00:07:09 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-02-03 00:07:09 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-02-03 00:07:09 +11:00
|
|
|
*
|
|
|
|
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_JSON_TREE_HPP
|
|
|
|
#define __UTIL_JSON_TREE_HPP
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-02-03 13:09:23 +11:00
|
|
|
#include "../iterator.hpp"
|
2016-06-28 14:16:28 +10:00
|
|
|
#include "../view.hpp"
|
2015-02-03 13:09:23 +11:00
|
|
|
#include "./flat.hpp"
|
2015-03-18 15:46:16 +11:00
|
|
|
#include "./fwd.hpp"
|
2015-02-03 00:07:09 +11:00
|
|
|
|
|
|
|
namespace json { namespace tree {
|
2015-03-18 15:47:16 +11:00
|
|
|
enum type_t {
|
|
|
|
OBJECT,
|
|
|
|
ARRAY,
|
|
|
|
STRING,
|
|
|
|
NUMBER,
|
|
|
|
BOOLEAN,
|
|
|
|
NONE
|
|
|
|
};
|
2015-02-03 00:07:09 +11:00
|
|
|
|
|
|
|
/// Parse an encoded form of JSON into a tree structure
|
2016-06-28 14:16:28 +10:00
|
|
|
template <typename T>
|
|
|
|
std::unique_ptr<node>
|
|
|
|
parse (util::view<T> data);
|
2015-02-03 00:07:09 +11:00
|
|
|
|
|
|
|
extern void write (const json::tree::node&, std::ostream&);
|
|
|
|
|
|
|
|
/// Abstract base for all JSON values
|
|
|
|
class node {
|
|
|
|
public:
|
2016-05-12 17:41:31 +10:00
|
|
|
node (const node&) = delete;
|
2015-02-03 00:07:09 +11:00
|
|
|
virtual ~node () { ; }
|
2015-03-18 15:44:46 +11:00
|
|
|
virtual std::unique_ptr<node> clone (void) const = 0;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2016-03-17 18:05:28 +11:00
|
|
|
virtual const object& as_object (void) const&;
|
|
|
|
virtual const array& as_array (void) const&;
|
|
|
|
virtual const string& as_string (void) const&;
|
|
|
|
virtual const number& as_number (void) const&;
|
|
|
|
virtual const boolean& as_boolean (void) const&;
|
|
|
|
virtual const null& as_null (void) const&;
|
|
|
|
|
|
|
|
virtual object& as_object (void)&;
|
|
|
|
virtual array& as_array (void)&;
|
|
|
|
virtual string& as_string (void)&;
|
|
|
|
virtual number& as_number (void)&;
|
|
|
|
virtual boolean& as_boolean (void)&;
|
|
|
|
virtual null& as_null (void)&;
|
2015-03-18 15:50:07 +11:00
|
|
|
|
2015-08-28 21:06:29 +10:00
|
|
|
// we don't provide operators for conversion due to ambiguities
|
|
|
|
// introduced when using indexing operators with pointer
|
|
|
|
// arguments.
|
2015-03-23 18:41:07 +11:00
|
|
|
virtual bool as_bool (void) const;
|
2015-02-16 23:34:50 +11:00
|
|
|
virtual float as_float (void) const;
|
|
|
|
virtual double as_double (void) const;
|
2016-05-12 17:41:31 +10:00
|
|
|
virtual intmax_t as_sint (void) const;
|
|
|
|
virtual uintmax_t as_uint (void) const;
|
2016-03-17 18:05:28 +11:00
|
|
|
virtual const char* as_chars (void) const&;
|
2015-02-16 23:34:50 +11:00
|
|
|
|
2015-09-08 14:33:39 +10:00
|
|
|
template <typename T>
|
|
|
|
T as (void) const;
|
|
|
|
|
2015-02-03 00:07:09 +11:00
|
|
|
virtual bool is_object (void) const { return false; }
|
|
|
|
virtual bool is_array (void) const { return false; }
|
|
|
|
virtual bool is_string (void) const { return false; }
|
|
|
|
virtual bool is_number (void) const { return false; }
|
|
|
|
virtual bool is_boolean (void) const { return false; }
|
|
|
|
virtual bool is_null (void) const { return false; }
|
|
|
|
|
2015-03-18 15:47:16 +11:00
|
|
|
virtual type_t type (void) const = 0;
|
|
|
|
|
2015-02-03 00:07:09 +11:00
|
|
|
virtual bool operator==(const node &rhs) const = 0;
|
|
|
|
virtual bool operator!=(const node &rhs) const;
|
|
|
|
virtual bool operator==(const object &) const { return false; }
|
|
|
|
virtual bool operator==(const array &) const { return false; }
|
|
|
|
virtual bool operator==(const string &) const { return false; }
|
|
|
|
virtual bool operator==(const number &) const { return false; }
|
|
|
|
virtual bool operator==(const boolean &) const { return false; }
|
|
|
|
virtual bool operator==(const null &) const { return false; }
|
|
|
|
|
|
|
|
virtual bool operator==(const char *rhs) const;
|
|
|
|
virtual bool operator!=(const char *rhs) const { return !(*this == rhs); }
|
|
|
|
|
2016-03-17 18:05:28 +11:00
|
|
|
virtual node& operator[] (const std::string&)&;
|
2016-05-12 17:41:31 +10:00
|
|
|
virtual node& operator[] (size_t)&;
|
2016-03-17 18:05:28 +11:00
|
|
|
virtual const node& operator[] (const std::string&) const&;
|
2016-05-12 17:41:31 +10:00
|
|
|
virtual const node& operator[] (size_t) const&;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
|
|
|
virtual std::ostream& write (std::ostream &os) const = 0;
|
2016-05-12 17:41:31 +10:00
|
|
|
|
|
|
|
protected:
|
|
|
|
node () = default;
|
2015-02-03 00:07:09 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Represents a JSON object, and contains its children.
|
2015-03-18 15:45:33 +11:00
|
|
|
class object final : public node {
|
2016-05-12 17:41:31 +10:00
|
|
|
private:
|
|
|
|
using value_store = std::map<std::string, std::unique_ptr<node>>;
|
|
|
|
|
2015-02-03 00:07:09 +11:00
|
|
|
public:
|
|
|
|
typedef value_store::iterator iterator;
|
|
|
|
typedef value_store::const_iterator const_iterator;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~object ();
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual std::unique_ptr<node> clone (void) const override;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2016-03-17 18:05:28 +11:00
|
|
|
virtual const object& as_object (void) const& override { return *this; }
|
|
|
|
virtual object& as_object (void) & override { return *this; }
|
|
|
|
virtual bool is_object (void) const override { return true; }
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual type_t type (void) const override { return OBJECT; }
|
2015-03-18 15:47:16 +11:00
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual bool operator==(const object &rhs) const override;
|
|
|
|
virtual bool operator==(const node &rhs) const override
|
2015-02-03 00:07:09 +11:00
|
|
|
{ return rhs == *this; }
|
|
|
|
|
2015-03-18 15:54:13 +11:00
|
|
|
virtual void insert (const std::string &key, std::unique_ptr<node>&& value);
|
2016-05-12 17:41:31 +10:00
|
|
|
virtual const node& operator[] (const std::string &key) const& override;
|
|
|
|
virtual node& operator[] (const std::string &key)& override;
|
2015-03-18 15:54:13 +11:00
|
|
|
virtual bool has (const std::string&) const;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2015-03-18 16:00:21 +11:00
|
|
|
virtual const_iterator find (const std::string&) const;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
|
|
|
virtual const_iterator begin (void) const;
|
|
|
|
virtual const_iterator end (void) const;
|
2015-03-18 15:59:29 +11:00
|
|
|
virtual const_iterator cbegin (void) const { return begin (); }
|
|
|
|
virtual const_iterator cend (void) const { return end (); }
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2015-03-18 16:00:21 +11:00
|
|
|
virtual size_t size (void) const;
|
|
|
|
|
|
|
|
virtual void clear (void);
|
|
|
|
virtual void erase (const std::string &key);
|
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual std::ostream& write (std::ostream &os) const override;
|
2016-05-12 17:41:31 +10:00
|
|
|
|
|
|
|
private:
|
|
|
|
value_store m_values;
|
2015-02-03 00:07:09 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Represents a JSON array, and contains its children.
|
2015-03-18 15:45:33 +11:00
|
|
|
class array final : public node {
|
2015-02-03 00:07:09 +11:00
|
|
|
protected:
|
|
|
|
typedef std::vector<std::unique_ptr<node>>::iterator pointer_array_iterator;
|
|
|
|
typedef std::vector<std::unique_ptr<node>>::const_iterator const_pointer_array_iterator;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef referencing_iterator<pointer_array_iterator> iterator;
|
|
|
|
typedef referencing_iterator<const_pointer_array_iterator> const_iterator;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::vector<std::unique_ptr<node>> m_values;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~array();
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual std::unique_ptr<node> clone (void) const override;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2016-03-17 18:05:28 +11:00
|
|
|
virtual const array& as_array (void) const& override { return *this; }
|
|
|
|
virtual array& as_array (void) & override { return *this; }
|
|
|
|
virtual bool is_array (void) const override { return true; }
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual type_t type (void) const override { return ARRAY; }
|
2015-03-18 15:47:16 +11:00
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual bool operator==(const array &rhs) const override;
|
|
|
|
virtual bool operator==(const node &rhs) const override;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2015-03-18 16:01:10 +11:00
|
|
|
virtual size_t size (void) const;
|
2016-05-12 17:41:31 +10:00
|
|
|
virtual node& operator[] (size_t idx)& override;
|
|
|
|
virtual const node& operator[] (size_t idx) const& override;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2015-03-18 15:59:29 +11:00
|
|
|
virtual iterator begin (void);
|
|
|
|
virtual iterator end (void);
|
|
|
|
virtual const_iterator begin (void) const;
|
|
|
|
virtual const_iterator end (void) const;
|
|
|
|
virtual const_iterator cbegin (void) const;
|
|
|
|
virtual const_iterator cend (void) const;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
|
|
|
virtual void insert (std::unique_ptr<json::tree::node> &&_value);
|
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual std::ostream& write (std::ostream &os) const override;
|
2015-02-03 00:07:09 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Represents a JSON string literal.
|
2015-03-18 15:45:33 +11:00
|
|
|
class string final : public node {
|
2015-02-03 00:07:09 +11:00
|
|
|
protected:
|
|
|
|
std::string m_value;
|
|
|
|
|
|
|
|
public:
|
2016-06-28 14:16:28 +10:00
|
|
|
template <typename T>
|
|
|
|
string (T first, T last): m_value (first, last) { ; }
|
|
|
|
|
2015-02-03 00:07:09 +11:00
|
|
|
explicit string (const std::string &_value): m_value (_value) { ; }
|
|
|
|
explicit string (const char *_value): m_value (_value) { ; }
|
|
|
|
string (const char *_first, const char *_last): m_value (_first, _last) { ; }
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual std::unique_ptr<node> clone (void) const override;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2016-03-17 18:05:28 +11:00
|
|
|
virtual const string& as_string (void) const& override { return *this; }
|
|
|
|
virtual string& as_string (void) & override { return *this; }
|
|
|
|
virtual bool is_string (void) const override { return true; }
|
2015-03-18 15:47:16 +11:00
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual type_t type (void) const override { return STRING; }
|
2015-03-18 15:47:16 +11:00
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual bool operator==(const char *rhs) const override;
|
|
|
|
virtual bool operator==(const string &rhs) const override;
|
2015-03-18 16:01:29 +11:00
|
|
|
virtual bool operator==(const std::string &rhs) const;
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual bool operator==(const node &rhs) const override
|
2015-02-03 00:07:09 +11:00
|
|
|
{ return rhs == *this; }
|
|
|
|
|
2015-03-18 16:01:29 +11:00
|
|
|
virtual bool operator!= (const std::string &rhs) const { return !(*this == rhs); }
|
|
|
|
|
2015-02-03 00:07:09 +11:00
|
|
|
virtual size_t size (void) const { return m_value.size (); }
|
|
|
|
|
|
|
|
operator const std::string&(void) const { return m_value; }
|
|
|
|
const std::string& native (void) const { return m_value; }
|
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual std::ostream& write (std::ostream &os) const override;
|
2015-02-03 00:07:09 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Represents a JSON integer/float literal.
|
2015-03-18 15:45:33 +11:00
|
|
|
class number final : public node {
|
2015-02-03 00:07:09 +11:00
|
|
|
public:
|
2016-05-12 17:41:31 +10:00
|
|
|
enum repr_t {
|
|
|
|
REAL,
|
|
|
|
SINT,
|
|
|
|
UINT
|
|
|
|
};
|
|
|
|
|
|
|
|
using real_t = double;
|
|
|
|
using sint_t = intmax_t;
|
|
|
|
using uint_t = uintmax_t;
|
|
|
|
|
|
|
|
|
|
|
|
explicit number (real_t _value): m_repr (REAL) { m_value.r = _value; }
|
|
|
|
explicit number (sint_t _value): m_repr (SINT) { m_value.s = _value; }
|
|
|
|
explicit number (uint_t _value): m_repr (UINT) { m_value.u = _value; }
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual std::unique_ptr<node> clone (void) const override;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2016-03-17 18:05:28 +11:00
|
|
|
virtual const number& as_number (void) const& override { return *this; }
|
|
|
|
virtual number& as_number (void) & override { return *this; }
|
|
|
|
virtual bool is_number (void) const override { return true; }
|
2015-03-18 15:47:16 +11:00
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual type_t type (void) const override { return NUMBER; }
|
2016-05-12 17:41:31 +10:00
|
|
|
virtual repr_t repr (void) const { return m_repr; }
|
2015-03-18 15:47:16 +11:00
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual bool operator==(const number &rhs) const override;
|
|
|
|
virtual bool operator==(const node &rhs) const override
|
2015-02-03 00:07:09 +11:00
|
|
|
{ return rhs == *this; }
|
|
|
|
|
2016-05-12 17:41:31 +10:00
|
|
|
operator real_t (void) const;
|
|
|
|
operator sint_t (void) const;
|
|
|
|
operator uint_t (void) const;
|
|
|
|
|
|
|
|
real_t real (void) const;
|
|
|
|
sint_t sint (void) const;
|
|
|
|
uint_t uint (void) const;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual std::ostream& write (std::ostream &os) const override;
|
2016-05-12 17:41:31 +10:00
|
|
|
|
|
|
|
private:
|
|
|
|
union {
|
|
|
|
real_t r;
|
|
|
|
sint_t s;
|
|
|
|
uint_t u;
|
|
|
|
} m_value;
|
|
|
|
|
|
|
|
repr_t m_repr;
|
2015-02-03 00:07:09 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Represents a JSON boolean literal.
|
2015-03-18 15:45:33 +11:00
|
|
|
class boolean final : public node {
|
2015-02-03 00:07:09 +11:00
|
|
|
protected:
|
|
|
|
bool m_value;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit boolean (bool _value): m_value (_value) { ; }
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual std::unique_ptr<node> clone (void) const override;
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2016-03-17 18:05:28 +11:00
|
|
|
virtual const boolean& as_boolean (void) const& override { return *this; }
|
|
|
|
virtual boolean& as_boolean (void) & override { return *this; }
|
|
|
|
virtual bool is_boolean (void) const override { return true; }
|
2015-03-18 15:47:16 +11:00
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual type_t type (void) const override { return BOOLEAN; }
|
2015-03-18 15:47:16 +11:00
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual bool operator==(const boolean &rhs) const override;
|
|
|
|
virtual bool operator==(const node &rhs) const override
|
2015-02-03 00:07:09 +11:00
|
|
|
{ return rhs == *this; }
|
|
|
|
|
|
|
|
operator bool (void) const { return m_value; }
|
|
|
|
bool native (void) const { return m_value; }
|
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual std::ostream& write (std::ostream &os) const override;
|
2015-02-03 00:07:09 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Represents a JSON null value.
|
2015-03-18 15:45:33 +11:00
|
|
|
class null final : public node {
|
2015-02-03 00:07:09 +11:00
|
|
|
public:
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual type_t type (void) const override { return NONE; }
|
|
|
|
virtual std::unique_ptr<node> clone (void) const override;
|
2015-03-18 15:44:46 +11:00
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual bool operator==(const null&) const override { return true; }
|
|
|
|
virtual bool operator==(const node &rhs) const override
|
2015-02-03 00:07:09 +11:00
|
|
|
{ return rhs == *this; }
|
|
|
|
|
2016-03-17 18:05:28 +11:00
|
|
|
virtual const null& as_null (void) const& override { return *this; }
|
|
|
|
virtual null& as_null (void) & override { return *this; }
|
|
|
|
virtual bool is_null (void) const override { return true; }
|
2015-02-03 00:07:09 +11:00
|
|
|
|
2015-06-30 22:02:20 +10:00
|
|
|
virtual std::ostream& write (std::ostream &os) const override;
|
2015-02-03 00:07:09 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
std::ostream&
|
|
|
|
operator <<(std::ostream &os, const json::tree::node &n);
|
|
|
|
|
|
|
|
|
|
|
|
// Instantiate this template for the type you wish to output. We use a
|
|
|
|
// helper class here to avoid partial template specialisation of a
|
|
|
|
// function (eg, for templated types).
|
|
|
|
template <typename T>
|
|
|
|
struct io {
|
|
|
|
static std::unique_ptr<json::tree::node> serialise (const T&);
|
|
|
|
static T deserialise (const json::tree::node&);
|
|
|
|
};
|
|
|
|
} }
|
|
|
|
|
|
|
|
template <typename T, class ...Args>
|
2016-03-17 18:05:28 +11:00
|
|
|
std::unique_ptr<json::tree::node>
|
|
|
|
to_json (const T &t, Args&&... args)
|
|
|
|
{
|
|
|
|
return json::tree::io<T>::serialise (
|
|
|
|
t, std::forward<Args>(args)...
|
|
|
|
);
|
2015-02-03 00:07:09 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class ...Args>
|
2016-03-17 18:05:28 +11:00
|
|
|
T
|
|
|
|
from_json (const json::tree::node &n, Args&&... args)
|
|
|
|
{
|
|
|
|
return json::tree::io<T>::deserialise (
|
|
|
|
n, std::forward<Args>(args)...
|
|
|
|
);
|
2015-02-03 00:07:09 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|