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

Json null values not ignored despite registration of a custom JsonSerializer #2800

Closed
MamboJoel opened this issue Oct 11, 2017 · 2 comments
Closed

Comments

@MamboJoel
Copy link

Description

We want are Nancy modules to ignore null values when returning json objects.
Despite registering a custom JsonSerializer null values aren't ignored. Unfortunatly this solved issue did not help.

Steps to Reproduce

  • Create a Visual Studio Nancy empty project wieth ASP.host.
  • Add an element containing this :
using Nancy;
using Nancy.TinyIoc;
using Newtonsoft.Json;

namespace NancyEmptyAspNetHost7
{
    /// <summary>
    /// A custom json serializer to ignore null values.
    /// </summary>
    public sealed class CustomJsonSerializer : JsonSerializer
    {
        public CustomJsonSerializer()
        {
            NullValueHandling = NullValueHandling.Ignore;
        }
    }

    /// <summary>
    /// A boostrapper registering the json serializer on ConfigureApplicationContainer.
    /// </summary>
    public class MyBoostrapper : DefaultNancyBootstrapper
    {
        protected override void ConfigureApplicationContainer(TinyIoCContainer container)
        {
            base.ConfigureApplicationContainer(container);
            container.Register<JsonSerializer, CustomJsonSerializer>();
        }
    }

    /// <summary>
    /// A simple class.
    /// </summary>
    public class Foo
    {
        public object Bar { get; set; }
    }

    /// <summary>
    /// A module.
    /// </summary>
    public class MyModule : NancyModule
    {
        public MyModule()
        {
            Get[""] = _ =>
            {
                return Response.AsJson(new Foo());
                // expected : {}
                // actual : {"bar":null}
            };
        }
    }
}
  • F5.
  • Browser shows {"bar": null}
  • instead of {}.

I'm pretty much sure I'm missing something simple, and I feel sorry for posting such a trivial issue, but it's starting to be very time consuming. Thanks for the help.

@MamboJoel
Copy link
Author

Turns out the Nancy.Serialization.JsonNet nuget package must be installed on the project.
See : https://github.com/NancyFx/Nancy.Serialization.JsonNet

@DeckardCain2014
Copy link

Nancy.Serialization.JsonNet is compatible with Nancy if you use the preRelease version 2.0 of Nancy.Serialization.JsonNet as it seems...

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

2 participants