❔ Is it possible to serialize this struct?

Hello,

I want to serialize this struct to json:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct EndOfBarTelegram
{
    public static readonly string Key = nameof(EndOfBarTelegram);

    public uint MessageLength;
    public uint MessageId;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
    public string ProfileType;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
    public string ProfileName;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
    public string BloomId;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
    public string TimeIn;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
    public string TimeOut;
    public float BarLength;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
    public float[] Mean;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
    public float[] Max;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
    public float[] Min;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
    public float[] DefectLength;
}


I'm have these methods:

public static ReadOnlyMemory<byte> SetTJson<T>(T obj)
{
    string s = JsonSerializer.Serialize(obj);
    return new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes(s));
}

public static ReadOnlyMemory<byte> SetTJson<T>(T obj, JsonSerializerOptions jsonOptions)
{
    string s = JsonSerializer.Serialize(obj, jsonOptions);
    return new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes(s));
}


Is it possible? At the moment it only returns 2 bytes (the curly braces). I'm assuming this has something to do with the marshalling because it works without it
Was this page helpful?