Path: redb-route/tests/redb.Route.Tests.DataFormats/XmlJsonJsonToXmlTests.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.DataFormats/XmlJsonJsonToXmlTests.cs ---- using System.Text; using System.Xml.Linq; using AwesomeAssertions; using redb.Route.DataFormats.XmlJson; using redb.Route.DataFormats.XmlJson.Internal; namespace redb.Route.Tests.DataFormats; /// /// xmljson, JSON to XML without a schema (step S5): the reverse notation, root=, arrays into repeated elements, the /// plural wrapper, namespaces from @xmlns keys, and the round trip of the S2 documents. /// public sealed class XmlJsonJsonToXmlTests { private static string Xml(string json, Action? configure = null) { var options = new XmlJsonOptions(); configure?.Invoke(options); var converter = new JsonToXml(options); return converter.Convert(Encoding.UTF8.GetBytes(json)).Root!.ToString(SaveOptions.DisableFormatting); } [Theory] [InlineData("""{"a":"5"}""", "5")] [InlineData("""{"a":""}""", "")] [InlineData("""{"a":12.50}""", "12.50")] [InlineData("""{"a":true}""", "true")] [InlineData("""{"a":{"@id":"1","#text":"x"}}""", """x""")] [InlineData("""{"a":{"@id":7,"b":"2"}}""", """2""")] [InlineData("""{"a":{"@id":null,"b":"2"}}""", "2")] [InlineData("""{"o":{"i":["1","2"]}}""", "12")] [InlineData("""{"o":{"i":[]}}""", "")] [InlineData("""{"a":"Привет"}""", "Привет")] public void TheNotation(string json, string xml) { Xml(json).Should().Be(xml); } [Fact] public void Null_IsXsiNil_WithTheNamespaceDeclared() { Xml("""{"r":{"a":null}}""").Should().Be(""""""); } [Fact] public void Namespaces_FromXmlnsKeys() { Xml("""{"s:Envelope":{"@xmlns:s":"urn:s","@xmlns":"urn:d","s:Body":{"Order":{"@s:id":"7","#text":"x"}}}}""") .Should().Be("""x"""); } [Fact] public void AnUndeclaredPrefix_IsAnError() { var act = () => Xml("""{"o:Order":{"Id":"1"}}"""); act.Should().Throw() .WithMessage("xmljson: /o:Order: the prefix 'o' is not declared; declare it with an \"@xmlns:o\" key on this element or an enclosing one."); } [Fact] public void Root_WrapsTheWholeJson() { Xml("""{"Id":"1","Tag":["a","b"]}""", o => o.Root = "Order").Should().Be("1ab"); Xml("""{"Item":["a","b"]}""", o => o.Root = "List").Should().Be("ab"); var act = () => Xml("""["a","b"]""", o => o.Root = "List"); act.Should().Throw().WithMessage("xmljson: the JSON is an array, and its items have no element name*root=List."); } [Theory] [InlineData("""{"a":"1","b":"2"}""", "xmljson: the JSON is an object of 2 key(s); XML has one root element: give the JSON one top-level key, or name the root with root=.")] [InlineData("""["a"]""", "xmljson: the JSON is an array; XML has one root element: give the JSON one top-level key, or name the root with root=.")] [InlineData("""{"a":["1"]}""", "xmljson: the top-level key 'a' holds an array; XML has one root element: name the root with root=.")] [InlineData("""{"a":{"first name":"x"}}""", "xmljson: /a/first name: 'first name' is not an XML name.")] [InlineData("""{"a":{"1x":"x"}}""", "xmljson: /a/1x: '1x' is not an XML name.")] [InlineData("""{"a":{"b":[["1"]]}}""", "xmljson: /a/b is an array inside an array; XML repeats an element, it cannot nest a list in a list.")] [InlineData("""{"a":{"@id":{"x":"1"}}}""", "xmljson: /a/@id holds an object of 1 key(s), where XML has text: an attribute, a text key or a namespace.")] [InlineData("not json", "xmljson: the body is not JSON*")] public void WhatXmlCannotHold_IsAnError(string json, string message) { var act = () => Xml(json); act.Should().Throw().WithMessage(message); } [Fact] public void ThePluralRule_WrapsAnArrayInItsPlural() { Xml("""{"r":{"Incidents":["1","2"]}}""", o => o.Plural = true) .Should().Be("12"); Xml("""{"r":{"Categories":["a"],"Addresses":["b"],"Cases":["c"]}}""", o => o.Plural = true) .Should().Be("a
b
c
"); } [Fact] public void ThePluralRule_IsSilentOnOtherNames_AndOffByDefault() { Xml("""{"r":{"Data":["1"]}}""", o => o.Plural = true).Should().Be("1"); Xml("""{"r":{"Incidents":["1"]}}""").Should().Be("1"); } [Fact] public void TheKeys_AreConfigurable() { Xml("""{"a":{"-id":"1","$":"x"}}""", o => { o.TextKey = "$"; o.AttributePrefix = "-"; }).Should().Be("""
x"""); } [Theory] [InlineData("5")] [InlineData("")] [InlineData("""x""")] [InlineData("""23""")] [InlineData("""x""")] [InlineData("""x""")] [InlineData("1")] [InlineData("Привет")] public void XmlToJsonToXml_RoundTrips(string xml) { var options = new XmlJsonOptions { Arrays = { "//Item" } }; var toJson = new XmlToJson(options); var json = toJson.Write(toJson.Convert(Encoding.UTF8.GetBytes(xml))); var back = new JsonToXml(options).Convert(json); Normalize(back.Root!).Should().Be(Normalize(XElement.Parse(xml))); } [Fact] public void Write_IsUtf8WithADeclarationAndNoBom() { var converter = new JsonToXml(new XmlJsonOptions()); var bytes = converter.Write(converter.Convert(Encoding.UTF8.GetBytes("""{"a":"Ж"}"""))); bytes[0].Should().Be((byte)'<'); Encoding.UTF8.GetString(bytes).Should().Be("""Ж"""); } /// An empty element written as <a></a> or <a/> is the same element. private static string Normalize(XElement element) { var copy = new XElement(element); foreach (var e in copy.DescendantsAndSelf().Where(e => !e.HasElements && e.Value.Length == 0)) e.RemoveNodes(); return copy.ToString(SaveOptions.DisableFormatting); } }