Update of /cvsroot/jsoncpp/jsoncpp/src/lib_json
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11379/src/lib_json
Modified Files:
json_value.cpp
Log Message:
* added rough implementation of Path::resolve() & Path::make()
Index: json_value.cpp
===================================================================
RCS file: /cvsroot/jsoncpp/jsoncpp/src/lib_json/json_value.cpp,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -C2 -d -r1.1.1.1 -r1.2
*** json_value.cpp 27 Jul 2005 07:09:23 -0000 1.1.1.1
--- json_value.cpp 27 Jul 2005 07:38:14 -0000 1.2
***************
*** 3,7 ****
#include "assert.h"
! #define JSON_ASSERT_UNREACHABLE
#define JSON_ASSERT( condition )
--- 3,7 ----
#include "assert.h"
! #define JSON_ASSERT_UNREACHABLE assert( false )
#define JSON_ASSERT( condition )
***************
*** 769,773 ****
Value::isObject() const
{
! return type_ == objectValue;
}
--- 769,773 ----
Value::isObject() const
{
! return type_ == nullValue || type_ == objectValue;
}
***************
*** 895,897 ****
--- 895,984 ----
+ const Value &
+ Path::resolve( const Value &root ) const
+ {
+ const Value *node = &root;
+ for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
+ {
+ const PathArgument &arg = *it;
+ if ( arg.kind_ == PathArgument::kindIndex )
+ {
+ if ( !node->isArray() || node->isValidIndex( arg.index_ ) )
+ {
+ // Error: unable to resolve path (array value expected at position...
+ }
+ node = &((*node)[arg.index_]);
+ }
+ else if ( arg.kind_ == PathArgument::kindKey )
+ {
+ if ( !node->isObject() )
+ {
+ // Error: unable to resolve path (object value expected at position...)
+ }
+ node = &((*node)[arg.key_]);
+ if ( node == &Value::null )
+ {
+ // Error: unable to resolve path (object has no member named '' at position...)
+ }
+ }
+ }
+ return *node;
+ }
+
+
+ Value
+ Path::resolve( const Value &root,
+ const Value &defaultValue ) const
+ {
+ const Value *node = &root;
+ for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
+ {
+ const PathArgument &arg = *it;
+ if ( arg.kind_ == PathArgument::kindIndex )
+ {
+ if ( !node->isArray() || node->isValidIndex( arg.index_ ) )
+ return defaultValue;
+ node = &((*node)[arg.index_]);
+ }
+ else if ( arg.kind_ == PathArgument::kindKey )
+ {
+ if ( !node->isObject() )
+ return defaultValue;
+ node = &((*node)[arg.key_]);
+ if ( node == &Value::null )
+ return defaultValue;
+ }
+ }
+ return *node;
+ }
+
+
+ Value &
+ Path::make( Value &root ) const
+ {
+ Value *node = &root;
+ for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
+ {
+ const PathArgument &arg = *it;
+ if ( arg.kind_ == PathArgument::kindIndex )
+ {
+ if ( !node->isArray() )
+ {
+ // Error: node is not an array at position ...
+ }
+ node = &((*node)[arg.index_]);
+ }
+ else if ( arg.kind_ == PathArgument::kindKey )
+ {
+ if ( !node->isObject() )
+ {
+ // Error: node is not an object at position...
+ }
+ node = &((*node)[arg.key_]);
+ }
+ }
+ return *node;
+ }
+
+
} // namespace Json
|