YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/jsonreader.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#include "yb/util/jsonreader.h"
33
34
#include "yb/gutil/strings/substitute.h"
35
36
#include <rapidjson/error/en.h>
37
38
#include "yb/util/status.h"
39
40
using rapidjson::Value;
41
using std::string;
42
using std::vector;
43
using strings::Substitute;
44
45
namespace yb {
46
47
593
JsonReader::JsonReader(string text) : text_(std::move(text)) {}
48
49
593
JsonReader::~JsonReader() {
50
593
}
51
52
593
Status JsonReader::Init() {
53
593
  document_.Parse<0>(text_.c_str());
54
593
  if (document_.HasParseError()) {
55
1
    return STATUS(Corruption, "JSON text is corrupt",
56
1
        rapidjson::GetParseError_En(document_.GetParseError()));
57
1
  }
58
592
  return Status::OK();
59
593
}
60
61
Status JsonReader::ExtractBool(const Value* object,
62
                               const char* field,
63
0
                               bool* result) const {
64
0
  const Value* val;
65
0
  RETURN_NOT_OK(ExtractField(object, field, &val));
66
0
  if (PREDICT_FALSE(!val->IsBool())) {
67
0
    return STATUS(InvalidArgument, Substitute(
68
0
        "Wrong type during field extraction: expected bool but got $0",
69
0
        val->GetType()));
70
0
  }
71
0
  *result = val->GetBool();
72
0
  return Status::OK();
73
0
}
74
75
Status JsonReader::ExtractInt32(const Value* object,
76
                                const char* field,
77
18
                                int32_t* result) const {
78
18
  const Value* val;
79
18
  RETURN_NOT_OK(ExtractField(object, field, &val));
80
17
  if (PREDICT_FALSE(!val->IsInt())) {
81
7
    return STATUS(InvalidArgument, Substitute(
82
7
        "Wrong type during field extraction: expected int32 but got $0",
83
7
        val->GetType()));
84
7
  }
85
10
  *result = val->GetUint();
86
10
  return Status::OK();
87
17
}
88
89
Status JsonReader::ExtractInt64(const Value* object,
90
                                const char* field,
91
567
                                int64_t* result) const {
92
567
  const Value* val;
93
567
  RETURN_NOT_OK(ExtractField(object, field, &val));
94
566
  if (PREDICT_FALSE(!val->IsInt64())) {
95
6
    return STATUS(InvalidArgument, Substitute(
96
6
        "Wrong type during field extraction: expected int64 but got $0",
97
6
        val->GetType()));  }
98
560
  *result = val->GetUint64();
99
560
  return Status::OK();
100
566
}
101
102
Status JsonReader::ExtractString(const Value* object,
103
                                 const char* field,
104
25.0k
                                 string* result) const {
105
25.0k
  const Value* val;
106
25.0k
  RETURN_NOT_OK(ExtractField(object, field, &val));
107
25.0k
  if (PREDICT_FALSE(!val->IsString())) {
108
6
    if (val->IsNull()) {
109
1
      *result = "";
110
1
      return Status::OK();
111
1
    }
112
5
    return STATUS(InvalidArgument, Substitute(
113
6
        "Wrong type during field extraction: expected string but got $0",
114
6
        val->GetType()));  }
115
25.0k
  result->assign(val->GetString());
116
25.0k
  return Status::OK();
117
25.0k
}
118
119
Status JsonReader::ExtractObject(const Value* object,
120
                                 const char* field,
121
15
                                 const Value** result) const {
122
15
  const Value* val;
123
15
  RETURN_NOT_OK(ExtractField(object, field, &val));
124
14
  if (PREDICT_FALSE(!val->IsObject())) {
125
7
    return STATUS(InvalidArgument, Substitute(
126
7
        "Wrong type during field extraction: expected object but got $0",
127
7
        val->GetType()));  }
128
7
  *result = val;
129
7
  return Status::OK();
130
14
}
131
132
Status JsonReader::ExtractObjectArray(const Value* object,
133
                                      const char* field,
134
1.14k
                                      vector<const Value*>* result) const {
135
1.14k
  const Value* val;
136
1.14k
  RETURN_NOT_OK(ExtractField(object, field, &val));
137
1.14k
  if (PREDICT_FALSE(!val->IsArray())) {
138
6
    return STATUS(InvalidArgument, Substitute(
139
6
        "Wrong type during field extraction: expected object array but got $0",
140
6
        val->GetType()));  }
141
24.6k
  
for (Value::ConstValueIterator iter = val->Begin(); 1.14k
iter != val->End();
++iter23.5k
) {
142
23.5k
    result->push_back(iter);
143
23.5k
  }
144
1.14k
  return Status::OK();
145
1.14k
}
146
147
Status JsonReader::ExtractField(const Value* object,
148
                                const char* field,
149
26.8k
                                const Value** result) const {
150
26.8k
  if (field && 
PREDICT_FALSE26.2k
(!object->HasMember(field))) {
151
5
    return STATUS(NotFound, "Missing field", field);
152
5
  }
153
26.7k
  *result = field ? 
&(*object)[field]26.2k
:
object583
;
154
26.7k
  return Status::OK();
155
26.8k
}
156
157
} // namespace yb