Skip to content

Commit d70186e

Browse files
committed
I forgor :3
1 parent ee734ec commit d70186e

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

EliteAPI/JsonExtensions.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json;
12
using Newtonsoft;
23
using Newtonsoft.Json.Linq;
34

@@ -18,7 +19,8 @@ public static IEnumerable<JValue> GetLeafValues(this JToken jToken)
1819
yield return result;
1920
}
2021

21-
yield return GetLeafValues(JToken.Parse($"{{ 'items': {{ 'Length': {jArray.Count} }} }}")).First();
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();
2224

2325
}
2426
else if (jToken is JProperty jProperty)
@@ -44,29 +46,43 @@ public static JsonPath ToCustomJsonPath(this JValue jValue)
4446
switch (jValue.Type)
4547
{
4648
case JTokenType.Integer:
47-
case JTokenType.Float:
4849
return new JsonPath
4950
{
50-
Path = jValue.Path,
51+
Path = NormalizePath(jValue.Path),
5152
Value = Convert.ToInt32(jValue.Value),
5253
Type = JsonType.Number
5354
};
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+
5463
case JTokenType.Uri:
5564
case JTokenType.Guid:
5665
case JTokenType.String:
5766
return new JsonPath
5867
{
59-
Path = jValue.Path,
68+
Path = NormalizePath(jValue.Path),
6069
Value = Convert.ToString(jValue.Value),
6170
Type = JsonType.String
6271
};
6372
case JTokenType.Boolean:
6473
return new JsonPath
6574
{
66-
Path = jValue.Path,
75+
Path = NormalizePath(jValue.Path),
6776
Value = Convert.ToBoolean(jValue.Value),
6877
Type = JsonType.Boolean
6978
};
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+
};
7086
default:
7187
return new JsonPath
7288
{
@@ -79,6 +95,13 @@ public static JsonPath ToCustomJsonPath(this JValue jValue)
7995

8096
#region Helpers
8197

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+
82105
static IEnumerable<JValue> GetLeafValuesFromJArray(JArray jArray)
83106
{
84107
for (var i = 0; i < jArray.Count; i++)

EliteAPI/JsonPath.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ namespace EliteAPI;
22

33
public readonly struct JsonPath(string path, dynamic value, JsonType type)
44
{
5+
56
public string Path { get; init; } = path;
67

78
public dynamic Value { get; init; } = value;
89

910
public JsonType Type { get; init; } = type;
11+
12+
1013
}
1114

1215
public enum JsonType

0 commit comments

Comments
 (0)