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: a95e2b875f7aa1f7c0a2f68761860d30779eff5d (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
#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())); }

class Json
{
  JsonHandle m_handle;

public:
  DECLARE_EXCEPTION(Exception, RootException);

  explicit Json(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 FromJSONObject(json_t * root, std::string const & field, double & result);
void FromJSONObject(json_t * root, std::string const & field, int & result);
void FromJSONObject(json_t * root, std::string const & field, int64_t & result);

void FromJSONObjectOptionalField(json_t * root, std::string const & field, int & result);
void FromJSONObjectOptionalField(json_t * root, std::string const & field, int64_t & result);
void FromJSONObjectOptionalField(json_t * root, std::string const & field, double & result);
void FromJSONObjectOptionalField(json_t * root, std::string const & field, bool & result,
                                 bool def = false);
void FromJSONObjectOptionalField(json_t * root, std::string const & field, json_t *& result);

void ToJSONObject(json_t & root, std::string const & field, double value);
void ToJSONObject(json_t & root, std::string const & field, int value);
void ToJSONObject(json_t & root, std::string const & field, int64_t value);

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

void FromJSONObject(json_t * root, std::string const & field, std::string & result);
void ToJSONObject(json_t & root, std::string const & field, std::string const & value);

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());
}

void FromJSONObjectOptionalField(json_t * root, std::string const & field, std::string & result);

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]);
}

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

namespace strings
{
void FromJSONObject(json_t * root, std::string const & field, UniString & result);
void ToJSONObject(json_t & root, std::string const & field, UniString const & value);
}