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
41 // ------------------------------------------------------------------------
45 // ----------- construction/destruction housekeeping things -------------------
47 Accept a string that contains valid json. Causes it to be parsed
48 after which the functions provided by the class can be used to
51 xapp::Jhash::Jhash( const char* jbuf ) :
60 Jhash::Jhash( Jhash&& soi ) {
61 master_st = soi.master_st;
64 soi.st = NULL; // prevent closing of RMR stuff on soi destroy
71 Jhash& Jhash::operator=( Jhash&& soi ) {
72 if( this != &soi ) { // cannot do self assignment
73 master_st = soi.master_st;
76 soi.st = NULL; // prevent closing of RMR stuff on soi destroy
86 xapp::Jhash::~Jhash() {
87 if( master_st != NULL ) { // revert blob set if needed
97 // ------------ public API ---------------------------------------------------
99 // IMPORTANT: all underlying jwrapper functions check for nil st and name pointers
100 // so that is NOT needed in this code.
103 // --- root control ----------------------------------------------------------
105 Sets the "root" to the named blob. Until unset, all subsequent
106 calls to Jhash functions (e.g. Is_missing()) will use this for the
107 look up. Returns true if name exists and was indeed a blob.
109 It is legit to call set multiple times without unsetting; set
110 overlays with the named root; unset needs only to be called to
111 return to the top level.
113 bool xapp::Jhash::Set_blob( const char* name ) {
114 void* bst; // blob symbol table
116 if( master_st == NULL ) { // must capture master
120 if( (bst = jw_blob( st, name )) != NULL ) {
129 Return the suss root (blob root) to the root of the symtab.
131 void xapp::Jhash::Unset_blob( ) {
132 if( master_st != NULL ) {
137 // ---------------- debugging and sanity checks ---------------------------------------
139 Returns true if there were parse errors which resulted in an empty
140 or non-existant hash.
142 Right now we don't have much to work with other than checking for a
145 bool xapp::Jhash::Parse_errors( ) {
150 Dump the selected blob as much as we can.
152 void xapp::Jhash::Dump() {
156 // ---------------- type testing -----------------------------------------
159 These funcitons return true if the named object in the current blob
160 is the indicated type
162 bool xapp::Jhash::Is_value( const char* name ) {
163 return jw_is_value( st, name ) == 1;
166 bool xapp::Jhash::Is_bool( const char* name ) {
167 return jw_is_bool( st, name ) == 1;
170 bool xapp::Jhash::Is_null( const char* name ) {
171 return jw_is_null( st, name ) == 1;
174 bool xapp::Jhash::Is_string( const char* name ) {
175 return jw_is_string( st, name ) == 1;
179 These functions return true if the indicated element in the array
180 <name> is the indicated type.
182 bool xapp::Jhash::Is_string_ele( const char* name, int eidx ) {
183 return jw_is_string_ele( st, name, eidx ) == 1;
186 bool xapp::Jhash::Is_value_ele( const char* name, int eidx ) {
187 return jw_is_value_ele( st, name, eidx ) == 1;
190 bool xapp::Jhash::Is_bool_ele( const char* name, int eidx ) {
191 return jw_is_bool_ele( st, name, eidx ) == 1;
194 bool xapp::Jhash::Is_null_ele( const char* name, int eidx ) {
195 return jw_is_null_ele( st, name, eidx ) == 1;
199 // ---------------- presence ------------------------------------------------
201 Returns true if the named element is in the hash.
203 bool xapp::Jhash::Exists( const char* name ) {
204 return jw_exists( st, name ) == 1;
208 Returns true if the named element is not in the hash.
210 bool xapp::Jhash::Is_missing( const char* name ) {
211 return jw_missing( st, name ) == 1;
214 // ---------------- value sussing ----------------------------------------
217 Returns the boolean value for the object if it is a bool; false otherwise.
219 Symtab saves bool values as 1 for true and doesn't provide a bool fetch
220 function. So, fetch the value and return true if it is 1.
222 bool xapp::Jhash::Bool( const char* name ) {
224 v = (int) jw_value( st, name );
230 Returns a C++ string to the named object; If the element is not
231 in the hash an empty string is returned.
233 std::string xapp::Jhash::String( const char* name ) {
237 if( (hashv = jw_string( st, name )) != NULL ) {
238 rv = std::string( hashv );
246 Returns the value assocated with the named object; If the element is not
247 in the hash 0 is returned.
249 double xapp::Jhash::Value( const char* name ) {
250 return jw_value( st, name );
253 // ------ array related things --------------------------------------------
256 Return the length of the named array, or -1 if it doesn't exist.
258 int xapp::Jhash::Array_len( const char* name ) {
259 return jw_array_len( st, name );
264 Sets the blob in the array <name>[eidx] to the current reference blob.
266 bool xapp::Jhash::Set_blob_ele( const char* name, int eidx ) {
269 if( (bst = jw_obj_ele( st, name, eidx )) != NULL ) {
270 if( master_st == NULL ) { // must capture master
281 Return the string at index eidx in the array <name>.
283 std::string xapp::Jhash::String_ele( const char* name, int eidx ) {
287 if( (hashv = jw_string_ele( st, name, eidx )) != NULL ) {
288 rv = std::string( hashv );
295 Return the value at index eidx in the array <name>.
297 double xapp::Jhash::Value_ele( const char* name, int eidx ) {
298 return jw_value_ele( st, name, eidx );
302 Return the bool value at index eidx in the array <name>.
304 bool xapp::Jhash::Bool_ele( const char* name, int eidx ) {
305 return jw_bool_ele( st, name, eidx ) == 1;