Skip to content

Commit

Permalink
Support IJsonService
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Sep 26, 2024
1 parent 7d1dc94 commit 5a06f34
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Build/Build.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace></RootNamespace>
<NoWarn>CS0649;CS0169</NoWarn>
<NukeRootDirectory>.</NukeRootDirectory>
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.8.0] / 2024-09-26
### Features
- Support `IJsonService` to use `NewtonsoftJsonService` if available.
### Removed
- Remove `Newtonsoft.Json` reference.

## [1.7.0] / 2024-01-16
### Features
- Add `net8.0` support.
Expand Down Expand Up @@ -40,6 +46,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Update `Test` project

[vNext]: ../../compare/1.5.0...HEAD
[1.8.0]: ../../compare/1.7.0...1.8.0
[1.7.0]: ../../compare/1.6.0...1.7.0
[1.6.0]: ../../compare/1.5.3...1.6.0
[1.5.3]: ../../compare/1.5.2...1.5.3
Expand Down
23 changes: 23 additions & 0 deletions NamedPipeWrapper/Json/IJsonService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace NamedPipeWrapper.Json
{
/// <summary>
/// Represents a service for JSON serialization and deserialization.
/// </summary>
public interface IJsonService
{
/// <summary>
/// Deserializes the specified JSON string into an object of type T.
/// </summary>
/// <typeparam name="T">The type of the object to deserialize.</typeparam>
/// <param name="value">The JSON string to deserialize.</param>
/// <returns>The deserialized object of type T.</returns>
T Deserialize<T>(string value);

/// <summary>
/// Serializes the specified object into a JSON string.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <returns>The JSON string representation of the serialized object.</returns>
string Serialize(object value);
}
}
38 changes: 38 additions & 0 deletions NamedPipeWrapper/Json/Json/JsonService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace NamedPipeWrapper.Json.Json
{
#if NETFRAMEWORK
using System.Web.Script.Serialization;
internal class JsonService : IJsonService
{
public JsonService()
{
JavaScriptSerializer = new JavaScriptSerializer();
}
public JavaScriptSerializer JavaScriptSerializer { get; set; }
public T Deserialize<T>(string value)
{
return JavaScriptSerializer.Deserialize<T>(value);
}

public string Serialize(object value)
{
return JavaScriptSerializer.Serialize(value);
}
}
#endif
#if NET
using System.Text.Json;
internal class JsonService : IJsonService
{
public T Deserialize<T>(string value)
{
return JsonSerializer.Deserialize<T>(value);
}

public string Serialize(object value)
{
return JsonSerializer.Serialize(value);
}
}
#endif
}
20 changes: 20 additions & 0 deletions NamedPipeWrapper/Json/Json/NewtonsoftJsonService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace NamedPipeWrapper.Json.Json
{
internal class NewtonsoftJsonService : IJsonService
{
public NewtonsoftJsonService()
{
Settings = new Newtonsoft.Json.JsonSerializerSettings();
}
public Newtonsoft.Json.JsonSerializerSettings Settings { get; }
public T Deserialize<T>(string value)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(value, Settings);
}

public string Serialize(object value)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(value, Settings);
}
}
}
29 changes: 26 additions & 3 deletions NamedPipeWrapper/Json/JsonExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using NamedPipeWrapper.Json.Json;
using System;

namespace NamedPipeWrapper.Json
{
Expand All @@ -7,6 +8,28 @@ namespace NamedPipeWrapper.Json
/// </summary>
public static class JsonExtension
{
/// <summary>
/// IJsonService
/// </summary>
public static IJsonService JsonService { get; set; } = CreateJsonService();

/// <summary>
/// Create JsonService
/// </summary>
/// <returns></returns>
/// <remarks>
/// If NewtonsoftJsonService is available, use it.
/// </remarks>
private static IJsonService CreateJsonService()
{
try
{
return new NewtonsoftJsonService();
}
catch { };
return new JsonService();
}

/// <summary>
/// JsonDeserialize
/// </summary>
Expand All @@ -15,7 +38,7 @@ public static class JsonExtension
/// <returns></returns>
public static T JsonDeserialize<T>(this string value)
{
return JsonUtils.DeserializeObject<T>(value);
return JsonService.Deserialize<T>(value);
}

/// <summary>
Expand All @@ -26,7 +49,7 @@ public static T JsonDeserialize<T>(this string value)
/// <returns></returns>
public static string JsonSerialize<T>(this T value)
{
return JsonUtils.SerializeObject<T>(value);
return JsonService.Serialize(value);
}
}
}
8 changes: 6 additions & 2 deletions NamedPipeWrapper/NamedPipeWrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<PropertyGroup>
<PackageId>NamedPipeWrapper.Json</PackageId>
<Version>1.7.0</Version>
<Version>1.8.0-alpha</Version>
<ProjectGuid>{73152691-3CDE-46DF-8D04-7117747DFFE7}</ProjectGuid>
</PropertyGroup>

Expand Down Expand Up @@ -77,8 +77,12 @@
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup Condition="$(DefineConstants.Contains('NETFRAMEWORK'))">
<Reference Include="System.Web.Extensions" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.*">
<PackageReference Include="Newtonsoft.Json" Version="9.*" IncludeAssets="build; compile" PrivateAssets="All">
<NoWarn>NU1903</NoWarn>
</PackageReference>
</ItemGroup>
Expand Down
File renamed without changes.

0 comments on commit 5a06f34

Please sign in to comment.