diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2025-08-18 21:23:02 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2025-08-18 21:23:02 +0300 |
| commit | bda71b704d17773316b4b08e7dae7e5e536d0d0c (patch) | |
| tree | d46f18a2bcfc5d5d089368c3e0c40cc714c4e29f /Software/Visual_Studio/Tango.Telemetry/Helpers/JsonFlattener.cs | |
| parent | 94fb36e2eb00dfb575a5f5cc18bd377224b126ce (diff) | |
| download | Tango-bda71b704d17773316b4b08e7dae7e5e536d0d0c.tar.gz Tango-bda71b704d17773316b4b08e7dae7e5e536d0d0c.zip | |
Improved Telemetry IoT Destination.
Diffstat (limited to 'Software/Visual_Studio/Tango.Telemetry/Helpers/JsonFlattener.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Telemetry/Helpers/JsonFlattener.cs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Telemetry/Helpers/JsonFlattener.cs b/Software/Visual_Studio/Tango.Telemetry/Helpers/JsonFlattener.cs new file mode 100644 index 000000000..1355a8fc4 --- /dev/null +++ b/Software/Visual_Studio/Tango.Telemetry/Helpers/JsonFlattener.cs @@ -0,0 +1,89 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Reflection; + +namespace Tango.Telemetry.Helpers +{ + 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); + } + } +} |
