Skip to content

serialization/deserialization with fields of different types like with rust/serde enums #4

@fg-sempre

Description

@fg-sempre

Is there any way to serialize a structure like this?

{
  "kinds" : [
    {
      "name": "name_1", 
      "role": "Test"
    },
    {
      "name": "name_2", 
      "role": { "Check": 1 }
    },
    {
      "name": "name_3", 
      "role": { "Check": 2 }
    }
  ]
}

For example, with rust serde we can use a enum like the one bellow to serialize/deserialize the "role" field

#[derive(Serialize, Deserialize)]
pub enum Role {
    Test,
    Check(i32),
}

But the following schema will fail, of course

    kind_schema <-
            s_map(
              name = s_string(),
              role = s_map(
                Check = s_integer()
              )
            )

          schema <-
            s_map(
              kind = s_vector(kind_schema)
            ) |>
            build_structure()

          list(
            kind = list(
              list(
                name = "name_1",
                role = list(
                  Check = 1L
                )
              ),

              list(
                name = "name_2",
                role = list(
                  Check = 2L
                )
              ),

              list(
                name = "name_3",
                role = "Test"
              )
            )
          ) |>
            serialize_json(schema, pretty = TRUE)

Is it possible to handle the { "role": "Test" } case together with { "role" : {"Check": 1} } ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions