Skip to content

Commit

Permalink
[RapidJson] Backup the current works.
Browse files Browse the repository at this point in the history
  • Loading branch information
slimek committed Aug 19, 2014
1 parent b191bcd commit edf7c35
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 57 deletions.
50 changes: 43 additions & 7 deletions include/Macaron/RapidJson/JsonValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <Macaron/Setup/MacaronDefs.h>
#include <Caramel/String/Utf8String.h>
#include <boost/optional.hpp>


namespace Macaron
Expand Down Expand Up @@ -75,16 +76,31 @@ class JsonValue
// - Returns false if the value doesn't exist.
// Throws if the value can't be converted to the type.

Bool GetBoolValue ( const std::string& name, Bool& value ) const;
Bool GetIntValue ( const std::string& name, Int& value ) const;
Bool GetUintValue ( const std::string& name, Uint& value ) const;
Bool GetFloatValue ( const std::string& name, Float& value ) const;
Bool GetDoubleValue( const std::string& name, Double& value ) const;
Bool GetBool ( const std::string& name, Bool& value ) const;
Bool GetInt ( const std::string& name, Int& value ) const;
Bool GetUint ( const std::string& name, Uint& value ) const;
Bool GetFloat ( const std::string& name, Float& value ) const;
Bool GetDouble( const std::string& name, Double& value ) const;

Bool GetString( const std::string& name, std::string& value ) const;

Bool GetStringValue( const std::string& name, std::string& value ) const;
Bool GetValue( const std::string& name, JsonValue& value ) const;
Bool GetArray( const std::string& name, JsonArray& value ) const;


private:
// Get Functions - Return optionals

boost::optional< std::string > GetString( const std::string& name ) const;


// Tag
// - For tracing and error messages.

void SetTag( const std::string& tag );
std::string GetTag() const;


protected:

explicit JsonValue( std::shared_ptr< JsonValueImpl > impl );

Expand All @@ -99,6 +115,26 @@ class JsonValue

class JsonArray : public JsonValue
{
friend class JsonValue;

public:

JsonArray();

// Throws if 'text' is not a JSON array, even if it is a valid JSON.
static JsonArray FromString( const std::string& text );


/// Array Elemenet Accessors ///

Uint Size() const;

const JsonValue& operator[]( Uint index ) const;


private:

explicit JsonArray( std::shared_ptr< JsonValueImpl > impl );
};


Expand Down
105 changes: 99 additions & 6 deletions src/RapidJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace RapidJson
// Contents
//
// JsonValue
// JsonArray
// JsonReader
// JsonErrorLocator
//
Expand Down Expand Up @@ -261,7 +262,7 @@ JsonValue JsonValue::operator[]( const std::string& name ) const
}


Bool JsonValue::GetBoolValue( const std::string& name, Bool& value ) const
Bool JsonValue::GetBool( const std::string& name, Bool& value ) const
{
if ( ! m_impl->HasMember( name.c_str() )) { return false; }

Expand All @@ -272,7 +273,7 @@ Bool JsonValue::GetBoolValue( const std::string& name, Bool& value ) const
}


Bool JsonValue::GetIntValue( const std::string& name, Int& value ) const
Bool JsonValue::GetInt( const std::string& name, Int& value ) const
{
if ( ! m_impl->HasMember( name.c_str() )) { return false; }

Expand All @@ -283,7 +284,7 @@ Bool JsonValue::GetIntValue( const std::string& name, Int& value ) const
}


Bool JsonValue::GetUintValue( const std::string& name, Uint& value ) const
Bool JsonValue::GetUint( const std::string& name, Uint& value ) const
{
if ( ! m_impl->HasMember( name.c_str() )) { return false; }

Expand All @@ -294,7 +295,7 @@ Bool JsonValue::GetUintValue( const std::string& name, Uint& value ) const
}


Bool JsonValue::GetFloatValue( const std::string& name, Float& value ) const
Bool JsonValue::GetFloat( const std::string& name, Float& value ) const
{
if ( ! m_impl->HasMember( name.c_str() )) { return false; }

Expand All @@ -305,7 +306,7 @@ Bool JsonValue::GetFloatValue( const std::string& name, Float& value ) const
}


Bool JsonValue::GetDoubleValue( const std::string& name, Double& value ) const
Bool JsonValue::GetDouble( const std::string& name, Double& value ) const
{
if ( ! m_impl->HasMember( name.c_str() )) { return false; }

Expand All @@ -316,7 +317,7 @@ Bool JsonValue::GetDoubleValue( const std::string& name, Double& value ) const
}


Bool JsonValue::GetStringValue( const std::string& name, std::string& value ) const
Bool JsonValue::GetString( const std::string& name, std::string& value ) const
{
if ( ! m_impl->HasMember( name.c_str() )) { return false; }

Expand All @@ -327,6 +328,47 @@ Bool JsonValue::GetStringValue( const std::string& name, std::string& value ) co
}


Bool JsonValue::GetArray( const std::string& name, JsonArray& value ) const
{
if ( ! m_impl->HasMember( name.c_str() )) { return false; }

auto& jvalue = m_impl->At( name.c_str() );

value = JsonArray( std::make_shared< JsonValueImpl >( m_impl->m_document, jvalue ));
return true;
}


boost::optional< std::string > JsonValue::GetString( const std::string& name ) const
{
std::string value;
if ( this->GetString( name, value ))
{
return boost::optional< std::string >( value );
}
else
{
return boost::optional< std::string >();
}
}


//
// Tag Management
//

void JsonValue::SetTag( const std::string& tag )
{
CARAMEL_NOT_IMPLEMENTED();
}


std::string JsonValue::GetTag() const
{
CARAMEL_NOT_IMPLEMENTED();
}


//
// JSON Value Implementation
//
Expand Down Expand Up @@ -356,6 +398,12 @@ JsonValueImpl::JsonValueImpl( std::shared_ptr< rapidjson::Document > doc, rapidj
// Children Accessors
//

Uint JsonValueImpl::Size() const
{
return static_cast< Uint >( m_value.Size() );
}


Bool JsonValueImpl::HasMember( const Char* name ) const
{
return m_value.HasMember( name );
Expand All @@ -374,6 +422,51 @@ const rapidjson::Value& JsonValueImpl::At( const Char* name ) const
}


rapidjson::Value& JsonValueImpl::At( const Char* name )
{
return m_value[ name ];
}


///////////////////////////////////////////////////////////////////////////////
//
// JSON Array
//

JsonArray::JsonArray()
{
}


JsonArray::JsonArray( std::shared_ptr< JsonValueImpl > impl )
: JsonValue( impl )
{
CARAMEL_ASSERT( this->IsArray() );
}


JsonArray JsonArray::FromString( const std::string& text )
{
CARAMEL_NOT_IMPLEMENTED();
}


//
// Array Element Accessors
//

Uint JsonArray::Size() const
{
return m_impl->Size();
}


const JsonValue& JsonArray::operator[]( Uint index ) const
{
CARAMEL_NOT_IMPLEMENTED();
}


///////////////////////////////////////////////////////////////////////////////
//
// JSON Reader
Expand Down
3 changes: 3 additions & 0 deletions src/RapidJson/JsonValueImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ class JsonValueImpl

/// Children Accessors ///

Uint Size() const;

Bool HasMember( const Char* name ) const;

std::shared_ptr< JsonValueImpl > GetValue( const Char* name );

const rapidjson::Value& At( const Char* name ) const;
rapidjson::Value& At( const Char* name );


private:
Expand Down
1 change: 1 addition & 0 deletions test/MacaronTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="RapidJson\RapidJsonPerformanceSuite.cpp" />
<ClCompile Include="RapidJson\RapidJsonReaderSuite.cpp" />
<ClCompile Include="RapidJson\RapidJsonValueSuite.cpp" />
<ClCompile Include="RapidJson\RapidJsonErrorLocatorSuite.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions test/MacaronTest.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<ClCompile Include="Sandbox\SandboxSuite.cpp">
<Filter>2. Tests\Sandbox</Filter>
</ClCompile>
<ClCompile Include="RapidJson\RapidJsonPerformanceSuite.cpp">
<Filter>2. Tests\RapidJson</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="MacaronTestPch.h">
Expand Down
59 changes: 59 additions & 0 deletions test/RapidJson/RapidJsonPerformanceSuite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Macaron C++ Library Test - RapidJson Performance Suite

#include "MacaronTestPch.h"

#include <Macaron/JsonCpp/JsonValue.h>
#include <Macaron/RapidJson/JsonValue.h>
#include <Caramel/Chrono/TickClock.h>
#include <Caramel/Io/InputFileStream.h>
#include <Caramel/Io/TextStreamReader.h>


namespace Macaron
{

namespace RapidJson
{

SUITE( RapidJsonPerformanceSuite )
{

TEST( RapidJsonPerformanceTest )
{
const std::string fileName = "RapidJson\\enemy-models.json";
TickWatch watch;

/// From File ///

auto fromFile = JsonValue::FromFile( fileName );

cout << "RapidJson FromFile : " << watch.Slice() << endl;


/// From String ///

InputFileStream file( fileName );
TextStreamReader reader( file );
const std::string jsonStr = reader.ReadAll();

cout << "(Read String) : " << watch.Slice() << endl;

auto fromString = JsonValue::FromString( jsonStr );

cout << "RapidJson FromString : " << watch.Slice() << endl;


/// Compare to JsonCpp ///

auto compare = JsonCpp::JsonValue::FromFile( fileName );

cout << "JsonCpp : " << watch.Slice() << endl;

}


} // SUITE RapidJsonPerformanceSuite

} // namespace RapidJson

} // namespace Macaron
Loading

0 comments on commit edf7c35

Please sign in to comment.