C
C#14mo ago
mindhardt

✅ STJ cannot deserialize record struct with TimeOnly

See REPL. Why is this the case and what can I do? This is a json I recieve from a remote api
34 Replies
MODiX
MODiX14mo ago
Hin
REPL Result: Success
record struct ValueWithTime(TimeOnly Time, decimal Value);

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
record struct ValueWithTime(TimeOnly Time, decimal Value);

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
Result: ValueWithTime
{
"time": "00:00:00",
"value": 0
}
{
"time": "00:00:00",
"value": 0
}
Compile: 596.992ms | Execution: 76.192ms | React with ❌ to remove this embed.
Thinker
Thinker14mo ago
I think this is because you can't set properties of structs using reflection
mindhardt
mindhardtOP14mo ago
Hmm
MODiX
MODiX14mo ago
Hin
REPL Result: Success
record ValueWithTime(TimeOnly Time, decimal Value);

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
record ValueWithTime(TimeOnly Time, decimal Value);

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
Result: ValueWithTime
{
"time": "00:00:00",
"value": 0
}
{
"time": "00:00:00",
"value": 0
}
Compile: 577.887ms | Execution: 88.690ms | React with ❌ to remove this embed.
MODiX
MODiX14mo ago
Thinker
REPL Result: Failure
System.Text.Json.JsonSerializer.Deserialize<TimeOnly>("\"12:00\"")
System.Text.Json.JsonSerializer.Deserialize<TimeOnly>("\"12:00\"")
Exception: JsonException
- The JSON value could not be converted to System.TimeOnly. Path: $ | LineNumber: 0 | BytePositionInLine: 7.
- The JSON value could not be converted to System.TimeOnly. Path: $ | LineNumber: 0 | BytePositionInLine: 7.
Compile: 519.984ms | Execution: 35.990ms | React with ❌ to remove this embed.
Thinker
Thinker14mo ago
You'll have to make your own converter I think
mindhardt
mindhardtOP14mo ago
Damn it
MODiX
MODiX14mo ago
Hin
REPL Result: Success
TimeOnly.Parse("12:00")
TimeOnly.Parse("12:00")
Result: TimeOnly
12:00:00
12:00:00
Compile: 493.913ms | Execution: 41.636ms | React with ❌ to remove this embed.
mindhardt
mindhardtOP14mo ago
Ok, gonna try
MODiX
MODiX14mo ago
Hin
REPL Result: Success
record ValueWithTime(TimeSpan Time, decimal Value);

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
record ValueWithTime(TimeSpan Time, decimal Value);

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
Result: ValueWithTime
{
"time": "00:00:00",
"value": 0
}
{
"time": "00:00:00",
"value": 0
}
Compile: 649.864ms | Execution: 83.945ms | React with ❌ to remove this embed.
MODiX
MODiX14mo ago
Hin
REPL Result: Success
record ValueWithTime
{
public TimeOnly Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
record ValueWithTime
{
public TimeOnly Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
Result: ValueWithTime
{
"time": "00:00:00",
"value": 0
}
{
"time": "00:00:00",
"value": 0
}
Compile: 653.156ms | Execution: 83.615ms | React with ❌ to remove this embed.
MODiX
MODiX14mo ago
Hin
REPL Result: Success
record ValueWithTime
{
public TimeSpan Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
record ValueWithTime
{
public TimeSpan Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
Result: ValueWithTime
{
"time": "00:00:00",
"value": 0
}
{
"time": "00:00:00",
"value": 0
}
Compile: 576.978ms | Execution: 77.966ms | React with ❌ to remove this embed.
MODiX
MODiX14mo ago
Hin
REPL Result: Success
record ValueWithTime
{
public TimeSpan Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00:00\",\"value\":1.5}")
record ValueWithTime
{
public TimeSpan Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00:00\",\"value\":1.5}")
Result: ValueWithTime
{
"time": "00:00:00",
"value": 0
}
{
"time": "00:00:00",
"value": 0
}
Compile: 610.089ms | Execution: 80.949ms | React with ❌ to remove this embed.
MODiX
MODiX14mo ago
Hin
REPL Result: Success
record ValueWithTime
{
public TimeOnly Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00:00\",\"value\":1.5}")
record ValueWithTime
{
public TimeOnly Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00:00\",\"value\":1.5}")
Result: ValueWithTime
{
"time": "00:00:00",
"value": 0
}
{
"time": "00:00:00",
"value": 0
}
Compile: 628.868ms | Execution: 81.249ms | React with ❌ to remove this embed.
Pobiega
Pobiega14mo ago
public class DateOnlyConverter : JsonConverter<DateOnly>
{
private readonly string serializationFormat;

public DateOnlyConverter() : this(null)
{
}

public DateOnlyConverter(string? serializationFormat)
{
this.serializationFormat = serializationFormat ?? "dd/MM/yyyy";
}

public override DateOnly Read(ref Utf8JsonReader reader,
Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();
return DateOnly.Parse(value!);
}

public override void Write(Utf8JsonWriter writer, DateOnly value,
JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString(serializationFormat));
}

//Usage
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.Converters.Add(new DateOnlyConverter());
public class DateOnlyConverter : JsonConverter<DateOnly>
{
private readonly string serializationFormat;

public DateOnlyConverter() : this(null)
{
}

public DateOnlyConverter(string? serializationFormat)
{
this.serializationFormat = serializationFormat ?? "dd/MM/yyyy";
}

public override DateOnly Read(ref Utf8JsonReader reader,
Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();
return DateOnly.Parse(value!);
}

public override void Write(Utf8JsonWriter writer, DateOnly value,
JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString(serializationFormat));
}

//Usage
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.Converters.Add(new DateOnlyConverter());
this is for DateOnly, but you get the idea this should already be included in .net 7 thou
mindhardt
mindhardtOP14mo ago
Thats what I thought, but no eta
MODiX
MODiX14mo ago
Hin
REPL Result: Success
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
public TimeOnly Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
public TimeOnly Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
Result: ValueWithTime
{
"time": "00:00",
"value": 0
}
{
"time": "00:00",
"value": 0
}
Compile: 702.348ms | Execution: 106.659ms | React with ❌ to remove this embed.
mindhardt
mindhardtOP14mo ago
Fuck you too, stj
MODiX
MODiX14mo ago
Hin
REPL Result: Failure
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
public required TimeOnly Time { get; set; }
public required decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
public required TimeOnly Time { get; set; }
public required decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
Exception: JsonException
- JSON deserialization for type 'Submission#0+ValueWithTime' was missing required properties, including the following: Time, Value
- JSON deserialization for type 'Submission#0+ValueWithTime' was missing required properties, including the following: Time, Value
Compile: 702.211ms | Execution: 90.297ms | React with ❌ to remove this embed.
mindhardt
mindhardtOP14mo ago
And why tf they are missing
MODiX
MODiX14mo ago
Hin
REPL Result: Success
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
public required TimeOnly Time { get; set; }
public required decimal Value { get; set; }
}

var vwt = new ValueWithTime()
{
Time = TimeOnly.Parse("12:00:00"), Value = 1.5m
};
System.Text.Json.JsonSerializer.Serialize(vwt)
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
public required TimeOnly Time { get; set; }
public required decimal Value { get; set; }
}

var vwt = new ValueWithTime()
{
Time = TimeOnly.Parse("12:00:00"), Value = 1.5m
};
System.Text.Json.JsonSerializer.Serialize(vwt)
Result: string
{"Time":"12:00","Value":1.5}
{"Time":"12:00","Value":1.5}
Compile: 827.257ms | Execution: 122.081ms | React with ❌ to remove this embed.
mindhardt
mindhardtOP14mo ago
Wait, is it...
MODiX
MODiX14mo ago
Hin
REPL Result: Success
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
public TimeOnly Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"Time\":\"12:00\",\"Value\":1.5}")
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
public TimeOnly Time { get; set; }
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"Time\":\"12:00\",\"Value\":1.5}")
Result: ValueWithTime
{
"time": "12:00",
"value": 1.5
}
{
"time": "12:00",
"value": 1.5
}
Compile: 765.091ms | Execution: 103.975ms | React with ❌ to remove this embed.
mindhardt
mindhardtOP14mo ago
The fuck... Default stj cannot comprehend camelCase???? Brb gonna kill myself (joke)
Thinker
Thinker14mo ago
You can configure it to use camelCase
mindhardt
mindhardtOP14mo ago
I know I thought it is the default Maybe it is in asp controllers..?
Thinker
Thinker14mo ago
I think the default ASP.NET serializer probably overrides the casing
MODiX
MODiX14mo ago
Hin
REPL Result: Success
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
[System.Text.Json.Serialization.JsonPropertyName("time")]
public TimeOnly Time { get; set; }
[System.Text.Json.Serialization.JsonPropertyName("value")]
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
[System.Text.Json.Serialization.JsonPropertyName("time")]
public TimeOnly Time { get; set; }
[System.Text.Json.Serialization.JsonPropertyName("value")]
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
Result: ValueWithTime
{
"time": "12:00",
"value": 1.5
}
{
"time": "12:00",
"value": 1.5
}
Compile: 703.383ms | Execution: 98.088ms | React with ❌ to remove this embed.
MODiX
MODiX14mo ago
Hin
REPL Result: Failure
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonPropertyName("time")]
public TimeOnly Time { get; set; }
[System.Text.Json.Serialization.JsonPropertyName("value")]
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime
{
[System.Text.Json.Serialization.JsonPropertyName("time")]
public TimeOnly Time { get; set; }
[System.Text.Json.Serialization.JsonPropertyName("value")]
public decimal Value { get; set; }
}

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
Exception: JsonException
- The JSON value could not be converted to System.TimeOnly. Path: $.time | LineNumber: 0 | BytePositionInLine: 15.
- The JSON value could not be converted to System.TimeOnly. Path: $.time | LineNumber: 0 | BytePositionInLine: 15.
Compile: 728.191ms | Execution: 96.349ms | React with ❌ to remove this embed.
mindhardt
mindhardtOP14mo ago
Ok so I still need the converter
MODiX
MODiX14mo ago
Hin
REPL Result: Failure
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime(
[property: System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
[property: System.Text.Json.Serialization.JsonPropertyName("time")]
TimeOnly Time,
[property: System.Text.Json.Serialization.JsonPropertyName("value")]
decimal Value)

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime(
[property: System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
[property: System.Text.Json.Serialization.JsonPropertyName("time")]
TimeOnly Time,
[property: System.Text.Json.Serialization.JsonPropertyName("value")]
decimal Value)

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
Exception: CompilationErrorException
- { expected
- } expected
- { expected
- } expected
Compile: 789.020ms | Execution: 0.000ms | React with ❌ to remove this embed.
mindhardt
mindhardtOP14mo ago
where
MODiX
MODiX14mo ago
Hin
REPL Result: Success
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime(
[property: System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
[property: System.Text.Json.Serialization.JsonPropertyName("time")]
TimeOnly Time,
[property: System.Text.Json.Serialization.JsonPropertyName("value")]
decimal Value);

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
class NsTimeOnlyConverter : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> TimeOnly.ParseExact(reader.GetString()!, "HH:mm", CultureInfo.InvariantCulture);

public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("HH:mm", CultureInfo.InvariantCulture));
}

record ValueWithTime(
[property: System.Text.Json.Serialization.JsonConverter(typeof(NsTimeOnlyConverter))]
[property: System.Text.Json.Serialization.JsonPropertyName("time")]
TimeOnly Time,
[property: System.Text.Json.Serialization.JsonPropertyName("value")]
decimal Value);

System.Text.Json.JsonSerializer.Deserialize<ValueWithTime>("{\"time\":\"12:00\",\"value\":1.5}")
Result: ValueWithTime
{
"time": "12:00",
"value": 1.5
}
{
"time": "12:00",
"value": 1.5
}
Compile: 772.635ms | Execution: 99.206ms | React with ❌ to remove this embed.
mindhardt
mindhardtOP14mo ago
Neat
Want results from more Discord servers?
Add your server