1717using System . Buffers . Text ;
1818using System . Collections . Generic ;
1919using System . Linq ;
20- using System . Text ;
2120using System . Text . Json ;
2221
2322namespace Hl7 . Fhir . Serialization ;
@@ -189,7 +188,7 @@ private void serializeFhirPrimitiveList(
189188 writeStartArray ( elementName , numNullsMissed , writer ) ;
190189 }
191190
192- SerializePrimitiveValue ( value , writer ) ;
191+ SerializePrimitiveInternal ( value , writer ) ;
193192 }
194193 else
195194 {
@@ -255,7 +254,7 @@ private void serializeFhirPrimitive(string elementName, PrimitiveType value, Utf
255254 {
256255 // Write a property with 'elementName'
257256 writer . WritePropertyName ( elementName ) ;
258- SerializePrimitiveValue ( value , writer ) ;
257+ SerializePrimitiveInternal ( value , writer ) ;
259258 }
260259
261260 if ( ! value . EnumerateElements ( ) . Any ( ) ) return ;
@@ -291,6 +290,17 @@ private void deferSerializeForFilter(string elementName, PrimitiveType value, Ut
291290 writer . WriteRawValue ( buffer . WrittenSpan , skipInputValidation : true ) ;
292291 }
293292
293+ internal void SerializePrimitiveInternal ( PrimitiveType value , Utf8JsonWriter writer )
294+ {
295+ // due to System.Text.Json limitations described in https://github.com/FirelyTeam/firely-net-sdk/issues/3501
296+ // Base64 strings need to be < 125MB, but the overload accepting byte array does not carry such limitation
297+ // Accessing the Value property might result in CodedValidationException, so we need to
298+ if ( value is Base64Binary { JsonValue : string { Length : > 0 } text } )
299+ tryWriteBase64 ( writer , text ) ;
300+ else
301+ SerializePrimitiveValue ( value . JsonValue , writer ) ;
302+ }
303+
294304 /// <summary>
295305 /// Serialize a primitive .NET value that may occur in the POCOs into Json.
296306 /// </summary>
@@ -304,17 +314,12 @@ private void deferSerializeForFilter(string elementName, PrimitiveType value, Ut
304314 /// to be written that fit in .NET's <see cref="decimal"/> type, which may be less
305315 /// precision than required by the FHIR specification (http://hl7.org/fhir/json.html#primitive).
306316 /// </remarks>
307- protected virtual void SerializePrimitiveValue ( PrimitiveType value , Utf8JsonWriter writer )
317+ protected virtual void SerializePrimitiveValue ( object ? value , Utf8JsonWriter writer )
308318 {
309-
310- switch ( value . JsonValue )
319+ switch ( value )
311320 {
312321 case int i32 : writer . WriteNumberValue ( i32 ) ; break ;
313322 case decimal dec : writer . WriteNumberValue ( dec ) ; break ;
314- // due to System.Text.Json limitations described in https://github.com/FirelyTeam/firely-net-sdk/issues/3501
315- // Base64 strings need to be < 125MB, but the overload accepting byte array does not carry such limitation
316- // Accessing the Value property might result in CodedValidationException, so we need to
317- case string s when value is Base64Binary : tryWriteBase64 ( writer , s ) ; break ;
318323 // A little note about trimming and whitespaces. The spec says:
319324 // "(...) In JSON and Turtle whitespace in string values is always significant. Primitive types other than
320325 // string SHALL NOT have leading or trailing whitespace."
0 commit comments