Skip to content

Commit 23ff3f9

Browse files
Make serialize_json_to_string output compactly (without newlines) when 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]>
1 parent 6f4ed5e commit 23ff3f9

File tree

3 files changed

+171
-4
lines changed

3 files changed

+171
-4
lines changed

src/opentimelineio/serialization.cpp

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ SerializableObject::clone(ErrorStatus* error_status) const
12161216

12171217
// to json_string
12181218
std::string
1219-
serialize_json_to_string(
1219+
serialize_json_to_string_pretty(
12201220
const any& value,
12211221
const schema_version_map* schema_version_targets,
12221222
ErrorStatus* error_status,
@@ -1232,11 +1232,39 @@ serialize_json_to_string(
12321232
OTIO_rapidjson::kWriteNanAndInfFlag>
12331233
json_writer(output_string_buffer);
12341234

1235-
if (indent >= 0)
1235+
json_writer.SetIndent(' ', indent);
1236+
1237+
JSONEncoder<decltype(json_writer)> json_encoder(json_writer);
1238+
1239+
if (!SerializableObject::Writer::write_root(
1240+
value,
1241+
json_encoder,
1242+
schema_version_targets,
1243+
error_status))
12361244
{
1237-
json_writer.SetIndent(' ', indent);
1245+
return std::string();
12381246
}
12391247

1248+
return std::string(output_string_buffer.GetString());
1249+
}
1250+
1251+
// to json_string
1252+
std::string
1253+
serialize_json_to_string_compact(
1254+
const any& value,
1255+
const schema_version_map* schema_version_targets,
1256+
ErrorStatus* error_status)
1257+
{
1258+
OTIO_rapidjson::StringBuffer output_string_buffer;
1259+
1260+
OTIO_rapidjson::Writer<
1261+
decltype(output_string_buffer),
1262+
OTIO_rapidjson::UTF8<>,
1263+
OTIO_rapidjson::UTF8<>,
1264+
OTIO_rapidjson::CrtAllocator,
1265+
OTIO_rapidjson::kWriteNanAndInfFlag>
1266+
json_writer(output_string_buffer);
1267+
12401268
JSONEncoder<decltype(json_writer)> json_encoder(json_writer);
12411269

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

1282+
// to json_string
1283+
std::string
1284+
serialize_json_to_string(
1285+
const any& value,
1286+
const schema_version_map* schema_version_targets,
1287+
ErrorStatus* error_status,
1288+
int indent)
1289+
{
1290+
if (indent > 0)
1291+
{
1292+
return serialize_json_to_string_pretty(
1293+
value,
1294+
schema_version_targets,
1295+
error_status,
1296+
indent);
1297+
}
1298+
return serialize_json_to_string_compact(
1299+
value,
1300+
schema_version_targets,
1301+
error_status);
1302+
}
1303+
12541304
bool
12551305
serialize_json_to_file(
12561306
any const& value,

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ foreach(test ${tests_opentime})
1515
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
1616
endforeach()
1717

18-
list(APPEND tests_opentimelineio test_clip test_serializableCollection test_timeline test_track)
18+
list(APPEND tests_opentimelineio test_clip test_serialization test_serializableCollection test_timeline test_track)
1919
foreach(test ${tests_opentimelineio})
2020
add_executable(${test} utils.h utils.cpp ${test}.cpp)
2121

tests/test_serialization.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright Contributors to the OpenTimelineIO project
3+
4+
#include "utils.h"
5+
6+
#include <opentimelineio/clip.h>
7+
#include <opentimelineio/timeline.h>
8+
#include <opentimelineio/track.h>
9+
#include <opentimelineio/serialization.h>
10+
#include <opentimelineio/serializableObject.h>
11+
#include <opentimelineio/serializableObjectWithMetadata.h>
12+
#include <opentimelineio/safely_typed_any.h>
13+
14+
#include <iostream>
15+
#include <string>
16+
17+
namespace otime = opentime::OPENTIME_VERSION;
18+
namespace otio = opentimelineio::OPENTIMELINEIO_VERSION;
19+
20+
int
21+
main(int argc, char** argv)
22+
{
23+
Tests tests;
24+
25+
tests.add_test(
26+
"success with default indent", [] {
27+
otio::SerializableObject::Retainer<otio::Clip> cl =
28+
new otio::Clip();
29+
otio::SerializableObject::Retainer<otio::Track> tr =
30+
new otio::Track();
31+
tr->append_child(cl);
32+
otio::SerializableObject::Retainer<otio::Timeline> tl =
33+
new otio::Timeline();
34+
tl->tracks()->append_child(tr);
35+
36+
otio::ErrorStatus err;
37+
auto output = tl.value->to_json_string(&err, {});
38+
assertFalse(otio::is_error(err));
39+
assertEqual(output.c_str(), R"CONTENT({
40+
"OTIO_SCHEMA": "Timeline.1",
41+
"metadata": {},
42+
"name": "",
43+
"global_start_time": null,
44+
"tracks": {
45+
"OTIO_SCHEMA": "Stack.1",
46+
"metadata": {},
47+
"name": "tracks",
48+
"source_range": null,
49+
"effects": [],
50+
"markers": [],
51+
"enabled": true,
52+
"children": [
53+
{
54+
"OTIO_SCHEMA": "Track.1",
55+
"metadata": {},
56+
"name": "",
57+
"source_range": null,
58+
"effects": [],
59+
"markers": [],
60+
"enabled": true,
61+
"children": [
62+
{
63+
"OTIO_SCHEMA": "Clip.2",
64+
"metadata": {},
65+
"name": "",
66+
"source_range": null,
67+
"effects": [],
68+
"markers": [],
69+
"enabled": true,
70+
"media_references": {
71+
"DEFAULT_MEDIA": {
72+
"OTIO_SCHEMA": "MissingReference.1",
73+
"metadata": {},
74+
"name": "",
75+
"available_range": null,
76+
"available_image_bounds": null
77+
}
78+
},
79+
"active_media_reference_key": "DEFAULT_MEDIA"
80+
}
81+
],
82+
"kind": "Video"
83+
}
84+
]
85+
}
86+
})CONTENT");
87+
});
88+
89+
tests.add_test(
90+
"success with indent set to 0", [] {
91+
otio::SerializableObject::Retainer<otio::SerializableObjectWithMetadata> so =
92+
new otio::SerializableObjectWithMetadata();
93+
94+
otio::ErrorStatus err;
95+
auto output = so.value->to_json_string(&err, {}, 0);
96+
assertFalse(otio::is_error(err));
97+
assertEqual(output.c_str(), R"CONTENT({"OTIO_SCHEMA":"SerializableObjectWithMetadata.1","metadata":{},"name":""})CONTENT");
98+
});
99+
100+
tests.add_test(
101+
"success with indent set to 2", [] {
102+
otio::SerializableObject::Retainer<otio::SerializableObjectWithMetadata> so =
103+
new otio::SerializableObjectWithMetadata();
104+
105+
otio::ErrorStatus err;
106+
auto output = so.value->to_json_string(&err, {}, 2);
107+
assertFalse(otio::is_error(err));
108+
assertEqual(output.c_str(), R"CONTENT({
109+
"OTIO_SCHEMA": "SerializableObjectWithMetadata.1",
110+
"metadata": {},
111+
"name": ""
112+
})CONTENT");
113+
});
114+
115+
tests.run(argc, argv);
116+
return 0;
117+
}

0 commit comments

Comments
 (0)