top | item 33909662

Convert JSON to convert JSON to new Datatyp C++ at runtime

1 points| farka01 | 3 years ago

i become data json, i have no idea about datatype, not from compiler time i must to convert to typedef struct or union , maybe nested structure

{ "type":"struct" "name:"MyStruct" "struct": { "var1":"int", "var2":float; "var3":"double" } }

convert AT RUNTIME to

typedef struct { var1:int; var2:float; var3:double }MyStruct`

2 comments

order

HelloNurse|3 years ago

These requirements are definitely wrong.

You can only use a C++ struct by writing C++ code that includes the definition of the struct. This means that if your program waits to see

  { "type":"struct" "name:"MyStruct" "struct": { "var1":"int", "var2":float; "var3":"double" } }
in an input file before acting on this declaration, it cannot be the same program that uses

  typedef struct { var1:int; var2:float; var3:double }MyStruct`
to process a conforming JSON file.

I can think of two plausible scenarios:

1) You can actually use the compiler: program 1 reads your type definition and generates the source code for program 2 (which includes your typedef and all specialized code you want), then you compile program 2 and finally you process JSON of the appropriate type with program 2. (Technically, program 2 could be a dynamic link library running within the same process as program 1.)

2) You only want high performance JSON processing, as if you used a straightforward specialized C++ program with compact structs: memory efficiency can be achieved by packing values inside buffers "by hand" without using C++ structs (I suggest looking at module ctypes in the Python standard library for inspiration), but being limited by not knowing names and structures ahead of time your program can only be an interpreter for a DSL about general-purpose JSON processing, like e.g. jq.

farka01|3 years ago

thank you, i know struct name and variabel type inside struct at runtime