Skip to content

Commit

Permalink
Handle OverflowException when converting an VR IS to an integer (#3365)
Browse files Browse the repository at this point in the history
Handle overflow error by saving the value as string
  • Loading branch information
arunmk-ms committed Feb 13, 2024
1 parent c72aa5c commit fa45a83
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ private void WriteJsonAsNumberOrString(Utf8JsonWriter writer, DicomElement elem,
{
numberWriterAction();
}
catch (FormatException)
catch (Exception ex) when (ex is FormatException || ex is OverflowException)
{
if (_numberSerializationMode == NumberSerializationMode.PreferablyAsNumber)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

using System;
using System.Linq;
using System.Text;
using System.Text.Json;
using FellowOakDicom;
using FellowOakDicom.IO.Buffer;
using Microsoft.Health.FellowOakDicom.Serialization;
using Xunit;

Expand Down Expand Up @@ -302,4 +304,28 @@ public static void GivenDicomJsonDatasetWithInvalidPrivateCreatorDataElement_Whe
DicomDataset ds = JsonSerializer.Deserialize<DicomDataset>(json, SerializerOptions);
Assert.NotNull(ds);
}

[Theory]
[InlineData("2147384638123")]
[InlineData("73.8")]
[InlineData("InvalidNumber")]
public static void GivenDatasetWithInvalidOrOverflowNumberForValueRepresentationIS_WhenSerialized_IsDeserializedCorrectly(string overflowNumber)
{
var dicomDataset = new DicomDataset().NotValidated();
dicomDataset.Add(new DicomIntegerString(DicomTag.Exposure, new MemoryByteBuffer(Encoding.ASCII.GetBytes(overflowNumber))));

var serializerOptions = new JsonSerializerOptions
{
Converters =
{
new DicomJsonConverter(autoValidate: false, numberSerializationMode: NumberSerializationMode.PreferablyAsNumber)
}
};

var json = JsonSerializer.Serialize(dicomDataset, serializerOptions);
JsonDocument.Parse(json);
DicomDataset deserializedDataset = JsonSerializer.Deserialize<DicomDataset>(json, serializerOptions);
var recoveredString = deserializedDataset.GetValue<string>(DicomTag.Exposure, 0);
Assert.Equal(overflowNumber, recoveredString);
}
}

0 comments on commit fa45a83

Please sign in to comment.