JsonCpp project page Classes Namespace JsonCpp home page

Json Namespace Reference

JSON (JavaScript Object Notation). More...

Classes

class  SecureAllocator
 
class  Features
 Configuration passed to reader and writer. More...
 
class  CharReader
 Interface for reading JSON from a char array. More...
 
class  CharReaderBuilder
 Build a CharReader implementation. More...
 
class  Exception
 Base class for all exceptions we throw. More...
 
class  RuntimeError
 Exceptions which the user cannot easily avoid. More...
 
class  LogicError
 Exceptions thrown by JSON_ASSERT/JSON_FAIL macros. More...
 
class  StaticString
 Lightweight wrapper to tag static string. More...
 
class  Value
 Represents a JSON value. More...
 
class  PathArgument
 Experimental and untested: represents an element of the "path" to access a node. More...
 
class  Path
 Experimental and untested: represents a "path" to access a node. More...
 
class  ValueIteratorBase
 base class for Value iterators. More...
 
class  ValueConstIterator
 const iterator for object and array value. More...
 
class  ValueIterator
 Iterator for object and array value. More...
 
class  StreamWriter
 Usage: More...
 
class  StreamWriterBuilder
 Build a StreamWriter implementation. More...
 

Typedefs

using Int = int
 
using UInt = unsigned int
 
using Int64 = __int64
 
using UInt64 = unsigned __int64
 
using LargestInt = Int64
 
using LargestUInt = UInt64
 
template<typename T >
using Allocator = typename std::conditional< 0, SecureAllocator< T >, std::allocator< T > >::type
 
using String = std::basic_string< char, std::char_traits< char >, Allocator< char > >
 
using IStringStream = std::basic_istringstream< String::value_type, String::traits_type, String::allocator_type >
 
using OStringStream = std::basic_ostringstream< String::value_type, String::traits_type, String::allocator_type >
 
using IStream = std::istream
 
using OStream = std::ostream
 
using ArrayIndex = unsigned int
 
using CharReaderPtr = std::auto_ptr< CharReader >
 
using UIntToStringBuffer = char[uintToStringBufferSize]
 
using StreamWriterPtr = std::auto_ptr< StreamWriter >
 

Enumerations

enum  ValueType {
  nullValue = 0 , intValue , uintValue , realValue ,
  stringValue , booleanValue , arrayValue , objectValue
}
 Type of the value held by a Value object. More...
 
enum  CommentPlacement { commentBefore = 0 , commentAfterOnSameLine , commentAfter , numberOfCommentPlacement }
 
enum  PrecisionType { significantDigits = 0 , decimalPlaces }
 Type of precision for formatting of real values. More...
 
enum  { uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1 }
 

Functions

template<typename T , typename U >
bool operator== (const SecureAllocator< T > &, const SecureAllocator< U > &)
 
template<typename T , typename U >
bool operator!= (const SecureAllocator< T > &, const SecureAllocator< U > &)
 
class __declspec (deprecated("Use CharReader and CharReaderBuilder instead.")) Reader
 Unserialize a JSON document into a Value. More...
 
bool parseFromStream (CharReader::Factory const &, IStream &, Value *root, String *errs)
 Consume entire stream and use its begin/end. More...
 
IStreamoperator>> (IStream &, Value &)
 Read from 'sin' into 'root'. More...
 
 __declspec (noreturn) void throwRuntimeError(String const &msg)
 used internally More...
 
void swap (Value &a, Value &b)
 
String writeString (StreamWriter::Factory const &factory, Value const &root)
 Write into stringstream, then return string, for convenience. More...
 
class __declspec (deprecated("Use StreamWriter instead")) Writer
 Abstract class for writers. More...
 
class __declspec(deprecated("Use StreamWriterBuilder instead")) FastWriter class __declspec(deprecated("Use StreamWriterBuilder instead")) StyledWriter class __declspec (deprecated("Use StreamWriterBuilder instead")) StyledStreamWriter
 Outputs a Value in JSON format without formatting (not human friendly). More...
 
String valueToString (Int value)
 
String valueToString (UInt value)
 
String valueToString (LargestInt value)
 
String valueToString (LargestUInt value)
 
String valueToString (double value, unsigned int precision=Value::defaultRealPrecision, PrecisionType precisionType=PrecisionType::significantDigits)
 
String valueToString (bool value)
 
String valueToQuotedString (const char *value)
 
OStreamoperator<< (OStream &, const Value &root)
 Output using the StyledStreamWriter. More...
 
static char getDecimalPoint ()
 
static String codePointToUTF8 (unsigned int cp)
 Converts a unicode code-point to UTF-8. More...
 
static void uintToString (LargestUInt value, char *&current)
 Converts an unsigned integer to string. More...
 
template<typename Iter >
Iter fixNumericLocale (Iter begin, Iter end)
 Change ',' to '. More...
 
template<typename Iter >
void fixNumericLocaleInput (Iter begin, Iter end)
 
template<typename Iter >
Iter fixZerosInTheEnd (Iter begin, Iter end, unsigned int precision)
 Return iterator that would be the new end of the range [begin,end), if we were to delete zeros in the end of string, but not the last zero before '. More...
 
template<typename T >
static std::unique_ptr< T > cloneUnique (const std::unique_ptr< T > &p)
 
template<typename T , typename U >
static bool InRange (double d, T min, U max)
 
static char * duplicateStringValue (const char *value, size_t length)
 Duplicates the specified string value. More...
 
static char * duplicateAndPrefixStringValue (const char *value, unsigned int length)
 
static void decodePrefixedString (bool isPrefixed, char const *prefixed, unsigned *length, char const **value)
 
static void releasePrefixedStringValue (char *value)
 Free the string duplicated by duplicateStringValue()/duplicateAndPrefixStringValue(). More...
 
static void releaseStringValue (char *value, unsigned)
 
static bool IsIntegral (double d)
 
static bool doesAnyCharRequireEscaping (char const *s, size_t n)
 
static unsigned int utf8ToCodepoint (const char *&s, const char *e)
 
static String toHex16Bit (unsigned int x)
 
static void appendRaw (String &result, unsigned ch)
 
static void appendHex (String &result, unsigned ch)
 
static String valueToQuotedStringN (const char *value, unsigned length, bool emitUTF8=false)
 

Variables

static const char hex2 []
 

Detailed Description

JSON (JavaScript Object Notation).

Typedef Documentation

◆ Allocator

template<typename T >
using Json::Allocator = typedef typename std::conditional< 0 , SecureAllocator<T>, std::allocator<T> >::type

◆ ArrayIndex

using Json::ArrayIndex = typedef unsigned int

◆ CharReaderPtr

using Json::CharReaderPtr = typedef std::auto_ptr<CharReader>

◆ Int

using Json::Int = typedef int

◆ Int64

using Json::Int64 = typedef __int64

◆ IStream

using Json::IStream = typedef std::istream

◆ IStringStream

using Json::IStringStream = typedef std::basic_istringstream<String::value_type, String::traits_type, String::allocator_type>

◆ LargestInt

using Json::LargestInt = typedef Int64

◆ LargestUInt

using Json::LargestUInt = typedef UInt64

◆ OStream

using Json::OStream = typedef std::ostream

◆ OStringStream

using Json::OStringStream = typedef std::basic_ostringstream<String::value_type, String::traits_type, String::allocator_type>

◆ StreamWriterPtr

using Json::StreamWriterPtr = typedef std::auto_ptr<StreamWriter>

◆ String

using Json::String = typedef std::basic_string<char, std::char_traits<char>, Allocator<char> >

◆ UInt

using Json::UInt = typedef unsigned int

◆ UInt64

using Json::UInt64 = typedef unsigned __int64

◆ UIntToStringBuffer

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
uintToStringBufferSize 

Constant that specify the size of the buffer that must be passed to uintToString.

◆ CommentPlacement

Enumerator
commentBefore 

a comment placed on the line before a value

commentAfterOnSameLine 

a comment just after a value on the same line

commentAfter 

a comment on the line after a value (only make sense for

numberOfCommentPlacement 

root value)

◆ PrecisionType

Type of precision for formatting of real values.

Enumerator
significantDigits 

we set max number of significant digits in string

decimalPlaces 

we set max number of digits after "." in string

◆ ValueType

Type of the value held by a Value object.

Enumerator
nullValue 

'null' value

intValue 

signed integer value

uintValue 

unsigned integer value

realValue 

double value

stringValue 

UTF-8 string value.

booleanValue 

bool value

arrayValue 

array value (ordered list)

objectValue 

object value (collection of name/value pairs).

Function Documentation

◆ __declspec() [1/4]

class Json::__declspec ( deprecated("Use CharReader and CharReaderBuilder instead.")  )

Unserialize a JSON document into a Value.

Deprecated:
Use CharReader and CharReaderBuilder.

An error tagged with where in the JSON text it was encountered.

The offsets give the [start, limit) range of bytes within the text. Note that this is bytes, not codepoints.

Constructs a Reader allowing all features for parsing.

Constructs a Reader allowing the specified feature set for parsing.

Read a Value from a JSON document.

Parameters
documentUTF-8 encoded string containing the document to read.
[out]rootContains the root value of the document if it was successfully parsed.
collectCommentstrue to collect comment and allow writing them back during serialization, false to discard comments. This parameter is ignored if Features::allowComments_ is false.
Returns
true if the document was successfully parsed, false if an error occurred.

Read a Value from a JSON document.

Parameters
beginDocPointer on the beginning of the UTF-8 encoded string of the document to read.
endDocPointer on the end of the UTF-8 encoded string of the document to read. Must be >= beginDoc.
[out]rootContains the root value of the document if it was successfully parsed.
collectCommentstrue to collect comment and allow writing them back during serialization, false to discard comments. This parameter is ignored if Features::allowComments_ is false.
Returns
true if the document was successfully parsed, false if an error occurred.

Parse from input stream.

See also
Json::operator>>(std::istream&, Json::Value&).

Returns a user friendly string that list errors in the parsed document.

Returns
Formatted error message with the list of errors with their location in the parsed document. An empty string is returned if no error occurred during parsing.
Deprecated:
Use getFormattedErrorMessages() instead (typo fix).

Returns a user friendly string that list errors in the parsed document.

Returns
Formatted error message with the list of errors with their location in the parsed document. An empty string is returned if no error occurred during parsing.

Returns a vector of structured errors encountered while parsing.

Returns
A (possibly empty) vector of StructuredError objects. Currently only one error can be returned, but the caller should tolerate multiple errors. This can occur if the parser recovers from a non-fatal parse error and then encounters additional errors.

Add a semantic error message.

Parameters
valueJSON Value location associated with the error
messageThe error message.
Returns
true if the error was successfully added, false if the Value offset exceeds the document size.

Add a semantic error message with extra context.

Parameters
valueJSON Value location associated with the error
messageThe error message.
extraAdditional JSON Value location to contextualize the error
Returns
true if the error was successfully added, false if either Value offset exceeds the document size.

Return whether there are any errors.

Returns
true if there are no errors to report false if errors have occurred.

◆ __declspec() [2/4]

class Json::__declspec ( deprecated("Use StreamWriter instead")  )

Abstract class for writers.

Deprecated:
Use StreamWriter. (And really, this is an implementation detail.)

◆ __declspec() [3/4]

class __declspec (deprecated( "Use StreamWriterBuilder instead" )) FastWriter class __declspec (deprecated( "Use StreamWriterBuilder instead" )) StyledWriter class Json::__declspec ( deprecated("Use StreamWriterBuilder instead")  )

Outputs a Value in JSON format without formatting (not human friendly).

The JSON document is written in a single line. It is not intended for 'human' consumption, but may be useful to support feature such as RPC where bandwidth is limited.

See also
Reader, Value
Deprecated:
Use StreamWriterBuilder.

Writes a Value in JSON format in a human friendly way.

The rules for line break and indent are as follow:

  • Object value:
    • if empty then print {} without indent and line break
    • if not empty the print '{', line break & indent, print one value per line and then unindent and line break and print '}'.
  • Array value:
    • if empty then print [] without indent and line break
    • if the array contains no object value, empty array or some other value types, and all the values fit on one lines, then print the array on a single line.
    • otherwise, it the values do not fit on one line, or the array contains object or non empty array, then print one value per line.

If the Value have comments then they are outputed according to their CommentPlacement.

See also
Reader, Value, Value::setComment()
Deprecated:
Use StreamWriterBuilder.

Writes a Value in JSON format in a human friendly way, to a stream rather than to a string.

The rules for line break and indent are as follow:

  • Object value:
    • if empty then print {} without indent and line break
    • if not empty the print '{', line break & indent, print one value per line and then unindent and line break and print '}'.
  • Array value:
    • if empty then print [] without indent and line break
    • if the array contains no object value, empty array or some other value types, and all the values fit on one lines, then print the array on a single line.
    • otherwise, it the values do not fit on one line, or the array contains object or non empty array, then print one value per line.

If the Value have comments then they are outputed according to their CommentPlacement.

See also
Reader, Value, Value::setComment()
Deprecated:
Use StreamWriterBuilder.
Parameters
indentationEach level will be indented by this amount extra.

Serialize a Value in JSON format.

Parameters
outStream to write to. (Can be ostringstream, e.g.)
rootValue to serialize.
Note
There is no point in deriving from Writer, since write() should not return a value.

◆ __declspec() [4/4]

Json::__declspec ( noreturn  ) const &

used internally

◆ appendHex()

static void Json::appendHex ( String result,
unsigned  ch 
)
static

◆ appendRaw()

static void Json::appendRaw ( String result,
unsigned  ch 
)
static

◆ cloneUnique()

template<typename T >
static std::unique_ptr<T> Json::cloneUnique ( const std::unique_ptr< T > &  p)
static

◆ codePointToUTF8()

static String Json::codePointToUTF8 ( unsigned int  cp)
inlinestatic

Converts a unicode code-point to UTF-8.

◆ decodePrefixedString()

static void Json::decodePrefixedString ( bool  isPrefixed,
char const *  prefixed,
unsigned *  length,
char const **  value 
)
inlinestatic

◆ doesAnyCharRequireEscaping()

static bool Json::doesAnyCharRequireEscaping ( char const *  s,
size_t  n 
)
static

◆ duplicateAndPrefixStringValue()

static char* Json::duplicateAndPrefixStringValue ( const char *  value,
unsigned int  length 
)
inlinestatic

◆ duplicateStringValue()

static char* Json::duplicateStringValue ( const char *  value,
size_t  length 
)
inlinestatic

Duplicates the specified string value.

Parameters
valuePointer to the string to duplicate. Must be zero-terminated if length is "unknown".
lengthLength of the value. if equals to unknown, then it will be computed using strlen(value).
Returns
Pointer on the duplicate instance of string.

◆ fixNumericLocale()

template<typename Iter >
Iter Json::fixNumericLocale ( Iter  begin,
Iter  end 
)

Change ',' to '.

' everywhere in buffer.

We had a sophisticated way, but it did not work in WinCE.

See also
https://github.com/open-source-parsers/jsoncpp/pull/9

◆ fixNumericLocaleInput()

template<typename Iter >
void Json::fixNumericLocaleInput ( Iter  begin,
Iter  end 
)

◆ fixZerosInTheEnd()

template<typename Iter >
Iter Json::fixZerosInTheEnd ( Iter  begin,
Iter  end,
unsigned int  precision 
)

Return iterator that would be the new end of the range [begin,end), if we were to delete zeros in the end of string, but not the last zero before '.

'.

◆ getDecimalPoint()

static char Json::getDecimalPoint ( )
inlinestatic

◆ InRange()

template<typename T , typename U >
static bool Json::InRange ( double  d,
min,
max 
)
inlinestatic

◆ IsIntegral()

static bool Json::IsIntegral ( double  d)
static

◆ operator!=()

template<typename T , typename U >
bool Json::operator!= ( const SecureAllocator< T > &  ,
const SecureAllocator< U > &   
)

◆ operator<<()

OStream & Json::operator<< ( OStream sout,
const Value root 
)

Output using the StyledStreamWriter.

See also
Json::operator>>()

◆ operator==()

template<typename T , typename U >
bool Json::operator== ( const SecureAllocator< T > &  ,
const SecureAllocator< U > &   
)

◆ operator>>()

IStream & Json::operator>> ( IStream sin,
Value root 
)

Read from 'sin' into 'root'.

Always keep comments from the input JSON.

This can be used to read a file into a particular sub-object. For example:

cin >> root["dir"]["file"];
cout << root;
Represents a JSON value.
Definition: value.h:193

Result:

* {
* "dir": {
*    "file": {
*    // The input stream JSON would be nested here.
*    }
* }
* }
* 
Exceptions
std::exceptionon parse error.
See also
Json::operator<<()

◆ parseFromStream()

bool Json::parseFromStream ( CharReader::Factory const &  fact,
IStream sin,
Value root,
String errs 
)

Consume entire stream and use its begin/end.

Someday we might have a real StreamReader, but for now this is convenient.

◆ releasePrefixedStringValue()

static void Json::releasePrefixedStringValue ( char *  value)
inlinestatic

Free the string duplicated by duplicateStringValue()/duplicateAndPrefixStringValue().

◆ releaseStringValue()

static void Json::releaseStringValue ( char *  value,
unsigned   
)
inlinestatic

◆ swap()

void Json::swap ( Value a,
Value b 
)
inline

◆ toHex16Bit()

static String Json::toHex16Bit ( unsigned int  x)
static

◆ uintToString()

static void Json::uintToString ( LargestUInt  value,
char *&  current 
)
inlinestatic

Converts an unsigned integer to string.

Parameters
valueUnsigned integer to convert to string
currentInput/Output string buffer. Must have at least uintToStringBufferSize chars free.

◆ utf8ToCodepoint()

static unsigned int Json::utf8ToCodepoint ( const char *&  s,
const char *  e 
)
static

◆ valueToQuotedString()

String Json::valueToQuotedString ( const char *  value)

◆ valueToQuotedStringN()

static String Json::valueToQuotedStringN ( const char *  value,
unsigned  length,
bool  emitUTF8 = false 
)
static

◆ valueToString() [1/6]

String Json::valueToString ( bool  value)

◆ valueToString() [2/6]

String Json::valueToString ( double  value,
unsigned int  precision = Value::defaultRealPrecision,
PrecisionType  precisionType = PrecisionType::significantDigits 
)

◆ valueToString() [3/6]

String Json::valueToString ( Int  value)

◆ valueToString() [4/6]

String Json::valueToString ( LargestInt  value)

◆ valueToString() [5/6]

String Json::valueToString ( LargestUInt  value)

◆ valueToString() [6/6]

String Json::valueToString ( UInt  value)

◆ writeString()

String Json::writeString ( StreamWriter::Factory const &  factory,
Value const &  root 
)

Write into stringstream, then return string, for convenience.

A StreamWriter will be created from the factory, used, and then deleted.

Variable Documentation

◆ hex2

const char Json::hex2[]
static
Initial value:
= "000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f"
"202122232425262728292a2b2c2d2e2f"
"303132333435363738393a3b3c3d3e3f"
"404142434445464748494a4b4c4d4e4f"
"505152535455565758595a5b5c5d5e5f"
"606162636465666768696a6b6c6d6e6f"
"707172737475767778797a7b7c7d7e7f"
"808182838485868788898a8b8c8d8e8f"
"909192939495969798999a9b9c9d9e9f"
"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"