Skip to content

Commit

Permalink
added operator<< to print out json
Browse files Browse the repository at this point in the history
  • Loading branch information
nestal committed May 1, 2013
1 parent 6fa6e69 commit 6ba04dc
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 23 deletions.
34 changes: 17 additions & 17 deletions libgrive/src/json/ValWriter.cc → libgrive/src/json/JsonWriter.cc
Expand Up @@ -18,7 +18,7 @@
MA 02110-1301, USA.
*/

#include "ValWriter.hh"
#include "JsonWriter.hh"
#include "util/DataStream.hh"

#include <yajl/yajl_gen.h>
Expand All @@ -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 ) ;

Expand Down
Expand Up @@ -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 ) ;
Expand Down
11 changes: 11 additions & 0 deletions libgrive/src/json/Val.cc
Expand Up @@ -19,7 +19,9 @@
*/

#include "Val.hh"
#include "JsonWriter.hh"
#include "ValVisitor.hh"
#include "util/StdStream.hh"

#include <iostream>

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libgrive/src/json/Val.hh
Expand Up @@ -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 :
Expand Down
42 changes: 42 additions & 0 deletions 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 <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 changes: 42 additions & 0 deletions 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 <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 changes: 2 additions & 2 deletions libgrive/test/btest/JsonValTest.cc
Expand Up @@ -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>
Expand All @@ -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}" ) ;
Expand Down

0 comments on commit 6ba04dc

Please sign in to comment.