Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Frends.JSON.ConvertXMLStringToJToken/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [1.2.0] - 2025-01-13
### Added
- Added ConvertAsArrayPaths parameter that specify which XML elements should be converted to arrays.

## [1.1.0] - 2024-08-20
### Updated
- Updated Newtonsoft.Json library to the latest version 13.0.3.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,30 @@ public void ShouldConvertXmlStringToJToken()
Assert.IsTrue(result.Success);
Assert.IsInstanceOfType(result.Jtoken, typeof(JObject));
}

[TestMethod]
public void ConvertXMLStringToJToken_WithConvertAsArrayPaths_ShouldTreatSingleItemsAsArrays()
{
var input = new Input()
{
XML = @"<root>
<items>
<item>item1</item>
</items>
<categories>
<category>category1</category>
</categories>
</root>",
ConvertAsArrayPaths = new[] { "$.root.items.item" }
};
var result = JSON.ConvertXMLStringToJToken(input);

Assert.IsTrue(result.Success);

Assert.AreEqual(result.Jtoken["root"]["items"]["item"].Type, JTokenType.Array);
Assert.AreEqual(result.Jtoken["root"]["items"]["item"][0].ToString(), "item1");

Assert.AreEqual(result.Jtoken["root"]["categories"]["category"].Type, JTokenType.String);
Assert.AreEqual(result.Jtoken["root"]["categories"]["category"].ToString(), "category1");
}
Comment thread
jefim marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.ComponentModel;
using System.Linq;
using System.Xml;

namespace Frends.JSON.ConvertXMLStringToJToken;
Expand All @@ -22,6 +23,25 @@ public static Result ConvertXMLStringToJToken([PropertyTab] Input input)
var doc = new XmlDocument();
doc.LoadXml(input.XML);
var jsonString = JsonConvert.SerializeXmlNode(doc);
return new Result(true, JToken.Parse(jsonString));
var jToken = JToken.Parse(jsonString);

if (input.ConvertAsArrayPaths != null && input.ConvertAsArrayPaths.Length > 0)
{
foreach (var path in input.ConvertAsArrayPaths)
{
var matchingTokens = jToken.SelectTokens(path).ToList();

foreach (var token in matchingTokens)
{
if (token.Type != JTokenType.Array)
{
var array = new JArray(token);
token.Replace(array);
}
}
}
}
Comment thread
jefim marked this conversation as resolved.

return new Result(true, jToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ public class Input
/// </summary>
/// <example>&lt;?xml version='1.0' standalone='no'?&gt;&lt;root&gt;&lt;foos id = '1' &gt;&lt;foo&gt;bar&lt;/name&gt;&lt;/foos&gt;&lt;/root&gt;</example>
public string XML { get; set; }

/// <summary>
/// A list of JSONPath expressions that specify which JSON elements should always be converted to arrays.
/// </summary>
/// <example>
/// new[] { "$.root.items.item" }
/// </example>
public string[] ConvertAsArrayPaths { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down