Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is an issue with type inference during serialization of an object. #3000

Open
peijiehuang opened this issue Nov 19, 2024 · 0 comments
Open

Comments

@peijiehuang
Copy link

peijiehuang commented Nov 19, 2024

Source/destination types

using Newtonsoft.Json;

namespace ConsoleApp1
{
    internal class Program
    {
        private static JsonSerializerSettings SerializerSettings = new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.All,
            ObjectCreationHandling = ObjectCreationHandling.Replace,
        };

        static void Main(string[] args)
        {
            var data = new Dictionary<string, object>
            {
                { "TEST", (Int16)123 },
                { "TEST2", (Int128)123 },
            };

            foreach (var item in data)
            {
                Console.WriteLine("data1:" + item.Value.GetType());
            }
            Console.WriteLine();
            Console.WriteLine();

            var jsonStr = JsonConvert.SerializeObject(data, SerializerSettings);

            Console.WriteLine(jsonStr);
            Console.WriteLine();
            Console.WriteLine();

            var data2 = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonStr, SerializerSettings);

            foreach (var item in data2)
            {
                Console.WriteLine("data2:" + item.Value.GetType());
            }

        }
    }
}

Source/destination JSON

{"$type":"System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[System.Object, System.Private.CoreLib]], System.Private.CoreLib","TEST":123,"TEST2":"123"}

Expected behavior

The data should retain the data type of TEST as int16 and TEST2 as int128.

Actual behavior

The data was actually converted to retain the data type of TEST as int32 and TEST2 as string.

Steps to reproduce

The complete code is provided above, and here are the images.
json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant