|
| 1 | +using System.Text.Json; |
| 2 | +using Newtonsoft; |
| 3 | +using Newtonsoft.Json.Linq; |
| 4 | + |
| 5 | +namespace EliteAPI; |
| 6 | + |
| 7 | +public static class JExtensions |
| 8 | +{ |
| 9 | + public static IEnumerable<JValue> GetLeafValues(this JToken jToken) |
| 10 | + { |
| 11 | + if (jToken is JValue jvalue) |
| 12 | + { |
| 13 | + yield return jvalue; |
| 14 | + } |
| 15 | + else if (jToken is JArray jArray) |
| 16 | + { |
| 17 | + foreach (var result in GetLeafValuesFromJArray(jArray)) |
| 18 | + { |
| 19 | + yield return result; |
| 20 | + } |
| 21 | + |
| 22 | + // yield return GetLeafValues(JToken.Parse($"{{ {jArray.Path.Replace(".", "")}: {{ 'Length': {jArray.Count} }} }}")).First(); |
| 23 | + yield return GetLeafValuesFromJProperty(new JProperty($"{jArray.Path}.Length", jArray.Count)).First(); |
| 24 | + |
| 25 | + } |
| 26 | + else if (jToken is JProperty jProperty) |
| 27 | + { |
| 28 | + foreach (var result in GetLeafValuesFromJProperty(jProperty)) |
| 29 | + { |
| 30 | + yield return result; |
| 31 | + } |
| 32 | + } |
| 33 | + else if (jToken is JObject jObject) |
| 34 | + { |
| 35 | + foreach (var result in GetLeafValuesFromJObject(jObject)) |
| 36 | + { |
| 37 | + yield return result; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + // JSONToken -> JsonPath |
| 44 | + public static JsonPath ToCustomJsonPath(this JValue jValue) |
| 45 | + { |
| 46 | + switch (jValue.Type) |
| 47 | + { |
| 48 | + case JTokenType.Integer: |
| 49 | + return new JsonPath |
| 50 | + { |
| 51 | + Path = NormalizePath(jValue.Path), |
| 52 | + Value = Convert.ToInt32(jValue.Value), |
| 53 | + Type = JsonType.Number |
| 54 | + }; |
| 55 | + case JTokenType.Float: |
| 56 | + return new JsonPath |
| 57 | + { |
| 58 | + Path = NormalizePath(jValue.Path), |
| 59 | + Value = Convert.ToDecimal(jValue.Value), |
| 60 | + Type = JsonType.Decimal |
| 61 | + }; |
| 62 | + |
| 63 | + case JTokenType.Uri: |
| 64 | + case JTokenType.Guid: |
| 65 | + case JTokenType.String: |
| 66 | + return new JsonPath |
| 67 | + { |
| 68 | + Path = NormalizePath(jValue.Path), |
| 69 | + Value = Convert.ToString(jValue.Value), |
| 70 | + Type = JsonType.String |
| 71 | + }; |
| 72 | + case JTokenType.Boolean: |
| 73 | + return new JsonPath |
| 74 | + { |
| 75 | + Path = NormalizePath(jValue.Path), |
| 76 | + Value = Convert.ToBoolean(jValue.Value), |
| 77 | + Type = JsonType.Boolean |
| 78 | + }; |
| 79 | + case JTokenType.Date: |
| 80 | + return new JsonPath |
| 81 | + { |
| 82 | + Path = NormalizePath(jValue.Path), |
| 83 | + Value = Convert.ToDateTime(jValue.Value), |
| 84 | + Type = JsonType.DateTime |
| 85 | + }; |
| 86 | + default: |
| 87 | + return new JsonPath |
| 88 | + { |
| 89 | + Path = "", |
| 90 | + Value = "", |
| 91 | + Type = JsonType.String |
| 92 | + }; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + #region Helpers |
| 97 | + |
| 98 | + static string NormalizePath(string rawPath) |
| 99 | + { |
| 100 | + if (rawPath.StartsWith("['") && rawPath.EndsWith("']")) |
| 101 | + return rawPath.Substring(2, rawPath.Length - 4); |
| 102 | + return rawPath; |
| 103 | + } |
| 104 | + |
| 105 | + static IEnumerable<JValue> GetLeafValuesFromJArray(JArray jArray) |
| 106 | + { |
| 107 | + for (var i = 0; i < jArray.Count; i++) |
| 108 | + { |
| 109 | + foreach (var result in GetLeafValues(jArray[i])) |
| 110 | + { |
| 111 | + yield return result; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + static IEnumerable<JValue> GetLeafValuesFromJProperty(JProperty jProperty) |
| 117 | + { |
| 118 | + foreach (var result in GetLeafValues(jProperty.Value)) |
| 119 | + { |
| 120 | + yield return result; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + static IEnumerable<JValue> GetLeafValuesFromJObject(JObject jObject) |
| 125 | + { |
| 126 | + foreach (var jToken in jObject.Children()) |
| 127 | + { |
| 128 | + foreach (var result in GetLeafValues(jToken)) |
| 129 | + { |
| 130 | + yield return result; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + #endregion |
| 136 | +} |
0 commit comments