0

I need to serialize a class object whose field is a custom data type matrix, like:

internal class MyClass
{
    private readonly MyMatrix[,] myField;
    public MyField { get => myField; }
}

I am using json serialization code from here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/how-to?pivots=dotnet-7-0

    public static bool Save(MyClass o, string fileName)
    {
        _ = new JsonSerializerOptions { IncludeFields = true };
        try
        {
            byte[] jsonString = JsonSerializer.SerializeToUtf8Bytes(o);
            File.WriteAllBytes(fileName, jsonString);
            return true;
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception in serialization: " + e.Message);
            return false;
        }
    }

When trying to serialize an object of this class, I get an exception like this: (https://i.stack.imgur.com/lhbbE.png)

I already understood that json supports a limited list of data types, but what should I do to serialize and then deserialize such an object?

dbc
  • 104,963
  • 20
  • 228
  • 340
Alexey
  • 3
  • 2
  • 1
    Please [edit] your question to include the full `ToString()` output of your exception (including message, traceback and inner exception(s), if any) as **text** rather than as a screenshot. On stack overflow images should not be used for textual content, see [*Why should I not upload images of code/data/errors?*](https://meta.stackoverflow.com/a/285557) for why. For instructions on formatting see *[How do I format my code blocks?](https://meta.stackexchange.com/q/22186)*. A [mcve] showing what you have tried that did not work would maximize your chances of getting help. See [ask]. – dbc Dec 06 '22 at 19:03
  • 1
    If you are using System.Text.Json, this might be a duplicate of [How can I serialize a `double[,]` 2d array to JSON using System.Text.Json?](https://stackoverflow.com/q/66280645/3744182). Try using `Array2DConverter` from [this answer](https://stackoverflow.com/a/66282422/3744182) (which should work for 2d arrays `T [,]` of any item type) and see if it works for you. – dbc Dec 06 '22 at 19:04
  • This example partially works. Partly because I only get a json file with an object that is a 2d matrix, and I don't get it encapsulated in the MyClass object described in the description. I assume that the converter from the example needs to be improved, but I don’t know what exactly and I’m not sure that I can do it. – Alexey Dec 06 '22 at 19:57
  • Then please [edit] your question to share a [mcve]. We don't have a definition for `MyMatrix` so we can't reproduce your problem. Incidentally, when you do `_ = new JsonSerializerOptions { IncludeFields = true };` you construct your options **but never use them**. Why do you do that? – dbc Dec 06 '22 at 20:19
  • Here is my attempt to create a [mcve] for your code: https://dotnetfiddle.net/aA1g32. As you can see, it doesn't compile, and it isn't entirely clear how to fix it: 1) No definition for `MyMatrix`. 2) `_ = new JsonSerializerOptions { }` not used. 3) No way to initialize `MyClass.myField`. Perhaps your "real" class has a parameterized constructor? – dbc Dec 06 '22 at 20:28
  • If make some guesses as to how to fix your compilation and classes, and make use of the options you ignored, then once I add `Array2DConverter` everything seems to work. See https://dotnetfiddle.net/xHe0Am. – dbc Dec 06 '22 at 20:38
  • @dbc Thanks for the help, the 2d converter example you provided does work completely for custom matrices as well. The problems I ran into after using it are either due to the absence of the necessary constructors for encapsulated classes, or inappropriate use of the abstract keyword, due to which the deserializer could not create an object of such a class. – Alexey Dec 07 '22 at 11:40

0 Answers0