From 7b542621101a4713f9cf5c08ae3fbefbed118a60 Mon Sep 17 00:00:00 2001 From: alec troemel Date: Mon, 19 Feb 2024 10:32:41 -0600 Subject: [PATCH] bump version and update readme --- Cargo.toml | 2 +- README.md | 93 ++++++++++++++++++++++++++++++++---------------------- 2 files changed, 57 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7964d31..16647ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "quickxml_to_serde" -version = "0.5.0" +version = "0.6.0" authors = ["Alec Troemel ", "Max Voskob "] description = "Convert between XML JSON using quickxml and serde" repository = "https://github.com/AlecTroemel/quickxml_to_serde" diff --git a/README.md b/README.md index 895ac09..96d00d3 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,25 @@ let conf = Config::new_with_custom_values(true, "", "text", NullValue::Ignore); ## Enforcing JSON types +### Matching based on absolute path or regex + +You can override the type of absolute paths within the XML + +``` rust +let config = Config::new_with_defaults() + .add_json_type_override("/a/b", JsonArray::Always(JsonType::AlwaysString)); +``` + +Or you can match based on a regex! + +``` rust +let config = Config::new_with_defaults() + .add_json_type_override( + Regex::new(r"element").unwrap(), + JsonArray::Always(JsonType::Infer) + ); +``` + #### Strings The default for this library is to attempt to infer scalar data types, which can be `int`, `float`, `bool` or `string` in JSON. Sometimes it is not desirable like in the example below. Let's assume that attribute `id` is always numeric and can be safely converted to JSON integer. @@ -75,9 +94,9 @@ let conf = Config::new_with_defaults().add_json_type_override("/a/@attr1", JsonA Configuration to make both attributes and the text node of `` always come out as a JSON string: ```rust let conf = Config::new_with_defaults() - .add_json_type_override("/a/@attr1", JsonArray::Infer(JsonType::AlwaysString)) - .add_json_type_override("/a/b/@attr1", JsonArray::Infer(JsonType::AlwaysString)) - .add_json_type_override("/a/b", JsonArray::Infer(JsonType::AlwaysString)); + .add_json_type_override("/a/@attr1", JsonArray::Infer(JsonType::AlwaysString)) + .add_json_type_override("/a/b/@attr1", JsonArray::Infer(JsonType::AlwaysString)) + .add_json_type_override("/a/b", JsonArray::Infer(JsonType::AlwaysString)); ``` #### Boolean @@ -86,7 +105,7 @@ The only two [valid boolean values in JSON](https://json-schema.org/understandin ```rust let conf = Config::new_with_defaults() - .add_json_type_override("/a/b", JsonArray::Infer(JsonType::Bool(vec!["True","true","1","yes"]))); + .add_json_type_override("/a/b", JsonArray::Infer(JsonType::Bool(vec!["True","true","1","yes"]))); ``` #### Arrays @@ -98,9 +117,9 @@ Multiple nodes with the same name are automatically converted into a JSON array. 2 ``` -is converted into +is converted into ```json -{ "a": +{ "a": { "b": [1,2] } } ``` @@ -112,27 +131,27 @@ By default, a single element like ``` is converted into a scalar value or a map ```json -{ "a": - { "b": 1 } +{ "a": + { "b": 1 } } ``` You can use `add_json_type_override()` with `JsonArray::Always()` to create a JSON array regardless of the number of elements so that `1` becomes `{ "a": { "b": [1] } }`. -`JsonArray::Always()` and `JsonArray::Infer()` can specify what underlying JSON type should be used, e.g. +`JsonArray::Always()` and `JsonArray::Infer()` can specify what underlying JSON type should be used, e.g. * `JsonArray::Infer(JsonType::AlwaysString)` - infer array, convert the values to JSON string * `JsonArray::Always(JsonType::Infer)` - always wrap the values in a JSON array, infer the value types * `JsonArray::Always(JsonType::AlwaysString)` - always wrap the values in a JSON array and convert values to JSON string ```rust let config = Config::new_with_defaults() - .add_json_type_override("/a/b", JsonArray::Always(JsonType::AlwaysString)); + .add_json_type_override("/a/b", JsonArray::Always(JsonType::AlwaysString)); ``` Conversion of empty XML nodes like `` depends on `NullValue` setting. For example, ```rust let config = Config::new_with_custom_values(false, "@", "#text", NullValue::Ignore) - .add_json_type_override("/a/b", JsonArray::Always(JsonType::Infer)); + .add_json_type_override("/a/b", JsonArray::Always(JsonType::Infer)); ``` converts `` to ```json @@ -165,15 +184,15 @@ is converted into ```json "Test": { - "Input": 1, - "TestId": "0001" + "Input": 1, + "TestId": "0001" } ``` - XML prolog is dropped. E.g. ``. - XML namespace definitions are dropped. E.g. `` becomes `"Tests":{}` - Processing instructions, comments and DTD are ignored - **Presence of CDATA in the XML results in malformed JSON** -- XML attributes can be prefixed via `Config::xml_attr_prefix`. E.g. using the default prefix `@` converts `` into `{ "a": {"@b":"y"} }`. You can use no prefix or set your own value. +- XML attributes can be prefixed via `Config::xml_attr_prefix`. E.g. using the default prefix `@` converts `` into `{ "a": {"@b":"y"} }`. You can use no prefix or set your own value. - Complex XML elements with text nodes put the XML text node value into a JSON property named in `Config::xml_text_node_prop_name`. E.g. setting `xml_text_node_prop_name` to `text` will convert ```xml 1234567 @@ -182,10 +201,10 @@ into ```json { "CardNumber": { - "Month": 3, - "Year": 19, - "text": 1234567 - } + "Month": 3, + "Year": 19, + "text": 1234567 + } } ``` - Elements with identical names are collected into arrays. E.g. @@ -193,14 +212,14 @@ into 7.25 - A - 3 - 24.50 + A + 3 + 24.50 - B - 1 - 89.99 + B + 1 + 89.99 ``` @@ -208,19 +227,19 @@ is converted into ```json { "Root": { - "Data": [ - { - "Category": "A", - "Price": 24.5, - "Quantity": 3 - }, - { - "Category": "B", - "Price": 89.99, - "Quantity": 1 - } - ], - "TaxRate": 7.25 + "Data": [ + { + "Category": "A", + "Price": 24.5, + "Quantity": 3 + }, + { + "Category": "B", + "Price": 89.99, + "Quantity": 1 + } + ], + "TaxRate": 7.25 } } ```