1- using System . Collections . Generic ;
2- using System . Dynamic ;
3- using Newtonsoft . Json ;
4- using Newtonsoft . Json . Linq ;
1+ using System . Dynamic ;
2+ using System . Text . Json ;
3+ using System . Text . Json . Nodes ;
4+ using System . Threading . Tasks ;
55
66namespace JsonFlatFileDataStore . Test ;
77
@@ -142,7 +142,7 @@ public async Task UpdateOneAsync_TypedUser_WrongCase()
142142
143143 var collection2 = store2 . GetCollection < User > ( "users2" ) ;
144144 await collection2 . UpdateOneAsync ( x => x . Id == 0 , new { name = "new value" } ) ;
145- await collection2 . UpdateOneAsync ( x => x . Id == 1 , JToken . Parse ( "{ name: \" new value 2\" } " ) ) ;
145+ await collection2 . UpdateOneAsync ( x => x . Id == 1 , JsonNode . Parse ( "{ \" name\" : \" new value 2\" } " ) ) ;
146146
147147 var store3 = new DataStore ( newFilePath ) ;
148148
@@ -203,9 +203,9 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleArray()
203203 Id = Guid . NewGuid ( ) . ToString ( ) ,
204204 Type = "empty" ,
205205 Fragments = new List < string >
206- {
207- Guid . NewGuid ( ) . ToString ( )
208- }
206+ {
207+ Guid . NewGuid ( ) . ToString ( )
208+ }
209209 } ;
210210
211211 var insertResult = collection . InsertOne ( newModel ) ;
@@ -220,10 +220,10 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleArray()
220220 {
221221 Type = "filled" ,
222222 Fragments = new List < string >
223- {
224- Guid . NewGuid ( ) . ToString ( ) ,
225- Guid . NewGuid ( ) . ToString ( )
226- }
223+ {
224+ Guid . NewGuid ( ) . ToString ( ) ,
225+ Guid . NewGuid ( ) . ToString ( )
226+ }
227227 } ;
228228
229229 await collection2 . UpdateOneAsync ( e => e . Id == newModel . Id , updateData ) ;
@@ -254,9 +254,9 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleIntArray()
254254 Id = Guid . NewGuid ( ) . ToString ( ) ,
255255 Type = "empty" ,
256256 Fragments = new List < int >
257- {
258- 1
259- }
257+ {
258+ 1
259+ }
260260 } ;
261261
262262 var insertResult = collection . InsertOne ( newModel ) ;
@@ -271,10 +271,10 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleIntArray()
271271 {
272272 Type = "filled" ,
273273 Fragments = new List < int >
274- {
275- 2 ,
276- 3
277- }
274+ {
275+ 2 ,
276+ 3
277+ }
278278 } ;
279279
280280 await collection2 . UpdateOneAsync ( e => e . Id == newModel . Id , updateData ) ;
@@ -319,10 +319,10 @@ public async Task UpdateOneAsync_TypedModel_NestedArrays()
319319 {
320320 Type = "filled" ,
321321 NestedLists = new List < List < int > >
322- {
323- null ,
324- new List < int > { 4 } ,
325- }
322+ {
323+ null ,
324+ new List < int > { 4 } ,
325+ }
326326 } ;
327327
328328 await collection2 . UpdateOneAsync ( e => e . Id == newModel . Id , updateData ) ;
@@ -407,10 +407,10 @@ public async Task UpdateManyAsync_DynamicUser()
407407
408408 var newUsers = new [ ]
409409 {
410- new { id = 20 , name = "A1" , age = 55 } ,
411- new { id = 21 , name = "A2" , age = 55 } ,
412- new { id = 22 , name = "A3" , age = 55 }
413- } ;
410+ new { id = 20 , name = "A1" , age = 55 } ,
411+ new { id = 21 , name = "A2" , age = 55 } ,
412+ new { id = 22 , name = "A3" , age = 55 }
413+ } ;
414414
415415 await collection . InsertManyAsync ( newUsers ) ;
416416
@@ -445,19 +445,20 @@ public async Task UpdateManyAsync_JsonUser()
445445 Assert . Equal ( 3 , collection . Count ) ;
446446
447447 var newUsersJson = @"
448- [
449- { 'id' : 20, ' name': 'A1', ' age' : 55 },
450- { 'id' : 21, ' name': 'A2', ' age' : 55 },
451- { 'id' : 22, ' name': 'A3', ' age' : 55 }
452- ]
453- " ;
448+ [
449+ { ""id"" : 20, "" name"": ""A1"", "" age"" : 55 },
450+ { ""id"" : 21, "" name"": ""A2"", "" age"" : 55 },
451+ { ""id"" : 22, "" name"": ""A3"", "" age"" : 55 }
452+ ]
453+ " ;
454454
455- var newUsers = JToken . Parse ( newUsersJson ) ;
455+ var newUsersArray = JsonNode . Parse ( newUsersJson ) . AsArray ( ) ;
456+ var newUsers = newUsersArray . Select ( n => n as dynamic ) ;
456457
457458 await collection . InsertManyAsync ( newUsers ) ;
458459
459- var newUserJson = "{ 'id' : 23, ' name': 'A4', ' age' : 22 }" ;
460- var newUser = JToken . Parse ( newUserJson ) ;
460+ var newUserJson = "{ \" id \" : 23, \" name\" : \" A4 \" , \" age\" : 22 }" ;
461+ var newUser = JsonNode . Parse ( newUserJson ) ;
461462
462463 await collection . InsertOneAsync ( newUser ) ;
463464
@@ -494,10 +495,10 @@ public void UpdateMany_TypedUser()
494495
495496 var newUsers = new [ ]
496497 {
497- new User { Id = 20 , Name = "A1" , Age = 55 } ,
498- new User { Id = 21 , Name = "A2" , Age = 55 } ,
499- new User { Id = 22 , Name = "A3" , Age = 55 }
500- } ;
498+ new User { Id = 20 , Name = "A1" , Age = 55 } ,
499+ new User { Id = 21 , Name = "A2" , Age = 55 } ,
500+ new User { Id = 22 , Name = "A3" , Age = 55 }
501+ } ;
501502
502503 collection . InsertMany ( newUsers ) ;
503504
@@ -623,7 +624,7 @@ public void ReplaceOne_Upsert_DynamicWithInnerData()
623624 var collection = store . GetCollection ( "sensor" ) ;
624625
625626 var success = collection . ReplaceOne ( e => e . id == 11 ,
626- JToken . Parse ( "{ 'id' : 11, ' mac': ' F4:A5:74:89:16:57', ' data' : { ' temperature' : 20.5 } }" ) ,
627+ JsonNode . Parse ( "{ \" id \" : 11, \" mac\" : \" F4:A5:74:89:16:57\" , \" data\" : { \" temperature\" : 20.5 } }" ) ,
627628 true ) ;
628629 Assert . True ( success ) ;
629630
@@ -884,13 +885,14 @@ public void UpdateOne_InnerExpandos()
884885 collection . InsertOne ( user ) ;
885886
886887 var patchData = new Dictionary < string , object >
887- {
888- { "Age" , 41 } ,
889- { "name" , "James" } ,
890- { "Work" , new Dictionary < string , object > { { "Name" , "ACME" } } }
891- } ;
892- var jobject = JObject . FromObject ( patchData ) ;
893- dynamic patchExpando = JsonConvert . DeserializeObject < ExpandoObject > ( jobject . ToString ( ) ) ;
888+ {
889+ { "Age" , 41 } ,
890+ { "name" , "James" } ,
891+ { "Work" , new Dictionary < string , object > { { "Name" , "ACME" } } }
892+ } ;
893+ var jsonString = JsonSerializer . Serialize ( patchData ) ;
894+ var options = new JsonSerializerOptions { Converters = { new SystemExpandoObjectConverter ( ) } } ;
895+ dynamic patchExpando = JsonSerializer . Deserialize < ExpandoObject > ( jsonString , options ) ;
894896
895897 collection . UpdateOne ( i => i . Id == 4 , patchExpando as object ) ;
896898
@@ -1043,25 +1045,29 @@ public async Task UpdateComplexObject_Dynamic()
10431045
10441046 var collection = store . GetCollection ( "employee" ) ;
10451047
1046- var ja = new JArray { "Hello World!" } ;
1047-
1048- var jObj = new JObject ( )
1048+ var data = new
10491049 {
1050- [ "custom_id" ] = 11 ,
1051- [ "nestedArray" ] = new JArray { ja } ,
1050+ custom_id = 11 ,
1051+ nestedArray = new [ ]
1052+ {
1053+ new [ ] { "Hello World!" }
1054+ }
10521055 } ;
10531056
1054- await collection . InsertOneAsync ( jObj ) ;
1057+ await collection . InsertOneAsync ( data ) ;
10551058
10561059 var original = collection . Find ( e => e . custom_id == 11 ) . First ( ) ;
10571060 Assert . Equal ( 0 , original . id ) ;
10581061 Assert . Equal ( 11 , original . custom_id ) ;
10591062 Assert . Equal ( "Hello World!" , original . nestedArray [ 0 ] [ 0 ] ) ;
10601063
1061- var update = new JObject ( )
1064+ var update = new
10621065 {
1063- [ "custom_id" ] = 12 ,
1064- [ "nestedArray" ] = new JArray { new JArray { "Other text" } } ,
1066+ custom_id = 12 ,
1067+ nestedArray = new [ ]
1068+ {
1069+ new [ ] { "Other text" }
1070+ }
10651071 } ;
10661072
10671073 await collection . UpdateOneAsync ( e => e . custom_id == 11 , update ) ;
0 commit comments