BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdljsn::JsonNumber Class Reference

#include <bdljsn_jsonnumber.h>

Public Types

enum  { k_OVERFLOW = Nu::k_OVERFLOW , k_UNDERFLOW = Nu::k_UNDERFLOW , k_NOT_INTEGRAL = Nu::k_NOT_INTEGRAL , k_INEXACT = Nu::k_INEXACT }
 
typedef bsl::allocator allocator_type
 

Public Member Functions

 BSLMF_NESTED_TRAIT_DECLARATION (JsonNumber, bslmf::IsBitwiseMoveable)
 
 JsonNumber ()
 
 JsonNumber (const allocator_type &allocator)
 
 JsonNumber (const char *text, const allocator_type &allocator=allocator_type())
 
 JsonNumber (const bsl::string_view &text, const allocator_type &allocator=allocator_type())
 
 JsonNumber (bslmf::MovableRef< bsl::string > text)
 
 JsonNumber (bslmf::MovableRef< bsl::string > text, const allocator_type &allocator)
 
 JsonNumber (int value, const allocator_type &allocator=allocator_type())
 
 JsonNumber (unsigned int value, const allocator_type &allocator=allocator_type())
 
 JsonNumber (bsls::Types::Int64 value, const allocator_type &allocator=allocator_type())
 
 JsonNumber (bsls::Types::Uint64 value, const allocator_type &allocator=allocator_type())
 
 JsonNumber (float value, const allocator_type &allocator=allocator_type())
 
 JsonNumber (double value, const allocator_type &allocator=allocator_type())
 
 JsonNumber (bdldfp::Decimal64 value, const allocator_type &allocator=allocator_type())
 
 JsonNumber (const JsonNumber &original, const allocator_type &allocator=allocator_type())
 
 JsonNumber (bslmf::MovableRef< JsonNumber > original) BSLS_KEYWORD_NOEXCEPT
 
 JsonNumber (bslmf::MovableRef< JsonNumber > original, const allocator_type &allocator)
 
 ~JsonNumber ()=default
 
JsonNumberoperator= (const JsonNumber &rhs)
 
JsonNumberoperator= (bslmf::MovableRef< JsonNumber > rhs)
 
JsonNumberoperator= (int rhs)
 
JsonNumberoperator= (unsigned int rhs)
 
JsonNumberoperator= (bsls::Types::Int64 rhs)
 
JsonNumberoperator= (bsls::Types::Uint64 rhs)
 
JsonNumberoperator= (float rhs)
 
JsonNumberoperator= (double rhs)
 
JsonNumberoperator= (bdldfp::Decimal64 rhs)
 
void swap (JsonNumber &other)
 
bool isEqual (const JsonNumber &other) const
 
bool isIntegral () const
 
const bsl::stringvalue () const
 Return the textual representation of this JsonNumber.
 
int asInt (int *result) const
 
int asInt64 (bsls::Types::Int64 *result) const
 
int asUint (unsigned int *result) const
 
int asUint64 (bsls::Types::Uint64 *result) const
 
float asFloat () const
 
double asDouble () const
 
bdldfp::Decimal64 asDecimal64 () const
 
int asDecimal64Exact (bdldfp::Decimal64 *result) const
 
bslma::Allocator *BSLS_ANNOTATION_DEPRECATED allocator () const
 
allocator_type get_allocator () const
 Return the allocator used by this object to supply memory.
 
bsl::ostream & print (bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
 

Static Public Member Functions

static bool isValidNumber (const bsl::string_view &text)
 

Friends

void swap (JsonNumber &, JsonNumber &)
 

Detailed Description

This class defines a value-semantic class that represents a JSON number. Objects of this class have a value determined at construction and does not change except by assignment from or swap with another JsonNumber object. The value can be specified by supplying a string that conforms to the {JSON Textual Specification} or from one of the {Supported Types}. The value of a JSON object can be converted to any of those types; however, some of those conversions can be inexact.

See bdljsn_jsonnumber

Member Typedef Documentation

◆ allocator_type

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
k_OVERFLOW 
k_UNDERFLOW 
k_NOT_INTEGRAL 
k_INEXACT 

Constructor & Destructor Documentation

◆ JsonNumber() [1/16]

bdljsn::JsonNumber::JsonNumber ( )
inline

◆ JsonNumber() [2/16]

bdljsn::JsonNumber::JsonNumber ( const allocator_type allocator)
inlineexplicit

Create a JsonNumber having the value "0". Optionally specify an allocator (e.g., the address of a bslma::Allocator object) used to supply memory.

◆ JsonNumber() [3/16]

bdljsn::JsonNumber::JsonNumber ( const char *  text,
const allocator_type allocator = allocator_type() 
)
inlineexplicit

◆ JsonNumber() [4/16]

bdljsn::JsonNumber::JsonNumber ( const bsl::string_view text,
const allocator_type allocator = allocator_type() 
)
inlineexplicit

Create a JsonNumber having the value of the specified text. Optionally specify an allocator (e.g., the address of a bslma::Allocator object) used to supply memory. The behavior is undefined unless isValidJsonNumber(text) is true. See {JSON Textual Specification}.

◆ JsonNumber() [5/16]

bdljsn::JsonNumber::JsonNumber ( bslmf::MovableRef< bsl::string text)
inlineexplicit

Create a JsonNumber object having the same value and the same allocator as the specified text. The contents of the value string becomes unspecified but valid, and its allocator remains unchanged. The behavior is undefined unless isValidNumber(text) is true. See {JSON Textual Specification}.

◆ JsonNumber() [6/16]

bdljsn::JsonNumber::JsonNumber ( bslmf::MovableRef< bsl::string text,
const allocator_type allocator 
)
inlineexplicit

Create a JsonNumber object having the same value as the specified text, using the specified allocator (e.g., the address of a bslma::Allocator object) to supply memory. The allocator of the text string remains unchanged. If the text and the newly created object have the same allocator then the contents of text string becomes unspecified but valid, and no exceptions will be thrown; otherwise the text string is unchanged and an exception may be thrown. The behavior is undefined unless isValidNumber(text) is true. See {JSON Textual Specification}.

◆ JsonNumber() [7/16]

bdljsn::JsonNumber::JsonNumber ( int  value,
const allocator_type allocator = allocator_type() 
)
inlineexplicit

◆ JsonNumber() [8/16]

bdljsn::JsonNumber::JsonNumber ( unsigned int  value,
const allocator_type allocator = allocator_type() 
)
inlineexplicit

◆ JsonNumber() [9/16]

bdljsn::JsonNumber::JsonNumber ( bsls::Types::Int64  value,
const allocator_type allocator = allocator_type() 
)
inlineexplicit

◆ JsonNumber() [10/16]

bdljsn::JsonNumber::JsonNumber ( bsls::Types::Uint64  value,
const allocator_type allocator = allocator_type() 
)
inlineexplicit

Create a JsonNumber having the specified value. Optionally specify an allocator (e.g., the address of a bslma::Allocator object) used to supply memory.

◆ JsonNumber() [11/16]

bdljsn::JsonNumber::JsonNumber ( float  value,
const allocator_type allocator = allocator_type() 
)
inlineexplicit

◆ JsonNumber() [12/16]

bdljsn::JsonNumber::JsonNumber ( double  value,
const allocator_type allocator = allocator_type() 
)
inlineexplicit

◆ JsonNumber() [13/16]

bdljsn::JsonNumber::JsonNumber ( bdldfp::Decimal64  value,
const allocator_type allocator = allocator_type() 
)
inlineexplicit

Create a JsonNumber having the specified value. Optionally specify an allocator (e.g., the address of a bslma::Allocator object) used to supply memory. The behavior is undefined if the value is infinite (INF) or not-a-number (NaN).

◆ JsonNumber() [14/16]

bdljsn::JsonNumber::JsonNumber ( const JsonNumber original,
const allocator_type allocator = allocator_type() 
)
inline

Create a JsonNumber object having the same value as the specified original object. Optionally specify an allocator (e.g., the address of a bslma::Allocator object) used to supply memory.

◆ JsonNumber() [15/16]

bdljsn::JsonNumber::JsonNumber ( bslmf::MovableRef< JsonNumber original)
inline

Create a JsonNumber object having the same value and the same allocator as the specified original object. The value of original becomes unspecified but valid, and its allocator remains unchanged.

◆ JsonNumber() [16/16]

bdljsn::JsonNumber::JsonNumber ( bslmf::MovableRef< JsonNumber original,
const allocator_type allocator 
)
inline

Create a JsonNumber object having the same value as the specified original object, using the specified allocator (e.g., the address of a bslma::Allocator object) to supply memory. The allocator of original remains unchanged. If original and the newly created object have the same allocator then the value of original becomes unspecified but valid, and no exceptions will be thrown; otherwise original is unchanged (and an exception may be thrown).

◆ ~JsonNumber()

bdljsn::JsonNumber::~JsonNumber ( )
default

Member Function Documentation

◆ allocator()

bslma::Allocator * bdljsn::JsonNumber::allocator ( ) const
inline
Deprecated:
Use get_allocator() instead.

Return get_allocator().mechanism(), i.e., the memory resource used by this object to supply memory.

◆ asDecimal64()

bdldfp::Decimal64 bdljsn::JsonNumber::asDecimal64 ( ) const
inline

◆ asDecimal64Exact()

int bdljsn::JsonNumber::asDecimal64Exact ( bdldfp::Decimal64 result) const
inline

Load to the specified result the closest floating point representation to this number, even if a non-zero status is returned. Return 0 if this number can be represented exactly, and return k_INEXACT and load result with the closest approximation if value cannot be represented exactly. If this number is outside the representable range, load result with +INF or -INF (as appropriate). A number can be represented exactly as a Decimal64 if, for the significand and exponent, abs(significand) <= 9,999,999,999,999,999 and -398 <= exponent <= 369.

◆ asDouble()

double bdljsn::JsonNumber::asDouble ( ) const
inline

◆ asFloat()

float bdljsn::JsonNumber::asFloat ( ) const
inline

Return the closest floating point representation to this number. If this number is outside the representable range, return +INF or -INF (as appropriate). Note that values smaller than the smallest representable non-zero value (a.k.a, MIN) are rounded to MIN (positive or negative, as appropriate) or 0, whichever is the better approximation.

◆ asInt()

int bdljsn::JsonNumber::asInt ( int *  result) const
inline

Load into the specified result the integer value of this number. Return 0 on success, k_OVERFLOW if value is larger than can be represented by result, k_UNDERFLOW if value is smaller than can be represented by result, and k_NOT_INTEGRAL if value is not an integral number (i.e., there is a fractional part). For underflow, result will be loaded with the minimum representable value, for overflow, result will be loaded with the maximum representable value, for non-integral values result will be loaded with the integer part of value (truncating the fractional part). If the result is not an integer and also either overflows or underflows, it is treated as an overflow or underflow (respectively). Note that this operation returns an error status value (unlike similar floating point conversions) because typically it is an error if a conversion to an integer results in an in-exact value.

◆ asInt64()

int bdljsn::JsonNumber::asInt64 ( bsls::Types::Int64 result) const
inline

◆ asUint()

int bdljsn::JsonNumber::asUint ( unsigned int *  result) const
inline

◆ asUint64()

int bdljsn::JsonNumber::asUint64 ( bsls::Types::Uint64 result) const
inline

◆ BSLMF_NESTED_TRAIT_DECLARATION()

bdljsn::JsonNumber::BSLMF_NESTED_TRAIT_DECLARATION ( JsonNumber  ,
bslmf::IsBitwiseMoveable   
)

◆ get_allocator()

JsonNumber::allocator_type bdljsn::JsonNumber::get_allocator ( ) const
inline

◆ isEqual()

bool bdljsn::JsonNumber::isEqual ( const JsonNumber other) const
inline

Return true if this number and the specified other number represent the same numeric value, and false otherwise. This method will return true for differing representations of the same number (e.g., 1.0, "1", "0.1e+1" are all equivalent) except in cases where the exponent cannot be represented by a 64-bit integer. If the exponent is outside the range of a 64-bit integer, true will be returned if *this == other. For example, comparing "1e18446744073709551615" with itself will return true, but comparing it to "10e18446744073709551614" will return false. Note that this method is more computationally expensive than the equality and inequality operators.

◆ isIntegral()

bool bdljsn::JsonNumber::isIntegral ( ) const
inline

Return true if the value of this JsonNumber is an (exact) integral value, or false otherwise. Note that this function may return true even this number cannot be represented in a fundamental integral type.

◆ isValidNumber()

bool bdljsn::JsonNumber::isValidNumber ( const bsl::string_view text)
inlinestatic

Return true if the specified text complies with the grammar of a JSON number, and false otherwise. See the {JSON Textual Specification}.

◆ operator=() [1/9]

JsonNumber & bdljsn::JsonNumber::operator= ( bdldfp::Decimal64  rhs)
inline

◆ operator=() [2/9]

JsonNumber & bdljsn::JsonNumber::operator= ( bslmf::MovableRef< JsonNumber rhs)
inline

Assign to this object the value of the specified rhs object, and return a non-const reference to this object. The allocators of this object and rhs both remain unchanged. If rhs and this object have the same allocator then the value of rhs becomes unspecified but valid, and no exceptions will be thrown; otherwise rhs is unchanged (and an exception may be thrown).

◆ operator=() [3/9]

JsonNumber & bdljsn::JsonNumber::operator= ( bsls::Types::Int64  rhs)
inline

◆ operator=() [4/9]

JsonNumber & bdljsn::JsonNumber::operator= ( bsls::Types::Uint64  rhs)
inline

◆ operator=() [5/9]

JsonNumber & bdljsn::JsonNumber::operator= ( const JsonNumber rhs)
inline

Assign to this object the value of the specified rhs object, and return a non-const reference to this object.

◆ operator=() [6/9]

JsonNumber & bdljsn::JsonNumber::operator= ( double  rhs)
inline

◆ operator=() [7/9]

JsonNumber & bdljsn::JsonNumber::operator= ( float  rhs)
inline

Assign to this object the value of the specified rhs, and return a non-const reference to this object. The behavior is undefined if rhs is infinite (INF) or not-a-number (NaN).

◆ operator=() [8/9]

JsonNumber & bdljsn::JsonNumber::operator= ( int  rhs)
inline

Assign to this object the value of the specified rhs, and return a non-const reference to this object.

◆ operator=() [9/9]

JsonNumber & bdljsn::JsonNumber::operator= ( unsigned int  rhs)
inline

◆ print()

bsl::ostream & bdljsn::JsonNumber::print ( bsl::ostream &  stream,
int  level = 0,
int  spacesPerLevel = 4 
) const

Write the value of this object to the specified output stream in a human-readable format, and return a non-const reference to stream. Optionally specify an initial indentation level, whose absolute value is incremented recursively for nested objects. If level is specified, optionally specify spacesPerLevel, whose absolute value indicates the number of spaces per indentation level for this and all of its nested objects. If level is negative, suppress indentation of the first line. If spacesPerLevel is negative, format the entire output on one line, suppressing all but the initial indentation (as governed by level). If stream is not valid on entry, this operation has no effect. Note that the format is not fully specified, and can change without notice.

◆ swap()

void bdljsn::JsonNumber::swap ( JsonNumber other)
inline

Efficiently exchange the value of this object with the value of the specified other object. This method provides the no-throw exception-safety guarantee. The behavior is undefined unless this object was created with the same allocator as other.

◆ value()

const bsl::string & bdljsn::JsonNumber::value ( ) const
inline

Friends And Related Symbol Documentation

◆ swap

void swap ( JsonNumber ,
JsonNumber  
)
friend

Exchange the values of the specified a and b objects. This function provides the no-throw exception-safety guarantee if the two objects were created with the same allocator and the basic guarantee otherwise.


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