1+ using System . Text . Json ;
12using Newtonsoft ;
23using 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 ++ )
0 commit comments