Respect serialize_with attr when serializing default
This commit is contained in:
parent
7e23e2ad7a
commit
b748a90c3f
6 changed files with 74 additions and 27 deletions
|
@ -14,11 +14,19 @@ fn six() -> i32 {
|
|||
6
|
||||
}
|
||||
|
||||
fn custom_serialize<S>(value: &MyStruct2, ser: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
ser.collect_str(&format_args!("i:{} b:{}", value.my_int, value.my_bool))
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, JsonSchema, Debug)]
|
||||
#[serde(default)]
|
||||
pub struct MyStruct {
|
||||
pub my_int: i32,
|
||||
pub my_bool: bool,
|
||||
#[serde(serialize_with = "custom_serialize")]
|
||||
pub my_struct2: MyStruct2,
|
||||
}
|
||||
|
||||
|
|
|
@ -4,24 +4,21 @@
|
|||
"type": "object",
|
||||
"properties": {
|
||||
"my_bool": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
"default": false,
|
||||
"type": "boolean"
|
||||
},
|
||||
"my_int": {
|
||||
"default": 0,
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"default": 0
|
||||
"format": "int32"
|
||||
},
|
||||
"my_struct2": {
|
||||
"default": "i:0 b:false",
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/MyStruct2"
|
||||
}
|
||||
],
|
||||
"default": {
|
||||
"my_bool": false,
|
||||
"my_int": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
|
@ -29,13 +26,13 @@
|
|||
"type": "object",
|
||||
"properties": {
|
||||
"my_bool": {
|
||||
"type": "boolean",
|
||||
"default": true
|
||||
"default": true,
|
||||
"type": "boolean"
|
||||
},
|
||||
"my_int": {
|
||||
"default": 6,
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"default": 6
|
||||
"format": "int32"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,6 @@
|
|||
"type": "object",
|
||||
"required": [
|
||||
"command_line",
|
||||
"system_cpu_time",
|
||||
"user_cpu_time",
|
||||
"wall_time"
|
||||
],
|
||||
"properties": {
|
||||
|
@ -13,10 +11,26 @@
|
|||
"type": "string"
|
||||
},
|
||||
"system_cpu_time": {
|
||||
"$ref": "#/definitions/DurationDef"
|
||||
"default": {
|
||||
"nanos": 0,
|
||||
"secs": 0
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/DurationDef"
|
||||
}
|
||||
]
|
||||
},
|
||||
"user_cpu_time": {
|
||||
"$ref": "#/definitions/DurationDef"
|
||||
"default": {
|
||||
"nanos": 0,
|
||||
"secs": 0
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/DurationDef"
|
||||
}
|
||||
]
|
||||
},
|
||||
"wall_time": {
|
||||
"$ref": "#/definitions/DurationDef"
|
||||
|
|
|
@ -2,31 +2,40 @@ mod util;
|
|||
|
||||
use other_crate::Duration;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Serialize;
|
||||
use util::*;
|
||||
|
||||
mod other_crate {
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Duration {
|
||||
pub secs: i64,
|
||||
pub nanos: i32,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, JsonSchema)]
|
||||
#[derive(Debug, JsonSchema, Serialize)]
|
||||
#[serde(remote = "Duration")]
|
||||
struct DurationDef {
|
||||
secs: i64,
|
||||
nanos: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, JsonSchema)]
|
||||
fn custom_serialize<S>(value: &Duration, ser: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
ser.collect_str(&format_args!("{}.{:09}s", value.secs, value.nanos))
|
||||
}
|
||||
|
||||
#[derive(Debug, JsonSchema, Serialize)]
|
||||
struct Process {
|
||||
command_line: String,
|
||||
#[serde(with = "DurationDef")]
|
||||
wall_time: Duration,
|
||||
#[serde(with = "DurationDef")]
|
||||
#[serde(default, with = "DurationDef")]
|
||||
user_cpu_time: Duration,
|
||||
#[serde(deserialize_with = "some_serialize_function")]
|
||||
// FIXME this should serialize the default as "0.000000000s"
|
||||
#[serde(default, serialize_with = "custom_serialize")]
|
||||
#[schemars(with = "DurationDef")]
|
||||
system_cpu_time: Duration,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue