• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MikkelSchubert / adapterremoval / #87

13 Apr 2025 07:59AM UTC coverage: 27.35% (+0.02%) from 27.335%
#87

push

travis-ci

web-flow
abort on sanitation warnings (#114)

* re-add `-fno-sanitize-recover=all`; this was lost during the transition to Meson
* remove tests that rely on undefined behavior
* fix and clean up duplicate tests

2723 of 9956 relevant lines covered (27.35%)

4049.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/src/json.hpp
1
// SPDX-License-Identifier: GPL-3.0-or-later
2
// SPDX-FileCopyrightText: 2015 Mikkel Schubert <mikkelsch@gmail.com>
3
#pragma once
4

5
#include "commontypes.hpp" // for string_vec
6
#include "counts.hpp"      // for counts, rates
7
#include <cstddef>         // for size_t
8
#include <cstdint>         // for int64_t
9
#include <functional>      // for less
10
#include <map>             // for map
11
#include <memory>          // for shared_ptr
12
#include <ostream>         // for ostream
13
#include <string>          // for string, basic_string
14
#include <vector>          // for vector
15

16
namespace adapterremoval {
17

18
class json_list;
19
class json_dict;
20
class json_value;
21

22
using json_dict_ptr = std::shared_ptr<json_dict>;
23
using json_list_ptr = std::shared_ptr<json_list>;
24
using json_ptr = std::shared_ptr<json_value>;
25

26
/** Base class for json values */
27
class json_value
28
{
29
public:
30
  json_value() = default;
97✔
31
  virtual ~json_value() = default;
97✔
32

×
33
  /** Returns the value as a valid JSON string */
97✔
34
  virtual std::string to_string() const;
35

36
  /**
37
   * Writes the object to a stream. The first line is not indented, but
38
   * subsequent lines are indented by `indent` spaces.
39
   */
40
  virtual void write(std::ostream& out, size_t indent = 0) const = 0;
41

42
  json_value(const json_value&) = delete;
43
  json_value(json_value&&) = delete;
44
  json_value& operator=(const json_value&) = delete;
45
  json_value& operator=(json_value&&) = delete;
46
};
47

48
class json_token : public json_value
49
{
50
public:
51
  /** Constructor takes a single assumed-to-be-valid JSON token/string */
52
  explicit json_token(std::string value);
53

54
  /** Create a JSON token from a string; special characters are escaped */
55
  static json_ptr from_str(std::string_view value);
56
  /** Create a single-line JSON token representing a list strings */
57
  static json_ptr from_str_vec(const string_vec& values);
58

59
  /** Create a JSON token from an integer value. */
60
  static json_ptr from_i64(int64_t value);
61
  /** Create a single-line JSON token representing a list of count values */
62
  static json_ptr from_i64_vec(const counts& values);
63

64
  /** Create a JSON token from an integer value. */
65
  static json_ptr from_u64(uint64_t value);
66

67
  /** Create a JSON token from a floating point value; formatted as '%.3f' */
68
  static json_ptr from_f64(double value);
69
  /** Create a single-line JSON token representing a list of '%.3f' values */
70
  static json_ptr from_f64_vec(const rates& values);
71

72
  /** Create a JSON token from a boolean value */
73
  static json_ptr from_boolean(bool value);
74
  /** Create a JSON token representing null */
75
  static json_ptr from_null();
76

77
  /** See json_value::to_string */
78
  std::string to_string() const override { return m_value; }
79

80
  /** See json_value::write */
34✔
81
  void write(std::ostream& out, size_t indent = 0) const override;
82

83
private:
84
  /** Create a single-line JSON list from a set of JSON encoded values */
85
  static json_ptr from_raw_vec(const string_vec& values);
86

87
  //! A valid JSON token/string representing or more objects
88
  const std::string m_value;
89
};
90

91
/** Represents a list of JSON objects written across multiple lines */
92
class json_list : public json_value
93
{
94
public:
95
  /** Creates empty list */
96
  json_list() = default;
97

98
  /** See json_value::write */
12✔
99
  void write(std::ostream& out, size_t indent = 0) const override;
100

101
  /** Appends an empty JSON dictionary to the list and returns it */
102
  json_dict_ptr dict();
103

104
  /** Appends an empty inline JSON dictionary to the list and returns it */
105
  json_dict_ptr inline_dict();
106

107
private:
108
  //! Items in the list
109
  std::vector<json_ptr> m_values{};
110
};
111

112
/**
113
 * Simple write-only JSON dictionary serializer.
114
 *
115
 * Keys are written in the order they are (first) assigned.
116
 */
117
class json_dict : public json_value
118
{
119
public:
120
  /** Creates empty dictionary */
121
  json_dict() = default;
122

123
  /** See json_value::write */
102✔
124
  void write(std::ostream& out, size_t indent = 0) const override;
125

126
  /** Add and return a sub-dict with the specified key */
127
  json_dict_ptr dict(std::string_view key);
128
  /** Add and return an inline sub-dict with the specified key */
129
  json_dict_ptr inline_dict(std::string_view key);
130
  /** Add and return a sub-list with the specified key */
131
  json_list_ptr list(std::string_view key);
132

133
  /** Set key with string value. */
134
  void str(std::string_view key, std::string_view value);
135
  /** Assign string value array. */
136
  void str_vec(std::string_view key, const string_vec& value);
137

138
  /** Assign integer value. */
139
  void i64(std::string_view key, int64_t value);
140
  /** Assign integer value array. */
141
  void i64_vec(std::string_view key, const counts& value);
142

143
  /** Assign integer value. */
144
  void u64(std::string_view key, uint64_t value);
145

146
  /** Assign floating point value. */
147
  void f64(std::string_view key, double value);
148
  /** Assign float value array. */
149
  void f64_vec(std::string_view key, const rates& value);
150

151
  /** Assign boolean value (true/false). */
152
  void boolean(std::string_view key, bool value);
153
  /** Assign null value. */
154
  void null(std::string_view key);
155

156
private:
157
  friend class json_list;
158

159
  /** Assigns a JSON object to a given key and updates the list of keys */
160
  void _set(std::string_view key, const json_ptr& ptr);
161

162
  //! List of keys in insertion order
163
  std::vector<std::pair<std::string, json_ptr>> m_items{};
164
  //! Multi-line or single (inline) dictionary
165
  bool m_multi_line = true;
166
};
167

168
} // namespace adapterremoval
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc