osgEarth 2.1.1
Classes | Public Types | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends

osgEarth::Json::Value Class Reference

Represents a JSON value. More...

Collaboration diagram for osgEarth::Json::Value:

List of all members.

Classes

struct  CommentInfo
class  CZString
union  ValueHolder

Public Types

typedef std::vector< std::string > Members
typedef int Int
typedef unsigned int UInt
typedef ValueIterator iterator
typedef ValueConstIterator const_iterator
typedef UInt ArrayIndex
typedef std::map< CZString, ValueObjectValues

Public Member Functions

 Value (ValueType type=nullValue)
 Create a default Value of the given type.
 Value (Int value)
 Value (UInt value)
 Value (double value)
 Value (const char *value)
 Value (const StaticString &value)
 Constructs a value from a static string.
 Value (const std::string &value)
 Value (bool value)
 Value (const Value &other)
 ~Value ()
Valueoperator= (const Value &other)
void swap (Value &other)
ValueType type () const
bool operator< (const Value &other) const
bool operator<= (const Value &other) const
bool operator>= (const Value &other) const
bool operator> (const Value &other) const
bool operator== (const Value &other) const
bool operator!= (const Value &other) const
int compare (const Value &other)
const char * asCString () const
std::string asString () const
Int asInt () const
UInt asUInt () const
double asDouble () const
bool asBool () const
bool isNull () const
bool isBool () const
bool isInt () const
bool isUInt () const
bool isIntegral () const
bool isDouble () const
bool isNumeric () const
bool isString () const
bool isArray () const
bool isObject () const
bool isConvertibleTo (ValueType other) const
UInt size () const
 Number of values in array or object.
bool empty () const
 Return true if empty array, empty object, or null; otherwise, false.
bool operator! () const
 Return isNull()
void clear ()
void resize (UInt size)
Valueoperator[] (UInt index)
const Valueoperator[] (UInt index) const
Value get (UInt index, const Value &defaultValue) const
bool isValidIndex (UInt index) const
 Return true if index < size().
Valueappend (const Value &value)
 Append value to array at the end.
Valueoperator[] (const char *key)
 Access an object value by name, create a null member if it does not exist.
const Valueoperator[] (const char *key) const
 Access an object value by name, returns null if there is no member with that name.
Valueoperator[] (const std::string &key)
 Access an object value by name, create a null member if it does not exist.
const Valueoperator[] (const std::string &key) const
 Access an object value by name, returns null if there is no member with that name.
Valueoperator[] (const StaticString &key)
 Access an object value by name, create a null member if it does not exist.
Value get (const char *key, const Value &defaultValue) const
 Return the member named key if it exist, defaultValue otherwise.
Value get (const std::string &key, const Value &defaultValue) const
 Return the member named key if it exist, defaultValue otherwise.
Value removeMember (const char *key)
 Remove and return the named member.
Value removeMember (const std::string &key)
 Same as removeMember(const char*)
bool isMember (const char *key) const
 Return true if the object has a member named key.
bool isMember (const std::string &key) const
 Return true if the object has a member named key.
Members getMemberNames () const
 Return a list of the member names.
void setComment (const char *comment, CommentPlacement placement)
 Comments must be //... or /* ... */.
void setComment (const std::string &comment, CommentPlacement placement)
 Comments must be //... or /* ... */.
bool hasComment (CommentPlacement placement) const
std::string getComment (CommentPlacement placement) const
 Include delimiters and embedded newlines.
std::string toStyledString () const
const_iterator begin () const
const_iterator end () const
iterator begin ()
iterator end ()

Static Public Attributes

static const Value null
static const Int minInt = Value::Int( ~(Value::UInt(-1)/2) )
static const Int maxInt = Value::Int( Value::UInt(-1)/2 )
static const UInt maxUInt = Value::UInt(-1)

Private Member Functions

ValueresolveReference (const char *key, bool isStatic)

Private Attributes

union
osgEarth::Json::Value::ValueHolder 
value_
ValueType type_: 8
int allocated_: 1
CommentInfocomments_

Friends

class ValueIteratorBase

Detailed Description

Represents a JSON value.

This class is a discriminated union wrapper that can represents a:

The type of the held value is represented by a ValueType and can be obtained using type().

values of an objectValue or arrayValue can be accessed using operator[]() methods. Non const methods will automatically create the a nullValue element if it does not exist. The sequence of an arrayValue will be automatically resize and initialized with nullValue. resize() can be used to enlarge or truncate an arrayValue.

The get() methods can be used to obtanis default value in the case the required element does not exist.

It is possible to iterate over the list of a objectValue values using the getMemberNames() method.

Definition at line 168 of file JsonUtils.


Member Typedef Documentation

Definition at line 181 of file JsonUtils.

Definition at line 180 of file JsonUtils.

Definition at line 177 of file JsonUtils.

Definition at line 179 of file JsonUtils.

typedef std::vector<std::string> osgEarth::Json::Value::Members

Definition at line 176 of file JsonUtils.

Definition at line 218 of file JsonUtils.

typedef unsigned int osgEarth::Json::Value::UInt

Definition at line 178 of file JsonUtils.


Constructor & Destructor Documentation

Value::Value ( ValueType  type = nullValue)

Create a default Value of the given type.

This is a very useful constructor. To create an empty array, pass arrayValue. To create an empty object, pass objectValue. Another Value can then be set to this one by assignment. This is useful since clear() and resize() will not alter types.

Examples:

        Json::Value null_value; // null
        Json::Value arr_value(Json::arrayValue); // []
        Json::Value obj_value(Json::objectValue); // {}

Default constructor initialization must be equivalent to: memset( this, 0, sizeof(Value) ) This optimization is used in ValueInternalMap fast allocator.

Definition at line 529 of file JsonUtils.cpp.

   : type_( type )
   , allocated_( 0 )
   , comments_( 0 )
# ifdef JSON_VALUE_USE_INTERNAL_MAP
   , itemIsUsed_( 0 )
#endif
{
   switch ( type )
   {
   case nullValue:
      break;
   case intValue:
   case uintValue:
      value_.int_ = 0;
      break;
   case realValue:
      value_.real_ = 0.0;
      break;
   case stringValue:
      value_.string_ = 0;
      break;
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   case arrayValue:
   case objectValue:
      value_.map_ = new ObjectValues();
      break;
#else
   case arrayValue:
      value_.array_ = arrayAllocator()->newArray();
      break;
   case objectValue:
      value_.map_ = mapAllocator()->newMap();
      break;
#endif
   case booleanValue:
      value_.bool_ = false;
      break;
   default:
      JSON_ASSERT_UNREACHABLE;
   }
}

Here is the caller graph for this function:

Value::Value ( Int  value)

Definition at line 573 of file JsonUtils.cpp.

   : type_( intValue )
   , comments_( 0 )
# ifdef JSON_VALUE_USE_INTERNAL_MAP
   , itemIsUsed_( 0 )
#endif
{
   value_.int_ = value;
}
Value::Value ( UInt  value)

Definition at line 584 of file JsonUtils.cpp.

   : type_( uintValue )
   , comments_( 0 )
# ifdef JSON_VALUE_USE_INTERNAL_MAP
   , itemIsUsed_( 0 )
#endif
{
   value_.uint_ = value;
}
Value::Value ( double  value)

Definition at line 594 of file JsonUtils.cpp.

   : type_( realValue )
   , comments_( 0 )
# ifdef JSON_VALUE_USE_INTERNAL_MAP
   , itemIsUsed_( 0 )
#endif
{
   value_.real_ = value;
}
Value::Value ( const char *  value)

Definition at line 604 of file JsonUtils.cpp.

   : type_( stringValue )
   , allocated_( true )
   , comments_( 0 )
# ifdef JSON_VALUE_USE_INTERNAL_MAP
   , itemIsUsed_( 0 )
#endif
{
   value_.string_ = valueAllocator()->duplicateStringValue( value );
}

Here is the call graph for this function:

Value::Value ( const StaticString value)

Constructs a value from a static string.

Like other value string constructor but do not duplicate the string for internal storage. The given string must remain alive after the call to this constructor. Example of usage:

 Json::Value aValue( StaticString("some text") );

Definition at line 628 of file JsonUtils.cpp.

   : type_( stringValue )
   , allocated_( false )
   , comments_( 0 )
# ifdef JSON_VALUE_USE_INTERNAL_MAP
   , itemIsUsed_( 0 )
#endif
{
   value_.string_ = const_cast<char *>( value.c_str() );
}

Here is the call graph for this function:

Value::Value ( const std::string &  value)

Definition at line 615 of file JsonUtils.cpp.

   : type_( stringValue )
   , allocated_( true )
   , comments_( 0 )
# ifdef JSON_VALUE_USE_INTERNAL_MAP
   , itemIsUsed_( 0 )
#endif
{
   value_.string_ = valueAllocator()->duplicateStringValue( value.c_str(), 
                                                            (unsigned int)value.length() );

}

Here is the call graph for this function:

Value::Value ( bool  value)

Definition at line 653 of file JsonUtils.cpp.

   : type_( booleanValue )
   , comments_( 0 )
# ifdef JSON_VALUE_USE_INTERNAL_MAP
   , itemIsUsed_( 0 )
#endif
{
   value_.bool_ = value;
}
Value::Value ( const Value other)

Definition at line 664 of file JsonUtils.cpp.

   : type_( other.type_ )
   , comments_( 0 )
# ifdef JSON_VALUE_USE_INTERNAL_MAP
   , itemIsUsed_( 0 )
#endif
{
   switch ( type_ )
   {
   case nullValue:
   case intValue:
   case uintValue:
   case realValue:
   case booleanValue:
      value_ = other.value_;
      break;
   case stringValue:
      if ( other.value_.string_ )
      {
         value_.string_ = valueAllocator()->duplicateStringValue( other.value_.string_ );
         allocated_ = true;
      }
      else
         value_.string_ = 0;
      break;
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   case arrayValue:
   case objectValue:
      value_.map_ = new ObjectValues( *other.value_.map_ );
      break;
#else
   case arrayValue:
      value_.array_ = arrayAllocator()->newArrayCopy( *other.value_.array_ );
      break;
   case objectValue:
      value_.map_ = mapAllocator()->newMapCopy( *other.value_.map_ );
      break;
#endif
   default:
      JSON_ASSERT_UNREACHABLE;
   }
   if ( other.comments_ )
   {
      comments_ = new CommentInfo[numberOfCommentPlacement];
      for ( int comment =0; comment < numberOfCommentPlacement; ++comment )
      {
         const CommentInfo &otherComment = other.comments_[comment];
         if ( otherComment.comment_ )
            comments_[comment].setComment( otherComment.comment_ );
      }
   }
}

Here is the call graph for this function:

Value::~Value ( )

Definition at line 718 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
   case nullValue:
   case intValue:
   case uintValue:
   case realValue:
   case booleanValue:
      break;
   case stringValue:
      if ( allocated_ )
         valueAllocator()->releaseStringValue( value_.string_ );
      break;
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   case arrayValue:
   case objectValue:
      delete value_.map_;
      break;
#else
   case arrayValue:
      arrayAllocator()->destructArray( value_.array_ );
      break;
   case objectValue:
      mapAllocator()->destructMap( value_.map_ );
      break;
#endif
   default:
      JSON_ASSERT_UNREACHABLE;
   }

   if ( comments_ )
      delete[] comments_;
}

Here is the call graph for this function:


Member Function Documentation

Value & Value::append ( const Value value)

Append value to array at the end.

Equivalent to jsonvalue[jsonvalue.size()] = value;

Definition at line 1384 of file JsonUtils.cpp.

{
   return (*this)[size()] = value;
}

Here is the call graph for this function:

bool Value::asBool ( ) const

Definition at line 1061 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
   case nullValue:
      return false;
   case intValue:
   case uintValue:
      return value_.int_ != 0;
   case realValue:
      return value_.real_ != 0.0;
   case booleanValue:
      return value_.bool_;
   case stringValue:
      return value_.string_  &&  value_.string_[0] != 0;
   case arrayValue:
   case objectValue:
      return value_.map_->size() != 0;
   default:
      JSON_ASSERT_UNREACHABLE;
   }
   return false; // unreachable;
}

Here is the caller graph for this function:

const char * Value::asCString ( ) const

Definition at line 923 of file JsonUtils.cpp.

Here is the caller graph for this function:

double Value::asDouble ( ) const

Definition at line 1036 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
   case nullValue:
      return 0.0;
   case intValue:
      return value_.int_;
   case uintValue:
      return value_.uint_;
   case realValue:
      return value_.real_;
   case booleanValue:
      return value_.bool_ ? 1.0 : 0.0;
   case stringValue:
   case arrayValue:
   case objectValue:
      JSON_ASSERT_MESSAGE( false, "Type is not convertible to double" );
   default:
      JSON_ASSERT_UNREACHABLE;
   }
   return 0; // unreachable;
}

Here is the caller graph for this function:

Value::Int Value::asInt ( ) const

Definition at line 982 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
   case nullValue:
      return 0;
   case intValue:
      return value_.int_;
   case uintValue:
      JSON_ASSERT_MESSAGE( value_.uint_ < (unsigned)maxInt, "integer out of signed integer range" );
      return value_.uint_;
   case realValue:
      JSON_ASSERT_MESSAGE( value_.real_ >= minInt  &&  value_.real_ <= maxInt, "Real out of signed integer range" );
      return Int( value_.real_ );
   case booleanValue:
      return value_.bool_ ? 1 : 0;
   case stringValue:
   case arrayValue:
   case objectValue:
      JSON_ASSERT_MESSAGE( false, "Type is not convertible to int" );
   default:
      JSON_ASSERT_UNREACHABLE;
   }
   return 0; // unreachable;
}

Here is the caller graph for this function:

std::string Value::asString ( ) const

Definition at line 930 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
   case nullValue:
      return "";
   case stringValue:
      return value_.string_ ? value_.string_ : "";
   case booleanValue:
      return value_.bool_ ? "true" : "false";
   case intValue:
       {
           std::stringstream buf;
           buf << value_.int_;
                   std::string bufStr;
                   bufStr = buf.str();
           return bufStr;
       }
   case uintValue:
       {
           std::stringstream buf;
           buf << value_.uint_;
           std::string bufStr;
                   bufStr = buf.str();
                   return bufStr;
       }
   case realValue:
       {
           std::stringstream buf;
           buf << value_.real_;
           std::string bufStr;
                   bufStr = buf.str();
                   return bufStr;
       }
   case arrayValue:
   case objectValue:
      JSON_ASSERT_MESSAGE( false, "Type is not convertible to string" );
   default:
      JSON_ASSERT_UNREACHABLE;
   }
   return ""; // unreachable
}

Here is the caller graph for this function:

Value::UInt Value::asUInt ( ) const

Definition at line 1009 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
   case nullValue:
      return 0;
   case intValue:
      JSON_ASSERT_MESSAGE( value_.int_ >= 0, "Negative integer can not be converted to unsigned integer" );
      return value_.int_;
   case uintValue:
      return value_.uint_;
   case realValue:
      JSON_ASSERT_MESSAGE( value_.real_ >= 0  &&  value_.real_ <= maxUInt,  "Real out of unsigned integer range" );
      return UInt( value_.real_ );
   case booleanValue:
      return value_.bool_ ? 1 : 0;
   case stringValue:
   case arrayValue:
   case objectValue:
      JSON_ASSERT_MESSAGE( false, "Type is not convertible to uint" );
   default:
      JSON_ASSERT_UNREACHABLE;
   }
   return 0; // unreachable;
}

Here is the caller graph for this function:

Value::const_iterator Value::begin ( ) const

Definition at line 1634 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
#ifdef JSON_VALUE_USE_INTERNAL_MAP
   case arrayValue:
      if ( value_.array_ )
      {
         ValueInternalArray::IteratorState it;
         value_.array_->makeBeginIterator( it );
         return const_iterator( it );
      }
      break;
   case objectValue:
      if ( value_.map_ )
      {
         ValueInternalMap::IteratorState it;
         value_.map_->makeBeginIterator( it );
         return const_iterator( it );
      }
      break;
#else
   case arrayValue:
   case objectValue:
      if ( value_.map_ )
         return const_iterator( value_.map_->begin() );
      break;
#endif
   default:
      break;
   }
   return const_iterator();
}
Value::iterator Value::begin ( )

Definition at line 1705 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
#ifdef JSON_VALUE_USE_INTERNAL_MAP
   case arrayValue:
      if ( value_.array_ )
      {
         ValueInternalArray::IteratorState it;
         value_.array_->makeBeginIterator( it );
         return iterator( it );
      }
      break;
   case objectValue:
      if ( value_.map_ )
      {
         ValueInternalMap::IteratorState it;
         value_.map_->makeBeginIterator( it );
         return iterator( it );
      }
      break;
#else
   case arrayValue:
   case objectValue:
      if ( value_.map_ )
         return iterator( value_.map_->begin() );
      break;
#endif
   default:
      break;
   }
   return iterator();
}
void Value::clear ( )

Remove all object members and array elements.

Precondition:
type() is arrayValue, objectValue, or nullValue
Postcondition:
type() is unchanged

Definition at line 1192 of file JsonUtils.cpp.

{
   JSON_ASSERT( type_ == nullValue  ||  type_ == arrayValue  || type_ == objectValue );

   switch ( type_ )
   {
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   case arrayValue:
   case objectValue:
      value_.map_->clear();
      break;
#else
   case arrayValue:
      value_.array_->clear();
      break;
   case objectValue:
      value_.map_->clear();
      break;
#endif
   default:
      break;
   }
}

Here is the caller graph for this function:

int Value::compare ( const Value other)

Definition at line 781 of file JsonUtils.cpp.

{
   /*
   int typeDelta = other.type_ - type_;
   switch ( type_ )
   {
   case nullValue:

      return other.type_ == type_;
   case intValue:
      if ( other.type_.isNumeric()
   case uintValue:
   case realValue:
   case booleanValue:
      break;
   case stringValue,
      break;
   case arrayValue:
      delete value_.array_;
      break;
   case objectValue:
      delete value_.map_;
   default:
      JSON_ASSERT_UNREACHABLE;
   }
   */
   return 0;  // unreachable
}
bool Value::empty ( ) const

Return true if empty array, empty object, or null; otherwise, false.

Definition at line 1175 of file JsonUtils.cpp.

{
   if ( isNull() || isArray() || isObject() )
      return size() == 0u;
   else
      return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Value::iterator Value::end ( )

Definition at line 1740 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
#ifdef JSON_VALUE_USE_INTERNAL_MAP
   case arrayValue:
      if ( value_.array_ )
      {
         ValueInternalArray::IteratorState it;
         value_.array_->makeEndIterator( it );
         return iterator( it );
      }
      break;
   case objectValue:
      if ( value_.map_ )
      {
         ValueInternalMap::IteratorState it;
         value_.map_->makeEndIterator( it );
         return iterator( it );
      }
      break;
#else
   case arrayValue:
   case objectValue:
      if ( value_.map_ )
         return iterator( value_.map_->end() );
      break;
#endif
   default:
      break;
   }
   return iterator();
}
Value::const_iterator Value::end ( ) const

Definition at line 1669 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
#ifdef JSON_VALUE_USE_INTERNAL_MAP
   case arrayValue:
      if ( value_.array_ )
      {
         ValueInternalArray::IteratorState it;
         value_.array_->makeEndIterator( it );
         return const_iterator( it );
      }
      break;
   case objectValue:
      if ( value_.map_ )
      {
         ValueInternalMap::IteratorState it;
         value_.map_->makeEndIterator( it );
         return const_iterator( it );
      }
      break;
#else
   case arrayValue:
   case objectValue:
      if ( value_.map_ )
         return const_iterator( value_.map_->end() );
      break;
#endif
   default:
      break;
   }
   return const_iterator();
}
Value Value::get ( UInt  index,
const Value defaultValue 
) const

If the array contains at least index+1 elements, returns the element value, otherwise returns defaultValue.

Definition at line 1312 of file JsonUtils.cpp.

{
   const Value *value = &((*this)[index]);
   return value == &null ? defaultValue : *value;
}

Here is the caller graph for this function:

Value Value::get ( const char *  key,
const Value defaultValue 
) const

Return the member named key if it exist, defaultValue otherwise.

Definition at line 1391 of file JsonUtils.cpp.

{
   const Value *value = &((*this)[key]);
   return value == &null ? defaultValue : *value;
}
Value Value::get ( const std::string &  key,
const Value defaultValue 
) const

Return the member named key if it exist, defaultValue otherwise.

Definition at line 1400 of file JsonUtils.cpp.

{
   return get( key.c_str(), defaultValue );
}
std::string Value::getComment ( CommentPlacement  placement) const

Include delimiters and embedded newlines.

Definition at line 1617 of file JsonUtils.cpp.

{
   if ( hasComment(placement) )
      return comments_[placement].comment_;
   return "";
}

Here is the call graph for this function:

Here is the caller graph for this function:

Value::Members Value::getMemberNames ( ) const

Return a list of the member names.

If null, return an empty list.

Precondition:
type() is objectValue or nullValue
Postcondition:
if type() was nullValue, it remains nullValue

Definition at line 1471 of file JsonUtils.cpp.

{
   JSON_ASSERT( type_ == nullValue  ||  type_ == objectValue );
   if ( type_ == nullValue )
       return Value::Members();
   Members members;
   members.reserve( value_.map_->size() );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   ObjectValues::const_iterator it = value_.map_->begin();
   ObjectValues::const_iterator itEnd = value_.map_->end();
   for ( ; it != itEnd; ++it )
      members.push_back( std::string( (*it).first.c_str() ) );
#else
   ValueInternalMap::IteratorState it;
   ValueInternalMap::IteratorState itEnd;
   value_.map_->makeBeginIterator( it );
   value_.map_->makeEndIterator( itEnd );
   for ( ; !ValueInternalMap::equals( it, itEnd ); ValueInternalMap::increment(it) )
      members.push_back( std::string( ValueInternalMap::key( it ) ) );
#endif
   return members;
}

Here is the caller graph for this function:

bool Value::hasComment ( CommentPlacement  placement) const

Definition at line 1611 of file JsonUtils.cpp.

{
   return comments_ != 0  &&  comments_[placement].comment_ != 0;
}

Here is the caller graph for this function:

bool Value::isArray ( ) const

Definition at line 1579 of file JsonUtils.cpp.

{
   return type_ == nullValue  ||  type_ == arrayValue;
}

Here is the caller graph for this function:

bool Value::isBool ( ) const

Definition at line 1528 of file JsonUtils.cpp.

{
   return type_ == booleanValue;
}
bool Value::isConvertibleTo ( ValueType  other) const

Definition at line 1087 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
   case nullValue:
      return true;
   case intValue:
      return ( other == nullValue  &&  value_.int_ == 0 )
             || other == intValue
             || ( other == uintValue  && value_.int_ >= 0 )
             || other == realValue
             || other == stringValue
             || other == booleanValue;
   case uintValue:
      return ( other == nullValue  &&  value_.uint_ == 0 )
             || ( other == intValue  && value_.uint_ <= (unsigned)maxInt )
             || other == uintValue
             || other == realValue
             || other == stringValue
             || other == booleanValue;
   case realValue:
      return ( other == nullValue  &&  value_.real_ == 0.0 )
             || ( other == intValue  &&  value_.real_ >= minInt  &&  value_.real_ <= maxInt )
             || ( other == uintValue  &&  value_.real_ >= 0  &&  value_.real_ <= maxUInt )
             || other == realValue
             || other == stringValue
             || other == booleanValue;
   case booleanValue:
      return ( other == nullValue  &&  value_.bool_ == false )
             || other == intValue
             || other == uintValue
             || other == realValue
             || other == stringValue
             || other == booleanValue;
   case stringValue:
      return other == stringValue
             || ( other == nullValue  &&  (!value_.string_  ||  value_.string_[0] == 0) );
   case arrayValue:
      return other == arrayValue
             ||  ( other == nullValue  &&  value_.map_->size() == 0 );
   case objectValue:
      return other == objectValue
             ||  ( other == nullValue  &&  value_.map_->size() == 0 );
   default:
      JSON_ASSERT_UNREACHABLE;
   }
   return false; // unreachable;
}
bool Value::isDouble ( ) const

Definition at line 1558 of file JsonUtils.cpp.

{
   return type_ == realValue;
}

Here is the caller graph for this function:

bool Value::isInt ( ) const

Definition at line 1535 of file JsonUtils.cpp.

{
   return type_ == intValue;
}
bool Value::isIntegral ( ) const

Definition at line 1549 of file JsonUtils.cpp.

{
   return type_ == intValue  
          ||  type_ == uintValue  
          ||  type_ == booleanValue;
}

Here is the caller graph for this function:

bool Value::isMember ( const char *  key) const

Return true if the object has a member named key.

Definition at line 1448 of file JsonUtils.cpp.

{
   const Value *value = &((*this)[key]);
   return value != &null;
}

Here is the caller graph for this function:

bool Value::isMember ( const std::string &  key) const

Return true if the object has a member named key.

Definition at line 1456 of file JsonUtils.cpp.

{
   return isMember( key.c_str() );
}

Here is the call graph for this function:

bool Value::isNull ( ) const

Definition at line 1521 of file JsonUtils.cpp.

{
   return type_ == nullValue;
}

Here is the caller graph for this function:

bool Value::isNumeric ( ) const

Definition at line 1565 of file JsonUtils.cpp.

{
   return isIntegral() || isDouble();
}

Here is the call graph for this function:

bool Value::isObject ( ) const

Definition at line 1586 of file JsonUtils.cpp.

{
   return type_ == nullValue  ||  type_ == objectValue;
}

Here is the caller graph for this function:

bool Value::isString ( ) const

Definition at line 1572 of file JsonUtils.cpp.

{
   return type_ == stringValue;
}
bool Value::isUInt ( ) const

Definition at line 1542 of file JsonUtils.cpp.

{
   return type_ == uintValue;
}
bool Value::isValidIndex ( UInt  index) const

Return true if index < size().

Definition at line 1321 of file JsonUtils.cpp.

{
   return index < size();
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool Value::operator! ( ) const

Return isNull()

Definition at line 1185 of file JsonUtils.cpp.

{
   return isNull();
}

Here is the call graph for this function:

bool Value::operator!= ( const Value other) const

Definition at line 917 of file JsonUtils.cpp.

{
   return !( *this == other );
}
bool Value::operator< ( const Value other) const

Definition at line 811 of file JsonUtils.cpp.

{
   int typeDelta = type_ - other.type_;
   if ( typeDelta )
      return typeDelta < 0 ? true : false;
   switch ( type_ )
   {
   case nullValue:
      return false;
   case intValue:
      return value_.int_ < other.value_.int_;
   case uintValue:
      return value_.uint_ < other.value_.uint_;
   case realValue:
      return value_.real_ < other.value_.real_;
   case booleanValue:
      return value_.bool_ < other.value_.bool_;
   case stringValue:
      return ( value_.string_ == 0  &&  other.value_.string_ )
             || ( other.value_.string_  
                  &&  value_.string_  
                  && strcmp( value_.string_, other.value_.string_ ) < 0 );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   case arrayValue:
   case objectValue:
      {
         int delta = int( value_.map_->size() - other.value_.map_->size() );
         if ( delta )
            return delta < 0;
         return (*value_.map_) < (*other.value_.map_);
      }
#else
   case arrayValue:
      return value_.array_->compare( *(other.value_.array_) ) < 0;
   case objectValue:
      return value_.map_->compare( *(other.value_.map_) ) < 0;
#endif
   default:
      JSON_ASSERT_UNREACHABLE;
   }
   return 0;  // unreachable
}
bool Value::operator<= ( const Value other) const

Definition at line 855 of file JsonUtils.cpp.

{
   return !(other > *this);
}
Value & Value::operator= ( const Value other)

Definition at line 754 of file JsonUtils.cpp.

{
   Value temp( other );
   swap( temp );
   return *this;
}

Here is the call graph for this function:

bool Value::operator== ( const Value other) const

Definition at line 873 of file JsonUtils.cpp.

{
   //if ( type_ != other.type_ )
   // GCC 2.95.3 says:
   // attempt to take address of bit-field structure member `Json::Value::type_'
   // Beats me, but a temp solves the problem.
   int temp = other.type_;
   if ( type_ != temp )
      return false;
   switch ( type_ )
   {
   case nullValue:
      return true;
   case intValue:
      return value_.int_ == other.value_.int_;
   case uintValue:
      return value_.uint_ == other.value_.uint_;
   case realValue:
      return value_.real_ == other.value_.real_;
   case booleanValue:
      return value_.bool_ == other.value_.bool_;
   case stringValue:
      return ( value_.string_ == other.value_.string_ )
             || ( other.value_.string_  
                  &&  value_.string_  
                  && strcmp( value_.string_, other.value_.string_ ) == 0 );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   case arrayValue:
   case objectValue:
      return value_.map_->size() == other.value_.map_->size()
             && (*value_.map_) == (*other.value_.map_);
#else
   case arrayValue:
      return value_.array_->compare( *(other.value_.array_) ) == 0;
   case objectValue:
      return value_.map_->compare( *(other.value_.map_) ) == 0;
#endif
   default:
      JSON_ASSERT_UNREACHABLE;
   }
   return 0;  // unreachable
}
bool Value::operator> ( const Value other) const

Definition at line 867 of file JsonUtils.cpp.

{
   return other < *this;
}
bool Value::operator>= ( const Value other) const

Definition at line 861 of file JsonUtils.cpp.

{
   return !(*this < other);
}
Value & Value::operator[] ( const char *  key)

Access an object value by name, create a null member if it does not exist.

Definition at line 1281 of file JsonUtils.cpp.

{
   return resolveReference( key, false );
}

Here is the call graph for this function:

Value & Value::operator[] ( UInt  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.)

Definition at line 1241 of file JsonUtils.cpp.

{
   JSON_ASSERT( type_ == nullValue  ||  type_ == arrayValue );
   if ( type_ == nullValue )
      *this = Value( arrayValue );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   CZString key( index );
   ObjectValues::iterator it = value_.map_->lower_bound( key );
   if ( it != value_.map_->end()  &&  (*it).first == key )
      return (*it).second;

   ObjectValues::value_type defaultValue( key, null );
   it = value_.map_->insert( it, defaultValue );
   return (*it).second;
#else
   return value_.array_->resolveReference( index );
#endif
}

Here is the call graph for this function:

const Value & Value::operator[] ( UInt  index) const

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.)

Definition at line 1262 of file JsonUtils.cpp.

{
   JSON_ASSERT( type_ == nullValue  ||  type_ == arrayValue );
   if ( type_ == nullValue )
      return null;
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   CZString key( index );
   ObjectValues::const_iterator it = value_.map_->find( key );
   if ( it == value_.map_->end() )
      return null;
   return (*it).second;
#else
   Value *value = value_.array_->find( index );
   return value ? *value : null;
#endif
}
const Value & Value::operator[] ( const char *  key) const

Access an object value by name, returns null if there is no member with that name.

Definition at line 1329 of file JsonUtils.cpp.

{
   JSON_ASSERT( type_ == nullValue  ||  type_ == objectValue );
   if ( type_ == nullValue )
      return null;
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   CZString actualKey( key, CZString::noDuplication );
   ObjectValues::const_iterator it = value_.map_->find( actualKey );
   if ( it == value_.map_->end() )
      return null;
   return (*it).second;
#else
   const Value *value = value_.map_->find( key );
   return value ? *value : null;
#endif
}
Value & Value::operator[] ( const StaticString key)

Access an object value by name, create a null member if it does not exist.

If the object as no entry for that name, then the member name used to store the new entry is not duplicated. Example of use:

 Json::Value object;
 static const StaticString code("code");
 object[code] = 1234;

Definition at line 1361 of file JsonUtils.cpp.

{
   return resolveReference( key, true );
}

Here is the call graph for this function:

Value & Value::operator[] ( const std::string &  key)

Access an object value by name, create a null member if it does not exist.

Definition at line 1348 of file JsonUtils.cpp.

{
   return (*this)[ key.c_str() ];
}
const Value & Value::operator[] ( const std::string &  key) const

Access an object value by name, returns null if there is no member with that name.

Definition at line 1355 of file JsonUtils.cpp.

{
   return (*this)[ key.c_str() ];
}
Value Value::removeMember ( const std::string &  key)

Same as removeMember(const char*)

Definition at line 1433 of file JsonUtils.cpp.

{
   return removeMember( key.c_str() );
}

Here is the call graph for this function:

Value Value::removeMember ( const char *  key)

Remove and return the named member.

Do nothing if it did not exist.

Returns:
the removed Value, or null.
Precondition:
type() is objectValue or nullValue
Postcondition:
type() is unchanged

Definition at line 1407 of file JsonUtils.cpp.

{
   JSON_ASSERT( type_ == nullValue  ||  type_ == objectValue );
   if ( type_ == nullValue )
      return null;
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   CZString actualKey( key, CZString::noDuplication );
   ObjectValues::iterator it = value_.map_->find( actualKey );
   if ( it == value_.map_->end() )
      return null;
   Value old(it->second);
   value_.map_->erase(it);
   return old;
#else
   Value *value = value_.map_->find( key );
   if (value){
      Value old(*value);
      value_.map_.remove( key );
      return old;
   } else {
      return null;
   }
#endif
}

Here is the caller graph for this function:

void Value::resize ( UInt  size)

Resize the array to size elements. New elements are initialized to null. May only be called on nullValue or arrayValue.

Precondition:
type() is arrayValue or nullValue
Postcondition:
type() is arrayValue

Definition at line 1217 of file JsonUtils.cpp.

{
   JSON_ASSERT( type_ == nullValue  ||  type_ == arrayValue );
   if ( type_ == nullValue )
      *this = Value( arrayValue );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   UInt oldSize = size();
   if ( newSize == 0 )
      clear();
   else if ( newSize > oldSize )
      (*this)[ newSize - 1 ];
   else
   {
      for ( UInt index = newSize; index < oldSize; ++index )
         value_.map_->erase( index );
      assert( size() == newSize );
   }
#else
   value_.array_->resize( newSize );
#endif
}

Here is the call graph for this function:

Value & Value::resolveReference ( const char *  key,
bool  isStatic 
) [private]

Definition at line 1288 of file JsonUtils.cpp.

{
   JSON_ASSERT( type_ == nullValue  ||  type_ == objectValue );
   if ( type_ == nullValue )
      *this = Value( objectValue );
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   CZString actualKey( key, isStatic ? CZString::noDuplication 
                                     : CZString::duplicateOnCopy );
   ObjectValues::iterator it = value_.map_->lower_bound( actualKey );
   if ( it != value_.map_->end()  &&  (*it).first == actualKey )
      return (*it).second;

   ObjectValues::value_type defaultValue( actualKey, null );
   it = value_.map_->insert( it, defaultValue );
   Value &value = (*it).second;
   return value;
#else
   return value_.map_->resolveReference( key, isStatic );
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Value::setComment ( const char *  comment,
CommentPlacement  placement 
)

Comments must be //... or /* ... */.

Definition at line 1593 of file JsonUtils.cpp.

{
   if ( !comments_ )
      comments_ = new CommentInfo[numberOfCommentPlacement];
   comments_[placement].setComment( comment );
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Value::setComment ( const std::string &  comment,
CommentPlacement  placement 
)

Comments must be //... or /* ... */.

Definition at line 1603 of file JsonUtils.cpp.

{
   setComment( comment.c_str(), placement );
}

Here is the call graph for this function:

Value::UInt Value::size ( ) const

Number of values in array or object.

Definition at line 1139 of file JsonUtils.cpp.

{
   switch ( type_ )
   {
   case nullValue:
   case intValue:
   case uintValue:
   case realValue:
   case booleanValue:
   case stringValue:
      return 0;
#ifndef JSON_VALUE_USE_INTERNAL_MAP
   case arrayValue:  // size of the array is highest index + 1
      if ( !value_.map_->empty() )
      {
         ObjectValues::const_iterator itLast = value_.map_->end();
         --itLast;
         return (*itLast).first.index()+1;
      }
      return 0;
   case objectValue:
      return Int( value_.map_->size() );
#else
   case arrayValue:
      return Int( value_.array_->size() );
   case objectValue:
      return Int( value_.map_->size() );
#endif
   default:
      JSON_ASSERT_UNREACHABLE;
   }
   return 0; // unreachable;
}

Here is the caller graph for this function:

void Value::swap ( Value other)

Swap values.

Note:
Currently, comments are intentionally not swapped, for both logic and efficiency.

Definition at line 762 of file JsonUtils.cpp.

{
   ValueType temp = type_;
   type_ = other.type_;
   other.type_ = temp;
   std::swap( value_, other.value_ );
   int temp2 = allocated_;
   allocated_ = other.allocated_;
   other.allocated_ = temp2;
}

Here is the caller graph for this function:

std::string Value::toStyledString ( ) const

Definition at line 1626 of file JsonUtils.cpp.

{
   StyledWriter writer;
   return writer.write( *this );
}

Here is the call graph for this function:

ValueType Value::type ( ) const

Definition at line 774 of file JsonUtils.cpp.

{
   return type_;
}

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class ValueIteratorBase [friend]

Definition at line 170 of file JsonUtils.


Member Data Documentation

Definition at line 496 of file JsonUtils.

Definition at line 501 of file JsonUtils.

const Value::Int Value::maxInt = Value::Int( Value::UInt(-1)/2 ) [static]

Definition at line 185 of file JsonUtils.

const Value::UInt Value::maxUInt = Value::UInt(-1) [static]

Definition at line 186 of file JsonUtils.

const Value::Int Value::minInt = Value::Int( ~(Value::UInt(-1)/2) ) [static]

Definition at line 184 of file JsonUtils.

const Value Value::null [static]

Definition at line 183 of file JsonUtils.

Definition at line 495 of file JsonUtils.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines