mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-06 05:27:24 +00:00
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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user