保存toISODate会反序列化为非iso字符串 [英] Saving a toISODate gets deserialized as a non-iso string

查看:52
本文介绍了保存toISODate会反序列化为非iso字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循通常的建议,即使用toISODate js函数序列化要发送到服务器的javascript Date对象.

I follow the usual recommendations of serializing javascript Date objects to be sent to the server by using the toISODate js function.

事实上,Breeze.js正在为我做这件事,这真是太好了.当我实体上的属性为DateTime时,一切都会按预期进行-日期将以UTC(0偏移日期)日期时间保存到数据库中.

In fact Breeze.js is doing this for me which is great. Everything works as expected when the property on my Entity is a DateTime - the date gets saved to the database as a UTC (0 offset date) datetime.

当Entity上的属性为字符串类型时,我遇到了问题.通过网络发送的日期为' 2013-06-08T23:00:00Z '的日期被反​​序列化为实体上的 string 属性为' 06 /08/2013 23:00:00 ",该值与数据库中保存到varchar backing列中的值相同.

I hit a problem when the property on the Entity is of type string. A date that is sent over the wire as '2013-06-08T23:00:00Z' is being deserialized into the string property on the Entity as '06/08/2013 23:00:00' and this is the same value that is saved into the varchar backing column in the database.

因此该日期将反序列化为" zh-CN "格式的日期(MM/dd/yyyy HH:mm:ss).我对为什么会发生这种情况或如何进行更改感到困惑,以便在将 string 反序列化为 string 属性时,它保持完整.

So the date is being deserialized into a 'en-US' formatted date (MM/dd/yyyy HH:mm:ss). I'm stuck as to why this is happening or how to change things so that the string remains intact as it's deserialized into a string property.

一些技术说明:

  • 我通过将BeforeSaveEntitiesDelegate连接到EFContextProvider来确认属性中的反序列化值,并在保存之前检查调试器中的Entity实例
  • 在服务器上的BeforeSaveEntitiesDelegate方法中检查实体时,我注意到Thread.CurrentThread.CurrentCulture和CurrentUICulture都是"en-GB"
  • 出于技术原因,我需要使用字符串属性而不是DateTime(或DateTimeOffset)-基本上,该属性可以接收任何类型的数据,因此string是适合所有情况的通用格式.

非常欢迎您提供帮助!

谢谢 克里斯蒂安·克罗赫斯特(Christian Crowhurst)

Thanks Christian Crowhurst

推荐答案

对于.NET服务器,Breeze使用JSON.net序列化/反序列化json. Breeze允许您通过自动检测"BreezeConfig"类的任何自定义"实现来配置它.

For a .NET server, Breeze uses JSON.net to serialize/deserialize json. Breeze allows you to configure this by automatically detecting any 'custom' implementations of the 'BreezeConfig' class.

这意味着您可以通过实现BreezeConfig的子类来自定义Breeze对JSON.Net的序列化行为的使用.在您的服务器项目中,可能看起来像这样.

This means that you can customize Breeze's use of JSON.Net's serialization behavior by implementing a subclass of BreezeConfig. This might look something like this within your server project.

using Breeze.ContextProvider;
using Breeze.WebApi2;
using Newtonsoft.Json;

namespace Sample_WebApi2 {

  public class CustomBreezeConfig : BreezeConfig {

  /// <summary>
  /// Override to use a specialized JsonSerializer implementation.
  /// </summary>
  protected override JsonSerializerSettings CreateJsonSerializerSettings() {
    // get the breeze default settings.
    var baseSettings = base.CreateJsonSerializerSettings();
    // Not sure if this is the setting you want but...
    baseSettings.DateParseHandling = DateParseHandling.None;
    return baseSettings;
  }
}

这篇关于保存toISODate会反序列化为非iso字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆