-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathScriptCollectionConverter.cs
More file actions
42 lines (35 loc) · 1.06 KB
/
ScriptCollectionConverter.cs
File metadata and controls
42 lines (35 loc) · 1.06 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
namespace RunScript.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;
internal class ScriptCollectionConverter : JsonConverter<ScriptCollection>
{
public override ScriptCollection Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
var scripts = new ScriptCollection();
using (var jsonDoc = JsonDocument.ParseValue(ref reader))
using (var jsonScripts = jsonDoc.RootElement.EnumerateObject())
{
foreach (var script in jsonScripts)
{
scripts.Add(script.Name, script.Value.ToString());
}
}
return scripts;
}
public override void Write(
Utf8JsonWriter writer,
ScriptCollection value,
JsonSerializerOptions options)
{
writer.WriteStartObject();
foreach (var element in value)
{
writer.WritePropertyName(element.Name);
writer.WriteStringValue(element.Script);
}
writer.WriteEndObject();
}
}