as2js: as2js::JSON::JSONValue Class Reference

#include <json.h>

Collaboration diagram for as2js::JSON::JSONValue:
Classes class  saving_t   Public Types typedef std::vector< JSONValue::pointer_tarray_t   typedef std::map< String, JSONValue::pointer_tobject_t   typedef std::shared_ptr< JSONValuepointer_t   enum  type_t {
  type_t::JSON_TYPE_UNKNOWN,
  type_t::JSON_TYPE_ARRAY,
  type_t::JSON_TYPE_FALSE,
  type_t::JSON_TYPE_FLOAT64,
  type_t::JSON_TYPE_INT64,
  type_t::JSON_TYPE_NULL,
  type_t::JSON_TYPE_OBJECT,
  type_t::JSON_TYPE_STRING,
  type_t::JSON_TYPE_TRUE
}   Public Member Functions  JSONValue (Position const &position)  Initialize a JSONValue object. More...
   JSONValue (Position const &position, Int64 integer)  Initialize a JSONValue object. More...
   JSONValue (Position const &position, Float64 floating_point)  Initialize a JSONValue object. More...
   JSONValue (Position const &position, String const &string)  Initialize a JSONValue object. More...
   JSONValue (Position const &position, bool boolean)  Initialize a JSONValue object. More...
   JSONValue (Position const &position, array_t const &array)  Initialize a JSONValue object. More...
   JSONValue (Position const &position, object_t const &object)  Initialize a JSONValue object. More...
  array_t const & get_array () const  Get a reference to this JSONValue array. More...
  Float64 get_float64 () const  Get the floating point. More...
  Int64 get_int64 () const  Get the integer. More...
  object_t const & get_object () const  Get a reference to this JSONValue object. More...
  Position const & get_position () const  Get a constant reference to the JSONValue position. More...
  String const & get_string () const  Get the string. More...
  type_t get_type () const  Retrieve the type of this JSONValue object. More...
  void set_item (size_t idx, JSONValue::pointer_t value)  Change the value of an array item. More...
  void set_member (String const &name, JSONValue::pointer_t value)  Change the value of an object member. More...
  String to_string () const  Get the JSONValue as a string. More...
  Private Attributes array_t f_array = array_t()   Float64 f_float = Float64()   Int64 f_integer = Int64()   object_t f_object = object_t()   Position f_position = Position()   bool f_saving = false   String f_string = String()   type_t const f_type   Friends class saving_t   Detailed Description

Definition at line 68 of file json.h.

Member Typedef Documentation

Definition at line 72 of file json.h.

Definition at line 73 of file json.h.

typedef std::shared_ptr<JSONValue> as2js::JSON::JSONValue::pointer_t

Definition at line 71 of file json.h.

Member Enumeration Documentation
EnumeratorJSON_TYPE_UNKNOWN  JSON_TYPE_ARRAY  JSON_TYPE_FALSE  JSON_TYPE_FLOAT64  JSON_TYPE_INT64  JSON_TYPE_NULL  JSON_TYPE_OBJECT  JSON_TYPE_STRING  JSON_TYPE_TRUE 

Definition at line 75 of file json.h.

Constructor & Destructor Documentation
as2js::JSON::JSONValue::JSONValue ( Position const &  position)

The NULL constructor only accepts a position and it marks this JSON value as a NULL value.

The type of this JSONValue will be set to JSON_TYPE_NULL.

Parameters
[in]positionThe position where this JSONValue was read from.

Definition at line 235 of file json.cpp.

Referenced by as2js::JSON::read_json_value().

as2js::JSON::JSONValue::JSONValue ( Position const &  position, Int64  integer  )

The integer constructor accepts a position and an integer. It creates an integer JSONValue object.

The value cannot be modified, however, it can be retrieved using the get_int64() function.

The type of this JSONValue will be set to JSON_TYPE_INT64.

Parameters
[in]positionThe position where this JSONValue was read from. [in]integerThe integer to save in this JSONValue.

Definition at line 255 of file json.cpp.

as2js::JSON::JSONValue::JSONValue ( Position const &  position, Float64  floating_point  )

The floating point constructor accepts a position and a floating point number.

The value cannot be modified, however, it can be retrieved using the get_float64() function.

The type of this JSON will be JSON_TYPE_FLOAT64.

Parameters
[in]positionThe position where this JSONValue was read from. [in]floating_pointThe floating point to save in the JSONValue.

Definition at line 276 of file json.cpp.

as2js::JSON::JSONValue::JSONValue ( Position const &  position, String const &  string  )

The string constructor accepts a position and a string parameter.

The value cannot be modified, however, it can be retrieved using the get_string() function.

The type of this JSONValue will be set to JSON_TYPE_STRING.

Parameters
[in]positionThe position where this JSONValue was read from. [in]stringThe string to save in this JSONValue object.

Definition at line 296 of file json.cpp.

as2js::JSON::JSONValue::JSONValue ( Position const &  position, bool  boolean  )

The Boolean constructor accepts a position and a Boolean value: true or false.

The value cannot be modified, however, it can be tested using the get_type() function and check the type of object.

The type of this JSONValue will be set to JSON_TYPE_TRUE when the boolean parameter is true, and to JSON_TYPE_FALSE when the boolean parameter is false.

Parameters
[in]positionThe position where this JSONValue was read from. [in]booleanThe boolean value to save in this JSONValue object.

Definition at line 319 of file json.cpp.

as2js::JSON::JSONValue::JSONValue ( Position const &  position, array_t const &  array  )

The array constructor accepts a position and an array as parameters.

The array in this JSONValue can be modified using the set_item() function. Also, it can be retrieved using the get_array() function.

The type of this JSONValue will be set to JSON_TYPE_ARRAY.

Parameters
[in]positionThe position where this JSONValue was read from. [in]arrayThe array value to save in this JSONValue object.

Definition at line 338 of file json.cpp.

as2js::JSON::JSONValue::JSONValue ( Position const &  position, object_t const &  object  )

The object constructor accepts a position and an object.

The object in this JSONValue can be modified using the set_member() function. Also, it can be retrieved using the get_object() function.

The type of this JSONValue will be set to JSON_TYPE_OBJECT.

Parameters
[in]positionThe position where this JSONValue was read from. [in]objectThe object value to save in this JSONValue object.

Definition at line 358 of file json.cpp.

Member Function Documentation
JSON::JSONValue::array_t const & as2js::JSON::JSONValue::get_array ( ) const

This function is used to retrieve a read-only reference to the array of a JSON_TYPE_ARRAY JSONValue.

You may change the array using the set_item() function. Note that if you did not make a copy of the array returned by this function, you will see the changes. It also means that iterators are likely not going to work once a call to set_item() was made.

Exceptions
exception_internal_errorThis exception is raised if the JSONValue object is not of type JSON_TYPE_ARRAY.
Returns
A constant reference to a JSONValue array_t object.

Definition at line 486 of file json.cpp.

References f_array, f_type, and JSON_TYPE_ARRAY.

Float64 as2js::JSON::JSONValue::get_float64 ( ) const

This function is used to retrieve the floating point from a JSON_TYPE_FLOAT64 JSONValue.

It is not possible to change the floating point value directly. Instead you have to create a new JSONValue with the new value and replace this object with the new one.

Exceptions
exception_internal_errorThis exception is raised if the JSONValue object is not of type JSON_TYPE_FLOAT64.
Returns
A Float64 object.

Definition at line 440 of file json.cpp.

References f_float, f_type, and JSON_TYPE_FLOAT64.

Int64 as2js::JSON::JSONValue::get_int64 ( ) const

This function is used to retrieve the integer from a JSON_TYPE_INT64 JSONValue.

It is not possible to change the integer value directly. Instead you have to create a new JSONValue with the new value and replace this object with the new one.

Exceptions
exception_internal_errorThis exception is raised if the JSONValue object is not of type JSON_TYPE_INT64.
Returns
An Int64 object.

Definition at line 415 of file json.cpp.

References f_integer, f_type, and JSON_TYPE_INT64.

JSON::JSONValue::object_t const & as2js::JSON::JSONValue::get_object ( ) const

This function is used to retrieve a read-only reference to the object of a JSON_TYPE_OBJECT JSONValue.

You may change the object using the set_member() function. Note that if you did not make a copy of the object returned by this function, you will see the changes. It also means that iterators are likely not going to work once a call to set_member() was made.

Exceptions
exception_internal_errorThis exception is raised if the JSONValue object is not of type JSON_TYPE_OBJECT.
Returns
A constant reference to a JSONValue object_t object.

Definition at line 568 of file json.cpp.

References f_object, f_type, and JSON_TYPE_OBJECT.

Position const & as2js::JSON::JSONValue::get_position ( ) const

This function returns a constant reference to the JSONValue position.

This position object is specific to this JSONValue so each one of them can have a different position.

The position of a JSONValue cannot be modified. When creating a JSONValue, the position passed in as a parameter is copied in the f_position of the JSONValue.

Returns
The position of the JSONValue object in the source.

Definition at line 649 of file json.cpp.

References f_position.

String const & as2js::JSON::JSONValue::get_string ( ) const

This function lets you retrieve the string of a JSON_TYPE_STRING object.

Exceptions
exception_internal_errorThis exception is raised if the JSONValue object is not of type JSON_TYPE_STRING.
Returns
The string of this JSONValue object.

Definition at line 460 of file json.cpp.

References f_string, f_type, and JSON_TYPE_STRING.

JSON::JSONValue::type_t as2js::JSON::JSONValue::get_type ( ) const

The type of a JSONValue cannot be modified. This value is read-only.

The type determines what get_...() and what set_...() (if any) functions can be called against this JSONValue object. If an invalid function is called, then an exception is raised. To know which functions are valid for this object, you need to check out its type.

Note that the Boolean JSONValue objects do not have any getter or setter functions. Their type defines their value: JSON_TYPE_TRUE and JSON_TYPE_FALSE.

The type is one of:

  • JSON_TYPE_ARRAY
  • JSON_TYPE_FALSE
  • JSON_TYPE_FLOAT64
  • JSON_TYPE_INT64
  • JSON_TYPE_NULL
  • JSON_TYPE_OBJECT
  • JSON_TYPE_STRING
  • JSON_TYPE_TRUE

A JSONValue cannot have the special type JSON_TYPE_UNKNOWN.

Returns
The type of this JSONValue.

Definition at line 394 of file json.cpp.

References f_type.

void as2js::JSON::JSONValue::set_item ( size_t  idx, JSONValue::pointer_t  value  )

This function is used to change the value of an array item. The index (idx) defines the position of the item to change. The value is the new value to save at that position.

Note that the pointer to the value cannot be set to NULL.

The index (idx) can be any value between 0 and the current size of the array. If idx is larger, then an exception is raised.

When idx is set to the current size of the array, the value is pushed at the end of the array (i.e. a new item is added to the existing array.)

Exceptions
exception_internal_errorIf the JSONValue is not of type JSON_TYPE_ARRAY, then this function raises this exception. exception_index_out_of_rangeIf idx is out of range (larger than the array size) then this exception is raised. Note that idx is unsigned so it cannot be negative. exception_invalid_dataIf the value pointer is a NULL pointer, then this exception is raised.
Parameters
[in]idxThe index where value is to be saved. [in]valueThe new value to be saved.

Definition at line 525 of file json.cpp.

References f_array, f_type, and JSON_TYPE_ARRAY.

void as2js::JSON::JSONValue::set_member ( String const &  name, JSONValue::pointer_t  value  )

This function is used to change the value of an object member. The name defines the member to change. The value is the new value to save along that name.

The name can be any string except the empty string. If name is set to the empty string, then an exception is raised.

If a member with the same name already exists, it gets overwritten with this new value. If the name is new, then the object is modified which may affect your copy of the object, if you have one.

In order to remove an object member, set it to a null pointer:

Exceptions
exception_internal_errorIf the JSONValue is not of type JSON_TYPE_OBJECT, then this function raises this exception. exception_invalid_indexIf name is the empty string then this exception is raised. exception_invalid_dataIf the value pointer is a NULL pointer, then this exception is raised.
Parameters
[in]nameThe name of the object field. [in]valueThe new value to be saved.

Definition at line 610 of file json.cpp.

References f_object, f_type, and JSON_TYPE_OBJECT.

String as2js::JSON::JSONValue::to_string ( ) const

This function transforms a JSONValue object into a string.

This is used to serialize the JSONValue and output it to a string.

This function may raise an exception in the event the JSONValue is cyclic, meaning that a child JSONValue points back at one of its parent JSONValue's.

Exceptions
exception_internal_errorThis exception is raised if a JSONValue object is of type JSON_TYPE_UNKNOWN, which should not happen.
Returns
A string representing the JSONValue.

Definition at line 671 of file json.cpp.

References as2js::anonymous_namespace{json.cpp}::append_string(), f_array, f_float, f_integer, f_object, f_string, f_type, as2js::Int64::get(), as2js::Float64::get(), JSON_TYPE_ARRAY, JSON_TYPE_FALSE, JSON_TYPE_FLOAT64, JSON_TYPE_INT64, JSON_TYPE_NULL, JSON_TYPE_OBJECT, JSON_TYPE_STRING, JSON_TYPE_TRUE, and JSON_TYPE_UNKNOWN.

Friends And Related Function Documentation
friend class saving_t friend

Definition at line 121 of file json.h.

Member Data Documentation
array_t as2js::JSON::JSONValue::f_array = array_t() private

Definition at line 130 of file json.h.

Referenced by get_array(), set_item(), and to_string().

Float64 as2js::JSON::JSONValue::f_float = Float64() private

Definition at line 128 of file json.h.

Referenced by get_float64(), and to_string().

Int64 as2js::JSON::JSONValue::f_integer = Int64() private

Definition at line 127 of file json.h.

Referenced by get_int64(), and to_string().

object_t as2js::JSON::JSONValue::f_object = object_t() private

Definition at line 131 of file json.h.

Referenced by get_object(), set_member(), and to_string().

Position as2js::JSON::JSONValue::f_position = Position() private

Definition at line 124 of file json.h.

Referenced by get_position().

bool as2js::JSON::JSONValue::f_saving = false private
String as2js::JSON::JSONValue::f_string = String() private

Definition at line 129 of file json.h.

Referenced by get_string(), and to_string().

type_t const as2js::JSON::JSONValue::f_type private

The documentation for this class was generated from the following files:
  • /home/snapwebsites/snapcpp/contrib/as2js/include/as2js/json.h
  • /home/snapwebsites/snapcpp/contrib/as2js/lib/json.cpp

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.

Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly