Coverage Report

Created: 2022-03-07 06:03

/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
918
void JsonProtoConverter::AppendArray(const ArrayValue& array_value) {
22
918
  data_ << '[';
23
918
  bool need_comma = false;
24
5.52k
  for (const auto& value : array_value.value()) {
25
    // Trailing comma inside of an array makes JSON invalid, avoid adding that.
26
5.52k
    if (need_comma)
27
5.08k
      data_ << ',';
28
439
    else
29
439
      need_comma = true;
30
31
5.52k
    AppendValue(value);
32
5.52k
  }
33
918
  data_ << ']';
34
918
}
35
36
1.50k
void JsonProtoConverter::AppendNumber(const NumberValue& number_value) {
37
1.50k
  if (number_value.has_float_value()) {
38
492
    data_ << number_value.float_value().value();
39
1.00k
  } else if (number_value.has_exponent_value()) {
40
296
    auto value = number_value.exponent_value();
41
296
    data_ << value.base();
42
296
    data_ << (value.use_uppercase() ? 'E' : 'e');
43
296
    data_ << value.exponent();
44
712
  } else if (number_value.has_exponent_frac_value()) {
45
213
    auto value = number_value.exponent_value();
46
213
    data_ << value.base();
47
213
    data_ << (value.use_uppercase() ? 'E' : 'e');
48
213
    data_ << value.exponent();
49
499
  } else {
50
499
    data_ << number_value.integer_value().value();
51
499
  }
52
1.50k
}
53
54
3.77k
void JsonProtoConverter::AppendObject(const JsonObject& json_object) {
55
3.77k
  data_ << '{' << '"' << json_object.name() << '"' << ':';
56
3.77k
  AppendValue(json_object.value());
57
3.77k
  data_ << '}';
58
3.77k
}
59
60
9.30k
void JsonProtoConverter::AppendValue(const JsonValue& json_value) {
61
9.30k
  if (json_value.has_object_value()) {
62
951
    AppendObject(json_value.object_value());
63
8.35k
  } else if (json_value.has_array_value()) {
64
918
    AppendArray(json_value.array_value());
65
7.43k
  } else if (json_value.has_number_value()) {
66
1.50k
    AppendNumber(json_value.number_value());
67
5.93k
  } else if (json_value.has_string_value()) {
68
678
    data_ << '"' << json_value.string_value().value() << '"';
69
5.25k
  } else if (json_value.has_boolean_value()) {
70
267
    data_ << (json_value.boolean_value().value() ? "true" : "false");
71
4.98k
  } else {
72
4.98k
    data_ << "null";
73
4.98k
  }
74
9.30k
}
75
76
2.82k
std::string JsonProtoConverter::Convert(const JsonObject& json_object) {
77
2.82k
  AppendObject(json_object);
78
2.82k
  return data_.str();
79
2.82k
}
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