Coverage Report

Created: 2025-06-24 06:12

/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.30k
void JsonProtoConverter::AppendArray(const ArrayValue& array_value) {
22
1.30k
  data_ << '[';
23
1.30k
  bool need_comma = false;
24
6.03k
  for (const auto& value : array_value.value()) {
25
    // Trailing comma inside of an array makes JSON invalid, avoid adding that.
26
6.03k
    if (need_comma)
27
5.18k
      data_ << ',';
28
856
    else
29
856
      need_comma = true;
30
31
6.03k
    AppendValue(value);
32
6.03k
  }
33
1.30k
  data_ << ']';
34
1.30k
}
35
36
1.28k
void JsonProtoConverter::AppendNumber(const NumberValue& number_value) {
37
1.28k
  if (number_value.has_float_value()) {
38
210
    data_ << number_value.float_value().value();
39
1.07k
  } else if (number_value.has_exponent_value()) {
40
335
    auto value = number_value.exponent_value();
41
335
    data_ << value.base();
42
335
    data_ << (value.use_uppercase() ? 'E' : 'e');
43
335
    data_ << value.exponent();
44
741
  } else if (number_value.has_exponent_frac_value()) {
45
212
    auto value = number_value.exponent_value();
46
212
    data_ << value.base();
47
212
    data_ << (value.use_uppercase() ? 'E' : 'e');
48
212
    data_ << value.exponent();
49
529
  } else {
50
529
    data_ << number_value.integer_value().value();
51
529
  }
52
1.28k
}
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
10.1k
void JsonProtoConverter::AppendValue(const JsonValue& json_value) {
61
10.1k
  if (json_value.has_object_value()) {
62
1.08k
    AppendObject(json_value.object_value());
63
9.07k
  } else if (json_value.has_array_value()) {
64
1.30k
    AppendArray(json_value.array_value());
65
7.77k
  } else if (json_value.has_number_value()) {
66
1.28k
    AppendNumber(json_value.number_value());
67
6.49k
  } else if (json_value.has_string_value()) {
68
636
    data_ << '"' << json_value.string_value().value() << '"';
69
5.85k
  } else if (json_value.has_boolean_value()) {
70
282
    data_ << (json_value.boolean_value().value() ? "true" : "false");
71
5.57k
  } else {
72
5.57k
    data_ << "null";
73
5.57k
  }
74
10.1k
}
75
76
3.04k
std::string JsonProtoConverter::Convert(const JsonObject& json_object) {
77
3.04k
  AppendObject(json_object);
78
3.04k
  return data_.str();
79
3.04k
}
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