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/error/en.h" // IWYU pragma: export
40#include "rapidjson/stringbuffer.h" // IWYU pragma: export
41#include "rapidjson/writer.h" // IWYU pragma: export
42
43#include "arrow/status.h" // IWYU pragma: export
44#include "arrow/type_fwd.h" // IWYU pragma: keep
45#include "arrow/util/visibility.h"
46
47namespace rj = arrow::rapidjson;
48using RjWriter = rj::Writer<rj::StringBuffer>;
49using RjArray = rj::Value::ConstArray;
50using RjObject = rj::Value::ConstObject;
51
52#define RETURN_NOT_FOUND(TOK, NAME, PARENT) \
53 if (NAME == (PARENT).MemberEnd()) { \
54 return Status::Invalid("field ", TOK, " not found"); \
55 }
56
57#define RETURN_NOT_STRING(TOK, NAME, PARENT) \
58 RETURN_NOT_FOUND(TOK, NAME, PARENT); \
59 if (!NAME->value.IsString()) { \
60 return Status::Invalid("field was not a string line ", __LINE__); \
61 }
62
63#define RETURN_NOT_BOOL(TOK, NAME, PARENT) \
64 RETURN_NOT_FOUND(TOK, NAME, PARENT); \
65 if (!NAME->value.IsBool()) { \
66 return Status::Invalid("field was not a boolean line ", __LINE__); \
67 }
68
69#define RETURN_NOT_INT(TOK, NAME, PARENT) \
70 RETURN_NOT_FOUND(TOK, NAME, PARENT); \
71 if (!NAME->value.IsInt()) { \
72 return Status::Invalid("field was not an int line ", __LINE__); \
73 }
74
75#define RETURN_NOT_ARRAY(TOK, NAME, PARENT) \
76 RETURN_NOT_FOUND(TOK, NAME, PARENT); \
77 if (!NAME->value.IsArray()) { \
78 return Status::Invalid("field was not an array line ", __LINE__); \
79 }
80
81#define RETURN_NOT_OBJECT(TOK, NAME, PARENT) \
82 RETURN_NOT_FOUND(TOK, NAME, PARENT); \
83 if (!NAME->value.IsObject()) { \
84 return Status::Invalid("field was not an object line ", __LINE__); \
85 }
86
87namespace arrow {
88namespace ipc {
89namespace internal {
90namespace json {
91
92ARROW_EXPORT Status WriteSchema(const Schema& schema, RjWriter* writer);
93ARROW_EXPORT Status WriteRecordBatch(const RecordBatch& batch, RjWriter* writer);
94ARROW_EXPORT Status WriteArray(const std::string& name, const Array& array,
95 RjWriter* writer);
96
97ARROW_EXPORT Status ReadSchema(const rj::Value& json_obj, MemoryPool* pool,
98 std::shared_ptr<Schema>* schema);
99
100ARROW_EXPORT Status ReadRecordBatch(const rj::Value& json_obj,
101 const std::shared_ptr<Schema>& schema,
102 MemoryPool* pool,
103 std::shared_ptr<RecordBatch>* batch);
104
105ARROW_EXPORT Status ReadArray(MemoryPool* pool, const rj::Value& json_obj,
106 const std::shared_ptr<DataType>& type,
107 std::shared_ptr<Array>* array);
108
109ARROW_EXPORT Status ReadArray(MemoryPool* pool, const rj::Value& json_obj,
110 const Schema& schema, std::shared_ptr<Array>* array);
111
112} // namespace json
113} // namespace internal
114} // namespace ipc
115} // namespace arrow
116
117#endif // ARROW_IPC_JSON_INTERNAL_H
118