softwarecontainer  0.18.0-739e8d7 2017-05-04
jsonparser.cpp
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #include "jsonparser.h"
21 
22 namespace softwarecontainer {
23 
24 bool JSONParser::readOptional(const json_t *element, const char *key, std::string &result)
25 {
26  if (!hasKey(element, key)) {
27  return true;
28  }
29 
30  return read(element, key, result);
31 }
32 
33 bool JSONParser::readOptional(const json_t *element, const char *key, bool &result)
34 {
35  if (!hasKey(element, key)) {
36  return true;
37  }
38 
39  return read(element, key, result);
40 }
41 
42 bool JSONParser::readOptional(const json_t *element, const char *key, int &result)
43 {
44  if (!hasKey(element, key)) {
45  return true;
46  }
47 
48  return read(element, key, result);
49 }
50 
51 bool JSONParser::read(const json_t *element, const char *key, std::string &result)
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 }
67 
68 bool JSONParser::read(const json_t *element, const char *key, bool &result)
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 }
84 
85 bool JSONParser::read(const json_t *element, const char *key, int &result)
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 }
101 
102 bool JSONParser::hasKey(const json_t *element, const char *key)
103 {
104  return json_object_get(element, key) != NULL;
105 }
106 
107 } // namespace softwarecontainer
static bool readOptional(const json_t *element, const char *key, std::string &result)
Reads an optional value from a JSON object.
Definition: jsonparser.cpp:24
static bool hasKey(const json_t *element, const char *key)
Checks if a given JSON object contains a certain key.
Definition: jsonparser.cpp:102
Developers guide to adding a config item:
static bool read(const json_t *element, const char *key, std::string &result)
Reads a string from a JSON Object.
Definition: jsonparser.cpp:51