Skip to content

Commit

Permalink
Avoid writing empty extensions object
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Nov 15, 2024
1 parent 0bc7245 commit 5766c70
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cesium3DTilesWriter/generated/src/TilesetJsonWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void writeExtensibleObject(
CesiumJsonWriter::JsonWriter& jsonWriter,
const CesiumJsonWriter::ExtensionWriterContext& context) {

if (!obj.extensions.empty()) {
if (hasWritableExtensions(obj, jsonWriter, context)) {
jsonWriter.Key("extensions");
writeJsonExtensions(obj, jsonWriter, context);
}
Expand Down
2 changes: 1 addition & 1 deletion CesiumGltfWriter/generated/src/ModelJsonWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ void writeExtensibleObject(
CesiumJsonWriter::JsonWriter& jsonWriter,
const CesiumJsonWriter::ExtensionWriterContext& context) {

if (!obj.extensions.empty()) {
if (hasWritableExtensions(obj, jsonWriter, context)) {
jsonWriter.Key("extensions");
writeJsonExtensions(obj, jsonWriter, context);
}
Expand Down
41 changes: 30 additions & 11 deletions CesiumJsonWriter/include/CesiumJsonWriter/writeJsonExtensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,38 @@ void writeJsonExtensions(
item.first,
item.second,
TExtended::TypeName);
if (!handler) {
if (context.getExtensionState(item.first) != ExtensionState::Disabled) {
jsonWriter.emplaceWarning(fmt::format(
"Encountered unregistered extension {}. This extension will be "
"ignored. To silence this warning, disable the extension with "
"ExtensionWriterContext::setExtensionState.",
item.first));
}
continue;
if (handler) {
jsonWriter.Key(item.first);
handler(item.second, jsonWriter, context);
}
jsonWriter.Key(item.first);
handler(item.second, jsonWriter, context);
}
jsonWriter.EndObject();
}

template <typename TExtended>
bool hasWritableExtensions(
const TExtended& obj,
JsonWriter& jsonWriter,
const ExtensionWriterContext& context) {
bool hasWritableExtensions = false;

for (const auto& item : obj.extensions) {
auto handler = context.createExtensionHandler(
item.first,
item.second,
TExtended::TypeName);
if (handler) {
hasWritableExtensions = true;
} else if (
context.getExtensionState(item.first) != ExtensionState::Disabled) {
jsonWriter.emplaceWarning(fmt::format(
"Encountered unregistered extension {}. This extension will be "
"ignored. To silence this warning, disable the extension with "
"ExtensionWriterContext::setExtensionState.",
item.first));
}
}

return hasWritableExtensions;
}
} // namespace CesiumJsonWriter
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void writeExtensibleObject(
CesiumJsonWriter::JsonWriter& jsonWriter,
const CesiumJsonWriter::ExtensionWriterContext& context) {

if (!obj.extensions.empty()) {
if (hasWritableExtensions(obj, jsonWriter, context)) {
jsonWriter.Key("extensions");
writeJsonExtensions(obj, jsonWriter, context);
}
Expand Down
34 changes: 17 additions & 17 deletions tools/generate-classes/generateCombinedWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function generateCombinedWriter(options) {
.join("\n")}
} // namespace ${namespace}
namespace ${writerNamespace} {
namespace ${writerNamespace} {
${writers
.map((writer) => {
return writer.writeDeclaration;
Expand All @@ -49,9 +49,9 @@ function generateCombinedWriter(options) {
const implementation = `
// This file was generated by generate-classes.
// DO NOT EDIT THIS FILE!
#include "${name}JsonWriter.h"
#include <CesiumUtility/JsonValue.h>
#include <CesiumJsonWriter/ExtensionWriterContext.h>
#include <CesiumJsonWriter/writeJsonExtensions.h>
Expand All @@ -63,17 +63,17 @@ function generateCombinedWriter(options) {
return writer.writeInclude;
})
.join("\n")}
namespace ${writerNamespace} {
namespace {
${writers
.map((writer) => {
return writer.writeJsonDeclaration;
})
.join("\n")}
// Forward declaration to avoid circular dependency since some properties
// are vector of unordered_map and others are unordered_map of vector
template <typename T>
Expand All @@ -88,21 +88,21 @@ function generateCombinedWriter(options) {
const CesiumJsonWriter::ExtensionWriterContext& /* context */) {
jsonWriter.String(str);
}
[[maybe_unused]] void writeJson(
double val,
CesiumJsonWriter::JsonWriter& jsonWriter,
const CesiumJsonWriter::ExtensionWriterContext& /* context */) {
jsonWriter.Double(val);
}
[[maybe_unused]] void writeJson(
bool val,
CesiumJsonWriter::JsonWriter& jsonWriter,
const CesiumJsonWriter::ExtensionWriterContext& /* context */) {
jsonWriter.Bool(val);
}
[[maybe_unused]] void writeJson(
int64_t val,
CesiumJsonWriter::JsonWriter& jsonWriter,
Expand All @@ -116,7 +116,7 @@ function generateCombinedWriter(options) {
const CesiumJsonWriter::ExtensionWriterContext& /* context */) {
jsonWriter.Int64(val);
}
[[maybe_unused]] void writeJson(
const CesiumUtility::JsonValue::Object& obj,
CesiumJsonWriter::JsonWriter& jsonWriter,
Expand All @@ -128,13 +128,13 @@ function generateCombinedWriter(options) {
}
jsonWriter.EndObject();
}
[[maybe_unused]] void writeJson(
const CesiumUtility::JsonValue& value,
CesiumJsonWriter::JsonWriter& jsonWriter,
const CesiumJsonWriter::ExtensionWriterContext& /* context */) {
CesiumJsonWriter::writeJsonValue(value, jsonWriter);
}
}
template <typename T>
[[maybe_unused]] void writeJson(
Expand All @@ -160,7 +160,7 @@ function generateCombinedWriter(options) {
}
jsonWriter.EndArray();
}
template <typename T>
[[maybe_unused]] void writeJson(
const std::optional<T>& val,
Expand All @@ -179,7 +179,7 @@ function generateCombinedWriter(options) {
CesiumJsonWriter::JsonWriter& jsonWriter,
const CesiumJsonWriter::ExtensionWriterContext& context) {
if (!obj.extensions.empty()) {
if (hasWritableExtensions(obj, jsonWriter, context)) {
jsonWriter.Key("extensions");
writeJsonExtensions(obj, jsonWriter, context);
}
Expand All @@ -203,7 +203,7 @@ function generateCombinedWriter(options) {
writeExtensibleObject(obj, jsonWriter, context);
}
${writers
.map((writer) => {
return writer.writeBaseJsonDefinition ? writer.writeBaseJsonDefinition : "";
Expand All @@ -223,7 +223,7 @@ function generateCombinedWriter(options) {
return writer.writeDefinition;
})
.join("\n")}
} // namespace ${writerNamespace}
`;

Expand Down

0 comments on commit 5766c70

Please sign in to comment.