~furry/mamoru-server

cf98d8e32e81c5b3553ecd9d600e435fc2e3f8b5 — nora 2 months ago 9953e64
fix json response
3 files changed, 18 insertions(+), 6 deletions(-)

M Mamoru.csproj
M Routes/StatusRoute.cs
M Utils/Response.cs
M Mamoru.csproj => Mamoru.csproj +1 -1
@@ 10,7 10,7 @@

    <ItemGroup>
        <PackageReference Include="EXILED" Version="9.0.0-beta.2"/>
        <PackageReference Include="System.Text.Json" Version="9.0.0-rc.1.24431.7" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
        <PackageReference Include="WebSocketSharp" Version="1.0.3-rc11"/>
    </ItemGroup>


M Routes/StatusRoute.cs => Routes/StatusRoute.cs +3 -2
@@ 9,6 9,7 @@ public class StatusRoute : AbstractRoute {
  public override bool Cors => true;

  public override void Get(ref HttpListenerRequest req, ref HttpListenerResponse res) {
    JsonResponse.Create(ref res, new Status(true, true, "1", "2"));
    var status = new Status(true, true, "9.0.0-alpha-2", "1.0.0-alpha");
    JsonResponse.Create(ref res, status);
  }
}
}
\ No newline at end of file

M Utils/Response.cs => Utils/Response.cs +14 -3
@@ 1,12 1,23 @@
using System.Text;
using System.Text.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using WebSocketSharp.Net;

namespace Mamoru.Utils;

public static class JsonResponse {
  private static readonly IContractResolver ContractResolver = new DefaultContractResolver {
    NamingStrategy = new SnakeCaseNamingStrategy(),
  };

  private static readonly JsonSerializerSettings Settings = new() {
    ContractResolver = ContractResolver,
    Formatting = Formatting.None
  };
  
  public static void Create(ref HttpListenerResponse res, object value, HttpStatusCode statusCode = HttpStatusCode.OK) {
    var data = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(value));
    var serialized = JsonConvert.SerializeObject(value, Settings);
    var data = Utf8.GetBytes(serialized);
    res.StatusCode = (int)statusCode;
    res.ContentType = "application/json";
    res.ContentEncoding = Encoding.UTF8;


@@ 16,7 27,7 @@ public static class JsonResponse {
}

public static class EmptyResponse {
  public static void Create(ref HttpListenerResponse res, HttpStatusCode statusCode = HttpStatusCode.OK) {
  public static void Create(ref HttpListenerResponse res, HttpStatusCode statusCode = HttpStatusCode.NoContent) {
    res.StatusCode = (int)statusCode;
    res.ContentLength64 = 0;
    res.Close();