-
Notifications
You must be signed in to change notification settings - Fork 378
/
FeatureContainerConverter.cs
51 lines (38 loc) · 1.59 KB
/
FeatureContainerConverter.cs
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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.DotNet.Interactive.CSharpProject;
internal abstract class FeatureContainerConverter<T> : JsonConverter where T : FeatureContainer
{
protected abstract void AddProperties(T result, JObject o);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is T result)
{
var o = new JObject();
AddProperties(result, o);
foreach (var feature in result.Features.Values.OfType<IRunResultFeature>())
{
feature.Apply(result);
}
foreach (var property in result.FeatureProperties.OrderBy(p => p.Name))
{
var jToken = JToken.FromObject(property.Value, serializer);
o.Add(new JProperty(property.Name, jToken));
}
o.WriteTo(writer);
}
else
{
throw new NotSupportedException();
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) =>
throw new NotImplementedException();
public override bool CanRead => false;
public override bool CanWrite => true;
public override bool CanConvert(Type objectType) => objectType == typeof(RunResult);
}