grive2/libgrive/src/protocol/Json.hh

89 lines
2.2 KiB
C++
Raw Normal View History

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
as published by the Free Software Foundation version 2
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 {
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 {} ;
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 ) ;
Json() ;
2012-04-25 20:13:17 +04:00
Json( const Json& rhs ) ;
~Json( ) ;
2012-04-26 05:20:41 +04:00
static Json Parse( const std::string& str ) ;
static Json ParseFile( const std::string& filename ) ;
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 ) ;
std::string Str() const ;
int Int() const ;
double Double() const ;
bool Bool() const ;
Array AsArray() const ;
Object AsObject() const ;
2012-04-25 20:13:17 +04:00
template <typename T>
bool Is() const ;
bool Has( const std::string& key ) const ;
2012-04-26 05:20:41 +04:00
void Add( const std::string& key, const Json& json ) ;
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
friend std::ostream& operator<<( std::ostream& os, const Json& json ) ;
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 ) ;
2012-04-25 20:13:17 +04:00
private :
2012-05-09 18:25:42 +04:00
public :
2012-04-25 20:13:17 +04:00
struct json_object *m_json ;
} ;
}