You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Being able to completely ignore a field in a JSON string in a no_std and no_alloc environment with the serde_json_core crate. The JSON processed may be of two forms :
If the status field is "error", then the data 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 returning Result::Err(_). Thus, the message 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.
Here, we tried simply using the #[derive(Deserialize)] macro.
Deserialization error :
Error: CustomError;; JSON does not match deserializer’s expected format.
Attempt 2
Here, we tried to deserialize with a custom deserializer thinking that serde will return None if it cannot successfully convert the data field into T.
Deserialization error :
Error: CustomError;; JSON does not match deserializer’s expected format.
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 to Ok(None)
Deserialization error :
Error: TrailingCharacters;; JSON has non-whitespace trailing characters after the value.
Attempt 4
Here, we tried to deserialize with a custom deserializer and custom Visitor emptying all fields in the data field's object. In this attempt, we realized that all the functions in the Deserializer<'de> trait are taking ownership which disallow us to perform an operation trying to convert T or to None depending on the status field.
Deserialization panic message :
Body is a 'Data' structure: ExpectedObjectCommaOrEnd
There exists a solution when using serde_json and allocating a HashMap as described in issue #1583. However, we cannot use this method because there must not be any allocator, thus HashMap cannot be used.
Environment
All compilation is run with the following Rust version :
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 :
The text was updated successfully, but these errors were encountered: