Implement optional scalars for JSON (#7322)

* Implement optional scalars for JSON

* Add optional scalars JSON test

* Extend JSON optional scalars test to test without defaults

* Fix optional scalars in JSON for binary schema

Co-authored-by: Caleb Zulawski <caleb.zulawski@caci.com>
This commit is contained in:
Caleb Zulawski
2022-06-14 18:00:24 -04:00
committed by GitHub
parent 090caa2809
commit a18ea40d6a
5 changed files with 132 additions and 10 deletions

View File

@@ -248,11 +248,23 @@ struct JsonPrinter {
template<typename T>
bool GenField(const FieldDef &fd, const Table *table, bool fixed,
int indent) {
return PrintScalar(
fixed ? reinterpret_cast<const Struct *>(table)->GetField<T>(
fd.value.offset)
: table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)),
fd.value.type, indent);
if (fixed) {
return PrintScalar(
reinterpret_cast<const Struct *>(table)->GetField<T>(fd.value.offset),
fd.value.type, indent);
} else if (fd.IsOptional()) {
auto opt = table->GetOptional<T, T>(fd.value.offset);
if (opt) {
return PrintScalar(*opt, fd.value.type, indent);
} else {
text += "null";
return true;
}
} else {
return PrintScalar(
table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)),
fd.value.type, indent);
}
}
// Generate text for non-scalar field.