力JsonConvert.SerializeXmlNode序列化节点值作为一个整数或布尔 [英] Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean

查看:441
本文介绍了力JsonConvert.SerializeXmlNode序列化节点值作为一个整数或布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SerializeXmlNode 函数 Newtonsoft.Json.JsonConvert 类总是输出的最后一个子节点的值。XML作为序列化过程中一个字符串类型,当有时你可能需要他们被序列化为一个整数或布尔



示例代码:



<预类=郎咸平的XML prettyprint-覆盖> <对象>
<&ID GT; 12< / ID>
<标题> mytitle< /标题>
<可见的GT假LT; /可见光>
< /对象>



输出:



<预类=朗JS prettyprint-覆盖> {ID:12,
标题:mytitle,
可见:假
}

所需的输出:



<预类=朗JS prettyprint-覆盖> {ID:12,
标题:mytitle,
可见:虚假
}

有没有办法强制XML节点被序列化作为一个整数或布尔?

$ b $ ; b

感谢您



请注意:请避免张贴的解决方法时,XML已经是序列化到JSON字符串,因为这些解决办法是那些我们愿意避免的。


解决方案

目前JSON.NET版本不提供所要求的功能,所以我修改的源代码提供此功能:



https://github.com/lukegothic/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs



这修改提供了XmlNodeConverter一种方法来读取称为类型中保存的节点值所需的序列化XML节点可选属性。默认情况下,转换器的序列化的所有值在结果JSON串串,但现在你可以添加指定所需的数据类型的输出属性。允许的类型是整数,浮点数,布尔和日期



例如,如果你有这样的XML源:

 <对象> 
< ID JSON:类型='整'> 12< / ID>
<标题> mytitle< /标题>
<可见JSON:类型='布尔'>假LT; /可见光>
<价格JSON:类型='浮动'> 1.55 LT; /价格>
<到期日期JSON:类型='日期'>&2013-12-31 LT; /到期日期>
< /对象>



这将被序列为:

  {
标识:12,
标题:mytitle,
可见:虚假,
价格: 1.55,
到期日期:2013-12-31T00:00:00
}


The SerializeXmlNode function from Newtonsoft.Json.JsonConvert class always outputs the value of the last child nodes of a XML as a string type in the serialization process, when sometimes you might need them to be serialized as an Integer or a Boolean.

Sample code:

<Object>
  <ID>12</ID>
  <Title>mytitle</Title>
  <Visible>false</Visible>
</Object>

Output:

{ "ID" : "12",
  "Title" : "mytitle",
  "Visible" : "false"
}

Desired output:

{ "ID" : 12,
  "Title" : "mytitle",
  "Visible" : false
}

Is there a way to force a XML node to be serialized as a Integer or a Boolean?

Thank you.

Note: Please avoid posting workarounds when the XML is already serialized to a JSON string, as those workarounds are the ones that we are willing to avoid.

解决方案

The current JSON.NET build doesn't provide the requested feature, so I modified the source code to provide this functionality:

https://github.com/lukegothic/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs

This modification provides the XmlNodeConverter a way to read an optional attribute from XML nodes called "Type" that holds the desired serialization of a node value. By default, the converter serializes all values as string in the result JSON string, but now you can add an attribute that specifies the desired DataType output. The allowed types are Integer, Float, Boolean and Date.

For example, if you have this source XML:

<Object>
  <ID json:Type='Integer'>12</ID>
  <Title>mytitle</Title>
  <Visible json:Type='Boolean'>false</Visible>
  <Price json:Type='Float'>1.55</Price>
  <ExpirationDate json:Type='Date'>2013-12-31</ExpirationDate>
</Object>

It will be serialized as:

{
    "ID":12,
    "Title":"mytitle",
    "Visible":false,
    "Price":1.55,
    "ExpirationDate":"2013-12-31T00:00:00"
}

这篇关于力JsonConvert.SerializeXmlNode序列化节点值作为一个整数或布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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