Manatee.Json icon indicating copy to clipboard operation
Manatee.Json copied to clipboard

XElement.ToJson + Json.ToXElement don't round-trip

Open SteveGilham opened this issue 4 years ago • 1 comments

Although it looks like it was intended to, with the adding of the "-" sigil to distinguish attribute names in the resulting JSON, exporting XML as JSON text and then re-importing throws,

To Reproduce Run this

using System;
using System.Xml.Linq;

using Manatee.Json;

namespace Example
{
  internal class Program
  {
    private static void Main(string[] args)
    {
      var xmltext = "<Items><Item k=\"1\" v=\"a\" /><Item k=\"2\" v=\"b\" /></Items>";
      var xml = XDocument.Parse(xmltext);

      var jsontext = XmlExtensions.ToJson(xml.Root).ToString();

      var json = JsonValue.Parse(jsontext);

      var xmlout = json.ToXElement("dummy");

      Console.WriteLine("{0}", xmlout);
    }
  }
}

Expected behavior A default formatted version of the input xmltext value printed out.

Actual behaviour Raises

System.Xml.XmlException
  HResult=0x80131940
  Message=Name cannot begin with the '-' character, hexadecimal value 0x2D.
  Source=System.Private.Xml
  StackTrace:
   at System.Xml.XmlConvert.VerifyNCName(String name, ExceptionType exceptionType) in /_/src/System.Private.Xml/src/System/Xml/XmlConvert.cs:line 417
   at System.Xml.XmlConvert.VerifyNCName(String name) in /_/src/System.Private.Xml/src/System/Xml/XmlConvert.cs:line 406
   at System.Xml.Linq.XName..ctor(XNamespace ns, String localName) in /_/src/System.Private.Xml.Linq/src/System/Xml/Linq/XName.cs:line 26
   at System.Xml.Linq.XNamespace.GetName(String localName, Int32 index, Int32 count) in /_/src/System.Private.Xml.Linq/src/System/Xml/Linq/XNamespace.cs:line 223
   at System.Xml.Linq.XNamespace.GetName(String localName) in /_/src/System.Private.Xml.Linq/src/System/Xml/Linq/XNamespace.cs:line 59
   at System.Xml.Linq.XName.Get(String expandedName) in /_/src/System.Private.Xml.Linq/src/System/Xml/Linq/XName.cs:line 85
   at System.Xml.Linq.XName.op_Implicit(String expandedName) in /_/src/System.Private.Xml.Linq/src/System/Xml/Linq/XName.cs:line 108
   at Manatee.Json.XmlExtensions._GetXName(String key)
   at Manatee.Json.XmlExtensions.ToXElement(JsonValue json, String key)
   at Manatee.Json.XmlExtensions.ToXElement(JsonValue json, String key)
   at Manatee.Json.XmlExtensions.ToXElement(JsonValue json, String key)
   at Manatee.Json.XmlExtensions.ToXElement(JsonValue json, String key)
   at Manatee.Json.XmlExtensions.ToXElement(JsonValue json, String key)
   at Example.Program.Main(String[] args) in C:\Users\steve\source\repos\ClassLibrary1\ConsoleApp6\Program.cs:line 19

Desktop (please complete the following information):

  • OS: Win10 Home
  • .Net Target: netcoreapp3.1
  • Version: 13.0.4

SteveGilham avatar Jan 21 '21 08:01 SteveGilham

@SteveGilham thanks for the report. I'm surprised that someone is using this functionality. I included it at a whim when I first created this library quite a long time ago.

That said, I've stopped maintaining this library. If you'd like to explore the issue, I'm happy to accept PRs. Alternatively, you're welcome to take the code and use it at your own discretion.

gregsdennis avatar Jan 21 '21 09:01 gregsdennis