Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Custom JsonSerializer ignored #2991

Open
6 of 8 tasks
DeckardCain2014 opened this issue Oct 25, 2019 · 1 comment
Open
6 of 8 tasks

Custom JsonSerializer ignored #2991

DeckardCain2014 opened this issue Oct 25, 2019 · 1 comment

Comments

@DeckardCain2014
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of Nancy
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • 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
    • Nancy.Owin (Kestrel with NetCore Owin 3.0)
    • Other: Newtonsoft.Json 12.0.2
  • Other Nancy packages and versions: None
@DeckardCain2014
Copy link
Author

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant