Json.NET,可以扩展SerializeXmlNode来检测数字吗? [英] Json.NET, can SerializeXmlNode be extended to detect numbers?

查看:73
本文介绍了Json.NET,可以扩展SerializeXmlNode来检测数字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SerializeXmlNode从XML转换为JSON.看起来预期的行为是将所有XML值都转换为字符串,但是我想在适当的地方发出真实的数字值.

I am converting from XML to JSON using SerializeXmlNode. Looks the expected behavior is to convert all XML values to strings, but I'd like to emit true numeric values where appropriate.

// Input:   <Type>1</Type>
string json = JsonConvert.SerializeXmlNode(node, Newtonsoft.Json.Formatting.Indented, true);
// Output:  "Type": "1"
// Desired: "Type": 1

我是否需要编写一个自定义转换器来执行此操作,或者是否有办法通过委托在适当的时候加入序列化过程?还是我必须编写自己的自定义JsonConverter类来管理过渡?

Do I need to write a custom converter to do this, or is there a way to hook into the serialization process at the appropriate points, through delegates perhaps? Or, must I write my own custom JsonConverter class to manage the transition?

鉴于适当解决方案的复杂性,这是另一种解决方案(我并不完全为此感到骄傲,但它确实有效...).

Given the complexity of a proper solution, here is another (which I'm not entirely proud of, but it works...).

// Convert to JSON, and remove quotes around numbers
string json = JsonConvert.SerializeXmlNode(node, Newtonsoft.Json.Formatting.Indented, true);

// HACK to force integers as numbers, not strings.
Regex rgx = new Regex("\"(\\d+)\"");
json = rgx.Replace(json, "$1");

推荐答案

XML无法像JSON一样区分原始类型.因此,当直接将XML转换为JSON时,Json.Net不知道值应该是什么类型,这简直就是猜测.如果始终假定仅由数字组成的值是序数,则邮政编码和邮政编码前加零的电话号码之类的东西将在转换中被破坏.那么,Json.Net走上安全道路并将所有值都视为字符串就不足为奇了.

XML does not have a way to differentiate primitive types like JSON does. Therefore, when converting XML directly to JSON, Json.Net does not know what types the values should be, short of guessing. If it always assumed that values consisting only of digits were ordinal numbers, then things like postal codes and phone numbers with leading zeros would get mangled in the conversion. It is not surprising, then, that Json.Net takes the safe road and treats all values as string.

解决此问题的一种方法是将XML反序列化为中间对象,然后将其序列化为JSON.由于中间对象具有强类型化的属性,因此Json.Net知道要输出什么.这是一个示例:

One way to work around this issue is to deserialize your XML to an intermediate object, then serialize that to JSON. Since the intermediate object has strongly typed properties, Json.Net knows what to output. Here is an example:

class Program
{
    static void Main(string[] args)
    {
        string xml = @"<root><ordinal>1</ordinal><postal>02345</postal></root>";

        XmlSerializer xs = new XmlSerializer(typeof(Intermediary));
        using (TextReader reader = new StringReader(xml))
        {
            Intermediary obj = (Intermediary)xs.Deserialize(reader);
            string json = JsonConvert.SerializeObject(obj , Formatting.Indented);
            Console.WriteLine(json);
        }
    }
}

[XmlRoot("root")]
public class Intermediary
{
    public int ordinal { get; set; }
    public string postal { get; set; }
}

以上内容的输出

{
  "ordinal": 1,
  "postal": "02345"
}

是一个更通用的解决方案,是的,您必须编写自己的转换器.实际上,调用SerializeXmlNode时发生的XML到JSON的转换是使用Json.Net附带的XmlNodeConverter完成的.该转换器本身似乎不是很可扩展,但是您可以始终使用其

To make a more generic solution, yes, you'd have to write your own converter. In fact, the XML-to-JSON conversion that takes place when calling SerializeXmlNode is done using an XmlNodeConverter that ships with Json.Net. This converter itself does not appear to be very extensible, but you could always use its source code as a starting point to creating your own.

这篇关于Json.NET,可以扩展SerializeXmlNode来检测数字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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