-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlFormatterJsonExtensions.cs
More file actions
79 lines (68 loc) · 3.14 KB
/
Copy pathHtmlFormatterJsonExtensions.cs
File metadata and controls
79 lines (68 loc) · 3.14 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#nullable enable
namespace EfMigrationDiff.Formatters;
/// <summary>
/// Provides JSON serialization and deserialization extensions for <see cref="HtmlFormatter"/>.
/// </summary>
public static class HtmlFormatterJsonExtensions
{
private static readonly System.Text.Json.JsonSerializerOptions _jsonOptions = new(System.Text.Json.JsonSerializerOptions.Default)
{
PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,
WriteIndented = false,
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
};
/// <summary>
/// Serializes the <see cref="HtmlFormatter"/> instance to a JSON string.
/// </summary>
/// <param name="value">The formatter instance to serialize.</param>
/// <param name="indented">Whether to format the JSON with indentation for readability.</param>
/// <returns>A JSON string representation of the formatter.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="value"/> is null.</exception>
public static string ToJson(this HtmlFormatter value, bool indented = false)
{
ArgumentNullException.ThrowIfNull(value);
var options = indented
? new System.Text.Json.JsonSerializerOptions(_jsonOptions) { WriteIndented = true }
: _jsonOptions;
return System.Text.Json.JsonSerializer.Serialize(value, options);
}
/// <summary>
/// Deserializes a JSON string to an <see cref="HtmlFormatter"/> instance.
/// </summary>
/// <param name="json">The JSON string to deserialize.</param>
/// <returns>The deserialized <see cref="HtmlFormatter"/> instance, or null if the JSON is null or empty.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="json"/> is null.</exception>
/// <exception cref="System.Text.Json.JsonException">Thrown when the JSON is invalid or cannot be deserialized.</exception>
public static HtmlFormatter? FromJson(string json)
{
ArgumentNullException.ThrowIfNull(json);
return string.IsNullOrWhiteSpace(json)
? null
: System.Text.Json.JsonSerializer.Deserialize<HtmlFormatter>(json, _jsonOptions);
}
/// <summary>
/// Attempts to deserialize a JSON string to an <see cref="HtmlFormatter"/> instance.
/// </summary>
/// <param name="json">The JSON string to deserialize.</param>
/// <param name="value">Receives the deserialized instance if successful.</param>
/// <returns>True if deserialization succeeded; otherwise, false.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="json"/> is null.</exception>
public static bool TryFromJson(string json, out HtmlFormatter? value)
{
ArgumentNullException.ThrowIfNull(json);
value = null;
if (string.IsNullOrWhiteSpace(json))
{
return false;
}
try
{
value = System.Text.Json.JsonSerializer.Deserialize<HtmlFormatter>(json, _jsonOptions);
return true;
}
catch (System.Text.Json.JsonException)
{
return false;
}
}
}