Skip to content

Exception when desocializing surrogates of abstract types. #84

@gershuk

Description

@gershuk

When deserializing surrogates of abstract classes, an exception appears "Yuzu.YuzuException: "Expected class tag, but found 'Type'"".

Example leading to an exception:

using System;

using Yuzu;
using Yuzu.Json;
using Yuzu.Metadata;

namespace Test
{
    [YuzuAll]
    public class JsonSystemType
    {
        public string Name { get; set; }

        public JsonSystemType(string name) => Name = name ?? throw new ArgumentNullException(nameof(name));

        public static Type FromSurrogate(object obj) => Type.GetType((obj as JsonSystemType).Name);

        public static implicit operator JsonSystemType(Type type) => ToSurrogate(type);

        public static implicit operator Type(JsonSystemType jsonSystemType) => FromSurrogate(jsonSystemType);

        public static JsonSystemType ToSurrogate(object obj) => new JsonSystemType((obj as Type).AssemblyQualifiedName);
    }

    [YuzuAll]
    public class TestType
    {
        public Type Type { get; set; }
    }

    public class NestedType
    { }

    class Program
    {
        static void Main(string[] args)
        {
            var yuzuCommonOptions = new CommonOptions()
            {
                Meta = new MetaOptions(),
                AllowEmptyTypes = true,
            };

            var surrogateType = Meta.Get(typeof(Type), yuzuCommonOptions).Surrogate;
            surrogateType.SurrogateType = typeof(JsonSystemType);
            surrogateType.FuncTo = JsonSystemType.ToSurrogate;
            surrogateType.FuncFrom = JsonSystemType.FromSurrogate;

            var jsonSerializer = new JsonSerializer() { Options = yuzuCommonOptions };
            var jsonString = jsonSerializer.ToString(new TestType() { Type = typeof(NestedType) });

            var jsonDeserializer = new JsonDeserializer() { Options = yuzuCommonOptions };
            var obj = jsonDeserializer.FromString<Type>(jsonString);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions