Json.NET中仅一个属性的自定义序列化程序 [英] custom serializer for just one property in Json.NET

查看:93
本文介绍了Json.NET中仅一个属性的自定义序列化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新 发现了问题-是从错误的类继承的,必须是JsonConverter.

我有一个类,其位置属性类型为System.Data.Entity.Spatial.DbGeography.默认的Json.NET序列化程序将输出JSON文本,如下所示:

   ...
  "PlaceType": 0,
  "Location": {
    "Geography": {
      "CoordinateSystemId": 4326,
      "WellKnownText": "POINT (-88.00000 44.00000)"
    }
  },
  "AddedDT": null,
  ...
 

我希望它显示出这样的文字:

   ...
  "PlaceType": 0,
  "Location": [-88.00000,44.00000],
  "AddedDT": null,
  ...
 

...所以在我看来,我应该做的是重写DbGeography类型上当前使用的任何转换器.

到目前为止,我所看到的使用CustomCreationConverters和ContractResolvers的示例似乎解决了如何为要序列化的主类而不是仅是该类属性的类型替换序列化器.涉及注释被覆盖的类的示例对我不起作用,因为我没有在代码中定义DbGeography,并且由于它没有构造函数并且只能由internal工厂方法实例化,因此它实际上是一个密封的类. /p>

是否可以流畅地将JsonConverter应用于类型?如果是这样,转换器将是什么样?我是否只重写WriteJson()方法?

解决方案

原来,我只需要从JsonConverter继承而不是从CustomCreationConverter继承,我尝试更改的其他所有内容都可以.

我仍然不确定是否可以流畅地应用JsonConverter,但是还有另一种方法可以应用JsonConverter,而无需在您的域/核心项目中引用Json.NET或使用对外围设备的引用来标记您的域类库:

var jsonSerializer = new JsonSerializer();
jsonSerializer.Converters.Add(new DbGeographyConverter());
jsonSerializer.Serialize(jsonWriter, place);

UPDATE Found the issue -- was inheriting from wrong class, needed to be JsonConverter.

I have a class that has a Location property of type System.Data.Entity.Spatial.DbGeography. The default Json.NET serializer puts out JSON text like this:

  ...
  "PlaceType": 0,
  "Location": {
    "Geography": {
      "CoordinateSystemId": 4326,
      "WellKnownText": "POINT (-88.00000 44.00000)"
    }
  },
  "AddedDT": null,
  ...

I want it to put out text like this:

  ...
  "PlaceType": 0,
  "Location": [-88.00000,44.00000],
  "AddedDT": null,
  ...

...so it seems to me what I should do would be to override whatever converter is currently being used on the DbGeography type.

The examples I've seen so far that use CustomCreationConverters and ContractResolvers seem to address how you'd replace the serializer for the main class being serialized, not for a type that's only a property of that class. The examples that involve annotating the class that's being overridden don't work for me because I don't define DbGeography in my code and it's effectively a sealed class because it has no constructor and can only be instantiated by internal factory methods.

Is there a way to apply a JsonConverter to a type fluently? If so, what would the converter look like? Do I just override the WriteJson() method?

解决方案

Turns out I just needed to inherit from JsonConverter instead of CustomCreationConverter, and everything else I was trying to change was OK all along.

I'm still not sure if there's a way to apply the JsonConverter fluently, but there is another way to apply the JsonConverter without referencing Json.NET in your domain/core project or marking up your domain classes with references to a peripheral library:

var jsonSerializer = new JsonSerializer();
jsonSerializer.Converters.Add(new DbGeographyConverter());
jsonSerializer.Serialize(jsonWriter, place);

这篇关于Json.NET中仅一个属性的自定义序列化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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