© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
11 replies
Determinism

❔ 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;
}
[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));
}
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Is it possible to define a struct inline ?
C#CC# / help
3y ago
❔ StructureMap.StructureMapException: StructureMap Exception Code: 207
C#CC# / help
3y ago
✅ Is it possible to simplify this litedb query?
C#CC# / help
3y ago