I'm developing an azure function in .Net8 which is a migration from a .net6 in-process function.
The code always breaks while trying to deserialize a json object that comes within the EventGridEvent.Data which triggers the function, the deserialization using System.Text.Json.JsonSerializer.Deserialize works locally but in Azure fails with the message:
The JSON value could not be converted to UpdateRunnersEvent. Path: $ | LineNumber: 0 | BytePositionInLine: 3603.; Src: System.Text.Json; Stack: at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter
1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue) at System.Text.Json.Serialization.JsonConverter1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state) at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan
1 utf8Json, JsonTypeInfo1 jsonTypeInfo, Nullable
1 actualByteCount) at System.Text.Json.JsonSerializer.Deserialize[TValue] at FunctionName(EventGridEvent eventGridEvent) . The code is below:
var options = new JsonSerializerOptions
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString,
ReadCommentHandling = JsonCommentHandling.Skip,
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
UnknownTypeHandling = JsonUnknownTypeHandling.JsonElement,
UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip,
};
var eventObject = System.Text.Json.JsonSerializer.Deserialize<UpdateRunnersEvent> (eventGridEvent.Data, options);
Things that I tried:
Deserializing to a string and serializing back such string to the object, Fails. The json object properties matches the properties of my object class (works locally)
Is there anything else that could be tried? ...