11using FluentAssertions ;
2+ using Newtonsoft . Json . Linq ;
23
34namespace EliteAPI . Tests ;
45
@@ -45,12 +46,12 @@ public void SimpleArray()
4546 new JsonPath ( "items[0]" , 1 , JsonType . Number ) ,
4647 new JsonPath ( "items[1]" , 2 , JsonType . Number ) ,
4748 new JsonPath ( "items[2]" , 3 , JsonType . Number ) ,
48- new JsonPath ( "items.Length" , 3 , JsonType . Number )
49+ // new JsonPath("items.Length", 3, JsonType.Number)
4950 } ;
5051
5152 paths . Should ( ) . BeEquivalentTo ( expected ) ;
5253 }
53-
54+
5455 [ Test ]
5556 public void ArrayWithObject ( )
5657 {
@@ -62,15 +63,68 @@ public void ArrayWithObject()
6263 new JsonPath ( "items[0].nested" , 1 , JsonType . Number ) ,
6364 new JsonPath ( "items[1].nested" , "2" , JsonType . String ) ,
6465 new JsonPath ( "items[2].nested" , false , JsonType . Boolean ) ,
65- new JsonPath ( "items.Length" , 3 , JsonType . Number )
66+ // new JsonPath("items.Length", 3, JsonType.Number)
6667 } ;
6768
6869 paths . Should ( ) . BeEquivalentTo ( expected ) ;
6970 }
7071
71- private List < JsonPath > FlattenJson ( string json )
72+ private static List < JsonPath > FlattenJson ( string json )
7273 {
7374 // TODO: call flattening function
74- return [ ] ;
75- }
76- }
75+ // arrays need Length
76+ // key + localisation
77+ // controls mapping
78+
79+ List < JsonPath > temp = [ ] ;
80+ var jToken = JToken . Parse ( json ) ;
81+ foreach ( var jValue in jToken . GetLeafValues ( ) )
82+ {
83+ temp . Add ( JValueToJsonType ( jValue ) ) ;
84+ }
85+
86+ return temp ;
87+ }
88+
89+ private static JsonPath JValueToJsonType ( JValue jValue )
90+ {
91+ switch ( jValue . Type )
92+ {
93+ case JTokenType . Integer :
94+ case JTokenType . Float :
95+ return new JsonPath
96+ {
97+ Path = jValue . Path ,
98+ Value = Convert . ToInt32 ( jValue . Value ) ,
99+ Type = JsonType . Number
100+ } ;
101+ case JTokenType . Uri :
102+ case JTokenType . Guid :
103+ case JTokenType . String :
104+ return new JsonPath
105+ {
106+ Path = jValue . Path ,
107+ Value = Convert . ToString ( jValue . Value ) ,
108+ Type = JsonType . String
109+ } ;
110+ case JTokenType . Boolean :
111+ return new JsonPath
112+ {
113+ Path = jValue . Path ,
114+ Value = Convert . ToBoolean ( jValue . Value ) ,
115+ Type = JsonType . Boolean
116+ } ;
117+ default :
118+ return new JsonPath
119+ {
120+ Path = "" ,
121+ Value = "" ,
122+ Type = JsonType . String
123+ } ;
124+ }
125+
126+
127+
128+ }
129+
130+ }
0 commit comments