Skip to content

Commit 811aaf6

Browse files
committed
refactor json converter
1 parent 400b44d commit 811aaf6

File tree

8 files changed

+498
-104
lines changed

8 files changed

+498
-104
lines changed

socket.io-client-csharp.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7FFBC536-6E4
2121
EndProject
2222
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{335237B5-C6F5-4D4D-BD0C-006558A6F1B6}"
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketIOClient.Newtonsoft.Json.UnitTests", "tests\SocketIOClient.Newtonsoft.Json.UnitTests\SocketIOClient.Newtonsoft.Json.UnitTests.csproj", "{1FA6E494-5D3E-42AC-BABF-BC0D9F5DD5C1}"
25+
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2628
Debug|Any CPU = Debug|Any CPU
@@ -55,6 +57,10 @@ Global
5557
{8C62BD1E-9F14-4170-8E19-29A39996AAA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
5658
{8C62BD1E-9F14-4170-8E19-29A39996AAA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
5759
{8C62BD1E-9F14-4170-8E19-29A39996AAA2}.Release|Any CPU.Build.0 = Release|Any CPU
60+
{1FA6E494-5D3E-42AC-BABF-BC0D9F5DD5C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
61+
{1FA6E494-5D3E-42AC-BABF-BC0D9F5DD5C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
62+
{1FA6E494-5D3E-42AC-BABF-BC0D9F5DD5C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
63+
{1FA6E494-5D3E-42AC-BABF-BC0D9F5DD5C1}.Release|Any CPU.Build.0 = Release|Any CPU
5864
EndGlobalSection
5965
GlobalSection(SolutionProperties) = preSolution
6066
HideSolutionNode = FALSE
@@ -70,5 +76,6 @@ Global
7076
{8C62BD1E-9F14-4170-8E19-29A39996AAA2} = {335237B5-C6F5-4D4D-BD0C-006558A6F1B6}
7177
{A12339F8-B60D-4296-A61E-A6FFFA4C4A73} = {335237B5-C6F5-4D4D-BD0C-006558A6F1B6}
7278
{B3A90FB0-5D1C-499C-A2F9-6E4DD331C8E9} = {335237B5-C6F5-4D4D-BD0C-006558A6F1B6}
79+
{1FA6E494-5D3E-42AC-BABF-BC0D9F5DD5C1} = {335237B5-C6F5-4D4D-BD0C-006558A6F1B6}
7380
EndGlobalSection
7481
EndGlobal

src/SocketIOClient.Newtonsoft.Json/NewtonsoftJsonSerializer.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,28 @@ namespace SocketIOClient.Newtonsoft.Json
77
{
88
public class NewtonsoftJsonSerializer : IJsonSerializer
99
{
10-
public Func<JsonSerializerSettings> JsonSerializerOptions { get; }
10+
public NewtonsoftJsonSerializer() : this(new JsonSerializerSettings())
11+
{
12+
}
13+
14+
public NewtonsoftJsonSerializer(JsonSerializerSettings settings)
15+
{
16+
_settings = settings;
17+
}
18+
19+
private readonly JsonSerializerSettings _settings;
20+
21+
private JsonSerializerSettings NewSettings(ByteArrayConverter converter)
22+
{
23+
var settings = new JsonSerializerSettings(_settings);
24+
settings.Converters.Add(converter);
25+
return settings;
26+
}
1127

1228
public JsonSerializeResult Serialize(object[] data)
1329
{
1430
var converter = new ByteArrayConverter();
15-
var settings = GetOptions();
16-
settings.Converters.Add(converter);
31+
var settings = NewSettings(converter);
1732
string json = JsonConvert.SerializeObject(data, settings);
1833
return new JsonSerializeResult
1934
{
@@ -24,33 +39,28 @@ public JsonSerializeResult Serialize(object[] data)
2439

2540
public T Deserialize<T>(string json)
2641
{
27-
var settings = GetOptions();
28-
return JsonConvert.DeserializeObject<T>(json, settings);
42+
return JsonConvert.DeserializeObject<T>(json, _settings);
43+
}
44+
45+
public object Deserialize(string json, Type type)
46+
{
47+
return JsonConvert.DeserializeObject(json, type, _settings);
2948
}
3049

3150
public T Deserialize<T>(string json, IList<byte[]> bytes)
3251
{
3352
var converter = new ByteArrayConverter();
3453
converter.Bytes.AddRange(bytes);
35-
var settings = GetOptions();
36-
settings.Converters.Add(converter);
54+
var settings = NewSettings(converter);
3755
return JsonConvert.DeserializeObject<T>(json, settings);
3856
}
3957

40-
private JsonSerializerSettings GetOptions()
58+
public object Deserialize(string json, Type type, IList<byte[]> bytes)
4159
{
42-
JsonSerializerSettings options = null;
43-
if (OptionsProvider != null)
44-
{
45-
options = OptionsProvider();
46-
}
47-
if (options == null)
48-
{
49-
options = new JsonSerializerSettings();
50-
}
51-
return options;
60+
var converter = new ByteArrayConverter();
61+
converter.Bytes.AddRange(bytes);
62+
var settings = NewSettings(converter);
63+
return JsonConvert.DeserializeObject(json, type, settings);
5264
}
53-
54-
public Func<JsonSerializerSettings> OptionsProvider { get; set; }
5565
}
56-
}
66+
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace SocketIOClient.JsonSerializer
45
{
56
public interface IJsonSerializer
67
{
78
JsonSerializeResult Serialize(object[] data);
89
T Deserialize<T>(string json);
10+
object Deserialize(string json, Type type);
911
T Deserialize<T>(string json, IList<byte[]> incomingBytes);
12+
object Deserialize(string json, Type type, IList<byte[]> incomingBytes);
1013
}
11-
}
14+
}
Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Text.Json;
45

56
namespace SocketIOClient.JsonSerializer
67
{
78
public class SystemTextJsonSerializer : IJsonSerializer
89
{
10+
public SystemTextJsonSerializer() : this(new JsonSerializerOptions())
11+
{
12+
}
13+
14+
public SystemTextJsonSerializer(JsonSerializerOptions options)
15+
{
16+
_options = options;
17+
}
18+
19+
private readonly JsonSerializerOptions _options;
20+
21+
private JsonSerializerOptions NewOptions(ByteArrayConverter converter)
22+
{
23+
var options = new JsonSerializerOptions(_options);
24+
options.Converters.Add(converter);
25+
return options;
26+
}
27+
928
public JsonSerializeResult Serialize(object[] data)
1029
{
1130
var converter = new ByteArrayConverter();
12-
var options = GetOptions();
13-
options.Converters.Add(converter);
31+
var options = NewOptions(converter);
1432
string json = System.Text.Json.JsonSerializer.Serialize(data, options);
1533
return new JsonSerializeResult
1634
{
@@ -21,33 +39,28 @@ public JsonSerializeResult Serialize(object[] data)
2139

2240
public T Deserialize<T>(string json)
2341
{
24-
var options = GetOptions();
25-
return System.Text.Json.JsonSerializer.Deserialize<T>(json, options);
42+
return System.Text.Json.JsonSerializer.Deserialize<T>(json, _options);
43+
}
44+
45+
public object Deserialize(string json, Type type)
46+
{
47+
return System.Text.Json.JsonSerializer.Deserialize(json, type, _options);
2648
}
2749

2850
public T Deserialize<T>(string json, IList<byte[]> bytes)
2951
{
30-
var options = GetOptions();
3152
var converter = new ByteArrayConverter();
32-
options.Converters.Add(converter);
3353
converter.Bytes.AddRange(bytes);
54+
var options = NewOptions(converter);
3455
return System.Text.Json.JsonSerializer.Deserialize<T>(json, options);
3556
}
3657

37-
private JsonSerializerOptions GetOptions()
58+
public object Deserialize(string json, Type type, IList<byte[]> bytes)
3859
{
39-
JsonSerializerOptions options = null;
40-
if (OptionsProvider != null)
41-
{
42-
options = OptionsProvider();
43-
}
44-
if (options == null)
45-
{
46-
options = new JsonSerializerOptions();
47-
}
48-
return options;
60+
var converter = new ByteArrayConverter();
61+
converter.Bytes.AddRange(bytes);
62+
var options = NewOptions(converter);
63+
return System.Text.Json.JsonSerializer.Deserialize(json, type, options);
4964
}
50-
51-
public Func<JsonSerializerOptions> OptionsProvider { get; set; }
5265
}
53-
}
66+
}

0 commit comments

Comments
 (0)