Skip to content

Commit

Permalink
bump version and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecTroemel committed Feb 19, 2024
1 parent 77613f5 commit 7b54262
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "quickxml_to_serde"
version = "0.5.0"
version = "0.6.0"
authors = ["Alec Troemel <alec@mirusresearch.com>", "Max Voskob <max@onebro.me>"]
description = "Convert between XML JSON using quickxml and serde"
repository = "https://github.com/AlecTroemel/quickxml_to_serde"
Expand Down
93 changes: 56 additions & 37 deletions README.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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 `<b />` 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
Expand All @@ -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
Expand All @@ -98,9 +117,9 @@ Multiple nodes with the same name are automatically converted into a JSON array.
<b>2</b>
</a>
```
is converted into
is converted into
```json
{ "a":
{ "a":
{ "b": [1,2] }
}
```
Expand All @@ -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 `<a><b>1</b></a>` 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 `<a><b /></a>` 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 `<a><b /></a>` to
```json
Expand Down Expand Up @@ -165,15 +184,15 @@ is converted into
```json
"Test":
{
"Input": 1,
"TestId": "0001"
"Input": 1,
"TestId": "0001"
}
```
- XML prolog is dropped. E.g. `<?xml version="1.0"?>`.
- XML namespace definitions are dropped. E.g. `<Tests xmlns="http://www.adatum.com" />` 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 `<a b="y" />` 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 `<a b="y" />` 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
<CardNumber Month="3" Year="19">1234567</CardNumber>
Expand All @@ -182,45 +201,45 @@ 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.
```xml
<Root>
<TaxRate>7.25</TaxRate>
<Data>
<Category>A</Category>
<Quantity>3</Quantity>
<Price>24.50</Price>
<Category>A</Category>
<Quantity>3</Quantity>
<Price>24.50</Price>
</Data>
<Data>
<Category>B</Category>
<Quantity>1</Quantity>
<Price>89.99</Price>
<Category>B</Category>
<Quantity>1</Quantity>
<Price>89.99</Price>
</Data>
</Root>
```
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
}
}
```
Expand Down

0 comments on commit 7b54262

Please sign in to comment.