C++ TargetRTS
Loading...
Searching...
No Matches
RTJsonResult.h
1/*
2 * Licensed Materials - Property of HCL and/or IBM
3 * Copyright HCL Technologies Ltd. 2016, 2024. All Rights Reserved.
4 * Copyright IBM Corporation 1999, 2016. All Rights Reserved.
5 *
6 * U.S. Government Users Restricted Rights - Use, duplication or
7 * disclosure restricted by GSA ADP Schedule.
8 */
9
10#ifndef __RTJsonResult_h__
11#define __RTJsonResult_h__ included
12
13#ifdef PRAGMA
14#pragma interface
15#endif
16
17#include <map>
18#include <ostream>
19#include <string>
20#include <vector>
21#include <RTConfig.h>
22
30{
31public:
35 typedef enum
36 {
50
51 explicit RTJsonResult();
53
59 const RTJsonResult& operator[]( const std::string& key ) const;
60
68 const RTJsonResult& operator[]( size_t index ) const;
69
74 RTS_INLINE size_t get_size() const;
75
77 RTS_INLINE std::map<std::string, RTJsonResult>::const_iterator keys_begin() const;
78
80 RTS_INLINE std::map<std::string, RTJsonResult>::const_iterator keys_end() const;
81
84
86 RTJsonResult( const RTJsonResult & other );
87
89 RTS_INLINE bool ok() const;
90
93 const std::string& get_string() const;
94
97
99 bool get_bool() const;
100
104 int get_int() const;
105
109 float get_float() const;
110
114 double get_double() const;
115
117 bool operator==(const RTJsonResult&) const;
118
120 bool operator==(const bool) const;
121
123 bool operator==(const int) const;
124
126 bool operator==(const float) const;
127
129 bool operator==(const double) const;
130
132 bool operator==(const char*) const;
133
135 RTJsonResult& merge( const RTJsonResult & other );
136
138 void unparse(std::ostream& out, int indent = 0) const;
139
140private:
141 friend class RTJsonParser;
142 friend class RTJsonParserUtils;
143
145 std::string value;
146 std::vector <RTJsonResult> arrayValues;
147 std::map <std::string, RTJsonResult> keysValues;
148
149 static const RTJsonResult jsonError;
150
151 RTS_INLINE void put_string( const std::string & val );
152 RTS_INLINE void put_number( const std::string & val );
153 RTS_INLINE void put_bool( bool );
154 RTS_INLINE void put_object();
155 RTS_INLINE void put_array();
156 RTS_INLINE void put_null();
157 RTS_INLINE void clear();
158};
159
160#if RTS_INLINES
161#include <RTJsonResult.inl>
162#endif
163
164#endif // __RTJsonResult_h__
Utility for parsing JSON.
Definition: RTJsonParser.h:23
Definition: RTJsonParserUtils.h:47
Represents the result of parsing a JSON string.
Definition: RTJsonResult.h:30
void unparse(std::ostream &out, int indent=0) const
Pretty print JSON object to output stream.
RTJsonResult & operator=(const RTJsonResult &other)
Assignment operator.
bool operator==(const int) const
Determines if the JSON value is numeric and equal to the given int.
std::map< std::string, RTJsonResult >::const_iterator keys_begin() const
For a JSON object, return its keys begin iterator.
Definition: RTJsonResult.inl:21
RTJsonResult(const RTJsonResult &other)
Copy constructor.
double get_double() const
Interpret the JSON value as a double.
bool operator==(const char *) const
Determines if the JSON value is a string and equal to the given string.
const std::string & get_string() const
Get a string representation of the JSON value.
bool operator==(const RTJsonResult &) const
Determines if the JSON value is equal (but not necessarily the same) as another JSON value.
bool get_bool() const
Interpret the JSON value as a bool.
float get_float() const
Interpret the JSON value as a float.
RTJsonType get_type() const
Get the type of JSON value represented by the RTJsonResult.
bool ok() const
Returns false if this object represents an error result, true otherwise.
Definition: RTJsonResult.inl:31
std::map< std::string, RTJsonResult >::const_iterator keys_end() const
For a JSON object, return its keys end iterator.
Definition: RTJsonResult.inl:26
bool operator==(const double) const
Determines if the JSON value is numeric and equal to the given double.
RTJsonType
The type of JSON value represented by the RTJsonResult object.
Definition: RTJsonResult.h:36
@ RTJSON_NUM
JSON numeric value.
Definition: RTJsonResult.h:46
@ RTJSON_BOOL
JSON boolean value.
Definition: RTJsonResult.h:48
@ RTJSON_ARR
JSON array value.
Definition: RTJsonResult.h:42
@ RTJSON_STR
JSON string value.
Definition: RTJsonResult.h:44
@ RTJSON_NULL
The special null value.
Definition: RTJsonResult.h:38
@ RTJSON_OBJ
JSON object value.
Definition: RTJsonResult.h:40
int get_int() const
Interpret the JSON value as an int.
const RTJsonResult & operator[](size_t index) const
For a JSON array, return the value at the given index.
bool operator==(const bool) const
Determines if the JSON value is boolean and equal to the given bool.
bool operator==(const float) const
Determines if the JSON value is numeric and equal to the given float.
RTJsonResult & merge(const RTJsonResult &other)
Merge properties from other to this object, override existing properties.
const RTJsonResult & operator[](const std::string &key) const
For a JSON object, return the value for the key with the given name.
size_t get_size() const
For a JSON array, return the number of array elements.
Definition: RTJsonResult.inl:36