Skip to content

Commit

Permalink
Fix two issues with JSON downconversion. (#328)
Browse files Browse the repository at this point in the history
* Fix decimal seperator loss, and annotation tracking when performing JSON downconverion

* Add one more test for decimal json conversion list separator
  • Loading branch information
nirosys authored Oct 16, 2023
1 parent cf747d3 commit 41f14c7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ionc/ion_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,10 @@ iERR ion_writer_add_annotation(hWRITER hwriter, iSTRING annotation)
if (!annotation || !annotation->value) FAILWITH(IERR_INVALID_ARG);
if (annotation->length < 0) FAILWITH(IERR_INVALID_ARG);

if (ION_TEXT_WRITER_IS_JSON()) {
SUCCEED();
}

annotation_prev = pwriter->annotation_curr;

IONCHECK(_ion_writer_add_annotation_helper(pwriter, annotation));
Expand All @@ -1115,6 +1119,10 @@ iERR _ion_writer_add_annotation_helper(ION_WRITER *pwriter, ION_STRING *annotati
ASSERT(!ION_STRING_IS_NULL(annotation));
ASSERT(annotation->length >= 0);

if (ION_TEXT_WRITER_IS_JSON()) {
SUCCEED();
}

if (!pwriter->annotations) {
int final_max_annotation_count = (pwriter->options.max_annotation_count > DEFAULT_ANNOTATION_LIMIT)
? pwriter->options.max_annotation_count : DEFAULT_ANNOTATION_LIMIT;
Expand Down
2 changes: 2 additions & 0 deletions ionc/ion_writer_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,8 @@ iERR _ion_writer_text_write_double_json(ION_WRITER *pwriter, double value) {
FAILWITH(IERR_UNRECOGNIZED_FLOAT);
}

IONCHECK(_ion_writer_text_close_value(pwriter));

fail:
return err;
}
Expand Down
39 changes: 39 additions & 0 deletions test/test_ion_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,7 @@ TEST(IonTextDownconvert, IntsFloatsAndDecimals) {
IONJSON_CMP("1.5e0", "1.5");
IONJSON_CMP("1e-5", "1e-05");
IONJSON_CMP("0.1", "0.1");
IONJSON_CMP("[1.0e0, 1.0e0]", "[1,1]");

// Decimals
// Decimal formatting does not follow the same pattern as float formatting.
Expand All @@ -1419,6 +1420,7 @@ TEST(IonTextDownconvert, IntsFloatsAndDecimals) {
IONJSON_CMP("1.5d0", "1.5");
IONJSON_CMP("1d-5", "0.00001");
IONJSON_CMP("1d+5", "1E+5");
IONJSON_CMP("[1d0, 1d0]", "[1,1]");
}

TEST(IonTextDownconvert, Lists) {
Expand Down Expand Up @@ -1465,3 +1467,40 @@ TEST(IonTextDownconvert, BlobsAndClobs) {
IONJSON_CMP("{{ \" \\r \" }}", "\" \\r \""); // Carriage Return
IONJSON_CMP("{{ \" \\t \" }}", "\" \\t \""); // Tab
}

TEST(IonTextDownconvert, Annotations) {
// This test validates that annotations are not included when converting to JSON.
// It also validates a fix where annotations were still being added to the writer's
// annotation list when performing JSON downconverion. Since the annotations were
// not written, the annotation list was not cleared, resulting in the writer potentially
// reaching the max annotation limit. In this test we write 2 values with 8 annotations,
// and validate that both values are serialized correctly, and without annotations.
const char *ion_text = "a::a::a::a::a::a::a::a::'hello'";
const char *json_expected = "\"hello\" \"hello\"";
char json_text[1024];
iERR err = IERR_OK;
hREADER reader = NULL;
hWRITER writer = NULL;
ION_WRITER_OPTIONS woptions = {0};
ION_READER_OPTIONS roptions = {0};

woptions.json_downconvert = TRUE;
memset((void*)json_text, 0, 1024);

IONCHECK(ion_writer_open_buffer(&writer, (BYTE*)json_text, sizeof(json_text), &woptions));

IONCHECK(ion_reader_open_buffer(&reader, (BYTE*)ion_text, strlen(ion_text), &roptions));
IONCHECK(ion_writer_write_all_values(writer, reader));
IONCHECK(ion_reader_close(reader));

IONCHECK(ion_reader_open_buffer(&reader, (BYTE*)ion_text, strlen(ion_text), &roptions));
IONCHECK(ion_writer_write_all_values(writer, reader));

EXPECT_STREQ(json_text, json_expected);
fail:
if (writer != NULL)
ion_writer_close(writer);
if (reader != NULL)
ion_reader_close(reader);
EXPECT_EQ(err, IERR_OK);
}

0 comments on commit 41f14c7

Please sign in to comment.