YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/jsonreader.h
Line
Count
Source
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
#ifndef YB_UTIL_JSONREADER_H_
33
#define YB_UTIL_JSONREADER_H_
34
35
#include <stdint.h>
36
#include <string>
37
#include <vector>
38
39
#include <rapidjson/document.h>
40
41
#include "yb/gutil/macros.h"
42
43
#include "yb/util/status_fwd.h"
44
45
namespace yb {
46
47
// Wraps the JSON parsing functionality of rapidjson::Document.
48
//
49
// Unlike JsonWriter, this class does not hide rapidjson internals from
50
// clients. That's because there's just no easy way to implement object and
51
// array parsing otherwise. At most, this class aspires to be a simpler
52
// error-handling wrapper for reading and parsing.
53
class JsonReader {
54
 public:
55
  explicit JsonReader(std::string text);
56
  ~JsonReader();
57
58
  CHECKED_STATUS Init();
59
60
  // Extractor methods.
61
  //
62
  // If 'field' is not NULL, will look for a field with that name in the
63
  // given object, returning Status::NotFound if it cannot be found. If
64
  // 'field' is NULL, will try to convert 'object' directly into the
65
  // desire type.
66
67
  CHECKED_STATUS ExtractBool(const rapidjson::Value* object,
68
                      const char* field,
69
                      bool* result) const;
70
71
  CHECKED_STATUS ExtractInt32(const rapidjson::Value* object,
72
                      const char* field,
73
                      int32_t* result) const;
74
75
  CHECKED_STATUS ExtractInt64(const rapidjson::Value* object,
76
                      const char* field,
77
                      int64_t* result) const;
78
79
  CHECKED_STATUS ExtractString(const rapidjson::Value* object,
80
                       const char* field,
81
                       std::string* result) const;
82
83
  // 'result' is only valid for as long as JsonReader is alive.
84
  CHECKED_STATUS ExtractObject(const rapidjson::Value* object,
85
                       const char* field,
86
                       const rapidjson::Value** result) const;
87
88
  // 'result' is only valid for as long as JsonReader is alive.
89
  CHECKED_STATUS ExtractObjectArray(const rapidjson::Value* object,
90
                            const char* field,
91
                            std::vector<const rapidjson::Value*>* result) const;
92
93
679
  const rapidjson::Value* root() const { return &document_; }
94
95
 private:
96
  CHECKED_STATUS ExtractField(const rapidjson::Value* object,
97
                      const char* field,
98
                      const rapidjson::Value** result) const;
99
100
  std::string text_;
101
  rapidjson::Document document_;
102
103
  DISALLOW_COPY_AND_ASSIGN(JsonReader);
104
};
105
106
} // namespace yb
107
108
#endif // YB_UTIL_JSONREADER_H_