MySQL 8.0.40
Source Code Documentation
Json_object Class Referencefinal

Represents a JSON container value of type "object" (ECMA), type J_OBJECT here. More...

#include <json_dom.h>

Inheritance diagram for Json_object:
[legend]

Public Types

typedef Json_object_map::const_iterator const_iterator
 Constant iterator over the elements in the JSON object. More...
 

Public Member Functions

 Json_object ()
 
enum_json_type json_type () const override
 
bool add_clone (const std::string &key, const Json_dom *value)
 Insert a clone of the value into the object. More...
 
bool add_alias (const std::string &key, Json_dom *value)
 Insert the value into the object. More...
 
bool add_alias (const std::string &key, Json_dom_ptr value)
 Insert the value into the object. More...
 
bool consume (Json_object_ptr other)
 Transfer all of the key/value pairs in the other object into this object. More...
 
Json_domget (const std::string &key) const
 Return the value at key. More...
 
Json_domget (const MYSQL_LEX_CSTRING &key) const
 
bool remove (const std::string &key)
 Remove the child element addressed by key. More...
 
size_t cardinality () const
 
uint32 depth () const override
 Compute the depth of a document. More...
 
Json_dom_ptr clone () const override
 Make a deep clone. More...
 
void replace_dom_in_container (const Json_dom *oldv, Json_dom_ptr newv) override
 Replace oldv contained inside this container array or object) with newv. More...
 
void clear ()
 Remove all elements in the object. More...
 
const_iterator begin () const
 Returns a const_iterator that refers to the first element. More...
 
const_iterator end () const
 Returns a const_iterator that refers past the last element. More...
 
bool merge_patch (Json_object_ptr patch)
 Implementation of the MergePatch function specified in RFC 7396: More...
 
- Public Member Functions inherited from Json_dom
virtual ~Json_dom ()=default
 
void * operator new (size_t size, const std::nothrow_t &) noexcept
 Allocate space on the heap for a Json_dom object. More...
 
void operator delete (void *ptr) noexcept
 Deallocate the space used by a Json_dom object. More...
 
void operator delete (void *ptr, const std::nothrow_t &) noexcept
 Nothrow delete. More...
 
Json_containerparent () const
 Get the parent dom to which this dom is attached. More...
 
virtual bool is_scalar () const
 
virtual bool is_number () const
 
Json_path get_location () const
 Get the path location of this dom, measured from the outermost document it nests inside. More...
 
bool seek (const Json_seekable_path &path, size_t legs, Json_dom_vector *hits, bool auto_wrap, bool only_need_one)
 Finds all of the json sub-documents which match the path expression. More...
 

Private Attributes

Json_object_map m_map
 Map to hold the object elements. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Json_dom
static Json_dom_ptr parse (const char *text, size_t length, const JsonParseErrorHandler &error_handler, const JsonDocumentDepthHandler &depth_handler)
 Parse Json text to DOM (using rapidjson). More...
 
static Json_dom_ptr parse (const json_binary::Value &v)
 Construct a DOM object based on a binary JSON value. More...
 

Detailed Description

Represents a JSON container value of type "object" (ECMA), type J_OBJECT here.

Member Typedef Documentation

◆ const_iterator

typedef Json_object_map::const_iterator Json_object::const_iterator

Constant iterator over the elements in the JSON object.

Each element is represented as a std::pair where first is a std::string that represents the key name, and second is a pointer to a Json_dom that represents the value.

Constructor & Destructor Documentation

◆ Json_object()

Json_object::Json_object ( )

Member Function Documentation

◆ add_alias() [1/2]

bool Json_object::add_alias ( const std::string &  key,
Json_dom value 
)
inline

Insert the value into the object.

If the key already exists in the object, the existing value is replaced ("last value wins").

Ownership of the value is effectively transferred to the object and the value will be deallocated by the object so only add values that can be deallocated safely (no stack variables please!)

New code should prefer add_alias(const std::string&, Json_dom_ptr) to this function, because that makes the transfer of ownership more explicit. This function might be removed in the future.

Parameters
[in]keythe JSON key of to be added
[in]valuea JSON value: the key's value
Return values
falseon success
trueon failure

◆ add_alias() [2/2]

bool Json_object::add_alias ( const std::string &  key,
Json_dom_ptr  value 
)

Insert the value into the object.

If the key already exists in the object, the existing value is replaced ("last value wins").

The ownership of the value is transferred to the object.

Parameters
[in]keythe key of the value to be added
[in]valuethe value to add
Returns
false on success, true on failure

◆ add_clone()

bool Json_object::add_clone ( const std::string &  key,
const Json_dom value 
)
inline

Insert a clone of the value into the object.

If the key already exists in the object, the existing value is replaced ("last value wins").

Parameters
[in]keythe JSON element key of to be added
[in]valuea JSON value: the element key's value
Return values
falseon success
trueon failure

◆ begin()

const_iterator Json_object::begin ( ) const
inline

Returns a const_iterator that refers to the first element.

◆ cardinality()

size_t Json_object::cardinality ( ) const
Returns
The number of elements in the JSON object.

◆ clear()

void Json_object::clear ( )
inline

Remove all elements in the object.

◆ clone()

Json_dom_ptr Json_object::clone ( ) const
overridevirtual

Make a deep clone.

The ownership of the returned object is henceforth with the caller.

Returns
a cloned Json_dom object.

Implements Json_dom.

◆ consume()

bool Json_object::consume ( Json_object_ptr  other)

Transfer all of the key/value pairs in the other object into this object.

The other object is deleted. If this object and the other object share a key, then the two values of the key are merged.

Parameters
[in]othera pointer to the object which will be consumed
Return values
falseon success
trueon failure

◆ depth()

uint32 Json_object::depth ( ) const
overridevirtual

Compute the depth of a document.

This is the value which would be returned by the JSON_DEPTH() system function.

  • for scalar values, empty array and empty object: 1
  • for non-empty array: 1+ max(depth of array elements)
  • for non-empty objects: 1+ max(depth of object values)

For example: "abc", [] and {} have depth 1. ["abc", [3]] and {"a": "abc", "b": [3]} have depth 3.

Returns
the depth of the document

Implements Json_dom.

◆ end()

const_iterator Json_object::end ( ) const
inline

Returns a const_iterator that refers past the last element.

◆ get() [1/2]

Json_dom * Json_object::get ( const MYSQL_LEX_CSTRING key) const

◆ get() [2/2]

Json_dom * Json_object::get ( const std::string &  key) const

Return the value at key.

The value is not cloned, so make one if you need it. Do not delete the returned value, please! If the key is not present, return a null pointer.

Parameters
[in]keythe key of the element whose value we want
Returns
the value associated with the key, or NULL if the key is not found

◆ json_type()

enum_json_type Json_object::json_type ( ) const
inlineoverridevirtual
Returns
the type corresponding to the actual Json_dom subclass

Implements Json_dom.

◆ merge_patch()

bool Json_object::merge_patch ( Json_object_ptr  patch)

Implementation of the MergePatch function specified in RFC 7396:

define MergePatch(Target, Patch):
  if Patch is an Object:
    if Target is not an Object:
      Target = {} # Ignore the contents and set it to an empty Object
    for each Key/Value pair in Patch:
      if Value is null:
        if Key exists in Target:
          remove the Key/Value pair from Target
      else:
        Target[Key] = MergePatch(Target[Key], Value)
    return Target
  else:
    return Patch
Parameters
patchthe object that describes the patch
Return values
falseon success
trueon memory allocation error

◆ remove()

bool Json_object::remove ( const std::string &  key)

Remove the child element addressed by key.

The removed child is deleted.

Parameters
keythe key of the element to remove
Return values
trueif an element was removed
falseif there was no element with that key

◆ replace_dom_in_container()

void Json_object::replace_dom_in_container ( const Json_dom oldv,
Json_dom_ptr  newv 
)
overridevirtual

Replace oldv contained inside this container array or object) with newv.

If this container does not contain oldv, calling the method is a no-op.

Parameters
[in]oldvthe value to be replaced
[in]newvthe new value to put in the container

Implements Json_container.

Member Data Documentation

◆ m_map

Json_object_map Json_object::m_map
private

Map to hold the object elements.


The documentation for this class was generated from the following files: