mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-07 13:53:38 +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.
|
||||
|
||||
@@ -1335,10 +1335,18 @@ CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value,
|
||||
ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
|
||||
builder_.PushElement(val); \
|
||||
} else { \
|
||||
CTYPE val, valdef; \
|
||||
ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
|
||||
ECHECK(atot(field->value.constant.c_str(), *this, &valdef)); \
|
||||
builder_.AddElement(field_value.offset, val, valdef); \
|
||||
if (field->IsScalarOptional()) { \
|
||||
if (field_value.constant != "null") { \
|
||||
CTYPE val; \
|
||||
ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
|
||||
builder_.AddElement(field_value.offset, val); \
|
||||
} \
|
||||
} else { \
|
||||
CTYPE val, valdef; \
|
||||
ECHECK(atot(field_value.constant.c_str(), *this, &val)); \
|
||||
ECHECK(atot(field->value.constant.c_str(), *this, &valdef)); \
|
||||
builder_.AddElement(field_value.offset, val, valdef); \
|
||||
} \
|
||||
} \
|
||||
break;
|
||||
FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
|
||||
@@ -2469,7 +2477,7 @@ bool Parser::SupportsOptionalScalars(const flatbuffers::IDLOptions &opts) {
|
||||
IDLOptions::kRust | IDLOptions::kSwift | IDLOptions::kLobster |
|
||||
IDLOptions::kKotlin | IDLOptions::kCpp | IDLOptions::kJava |
|
||||
IDLOptions::kCSharp | IDLOptions::kTs | IDLOptions::kBinary |
|
||||
IDLOptions::kGo | IDLOptions::kPython;
|
||||
IDLOptions::kGo | IDLOptions::kPython | IDLOptions::kJson;
|
||||
unsigned long langs = opts.lang_to_generate;
|
||||
return (langs > 0 && langs < IDLOptions::kMAX) && !(langs & ~supported_langs);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user