Skip to content

Commit

Permalink
Make serialize_json_to_string output compactly (without newlines) whe…
Browse files Browse the repository at this point in the history
…n indent argument is less than 1 (#1578)

* Make serialize_json_to_string output compact when indent is less than 1
* Add C++ test

---------

Signed-off-by: Jean-Christophe Morin <[email protected]>
  • Loading branch information
JeanChristopheMorinPerso authored Apr 13, 2023
1 parent 6f4ed5e commit 23ff3f9
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 4 deletions.
56 changes: 53 additions & 3 deletions src/opentimelineio/serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ SerializableObject::clone(ErrorStatus* error_status) const

// to json_string
std::string
serialize_json_to_string(
serialize_json_to_string_pretty(
const any& value,
const schema_version_map* schema_version_targets,
ErrorStatus* error_status,
Expand All @@ -1232,11 +1232,39 @@ serialize_json_to_string(
OTIO_rapidjson::kWriteNanAndInfFlag>
json_writer(output_string_buffer);

if (indent >= 0)
json_writer.SetIndent(' ', indent);

JSONEncoder<decltype(json_writer)> json_encoder(json_writer);

if (!SerializableObject::Writer::write_root(
value,
json_encoder,
schema_version_targets,
error_status))
{
json_writer.SetIndent(' ', indent);
return std::string();
}

return std::string(output_string_buffer.GetString());
}

// to json_string
std::string
serialize_json_to_string_compact(
const any& value,
const schema_version_map* schema_version_targets,
ErrorStatus* error_status)
{
OTIO_rapidjson::StringBuffer output_string_buffer;

OTIO_rapidjson::Writer<
decltype(output_string_buffer),
OTIO_rapidjson::UTF8<>,
OTIO_rapidjson::UTF8<>,
OTIO_rapidjson::CrtAllocator,
OTIO_rapidjson::kWriteNanAndInfFlag>
json_writer(output_string_buffer);

JSONEncoder<decltype(json_writer)> json_encoder(json_writer);

if (!SerializableObject::Writer::write_root(
Expand All @@ -1251,6 +1279,28 @@ serialize_json_to_string(
return std::string(output_string_buffer.GetString());
}

// to json_string
std::string
serialize_json_to_string(
const any& value,
const schema_version_map* schema_version_targets,
ErrorStatus* error_status,
int indent)
{
if (indent > 0)
{
return serialize_json_to_string_pretty(
value,
schema_version_targets,
error_status,
indent);
}
return serialize_json_to_string_compact(
value,
schema_version_targets,
error_status);
}

bool
serialize_json_to_file(
any const& value,
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ foreach(test ${tests_opentime})
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endforeach()

list(APPEND tests_opentimelineio test_clip test_serializableCollection test_timeline test_track)
list(APPEND tests_opentimelineio test_clip test_serialization test_serializableCollection test_timeline test_track)
foreach(test ${tests_opentimelineio})
add_executable(${test} utils.h utils.cpp ${test}.cpp)

Expand Down
117 changes: 117 additions & 0 deletions tests/test_serialization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenTimelineIO project

#include "utils.h"

#include <opentimelineio/clip.h>
#include <opentimelineio/timeline.h>
#include <opentimelineio/track.h>
#include <opentimelineio/serialization.h>
#include <opentimelineio/serializableObject.h>
#include <opentimelineio/serializableObjectWithMetadata.h>
#include <opentimelineio/safely_typed_any.h>

#include <iostream>
#include <string>

namespace otime = opentime::OPENTIME_VERSION;
namespace otio = opentimelineio::OPENTIMELINEIO_VERSION;

int
main(int argc, char** argv)
{
Tests tests;

tests.add_test(
"success with default indent", [] {
otio::SerializableObject::Retainer<otio::Clip> cl =
new otio::Clip();
otio::SerializableObject::Retainer<otio::Track> tr =
new otio::Track();
tr->append_child(cl);
otio::SerializableObject::Retainer<otio::Timeline> tl =
new otio::Timeline();
tl->tracks()->append_child(tr);

otio::ErrorStatus err;
auto output = tl.value->to_json_string(&err, {});
assertFalse(otio::is_error(err));
assertEqual(output.c_str(), R"CONTENT({
"OTIO_SCHEMA": "Timeline.1",
"metadata": {},
"name": "",
"global_start_time": null,
"tracks": {
"OTIO_SCHEMA": "Stack.1",
"metadata": {},
"name": "tracks",
"source_range": null,
"effects": [],
"markers": [],
"enabled": true,
"children": [
{
"OTIO_SCHEMA": "Track.1",
"metadata": {},
"name": "",
"source_range": null,
"effects": [],
"markers": [],
"enabled": true,
"children": [
{
"OTIO_SCHEMA": "Clip.2",
"metadata": {},
"name": "",
"source_range": null,
"effects": [],
"markers": [],
"enabled": true,
"media_references": {
"DEFAULT_MEDIA": {
"OTIO_SCHEMA": "MissingReference.1",
"metadata": {},
"name": "",
"available_range": null,
"available_image_bounds": null
}
},
"active_media_reference_key": "DEFAULT_MEDIA"
}
],
"kind": "Video"
}
]
}
})CONTENT");
});

tests.add_test(
"success with indent set to 0", [] {
otio::SerializableObject::Retainer<otio::SerializableObjectWithMetadata> so =
new otio::SerializableObjectWithMetadata();

otio::ErrorStatus err;
auto output = so.value->to_json_string(&err, {}, 0);
assertFalse(otio::is_error(err));
assertEqual(output.c_str(), R"CONTENT({"OTIO_SCHEMA":"SerializableObjectWithMetadata.1","metadata":{},"name":""})CONTENT");
});

tests.add_test(
"success with indent set to 2", [] {
otio::SerializableObject::Retainer<otio::SerializableObjectWithMetadata> so =
new otio::SerializableObjectWithMetadata();

otio::ErrorStatus err;
auto output = so.value->to_json_string(&err, {}, 2);
assertFalse(otio::is_error(err));
assertEqual(output.c_str(), R"CONTENT({
"OTIO_SCHEMA": "SerializableObjectWithMetadata.1",
"metadata": {},
"name": ""
})CONTENT");
});

tests.run(argc, argv);
return 0;
}

0 comments on commit 23ff3f9

Please sign in to comment.