diff --git a/forks/Microsoft.Health.FellowOakDicom/Serialization/JsonDicomConverter.cs b/forks/Microsoft.Health.FellowOakDicom/Serialization/JsonDicomConverter.cs index 751b3f6196..46841f3844 100644 --- a/forks/Microsoft.Health.FellowOakDicom/Serialization/JsonDicomConverter.cs +++ b/forks/Microsoft.Health.FellowOakDicom/Serialization/JsonDicomConverter.cs @@ -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) { diff --git a/src/Microsoft.Health.Dicom.Core.UnitTests/Serialization/JsonDicomConverterExtendedTests.cs b/src/Microsoft.Health.Dicom.Core.UnitTests/Serialization/JsonDicomConverterExtendedTests.cs index 75f1b85176..9e97aa2a14 100644 --- a/src/Microsoft.Health.Dicom.Core.UnitTests/Serialization/JsonDicomConverterExtendedTests.cs +++ b/src/Microsoft.Health.Dicom.Core.UnitTests/Serialization/JsonDicomConverterExtendedTests.cs @@ -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; @@ -302,4 +304,28 @@ public static void GivenDicomJsonDatasetWithInvalidPrivateCreatorDataElement_Whe DicomDataset ds = JsonSerializer.Deserialize(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(json, serializerOptions); + var recoveredString = deserializedDataset.GetValue(DicomTag.Exposure, 0); + Assert.Equal(overflowNumber, recoveredString); + } }