3 ==================================================================================
4 Copyright (c) 2020 Nokia
5 Copyright (c) 2020 AT&T Intellectual Property.
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 ==================================================================================
23 Abstract: This class provides the ability to parse a json string into
24 a hashtable, and exposes various functions that can be used
25 to read the data from the hash.
28 Author: E. Scott Daniels
37 // ------------------------------------------------------------------------
41 // ----------- construction/destruction housekeeping things -------------------
43 Accept a string that contains valid json. Causes it to be parsed
44 after which the functions provided by the class can be used to
47 Jhash::Jhash( const char* jbuf ) :
56 Jhash::Jhash( Jhash&& soi ) {
57 master_st = soi.master_st;
60 soi.st = NULL; // prevent closing of RMR stuff on soi destroy
67 Jhash& Jhash::operator=( Jhash&& soi ) {
68 if( this != &soi ) { // cannot do self assignment
69 master_st = soi.master_st;
72 soi.st = NULL; // prevent closing of RMR stuff on soi destroy
83 if( master_st != NULL ) { // revert blob set if needed
93 // ------------ public API ---------------------------------------------------
95 // IMPORTANT: all underlying jwrapper functions check for nil st and name pointers
96 // so that is NOT needed in this code.
99 // --- root control ----------------------------------------------------------
101 Sets the "root" to the named blob. Until unset, all subsequent
102 calls to Jhash functions (e.g. Is_missing()) will use this for the
103 look up. Returns true if name exists and was indeed a blob.
105 It is legit to call set multiple times without unsetting; set
106 overlays with the named root; unset needs only to be called to
107 return to the top level.
109 bool Jhash::Set_blob( const char* name ) {
110 void* bst; // blob symbol table
112 if( master_st == NULL ) { // must capture master
116 if( (bst = jw_blob( st, name )) != NULL ) {
125 Return the suss root (blob root) to the root of the symtab.
127 void Jhash::Unset_blob( ) {
128 if( master_st != NULL ) {
133 // ---------------- debugging and sanity checks ---------------------------------------
135 Returns true if there were parse errors which resulted in an empty
136 or non-existant hash.
138 Right now we don't have much to work with other than checking for a
141 bool Jhash::Parse_errors( ) {
146 Dump the selected blob as much as we can.
152 // ---------------- type testing -----------------------------------------
155 These funcitons return true if the named object in the current blob
156 is the indicated type
158 bool Jhash::Is_value( const char* name ) {
159 return jw_is_value( st, name ) == 1;
162 bool Jhash::Is_bool( const char* name ) {
163 return jw_is_bool( st, name ) == 1;
166 bool Jhash::Is_null( const char* name ) {
167 return jw_is_null( st, name ) == 1;
170 bool Jhash::Is_string( const char* name ) {
171 return jw_is_string( st, name ) == 1;
175 These functions return true if the indicated element in the array
176 <name> is the indicated type.
178 bool Jhash::Is_string_ele( const char* name, int eidx ) {
179 return jw_is_string_ele( st, name, eidx ) == 1;
182 bool Jhash::Is_value_ele( const char* name, int eidx ) {
183 return jw_is_value_ele( st, name, eidx ) == 1;
186 bool Jhash::Is_bool_ele( const char* name, int eidx ) {
187 return jw_is_bool_ele( st, name, eidx ) == 1;
190 bool Jhash::Is_null_ele( const char* name, int eidx ) {
191 return jw_is_null_ele( st, name, eidx ) == 1;
195 // ---------------- presence ------------------------------------------------
197 Returns true if the named element is in the hash.
199 bool Jhash::Exists( const char* name ) {
200 return jw_exists( st, name ) == 1;
204 Returns true if the named element is not in the hash.
206 bool Jhash::Is_missing( const char* name ) {
207 return jw_missing( st, name ) == 1;
210 // ---------------- value sussing ----------------------------------------
213 Returns the boolean value for the object if it is a bool; false otherwise.
215 Symtab saves bool values as 1 for true and doesn't provide a bool fetch
216 function. So, fetch the value and return true if it is 1.
218 bool Jhash::Bool( const char* name ) {
220 v = (int) jw_value( st, name );
225 Returns a C++ string to the named object; If the element is not
226 in the hash an empty string is returned.
228 std::string Jhash::String( const char* name ) {
232 if( (hashv = jw_string( st, name )) != NULL ) {
233 rv = std::string( hashv );
241 Returns the value assocated with the named object; If the element is not
242 in the hash 0 is returned.
244 double Jhash::Value( const char* name ) {
245 return jw_value( st, name );
248 // ------ array related things --------------------------------------------
251 Return the length of the named array, or -1 if it doesn't exist.
253 int Jhash::Array_len( const char* name ) {
254 return jw_array_len( st, name );
259 Sets the blob in the array <name>[eidx] to the current reference blob.
261 bool Jhash::Set_blob_ele( const char* name, int eidx ) {
264 if( (bst = jw_obj_ele( st, name, eidx )) != NULL ) {
265 if( master_st == NULL ) { // must capture master
276 Return the string at index eidx in the array <name>.
278 std::string Jhash::String_ele( const char* name, int eidx ) {
282 if( (hashv = jw_string_ele( st, name, eidx )) != NULL ) {
283 rv = std::string( hashv );
290 Return the value at index eidx in the array <name>.
292 double Jhash::Value_ele( const char* name, int eidx ) {
293 return jw_value_ele( st, name, eidx );
297 Return the bool value at index eidx in the array <name>.
299 bool Jhash::Bool_ele( const char* name, int eidx ) {
300 return jw_bool_ele( st, name, eidx ) == 1;