Nancy icon indicating copy to clipboard operation
Nancy copied to clipboard

Custom JsonSerializer ignored

Open DeckardCain2014 opened this issue 5 years ago • 1 comments

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have verified that I am running the latest version of Nancy
  • [x] I have verified if the problem exist in both DEBUG and RELEASE mode
  • [x] I have searched open and closed issues to ensure it has not already been reported

Description

I want to use a custom JsonSerializer for Serializing/Deserializing TimeSpan and Dictionary objects. In V.1.4.5 I could override ConfigureApplicationContainer and register my own Serializers inheriting Newtonsoft.Json.JsonSerializer. The same registration in V2.0 seems now to be ignored. I also registered a new environment.Json Method in Configure in the Bootstrapper. However, this takes JavaScriptConverter as custom serializers. Furthermore, I saw Bug #2800 , but the Nancy.Json.Serializers NuGet isn't compatible with NetStandard2.0 !

Steps to Reproduce

CustomBootstrapper

public class CustomBootstrapper : DefaultNancyBootstrapper
    {
        protected override void ConfigureApplicationContainer(TinyIoCContainer container)
        {
            base.ConfigureApplicationContainer(container);
            container.Register<JsonSerializer, CustomNancyJsonSerializer>();
        }
    }

CustomNancyJsonSerializer

public class CustomNancyJsonSerializer : JsonSerializer
    {
        public CustomNancyJsonSerializer()
        {
            MissingMemberHandling = MissingMemberHandling.Ignore;
            NullValueHandling = NullValueHandling.Ignore; 
            DefaultValueHandling = DefaultValueHandling.Include; 
            DateFormatHandling = DateFormatHandling.IsoDateFormat; 
            DateTimeZoneHandling = DateTimeZoneHandling.Unspecified;
            ContractResolver = new DefaultContractResolver();
            Formatting = Formatting.None;
        }
    }

Startup

public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseOwin(x => x.UseNancy(o => o.Bootstrapper = new CustomBootstrapper()));
        }
    }

TestModule

public class TestModule : NancyModule
    {
        public TestModule() : base("/")
        {
            Get("/", parameters =>
            {
                return Negotiate.WithModel(new
                {
                    Id = 1,
                    Date = DateTime.Now,
                    TimeS = new TimeSpan(1, 10, 20),
                    NullField = (int?)null
                });
            });
        }
    }
Expected Json
{
    "id": 1,
    "date": "2019-10-25T08:16:35.0424088",
    "timeS": {
        "days": 0,
        "hours": 1,
        "minutes": 10,
        "seconds": 20,
        "milliseconds": 0
    },
    
}

Actual Json

{
    "id": 1,
    "date": "2019-10-25T08:16:35.0424088+02:00",
    "timeS": {
        "days": 0,
        "hours": 1,
        "minutes": 10,
        "seconds": 20,
        "milliseconds": 0
    },
    "nullField": null
}

System Configuration

.Net Core 3.0 WebAPI x86 Windows 10 1809 VisualStudio 2019

  • Nancy version: 2.0
  • Nancy host
    • [ ] Nancy.Hosting.Aspnet
    • [ ] Nancy.Hosting.Self
    • [x] Nancy.Owin (Kestrel with NetCore Owin 3.0)
    • [x] Other: Newtonsoft.Json 12.0.2
  • Other Nancy packages and versions: None

DeckardCain2014 avatar Oct 25 '19 06:10 DeckardCain2014

Nancy.Serialization.JsonNet is compatible with Nancy if you use the preRelease version 2.0 of Nancy.Serialization.JsonNet as it seems... Is there no final release?

DeckardCain2014 avatar Oct 28 '19 13:10 DeckardCain2014