1

I expect the applied code to return something like {"$type": "Newtonsoft.Json.Guid", "value": "34861761826d4419a2084ef161992616"} (It's TypeNameHandling.All job and it works with more complex types)

But instead I get just "34861761826d4419a2084ef161992616"

So, when I try to var guid = (Guid)JsonConvert.DeserializeObject(serialised, jsonSettings); it fails, cause deserialization returns a string. (I do not use generic deserializer due to some complex reasons based on generating a huge amount of code around existing base)

Minimal code:

using System;
using Newtonsoft.Json;

namespace ConsoleApplication1
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var jsonSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
            var guid = Guid.NewGuid();

            var serialised = JsonConvert.SerializeObject(guid, jsonSettings);
            Console.WriteLine(serialised);
        }
    }
}
Andrey Golubev
  • 132
  • 1
  • 2
  • 8

0 Answers0