【问题标题】:How do you implement the NodaTime JSON converters to be utilized by NEST?您如何实现 NEST 使用的 NodaTime JSON 转换器?
【发布时间】:2014-02-12 17:40:13
【问题描述】:

我已经索引了一个这样定义的对象:

public class CourseOffering
{
    public int CourseId { get; set; }
    public string Title { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public ICollection<TimeBlock> TimeBlocks { get; set; }
}

TimeBlock 类定义为:

public class TimeBlock
{
    public DayOfWeek Day { get; set; }
    public LocalTime StartTime { get; set; }
    public LocalTime EndTime { get; set; }
}

调用动态搜索方法时,一切正常(大概是因为它不知道要反序列化到什么)。结果正确返回。

在调用通用搜索方法并传入 CourseOffering 类型时,默认序列化程序在反序列化 LocalTime 对象时出现问题。我得到以下 JsonReaderException:

读取整数时出错。意外标记:StartObject。路径 'hits.hits[0]._source.timeBlocks[0].startTime',第 1 行,位置 655。

我已经尝试像这样添加 LocalTimeConverter:

var settings = new ConnectionSettings(uri);
settings.SetDefaultIndex(index);
settings.AddContractJsonConverters(t => typeof (LocalTime).IsAssignableFrom(t) ? NodaConverters.LocalTimeConverter : null);

但它会导致这个 JsonReaderException:

无法将字符串转换为整数:12:30:00。路径 'hits.hits[0]._source.timeBlocks[0].startTime',第 1 行,位置 664。

老实说,我无法判断我做错了什么,或者某个地方是否存在问题。任何帮助将不胜感激。

【问题讨论】:

  • 看起来您正在尝试使用 Json.NET 转换器 - 但该方法实际上是否需要 DataContractJsonSerializer

标签: c# nest nodatime


【解决方案1】:

我今天学到了新的一课:始终在 Stack Overflow 上发布与问题中出现的完全相同的代码。为了简化我遇到的问题,我遗漏了一个关于序列化/反序列化对象的非常明显的问题:我的 TimeBlock 类上没有公共无参数构造函数。

为了清楚起见,这是我实际的 TimeBlock 类:

public class TimeBlock
{
    public DayOfWeek Day { get; set; }
    public LocalTime StartTime { get; set; }
    public LocalTime EndTime { get; set; }

    public TimeBlock(int day, int startTime, int endTime)
    {
        Day = (DayOfWeek)day;
        StartTime = new LocalTime(startTime / 100, startTime % 100);
        EndTime = new LocalTime(endTime / 100, endTime % 100);
    }

    public string GetDayOfWeekString()
    {
        return CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(Day);
    }
}

添加本地构造函数使一切变得美好。

public TimeBlock() {}

【讨论】:

    【解决方案2】:

    使用 NEST 5.5.0 / Nodatime 2.2.2,我遇到了类似的错误,但必须使用以下命令来启用 Nodatime 的 JSON 转换器:

    var pool = new SingleNodeConnectionPool( new Uri( "http://localhost:9200" ) );
    var settings = new ConnectionSettings( pool, new HttpConnection(), new SerializerFactory( ( s, v ) => s.ConfigureForNodaTime( DateTimeZoneProviders.Tzdb ) ) );
    

    【讨论】:

      【解决方案3】:

      NEST 7.13.2 和 NodaTime 3.0.5 的现代答案

      需要额外的 NuGet 包:

      • NEST.JsonNetSerializer
      • NodaTime.Serialization.JsonNet

      实施:

      var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
      
      var settings = new ConnectionSettings(
          pool,
          sourceSerializer: (s, v) =>
              new JsonNetSerializer(s, v,
                  () => new JsonSerializerSettings()
                      .ConfigureForNodaTime(DateTimeZoneProviders.Tzdb)
              )
          );
      
      return new ElasticClient(settings);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2023-01-27
        相关资源
        最近更新 更多