Open joboudreault opened 4 months ago
I suspect https://github.com/rust-embedded-community/serde-json-core/issues/86 may resolve this issue, but in general, serde-json-core
is not really intended to be handling dynamic types.
As a workaround, I'd recommend that you do a two step deserialization process, where you ignore the data
field (serde attribute skip
) for the first deserialization round. Then, check the status for success and only deserialize the full structure if the request was successful.
Goal
Being able to completely ignore a field in a JSON string in a
no_std
andno_alloc
environment with theserde_json_core
crate. The JSON processed may be of two forms :Successful response :
Unsuccessful response :
The JSON is then converted into this Rust structure :
If the
status
field is"error"
, then thedata
field must be completely ignored even if there are unknown fields of any kind (string, number, object, null, ...).Problem
Currently : there is no way to perform this deserialization without the
serde_json_core::from_slice()
function returningResult::Err(_)
. Thus, themessage
field is not accessible.Expected : there exists a way to successfully deserialize the JSON string whether it is an error or not, and access the error message.
Attempt 1 (normal)
Here, we tried simply using the
#[derive(Deserialize)]
macro.Deserialization error :
Attempt 2
Here, we tried to deserialize with a custom deserializer thinking that
serde
will returnNone
if it cannot successfully convert thedata
field intoT
.Deserialization error :
Attempt 3
Here, we tried to deserialize with a custom deserializer thinking that the
Result
returned by the<T as Deserialize>::deserialize()
function may be intercepted and mapped toOk(None)
Deserialization error :
Attempt 4
Here, we tried to deserialize with a custom deserializer and custom
Visitor
emptying all fields in thedata
field's object. In this attempt, we realized that all the functions in theDeserializer<'de>
trait are taking ownership which disallow us to perform an operation trying to convertT
or toNone
depending on thestatus
field.Deserialization panic message :
Possible Solution
There exists a solution when using
serde_json
and allocating aHashMap
as described in issue #1583. However, we cannot use this method because there must not be any allocator, thusHashMap
cannot be used.Environment
All compilation is run with the following Rust version :