Reenable optional json (#7352)

* Revert "Revert "Implement optional scalars for JSON (#7322)" (#7351)"

This reverts commit 9a1913a87a.

* Add optional scalars json to bazel

Co-authored-by: Caleb Zulawski <caleb.zulawski@caci.com>
This commit is contained in:
Caleb Zulawski
2022-06-16 12:26:44 -04:00
committed by GitHub
parent 5f01376027
commit 83a43fc797
6 changed files with 134 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.