Coverage Report

Created: 2025-06-13 06:41

/src/json_proto_converter.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2020 Google Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
17
#include "json_proto_converter.h"
18
19
namespace json_proto {
20
21
1.22k
void JsonProtoConverter::AppendArray(const ArrayValue& array_value) {
22
1.22k
  data_ << '[';
23
1.22k
  bool need_comma = false;
24
7.47k
  for (const auto& value : array_value.value()) {
25
    // Trailing comma inside of an array makes JSON invalid, avoid adding that.
26
7.47k
    if (need_comma)
27
6.64k
      data_ << ',';
28
833
    else
29
833
      need_comma = true;
30
31
7.47k
    AppendValue(value);
32
7.47k
  }
33
1.22k
  data_ << ']';
34
1.22k
}
35
36
1.57k
void JsonProtoConverter::AppendNumber(const NumberValue& number_value) {
37
1.57k
  if (number_value.has_float_value()) {
38
412
    data_ << number_value.float_value().value();
39
1.16k
  } else if (number_value.has_exponent_value()) {
40
384
    auto value = number_value.exponent_value();
41
384
    data_ << value.base();
42
384
    data_ << (value.use_uppercase() ? 'E' : 'e');
43
384
    data_ << value.exponent();
44
782
  } else if (number_value.has_exponent_frac_value()) {
45
228
    auto value = number_value.exponent_value();
46
228
    data_ << value.base();
47
228
    data_ << (value.use_uppercase() ? 'E' : 'e');
48
228
    data_ << value.exponent();
49
554
  } else {
50
554
    data_ << number_value.integer_value().value();
51
554
  }
52
1.57k
}
53
54
4.12k
void JsonProtoConverter::AppendObject(const JsonObject& json_object) {
55
4.12k
  data_ << '{' << '"' << json_object.name() << '"' << ':';
56
4.12k
  AppendValue(json_object.value());
57
4.12k
  data_ << '}';
58
4.12k
}
59
60
11.6k
void JsonProtoConverter::AppendValue(const JsonValue& json_value) {
61
11.6k
  if (json_value.has_object_value()) {
62
1.11k
    AppendObject(json_value.object_value());
63
10.4k
  } else if (json_value.has_array_value()) {
64
1.22k
    AppendArray(json_value.array_value());
65
9.26k
  } else if (json_value.has_number_value()) {
66
1.57k
    AppendNumber(json_value.number_value());
67
7.68k
  } else if (json_value.has_string_value()) {
68
643
    data_ << '"' << json_value.string_value().value() << '"';
69
7.04k
  } else if (json_value.has_boolean_value()) {
70
404
    data_ << (json_value.boolean_value().value() ? "true" : "false");
71
6.64k
  } else {
72
6.64k
    data_ << "null";
73
6.64k
  }
74
11.6k
}
75
76
3.01k
std::string JsonProtoConverter::Convert(const JsonObject& json_object) {
77
3.01k
  AppendObject(json_object);
78
3.01k
  return data_.str();
79
3.01k
}
80
81
std::string JsonProtoConverter::Convert(
82
0
    const json_proto::ArrayValue& json_array) {
83
0
  AppendArray(json_array);
84
0
  return data_.str();
85
0
}
86
87
}  // namespace json_proto