Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added operator<< to print out json

  • Loading branch information...
commit 6ba04dcfba110bca134fc72f055d2be991771a1e 1 parent 6fa6e69
Nestal Wan nestal authored
34 libgrive/src/json/ValWriter.cc → libgrive/src/json/JsonWriter.cc
View
@@ -18,7 +18,7 @@
MA 02110-1301, USA.
*/
-#include "ValWriter.hh"
+#include "JsonWriter.hh"
#include "util/DataStream.hh"
#include <yajl/yajl_gen.h>
@@ -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<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 ) ;
}
-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<ValWriter*>(ctx) ;
+ JsonWriter *pthis = reinterpret_cast<JsonWriter*>(ctx) ;
assert( pthis != 0 ) ;
assert( pthis->m_impl->out != 0 ) ;
6 libgrive/src/json/ValWriter.hh → libgrive/src/json/JsonWriter.hh
View
@@ -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 ) ;
11 libgrive/src/json/Val.cc
View
@@ -19,7 +19,9 @@
*/
#include "Val.hh"
+#include "JsonWriter.hh"
#include "ValVisitor.hh"
+#include "util/StdStream.hh"
#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
namespace std
2  libgrive/src/json/Val.hh
View
@@ -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 :
42 libgrive/src/util/StdStream.cc
View
@@ -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
42 libgrive/src/util/StdStream.hh
View
@@ -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
+
4 libgrive/test/btest/JsonValTest.cc
View
@@ -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 <boost/test/unit_test.hpp>
@@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE( Test )
BOOST_CHECK_EQUAL( json["key"].As<long long>(), 100 ) ;
StringStream ss ;
- ValWriter wr( &ss ) ;
+ JsonWriter wr( &ss ) ;
json.Visit( &wr ) ;
BOOST_CHECK_EQUAL( ss.Str(), "{\"key\":100}" ) ;
Please sign in to comment.
Something went wrong with that request. Please try again.