Skip to content

Commit

Permalink
- Array index can be passed as int to operator[], allowing use of lit…
Browse files Browse the repository at this point in the history
…eral:

  Json::Value array;
  array.append( 1234 );
  int value = array[0].asInt();  // did not compile previously
  • Loading branch information
blep committed Dec 24, 2010
1 parent e6046e5 commit fa130ef
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
5 changes: 5 additions & 0 deletions NEWS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@

- The type Json::ArrayIndex is used for indexes of a JSON value array. It
is an unsigned int (typically 32 bits).

- Array index can be passed as int to operator[], allowing use of literal:
Json::Value array;
array.append( 1234 );
int value = array[0].asInt(); // did not compile previously

* Tests

Expand Down
18 changes: 16 additions & 2 deletions include/json/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,25 @@ namespace Json {
/// (You may need to say 'value[0u]' to get your compiler to distinguish
/// this from the operator[] which takes a string.)
Value &operator[]( ArrayIndex index );
/// Access an array element (zero based index )

/// Access an array element (zero based index ).
/// If the array contains less than index element, then null value are inserted
/// in the array so that its size is index+1.
/// (You may need to say 'value[0u]' to get your compiler to distinguish
/// this from the operator[] which takes a string.)
Value &operator[]( int index );

/// Access an array element (zero based index )
/// (You may need to say 'value[0u]' to get your compiler to distinguish
/// this from the operator[] which takes a string.)
const Value &operator[]( ArrayIndex index ) const;
/// If the array contains at least index+1 elements, returns the element value,

/// Access an array element (zero based index )
/// (You may need to say 'value[0u]' to get your compiler to distinguish
/// this from the operator[] which takes a string.)
const Value &operator[]( int index ) const;

/// If the array contains at least index+1 elements, returns the element value,
/// otherwise returns defaultValue.
Value get( ArrayIndex index,
const Value &defaultValue ) const;
Expand Down
4 changes: 4 additions & 0 deletions src/jsontestrunner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE

/* This executable is used for testing parser/writer using real JSON files.
*/


#include <json/json.h>
#include <algorithm> // sort
#include <stdio.h>
Expand Down
16 changes: 16 additions & 0 deletions src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,14 @@ Value::operator[]( ArrayIndex index )
}


Value &
Value::operator[]( int index )
{
JSON_ASSERT( index >= 0 );
return (*this)[ ArrayIndex(index) ];
}


const Value &
Value::operator[]( ArrayIndex index ) const
{
Expand All @@ -994,6 +1002,14 @@ Value::operator[]( ArrayIndex index ) const
}


const Value &
Value::operator[]( int index ) const
{
JSON_ASSERT( index >= 0 );
return (*this)[ ArrayIndex(index) ];
}


Value &
Value::operator[]( const char *key )
{
Expand Down
2 changes: 1 addition & 1 deletion src/test_lib_json/jsontest.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ namespace JsonTest {
/// JSONTEST_ASSERT( x == y ) << "x=" << x << ", y=" << y;
/// JSONTEST_ASSERT( x == y );
#define JSONTEST_ASSERT( expr ) \
if ( condition ) \
if ( expr ) \
{ \
} \
else \
Expand Down
14 changes: 14 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ JSONTEST_FIXTURE( ValueTest, isUInt )
}


JSONTEST_FIXTURE( ValueTest, accessArray )
{
const unsigned int index0 = 0;
JSONTEST_ASSERT( Json::Value(1234) == array1_[index0] ) << "Json::Value::operator[ArrayIndex]";
JSONTEST_ASSERT( Json::Value(1234) == array1_[0] ) << "Json::Value::operator[int]";

const Json::Value &constArray = array1_;
JSONTEST_ASSERT( Json::Value(1234) == constArray[index0] ) << "Json::Value::operator[ArrayIndex] const";
JSONTEST_ASSERT( Json::Value(1234) == constArray[0] ) << "Json::Value::operator[int] const";
}


void
ValueTest::checkConstMemberCount( const Json::Value &value, unsigned int expectedCount )
{
Expand Down Expand Up @@ -245,5 +257,7 @@ int main( int argc, const char *argv[] )
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isDouble );
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isString );
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isNull );
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isNull );
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, accessArray );
return runner.runCommandLine( argc, argv );
}

0 comments on commit fa130ef

Please sign in to comment.