Home > Blockchain >  C# Invalid Option Error deserializing nested dynamic object with system.text.json in .Net 6
C# Invalid Option Error deserializing nested dynamic object with system.text.json in .Net 6

Time:09-15

I'm working with a lot of media files and turning their properties into a dynamic object to process them in VS 2022, C#, .Net 6 Core, and system.text.json. Most of the files have nested parameters. Since every audio/video file has a different type and number of parameters, POCO's don't make sense here and these dynamic objects have been extensively used throughout this and other parts of the system... making them impossible to replace.

After processing the media files and adding additional information to the objects, I want to serialize them with system.text.json into a string. When I do, I'm getting an error that says:

  public static string ObjectToString(dynamic o)
    {
        return System.Text.Json.JsonSerializer.Deserialize<dynamic>(o);
        // stupid typo... should be serialize here
    }

System.Text.Json.JsonSerializer.Deserialize<object>(System.Text.Json.JsonDocument,
System.Text.Json.JsonSerializerOptions)' has some invalid arguments'

Actually, I've tried serializing the same object I just serialized without modification and get the same error.

In .Net Framework, Newtonsoft had no trouble doing this, but in Core, I'd like to get away from it if possible. So, what do I need to do in order to deserialize dynamic objects to strings?

CodePudding user response:

Wait, you want to go from an object to a string? That's serialization, no deserialization.

Also, if you read your error, the method you're calling requires two arguments, the object you're serializing (?) and a JsonSerializerOptions instance that defines how the serialization should happen.

  • Related