.Net Core 3.0 JsonSerializer populate existing object

前端 未结 7 950
再見小時候
再見小時候 2020-12-24 10:51

I\'m preparing a migration from ASP.NET Core 2.2 to 3.0.

As I don\'t use more advanced JSON features (but maybe one as described below), and 3.0 now comes with a buil

7条回答
  •  轮回少年
    2020-12-24 11:48

    I am not sure if this will fix your problem, but it should work as a temporary workaround. All I did was write a simple class with a populateobject method in it.

    public class MyDeserializer
    {
        public static string PopulateObject(string[] jsonStrings)
        {
            Dictionary fullEntity = new Dictionary();
    
            if (jsonStrings != null && jsonStrings.Length > 0)
            {
                for (int i = 0; i < jsonStrings.Length; i++)
                {
    
                    var myEntity = JsonSerializer.Parse>(jsonStrings[i]);
    
                    foreach (var key in myEntity.Keys)
                    {
                        if (!fullEntity.ContainsKey(key))
                        {
                            fullEntity.Add(key, myEntity[key]);
                        }
                        else
                        {
                            fullEntity[key] = myEntity[key];
                        }
                    }
                }
            }
    
            return JsonSerializer.ToString(fullEntity);
        }    
    }
    

    I put it into a console app for testing purposes. Below is the entire app if you would like to test it yourself.

    using System;
    using System.Text.Json;
    using System.IO;
    using System.Text.Json.Serialization;
    
    namespace JsonQuestion1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Only used for testing
                string path = @"C:\Users\Path\To\JsonFiles";
                string st1 = File.ReadAllText(path + @"\st1.json");
                string st2 = File.ReadAllText(path + @"\st2.json");
                // Only used for testing ^^^
    
                string myObject = MyDeserializer.PopulateObject(new[] { st1, st2 } );
    
                Console.WriteLine(myObject);
                Console.ReadLine();
    
            }
        }
    
        public class MyDeserializer
        {
        public static string PopulateObject(string[] jsonStrings)
        {
            Dictionary fullEntity = new Dictionary();
    
            if (jsonStrings != null && jsonStrings.Length > 0)
            {
                for (int i = 0; i < jsonStrings.Length; i++)
                {
    
                    var myEntity = JsonSerializer.Parse>(jsonStrings[i]);
    
                    foreach (var key in myEntity.Keys)
                    {
                        if (!fullEntity.ContainsKey(key))
                        {
                            fullEntity.Add(key, myEntity[key]);
                        }
                        else
                        {
                            fullEntity[key] = myEntity[key];
                        }
                    }
                }
            }
    
                return JsonSerializer.ToString(fullEntity);
          }
        }
    }
    

    Json File Contents:

    st1.json

    {
        "Title": "Startpage",
        "Link": "/index"
    }
    

    st2.json

    {
      "Title": "Startpage",
      "Head": "Latest news",
      "Link": "/news"
    }
    

提交回复
热议问题