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

myjansson.cpp « jansson « 3party - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ed59a2c7a004fcad8bb0e6e095ac229c2fd68c5e (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
#include "3party/jansson/myjansson.hpp"

#include <type_traits>

using namespace std;

namespace my
{
json_t * GetJSONObligatoryField(json_t * root, std::string const & field)
{
  auto * value = my::GetJSONOptionalField(root, field);
  if (!value)
    MYTHROW(my::Json::Exception, ("Obligatory field", field, "is absent."));
  return value;
}

json_t * GetJSONOptionalField(json_t * root, std::string const & field)
{
  if (!json_is_object(root))
    MYTHROW(my::Json::Exception, ("Bad json object while parsing", field));
  return json_object_get(root, field.c_str());
}

bool JSONIsNull(json_t * root) { return json_is_null(root); }
}  // namespace my

void FromJSON(json_t * root, double & result)
{
  if (!json_is_number(root))
    MYTHROW(my::Json::Exception, ("Object must contain a json number."));
  result = json_number_value(root);
}

void FromJSON(json_t * root, bool & result)
{
  if (!json_is_true(root) && !json_is_false(root) )
    MYTHROW(my::Json::Exception, ("Object must contain a boolean value."));
  result = json_is_true(root);
}

namespace std
{
void FromJSON(json_t * root, string & result)
{
  if (!json_is_string(root))
    MYTHROW(my::Json::Exception, ("The field must contain a json string."));
  result = json_string_value(root);
}
}  // namespace std

namespace strings
{
void FromJSON(json_t * root, UniString & result)
{
  string s;
  FromJSON(root, s);
  result = MakeUniString(s);
}

my::JSONPtr ToJSON(UniString const & s) { return ToJSON(ToUtf8(s)); }
}  // namespace strings