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

serdes_json.hpp « ugc - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c850db6cbe5e81a7601e3a3c3504a33b83d669c4 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#pragma once

#include "ugc/types.hpp"

#include "base/exception.hpp"

#include "3party/jansson/myjansson.hpp"

#include <cstdint>
#include <cstdlib>

namespace ugc
{
template <typename Sink>
class SerializerJson
{
public:
  SerializerJson(Sink & sink) : m_sink(sink) {}
  ~SerializerJson()
  {
    char * str = json_dumps(m_json.get(), 0);
    std::string json(str);
    std::free(static_cast<void *>(str));
    m_sink.Write(json.data(), json.size());
  }
  void operator()(bool const d, char const * name = nullptr) { ToJSONObject(*m_json, name, d); }
  void operator()(uint8_t const d, char const * name = nullptr) { ToJSONObject(*m_json, name, d); }
  void operator()(uint32_t const d, char const * name = nullptr) { ToJSONObject(*m_json, name, d); }
  void operator()(uint64_t const d, char const * name = nullptr) { ToJSONObject(*m_json, name, d); }
  void operator()(std::string const & s, char const * name = nullptr)
  {
    ToJSONObject(*m_json, name, s);
  }

  void operator()(TranslationKey const & key, char const * name = nullptr)
  {
    (*this)(key.m_key, name);
  }

  void operator()(Time const & t, char const * name = nullptr)
  {
    (*this)(ToDaysSinceEpoch(t), name);
  }

  void operator()(Sentiment sentiment, char const * name = nullptr)
  {
    switch (sentiment)
    {
    case Sentiment::Negative: return (*this)(false, name);
    case Sentiment::Positive: return (*this)(true, name);
    }
  }

  template <typename T>
  void operator()(vector<T> const & vs, char const * name = nullptr)
  {
    NewScopeWith(my::NewJSONArray(), name, [this, &vs] {
      for (auto const & v : vs)
        (*this)(v);
    });
  }

  template <typename R>
  void operator()(R const & r, char const * name = nullptr)
  {
    NewScopeWith(my::NewJSONObject(), name, [this, &r] { r.Visit(*this); });
  }

  void VisitRating(float const f, char const * name = nullptr)
  {
    CHECK_GREATER_OR_EQUAL(f, 0.0, ());
    auto const d = static_cast<double>(f);
    ToJSONObject(*m_json, name, d);
  }

  template <typename T>
  void VisitVarUint(T const & t, char const * name = nullptr)
  {
    ToJSONObject(*m_json, name, t);
  }

private:
  template <typename Fn>
  void NewScopeWith(my::JSONPtr json_object, char const * name, Fn && fn)
  {
    my::JSONPtr safe_json = std::move(m_json);
    m_json = std::move(json_object);

    fn();

    if (safe_json && json_is_array(safe_json))
      json_array_append_new(safe_json.get(), m_json.release());
    else if (safe_json && json_is_object(safe_json))
      json_object_set_new(safe_json.get(), name, m_json.release());

    if (safe_json)
      m_json = std::move(safe_json);
  }

  my::JSONPtr m_json = nullptr;
  Sink & m_sink;
};

class DeserializerJsonV0
{
public:
  DECLARE_EXCEPTION(Exception, RootException);

  template <typename Source,
            typename std::enable_if<
              !std::is_convertible<Source, std::string>::value, Source>::type * = nullptr>
  DeserializerJsonV0(Source & source)
  {
    std::string src(source.Size(), '\0');
    source.Read(static_cast<void *>(&src[0]), source.Size());
    m_jsonObject.ParseFrom(src);
    m_json = m_jsonObject.get();
  }

  DeserializerJsonV0(std::string const & source)
    : m_jsonObject(source)
    , m_json(m_jsonObject.get())
  {
  }

  void operator()(bool & d, char const * name = nullptr) { FromJSONObject(m_json, name, d); }
  void operator()(uint8_t & d, char const * name = nullptr) { FromJSONObject(m_json, name, d); }
  void operator()(uint32_t & d, char const * name = nullptr) { FromJSONObject(m_json, name, d); }
  void operator()(uint64_t & d, char const * name = nullptr) { FromJSONObject(m_json, name, d); }
  void operator()(std::string & s, char const * name = nullptr) { FromJSONObject(m_json, name, s); }
  void operator()(TranslationKey & key, char const * name = nullptr)
  {
    (*this)(key.m_key, name);
  }

  void operator()(Time & t, char const * name = nullptr)
  {
    uint32_t d = 0;
    FromJSONObject(m_json, name, d);
    t = FromDaysSinceEpoch(d);
  }

  void operator()(Sentiment & sentiment, char const * name = nullptr)
  {
    bool s = false;
    FromJSONObject(m_json, name, s);
    sentiment = s ? Sentiment::Positive : Sentiment::Negative;
  }

  template <typename T>
  void operator()(vector<T> & vs, char const * name = nullptr)
  {
    json_t * context = SaveContext(name);

    if (!json_is_array(m_json))
      MYTHROW(my::Json::Exception, ("The field", name, "must contain a json array."));

    vs.resize(json_array_size(m_json));
    for (size_t index = 0; index < vs.size(); ++index)
    {
      json_t * context = SaveContext();
      m_json = json_array_get(context, index);
      (*this)(vs[index]);
      RestoreContext(context);
    }

    RestoreContext(context);
  }

  template <typename R>
  void operator()(R & r, char const * name = nullptr)
  {
    json_t * context = SaveContext(name);
    r.Visit(*this);
    RestoreContext(context);
  }

  void VisitRating(float & f, char const * name = nullptr)
  {
    double d = 0.0;
    FromJSONObject(m_json, name, d);
    f = static_cast<float>(d);
  }

  template <typename T>
  void VisitVarUint(T & t, char const * name = nullptr)
  {
    FromJSONObject(m_json, name, t);
  }

private:
  json_t * SaveContext(char const * name = nullptr)
  {
    json_t * context = m_json;
    if (name)
      m_json = my::GetJSONObligatoryField(context, name);
    return context;
  }

  void RestoreContext(json_t * context)
  {
    if (context)
      m_json = context;
  }

  my::Json m_jsonObject;
  json_t * m_json = nullptr;
};
}  // namespace ugc