I am currently at the Start of Chapter 13 of the Beginner Book and between the chapters i experiment with the language and already create small little dll's to use them in a high level language hobby project to offload process heavy code. I am proud of myself that i managed to get this working for some parts of my code already and increased performance of high use functions in my project dramatically. But in this case i try to create a json decoder, because those that are available in the high level are very slow compared to serde_json in rust.
The json i currently try to decode within rust and then move it in a decoded form back to the caller, is dynamic and its contents arent predictable. In the high level language i need to iterate over its contents. Let me show
My json can look like this. It contains String, bool and number values
{"maxrpm":4700,"absMode":"realistic","minGearIndex":-1,"checkengine":false,"engineRunning":1,"freezeState":false,"gearIndex":0,"parking":0,"gearboxMode":"realistic","lowpressure":0,"oil":0,"lowhighbeam":0,"nop":0,"highbeam":0,"lowhighbeam_signal_R":0,"lowhighbeam_signal_L":0,"horn":0,"reverse":0,"lowfuel":false,"highbeam_wigwag_R":0,"highbeam_wigwag_L":0,"reverse_wigwag_R":0,"lights_state":0,"boost":0,"boostMax":0,"ignition":true,"brakelight_signal_L":0,"smoothShiftLogicAV":0,"hazard_enabled":0,"gear":0,"fog":0,"maxGearIndex":4,"reverse_wigwag_L":0,"lightbar":0,"odometer":0,"lowbeam":0,"running":true,"signal_left_input":0,"signal_right_input":0,"fuelVolume":1,"brakelight_signal_R":0,"idlerpm":700}
Sometimes it can also be just this
{"odometer":0.022705062608801,"smoothShiftLogicAV":0.017504340433356}
Or as little as this
{"parking":1}
The fields arent defineable, they can be anything, just as their values. And because of it, with my current knowledge i cant pre define a struct where serde_json will put the data into.
with
let json: Value = serde_json::from_str(&json)?;
i can get, what seems like, a hashmap. The question for me is tho can i bring this hasmap either in its form directly or in another over the ffi border back to the caller. Is this possible with serde_json or within rust at all?
I had sucess bringing predefined structs over the ffi border that where filled with json's that always have the same contents, but predefining structs for a dynamic json seems to be not possible or maybe that is possible?
Im sitting here looking like this
trying to think of a dynamic data structure that is associative and can hold variable data. Structs are pre defined - not applicable. Vectors and hashmaps can only hold data of the same type - not applicable. Maybe a dictionary object is of help here. But then there is the problem of also moving whatever data structure across the ffi border back to the caller.
I dont see a way todo this
Obligatory pls halp
I hope you guys understand that my ability to research this is very limited because of how new i am to low level programming and FFI overall