-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordingSerializer.cs
More file actions
60 lines (53 loc) · 2.37 KB
/
Copy pathRecordingSerializer.cs
File metadata and controls
60 lines (53 loc) · 2.37 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace PianoEnhancer
{
internal class RecordingSerializer : JsonConverter<Recording>
{
public override Recording Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
reader.Read();
if(reader.TokenType != JsonTokenType.PropertyName || !reader.ValueTextEquals("Track"))
throw new InvalidDataException("Expected Track");
var track = JsonSerializer.Deserialize<List<Chord>>(ref reader,options);
reader.Read();
if(reader.TokenType != JsonTokenType.PropertyName || !reader.ValueTextEquals("SwitchPoints"))
throw new InvalidDataException("Expected SwitchPoints");
reader.Read();
if(reader.TokenType != JsonTokenType.StartArray) throw new InvalidDataException("Expected start of SwitchPoints list");
reader.Read();
var switchPoints = new Dictionary<int,VoiceComposition>();
var customOptions = new JsonSerializerOptions();
customOptions.Converters.Add(new MidiEventSerializer());
while (reader.TokenType != JsonTokenType.EndArray)
{
var point = reader.GetInt32();
reader.Read();
var voice = JsonSerializer.Deserialize<VoiceComposition>(ref reader,customOptions);
switchPoints[point] = voice;
reader.Read();
}
reader.Read();
return new Recording(track,switchPoints);
}
public override void Write(Utf8JsonWriter writer, Recording value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("Track");
JsonSerializer.Serialize(writer, value.Track, options);
var customOptions = new JsonSerializerOptions();
customOptions.Converters.Add(new MidiEventSerializer());
writer.WriteStartArray("SwitchPoints");
foreach (var (point, voice) in value.VoiceSwitchPoints)
{
writer.WriteNumberValue(point);
JsonSerializer.Serialize(writer,voice,customOptions);
}
writer.WriteEndArray();
writer.WriteEndObject();
}
}
}