0

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.

pushkin
  • 9,575
  • 15
  • 51
  • 95
  • Have you tried to pass `JsonSerializerOptions` as described here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-polymorphism – Serhii Feb 04 '22 at 00:40
  • @Serhii I did pass some options but they were minimal and didn't do much – pushkin Feb 04 '22 at 01:03

1 Answers1

0

This works for me. Note that I made Val a public property to get a value in json.

using System;
using System.Diagnostics;
using Xunit;

namespace SerializeTests {
    public class SerializeIt {
        [Fact]
        public void SerializeToString() {

            MyClass objToTry = new MyClass();
            string serialized = System.Text.Json.JsonSerializer.Serialize(objToTry);

            // works 
            Debug.WriteLine(serialized);// {"Val":null}
        }
        [Fact]
        public void SerializeToBytes() {

            MyClass objToTry = new MyClass();
            byte[] bytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(objToTry);

            // works 
            Debug.WriteLine($"total bytes: {bytes.Length.ToString()}");  // total bytes: 12
            Debug.WriteLine(Convert.ToBase64String(bytes)); // eyJWYWwiOm51bGx9
        }
    }
    class MyClass {
        public object Val { get; set; }
    }
}

Also as you do not control what is set to Val property you may run into problems as that object may not be fully serializable to/from JSON.

dbCoder
  • 114
  • 5
  • weird, a minimal example works for me. I'm going to try to figure out what's special about my actual case where it doesn't – pushkin Feb 04 '22 at 17:08
  • oh it seems like this works when sending data from .net core to core. But in my case, I'm sending data from a .net framework process to a .net core process and trying to serialize that result, and it fails. Though I don't exactly know why yet – pushkin Feb 04 '22 at 17:17
  • Find out what gets sent in the Val property and why you need to deserialize it and what you are expected to do with the data. You can design your MyClass to ignore value in Val property if it's not needed. – dbCoder Feb 04 '22 at 17:39
  • I'm not deserializing in this case - just serializing. And I do need the Val property. I'm setting it to exactly what it gets set to in .NET Core, but it fails to serialize for some reason. Maybe some other properties are getting added to it? – pushkin Feb 04 '22 at 17:45
  • oh well I guess in .NET Core, I have some class (MyClass) and am setting it to a variable of type object that I'm serializing. The JsonSerializer is still able to figure out the type of this class. In the .net framework -> .net core case, I guess that type is being lost so it just sees a generic object?\ – pushkin Feb 04 '22 at 17:46
  • Could it be due to System.Text.Json being picky about the case of json properties? – Jeremy Lakeman Feb 08 '22 at 01:57