-
-
Notifications
You must be signed in to change notification settings - Fork 36
JSON parsing #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON parsing #392
Changes from all commits
76592d7
08afa34
97493a4
26f1c15
ee734ec
d70186e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
|
||
| { | ||
| var file = File.ReadAllLines("C:\\Users\\thevi\\Documents\\WORK_SPACE\\EliteAPI\\EliteAPI.Tests\\TestFiles\\Journals\\Journal.2000-01-01T100000.01.log"); | ||
|
|
||
| } | ||
| } | ||
| 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 | ||
| }; | ||
| 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
|
||
| 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 | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.