aboutsummaryrefslogtreecommitdiff
path: root/util/json/json_helpers.h
blob: ebd25add4677221a6751a8c804cb6703ed2b125c (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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UTIL_JSON_JSON_HELPERS_H_
#define UTIL_JSON_JSON_HELPERS_H_

#include <chrono>
#include <cmath>
#include <functional>
#include <string>
#include <utility>
#include <vector>

#include "absl/strings/string_view.h"
#include "json/value.h"
#include "platform/base/error.h"
#include "util/chrono_helpers.h"
#include "util/json/json_serialization.h"
#include "util/simple_fraction.h"

// This file contains helper methods for parsing JSON, in an attempt to
// reduce boilerplate code when working with JsonCpp.
namespace openscreen {
namespace json {

inline bool TryParseBool(const Json::Value& value, bool* out) {
  if (!value.isBool()) {
    return false;
  }
  *out = value.asBool();
  return true;
}

// A general note about parsing primitives. "Validation" in this context
// generally means ensuring that the values are non-negative, excepting doubles
// which may be negative in some cases.
inline bool TryParseDouble(const Json::Value& value,
                           double* out,
                           bool allow_negative = false) {
  if (!value.isDouble()) {
    return false;
  }
  const double d = value.asDouble();
  if (std::isnan(d)) {
    return false;
  }
  if (!allow_negative && d < 0) {
    return false;
  }
  *out = d;
  return true;
}

inline bool TryParseInt(const Json::Value& value, int* out) {
  if (!value.isInt()) {
    return false;
  }
  int i = value.asInt();
  if (i < 0) {
    return false;
  }
  *out = i;
  return true;
}

inline bool TryParseUint(const Json::Value& value, uint32_t* out) {
  if (!value.isUInt()) {
    return false;
  }
  *out = value.asUInt();
  return true;
}

inline bool TryParseString(const Json::Value& value, std::string* out) {
  if (!value.isString()) {
    return false;
  }
  *out = value.asString();
  return true;
}

// We want to be more robust when we parse fractions then just
// allowing strings, this will parse numeral values such as
// value: 50 as well as value: "50" and value: "100/2".
inline bool TryParseSimpleFraction(const Json::Value& value,
                                   SimpleFraction* out) {
  if (value.isInt()) {
    int parsed = value.asInt();
    if (parsed < 0) {
      return false;
    }
    *out = SimpleFraction{parsed, 1};
    return true;
  }

  if (value.isString()) {
    auto fraction_or_error = SimpleFraction::FromString(value.asString());
    if (!fraction_or_error) {
      return false;
    }

    if (!fraction_or_error.value().is_positive() ||
        !fraction_or_error.value().is_defined()) {
      return false;
    }
    *out = std::move(fraction_or_error.value());
    return true;
  }
  return false;
}

inline bool TryParseMilliseconds(const Json::Value& value, milliseconds* out) {
  int out_ms;
  if (!TryParseInt(value, &out_ms) || out_ms < 0) {
    return false;
  }
  *out = milliseconds(out_ms);
  return true;
}

template <typename T>
using Parser = std::function<bool(const Json::Value&, T*)>;

// NOTE: array parsing methods reset the output vector to an empty vector in
// any error case. This is especially useful for optional arrays.
template <typename T>
bool TryParseArray(const Json::Value& value,
                   Parser<T> parser,
                   std::vector<T>* out) {
  out->clear();
  if (!value.isArray() || value.empty()) {
    return false;
  }

  out->reserve(value.size());
  for (Json::ArrayIndex i = 0; i < value.size(); ++i) {
    T v;
    if (!parser(value[i], &v)) {
      out->clear();
      return false;
    }
    out->push_back(v);
  }

  return true;
}

inline bool TryParseIntArray(const Json::Value& value, std::vector<int>* out) {
  return TryParseArray<int>(value, TryParseInt, out);
}

inline bool TryParseUintArray(const Json::Value& value,
                              std::vector<uint32_t>* out) {
  return TryParseArray<uint32_t>(value, TryParseUint, out);
}

inline bool TryParseStringArray(const Json::Value& value,
                                std::vector<std::string>* out) {
  return TryParseArray<std::string>(value, TryParseString, out);
}

}  // namespace json
}  // namespace openscreen

#endif  // UTIL_JSON_JSON_HELPERS_H_