Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions EliteAPI.Tests/Flattening.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using Newtonsoft.Json.Linq;

namespace EliteAPI.Tests;

Expand Down Expand Up @@ -50,7 +51,7 @@ public void SimpleArray()

paths.Should().BeEquivalentTo(expected);
}

[Test]
public void ArrayWithObject()
{
Expand Down Expand Up @@ -506,22 +507,6 @@ public void VeryLongString()
paths.Should().BeEquivalentTo(expected);
}

[Test]
public void NullInArray()
{
var json = """{ "items": [1, null, 3] }""";
var paths = FlattenJson(json);

var expected = new[]
{
new JsonPath("items[0]", 1, JsonType.Number),
new JsonPath("items[1]", 3, JsonType.Number),
new JsonPath("items.Length", 3, JsonType.Number)
};

paths.Should().BeEquivalentTo(expected);
}

[Test]
public void NullInNestedObject()
{
Expand All @@ -537,9 +522,22 @@ public void NullInNestedObject()
paths.Should().BeEquivalentTo(expected);
}

private List<JsonPath> FlattenJson(string json)
private static List<JsonPath> FlattenJson(string json)
{
// TODO: call flattening function
return [];
// arrays need Length
// key + localisation
// controls mapping

List<JsonPath> temp = [];
var jToken = JToken.Parse(json);
foreach (var jValue in jToken.GetLeafValues())
{
var jpath = jValue.ToCustomJsonPath();
// Skips values that are null
if (!(string.IsNullOrWhiteSpace(jpath.Path) || string.IsNullOrWhiteSpace(jpath.Path))) temp.Add(jpath);
}

return temp;
}
}
}
12 changes: 12 additions & 0 deletions EliteAPI/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json.Linq;

namespace EliteAPI;

public class EliteApi
{
public static void Main()

Check warning on line 7 in EliteAPI/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The entry point of the program is global code; ignoring 'EliteApi.Main()' entry point.

Check warning on line 7 in EliteAPI/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The entry point of the program is global code; ignoring 'EliteApi.Main()' entry point.

Check warning on line 7 in EliteAPI/Class1.cs

View workflow job for this annotation

GitHub Actions / test

The entry point of the program is global code; ignoring 'EliteApi.Main()' entry point.

Check warning on line 7 in EliteAPI/Class1.cs

View workflow job for this annotation

GitHub Actions / test

The entry point of the program is global code; ignoring 'EliteApi.Main()' entry point.

Check warning on line 7 in EliteAPI/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The entry point of the program is global code; ignoring 'EliteApi.Main()' entry point.

Check warning on line 7 in EliteAPI/Class1.cs

View workflow job for this annotation

GitHub Actions / build

The entry point of the program is global code; ignoring 'EliteApi.Main()' entry point.

Check warning on line 7 in EliteAPI/Class1.cs

View workflow job for this annotation

GitHub Actions / test

The entry point of the program is global code; ignoring 'EliteApi.Main()' entry point.

Check warning on line 7 in EliteAPI/Class1.cs

View workflow job for this annotation

GitHub Actions / test

The entry point of the program is global code; ignoring 'EliteApi.Main()' entry point.
{
var file = File.ReadAllLines("C:\\Users\\thevi\\Documents\\WORK_SPACE\\EliteAPI\\EliteAPI.Tests\\TestFiles\\Journals\\Journal.2000-01-01T100000.01.log");

}
}
4 changes: 4 additions & 0 deletions EliteAPI/EliteAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>

</Project>
136 changes: 136 additions & 0 deletions EliteAPI/JsonExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System.Text.Json;
using Newtonsoft;
using Newtonsoft.Json.Linq;

namespace EliteAPI;

public static class JExtensions
{
public static IEnumerable<JValue> GetLeafValues(this JToken jToken)
{
if (jToken is JValue jvalue)
{
yield return jvalue;
}
else if (jToken is JArray jArray)
{
foreach (var result in GetLeafValuesFromJArray(jArray))
{
yield return result;
}

// yield return GetLeafValues(JToken.Parse($"{{ {jArray.Path.Replace(".", "")}: {{ 'Length': {jArray.Count} }} }}")).First();
yield return GetLeafValuesFromJProperty(new JProperty($"{jArray.Path}.Length", jArray.Count)).First();

}
else if (jToken is JProperty jProperty)
{
foreach (var result in GetLeafValuesFromJProperty(jProperty))
{
yield return result;
}
}
else if (jToken is JObject jObject)
{
foreach (var result in GetLeafValuesFromJObject(jObject))
{
yield return result;
}
}
}


// JSONToken -> JsonPath
public static JsonPath ToCustomJsonPath(this JValue jValue)
{
switch (jValue.Type)
{
case JTokenType.Integer:
return new JsonPath
{
Path = NormalizePath(jValue.Path),
Value = Convert.ToInt32(jValue.Value),
Type = JsonType.Number
};
Comment thread
Somfic marked this conversation as resolved.
case JTokenType.Float:
return new JsonPath
{
Path = NormalizePath(jValue.Path),
Value = Convert.ToDecimal(jValue.Value),
Type = JsonType.Decimal
};

case JTokenType.Uri:
case JTokenType.Guid:
case JTokenType.String:
return new JsonPath
{
Path = NormalizePath(jValue.Path),
Value = Convert.ToString(jValue.Value),

Check warning on line 69 in EliteAPI/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 69 in EliteAPI/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 69 in EliteAPI/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference assignment.

Check warning on line 69 in EliteAPI/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference assignment.

Check warning on line 69 in EliteAPI/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 69 in EliteAPI/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.

Check warning on line 69 in EliteAPI/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference assignment.

Check warning on line 69 in EliteAPI/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference assignment.
Type = JsonType.String
};
case JTokenType.Boolean:
return new JsonPath
{
Path = NormalizePath(jValue.Path),
Value = Convert.ToBoolean(jValue.Value),
Type = JsonType.Boolean
};
case JTokenType.Date:
return new JsonPath
{
Path = NormalizePath(jValue.Path),
Value = Convert.ToDateTime(jValue.Value),
Type = JsonType.DateTime
};
default:
return new JsonPath
{
Path = "",
Value = "",
Type = JsonType.String
};
}
}

#region Helpers

static string NormalizePath(string rawPath)
{
if (rawPath.StartsWith("['") && rawPath.EndsWith("']"))
return rawPath.Substring(2, rawPath.Length - 4);
return rawPath;
}

static IEnumerable<JValue> GetLeafValuesFromJArray(JArray jArray)
{
for (var i = 0; i < jArray.Count; i++)
{
foreach (var result in GetLeafValues(jArray[i]))
{
yield return result;
}
}
}

static IEnumerable<JValue> GetLeafValuesFromJProperty(JProperty jProperty)
{
foreach (var result in GetLeafValues(jProperty.Value))
{
yield return result;
}
}

static IEnumerable<JValue> GetLeafValuesFromJObject(JObject jObject)
{
foreach (var jToken in jObject.Children())
{
foreach (var result in GetLeafValues(jToken))
{
yield return result;
}
}
}

#endregion
}
3 changes: 3 additions & 0 deletions EliteAPI/JsonPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ namespace EliteAPI;

public readonly struct JsonPath(string path, dynamic value, JsonType type)
{

public string Path { get; init; } = path;

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

public JsonType Type { get; init; } = type;


}

public enum JsonType
Expand Down
Loading