如何为“自定义”JsonDotNetResult提供可选设置?

时间:2023-01-29 02:22:00

I have an alternate JsonResult, being the class below, to provide a better JSON serializer than the default for MVC Web Apps (irrelevant bits omitted):

我有一个替代的JsonResult,作为下面的类,它提供了一个比默认的MVC Web应用程序更好的JSON序列化器(不相关的部分省略了):

public class JsonDotNetResult : JsonResult
{
    public readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        PreserveReferencesHandling = PreserveReferencesHandling.Arrays,
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        ContractResolver = new CamelCasePropertyNamesContractResolver(),
        Converters = new List<JsonConverter> { new StringEnumConverter() }
    };

    public override void ExecuteResult(ControllerContext context)
    {
        ...
        var response = context.HttpContext.Response;
        ...
        response.Write(JsonConvert.SerializeObject(Data, Settings));
    }
}

Then I have an overload for Controller.Json, declared as:

然后是控制器的重载。Json,声明为:

protected JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior, JsonSerializerSettings settings = null)

but I don't have the existing default settings set in the Settings field of JsonDotNetResult, so if I pass in a new JsonSerializerSettings, with a few properties set by the caller, I will overwrite all the properties of the Settings object with the default values for JsonSerializerSettings, and with the new values provided by the caller. The latter being desired, the former I'm trying to avoid.

但我没有现有的默认设置中设置设置JsonDotNetResult领域,所以如果我通过一个新的JsonSerializerSettings,由调用者和一些属性设置,我将覆盖所有的属性设置为JsonSerializerSettings对象使用默认值,调用者提供的新值。后者是我想要避免的。

How can I pass in a subset of JsonSerializerSettings property values and apply them to the Settings object in JsonDotNetResult? My best (and first) effort is to loop through the properties of the default Settings object and compare them to property values on a new JsonSerializerSettings object passed by the user, and where the differ, set the value on Settings to the value supplied by the user.

如何传递jsonalizersettings属性值的子集并将它们应用到JsonDotNetResult中的Settings对象?我最大的(也是第一个)努力是循环使用默认设置对象的属性,并将它们与用户传递的新JsonSerializerSettings对象上的属性值进行比较,不同之处在于,将设置上的值设置为用户提供的值。

1 个解决方案

#1


1  

I would define global default settings for JsonConvert in Global.asax.cs:

我将在global .asax.cs中定义JsonConvert的全局默认设置:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    JsonConvert.DefaultSettings = () => new JsonSerializerSettings
    {
        PreserveReferencesHandling = PreserveReferencesHandling.Arrays,
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        ContractResolver = new CamelCasePropertyNamesContractResolver(),
        Converters = new List<JsonConverter> { new StringEnumConverter() }
    };
}

JsonConvert.DefaultSettings is a lambda function that creates default settings used by converter. When you supply your own setting the default one will get overridden. So, then whenever you want to change that settings just call

JsonConvert。DefaultSettings是一个lambda函数,它创建转换器使用的默认设置。当您提供自己的设置时,默认设置将被覆盖。所以,无论何时你想改变设置,只要调用。

var settings = (JsonSerializerSettings)JsonConvert.DefaultSettings.Invoke();

to get new copy of default settings that you can modify as you need.

要获得默认设置的新副本,可以根据需要进行修改。

settings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;

That way you can remove readonly JsonSerializerSettings Settings = new JsonSerializerSettings from JsonDotNetResult because it will use default settings anyway:

这样你就可以从JsonDotNetResult中删除readonly JsonSerializerSettings =新的jsonalizersettings,因为它将使用默认设置:

public class JsonDotNetResult : JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        ...
        var response = context.HttpContext.Response;
        ...
        response.Write(JsonConvert.SerializeObject(Data));
    }
}

What I like in this method is that you have your default settings in one place so it keeps your code clean and easy to maintain.

在这个方法中,我喜欢的是在一个地方设置默认设置,这样可以保持代码的整洁和易于维护。

#1


1  

I would define global default settings for JsonConvert in Global.asax.cs:

我将在global .asax.cs中定义JsonConvert的全局默认设置:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    JsonConvert.DefaultSettings = () => new JsonSerializerSettings
    {
        PreserveReferencesHandling = PreserveReferencesHandling.Arrays,
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        ContractResolver = new CamelCasePropertyNamesContractResolver(),
        Converters = new List<JsonConverter> { new StringEnumConverter() }
    };
}

JsonConvert.DefaultSettings is a lambda function that creates default settings used by converter. When you supply your own setting the default one will get overridden. So, then whenever you want to change that settings just call

JsonConvert。DefaultSettings是一个lambda函数,它创建转换器使用的默认设置。当您提供自己的设置时,默认设置将被覆盖。所以,无论何时你想改变设置,只要调用。

var settings = (JsonSerializerSettings)JsonConvert.DefaultSettings.Invoke();

to get new copy of default settings that you can modify as you need.

要获得默认设置的新副本,可以根据需要进行修改。

settings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;

That way you can remove readonly JsonSerializerSettings Settings = new JsonSerializerSettings from JsonDotNetResult because it will use default settings anyway:

这样你就可以从JsonDotNetResult中删除readonly JsonSerializerSettings =新的jsonalizersettings,因为它将使用默认设置:

public class JsonDotNetResult : JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        ...
        var response = context.HttpContext.Response;
        ...
        response.Write(JsonConvert.SerializeObject(Data));
    }
}

What I like in this method is that you have your default settings in one place so it keeps your code clean and easy to maintain.

在这个方法中,我喜欢的是在一个地方设置默认设置,这样可以保持代码的整洁和易于维护。