Welcome to mirror list, hosted at ThFree Co, Russian Federation.

myjansson.hpp « jansson « 3party - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 610ef8e870b40938ff2880c2ef1d381fb76a77e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#pragma once

#include "jansson_handle.hpp"

#include "base/exception.hpp"
#include "base/string_utils.hpp"

#include <cstdint>
#include <memory>
#include <string>
#include <vector>

#include "3party/jansson/src/jansson.h"

namespace my
{
struct JSONDecRef
{
  void operator()(json_t * root) const { json_decref(root); }
};

using JSONPtr = std::unique_ptr<json_t, JSONDecRef>;

inline JSONPtr NewJSONObject() { return JSONPtr(json_object()); }
inline JSONPtr NewJSONArray() { return JSONPtr(json_array()); }
inline JSONPtr NewJSONString(std::string const & s) { return JSONPtr(json_string(s.c_str())); }
inline JSONPtr NewJSONInt(json_int_t value) { return JSONPtr(json_integer(value)); }
inline JSONPtr NewJSONReal(double value) { return JSONPtr(json_real(value)); }
inline JSONPtr NewJSONBool(bool value) { return JSONPtr(value ? json_true() : json_false()); }
inline JSONPtr NewJSONNull() { return JSONPtr(json_null()); }

class Json
{
  JsonHandle m_handle;

public:
  DECLARE_EXCEPTION(Exception, RootException);

  Json() = default;
  explicit Json(std::string const & s) { ParseFrom(s); }
  explicit Json(char const * s) { ParseFrom(s); }

  void ParseFrom(std::string const & s) { ParseFrom(s.c_str()); }
  void ParseFrom(char const * s)
  {
    json_error_t jsonError;
    m_handle.AttachNew(json_loads(s, 0, &jsonError));
    if (!m_handle)
      MYTHROW(Exception, (jsonError.line, jsonError.text));
  }

  json_t * get() const { return m_handle.get(); }
};

json_t * GetJSONObligatoryField(json_t * root, std::string const & field);
json_t * GetJSONOptionalField(json_t * root, std::string const & field);
bool JSONIsNull(json_t * root);
}  // namespace my

inline void FromJSON(json_t * root, json_t *& value) { value = root; }

void FromJSON(json_t * root, double & result);
void FromJSON(json_t * root, bool & result);

template <typename T,
          typename std::enable_if<std::is_integral<T>::value, void>::type* = nullptr>
void FromJSON(json_t * root, T & result)
{
  if (!json_is_number(root))
    MYTHROW(my::Json::Exception, ("Object must contain a json number."));
  result = static_cast<T>(json_integer_value(root));
}

template <typename T>
void FromJSONObject(json_t * root, std::string const & field, T & result)
{
  auto * json = my::GetJSONObligatoryField(root, field);
  try
  {
    FromJSON(json, result);
  }
  catch (my::Json::Exception const & e)
  {
    MYTHROW(my::Json::Exception, ("An error occured while parsing field", field, e.Msg()));
  }
}

template <typename T>
void FromJSONObjectOptionalField(json_t * root, std::string const & field, T & result)
{
  auto * json = my::GetJSONOptionalField(root, field);
  if (!json)
  {
    result = T{};
    return;
  }
  FromJSON(json, result);
}

template <typename T,
          typename std::enable_if<std::is_integral<T>::value, void>::type* = nullptr>
inline my::JSONPtr ToJSON(T value) { return my::NewJSONInt(value); }
inline my::JSONPtr ToJSON(double value) { return my::NewJSONReal(value); }
inline my::JSONPtr ToJSON(bool value) { return my::NewJSONBool(value); }

template <typename T>
void ToJSONObject(json_t & root, std::string const & field, T const & value)
{
  json_object_set_new(&root, field.c_str(), ToJSON(value).release());
}

template <typename T>
void FromJSONObject(json_t * root, std::string const & field, std::vector<T> & result)
{
  auto * arr = my::GetJSONObligatoryField(root, field);
  if (!json_is_array(arr))
    MYTHROW(my::Json::Exception, ("The field", field, "must contain a json array."));
  size_t sz = json_array_size(arr);
  result.resize(sz);
  for (size_t i = 0; i < sz; ++i)
    FromJSON(json_array_get(arr, i), result[i]);
}

// The function tries to parse array of values from a value
// corresponding to |field| in a json object corresponding to |root|.
// Returns true when the value is non-null and array is successfully
// parsed.  Returns false when there is no such |field| in the |root|
// or the value is null.  Also, the method may throw an exception in
// case of json parsing errors.
template <typename T>
bool FromJSONObjectOptional(json_t * root, std::string const & field, std::vector<T> & result)
{
  auto * arr = my::GetJSONOptionalField(root, field);
  if (!arr || my::JSONIsNull(arr))
  {
    result.clear();
    return false;
  }
  if (!json_is_array(arr))
    MYTHROW(my::Json::Exception, ("The field", field, "must contain a json array."));
  size_t const sz = json_array_size(arr);
  result.resize(sz);
  for (size_t i = 0; i < sz; ++i)
    FromJSON(json_array_get(arr, i), result[i]);
  return true;
}

template <typename T>
void ToJSONObject(json_t & root, std::string const & field, std::vector<T> const & values)
{
  auto arr = my::NewJSONArray();
  for (auto const & value : values)
    json_array_append_new(arr.get(), ToJSON(value).release());
  json_object_set_new(&root, field.c_str(), arr.release());
}

template <typename T>
void FromJSONObjectOptionalField(json_t * root, std::string const & field, std::vector<T> & result)
{
  json_t * arr = my::GetJSONOptionalField(root, field);
  if (!arr)
  {
    result.clear();
    return;
  }
  if (!json_is_array(arr))
    MYTHROW(my::Json::Exception, ("The field", field, "must contain a json array."));
  size_t sz = json_array_size(arr);
  result.resize(sz);
  for (size_t i = 0; i < sz; ++i)
    FromJSON(json_array_get(arr, i), result[i]);
}

bool CheckJsonArray(json_t const * data);

struct JSONFreeDeleter
{
  void operator()(char * buffer) const { free(buffer); }
};

namespace std
{
void FromJSON(json_t * root, std::string & result);
inline my::JSONPtr ToJSON(std::string const & s) { return my::NewJSONString(s); }
}  // namespace std

namespace strings
{
void FromJSON(json_t * root, UniString & result);
my::JSONPtr ToJSON(UniString const & s);
}  // namespace strings