Skip to content

Commit

Permalink
(#32) webApi: update resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Sep 22, 2024
1 parent bda75af commit bfb07f2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 658 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ namespace Paralax.WebApi.Formatters
{
internal class JsonInputFormatter : IInputFormatter
{
private const string EmptyJson = "{}";
private readonly ConcurrentDictionary<Type, MethodInfo> _methods = new();
private const string EmptyJson = "{}";
private readonly ConcurrentDictionary<Type, MethodInfo> _methods = new();
private readonly MethodInfo _deserializeMethod;

public JsonInputFormatter()
{
// Ensure that the correct Deserialize method is selected
_deserializeMethod = typeof(NetJSON.NetJSON).GetMethods()
.Single(m => m.IsGenericMethod && m.Name == nameof(NetJSON.NetJSON.Deserialize));
.Single(m => m.IsGenericMethod && m.Name == nameof(NetJSON.NetJSON.Deserialize)
&& m.GetParameters().Length == 1
&& m.GetParameters()[0].ParameterType == typeof(string)); // Ensure it's the correct overload
}

public bool CanRead(InputFormatterContext context)
Expand Down Expand Up @@ -47,10 +50,18 @@ public async Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
json = EmptyJson;
}

// Use NetJSON to deserialize the JSON string
var result = method.Invoke(null, new object[] { json });
try
{
// Use NetJSON to deserialize the JSON string
var result = method.Invoke(null, new object[] { json });

return await InputFormatterResult.SuccessAsync(result);
return await InputFormatterResult.SuccessAsync(result);
}
catch (Exception ex)
{
// Handle deserialization errors
throw new InvalidOperationException("Invalid JSON format", ex);
}
}
}
}
7 changes: 7 additions & 0 deletions src/Paralax.WebApi/src/Paralax.WebApi/Paralax.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
<PackageReference Include="NetJSON" Version="1.4.4" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Paralax.WebApi.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>



</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal sealed class ParalaxFormatterResolver : IJsonFormatterResolver
// Resolvers that will be used if no custom formatter is available.
private static readonly IJsonFormatterResolver[] Resolvers =
{
StandardResolver.AllowPrivateCamelCase,
StandardResolver.CamelCase, // Use CamelCase as a standard fallback resolver
};

public static List<IJsonFormatter> CustomFormatters { get; } = new List<IJsonFormatter>();
Expand Down
Loading

0 comments on commit bfb07f2

Please sign in to comment.