/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 | 184 | JsonReader::JsonReader(string text) : text_(std::move(text)) {} |
48 | | |
49 | 184 | JsonReader::~JsonReader() { |
50 | 184 | } |
51 | | |
52 | 184 | Status JsonReader::Init() { |
53 | 184 | document_.Parse<0>(text_.c_str()); |
54 | 184 | if (document_.HasParseError()) { |
55 | 1 | return STATUS(Corruption, "JSON text is corrupt", |
56 | 1 | rapidjson::GetParseError_En(document_.GetParseError())); |
57 | 1 | } |
58 | 183 | return Status::OK(); |
59 | 183 | } |
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 | 13 | int32_t* result) const { |
78 | 13 | const Value* val; |
79 | 13 | RETURN_NOT_OK(ExtractField(object, field, &val)); |
80 | 12 | 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 | 5 | *result = val->GetUint(); |
86 | 5 | return Status::OK(); |
87 | 5 | } |
88 | | |
89 | | Status JsonReader::ExtractInt64(const Value* object, |
90 | | const char* field, |
91 | 179 | int64_t* result) const { |
92 | 179 | const Value* val; |
93 | 179 | RETURN_NOT_OK(ExtractField(object, field, &val)); |
94 | 178 | 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 | 172 | *result = val->GetUint64(); |
99 | 172 | return Status::OK(); |
100 | 172 | } |
101 | | |
102 | | Status JsonReader::ExtractString(const Value* object, |
103 | | const char* field, |
104 | 476 | string* result) const { |
105 | 476 | const Value* val; |
106 | 476 | RETURN_NOT_OK(ExtractField(object, field, &val)); |
107 | 475 | 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 | 5 | "Wrong type during field extraction: expected string but got $0", |
114 | 5 | val->GetType())); } |
115 | 469 | result->assign(val->GetString()); |
116 | 469 | return Status::OK(); |
117 | 469 | } |
118 | | |
119 | | Status JsonReader::ExtractObject(const Value* object, |
120 | | const char* field, |
121 | 12 | const Value** result) const { |
122 | 12 | const Value* val; |
123 | 12 | RETURN_NOT_OK(ExtractField(object, field, &val)); |
124 | 11 | 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 | 4 | *result = val; |
129 | 4 | return Status::OK(); |
130 | 4 | } |
131 | | |
132 | | Status JsonReader::ExtractObjectArray(const Value* object, |
133 | | const char* field, |
134 | 350 | vector<const Value*>* result) const { |
135 | 350 | const Value* val; |
136 | 350 | RETURN_NOT_OK(ExtractField(object, field, &val)); |
137 | 349 | 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 | 755 | for (Value::ConstValueIterator iter = val->Begin(); iter != val->End(); ++iter) { |
142 | 412 | result->push_back(iter); |
143 | 412 | } |
144 | 343 | return Status::OK(); |
145 | 343 | } |
146 | | |
147 | | Status JsonReader::ExtractField(const Value* object, |
148 | | const char* field, |
149 | 1.03k | const Value** result) const { |
150 | 1.03k | if (field && PREDICT_FALSE(!object->HasMember(field))) { |
151 | 5 | return STATUS(NotFound, "Missing field", field); |
152 | 5 | } |
153 | 1.02k | *result = field ? &(*object)[field] : object; |
154 | 1.02k | return Status::OK(); |
155 | 1.02k | } |
156 | | |
157 | | } // namespace yb |