libcruft-util/json.hpp

302 lines
11 KiB
C++
Raw Normal View History

2011-05-23 17:18:52 +10:00
/*
* This file is part of libgim.
2011-05-23 17:18:52 +10:00
*
* libgim is free software: you can redistribute it and/or modify it under the
2011-05-23 17:18:52 +10:00
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
2011-05-23 17:18:52 +10:00
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
2011-05-23 17:18:52 +10:00
*
2012-04-23 13:06:41 +10:00
* Copyright 2010-2012 Danny Robson <danny@nerdcruft.net>
2011-05-23 17:18:52 +10:00
*/
#ifndef __UTIL_JSON_HPP
#define __UTIL_JSON_HPP
2012-01-04 17:05:40 +11:00
#include <iostream>
2011-05-23 17:18:52 +10:00
#include <map>
2012-01-04 17:05:40 +11:00
#include <memory>
2011-05-23 17:18:52 +10:00
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
#include "iterator.hpp"
2011-05-23 17:18:52 +10:00
namespace json {
class node;
class object;
class array;
class string;
class number;
class boolean;
class null;
2014-05-26 17:10:31 +10:00
/// Parse an encoded form of JSON into a tree structure
2012-01-04 17:05:40 +11:00
extern std::unique_ptr<node> parse (const boost::filesystem::path &path);
extern std::unique_ptr<node> parse (const char *start, const char *stop);
extern std::unique_ptr<node> parse (const char *start);
extern std::unique_ptr<node> parse (const std::string&);
2011-05-23 17:18:52 +10:00
2012-04-12 14:09:33 +10:00
extern void write (const json::node&, std::ostream&);
2011-06-23 22:06:28 +10:00
/// Abstract base for all JSON values
2011-05-23 17:18:52 +10:00
class node {
public:
virtual ~node () { ; }
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;
2012-04-13 11:15:08 +10:00
virtual const null& as_null (void) const;
2011-05-23 17:18:52 +10:00
2012-04-12 14:09:33 +10: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; }
2014-05-20 14:11:28 +10:00
virtual bool is_boolean (void) const { return false; }
2012-04-12 14:09:33 +10:00
virtual bool is_null (void) const { return false; }
2011-05-23 17:18:52 +10: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; }
2012-01-04 17:05:40 +11:00
virtual bool operator==(const char *rhs) const;
virtual bool operator!=(const char *rhs) const { return !(*this == rhs); }
virtual const node& operator[] (const std::string&) const;
virtual const node& operator[] (unsigned int) const;
2011-05-23 17:18:52 +10:00
2012-04-12 14:09:33 +10:00
virtual std::ostream& write (std::ostream &os) const = 0;
2014-05-20 14:11:28 +10:00
};
2011-05-23 17:18:52 +10:00
2011-06-23 22:06:28 +10:00
/// Represents a JSON object, and contains its children.
2011-05-23 17:18:52 +10:00
class object : public node {
protected:
2012-04-19 16:44:35 +10:00
typedef std::map<std::string, std::unique_ptr<node>> value_store;
2012-04-20 18:07:16 +10:00
public:
2012-04-19 16:44:35 +10:00
typedef value_store::iterator iterator;
typedef value_store::const_iterator const_iterator;
2012-04-20 18:07:16 +10:00
protected:
2012-04-19 16:44:35 +10:00
value_store m_values;
2011-05-23 17:18:52 +10:00
public:
virtual ~object ();
virtual const object& as_object (void) const { return *this; }
2011-05-23 17:18:52 +10:00
virtual bool is_object (void) const { return true; }
virtual bool operator==(const object &rhs) const;
virtual bool operator==(const node &rhs) const
{ return rhs == *this; }
2012-04-20 18:09:23 +10:00
virtual void insert (const std::string &key, std::unique_ptr<node>&& value);
2011-05-23 17:18:52 +10:00
virtual const node& operator[](const std::string &key) const;
2012-04-20 18:09:23 +10:00
virtual bool has (const std::string&) const;
2011-05-23 17:18:52 +10:00
2012-04-19 16:44:35 +10:00
virtual void clear (void);
virtual void erase (const std::string &key);
virtual const_iterator begin (void) const;
virtual const_iterator end (void) const;
2012-04-12 14:09:33 +10:00
virtual std::ostream& write (std::ostream &os) const;
2011-05-23 17:18:52 +10:00
};
2011-06-23 22:06:28 +10:00
/// Represents a JSON array, and contains its children.
2011-05-23 17:18:52 +10:00
class array : public node {
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;
2011-05-23 17:18:52 +10:00
protected:
std::vector<std::unique_ptr<node>> m_values;
2011-05-23 17:18:52 +10:00
public:
virtual ~array();
virtual const array& as_array (void) const { return *this; }
2011-05-23 17:18:52 +10:00
virtual bool is_array (void) const { return true; }
virtual bool operator==(const array &rhs) const;
virtual bool operator==(const node &rhs) const
{ return rhs == *this; }
virtual size_t size (void) const
2014-05-20 14:11:28 +10:00
{ return m_values.size (); }
2011-05-23 17:18:52 +10:00
virtual node& operator [](unsigned int idx)
{ return *m_values[idx]; }
virtual const node& operator [](unsigned int idx) const
{ return *m_values[idx]; }
virtual const_iterator begin (void) const { return const_iterator (m_values.begin ()); }
virtual const_iterator end (void) const { return const_iterator (m_values.end ()); }
2011-05-23 17:18:52 +10:00
virtual void insert (std::unique_ptr<json::node> &&_value);
2011-05-23 17:18:52 +10:00
2012-04-12 14:09:33 +10:00
virtual std::ostream& write (std::ostream &os) const;
2011-05-23 17:18:52 +10:00
};
2011-06-23 22:06:28 +10:00
/// Represents a JSON string literal.
2011-05-23 17:18:52 +10:00
class string : public node {
protected:
std::string m_value;
public:
2012-04-20 18:07:42 +10:00
explicit string (const std::string &_value): m_value (_value) { ; }
explicit string (const char *_value): m_value (_value) { ; }
2011-05-23 17:18:52 +10:00
virtual const string& as_string (void) const { return *this; }
2011-05-23 17:18:52 +10:00
virtual bool is_string (void) const { return true; }
2012-04-20 18:18:47 +10:00
virtual bool operator==(const char *rhs) const;
2011-05-23 17:18:52 +10:00
virtual bool operator==(const string &rhs) const;
virtual bool operator==(const node &rhs) const
{ return rhs == *this; }
2012-04-19 16:45:10 +10:00
virtual size_t size (void) const { return m_value.size (); }
2011-05-23 17:18:52 +10:00
operator const std::string&(void) const { return m_value; }
const std::string& native (void) const { return m_value; }
2011-05-23 17:18:52 +10:00
2012-04-12 14:09:33 +10:00
virtual std::ostream& write (std::ostream &os) const;
2011-05-23 17:18:52 +10:00
};
2011-06-23 22:06:28 +10:00
/// Represents a JSON integer/float literal.
2011-05-23 17:18:52 +10:00
class number : public node {
protected:
double m_value;
public:
2012-04-20 18:07:42 +10:00
explicit number (double _value): m_value (_value) { ; }
explicit number (int _value): m_value (_value) { ; }
explicit number (size_t _value): m_value (_value) { ; }
2011-05-23 17:18:52 +10:00
virtual const number& as_number (void) const { return *this; }
2011-05-23 17:18:52 +10:00
virtual bool is_number (void) const { return true; }
virtual bool operator==(const number &rhs) const;
virtual bool operator==(const node &rhs) const
{ return rhs == *this; }
operator double(void) const { return m_value; }
double native (void) const { return m_value; }
2011-05-23 17:18:52 +10:00
2012-04-12 14:09:33 +10:00
virtual std::ostream& write (std::ostream &os) const;
2011-05-23 17:18:52 +10:00
};
2011-06-23 22:06:28 +10:00
/// Represents a JSON boolean literal.
2011-05-23 17:18:52 +10:00
class boolean : public node {
protected:
bool m_value;
public:
2012-04-20 18:07:42 +10:00
explicit boolean (bool _value): m_value (_value) { ; }
2011-05-23 17:18:52 +10:00
virtual const boolean& as_boolean (void) const { return *this; }
2011-05-23 17:18:52 +10:00
virtual bool is_boolean (void) const { return true; }
virtual bool operator==(const boolean &rhs) const;
virtual bool operator==(const node &rhs) const
{ return rhs == *this; }
2011-10-01 01:57:46 +10:00
operator bool (void) const { return m_value; }
bool native (void) const { return m_value; }
2012-04-12 14:09:33 +10:00
virtual std::ostream& write (std::ostream &os) const;
2011-05-23 17:18:52 +10:00
};
2011-06-23 22:06:28 +10:00
/// Represents a JSON null value.
2011-05-23 17:18:52 +10:00
class null : public node {
public:
virtual bool operator==(const null&) const { return true; }
virtual bool operator==(const node &rhs) const
{ return rhs == *this; }
2012-04-12 14:09:33 +10:00
2012-04-13 11:15:08 +10:00
virtual bool is_null (void) const { return true; }
virtual const null& as_null (void) const { return *this; }
2012-04-12 14:09:33 +10:00
virtual std::ostream& write (std::ostream &os) const;
2011-05-23 17:18:52 +10:00
};
2011-06-23 22:06:28 +10:00
/// The base class for all exceptions throw directly by the json namespace.
2011-05-23 17:18:52 +10:00
class error : public std::runtime_error {
public:
error (const std::string &_what):
std::runtime_error (_what)
{ ; }
};
/// Base class for all type conversion errors
class type_error : public error {
public:
type_error (const std::string &_what):
error (_what)
{ ; }
};
/// Base class for errors thrown during parsing
class parse_error : public error {
public:
parse_error (const std::string &_what):
error (_what)
{ ; }
};
/// Base class for errors thrown during schema validation
class schema_error : public error {
public:
schema_error (const std::string &_what):
error (_what)
{ ; }
};
2012-04-19 16:45:10 +10:00
std::ostream&
operator <<(std::ostream &os, const json::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::node> serialise (const T&);
static T deserialise (const json::node&);
};
}
2011-05-23 17:18:52 +10:00
template <typename T, class ...Args>
2013-08-05 20:54:19 +10:00
std::unique_ptr<json::node> to_json (const T &t, Args&&... args) {
return json::io<T>::serialise (t, std::forward<Args>(args)...);
}
template <typename T, class ...Args>
T from_json (const json::node &n, Args&&... args) {
return json::io<T>::deserialise (n, std::forward<Args>(args)...);
}
2011-05-23 17:18:52 +10:00
#endif