Skip to content

Commit 4f280be

Browse files
authored
Merge pull request #392 from Somfic/Thevin
2 parents fa54dc2 + d70186e commit 4f280be

5 files changed

Lines changed: 173 additions & 20 deletions

File tree

EliteAPI.Tests/Flattening.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FluentAssertions;
2+
using Newtonsoft.Json.Linq;
23

34
namespace EliteAPI.Tests;
45

@@ -50,7 +51,7 @@ public void SimpleArray()
5051

5152
paths.Should().BeEquivalentTo(expected);
5253
}
53-
54+
5455
[Test]
5556
public void ArrayWithObject()
5657
{
@@ -506,22 +507,6 @@ public void VeryLongString()
506507
paths.Should().BeEquivalentTo(expected);
507508
}
508509

509-
[Test]
510-
public void NullInArray()
511-
{
512-
var json = """{ "items": [1, null, 3] }""";
513-
var paths = FlattenJson(json);
514-
515-
var expected = new[]
516-
{
517-
new JsonPath("items[0]", 1, JsonType.Number),
518-
new JsonPath("items[1]", 3, JsonType.Number),
519-
new JsonPath("items.Length", 3, JsonType.Number)
520-
};
521-
522-
paths.Should().BeEquivalentTo(expected);
523-
}
524-
525510
[Test]
526511
public void NullInNestedObject()
527512
{
@@ -537,9 +522,22 @@ public void NullInNestedObject()
537522
paths.Should().BeEquivalentTo(expected);
538523
}
539524

540-
private List<JsonPath> FlattenJson(string json)
525+
private static List<JsonPath> FlattenJson(string json)
541526
{
542527
// TODO: call flattening function
543-
return [];
528+
// arrays need Length
529+
// key + localisation
530+
// controls mapping
531+
532+
List<JsonPath> temp = [];
533+
var jToken = JToken.Parse(json);
534+
foreach (var jValue in jToken.GetLeafValues())
535+
{
536+
var jpath = jValue.ToCustomJsonPath();
537+
// Skips values that are null
538+
if (!(string.IsNullOrWhiteSpace(jpath.Path) || string.IsNullOrWhiteSpace(jpath.Path))) temp.Add(jpath);
539+
}
540+
541+
return temp;
544542
}
545-
}
543+
}

EliteAPI/Class1.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Newtonsoft.Json.Linq;
2+
3+
namespace EliteAPI;
4+
5+
public class EliteApi
6+
{
7+
public static void Main()
8+
{
9+
var file = File.ReadAllLines("C:\\Users\\thevi\\Documents\\WORK_SPACE\\EliteAPI\\EliteAPI.Tests\\TestFiles\\Journals\\Journal.2000-01-01T100000.01.log");
10+
11+
}
12+
}

EliteAPI/EliteAPI.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
<OutputType>Exe</OutputType>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
12+
</ItemGroup>
13+
1014
</Project>

EliteAPI/JsonExtensions.cs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}

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)