Skip to content

Commit 72c24fe

Browse files
committed
Add a minimal literal JSON example
Change-Id: Icd4cd4b58cb0ce392a856f83f89dd3e8a01a54b9
1 parent a2cf47b commit 72c24fe

File tree

2 files changed

+104
-30
lines changed

2 files changed

+104
-30
lines changed

cpp/src/arrow/ipc/ipc-json-test.cc

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,79 @@ TEST(TestJsonFileReadWrite, BasicRoundTrip) {
275275
}
276276
}
277277

278+
TEST(TestJsonFileReadWrite, MinimalFormatExample) {
279+
static const char* example = R"example(
280+
{
281+
"schema": {
282+
"fields": [
283+
{
284+
"name": "foo",
285+
"type": {"name": "int", "isSigned": true, "bitWidth": 32},
286+
"nullable": true, "children": [],
287+
"typeLayout": [
288+
{"type": "VALIDITY", "typeBitWidth": 1},
289+
{"type": "DATA", "typeBitWidth": 32}
290+
]
291+
},
292+
{
293+
"name": "bar",
294+
"type": {"name": "floatingpoint", "precision": "DOUBLE"},
295+
"nullable": true, "children": [],
296+
"typeLayout": [
297+
{"type": "VALIDITY", "typeBitWidth": 1},
298+
{"type": "DATA", "typeBitWidth": 64}
299+
]
300+
}
301+
]
302+
},
303+
"batches": [
304+
{
305+
"count": 5,
306+
"columns": [
307+
{
308+
"name": "foo",
309+
"count": 5,
310+
"DATA": [1, 2, 3, 4, 5],
311+
"VALIDITY": [1, 0, 1, 1, 1]
312+
},
313+
{
314+
"name": "bar",
315+
"count": 5,
316+
"DATA": [1.0, 2.0, 3.0, 4.0, 5.0],
317+
"VALIDITY": [1, 0, 0, 1, 1]
318+
}
319+
]
320+
}
321+
]
322+
}
323+
)example";
324+
325+
auto buffer = std::make_shared<Buffer>(
326+
reinterpret_cast<const uint8_t*>(example), strlen(example));
327+
328+
std::unique_ptr<JsonReader> reader;
329+
ASSERT_OK(JsonReader::Open(buffer, &reader));
330+
331+
Schema ex_schema({field("foo", int32()), field("bar", float64())});
332+
333+
ASSERT_TRUE(reader->schema()->Equals(ex_schema));
334+
ASSERT_EQ(1, reader->num_record_batches());
335+
336+
std::shared_ptr<RecordBatch> batch;
337+
ASSERT_OK(reader->GetRecordBatch(0, &batch));
338+
339+
std::vector<bool> foo_valid = {true, false, true, true, true};
340+
std::vector<int32_t> foo_values = {1, 2, 3, 4, 5};
341+
std::shared_ptr<Array> foo;
342+
MakeArray<Int32Type, int32_t>(int32(), foo_valid, foo_values, &foo);
343+
ASSERT_TRUE(batch->column(0)->Equals(foo));
344+
345+
std::vector<bool> bar_valid = {true, false, false, true, true};
346+
std::vector<double> bar_values = {1, 2, 3, 4, 5};
347+
std::shared_ptr<Array> bar;
348+
MakeArray<DoubleType, double>(float64(), bar_valid, bar_values, &bar);
349+
ASSERT_TRUE(batch->column(1)->Equals(bar));
350+
}
351+
278352
} // namespace ipc
279353
} // namespace arrow

cpp/src/arrow/ipc/json-internal.h

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,44 @@ using RjWriter = rj::Writer<rj::StringBuffer>;
4343
return Status::Invalid(ss.str()); \
4444
}
4545

46-
#define RETURN_NOT_STRING(TOK, NAME, PARENT) \
47-
RETURN_NOT_FOUND(TOK, NAME, PARENT); \
48-
if (!NAME->value.IsString()) { \
49-
std::stringstream ss; \
50-
ss << "field was not a string"; \
51-
return Status::Invalid(ss.str()); \
46+
#define RETURN_NOT_STRING(TOK, NAME, PARENT) \
47+
RETURN_NOT_FOUND(TOK, NAME, PARENT); \
48+
if (!NAME->value.IsString()) { \
49+
std::stringstream ss; \
50+
ss << "field was not a string" << " line " << __LINE__; \
51+
return Status::Invalid(ss.str()); \
5252
}
5353

54-
#define RETURN_NOT_BOOL(TOK, NAME, PARENT) \
55-
RETURN_NOT_FOUND(TOK, NAME, PARENT); \
56-
if (!NAME->value.IsBool()) { \
57-
std::stringstream ss; \
58-
ss << "field was not a boolean"; \
59-
return Status::Invalid(ss.str()); \
54+
#define RETURN_NOT_BOOL(TOK, NAME, PARENT) \
55+
RETURN_NOT_FOUND(TOK, NAME, PARENT); \
56+
if (!NAME->value.IsBool()) { \
57+
std::stringstream ss; \
58+
ss << "field was not a boolean" << " line " << __LINE__; \
59+
return Status::Invalid(ss.str()); \
6060
}
6161

62-
#define RETURN_NOT_INT(TOK, NAME, PARENT) \
63-
RETURN_NOT_FOUND(TOK, NAME, PARENT); \
64-
if (!NAME->value.IsInt()) { \
65-
std::stringstream ss; \
66-
ss << "field was not an int"; \
67-
return Status::Invalid(ss.str()); \
62+
#define RETURN_NOT_INT(TOK, NAME, PARENT) \
63+
RETURN_NOT_FOUND(TOK, NAME, PARENT); \
64+
if (!NAME->value.IsInt()) { \
65+
std::stringstream ss; \
66+
ss << "field was not an int" << " line " << __LINE__; \
67+
return Status::Invalid(ss.str()); \
6868
}
6969

70-
#define RETURN_NOT_ARRAY(TOK, NAME, PARENT) \
71-
RETURN_NOT_FOUND(TOK, NAME, PARENT); \
72-
if (!NAME->value.IsArray()) { \
73-
std::stringstream ss; \
74-
ss << "field was not an array"; \
75-
return Status::Invalid(ss.str()); \
70+
#define RETURN_NOT_ARRAY(TOK, NAME, PARENT) \
71+
RETURN_NOT_FOUND(TOK, NAME, PARENT); \
72+
if (!NAME->value.IsArray()) { \
73+
std::stringstream ss; \
74+
ss << "field was not an array" << " line " << __LINE__; \
75+
return Status::Invalid(ss.str()); \
7676
}
7777

78-
#define RETURN_NOT_OBJECT(TOK, NAME, PARENT) \
79-
RETURN_NOT_FOUND(TOK, NAME, PARENT); \
80-
if (!NAME->value.IsObject()) { \
81-
std::stringstream ss; \
82-
ss << "field was not an object"; \
83-
return Status::Invalid(ss.str()); \
78+
#define RETURN_NOT_OBJECT(TOK, NAME, PARENT) \
79+
RETURN_NOT_FOUND(TOK, NAME, PARENT); \
80+
if (!NAME->value.IsObject()) { \
81+
std::stringstream ss; \
82+
ss << "field was not an object" << " line " << __LINE__; \
83+
return Status::Invalid(ss.str()); \
8484
}
8585

8686
namespace arrow {

0 commit comments

Comments
 (0)