aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Telemetry/JsonFlattener.cs
blob: 6658dcd9fd61472ff505f5a937771481792fbca5 (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 Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Tango.Telemetry
{
    internal static class JsonFlattener
    {
        public static string FlattenObjectToFlatJson(object obj, Formatting format)
        {
            var flat = new JObject();
            FlattenRecursive(obj, flat, prefix: null);
            return flat.ToString(format);
        }

        private static void FlattenRecursive(object obj, JObject target, string prefix)
        {
            if (obj == null)
                return;

            var type = obj.GetType();
            if (type == typeof(JObject))
            {
                foreach (var prop in ((JObject)obj).Properties())
                {
                    FlattenRecursive(prop.Value, target, Combine(prefix, prop.Name));
                }
                return;
            }

            if (obj is JValue jVal)
            {
                target[Combine(prefix, "Value")] = JToken.FromObject(jVal.Value);
                return;
            }

            if (obj is JToken jToken && jToken.Type == JTokenType.Object)
            {
                foreach (var prop in ((JObject)jToken).Properties())
                {
                    FlattenRecursive(prop.Value, target, Combine(prefix, prop.Name));
                }
                return;
            }

            foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (!prop.CanRead) continue;

                var value = prop.GetValue(obj);

                if (value == null) continue;

                var valueType = value.GetType();

                if (IsSimpleType(valueType))
                {
                    target[Combine(prefix, prop.Name)] = JToken.FromObject(value);
                }
                else if (value is IEnumerable enumerable && !(value is string))
                {
                    int index = 0;
                    foreach (var item in enumerable)
                    {
                        FlattenRecursive(item, target, Combine(prefix, $"{prop.Name}_{index}"));
                        index++;
                    }
                }
                else
                {
                    FlattenRecursive(value, target, Combine(prefix, prop.Name));
                }
            }
        }

        private static string Combine(string prefix, string name)
        {
            return string.IsNullOrEmpty(prefix) ? name : $"{prefix}_{name}";
        }

        private static bool IsSimpleType(Type type)
        {
            return type.IsPrimitive || type.IsValueType || type == typeof(string);
        }
    }
}