1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Reflection;
|
---|
4 | using Newtonsoft.Json;
|
---|
5 | using Newtonsoft.Json.Linq;
|
---|
6 | using Newtonsoft.Json.Serialization;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.DefinitionLanguage {
|
---|
9 | internal enum PropertyType : long {
|
---|
10 | Primitive,
|
---|
11 | Reference
|
---|
12 | }
|
---|
13 |
|
---|
14 | [JsonConverter(typeof(BaseConverter))]
|
---|
15 | internal abstract class Property {
|
---|
16 | public string Key { get; set; }
|
---|
17 | public string PropertyType { get; set; }
|
---|
18 |
|
---|
19 | public abstract void Set(object instance);
|
---|
20 |
|
---|
21 | protected PropertyInfo GetInfo(object instance) {
|
---|
22 | var type = instance.GetType();
|
---|
23 | var all = type.GetProperties().Where(x => x.Name == Key);
|
---|
24 | return all.FirstOrDefault(x => x.DeclaringType == type) ?? all.First();
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | internal class PrimitiveProperty : Property {
|
---|
29 | public string Value { get; set; }
|
---|
30 | public PrimitiveProperty() {
|
---|
31 | PropertyType = "Primitive";
|
---|
32 | }
|
---|
33 | public override void Set(object instance) {
|
---|
34 | var info = GetInfo(instance);
|
---|
35 | info.SetValue(instance, Parser.ParseString(Value, info.PropertyType));
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | internal class ReferenceProperty : Property {
|
---|
40 | public string Value { get; set; }
|
---|
41 |
|
---|
42 | public ReferenceProperty() {
|
---|
43 | PropertyType = "Reference";
|
---|
44 | }
|
---|
45 | public override void Set(object instance) {
|
---|
46 | var info = GetInfo(instance);
|
---|
47 | info.SetValue(instance, Parser.GetInstance(Value));
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | internal class ReferenceListProperty : Property {
|
---|
52 | public string[] ReferenceList { get; set; }
|
---|
53 |
|
---|
54 | public override void Set(object instance) {
|
---|
55 | throw new NotImplementedException();
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | //https://stackoverflow.com/questions/20995865/deserializing-json-to-abstract-class
|
---|
60 | public class BaseSpecifiedConcreteClassConverter : DefaultContractResolver {
|
---|
61 | protected override JsonConverter ResolveContractConverter(Type objectType) {
|
---|
62 | if (typeof(Property).IsAssignableFrom(objectType) && !objectType.IsAbstract)
|
---|
63 | return null; // pretend TableSortRuleConvert is not specified (thus avoiding a stack overflow)
|
---|
64 | return base.ResolveContractConverter(objectType);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public class BaseConverter : JsonConverter {
|
---|
69 | static JsonSerializerSettings SpecifiedSubclassConversion = new JsonSerializerSettings() { ContractResolver = new BaseSpecifiedConcreteClassConverter() };
|
---|
70 |
|
---|
71 | public override bool CanConvert(Type objectType) => objectType == typeof(Property);
|
---|
72 |
|
---|
73 | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
|
---|
74 | JObject jo = JObject.Load(reader);
|
---|
75 | switch (jo["PropertyType"].Value<string>()) {
|
---|
76 | case "Primitive":
|
---|
77 | return JsonConvert.DeserializeObject<PrimitiveProperty>(jo.ToString(), SpecifiedSubclassConversion);
|
---|
78 | case "Reference":
|
---|
79 | return JsonConvert.DeserializeObject<ReferenceProperty>(jo.ToString(), SpecifiedSubclassConversion);
|
---|
80 | default:
|
---|
81 | throw new Exception();
|
---|
82 | }
|
---|
83 | throw new NotImplementedException();
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override bool CanWrite => false;
|
---|
87 |
|
---|
88 | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
|
---|
89 | throw new NotImplementedException(); // won't be called because CanWrite returns false
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|