diff --git a/libgrive/src/json/ValWriter.cc b/libgrive/src/json/JsonWriter.cc similarity index 69% rename from libgrive/src/json/ValWriter.cc rename to libgrive/src/json/JsonWriter.cc index 502e432..9fbcc2b 100644 --- a/libgrive/src/json/ValWriter.cc +++ b/libgrive/src/json/JsonWriter.cc @@ -18,7 +18,7 @@ MA 02110-1301, USA. */ -#include "ValWriter.hh" +#include "JsonWriter.hh" #include "util/DataStream.hh" #include @@ -27,81 +27,81 @@ namespace gr { -struct ValWriter::Impl +struct JsonWriter::Impl { yajl_gen gen ; DataStream *out ; } ; -ValWriter::ValWriter( DataStream *out ) : +JsonWriter::JsonWriter( DataStream *out ) : m_impl( new Impl ) { assert( out != 0 ) ; m_impl->out = out ; 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 ) ; } -void ValWriter::Visit( long long t ) +void JsonWriter::Visit( long long 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 ) ; } -void ValWriter::Visit( const std::string& t ) +void JsonWriter::Visit( const std::string& t ) { yajl_gen_string( m_impl->gen, reinterpret_cast(t.c_str()), t.size() ) ; } -void ValWriter::Visit( bool t ) +void JsonWriter::Visit( bool t ) { yajl_gen_bool( m_impl->gen, t ) ; } -void ValWriter::VisitNull() +void JsonWriter::VisitNull() { yajl_gen_null( m_impl->gen ) ; } -void ValWriter::StartArray() +void JsonWriter::StartArray() { yajl_gen_array_open( m_impl->gen ) ; } -void ValWriter::EndArray() +void JsonWriter::EndArray() { yajl_gen_array_close( m_impl->gen ) ; } -void ValWriter::StartObject() +void JsonWriter::StartObject() { yajl_gen_map_open( m_impl->gen ) ; } -void ValWriter::VisitKey( const std::string& t ) +void JsonWriter::VisitKey( const std::string& t ) { Visit(t) ; } -void ValWriter::EndObject() +void JsonWriter::EndObject() { 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(ctx) ; + JsonWriter *pthis = reinterpret_cast(ctx) ; assert( pthis != 0 ) ; assert( pthis->m_impl->out != 0 ) ; diff --git a/libgrive/src/json/ValWriter.hh b/libgrive/src/json/JsonWriter.hh similarity index 93% rename from libgrive/src/json/ValWriter.hh rename to libgrive/src/json/JsonWriter.hh index 351cbd8..350357d 100644 --- a/libgrive/src/json/ValWriter.hh +++ b/libgrive/src/json/JsonWriter.hh @@ -27,11 +27,11 @@ namespace gr { class DataStream ; -class ValWriter : public ValVisitor +class JsonWriter : public ValVisitor { public : - ValWriter( DataStream *out ) ; - ~ValWriter() ; + JsonWriter( DataStream *out ) ; + ~JsonWriter() ; void Visit( long long t ) ; void Visit( double t ) ; diff --git a/libgrive/src/json/Val.cc b/libgrive/src/json/Val.cc index e4c2a85..02f7dff 100644 --- a/libgrive/src/json/Val.cc +++ b/libgrive/src/json/Val.cc @@ -19,7 +19,9 @@ */ #include "Val.hh" +#include "JsonWriter.hh" #include "ValVisitor.hh" +#include "util/StdStream.hh" #include @@ -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 namespace std diff --git a/libgrive/src/json/Val.hh b/libgrive/src/json/Val.hh index 46003f0..56e43bc 100644 --- a/libgrive/src/json/Val.hh +++ b/libgrive/src/json/Val.hh @@ -111,7 +111,7 @@ public : Val FindInArray( const std::string& key, const std::string& value ) 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 ; private : diff --git a/libgrive/src/util/StdStream.cc b/libgrive/src/util/StdStream.cc new file mode 100644 index 0000000..eaa10ce --- /dev/null +++ b/libgrive/src/util/StdStream.cc @@ -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 + +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 diff --git a/libgrive/src/util/StdStream.hh b/libgrive/src/util/StdStream.hh new file mode 100644 index 0000000..4751e38 --- /dev/null +++ b/libgrive/src/util/StdStream.hh @@ -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 + +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 + diff --git a/libgrive/test/btest/JsonValTest.cc b/libgrive/test/btest/JsonValTest.cc index 61e98da..d5087cd 100644 --- a/libgrive/test/btest/JsonValTest.cc +++ b/libgrive/test/btest/JsonValTest.cc @@ -20,7 +20,7 @@ #include "json/JsonParser.hh" #include "json/Val.hh" #include "json/ValBuilder.hh" -#include "json/ValWriter.hh" +#include "json/JsonWriter.hh" #include "util/StringStream.hh" #include @@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE( Test ) BOOST_CHECK_EQUAL( json["key"].As(), 100 ) ; StringStream ss ; - ValWriter wr( &ss ) ; + JsonWriter wr( &ss ) ; json.Visit( &wr ) ; BOOST_CHECK_EQUAL( ss.Str(), "{\"key\":100}" ) ;