22using System . Collections . Generic ;
33using System . Dynamic ;
44using System . Linq ;
5+ using System . Text . Json ;
6+ using System . Text . Json . Nodes ;
57using System . Threading . Tasks ;
6- using Newtonsoft . Json ;
7- using Newtonsoft . Json . Linq ;
88using Xunit ;
99
1010namespace JsonFlatFileDataStore . Test ;
@@ -146,7 +146,7 @@ public async Task UpdateOneAsync_TypedUser_WrongCase()
146146
147147 var collection2 = store2 . GetCollection < User > ( "users2" ) ;
148148 await collection2 . UpdateOneAsync ( x => x . Id == 0 , new { name = "new value" } ) ;
149- await collection2 . UpdateOneAsync ( x => x . Id == 1 , JToken . Parse ( "{ name: \" new value 2\" } " ) ) ;
149+ await collection2 . UpdateOneAsync ( x => x . Id == 1 , JsonNode . Parse ( "{ \" name\" : \" new value 2\" } " ) ) ;
150150
151151 var store3 = new DataStore ( newFilePath ) ;
152152
@@ -207,9 +207,9 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleArray()
207207 Id = Guid . NewGuid ( ) . ToString ( ) ,
208208 Type = "empty" ,
209209 Fragments = new List < string >
210- {
211- Guid . NewGuid ( ) . ToString ( )
212- }
210+ {
211+ Guid . NewGuid ( ) . ToString ( )
212+ }
213213 } ;
214214
215215 var insertResult = collection . InsertOne ( newModel ) ;
@@ -224,10 +224,10 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleArray()
224224 {
225225 Type = "filled" ,
226226 Fragments = new List < string >
227- {
228- Guid . NewGuid ( ) . ToString ( ) ,
229- Guid . NewGuid ( ) . ToString ( )
230- }
227+ {
228+ Guid . NewGuid ( ) . ToString ( ) ,
229+ Guid . NewGuid ( ) . ToString ( )
230+ }
231231 } ;
232232
233233 await collection2 . UpdateOneAsync ( e => e . Id == newModel . Id , updateData ) ;
@@ -258,9 +258,9 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleIntArray()
258258 Id = Guid . NewGuid ( ) . ToString ( ) ,
259259 Type = "empty" ,
260260 Fragments = new List < int >
261- {
262- 1
263- }
261+ {
262+ 1
263+ }
264264 } ;
265265
266266 var insertResult = collection . InsertOne ( newModel ) ;
@@ -275,10 +275,10 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleIntArray()
275275 {
276276 Type = "filled" ,
277277 Fragments = new List < int >
278- {
279- 2 ,
280- 3
281- }
278+ {
279+ 2 ,
280+ 3
281+ }
282282 } ;
283283
284284 await collection2 . UpdateOneAsync ( e => e . Id == newModel . Id , updateData ) ;
@@ -323,10 +323,10 @@ public async Task UpdateOneAsync_TypedModel_NestedArrays()
323323 {
324324 Type = "filled" ,
325325 NestedLists = new List < List < int > >
326- {
327- null ,
328- new List < int > { 4 } ,
329- }
326+ {
327+ null ,
328+ new List < int > { 4 } ,
329+ }
330330 } ;
331331
332332 await collection2 . UpdateOneAsync ( e => e . Id == newModel . Id , updateData ) ;
@@ -411,10 +411,10 @@ public async Task UpdateManyAsync_DynamicUser()
411411
412412 var newUsers = new [ ]
413413 {
414- new { id = 20 , name = "A1" , age = 55 } ,
415- new { id = 21 , name = "A2" , age = 55 } ,
416- new { id = 22 , name = "A3" , age = 55 }
417- } ;
414+ new { id = 20 , name = "A1" , age = 55 } ,
415+ new { id = 21 , name = "A2" , age = 55 } ,
416+ new { id = 22 , name = "A3" , age = 55 }
417+ } ;
418418
419419 await collection . InsertManyAsync ( newUsers ) ;
420420
@@ -449,19 +449,20 @@ public async Task UpdateManyAsync_JsonUser()
449449 Assert . Equal ( 3 , collection . Count ) ;
450450
451451 var newUsersJson = @"
452- [
453- { 'id' : 20, ' name': 'A1', ' age' : 55 },
454- { 'id' : 21, ' name': 'A2', ' age' : 55 },
455- { 'id' : 22, ' name': 'A3', ' age' : 55 }
456- ]
457- " ;
452+ [
453+ { ""id"" : 20, "" name"": ""A1"", "" age"" : 55 },
454+ { ""id"" : 21, "" name"": ""A2"", "" age"" : 55 },
455+ { ""id"" : 22, "" name"": ""A3"", "" age"" : 55 }
456+ ]
457+ " ;
458458
459- var newUsers = JToken . Parse ( newUsersJson ) ;
459+ var newUsersArray = JsonNode . Parse ( newUsersJson ) . AsArray ( ) ;
460+ var newUsers = newUsersArray . Select ( n => n as dynamic ) ;
460461
461462 await collection . InsertManyAsync ( newUsers ) ;
462463
463- var newUserJson = "{ 'id' : 23, ' name': 'A4', ' age' : 22 }" ;
464- var newUser = JToken . Parse ( newUserJson ) ;
464+ var newUserJson = "{ \" id \" : 23, \" name\" : \" A4 \" , \" age\" : 22 }" ;
465+ var newUser = JsonNode . Parse ( newUserJson ) ;
465466
466467 await collection . InsertOneAsync ( newUser ) ;
467468
@@ -498,10 +499,10 @@ public void UpdateMany_TypedUser()
498499
499500 var newUsers = new [ ]
500501 {
501- new User { Id = 20 , Name = "A1" , Age = 55 } ,
502- new User { Id = 21 , Name = "A2" , Age = 55 } ,
503- new User { Id = 22 , Name = "A3" , Age = 55 }
504- } ;
502+ new User { Id = 20 , Name = "A1" , Age = 55 } ,
503+ new User { Id = 21 , Name = "A2" , Age = 55 } ,
504+ new User { Id = 22 , Name = "A3" , Age = 55 }
505+ } ;
505506
506507 collection . InsertMany ( newUsers ) ;
507508
@@ -627,7 +628,7 @@ public void ReplaceOne_Upsert_DynamicWithInnerData()
627628 var collection = store . GetCollection ( "sensor" ) ;
628629
629630 var success = collection . ReplaceOne ( e => e . id == 11 ,
630- JToken . Parse ( "{ 'id' : 11, ' mac': ' F4:A5:74:89:16:57', ' data' : { ' temperature' : 20.5 } }" ) ,
631+ JsonNode . Parse ( "{ \" id \" : 11, \" mac\" : \" F4:A5:74:89:16:57\" , \" data\" : { \" temperature\" : 20.5 } }" ) ,
631632 true ) ;
632633 Assert . True ( success ) ;
633634
@@ -888,13 +889,14 @@ public void UpdateOne_InnerExpandos()
888889 collection . InsertOne ( user ) ;
889890
890891 var patchData = new Dictionary < string , object >
891- {
892- { "Age" , 41 } ,
893- { "name" , "James" } ,
894- { "Work" , new Dictionary < string , object > { { "Name" , "ACME" } } }
895- } ;
896- var jobject = JObject . FromObject ( patchData ) ;
897- dynamic patchExpando = JsonConvert . DeserializeObject < ExpandoObject > ( jobject . ToString ( ) ) ;
892+ {
893+ { "Age" , 41 } ,
894+ { "name" , "James" } ,
895+ { "Work" , new Dictionary < string , object > { { "Name" , "ACME" } } }
896+ } ;
897+ var jsonString = JsonSerializer . Serialize ( patchData ) ;
898+ var options = new JsonSerializerOptions { Converters = { new SystemExpandoObjectConverter ( ) } } ;
899+ dynamic patchExpando = JsonSerializer . Deserialize < ExpandoObject > ( jsonString , options ) ;
898900
899901 collection . UpdateOne ( i => i . Id == 4 , patchExpando as object ) ;
900902
@@ -1047,25 +1049,29 @@ public async Task UpdateComplexObject_Dynamic()
10471049
10481050 var collection = store . GetCollection ( "employee" ) ;
10491051
1050- var ja = new JArray { "Hello World!" } ;
1051-
1052- var jObj = new JObject ( )
1052+ var data = new
10531053 {
1054- [ "custom_id" ] = 11 ,
1055- [ "nestedArray" ] = new JArray { ja } ,
1054+ custom_id = 11 ,
1055+ nestedArray = new [ ]
1056+ {
1057+ new [ ] { "Hello World!" }
1058+ }
10561059 } ;
10571060
1058- await collection . InsertOneAsync ( jObj ) ;
1061+ await collection . InsertOneAsync ( data ) ;
10591062
10601063 var original = collection . Find ( e => e . custom_id == 11 ) . First ( ) ;
10611064 Assert . Equal ( 0 , original . id ) ;
10621065 Assert . Equal ( 11 , original . custom_id ) ;
10631066 Assert . Equal ( "Hello World!" , original . nestedArray [ 0 ] [ 0 ] ) ;
10641067
1065- var update = new JObject ( )
1068+ var update = new
10661069 {
1067- [ "custom_id" ] = 12 ,
1068- [ "nestedArray" ] = new JArray { new JArray { "Other text" } } ,
1070+ custom_id = 12 ,
1071+ nestedArray = new [ ]
1072+ {
1073+ new [ ] { "Other text" }
1074+ }
10691075 } ;
10701076
10711077 await collection . UpdateOneAsync ( e => e . custom_id == 11 , update ) ;
0 commit comments