softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontainer::JSONParser Class Referencefinal

Convenience layer for the Jansson JSON parser. More...

#include <jsonparser.h>

Static Public Member Functions

static bool readOptional (const json_t *element, const char *key, std::string &result)
 Reads an optional value from a JSON object. More...
 
static bool readOptional (const json_t *element, const char *key, bool &result)
 
static bool readOptional (const json_t *element, const char *key, int &result)
 
static bool read (const json_t *element, const char *key, std::string &result)
 Reads a string from a JSON Object. More...
 
static bool read (const json_t *element, const char *key, bool &result)
 Reads a boolean from a JSON Object. More...
 
static bool read (const json_t *element, const char *key, int &result)
 Reads an integer from a JSON Object. More...
 
static bool hasKey (const json_t *element, const char *key)
 Checks if a given JSON object contains a certain key. More...
 

Detailed Description

Convenience layer for the Jansson JSON parser.

This class provides convenience functions that handles commonly used operations using the Janssson JSON parser.

Definition at line 32 of file jsonparser.h.

Member Function Documentation

bool softwarecontainer::JSONParser::readOptional ( const json_t *  element,
const char *  key,
std::string &  result 
)
static

Reads an optional value from a JSON object.

This differs from the read functions in that it return true instead of false if the key was not found, so the only way it returns false is if the key was of bad type or could not be parsed (but existed)

Returns
false if the key existed and was of bad type
true otherwise

Definition at line 24 of file jsonparser.cpp.

References hasKey(), and read().

25 {
26  if (!hasKey(element, key)) {
27  return true;
28  }
29 
30  return read(element, key, result);
31 }
static bool hasKey(const json_t *element, const char *key)
Checks if a given JSON object contains a certain key.
Definition: jsonparser.cpp:102
static bool read(const json_t *element, const char *key, std::string &result)
Reads a string from a JSON Object.
Definition: jsonparser.cpp:51

Here is the call graph for this function:

bool softwarecontainer::JSONParser::read ( const json_t *  element,
const char *  key,
std::string &  result 
)
static

Reads a string from a JSON Object.

Tries to read a string value form a given JSON object using a given key. If the read is successful true will be returned and the result reference will be populated with the read value. If the string could not be read, either due to the given object does not contain the given key or the given key did not hold a string, false will be returned and the result reference will not be changed.

Returns
True: If the string was successfully read.
False: If the string could not be read.

Definition at line 51 of file jsonparser.cpp.

Referenced by softwarecontainer::BaseConfigStore::BaseConfigStore(), softwarecontainer::Device::parse(), softwarecontainer::NetworkGatewayParser::parseNetworkGatewayConfiguration(), softwarecontainer::WaylandGateway::readConfigElement(), softwarecontainer::PulseGateway::readConfigElement(), and readOptional().

52 {
53  json_t *value = json_object_get(element, key);
54  if (!value) {
55  log_warning() << "Could not fetch \"" << key << "\" from json element";
56  return false;
57  }
58 
59  if (!json_is_string(value)) {
60  log_error() << "json element is not a string";
61  return false;
62  }
63 
64  result = json_string_value(value);
65  return true;
66 }

Here is the caller graph for this function:

bool softwarecontainer::JSONParser::read ( const json_t *  element,
const char *  key,
bool &  result 
)
static

Reads a boolean from a JSON Object.

Tries to read a boolean value form a given JSON object using a given key. If the read is successful true will be returned and the result reference will be populated with the read value. If the boolean could not be read, either due to the given object does not contain the given key or the given key did not hold a boolean value, false will be returned and the result reference will not be changed.

Returns
True: If the boolean was successfully read.
False: If the boolean could not be read.

Definition at line 68 of file jsonparser.cpp.

69 {
70  json_t *value = json_object_get(element, key);
71  if (!value) {
72  log_warning() << "Could not fetch \"" << key << "\" from json element";
73  return false;
74  }
75 
76  if (!json_is_boolean(value)) {
77  log_error() << "json element is not a boolean";
78  return false;
79  }
80 
81  result = json_is_true(value);
82  return true;
83 }
bool softwarecontainer::JSONParser::read ( const json_t *  element,
const char *  key,
int &  result 
)
static

Reads an integer from a JSON Object.

Tries to read an integer value form a given JSON object using a given key. If the read is successful true will be returned and the result reference will be populated with the read value. If the integer could not be read, either due to the given object does not contain the given key or the given key did not hold an integer value, false will be returned and the result reference will not be changed.

Returns
True: If the integer was successfully read.
False: If the integer could not be read.

Definition at line 85 of file jsonparser.cpp.

86 {
87  json_t *value = json_object_get(element, key);
88  if (!value) {
89  log_warning() << "Could not fetch \"" << key << "\" from json element";
90  return false;
91  }
92 
93  if (!json_is_integer(value)) {
94  log_error() << "json element is not an integer";
95  return false;
96  }
97 
98  result = json_integer_value(value);
99  return true;
100 }
bool softwarecontainer::JSONParser::hasKey ( const json_t *  element,
const char *  key 
)
static

Checks if a given JSON object contains a certain key.

Definition at line 102 of file jsonparser.cpp.

Referenced by readOptional().

103 {
104  return json_object_get(element, key) != NULL;
105 }

Here is the caller graph for this function:


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