Apologies if this has been asked before, but I couldn't quite find the relevant question.
I am using Newtonsoft to serialize a type like this:
class MyClass {
object Val { get; set; }
}
The Val
property is set/given to me from another assembly, and I have no idea what it will be at compile time.
In Newtonsoft, I was able to serialize to an array of bytes like so:
MyClass obj = someObjFromSomewhere;
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj);
In System.Text.Json
I've tried:
return System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(obj);
but this crashes with:
"The collection type 'System.Object' on 'MyClass.Val' is not supported."
Indeed, object
isn't listed as a supported type here
I'm working in .NET Core 3.1.
How am I meant to make this work? Am I supposed to create a custom converter?
Update: this is only an issue when I'm first sending data from a .NET Framework process to a .NET Core process (serializing via Newtonsoft) and then sending it from the .NET Core out somewhere else, this time serializing with System.Text.Json. Seems like the type info is getting lost and it just sees "Object".
However, this worked in Newtonsoft.