Coverage Report

Created: 2025-07-18 06:30

/src/spotify-json/include/spotify/json/decode.hpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2015-2016 Spotify AB
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
 * use this file except in compliance with the License. You may obtain a copy of
6
 * the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations under
14
 * the License.
15
 */
16
17
#pragma once
18
19
#include <cstring>
20
21
#include <spotify/json/decode_context.hpp>
22
#include <spotify/json/default_codec.hpp>
23
#include <spotify/json/detail/decode_helpers.hpp>
24
#include <spotify/json/detail/macros.hpp>
25
26
namespace spotify {
27
namespace json {
28
29
/*
30
 * json::decode(codec, data...)
31
 */
32
33
template <typename codec_type>
34
2.40k
typename codec_type::object_type decode(const codec_type &codec, const char *data, size_t size) {
35
2.40k
  decode_context c(data, data + size);
36
2.40k
  detail::skip_any_whitespace(c);
37
2.40k
  const auto result = codec.decode(c);
38
2.40k
  detail::skip_any_whitespace(c);
39
2.40k
  detail::fail_if(c, c.position != c.end, "Unexpected trailing input");
40
2.40k
  return result;
41
2.40k
}
42
43
template <typename codec_type>
44
typename codec_type::object_type decode(const codec_type &codec, const char *cstr) {
45
  return decode(codec, cstr, cstr ? std::strlen(cstr) : 0);
46
}
47
48
template <typename codec_type, typename string_type>
49
typename codec_type::object_type decode(const codec_type &codec, const string_type &string) {
50
  return decode(codec, string.data(), string.size());
51
}
52
53
/*
54
 * json::decode(data...)
55
 */
56
57
template <typename value_type>
58
value_type decode(const char *data, size_t size) {
59
  return decode(default_codec<value_type>(), data, size);
60
}
61
62
template <typename value_type>
63
value_type decode(const char *cstr) {
64
  return decode(default_codec<value_type>(), cstr);
65
}
66
67
template <typename value_type, typename string_type>
68
value_type decode(const string_type &string) {
69
  return decode(default_codec<value_type>(), string);
70
}
71
72
/*
73
 * json::try_decode(&object, codec, data...)
74
 */
75
76
template <typename codec_type>
77
bool try_decode(
78
    typename codec_type::object_type &object,
79
    const codec_type &codec,
80
    const char *data,
81
2.40k
    size_t size) noexcept {
82
2.40k
  if (size == 0) {
83
0
    return false;  // avoid exceptions below
84
0
  }
85
2.40k
  try {
86
2.40k
    object = decode(codec, data, size);
87
2.40k
    return true;
88
2.40k
  } catch (...) {
89
2.40k
    return false;
90
2.40k
  }
91
2.40k
}
92
93
template <typename codec_type>
94
bool try_decode(
95
    typename codec_type::object_type &object,
96
    const codec_type &codec,
97
    const char *cstr) noexcept {
98
  return try_decode(object, codec, cstr, cstr ? std::strlen(cstr) : 0);
99
}
100
101
template <typename codec_type, typename string_type>
102
bool try_decode(
103
    typename codec_type::object_type &object,
104
    const codec_type &codec,
105
2.40k
    const string_type &string) noexcept {
106
2.40k
  return try_decode(object, codec, string.data(), string.size());
107
2.40k
}
108
109
/*
110
 * json::try_decode(&object, data...)
111
 */
112
113
template <typename value_type>
114
bool try_decode(value_type &object, const char *data, size_t size) noexcept {
115
  return try_decode(object, default_codec<value_type>(), data, size);
116
}
117
118
template <typename value_type>
119
bool try_decode(value_type &object, const char *cstr) noexcept {
120
  return try_decode(object, default_codec<value_type>(), cstr);
121
}
122
123
template <typename value_type, typename string_type>
124
2.40k
bool try_decode(value_type &object, const string_type &string) noexcept {
125
2.40k
  return try_decode(object, default_codec<value_type>(), string);
126
2.40k
}
127
128
}  // namespace json
129
}  // namespace spotify