I have the following class:
public class DeviceInfo
{
public string? ip { get; set; }
public string? deviceKey { get; set; }
public long time { get; set; }
public string? version { get; set; }
public int faceCount { get; set; }
public int personCount { get; set; }
}
On the other hand, I have this json string:
{
"ip": "192.168.0.146",
"deviceKey": "A34CC830D348437A",
"time": "1741375392869",
"version": "1.41.9.7",
"faceCount": "0",
"personCount": "0"
}
When executing:
JsonSerializerOptions? options = customImplementation ? new JsonSerializerOptions
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers = { JsonImplementationAttribute.ReadAttributeFromTypeInfo }
}
} : null;
JsonSerializer.Deserialize<DeviceInfo>(json, options);
I got this error:
Exception: System.Text.Json.JsonException: The JSON value could not be converted to System.Int64. Path: $.time | LineNumber: 0 | BytePositionInLine: 75. ---> System.InvalidOperationException: Cannot get the value of a token type 'String' as a number. at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_ExpectedNumber(JsonTokenType tokenType) at System.Text.Json.Utf8JsonReader.TryGetInt64(Int64& value) at System.Text.Json.Utf8JsonReader.GetInt64() at System.Text.Json.Serialization.Metadata.JsonPropertyInfo
1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter1.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) --- End of inner exception stack trace --- at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex) at System.Text.Json.Serialization.JsonConverter1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state) at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan1 utf8Json, JsonTypeInfo1 jsonTypeInfo, Nullable1 actualByteCount)
at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan1 json, JsonTypeInfo1 jsonTypeInfo) at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options) at Modules.Integration.Extensions.JsonExtensions.ToObject[T](Object o, Boolean customImplementation) in C:\WorkingFolder\Projects\Desytec\Development\Desytec-Security-Platform\Web\Modules\Modules.Integration\Extensions\JsonExtensions.cs:line 29 at AdmsServer.Services.PassDeviceService.SaveOptions(String serialNumber) in C:\WorkingFolder\Projects\Desytec\Development\Desytec-Security-Platform\Web\Devices\AdmsServer\Services\PassDeviceService.cs:line 237 at AdmsServer.Controllers.Drivers.API.PassController.Heartbeat(DeviceInfo device) in C:\WorkingFolder\Projects\Desytec\Development\Desytec-Security-Platform\Web\Devices\AdmsServer\Controllers\Drivers\API\PassController.cs:line 83
What wrong thing do you see here?
Regards
timeis declared as alongin the class, but it's astringin the json.faceCountandpersonCountwill likely fail for the same reason.