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

System.NullReferenceException - when known types differ in serialization and deserialization process #188

Open
jakubbukaj opened this issue Oct 13, 2020 · 3 comments

Comments

@jakubbukaj
Copy link

public class SimplePerson
{
    public string Name { get; set; }
}

private static void Test()
{
    var serializer = new Hyperion.Serializer();
    var to = new List<SimplePerson>();
    to.Add(new SimplePerson { Name = "Joe" });
    to.Add(new SimplePerson { Name = "Richard" });

    byte[] serialized;
    using (var ms = new MemoryStream())
    {
        serializer.Serialize(to, ms);
        serialized = ms.ToArray();
    }

    serializer = new Hyperion.Serializer(new SerializerOptions(false, false, null, null, new[] { typeof(int) }));

    using (var ms = new MemoryStream(serialized))
    {
        var res = serializer.Deserialize(ms); // exception is thrown here
    }
}
@jakubbukaj
Copy link
Author

System.OutOfMemoryException: 'Exception of type 'System.OutOfMemoryException' was thrown.' Will be thrown if I encapsulate serialized data by TransportObject.

  public class TranspObj
     {
         public Dictionary<string, object> Items
         {
             get { return _items; }
             set { _items = value; }
         }

         private Dictionary<string, object> _items = new Dictionary<string, object>();

         public T Get<T>(string key)
         {
             if (!_items.ContainsKey(key))
                 return default(T);

             return (T)_items[key];
         }

         public void Add(string key, object value)
         {
             _items[key] = value;
         }
     }

     public class SimplePerson
     {
         public string Name { get; set; }
     }

     private static void Test()
     {
         var serializer = new Hyperion.Serializer();
         var to = new TranspObj();
         to.Add("K1", new SimplePerson { Name = "Joe" });
         to.Add("K2", new SimplePerson { Name = "Richard" });

         byte[] serialized;
         using (var ms = new MemoryStream())
         {
             serializer.Serialize(to, ms);
             serialized = ms.ToArray();
         }

         serializer = new Hyperion.Serializer(new SerializerOptions(false, false, null, null, new[] { typeof(int) }));

         using (var ms = new MemoryStream(serialized))
         {
             var res = serializer.Deserialize(ms); // exception is thrown here
         }
     }

@Aaronontheweb
Copy link
Member

I'm sorry, what was the expected vs. actual behavior here?

@jakubbukaj
Copy link
Author

In the first case - if well known types differs (imagine client server application where server is already updated and client is connecting to updated server for its update) there should be more polite way to deal with these changes (e.g. hooking some event).

In case of "System.OutOfMemoryException" it seems there is some kind of error in memory allocation. I am sorry I should create two threads instead of one.

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

2 participants