@@ -6,131 +6,122 @@ namespace EliteAPI;
66
77public static class JExtensions
88{
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- }
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+ }
2121
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 ( ) ;
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 ( ) ;
2424
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- }
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+ }
4141
4242
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- } ;
43+ // JSONToken -> JsonPath
44+ public static JsonPath ToCustomJsonPath ( this JValue jValue )
45+ {
46+ return jValue . Type switch
47+ {
48+ JTokenType . Integer => new JsonPath
49+ {
50+ Path = NormalizePath ( jValue . Path ) ,
51+ Value = Convert . ToInt32 ( jValue . Value ) ,
52+ Type = JsonType . Number
53+ } ,
54+ JTokenType . Float => new JsonPath
55+ {
56+ Path = NormalizePath ( jValue . Path ) ,
57+ Value = Convert . ToDecimal ( jValue . Value ) ,
58+ Type = JsonType . Decimal
59+ } ,
60+ JTokenType . Uri or JTokenType . Guid or JTokenType . String => new JsonPath
61+ {
62+ Path = NormalizePath ( jValue . Path ) ,
63+ Value = Convert . ToString ( jValue . Value ) ,
64+ Type = JsonType . String
65+ } ,
66+ JTokenType . Boolean => new JsonPath
67+ {
68+ Path = NormalizePath ( jValue . Path ) ,
69+ Value = Convert . ToBoolean ( jValue . Value ) ,
70+ Type = JsonType . Boolean
71+ } ,
72+ JTokenType . Date => new JsonPath
73+ {
74+ Path = NormalizePath ( jValue . Path ) ,
75+ Value = Convert . ToDateTime ( jValue . Value ) ,
76+ Type = JsonType . DateTime
77+ } ,
78+ _ => new JsonPath
79+ {
80+ Path = "" ,
81+ Value = "" ,
82+ Type = JsonType . String
83+ } ,
84+ } ;
85+ }
6286
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- }
87+ #region Helpers
9588
96- #region Helpers
89+ static string NormalizePath ( string rawPath )
90+ {
91+ if ( rawPath . StartsWith ( "['" ) && rawPath . EndsWith ( "']" ) )
92+ return rawPath . Substring ( 2 , rawPath . Length - 4 ) ;
93+ return rawPath ;
94+ }
9795
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- }
96+ static IEnumerable < JValue > GetLeafValuesFromJArray ( JArray jArray )
97+ {
98+ for ( var i = 0 ; i < jArray . Count ; i ++ )
99+ {
100+ foreach ( var result in GetLeafValues ( jArray [ i ] ) )
101+ {
102+ yield return result ;
103+ }
104+ }
105+ }
104106
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- }
107+ static IEnumerable < JValue > GetLeafValuesFromJProperty ( JProperty jProperty )
108+ {
109+ foreach ( var result in GetLeafValues ( jProperty . Value ) )
110+ {
111+ yield return result ;
112+ }
113+ }
115114
116- static IEnumerable < JValue > GetLeafValuesFromJProperty ( JProperty jProperty )
117- {
118- foreach ( var result in GetLeafValues ( jProperty . Value ) )
119- {
120- yield return result ;
121- }
122- }
115+ static IEnumerable < JValue > GetLeafValuesFromJObject ( JObject jObject )
116+ {
117+ foreach ( var jToken in jObject . Children ( ) )
118+ {
119+ foreach ( var result in GetLeafValues ( jToken ) )
120+ {
121+ yield return result ;
122+ }
123+ }
124+ }
123125
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
126+ #endregion
136127}
0 commit comments