sajson
High-Performance JSON Parser
Public Member Functions | Friends | List of all members
sajson::value Class Reference

Represents a JSON value. More...

#include <sajson.h>

Public Member Functions

type get_type () const
 Returns the JSON value's type. More...
 
size_t get_length () const
 Returns the length of the object or array. More...
 
value get_array_element (size_t index) const
 Returns the nth element of an array. More...
 
string get_object_key (size_t index) const
 Returns the nth key of an object. More...
 
value get_object_value (size_t index) const
 Returns the nth value of an object. More...
 
value get_value_of_key (const string &key) const
 Given a string key, returns the value with that key or a null value if the key is not found. More...
 
size_t find_object_key (const string &key) const
 Given a string key, returns the index of the associated value if one exists. More...
 
int get_integer_value () const
 If a numeric value was parsed as a 32-bit integer, returns it. More...
 
double get_double_value () const
 If a numeric value was parsed as a double, returns it. More...
 
double get_number_value () const
 Returns a numeric value as a double-precision float. More...
 
bool get_int53_value (int64_t *out) const
 Returns true and writes to the output argument if the numeric value fits in a 53-bit integer. More...
 
size_t get_string_length () const
 Returns the length of the string. More...
 
const char * as_cstring () const
 Returns a pointer to the beginning of a string value's data. More...
 
std::string as_string () const
 Returns a string's value as a std::string. More...
 

Friends

class document
 

Detailed Description

Represents a JSON value.

First, call get_type() to check its type, which determines which methods are available.

Note that value does not maintain any backing memory, only the corresponding document does. It is illegal to access a value after its document has been destroyed.

Member Function Documentation

type sajson::value::get_type ( ) const
inline

Returns the JSON value's type.

size_t sajson::value::get_length ( ) const
inline

Returns the length of the object or array.

Only legal if get_type() is TYPE_ARRAY or TYPE_OBJECT.

value sajson::value::get_array_element ( size_t  index) const
inline

Returns the nth element of an array.

Calling with an out-of-bound index is undefined behavior. Only legal if get_type() is TYPE_ARRAY.

string sajson::value::get_object_key ( size_t  index) const
inline

Returns the nth key of an object.

Calling with an out-of-bound index is undefined behavior. Only legal if get_type() is TYPE_OBJECT.

value sajson::value::get_object_value ( size_t  index) const
inline

Returns the nth value of an object.

Calling with an out-of-bound index is undefined behavior. Only legal if get_type() is TYPE_OBJECT.

value sajson::value::get_value_of_key ( const string key) const
inline

Given a string key, returns the value with that key or a null value if the key is not found.

Running time is O(lg N). Only legal if get_type() is TYPE_OBJECT.

size_t sajson::value::find_object_key ( const string key) const
inline

Given a string key, returns the index of the associated value if one exists.

Returns get_length() if there is no such key. Note: sajson sorts object keys, so the running time is O(lg N). Only legal if get_type() is TYPE_OBJECT

int sajson::value::get_integer_value ( ) const
inline

If a numeric value was parsed as a 32-bit integer, returns it.

Only legal if get_type() is TYPE_INTEGER.

double sajson::value::get_double_value ( ) const
inline

If a numeric value was parsed as a double, returns it.

Only legal if get_type() is TYPE_DOUBLE.

double sajson::value::get_number_value ( ) const
inline

Returns a numeric value as a double-precision float.

Only legal if get_type() is TYPE_INTEGER or TYPE_DOUBLE.

bool sajson::value::get_int53_value ( int64_t *  out) const
inline

Returns true and writes to the output argument if the numeric value fits in a 53-bit integer.

This is useful for timestamps and other situations where integral values with greater than 32-bit precision are used, as 64-bit values are not understood by all JSON implementations or languages. Returns false if the value is not an integer or not in range. Only legal if get_type() is TYPE_INTEGER or TYPE_DOUBLE.

size_t sajson::value::get_string_length ( ) const
inline

Returns the length of the string.

Only legal if get_type() is TYPE_STRING.

const char* sajson::value::as_cstring ( ) const
inline

Returns a pointer to the beginning of a string value's data.

WARNING: Calling this function and using the return value as a C-style string (that is, without also using get_string_length()) will cause the string to appear truncated if the string has embedded NULs. Only legal if get_type() is TYPE_STRING.

std::string sajson::value::as_string ( ) const
inline

Returns a string's value as a std::string.

Only legal if get_type() is TYPE_STRING.


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