2012-04-25 20:13:17 +04:00
|
|
|
/*
|
|
|
|
grive: an GPL program to sync a local directory with Google Drive
|
|
|
|
Copyright (C) 2012 Wan Wai Ho
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
2012-04-26 19:40:38 +04:00
|
|
|
as published by the Free Software Foundation version 2
|
2012-04-26 08:21:40 +04:00
|
|
|
of the License.
|
2012-04-25 20:13:17 +04:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY 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 this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2012-05-13 11:27:58 +04:00
|
|
|
#include "util/Exception.hh"
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
struct json_object ;
|
|
|
|
|
|
|
|
namespace gr {
|
|
|
|
|
2013-04-28 14:40:21 +04:00
|
|
|
class DataStream ;
|
2012-05-24 15:06:03 +04:00
|
|
|
|
2013-04-28 14:40:21 +04:00
|
|
|
/*! \brief Simple wrapper around JSON-C objects.
|
|
|
|
|
|
|
|
This class represents JSON-C objects, which can be integers, booleans, strings
|
|
|
|
double, arrays and object.
|
|
|
|
*/
|
2012-04-25 20:13:17 +04:00
|
|
|
class Json
|
|
|
|
{
|
|
|
|
public :
|
|
|
|
typedef std::map<std::string, Json> Object ;
|
|
|
|
typedef std::vector<Json> Array ;
|
|
|
|
|
2012-05-13 11:27:58 +04:00
|
|
|
struct Error : virtual Exception {} ;
|
2013-04-28 14:40:21 +04:00
|
|
|
typedef boost::error_info<struct JsonTag, std::string> Json_ ;
|
|
|
|
typedef boost::error_info<struct OutOfRange, std::size_t> OutOfRange_ ;
|
|
|
|
typedef boost::error_info<struct KeyNotFound, std::string> KeyNotFound_ ;
|
|
|
|
typedef boost::error_info<struct JsonCApi, std::string> JsonCApi_ ;
|
|
|
|
typedef boost::error_info<struct Value, std::string> Value_ ;
|
2013-04-28 19:50:20 +04:00
|
|
|
typedef boost::error_info<struct ErrMsg, std::string> ErrMsg_ ;
|
2013-04-28 14:40:21 +04:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct Val_
|
|
|
|
{
|
|
|
|
typedef boost::error_info<struct Value, T> Err ;
|
|
|
|
} ;
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
public :
|
2012-04-26 05:20:41 +04:00
|
|
|
template <typename T>
|
|
|
|
explicit Json( const T& val ) ;
|
|
|
|
|
2013-04-28 14:40:21 +04:00
|
|
|
template <std::size_t n>
|
|
|
|
explicit Json( const char (&str)[n] ) :
|
|
|
|
m_json( InitStr( str, n ) )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-26 05:20:41 +04:00
|
|
|
Json() ;
|
2012-04-25 20:13:17 +04:00
|
|
|
Json( const Json& rhs ) ;
|
2012-05-24 20:55:28 +04:00
|
|
|
Json( const char *str ) ;
|
2013-04-28 14:40:21 +04:00
|
|
|
~Json() ;
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-04-26 05:20:41 +04:00
|
|
|
static Json Parse( const std::string& str ) ;
|
2013-04-28 14:40:21 +04:00
|
|
|
static Json Parse( DataStream *in ) ;
|
2012-04-26 05:20:41 +04:00
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
Json operator[]( const std::string& key ) const ;
|
2012-04-26 05:20:41 +04:00
|
|
|
Json operator[]( const std::size_t& idx ) const ;
|
2012-04-25 20:13:17 +04:00
|
|
|
Json& operator=( const Json& rhs ) ;
|
|
|
|
|
|
|
|
void Swap( Json& other ) ;
|
2012-05-15 20:54:58 +04:00
|
|
|
|
2012-05-01 19:24:40 +04:00
|
|
|
std::string Str() const ;
|
2012-05-15 20:54:58 +04:00
|
|
|
int Int() const ;
|
|
|
|
double Double() const ;
|
|
|
|
bool Bool() const ;
|
|
|
|
Array AsArray() const ;
|
|
|
|
Object AsObject() const ;
|
2012-05-01 19:24:40 +04:00
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
template <typename T>
|
|
|
|
bool Is() const ;
|
|
|
|
|
2013-04-28 14:40:21 +04:00
|
|
|
template <typename T>
|
|
|
|
T As() const ;
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
bool Has( const std::string& key ) const ;
|
2012-05-31 19:37:47 +04:00
|
|
|
bool Get( const std::string& key, Json& json ) const ;
|
2012-04-26 05:20:41 +04:00
|
|
|
void Add( const std::string& key, const Json& json ) ;
|
2012-05-16 20:52:17 +04:00
|
|
|
void Add( const Json& json ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
Json FindInArray( const std::string& key, const std::string& value ) const ;
|
|
|
|
bool FindInArray( const std::string& key, const std::string& value, Json& result ) const ;
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2013-04-29 11:45:58 +04:00
|
|
|
/** Expect *this is a JSON array of objects. Select all "key" values inside each
|
|
|
|
objects in the array and copies them in the output iterator \a out.
|
|
|
|
*/
|
|
|
|
template <typename T, typename Out>
|
|
|
|
Out Select( const std::string& key, Out out )
|
|
|
|
{
|
|
|
|
Array a = AsArray() ;
|
|
|
|
for ( Array::iterator i = a.begin() ; i != a.end() ; ++i )
|
|
|
|
*out++ = i->As<T>() ;
|
|
|
|
return out ;
|
|
|
|
}
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
friend std::ostream& operator<<( std::ostream& os, const Json& json ) ;
|
2013-04-28 14:40:21 +04:00
|
|
|
void Write( DataStream *out ) const ;
|
2012-04-25 20:13:17 +04:00
|
|
|
|
|
|
|
enum Type { null_type, bool_type, double_type, int_type, object_type, array_type, string_type } ;
|
|
|
|
|
|
|
|
Type DataType() const ;
|
|
|
|
|
|
|
|
private :
|
|
|
|
Json( struct json_object *json ) ;
|
|
|
|
|
2012-04-26 05:20:41 +04:00
|
|
|
struct NotOwned {} ;
|
|
|
|
Json( struct json_object *json, NotOwned ) ;
|
2013-04-28 14:40:21 +04:00
|
|
|
|
|
|
|
static struct json_object* InitStr( const char *str, std::size_t n ) ;
|
|
|
|
|
|
|
|
// helper for throwing exception
|
|
|
|
template <typename T> static typename Val_<T>::Err ValueErr( const T& t )
|
|
|
|
{
|
|
|
|
return typename Val_<T>::Err(t);
|
|
|
|
}
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
private :
|
|
|
|
struct json_object *m_json ;
|
|
|
|
} ;
|
|
|
|
|
2013-04-28 14:40:21 +04:00
|
|
|
}
|