- 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
diff --git a/src/jsontestrunner/main.cpp b/src/jsontestrunner/main.cpp
index be3f44c..67344e0 100644
--- a/src/jsontestrunner/main.cpp
+++ b/src/jsontestrunner/main.cpp
@@ -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>
diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp
index b257b45..a7b7328 100644
--- a/src/lib_json/json_value.cpp
+++ b/src/lib_json/json_value.cpp
@@ -975,6 +975,14 @@
}
+Value &
+Value::operator[]( int index )
+{
+ JSON_ASSERT( index >= 0 );
+ return (*this)[ ArrayIndex(index) ];
+}
+
+
const Value &
Value::operator[]( ArrayIndex index ) const
{
@@ -994,6 +1002,14 @@
}
+const Value &
+Value::operator[]( int index ) const
+{
+ JSON_ASSERT( index >= 0 );
+ return (*this)[ ArrayIndex(index) ];
+}
+
+
Value &
Value::operator[]( const char *key )
{
diff --git a/src/test_lib_json/jsontest.h b/src/test_lib_json/jsontest.h
index 75c7f78..0d07238 100644
--- a/src/test_lib_json/jsontest.h
+++ b/src/test_lib_json/jsontest.h
@@ -199,7 +199,7 @@
/// JSONTEST_ASSERT( x == y ) << "x=" << x << ", y=" << y;
/// JSONTEST_ASSERT( x == y );
#define JSONTEST_ASSERT( expr ) \
- if ( condition ) \
+ if ( expr ) \
{ \
} \
else \
diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp
index 3e5b53d..30d039c 100644
--- a/src/test_lib_json/main.cpp
+++ b/src/test_lib_json/main.cpp
@@ -172,6 +172,18 @@
}
+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 )
{
@@ -245,5 +257,7 @@
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 );
}