summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/ControlStyleJsonConverter.cs
blob: c31549b05c07c485e90176669494e3557aa09354 (plain)
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
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using MonoGame.Extended.Collections;
using MonoGame.Extended.Gui.Controls;

namespace MonoGame.Extended.Gui.Serialization
{
    public class ControlStyleJsonConverter : JsonConverter<ControlStyle>
    {
        private readonly Dictionary<string, Type> _controlTypes;
        private const string _typeProperty = "Type";
        private const string _nameProperty = "Name";

        public ControlStyleJsonConverter(params Type[] customControlTypes)
        {
            _controlTypes = typeof(Control)
                .GetTypeInfo()
                .Assembly
                .ExportedTypes
                .Concat(customControlTypes)
                .Where(t => t.GetTypeInfo().IsSubclassOf(typeof(Control)))
                .ToDictionary(t => t.Name);
        }

        /// <inheritdoc />
        public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(ControlStyle);

        /// <inheritdoc />
        public override ControlStyle Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var dictionary = JsonSerializer.Deserialize<Dictionary<string, object>>(ref reader, options);
            var name = dictionary.GetValueOrDefault(_nameProperty) as string;
            var typeName = dictionary.GetValueOrDefault(_typeProperty) as string;

            if (!_controlTypes.TryGetValue(typeName, out Type controlType))
                throw new FormatException("invalid control type: " + typeName);

            var targetType = typeName != null ? controlType : typeof(Control);
            var properties = targetType
                .GetRuntimeProperties()
                .ToDictionary(p => p.Name);
            var style = new ControlStyle(name, targetType);

            foreach (var keyValuePair in dictionary.Where(i => i.Key != _typeProperty))
            {
                var propertyName = keyValuePair.Key;
                var rawValue = keyValuePair.Value;

                PropertyInfo propertyInfo;
                var value = properties.TryGetValue(propertyName, out propertyInfo)
                    ? DeserializeValueAs(rawValue, propertyInfo.PropertyType)
                    : DeserializeValueAs(rawValue, typeof(object));

                style.Add(propertyName, value);
            }

            return style;
        }

        private static object DeserializeValueAs(object value, Type type)
        {
            var json = JsonSerializer.Serialize(value, type);
            return JsonSerializer.Deserialize(json, type);
        }

        /// <inheritdoc />
        public override void Write(Utf8JsonWriter writer, ControlStyle value, JsonSerializerOptions options)
        {
            var style = (ControlStyle)value;
            var dictionary = new Dictionary<string, object> { [_typeProperty] = style.TargetType.Name };

            foreach (var keyValuePair in style)
                dictionary.Add(keyValuePair.Key, keyValuePair.Value);

            JsonSerializer.Serialize(writer, dictionary);
        }






    }
}