Skip to content

Commit b51bb12

Browse files
committed
feat: replace Newtonsoft.Json with System.Text.Json
1 parent e3cbcec commit b51bb12

18 files changed

+1218
-259
lines changed

JsonFlatFileDataStore.Benchmark/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ private static void Main(string[] args)
88
{
99
var switcher = new BenchmarkSwitcher(new[]
1010
{
11-
typeof(TypedCollectionBenchmark),
12-
typeof(DynamicCollectionBenchmark),
13-
typeof(ObjectExtensionsBenchmark)
14-
});
11+
typeof(TypedCollectionBenchmark),
12+
typeof(DynamicCollectionBenchmark),
13+
typeof(ObjectExtensionsBenchmark)
14+
});
1515

1616
switcher.Run(args);
1717
}

JsonFlatFileDataStore.Test/CollectionModificationTests.cs

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using System.Collections.Generic;
33
using System.Dynamic;
44
using System.Linq;
5+
using System.Text.Json;
6+
using System.Text.Json.Nodes;
57
using System.Threading.Tasks;
6-
using Newtonsoft.Json;
7-
using Newtonsoft.Json.Linq;
88
using Xunit;
99

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

JsonFlatFileDataStore.Test/CollectionQueryTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Linq;
2-
using Newtonsoft.Json.Linq;
2+
using System.Text.Json.Nodes;
33
using Xunit;
44

55
namespace JsonFlatFileDataStore.Test;
@@ -134,22 +134,22 @@ public void GetNextIdValue_StringType_JToken()
134134
var collection = store.GetCollection("collectionWithStringId");
135135

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

139139
var nextId = collection.GetNextIdValue();
140140
Assert.Equal("test2", nextId);
141141

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

146146
nextId = collection.GetNextIdValue();
147147
Assert.Equal("test3", nextId);
148148

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

154154
nextId = collection.GetNextIdValue();
155155
Assert.Equal("test4", nextId);

JsonFlatFileDataStore.Test/CopyPropertiesTests.cs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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;
65
using Xunit;
76

87
namespace JsonFlatFileDataStore.Test;
@@ -74,10 +73,10 @@ public void CopyProperties_TypedFamily()
7473
var family = new Family
7574
{
7675
Parents = new List<Parent>
77-
{
78-
new Parent { Name = "Jim", Age = 52 },
79-
new Parent { Name = "Theodor", Age = 14 }
80-
},
76+
{
77+
new Parent { Name = "Jim", Age = 52 },
78+
new Parent { Name = "Theodor", Age = 14 }
79+
},
8180
Address = new Address { City = "Helsinki" }
8281
};
8382

@@ -124,10 +123,10 @@ public void CopyProperties_DynamicFamily()
124123
sParent.Age = 14;
125124

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

132131
family.Address = new ExpandoObject();
133132
family.Address.City = "Helsinki";
@@ -229,9 +228,9 @@ public void CopyProperties_TypedFamilyParents()
229228
var family = new Family
230229
{
231230
Parents = new List<Parent>
232-
{
233-
new Parent { Name = "Jim", Age = 52 }
234-
},
231+
{
232+
new Parent { Name = "Jim", Age = 52 }
233+
},
235234
Address = new Address { City = "Helsinki" }
236235
};
237236

@@ -252,13 +251,14 @@ public void CopyProperties_DynamicWithInnerExpandos()
252251
user.work = work;
253252

254253
var patchData = new Dictionary<string, object>
255-
{
256-
{ "age", 41 },
257-
{ "name", "James" },
258-
{ "work", new Dictionary<string, object> { { "name", "ACME" } } }
259-
};
260-
var jobject = JObject.FromObject(patchData);
261-
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);
262262

263263
ObjectExtensions.CopyProperties(patchExpando, user);
264264
Assert.Equal("James", user.name);
@@ -297,10 +297,10 @@ public void CopyProperties_DynamicEmptyWithInnerDictionary()
297297
sensor.mac = "F4:A5:74:89:16:57";
298298
sensor.timestamp = null;
299299
sensor.data = new Dictionary<string, object>
300-
{
301-
{ "temperature", 24.3 },
302-
{ "identifier", null }
303-
};
300+
{
301+
{ "temperature", 24.3 },
302+
{ "identifier", null }
303+
};
304304

305305
ObjectExtensions.CopyProperties(sensor, destination);
306306

@@ -320,13 +320,14 @@ public void CopyProperties_TypedWithInnerExpandos()
320320
};
321321

322322
var patchData = new Dictionary<string, object>
323-
{
324-
{ "Age", 41 },
325-
{ "Name", "James" },
326-
{ "Work", new Dictionary<string, object> { { "Name", "ACME" } } }
327-
};
328-
var jobject = JObject.FromObject(patchData);
329-
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);
330331

331332
ObjectExtensions.CopyProperties(patchExpando, user);
332333
Assert.Equal("James", user.Name);

0 commit comments

Comments
 (0)