本文介绍了配置JsonNetSerializer使用南希TinyIoC JsonNetBodyDeserializer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是一个小白南希。我一直在使用它作为一个框架,以产生一个REST API。我所熟悉的Json.NET,所以我一直在玩的 Nancy.Serialization.JsonNet 包。I am a noob to Nancy. I have been using it as a framework to produce a REST API. I am familiar with Json.NET so I've been playing with the Nancy.Serialization.JsonNet package. 我的目标:自定义行为(即更改设置)的 JsonNetSerializer 和 JsonNetBodyDeserializer My goal: to customize the behavior (i.e. change the settings) of the JsonNetSerializer and JsonNetBodyDeserializer.我特别想包含以下设置... Specifically I'd like to incorporate the following settings...var settings = new JsonSerializerSettings { Formatting = Formatting.Indented };settings.Converters.Add( new StringEnumConverter { AllowIntegerValues = false, CamelCaseText = true } ); 我想使用内建的TinyIoC容器内,避免继承链,并限制所产生的潜在问题来执行此定制从 Nancy.Serialization.JsonNet 包中的任何变化。 的注:作为暂时的解决办法,我已经利用继承来创建 CustomJsonNetSerializer 和 CustomJsonNetBodyDeserializer 。的我曾尝试几种方法来至少将这一配置的 JsonNetSerializer 。我还没有尝试配置 JsonNetBodyDeserializer 使用TinyIoC呢。我想它会同样进行。所有我尝试过的工作是在我的 CustomNancyBootstrapper 。I have tried several approaches to incorporate this configuration at least for the JsonNetSerializer. I've not tried configuring the JsonNetBodyDeserializer using the TinyIoC yet. I imagine it will be done similarly. All the work I've tried is in my CustomNancyBootstrapper (which inherits from DefaultNancyBootstrapper).最成功的方法至今:覆盖 ConfigureApplicationContainer Most successful approach so far: override ConfigureApplicationContainerprotected override void ConfigureApplicationContainer( TinyIoCContainer container ){ base.ConfigureApplicationContainer( container ); // probably don't need both registrations, and I've tried only keeping one or the other var settings = new JsonSerializerSettings { Formatting = Formatting.Indented }; settings.Converters.Add( new StringEnumConverter { AllowIntegerValues = false, CamelCaseText = true } ); container.Register( new JsonNetSerializer( JsonSerializer.CreateDefault( settings ) ) ); container.Register<ISerializer>( new JsonNetSerializer( JsonSerializer.CreateDefault( settings ) ) );} 我已经追踪代码,并观看了 JsonNetSerializer( 。JsonSerializer串行)在JsonNet包构造潜在的问题:我注意到构造函数被调用两次。没想到这一行为。Potential problem: I noticed the constructor is called twice. I did not expect this behavior.第一次一切都刚刚好 - 我的定制添加和正确注册。但是,则第二时间发生和类型都重新登记,没有设置的定制。重新注册出现,以取代原登记失去了我的设置,自定义设置。The first time everything is just right - my customization is added and registered properly. But, then the second time happens and the types are re-registered, without the settings customization. The re-registration appears to replace the original registration losing my settings customization.调用堆栈中的第二次调用构造函数表明,它在 GetEngine 和 GetEngineInternal 这似乎试图建立一个 NancyEngine (我用自主机包,以便这种情况发生在Program.cs中 - 使用(VAR主机=新NancyHost(URI)))The call stack the second time the constructor is called shows that it is called during GetEngine and GetEngineInternal which seems to try to build a NancyEngine (I am using the self-host package so this happens in program.cs -- using(var host = new NancyHost(uri)) ).好像我不是要告诉南希不要做某件事,或者我需要挂钩到链中稍后的一部分。Seems like I either need to tell Nancy not to do something or I need to hook in to a later part in the chain.任何帮助将是。欣赏推荐答案一般来说,在南希来解决这个问题的方法是实现自己的JSON序列化,像这样:Typically the way to solve this in Nancy is to implement your own JSON Serializer like so:public sealed class CustomJsonSerializer : JsonSerializer{ public CustomJsonSerializer() { ContractResolver = new CamelCasePropertyNamesContractResolver(); Converters.Add(new StringEnumConverter { AllowIntegerValues = false, CamelCaseText = true }); Formatting = Formatting.Indented; }} 在这里,您可以覆盖所有设置。 Here you can override all the settings. 然后你可以使用 IRegistrations public class JsonRegistration : IRegistrations{ public IEnumerable<TypeRegistration> TypeRegistrations { get { yield return new TypeRegistration(typeof(JsonSerializer), typeof(CustomJsonSerializer)); } } public IEnumerable<CollectionTypeRegistration> CollectionTypeRegistrations { get; protected set; } public IEnumerable<InstanceRegistration> InstanceRegistrations { get; protected set; }} 问: 如何做到这一点办法从创建CustomJsonNetSerializer从JsonNetSerializer继承,然后在ConfigureApplicationContainer(container.Register(typeof运算(JsonNetSerializer)的typeof(CustomJsonNetSerializer))注册它有什么不同?的 答:的JsonSerializer是执行,如果json.net南希,这是我们定义在GitHub上的自述推荐的方法:A: The JsonSerializer is the implementation if json.net for Nancy, this is the recommended method we define on the readme on github: https://github.com/NancyFx/Nancy.Serialization.JsonNet#customization 你提到的类是对象到JSON的序列化,还有一个负责处理反序列化,这两者利用JsonSerializer内部:The class you mention is the serialization of an object to JSON, there is another which handles deserialization, both of which utilize JsonSerializer internally: https://github.com/NancyFx/ Nancy.Serialization.JsonNet / BLOB /主/ src目录/ Nancy.Serialization.JsonNet / JsonNetSerializer.cs#L10 使用这种方法使执行设置保持一致任何地方,该JsonSerializer被使用。 Using this method makes the implementation settings consistent anywhere that the JsonSerializer is used. 问: 我能正确地从你介绍,我将不再需要在明确注册CustomJsonSerializer的做法推断?ConfigureApplicationContainer覆盖在我的CustomNancyBootstrapper 的 答:我已经登记完成的方法只是用于注册依赖清洁抽象,而不是使一个巨大的引导程序,你可以创建一个小一些特定的类。 A: The method I've done for registering is just a cleaner abstraction for registering dependencies, rather than making one giant Bootstrapper, you can create a few smaller specific classes. 使用我的方法是意味着你不需要引导程序进行注册。Yes using my method means you do not need to register in the bootstrapper. 这篇关于配置JsonNetSerializer使用南希TinyIoC JsonNetBodyDeserializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 01:00