Home > Enterprise >  Newtonsoft.Json convertion xml document to json with array detection
Newtonsoft.Json convertion xml document to json with array detection

Time:03-02

what I am trying to do is to convert complex xml document to json format, and I am using Newtonsoft.Json to achieve my goal. and I have came across small - big problem. So for example

I have a model that looks like:

public class assets
{
    public UInt32 id {get; set;}
    public String providerName {get; set;}
    public String provider {get; set;}
    public String realm {get; set;}
    public ICollection<unit> unit {get; set;}
}

My intention is that user will stream xml content to method that will change that xml to json and i will post it to API.

To simplify User is pasting simple xml (normal xml is far more complex, but basically it would looks like many levels of example bellow)

<assets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="assets.xsd" providerName="myProviderName" provider="myProvider" realm="myCatalog">
  <unit idKey="newGeo_63119679"></unit>
  <unit idKey="newGeo_63119179"></unit>
</assets>

Json result will look like:

{"@providerName":"myProviderName","@provider":"myProvider","@realm":"myCatalog","unit":[{"@idKey":"newGeo_63119679"},{"@idKey":"newGeo_63119577"}]}

So service that does all the magic looks like:

public async ValueTask<string> AddAsset(string body)
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(body);
    string json = JsonConvert.SerializeXmlNode(doc, Formatting.None, true);
    
    HttpResponseMessage response = await this._httpClient.PostAsJsonAsync("/Asset/create_asset", json);
    string responseJson = await response.Content.ReadAsStringAsync();
    return responseJson;
}

Well OK this part works, but when I remove from xml one unit node (so only one unit node is left), my result is:

{"@providerName":"myProviderName","@provider":"myProvider","@realm":"myCatalog","unit":{"@idKey":"newGeo_63119679"}}

And now so needed array to deserialize it to model is gone. I know I could manipulate xml attributes to add json:Array='true'.

But I was wondering if there is more complex solution for example JsonConverter that can take search for property in given type and check it if its collection and if so assign it as json collection. How can I bite this problem?

And Also as I checked SerializeXmlNode has no converter parameter.

CodePudding user response:

Well, I found my answer, by mixing information from @dbc and my happy innovation:

public class Converter<TEntity> : JsonConverter<TEntity> where TEntity : class
{
    private readonly IEnumerable<Type> _entityTypes =
            Assembly.GetExecutingAssembly().GetReferencedAssemblies()
                .SelectMany(assembly => Assembly.Load(assembly).GetTypes().Where(t => t.Namespace == MigrationService.EntitiesNameSpace));

    public override bool CanWrite => false;
    
    public override void WriteJson(JsonWriter writer, TEntity value, JsonSerializer serialize) =>
        throw new NotImplementedException($"Write option is turned off for {nameof(CollectionConverter)} custom json converter");

    public override TEntity ReadJson(JsonReader reader, Type objectType, TEntity existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        void SetSimpleTypes(JToken token, TEntity instance, PropertyInfo property)
        {
            var value = token[property.Name];
            if(value == null) return;
            var cast = Convert.ChangeType(value, property.PropertyType);
            property.SetValue(instance, cast);
        }

        void SetClassTypes(JToken token, TEntity instance, PropertyInfo property)
        {
            var constructedConverterType = typeof(CollectionConverter<>).MakeGenericType(property.PropertyType);
            if (Activator.CreateInstance(constructedConverterType) is not JsonConverter converter) return;
            var propertyInstance = JsonConvert.DeserializeObject(token[property.Name].ToString(), property.PropertyType, converter);
            property.SetValue(instance, propertyInstance);
        }
        
        void SetCollectionTypes(JToken token, TEntity instance, PropertyInfo property)
        {
            var constructedCollectionType = typeof(List<>).MakeGenericType(property.PropertyType.GetGenericArguments().Single());
            var collectionInstance = Activator.CreateInstance(constructedCollectionType) as IList;
            var value = token[property.Name];
            
            void HandleSingleCollectionType(string body)
            {
                var propertyCollectionType = property.PropertyType.GetGenericArguments().Single();
                var constructedConverterType = typeof(CollectionConverter<>).MakeGenericType(propertyCollectionType);
                if (Activator.CreateInstance(constructedConverterType) is not JsonConverter converter) return;
                var convertedInstance = JsonConvert.DeserializeObject(body, propertyCollectionType, converter);
                collectionInstance?.Add(convertedInstance);
            }
            
            switch (value)
            {
                case JArray array:
                    foreach (var body in array)
                    {
                        HandleSingleCollectionType(body.ToString());
                    }
                    break;
                case JObject:
                    HandleSingleCollectionType(value.ToString());
                    break;
                default:
                    Debug.WriteLine($"Unknown or empty json token value for property {property.Name}");
                    break;
            }
            
            property.SetValue(instance, collectionInstance);
        }

        JToken token = JToken.Load(reader);
        var instance = (TEntity)Activator.CreateInstance(typeof(TEntity));
        var properties = instance?.GetType().GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);

        if (properties == null) return default;
        foreach (PropertyInfo property in properties)
        {
            try
            {
                if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
                {
                    SetCollectionTypes(token, instance, property);
                }
                else if (this._entityTypes.Any(type => type == property.PropertyType) && property.PropertyType.IsClass)
                {
                    SetClassTypes(token, instance, property);
                }
                else
                {
                    SetSimpleTypes(token, instance, property);
                }
            }
            catch (NullReferenceException ex)
            {
                Debug.WriteLine($"Null value for entity class property {property.Name}, exception: {ex}");
            }
            catch (FormatException ex)
            {
                Debug.WriteLine($"Can not convert value property {property.Name} in {instance.GetType().Name}, exception: {ex}");
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Undefined exception for {property.Name} exception: {ex}");
            }
        }
        return instance;
    }
}

It converts very complex xml structures based on class structure so if class structure says something is collection it will build collection, if its an object (I mean class) it will create instance of a class and so on. Additionally it uses itself to convert any child xml has.

  • Related