mirror of https://github.com/vitalif/grive2
added operator<< to print out json
parent
6fa6e690c6
commit
6ba04dcfba
|
@ -18,7 +18,7 @@
|
||||||
MA 02110-1301, USA.
|
MA 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ValWriter.hh"
|
#include "JsonWriter.hh"
|
||||||
#include "util/DataStream.hh"
|
#include "util/DataStream.hh"
|
||||||
|
|
||||||
#include <yajl/yajl_gen.h>
|
#include <yajl/yajl_gen.h>
|
||||||
|
@ -27,81 +27,81 @@
|
||||||
|
|
||||||
namespace gr {
|
namespace gr {
|
||||||
|
|
||||||
struct ValWriter::Impl
|
struct JsonWriter::Impl
|
||||||
{
|
{
|
||||||
yajl_gen gen ;
|
yajl_gen gen ;
|
||||||
DataStream *out ;
|
DataStream *out ;
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
ValWriter::ValWriter( DataStream *out ) :
|
JsonWriter::JsonWriter( DataStream *out ) :
|
||||||
m_impl( new Impl )
|
m_impl( new Impl )
|
||||||
{
|
{
|
||||||
assert( out != 0 ) ;
|
assert( out != 0 ) ;
|
||||||
|
|
||||||
m_impl->out = out ;
|
m_impl->out = out ;
|
||||||
m_impl->gen = yajl_gen_alloc(0) ;
|
m_impl->gen = yajl_gen_alloc(0) ;
|
||||||
yajl_gen_config( m_impl->gen, yajl_gen_print_callback, &ValWriter::WriteCallback, this ) ;
|
yajl_gen_config( m_impl->gen, yajl_gen_print_callback, &JsonWriter::WriteCallback, this ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
ValWriter::~ValWriter()
|
JsonWriter::~JsonWriter()
|
||||||
{
|
{
|
||||||
yajl_gen_free( m_impl->gen ) ;
|
yajl_gen_free( m_impl->gen ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValWriter::Visit( long long t )
|
void JsonWriter::Visit( long long t )
|
||||||
{
|
{
|
||||||
yajl_gen_integer( m_impl->gen, t ) ;
|
yajl_gen_integer( m_impl->gen, t ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValWriter::Visit( double t )
|
void JsonWriter::Visit( double t )
|
||||||
{
|
{
|
||||||
yajl_gen_double( m_impl->gen, t ) ;
|
yajl_gen_double( m_impl->gen, t ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValWriter::Visit( const std::string& t )
|
void JsonWriter::Visit( const std::string& t )
|
||||||
{
|
{
|
||||||
yajl_gen_string( m_impl->gen,
|
yajl_gen_string( m_impl->gen,
|
||||||
reinterpret_cast<const unsigned char*>(t.c_str()), t.size() ) ;
|
reinterpret_cast<const unsigned char*>(t.c_str()), t.size() ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValWriter::Visit( bool t )
|
void JsonWriter::Visit( bool t )
|
||||||
{
|
{
|
||||||
yajl_gen_bool( m_impl->gen, t ) ;
|
yajl_gen_bool( m_impl->gen, t ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValWriter::VisitNull()
|
void JsonWriter::VisitNull()
|
||||||
{
|
{
|
||||||
yajl_gen_null( m_impl->gen ) ;
|
yajl_gen_null( m_impl->gen ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValWriter::StartArray()
|
void JsonWriter::StartArray()
|
||||||
{
|
{
|
||||||
yajl_gen_array_open( m_impl->gen ) ;
|
yajl_gen_array_open( m_impl->gen ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValWriter::EndArray()
|
void JsonWriter::EndArray()
|
||||||
{
|
{
|
||||||
yajl_gen_array_close( m_impl->gen ) ;
|
yajl_gen_array_close( m_impl->gen ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValWriter::StartObject()
|
void JsonWriter::StartObject()
|
||||||
{
|
{
|
||||||
yajl_gen_map_open( m_impl->gen ) ;
|
yajl_gen_map_open( m_impl->gen ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValWriter::VisitKey( const std::string& t )
|
void JsonWriter::VisitKey( const std::string& t )
|
||||||
{
|
{
|
||||||
Visit(t) ;
|
Visit(t) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValWriter::EndObject()
|
void JsonWriter::EndObject()
|
||||||
{
|
{
|
||||||
yajl_gen_map_close( m_impl->gen ) ;
|
yajl_gen_map_close( m_impl->gen ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValWriter::WriteCallback( void *ctx, const char *str, std::size_t size )
|
void JsonWriter::WriteCallback( void *ctx, const char *str, std::size_t size )
|
||||||
{
|
{
|
||||||
ValWriter *pthis = reinterpret_cast<ValWriter*>(ctx) ;
|
JsonWriter *pthis = reinterpret_cast<JsonWriter*>(ctx) ;
|
||||||
assert( pthis != 0 ) ;
|
assert( pthis != 0 ) ;
|
||||||
assert( pthis->m_impl->out != 0 ) ;
|
assert( pthis->m_impl->out != 0 ) ;
|
||||||
|
|
|
@ -27,11 +27,11 @@ namespace gr {
|
||||||
|
|
||||||
class DataStream ;
|
class DataStream ;
|
||||||
|
|
||||||
class ValWriter : public ValVisitor
|
class JsonWriter : public ValVisitor
|
||||||
{
|
{
|
||||||
public :
|
public :
|
||||||
ValWriter( DataStream *out ) ;
|
JsonWriter( DataStream *out ) ;
|
||||||
~ValWriter() ;
|
~JsonWriter() ;
|
||||||
|
|
||||||
void Visit( long long t ) ;
|
void Visit( long long t ) ;
|
||||||
void Visit( double t ) ;
|
void Visit( double t ) ;
|
|
@ -19,7 +19,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Val.hh"
|
#include "Val.hh"
|
||||||
|
#include "JsonWriter.hh"
|
||||||
#include "ValVisitor.hh"
|
#include "ValVisitor.hh"
|
||||||
|
#include "util/StdStream.hh"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
@ -172,6 +174,15 @@ void Val::Visit( ValVisitor *visitor ) const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream& operator<<( std::ostream& os, const Val& val )
|
||||||
|
{
|
||||||
|
StdStream ss( os.rdbuf() ) ;
|
||||||
|
JsonWriter wr( &ss ) ;
|
||||||
|
val.Visit( &wr ) ;
|
||||||
|
|
||||||
|
return os ;
|
||||||
|
}
|
||||||
|
|
||||||
} // end of namespace
|
} // end of namespace
|
||||||
|
|
||||||
namespace std
|
namespace std
|
||||||
|
|
|
@ -111,7 +111,7 @@ public :
|
||||||
Val FindInArray( const std::string& key, const std::string& value ) const ;
|
Val FindInArray( const std::string& key, const std::string& value ) const ;
|
||||||
bool FindInArray( const std::string& key, const std::string& value, Val& result ) const ;
|
bool FindInArray( const std::string& key, const std::string& value, Val& result ) const ;
|
||||||
|
|
||||||
// friend std::ostream& operator<<( std::ostream& os, const Val& json ) ;
|
friend std::ostream& operator<<( std::ostream& os, const Val& val ) ;
|
||||||
void Visit( ValVisitor *visitor ) const ;
|
void Visit( ValVisitor *visitor ) const ;
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
grive: an GPL program to sync a local directory with Google Drive
|
||||||
|
Copyright (C) 2013 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.
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "StdStream.hh"
|
||||||
|
|
||||||
|
#include <streambuf>
|
||||||
|
|
||||||
|
namespace gr {
|
||||||
|
|
||||||
|
StdStream::StdStream( std::streambuf *buf ) :
|
||||||
|
m_adaptee( buf )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t StdStream::Read( char *data, std::size_t size )
|
||||||
|
{
|
||||||
|
return m_adaptee == 0 ? 0 : m_adaptee->sgetn( data, size ) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t StdStream::Write( const char *data, std::size_t size )
|
||||||
|
{
|
||||||
|
return m_adaptee == 0 ? 0 : m_adaptee->sputn( data, size ) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end of namespace
|
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
grive: an GPL program to sync a local directory with Google Drive
|
||||||
|
Copyright (C) 2013 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.
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
#include "DataStream.hh"
|
||||||
|
|
||||||
|
#include <iosfwd>
|
||||||
|
|
||||||
|
namespace gr {
|
||||||
|
|
||||||
|
class StdStream : public DataStream
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
explicit StdStream( std::streambuf *buf ) ;
|
||||||
|
|
||||||
|
std::size_t Read( char *data, std::size_t size ) ;
|
||||||
|
std::size_t Write( const char *data, std::size_t size ) ;
|
||||||
|
|
||||||
|
private :
|
||||||
|
std::streambuf *m_adaptee ;
|
||||||
|
} ;
|
||||||
|
|
||||||
|
} // end of namespace
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
#include "json/JsonParser.hh"
|
#include "json/JsonParser.hh"
|
||||||
#include "json/Val.hh"
|
#include "json/Val.hh"
|
||||||
#include "json/ValBuilder.hh"
|
#include "json/ValBuilder.hh"
|
||||||
#include "json/ValWriter.hh"
|
#include "json/JsonWriter.hh"
|
||||||
#include "util/StringStream.hh"
|
#include "util/StringStream.hh"
|
||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE( Test )
|
||||||
BOOST_CHECK_EQUAL( json["key"].As<long long>(), 100 ) ;
|
BOOST_CHECK_EQUAL( json["key"].As<long long>(), 100 ) ;
|
||||||
|
|
||||||
StringStream ss ;
|
StringStream ss ;
|
||||||
ValWriter wr( &ss ) ;
|
JsonWriter wr( &ss ) ;
|
||||||
json.Visit( &wr ) ;
|
json.Visit( &wr ) ;
|
||||||
|
|
||||||
BOOST_CHECK_EQUAL( ss.Str(), "{\"key\":100}" ) ;
|
BOOST_CHECK_EQUAL( ss.Str(), "{\"key\":100}" ) ;
|
||||||
|
|
Loading…
Reference in New Issue