AlecTroemel / quickxml_to_serde

Convert between XML JSON using quickxml and serde
MIT License
24 stars 14 forks source link

Enforcing JSON ARRAY for some XML nodes #13

Closed rimutaka closed 3 years ago

rimutaka commented 3 years ago

If an array with a single value is serialized into XML and then converted into JSON it stops being an array and becomes a value or an object. Then it fails to deserialize into Vec or any other collection type. Examples:

<a>
    <b>1</b>
    <b>2</b>
</a>

becomes

"a": {
    "b": [1,2]
}

and can be deserialized into Vec<i64>, but

<a>
    <b>1</b>
</a>

becomes

"a": {
    "b": 1
}

and fails if we try to put it into the same Vec<i64> type.

Proposed solution

  1. Add JsonType::Array to enforce an array on the mapped XML elements the same way we enforce other types

  2. Allow mixing JsonType::Array with other types, e.g. JsonType::AlwaysString so that

    <a>
    <b>1</b>
    </a>

    becomes

    "a": { "b": [1] }

    with Infer or

    "a": { "b": ["1"] }

    with AlwaysString

  3. This will be an extension to recently merged type enforcement, which is an optional feature and will affect existing implementations.

@AlecTroemel , Alec, do you have a minute to review this? Does it make sense? Am I on the right path here?