All Questions
Tagged with marshalling struct
248 questions
1
vote
1
answer
90
views
Go how to only marshal a slice if its not nil. Json showing null for for nil slice
I have a struct in Golang which a slice of my alias type:
type Foo struct {
NextSet []House `json:"next_set"`
}
I want to marshal this into a json. When I do not initialise ...
0
votes
1
answer
49
views
Fixed size byte array with fixed offset inside manually marshalled struct throws runtime exception?
Target is to have the following struct working in C# (.net 8.0)
[StructLayout(LayoutKind.Explicit, Size = 257, Pack = 1)]
public struct MyFrame
{
[FieldOffset(0)] public byte valueA;
...
2
votes
1
answer
93
views
Why does C# consider this unmanaged struct to have an an overlap
I've defined a struct for an externally defined data-packet as follows:
[StructLayout(LayoutKind.Explicit, Size=18, Pack=1, CharSet=CharSet.Ansi)]
struct FlexTransmission
{
[FieldOffset(0), ...
0
votes
1
answer
49
views
go-toml Marshal fails
the following program tries to Marshal a struct and write to file.
The struct is nested and it contains toml tags.
I am not receiving any error message and it seems correct.
The print of the structure ...
2
votes
1
answer
177
views
Overlapping or wrongly aligned struct
I have an issue with my struct layout. I want to have a struct with an array of other structs. The compiler does not complain with my implementation:
[StructLayout(LayoutKind.Explicit, Pack = 1, Size =...
0
votes
1
answer
140
views
Python struct.unpack format from header .h file
I have a header (.h) file describing some datastructures (the data is collected from a sensor). Example:
...
struct SensorIf
{
DWORD SensorType;
char Name[...
0
votes
0
answers
151
views
How to pass / marshal a struct with a string[] field from C# to C++ library with C bindings?
I would like to pass a configuration structure from C# to a library with a C interface (that has a C++ implementation behind the interface).
Below are the .h/.cpp files and the C# code calling into it ...
1
vote
2
answers
133
views
Passing array of structs from C# to C++
I am trying to create C# code which calls a function in a DLL with a C interface. I am a C and C++ expert but my knowledge of C# is pretty limited. I have already managed to load the DLL into the C# ...
1
vote
0
answers
219
views
Marshalling struct in c# from C++ dll
In C++ I have a struct:-
typedef struct
{
unsigned char ucSpeed;
unsigned long ulLength;
unsigned char ucBulkInPipe;
unsigned char ucBulkOutPipe;
unsigned char ucInterruptPipe;
}...
2
votes
1
answer
202
views
Any alternative about CS0233 for sizeof(T) in StructLayoutAttribute?
Wrote the following struct to minimize the number of types for vectors with different components:
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 3 * sizeof(T))]
public struct Vector3<T> ...
1
vote
1
answer
218
views
Marshal c# struct to C struct is not working
I want to call the following C function from C#, but it doesn't work.
The C code is part of the .so file. In my case the code should run in an Linux environment!
The C# Code is part of a console app ...
1
vote
1
answer
285
views
How do I copy part of a custom struct to a different struct in Golang
I have been stuck with this for a couple of hours now and was hoping one of you could help me out with an elegant solution.
Basically, I have the following struct:
type GetERC20TransferResponse struct ...
2
votes
1
answer
385
views
Building a struct with a field name which has a comma
I'm trying to create a struct based on a response I get.
I have no control over the response, and in it's structure there are field names which use comma as part of the filed name itself
JSON Example:
...
1
vote
0
answers
850
views
MemoryMarshal.Cast Only value types without pointers or references are supported
Hello I am working on a project where I have to cast a byte array from a UDP package into a data struct. This struct contains arrays with a fixed size. However, when casting the byte array with the ...
-1
votes
1
answer
197
views
Incorrect struct members value when passing the struct reference from C# to C
I am working on to use a C library in C#. Library is the globalplatform by kaoh, here is the github link.
What I have done is compiled the C library into a shared library (DLL) by following these ...
2
votes
2
answers
2k
views
Ignoring an object in struct is nil and not when it's an empty array
Is it possible to only use omitempty when an object is nil and not when it's an empty array?
I would like for the JSON marshaller to not display the value when an object is nil, but show object: [] ...
1
vote
1
answer
270
views
Is it possible to have a union struct with fixed buffer and another struct with array or string fields in C#?
I'm trying to allocate memory for hundreds of thousands objects to initialize them later from an array of bytes. My goal is to skip memory allocation on each object. That is why I am using C# structs.
...
-2
votes
1
answer
419
views
Encoding and decoding structs of
I'm trying to encode and decode structs, I've searched around quite a bit and a lot of the questions regarding this topic is usually people who want to encode primitives, or simple structs. What I ...
3
votes
2
answers
716
views
Marshal.OffsetOf don't reflect the runtime reality?
I would like to get the offset of a field in an unmanaged structure. For this I use the Marshal.OffsetOf method and I realized that the result does not reflect the Packing used with StructLayout
Take ...
0
votes
2
answers
761
views
Marshaling complex C structs with c unions for C#
i am desperate to get a complex c datatype correctly marshaled for C#. I already read all the other posts regarding that topic and i am running out of ideas although it seems to me to be quite close ...
1
vote
1
answer
400
views
How to Marshal/Unmarshal a common JSON & BSON key/field that can have two different formats in Go?
I currently have mongo data stored in two forms (specifically for content key) in a collection. Partial sample data shown below:
Format 1.
{
"type": "text",
"content&...
1
vote
0
answers
470
views
C# unaligned struct fields with an explicit layout
I am trying to serialise a struct that has a the following layout:
[StructLayout(LayoutKind.Explicit, Pack = 2)]
public readonly struct EntryInfo
{
[FieldOffset(0x00)]
[MarshalAs(UnmanagedType....
1
vote
2
answers
2k
views
Can I skip a json tag while Marshalling a struct in Golang?
I have a scenario where I would like to skip a json tag while marshalling a struct in Golang. Is that possible? If so how can I achieve this?
For example I am getting this json:
{"Employee":{...
-1
votes
2
answers
2k
views
How do you unmarshal a JSON object to a Golang struct when the JSON field key is a date?
I have the following JSON response. What would be the best way to unmarshal this into a Golang struct? The JSON to Golang autogenerated struct is saying the named properties of the struct should be ...
1
vote
1
answer
364
views
C# marshaling C++ functions
I am trying to use the Hikvision SDK https://www.hikvision.com/en/support/download/sdk/
My current goal is to open the door (trigger an output) with the intercom outdoor station.
I managed to do the ...
0
votes
2
answers
3k
views
How effectively to change JSON keys
At the input, the program receives a JSON.
I need to return the same JSON, but with camelCase keys.
Is there any effective way to convert all snake_case keys in JSON to camelCase keys in Go?
Snippet ...
1
vote
1
answer
218
views
C# - Call C++ DLL function whit ref
I am trying to call a DLL function in C++ from my C# code.
DLL function code and struct (C++):
typedef struct{
Operacion RespuestaOperacion;
char var1[256];
int var2; ...
0
votes
0
answers
378
views
Marshalling a map with struct as key
I have a struct where one of the fields is a map with the key being another struct. I am using Go version 16.1 and the map created that way is supposedly supported in this version (albeit unsure when ...
-1
votes
2
answers
788
views
Marshal a struct containing pointer to array
How to correctly marshal this C struct with pointer to array and pointer to pointer members in C# for use with 3rd party dll?
C:
typedef struct SomeStruct {
uint8_t *data[8];
int size[8];
...
-2
votes
1
answer
1k
views
How to Marshall any data type to string and Unmarshal from string to specific data type on condition
type State struct {
Type string `json:"type" validate:"required"`
Value string `json:"value"`
}
I have a struct like this. I need to pass the ...
3
votes
1
answer
2k
views
How to marshal struct as if it were an anonymous struct?
The documentation states:
Anonymous struct fields are usually marshaled as if their inner exported fields were fields in the outer struct.
For examble:
type foo struct {
Foo1 string `json:"...
2
votes
1
answer
3k
views
How to marshal nested struct to flat JSON
I am trying to marshall a nested struct with several of the same structs to a flat JSON structure E.G.
type A struct {
Value float64
Unit string
}
type B struct {
p1 string `json:p1`
...
0
votes
0
answers
188
views
How to Copy a Struct with a string Set to ByValTStr
I have a struct that looks like the following:
[StructLayout(LayoutKind.Explicit, Size = 17, CharSet = CharSet.Ansi)]
public struct MyStruct
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]
...
1
vote
2
answers
709
views
byte array from specific index as struct in c# without making a copy
Currently I code client-server junk and deal a lot with C++ structs passed over network.
I know about ways provided here Reading a C/C++ data structure in C# from a byte array, but they all about ...
0
votes
1
answer
572
views
Marshalling a complex structure from C# to C
I'm reading all about structure marshalling between C and C# in 64bit environment without success.
What I need to do is to pass the structure
typedef struct DescStructTa
{
char* pszNa;
...
0
votes
0
answers
30
views
How to mashall a return of an array of structs from C to C#
Please could I ask for help regarding the following?
I have a function in a C dll which returns an array of structs. I can't change the source code. I have, as a test, written my own C dll to return ...
-2
votes
1
answer
53
views
What Go tag should be used while defining struct of a JSON data if "field/key" for some values is varying or not known?
While trying to parse a time-series data I found a key field in the JSON data is the timestamp(obviously in string format). But creating a struct for the same beforehand is not possible as I cannot ...
1
vote
1
answer
609
views
Can I marshal data from a C/C++ struct into a C# struct with properties?
I have a C# struct which is used for interfacing with some native code. Let's say it looks like this and there's an entry point in the dll that can be used to retrieve values for this struct:
public ...
1
vote
0
answers
136
views
Marshalling C++ struct with union to C#
I'm trying to implement a C# wrapper around the Nikon DS-QI2 Camera SDK (only C++ dll available).
I am struggling with marshalling struct data, since the sizes are not matching.
The dll is working ...
1
vote
2
answers
4k
views
How to Marshall json from one struct to another with different json tags in Go?
I am creating a Go application that consumes data from multiple sources that all have similar data but different structures to their data/responses. These responses need to be marshalled into a common ...
0
votes
1
answer
119
views
Correct Structure Marshalling
I have a structure:
typedef struct _wfs_bcr_caps
{
WORD wClass;
BOOL bCompound;
BOOL bCanFilterSymbologies;
LPUSHORT lpwSymbologies;
DWORD ...
2
votes
2
answers
226
views
Unmarshalling Decimal Property in C++
I am marshalling struct in c# which contain some decimal properties, but I am not able to Unmarshall it in c++ as there is no decimal datatype in c++. As it is a financial application i don't have any ...
3
votes
1
answer
897
views
Getting a complex struct from C++ to C#
Days ago I made this question: Passing a complex Struct (with inner array of struct) from C# to C++
Fortunately it as been answered and the code seems to work.
Now I need to do the oposite case, I ...
2
votes
1
answer
1k
views
Marshaling C# struct with array of structs and size param index
I've read several topics about but I still can't understand the real limitation of not being able to convert this structure to byte array easily:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
...
3
votes
1
answer
765
views
C - Marshaling/Unmarshaling Struct with htonl and ntohl
So I have a struct in c, and I need to send it over a UDP socket. The RPCmessage should be flattened into a byte array which is message.data. Then message.data is sent over the UDP socket.
typedef ...
0
votes
1
answer
565
views
Marshal callback in a struct from C++ to C#
I need to return the necessary information about an object as a struct with callbacks and other data.
This is what it looks like on the C# side:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
...
3
votes
1
answer
2k
views
Unmarshall should return error when having wrong json input structure
I have struct A and B. When a JSON string A is unmarshalled to struct A then it is valid, however if the JSON string A is unmarshalled to struct B it is still successful (which should not).
Is there ...
1
vote
1
answer
11k
views
Type cannot be marshaled as an unmanaged structure
I'm trying to change the resolution in a core project.
var devmode = default(Devmode);
devmode.DmDeviceName = new string(new char[32]);
devmode.DmFormName = new string(new char[32]);
devmode.Dmsize =...
2
votes
1
answer
122
views
how to transform byte[] to struct (contains a byte[] member and length member)
i have a struct define as:
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi,Pack = 1)]
internal struct Message
{
[MarshalAs(UnmanagedType.U1, SizeConst = 1)]
public byte age;
[...
1
vote
2
answers
790
views
Create heterogeneous json array in go
Suppose I have an struct like this in go:
type Message struct {
Args []interface{}
Kwargs map[string]interface{}
}
message := Message{ ...