2010-01-13 08:07:57 +08:00
|
|
|
// janssonxx - C++ wrapper for jansson
|
|
|
|
//
|
|
|
|
// author: Sean Middleditch <sean@middleditch.us>
|
|
|
|
//
|
|
|
|
// janssonxx is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the MIT license. See LICENSE for details.
|
|
|
|
|
|
|
|
|
2010-01-12 17:10:09 +08:00
|
|
|
#if !defined(JANSSONXX_H)
|
|
|
|
#define JANSSONXX_H 1
|
|
|
|
|
|
|
|
#include <string>
|
2010-01-13 07:29:45 +08:00
|
|
|
#include <ostream>
|
|
|
|
#include <istream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <cstdlib>
|
2010-01-12 17:10:09 +08:00
|
|
|
|
|
|
|
namespace jansson {
|
2010-01-17 12:15:33 +08:00
|
|
|
// include Jansson C library in the jansson namespace
|
|
|
|
#include <jansson.h>
|
2010-01-16 17:13:19 +08:00
|
|
|
|
|
|
|
class Iterator;
|
|
|
|
class Value;
|
|
|
|
|
2010-01-17 12:15:33 +08:00
|
|
|
// implementation details; do not use directly
|
2010-01-16 17:36:13 +08:00
|
|
|
namespace _private {
|
2010-01-16 17:40:16 +08:00
|
|
|
class ElementProxy;
|
|
|
|
class PropertyProxy;
|
2010-01-16 17:36:13 +08:00
|
|
|
|
|
|
|
// base class for JSON value interface
|
|
|
|
template <typename _Base>
|
|
|
|
class ValueBase : public _Base {
|
|
|
|
public:
|
|
|
|
// empty constructor
|
|
|
|
ValueBase() : _Base() {}
|
|
|
|
|
|
|
|
// copy constructor
|
|
|
|
ValueBase(const _Base& base) : _Base(base) {}
|
2010-01-16 17:13:19 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
// create reference to value
|
|
|
|
ValueBase(json_t* json) : _Base(json) {}
|
2010-01-16 17:13:19 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
// assignment operator
|
2010-01-17 12:15:33 +08:00
|
|
|
inline ValueBase& operator=(const Value& value);
|
2010-01-16 17:36:13 +08:00
|
|
|
|
|
|
|
// check value type
|
2010-01-17 12:15:33 +08:00
|
|
|
inline bool is_undefined() const;
|
|
|
|
inline bool is_object() const;
|
|
|
|
inline bool is_array() const;
|
|
|
|
inline bool is_string() const;
|
|
|
|
inline bool is_integer() const;
|
|
|
|
inline bool is_real() const;
|
|
|
|
inline bool is_number() const;
|
|
|
|
inline bool is_true() const;
|
|
|
|
inline bool is_false() const;
|
|
|
|
inline bool is_boolean() const;
|
|
|
|
inline bool is_null() const;
|
2010-01-16 17:36:13 +08:00
|
|
|
|
|
|
|
// get size of array or object
|
|
|
|
inline unsigned int size() const;
|
|
|
|
|
|
|
|
// get value at array index (const version)
|
|
|
|
inline const Value at(unsigned int index) const;
|
|
|
|
|
|
|
|
inline const Value operator[](signed int index) const;
|
|
|
|
inline const Value operator[](unsigned int index) const;
|
|
|
|
inline const Value operator[](signed short index) const;
|
|
|
|
inline const Value operator[](unsigned short index) const;
|
|
|
|
inline const Value operator[](signed long index) const;
|
|
|
|
inline const Value operator[](unsigned long index) const;
|
|
|
|
|
|
|
|
// get value at array index (non-const version)
|
2010-01-16 17:40:16 +08:00
|
|
|
inline ValueBase<ElementProxy> at(unsigned int index);
|
2010-01-16 17:36:13 +08:00
|
|
|
|
2010-01-16 17:40:16 +08:00
|
|
|
inline ValueBase<ElementProxy> operator[](signed int index);
|
|
|
|
inline ValueBase<ElementProxy> operator[](unsigned int index);
|
|
|
|
inline ValueBase<ElementProxy> operator[](signed short index);
|
|
|
|
inline ValueBase<ElementProxy> operator[](unsigned short index);
|
|
|
|
inline ValueBase<ElementProxy> operator[](signed long index);
|
|
|
|
inline ValueBase<ElementProxy> operator[](unsigned long index);
|
2010-01-16 17:36:13 +08:00
|
|
|
|
|
|
|
// get object property (const version)
|
|
|
|
inline const Value get(const char* key) const;
|
|
|
|
|
|
|
|
inline const Value get(const std::string& key) const;
|
|
|
|
inline const Value operator[](const char* key) const;
|
|
|
|
inline const Value operator[](const std::string& key) const;
|
|
|
|
|
|
|
|
// get object property (non-const version)
|
2010-01-16 17:40:16 +08:00
|
|
|
inline ValueBase<PropertyProxy> get(const char* key);
|
2010-01-16 17:36:13 +08:00
|
|
|
|
2010-01-16 17:40:16 +08:00
|
|
|
inline ValueBase<PropertyProxy> get(const std::string& key);
|
|
|
|
inline ValueBase<PropertyProxy> operator[](const char* key);
|
|
|
|
inline ValueBase<PropertyProxy> operator[](const std::string& key);
|
2010-01-16 17:36:13 +08:00
|
|
|
|
|
|
|
// clear all array/object values
|
|
|
|
inline void clear();
|
|
|
|
|
|
|
|
// get value cast to specified type
|
|
|
|
inline const char* as_cstring() const;
|
|
|
|
inline std::string as_string() const;
|
|
|
|
inline int as_integer() const;
|
|
|
|
inline double as_real() const;
|
|
|
|
inline double as_number() const;
|
|
|
|
inline bool as_boolean() const;
|
|
|
|
|
|
|
|
// set an object property (converts value to object is not one already)
|
|
|
|
inline _Base& set_key(const char* key, const Value& value);
|
|
|
|
|
|
|
|
inline _Base& set_key(const std::string& key, const Value& value);
|
|
|
|
|
|
|
|
// set an array index (converts value to object is not one already)
|
|
|
|
inline _Base& set_at(unsigned int index, const Value& value);
|
|
|
|
|
|
|
|
// delete an object key
|
|
|
|
inline _Base& del_key(const char* key);
|
|
|
|
|
|
|
|
inline _Base& del_key(const std::string& key);
|
|
|
|
|
|
|
|
// delete an item from an array by index
|
|
|
|
inline _Base& del_at(unsigned int index);
|
|
|
|
|
|
|
|
// insert an item into an array at a given index
|
|
|
|
inline _Base& insert_at(unsigned int index, const Value& value);
|
2010-01-17 12:15:33 +08:00
|
|
|
|
|
|
|
// write the value to a file
|
|
|
|
inline int save_file(const char* path, int flags = 0) const;
|
|
|
|
|
|
|
|
// write the value to a string (caller must deallocate with free()!)
|
|
|
|
inline char* save_string(int flags = 0) const;
|
2010-01-16 17:36:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// represents any JSON value, private base
|
|
|
|
class Basic {
|
|
|
|
public:
|
|
|
|
// construct new Value with an undefined value
|
|
|
|
Basic() : _value(0) {}
|
|
|
|
|
|
|
|
// copy constructor
|
|
|
|
Basic(const Basic& value) : _value(json_incref(value._value)) {}
|
|
|
|
|
|
|
|
// make a reference to an existing json_t value
|
|
|
|
explicit Basic(json_t* value) : _value(json_incref(value)) {}
|
|
|
|
|
|
|
|
// free Value resources
|
2010-01-17 12:15:33 +08:00
|
|
|
inline ~Basic();
|
2010-01-16 17:36:13 +08:00
|
|
|
|
|
|
|
// copy an existing Value
|
2010-01-17 12:15:33 +08:00
|
|
|
inline Basic& operator=(const Basic& e);
|
2010-01-16 17:13:19 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
// get the underlying json_t
|
2010-01-17 12:15:33 +08:00
|
|
|
inline json_t* as_json() const;
|
2010-01-16 17:13:19 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
protected:
|
|
|
|
// take ownership of a json_t (does not increase reference count)
|
2010-01-17 12:15:33 +08:00
|
|
|
static inline Basic _take(json_t* json);
|
2010-01-13 08:17:11 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
private:
|
|
|
|
// internal value pointer
|
|
|
|
json_t* _value;
|
|
|
|
};
|
2010-01-13 08:17:11 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
// proxies an array element
|
2010-01-16 17:40:16 +08:00
|
|
|
class ElementProxy {
|
2010-01-16 17:36:13 +08:00
|
|
|
public:
|
|
|
|
// constructor
|
2010-01-16 17:40:16 +08:00
|
|
|
ElementProxy(json_t* array, unsigned int index) : _array(array), _index(index) {}
|
2010-01-13 08:17:11 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
// assign to the proxied element
|
2010-01-16 17:40:16 +08:00
|
|
|
inline ElementProxy& operator=(const Value& value);
|
2010-01-16 17:13:19 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
// get the proxied element
|
2010-01-17 12:15:33 +08:00
|
|
|
inline json_t* as_json() const;
|
2010-01-16 17:24:27 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
private:
|
|
|
|
// array object we wrap
|
|
|
|
json_t* _array;
|
2010-01-16 17:24:27 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
// index of property
|
|
|
|
unsigned int _index;
|
|
|
|
};
|
2010-01-16 17:24:27 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
// proxies an object property
|
2010-01-16 17:40:16 +08:00
|
|
|
class PropertyProxy {
|
2010-01-16 17:36:13 +08:00
|
|
|
public:
|
|
|
|
// constructor
|
2010-01-16 17:40:16 +08:00
|
|
|
PropertyProxy(json_t* array, const char* key) : _object(array), _key(key) {}
|
2010-01-16 17:24:27 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
// assign to the proxied element
|
2010-01-16 17:40:16 +08:00
|
|
|
inline PropertyProxy& operator=(const Value& value);
|
2010-01-16 17:31:37 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
// get the proxied element
|
2010-01-17 12:15:33 +08:00
|
|
|
inline json_t* as_json() const;
|
2010-01-16 17:31:37 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
private:
|
|
|
|
// array object we wrap
|
|
|
|
json_t* _object;
|
2010-01-16 17:31:37 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
// key of property
|
|
|
|
const char* _key;
|
|
|
|
};
|
2010-01-16 17:31:37 +08:00
|
|
|
|
2010-01-16 17:36:13 +08:00
|
|
|
} // namespace jansson::_private
|
2010-01-16 17:31:37 +08:00
|
|
|
|
2010-01-16 17:13:19 +08:00
|
|
|
// represents any JSON value
|
2010-01-16 17:36:13 +08:00
|
|
|
class Value : public _private::ValueBase<_private::Basic> {
|
2010-01-16 17:13:19 +08:00
|
|
|
public:
|
|
|
|
// empty constructor
|
2010-01-16 17:36:13 +08:00
|
|
|
Value() : _private::ValueBase<_private::Basic>() {}
|
2010-01-16 17:13:19 +08:00
|
|
|
|
|
|
|
// copy constructor for base
|
2010-01-16 17:36:13 +08:00
|
|
|
Value(const _private::Basic& value) : _private::ValueBase<_private::Basic>(value) {}
|
2010-01-16 17:13:19 +08:00
|
|
|
|
|
|
|
// copy constructor for base
|
2010-01-16 17:36:13 +08:00
|
|
|
Value(const _private::ValueBase<_private::Basic>& value) : _private::ValueBase<_private::Basic>(value) {}
|
2010-01-16 17:13:19 +08:00
|
|
|
|
|
|
|
// copy constructor
|
2010-01-16 17:36:13 +08:00
|
|
|
Value(const Value& value) : _private::ValueBase<_private::Basic>(value) {}
|
2010-01-16 17:13:19 +08:00
|
|
|
|
|
|
|
// create reference to value
|
2010-01-16 17:36:13 +08:00
|
|
|
explicit Value(json_t* json) : _private::ValueBase<_private::Basic>(json) {}
|
2010-01-16 17:13:19 +08:00
|
|
|
|
|
|
|
// construct Value from input
|
2010-01-17 12:15:33 +08:00
|
|
|
static inline Value from(const char* value);
|
|
|
|
static inline Value from(const std::string& value);
|
|
|
|
static inline Value from(bool value);
|
|
|
|
static inline Value from(signed int value);
|
|
|
|
static inline Value from(unsigned int value);
|
|
|
|
static inline Value from(signed short value);
|
|
|
|
static inline Value from(unsigned short value);
|
|
|
|
static inline Value from(signed long value);
|
|
|
|
static inline Value from(unsigned long value);
|
|
|
|
static inline Value from(float value);
|
|
|
|
static inline Value from(double value);
|
2010-01-16 17:13:19 +08:00
|
|
|
|
|
|
|
// create a new empty object
|
2010-01-17 12:15:33 +08:00
|
|
|
static inline Value object();
|
2010-01-16 17:13:19 +08:00
|
|
|
|
|
|
|
// create a new empty array
|
2010-01-17 12:15:33 +08:00
|
|
|
static inline Value array();
|
2010-01-16 17:13:19 +08:00
|
|
|
|
|
|
|
// create a new null value
|
2010-01-17 12:15:33 +08:00
|
|
|
static inline Value null();
|
2010-01-16 17:13:19 +08:00
|
|
|
|
|
|
|
// load a file as a JSON value
|
2010-01-17 12:15:33 +08:00
|
|
|
static inline Value load_file(const char* path, json_error_t* error = 0);
|
2010-01-12 17:26:47 +08:00
|
|
|
|
2010-01-16 17:13:19 +08:00
|
|
|
// load a string as a JSON value
|
2010-01-17 12:15:33 +08:00
|
|
|
static inline Value load_string(const char* string, json_error_t* error = 0);
|
2010-01-16 17:13:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// iterators over a JSON object
|
|
|
|
class Iterator {
|
|
|
|
public:
|
|
|
|
// construct a new iterator for a given object
|
2010-01-17 12:15:33 +08:00
|
|
|
inline Iterator(const Value& value);
|
2010-01-12 17:10:09 +08:00
|
|
|
|
2010-01-16 17:31:37 +08:00
|
|
|
// construct a new iterator for a given object
|
2010-01-17 12:15:33 +08:00
|
|
|
inline Iterator(const _private::ValueBase<_private::PropertyProxy>& value);
|
2010-01-16 17:31:37 +08:00
|
|
|
|
2010-01-16 17:13:19 +08:00
|
|
|
// increment iterator
|
2010-01-17 12:15:33 +08:00
|
|
|
inline void next();
|
2010-01-12 17:10:09 +08:00
|
|
|
|
2010-01-17 12:15:33 +08:00
|
|
|
inline Iterator& operator++();
|
2010-01-12 17:10:09 +08:00
|
|
|
|
2010-01-16 17:13:19 +08:00
|
|
|
// test if iterator is still valid
|
2010-01-17 12:15:33 +08:00
|
|
|
inline bool valid() const;
|
|
|
|
|
|
|
|
inline operator bool() const;
|
2010-01-12 17:10:09 +08:00
|
|
|
|
2010-01-16 17:13:19 +08:00
|
|
|
// get key
|
2010-01-17 12:15:33 +08:00
|
|
|
inline const char* ckey() const;
|
2010-01-12 17:10:09 +08:00
|
|
|
|
2010-01-17 12:15:33 +08:00
|
|
|
inline std::string key() const;
|
2010-01-12 17:10:09 +08:00
|
|
|
|
2010-01-16 17:13:19 +08:00
|
|
|
// get value
|
2010-01-17 12:15:33 +08:00
|
|
|
inline const Value value() const;
|
2010-01-12 17:10:09 +08:00
|
|
|
|
2010-01-16 17:13:19 +08:00
|
|
|
// dereference value
|
2010-01-17 12:15:33 +08:00
|
|
|
inline const Value operator*() const;
|
2010-01-12 17:10:09 +08:00
|
|
|
|
2010-01-16 17:13:19 +08:00
|
|
|
private:
|
|
|
|
// disallow copying
|
|
|
|
Iterator(const Iterator&);
|
|
|
|
Iterator& operator=(const Iterator&);
|
2010-01-12 17:10:09 +08:00
|
|
|
|
2010-01-16 17:13:19 +08:00
|
|
|
// object being iterated over
|
|
|
|
Value _object;
|
2010-01-12 17:10:09 +08:00
|
|
|
|
2010-01-16 17:13:19 +08:00
|
|
|
// iterator value
|
|
|
|
void* _iter;
|
|
|
|
};
|
2010-01-12 17:10:09 +08:00
|
|
|
|
|
|
|
} // namespace jansson
|
|
|
|
|
2010-01-13 07:29:45 +08:00
|
|
|
// stream JSON value out
|
2010-01-17 12:15:33 +08:00
|
|
|
inline std::ostream& operator<<(std::ostream& os, const jansson::Value& value);
|
2010-01-13 07:29:45 +08:00
|
|
|
|
|
|
|
// read JSON value
|
2010-01-17 12:15:33 +08:00
|
|
|
inline std::istream& operator>>(std::istream& is, jansson::Value& value);
|
2010-01-13 07:29:45 +08:00
|
|
|
|
2010-01-17 12:17:48 +08:00
|
|
|
// include implementation code
|
2010-01-16 17:13:19 +08:00
|
|
|
#include "janssonxx.tcc"
|
|
|
|
|
2010-01-17 12:17:48 +08:00
|
|
|
#endif // defined(JANSSONXX_H)
|