Skip to content

Commit 4c7dacf

Browse files
committed
feat: replace Newtonsoft.Json with System.Text.Json
1 parent d23efd1 commit 4c7dacf

19 files changed

+1215
-262
lines changed

JsonFlatFileDataStore.Benchmark/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ private static void Main(string[] args)
66
{
77
var switcher = new BenchmarkSwitcher(new[]
88
{
9-
typeof(TypedCollectionBenchmark),
10-
typeof(DynamicCollectionBenchmark),
11-
typeof(ObjectExtensionsBenchmark)
12-
});
9+
typeof(TypedCollectionBenchmark),
10+
typeof(DynamicCollectionBenchmark),
11+
typeof(ObjectExtensionsBenchmark)
12+
});
1313

1414
switcher.Run(args);
1515
}

JsonFlatFileDataStore.Test/CollectionModificationTests.cs

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

66
namespace 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);

JsonFlatFileDataStore.Test/CollectionQueryTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json.Linq;
1+
using System.Text.Json.Nodes;
22

33
namespace JsonFlatFileDataStore.Test;
44

@@ -132,22 +132,22 @@ public void GetNextIdValue_StringType_JToken()
132132
var collection = store.GetCollection("collectionWithStringId");
133133

134134
// Insert seed value with upsert
135-
collection.ReplaceOne(e => e, JToken.Parse("{ 'myId': 'test1' }"), true);
135+
collection.ReplaceOne(e => e, JsonNode.Parse("{ \"myId\": \"test1\" }"), true);
136136

137137
var nextId = collection.GetNextIdValue();
138138
Assert.Equal("test2", nextId);
139139

140-
var nextUpdate = JToken.Parse("{ 'myId': 'somethingWrong2' }");
140+
var nextUpdate = JsonNode.Parse("{ \"myId\": \"somethingWrong2\" }");
141141
collection.InsertOne(nextUpdate);
142-
Assert.Equal(nextId, nextUpdate["myId"]);
142+
Assert.Equal(nextId, nextUpdate["myId"].ToString());
143143

144144
nextId = collection.GetNextIdValue();
145145
Assert.Equal("test3", nextId);
146146

147-
nextUpdate = JToken.Parse("{ 'xxx': 111 }");
147+
nextUpdate = JsonNode.Parse("{ \"xxx\": 111 }");
148148
collection.InsertOne(nextUpdate);
149-
Assert.Equal(nextId, nextUpdate["myId"]);
150-
Assert.Equal(111, nextUpdate["xxx"]);
149+
Assert.Equal(nextId, nextUpdate["myId"].GetValue<string>());
150+
Assert.Equal(111, nextUpdate["xxx"].GetValue<int>());
151151

152152
nextId = collection.GetNextIdValue();
153153
Assert.Equal("test4", nextId);

JsonFlatFileDataStore.Test/CopyPropertiesTests.cs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Collections.Generic;
22
using System.Collections.ObjectModel;
33
using System.Dynamic;
4-
using Newtonsoft.Json;
5-
using Newtonsoft.Json.Linq;
4+
using System.Text.Json;
5+
using Xunit;
66

77
namespace JsonFlatFileDataStore.Test;
88

@@ -73,10 +73,10 @@ public void CopyProperties_TypedFamily()
7373
var family = new Family
7474
{
7575
Parents = new List<Parent>
76-
{
77-
new Parent { Name = "Jim", Age = 52 },
78-
new Parent { Name = "Theodor", Age = 14 }
79-
},
76+
{
77+
new Parent { Name = "Jim", Age = 52 },
78+
new Parent { Name = "Theodor", Age = 14 }
79+
},
8080
Address = new Address { City = "Helsinki" }
8181
};
8282

@@ -123,10 +123,10 @@ public void CopyProperties_DynamicFamily()
123123
sParent.Age = 14;
124124

125125
family.Parents = new List<ExpandoObject>
126-
{
127-
fParent,
128-
sParent,
129-
};
126+
{
127+
fParent,
128+
sParent,
129+
};
130130

131131
family.Address = new ExpandoObject();
132132
family.Address.City = "Helsinki";
@@ -228,9 +228,9 @@ public void CopyProperties_TypedFamilyParents()
228228
var family = new Family
229229
{
230230
Parents = new List<Parent>
231-
{
232-
new Parent { Name = "Jim", Age = 52 }
233-
},
231+
{
232+
new Parent { Name = "Jim", Age = 52 }
233+
},
234234
Address = new Address { City = "Helsinki" }
235235
};
236236

@@ -251,13 +251,14 @@ public void CopyProperties_DynamicWithInnerExpandos()
251251
user.work = work;
252252

253253
var patchData = new Dictionary<string, object>
254-
{
255-
{ "age", 41 },
256-
{ "name", "James" },
257-
{ "work", new Dictionary<string, object> { { "name", "ACME" } } }
258-
};
259-
var jobject = JObject.FromObject(patchData);
260-
dynamic patchExpando = JsonConvert.DeserializeObject<ExpandoObject>(jobject.ToString());
254+
{
255+
{ "age", 41 },
256+
{ "name", "James" },
257+
{ "work", new Dictionary<string, object> { { "name", "ACME" } } }
258+
};
259+
var jsonString = JsonSerializer.Serialize(patchData);
260+
var options = new JsonSerializerOptions { Converters = { new SystemExpandoObjectConverter() } };
261+
dynamic patchExpando = JsonSerializer.Deserialize<ExpandoObject>(jsonString, options);
261262

262263
ObjectExtensions.CopyProperties(patchExpando, user);
263264
Assert.Equal("James", user.name);
@@ -296,10 +297,10 @@ public void CopyProperties_DynamicEmptyWithInnerDictionary()
296297
sensor.mac = "F4:A5:74:89:16:57";
297298
sensor.timestamp = null;
298299
sensor.data = new Dictionary<string, object>
299-
{
300-
{ "temperature", 24.3 },
301-
{ "identifier", null }
302-
};
300+
{
301+
{ "temperature", 24.3 },
302+
{ "identifier", null }
303+
};
303304

304305
ObjectExtensions.CopyProperties(sensor, destination);
305306

@@ -319,13 +320,14 @@ public void CopyProperties_TypedWithInnerExpandos()
319320
};
320321

321322
var patchData = new Dictionary<string, object>
322-
{
323-
{ "Age", 41 },
324-
{ "Name", "James" },
325-
{ "Work", new Dictionary<string, object> { { "Name", "ACME" } } }
326-
};
327-
var jobject = JObject.FromObject(patchData);
328-
dynamic patchExpando = JsonConvert.DeserializeObject<ExpandoObject>(jobject.ToString());
323+
{
324+
{ "Age", 41 },
325+
{ "Name", "James" },
326+
{ "Work", new Dictionary<string, object> { { "Name", "ACME" } } }
327+
};
328+
var jsonString = JsonSerializer.Serialize(patchData);
329+
var options = new JsonSerializerOptions { Converters = { new SystemExpandoObjectConverter() } };
330+
dynamic patchExpando = JsonSerializer.Deserialize<ExpandoObject>(jsonString, options);
329331

330332
ObjectExtensions.CopyProperties(patchExpando, user);
331333
Assert.Equal("James", user.Name);

0 commit comments

Comments
 (0)