-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVersionJson.cs
More file actions
33 lines (30 loc) · 1.19 KB
/
VersionJson.cs
File metadata and controls
33 lines (30 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System.Text.Json;
using System.Text.Json.Serialization;
namespace PolyMod.Json;
/// <summary>
/// Converts a <see cref="Version"/> to and from a JSON string.
/// </summary>
public class VersionJson : JsonConverter<Version>
{
/// <summary>
/// Reads and converts the JSON to a <see cref="Version"/>.
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="typeToConvert">The type to convert.</param>
/// <param name="options">An object that specifies serialization options to use.</param>
/// <returns>The converted value.</returns>
public override Version? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new(reader.GetString()!);
}
/// <summary>
/// Writes a specified value as JSON.
/// </summary>
/// <param name="writer">The writer to write to.</param>
/// <param name="value">The value to convert to JSON.</param>
/// <param name="options">An object that specifies serialization options to use.</param>
public override void Write(Utf8JsonWriter writer, Version value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}