从JObject转换为XDocument时需要保留尾随零

本文关键字:保留 JObject 转换 XDocument | 更新日期: 2023-09-27 18:25:34

我有一个相当简单的请求。我正在C#中创建一个json对象。我有一些属性是替身。最初,我创建了xml,并将这些值格式化为在小数点后有适当数量的尾随零。现在我正在创建json和xml。我正在使用LINQ to json生成json。然后,我使用JSONConvert创建和XDocument。但问题是,如果我将double值作为对象而不是格式化字符串放入JObject,那么与将其存储为字符串相比,我会丢失后面的零
以下是一些示例代码:

double value = 1.0;
string stringVal = "1.000";
JObject test = new JObject(new JProperty("sta", new JObject(
        new JProperty("@valDouble", value),
        new JProperty("@valString", stringVal)
        )));
var testXml = JsonConvert.DeserializeXNode(test.ToString());

我意识到我可以遍历我创建的JObjects,取出值,并用代码手动将其格式化为字符串。但是有没有办法让转换器用某种格式处理值。所以没有得到

<sta valDouble="1" valString="1.000">
</sta>

我想得到,

<sta valDouble="1.000" valString="1.000">
</sta>

我希望我已经很好地解释了这个问题。我已经查看了Deserialize方法,但它们只是有其他选项来选择根元素名称以及是否编写数组属性。在这种情况下他们帮不了我。

从JObject转换为XDocument时需要保留尾随零

好的,我实际上已经找到了解决方案,但它需要修改版本的XmlNodeConverter。为了保持我的影响较小,我实际上对实际代码进行了最小的更改,然后创建了一个派生类。

//added this virtual method
protected virtual string GetAttributeValue(string attributeName, JsonReader reader)
{
    return reader.Value.ToString();
}
private void ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, XmlNamespaceManager manager)
{
  if (string.IsNullOrEmpty(propertyName))
    throw new JsonSerializationException("XmlNodeConverter cannot convert JSON with an empty property name to XML.");
  Dictionary<string, string> attributeNameValues = ReadAttributeElements(reader, manager);
  string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName);
  if (propertyName.StartsWith("@"))
  {
    var attributeName = propertyName.Substring(1);
    //Made the change below to use the new method
    //var attributeValue = reader.Value.ToString();
    var attributeValue = GetAttributeValue(attributeName, reader);
    ...
// Modified to virtual to allow derived class to override.  Added attributeName parameter
protected virtual string ConvertTokenToXmlValue(string attributeName, JsonReader reader)
{
  if (reader.TokenType == JsonToken.String)
  {
    return reader.Value.ToString();
  }
  ....

然后,我创建了以下派生的xml节点转换器。

public class DerivedXmlNodeConverter : Newtonsoft.Json.Converters.XmlNodeConverter
{
private Dictionary<string, string> _attributeFormatStrings;
public Dictionary<string, string> AttributeFormatStrings
{
    get
    {
        if (_attributeFormatStrings == null)
            _attributeFormatStrings = new Dictionary<string,string>();
        return _attributeFormatStrings;
    }
}
protected override string GetAttributeValue(string attributeName, JsonReader reader)
{
    Console.WriteLine("getting attribute value: " + attributeName);
    if (AttributeFormatStrings.ContainsKey(attributeName))
    {
        return string.Format(AttributeFormatStrings[attributeName], reader.Value);
    }
    else
        return base.GetAttributeValue(attributeName, reader);
}
protected override string ConvertTokenToXmlValue(string attributeName, JsonReader reader)
{
    if (AttributeFormatStrings.ContainsKey(attributeName))
    {
        return string.Format(AttributeFormatStrings[attributeName], reader.Value);
    }
    else
        return base.ConvertTokenToXmlValue(attributeName, reader);
} 
}

然后,我不得不复制JsonConvert.DeserializeXNode所做的代码。

    DerivedXmlNodeConverter derived = new DerivedXmlNodeConverter();
    derived.WriteArrayAttribute = false;
    derived.DeserializeRootElementName = null;
    derived.AttributeFormatStrings["valDouble"] = "{0:0.000}";
    JsonSerializerSettings settings = new JsonSerializerSettings { Converters = new JsonConverter[] { derived } };
    StringReader sr = new StringReader(test.ToString(Newtonsoft.Json.Formatting.Indented));
    JsonReader reader = new JsonTextReader(sr);
    JsonSerializer ser = JsonSerializer.CreateDefault(settings);
    ser.CheckAdditionalContent = true;
    XDocument intoXml = (XDocument)(ser.Deserialize(reader, typeof(XDocument)));

我认为这些将是Newtownsoft框架中的巨大变化——允许这些钩子进行自定义。我希望这个代码能帮助其他需要它的人。

-David