Skip to content

Commit

Permalink
Add support for custom json converters
Browse files Browse the repository at this point in the history
  • Loading branch information
racerxdl committed Sep 23, 2024
1 parent 1dc04a8 commit 194ed20
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 10 additions & 2 deletions SharpBoss/Processors/RestProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Net;
using System.Reflection;
using System.Text.Json;

using System.Text.Json.Serialization;
using SharpBoss.Attributes;
using SharpBoss.Attributes.Methods;
using SharpBoss.Exceptions;
Expand All @@ -25,6 +25,7 @@ public class RestProcessor {
private Dictionary<string, RestProxy> _proxies;
private Dictionary<string, IRestExceptionHandler> _exceptionHandlers;
private Dictionary<string, object> _injectables;
private JsonSerializerOptions _serializerOptions;

/// <summary>
/// Create new REST processor
Expand All @@ -34,6 +35,7 @@ public RestProcessor () {
this._proxies = new Dictionary<string, RestProxy> ();
this._exceptionHandlers = new Dictionary<string, IRestExceptionHandler> ();
this._injectables = new Dictionary<string, object> ();
this._serializerOptions = new JsonSerializerOptions() { WriteIndented = true };
}

/// <summary>
Expand All @@ -49,6 +51,12 @@ public void Init (Assembly runningAssembly, string nameSpace) {
foreach (var type in types) {
var restAttribute = type.GetCustomAttribute (typeof (REST));

if (typeof(JsonConverter<>).IsAssignableFrom(type) || typeof(JsonConverter).IsAssignableFrom(type))
{
Logger.Info($"Found JsonConverter {type.Name}");
_serializerOptions.Converters.Add((JsonConverter)Activator.CreateInstance(type));
}

if (restAttribute != null) {
Logger.Info ("Found REST class " + type.Name);
var rest = (REST)restAttribute;
Expand Down Expand Up @@ -137,7 +145,7 @@ public RestResponse Process (string path, string method, RestRequest request) {
if (response is string) {
return new RestResponse ((string)response);
} else {
return new RestResponse (JsonSerializer.Serialize (response), "application/json");
return new RestResponse (JsonSerializer.Serialize (response, _serializerOptions), "application/json");
}
} else {
return new RestResponse ("Not found", "text/plain", HttpStatusCode.NotFound);
Expand Down
3 changes: 1 addition & 2 deletions SharpBoss/SharpBoss.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@
<Folder Include="Processors\" />
<Folder Include="Exceptions\" />
<Folder Include="Interfaces\" />
<Folder Include="Attributes\" />
<Folder Include="Attributes\Methods\" />
<Folder Include="Proxies\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="EmbedIO" Version="3.5.2" />
<PackageReference Include="System.Text.Json" Version="6.0.5" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharpBoss.Logging\SharpBoss.Logging.csproj" />
Expand Down

0 comments on commit 194ed20

Please sign in to comment.