-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#58 replace of Newtonsoft with System.Text.Json
- Loading branch information
Showing
4 changed files
with
67 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
DeviceDetector.NET/JsonSerializer/IJsonSerializerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace DeviceDetectorNET.JsonSerializer | ||
{ | ||
public interface IJsonSerializerProvider | ||
{ | ||
bool CanHandle(Type type); | ||
string Serialize(object obj, bool camelCase = true, bool indented = false); | ||
T Deserialize<T>(string jsonString, bool camelCase = true); | ||
object Deserialize(Type type, string jsonString, bool camelCase = true); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
DeviceDetector.NET/JsonSerializer/SystemTextJsonSerializerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Text.Json; | ||
|
||
namespace DeviceDetectorNET.JsonSerializer | ||
{ | ||
public class SystemTextJsonSerializerProvider : IJsonSerializerProvider | ||
{ | ||
public bool CanHandle(Type type) | ||
{ | ||
return true; | ||
} | ||
|
||
public T Deserialize<T>(string jsonString, bool camelCase = true) | ||
{ | ||
return System.Text.Json.JsonSerializer.Deserialize<T>(jsonString, CreateJsonSerializerOptions(camelCase)); | ||
} | ||
|
||
public object Deserialize(Type type, string jsonString, bool camelCase = true) | ||
{ | ||
return System.Text.Json.JsonSerializer.Deserialize(jsonString, type, CreateJsonSerializerOptions(camelCase)); | ||
} | ||
|
||
public string Serialize(object obj, bool camelCase = true, bool indented = false) | ||
{ | ||
return System.Text.Json.JsonSerializer.Serialize(obj, CreateJsonSerializerOptions(camelCase, indented)); | ||
} | ||
|
||
protected virtual JsonSerializerOptions CreateJsonSerializerOptions(bool camelCase = true, bool indented = false) | ||
{ | ||
var settings = new JsonSerializerOptions(); | ||
|
||
if (camelCase) | ||
{ | ||
settings.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; | ||
} | ||
|
||
if (indented) | ||
{ | ||
settings.WriteIndented = true; | ||
} | ||
|
||
return settings; | ||
} | ||
} | ||
} |