Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
json-internal.h
Go to the documentation of this file.
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 #ifndef ARROW_IPC_JSON_INTERNAL_H
19 #define ARROW_IPC_JSON_INTERNAL_H
20 
21 #define RAPIDJSON_HAS_STDSTRING 1
22 #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1
23 #define RAPIDJSON_HAS_CXX11_RANGE_FOR 1
24 
25 #define RAPIDJSON_NAMESPACE arrow::rapidjson
26 #define RAPIDJSON_NAMESPACE_BEGIN \
27  namespace arrow { \
28  namespace rapidjson {
29 #define RAPIDJSON_NAMESPACE_END \
30  } \
31  }
32 
33 #include <memory>
34 #include <sstream>
35 #include <string>
36 
37 #include "rapidjson/document.h" // IWYU pragma: export
38 #include "rapidjson/encodings.h" // IWYU pragma: export
39 #include "rapidjson/stringbuffer.h" // IWYU pragma: export
40 #include "rapidjson/writer.h" // IWYU pragma: export
41 
42 #include "arrow/status.h" // IWYU pragma: export
43 #include "arrow/type_fwd.h" // IWYU pragma: keep
44 #include "arrow/util/visibility.h"
45 
46 namespace rj = arrow::rapidjson;
47 using RjWriter = rj::Writer<rj::StringBuffer>;
48 using RjArray = rj::Value::ConstArray;
49 using RjObject = rj::Value::ConstObject;
50 
51 #define RETURN_NOT_FOUND(TOK, NAME, PARENT) \
52  if (NAME == (PARENT).MemberEnd()) { \
53  std::stringstream ss; \
54  ss << "field " << TOK << " not found"; \
55  return Status::Invalid(ss.str()); \
56  }
57 
58 #define RETURN_NOT_STRING(TOK, NAME, PARENT) \
59  RETURN_NOT_FOUND(TOK, NAME, PARENT); \
60  if (!NAME->value.IsString()) { \
61  std::stringstream ss; \
62  ss << "field was not a string" \
63  << " line " << __LINE__; \
64  return Status::Invalid(ss.str()); \
65  }
66 
67 #define RETURN_NOT_BOOL(TOK, NAME, PARENT) \
68  RETURN_NOT_FOUND(TOK, NAME, PARENT); \
69  if (!NAME->value.IsBool()) { \
70  std::stringstream ss; \
71  ss << "field was not a boolean" \
72  << " line " << __LINE__; \
73  return Status::Invalid(ss.str()); \
74  }
75 
76 #define RETURN_NOT_INT(TOK, NAME, PARENT) \
77  RETURN_NOT_FOUND(TOK, NAME, PARENT); \
78  if (!NAME->value.IsInt()) { \
79  std::stringstream ss; \
80  ss << "field was not an int" \
81  << " line " << __LINE__; \
82  return Status::Invalid(ss.str()); \
83  }
84 
85 #define RETURN_NOT_ARRAY(TOK, NAME, PARENT) \
86  RETURN_NOT_FOUND(TOK, NAME, PARENT); \
87  if (!NAME->value.IsArray()) { \
88  std::stringstream ss; \
89  ss << "field was not an array" \
90  << " line " << __LINE__; \
91  return Status::Invalid(ss.str()); \
92  }
93 
94 #define RETURN_NOT_OBJECT(TOK, NAME, PARENT) \
95  RETURN_NOT_FOUND(TOK, NAME, PARENT); \
96  if (!NAME->value.IsObject()) { \
97  std::stringstream ss; \
98  ss << "field was not an object" \
99  << " line " << __LINE__; \
100  return Status::Invalid(ss.str()); \
101  }
102 
103 namespace arrow {
104 namespace ipc {
105 namespace internal {
106 namespace json {
107 
108 ARROW_EXPORT Status WriteSchema(const Schema& schema, RjWriter* writer);
109 ARROW_EXPORT Status WriteRecordBatch(const RecordBatch& batch, RjWriter* writer);
110 ARROW_EXPORT Status WriteArray(const std::string& name, const Array& array,
111  RjWriter* writer);
112 
113 ARROW_EXPORT Status ReadSchema(const rj::Value& json_obj, MemoryPool* pool,
114  std::shared_ptr<Schema>* schema);
115 
116 ARROW_EXPORT Status ReadRecordBatch(const rj::Value& json_obj,
117  const std::shared_ptr<Schema>& schema,
118  MemoryPool* pool,
119  std::shared_ptr<RecordBatch>* batch);
120 
121 ARROW_EXPORT Status ReadArray(MemoryPool* pool, const rj::Value& json_obj,
122  const std::shared_ptr<DataType>& type,
123  std::shared_ptr<Array>* array);
124 
125 ARROW_EXPORT Status ReadArray(MemoryPool* pool, const rj::Value& json_obj,
126  const Schema& schema, std::shared_ptr<Array>* array);
127 
128 } // namespace json
129 } // namespace internal
130 } // namespace ipc
131 } // namespace arrow
132 
133 #endif // ARROW_IPC_JSON_INTERNAL_H
Status WriteRecordBatch(const RecordBatch &batch, int64_t buffer_start_offset, io::OutputStream *dst, int32_t *metadata_length, int64_t *body_length, MemoryPool *pool, int max_recursion_depth=kMaxNestingDepth, bool allow_64bit=false)
Low-level API for writing a record batch (without schema) to an OutputStream.
rj::Writer< rj::StringBuffer > RjWriter
Definition: json-internal.h:47
Status ReadRecordBatch(const std::shared_ptr< Schema > &schema, io::InputStream *stream, std::shared_ptr< RecordBatch > *out)
Read record batch as encapsulated IPC message with metadata size prefix and header.
rj::Value::ConstObject RjObject
Definition: json-internal.h:49
rj::Value::ConstArray RjArray
Definition: json-internal.h:48
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
std::shared_ptr< Schema > schema(const std::vector< std::shared_ptr< Field >> &fields, const std::shared_ptr< const KeyValueMetadata > &metadata=NULLPTR)
Create a Schema instance.
Status ReadSchema(io::InputStream *stream, std::shared_ptr< Schema > *out)
Read Schema from stream serialized as a sequence of one or more IPC messages.