Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite serializer #490

Merged
merged 21 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7883a77
Correctly test `deserialize_ignored_any` for simple type deserializers
Mingun Sep 15, 2022
856bfe3
Replace HEX escape codes in tests with decimal codes to be compatible…
Mingun Sep 15, 2022
512a1e0
Remove excess tests, that already covered by other tests more systema…
Mingun Sep 14, 2022
e1f64af
Rename XmlNameSerializer to QNameSerializer
Mingun Sep 14, 2022
bf48446
Do not allow serialize unit structs as XML names. We never use struct…
Mingun Sep 14, 2022
d1b0a9c
Introduce internal `XmlName` struct to validate serialized names agai…
Mingun Sep 8, 2022
e24716b
Implement escaping with 3 levels targeted for a text and attribute value
Mingun Aug 28, 2022
397422e
Implement AtomicSerializer for serialize XSD atomic values
Mingun Aug 28, 2022
0d955d7
Implement SimpleTypeSerializer that used for serializing content of a…
Mingun Aug 28, 2022
459b9bf
Remove `$unflatten=` prefix, introduced in #298
Mingun Apr 2, 2022
931db2e
Add a `$text` special name
Mingun Sep 7, 2022
ad854e1
Implement ElementSerializer which can store object as an XML tree. Fi…
Mingun Aug 29, 2022
bd0b792
Implement indentation for new serializer. Fix #361
Mingun Sep 9, 2022
d7daaf2
Change expectations about results of serializing
Mingun Aug 31, 2022
d050c2b
Add tests for serialization of primitives
Mingun Aug 31, 2022
033be6e
Copy all serializer tests to test serializer in mode without root tag…
Mingun Aug 31, 2022
0868bee
Removed tests, that became duplicates of new tests:
Mingun Sep 1, 2022
4f376b1
Switch to new serializer
Mingun Aug 30, 2022
58924da
Add tests for indentation
Mingun Sep 4, 2022
d0dc219
Change expectations about deserializing attributes
Mingun Jul 21, 2022
8b50c64
Implement special deserializer for key names
Mingun Jul 2, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions Cargo.toml
Expand Up @@ -179,11 +179,6 @@ name = "encodings"
required-features = ["encoding"]
path = "tests/encodings.rs"

[[test]]
name = "serde_attrs"
required-features = ["serialize"]
path = "tests/serde_attrs.rs"

[[test]]
name = "serde_roundtrip"
required-features = ["serialize"]
Expand Down
31 changes: 31 additions & 0 deletions Changelog.md
Expand Up @@ -14,8 +14,39 @@

### Bug Fixes

- [#490]: Ensure that serialization of map keys always produces valid XML names.
In particular, that means that maps with numeric and numeric-like keys (for
example, `"42"`) no longer can be serialized because [XML name] cannot start
from a digit

### Misc Changes

- [#490]: Removed `$unflatten=` special prefix for fields for serde (de)serializer, because:
- it is useless for deserializer
- serializer was rewritten and does not require it anymore

This prefix allowed you to serialize struct field as an XML element and now
replaced by a more thoughtful system explicitly indicating that a field should
be serialized as an attribute by prepending `@` character to its name
- [#490]: Removed `$primitive=` prefix. That prefix allowed you to serialize struct
field as an attribute instead of an element and now replaced by a more thoughtful
system explicitly indicating that a field should be serialized as an attribute
by prepending `@` character to its name
- [#490]: In addition to the `$value` special name for a field a new `$text`
special name was added:
- `$text` is used if you want to map field to text content only. No markup is
expected (but text can represent a list as defined by `xs:list` type)
- `$value` is used if you want to map elements with different names to one field,
that should be represented either by an `enum`, or by sequence of `enum`s
(`Vec`, tuple, etc.), or by string. Use it when you want to map field to any
content of the field, text or markup

Refer to [documentation] for details.

[#490]: https://github.com/tafia/quick-xml/pull/490
[XML name]: https://www.w3.org/TR/xml11/#NT-Name
[documentation]: https://docs.rs/quick-xml/0.27.0/quick_xml/de/index.html#difference-between-text-and-value-special-names

## 0.26.0 -- 2022-10-23

### Misc Changes
Expand Down
36 changes: 4 additions & 32 deletions README.md
Expand Up @@ -119,46 +119,18 @@ quick-xml follows its convention for deserialization, including the

### Parsing the "value" of a tag

If you have an input of the form `<foo abc="xyz">bar</foo>`, and you want to get at the `bar`, you can use the special name `$value`:
If you have an input of the form `<foo abc="xyz">bar</foo>`, and you want to get at the `bar`,
you can use either the special name `$text`, or the special name `$value`:

```rust,ignore
struct Foo {
pub abc: String,
#[serde(rename = "$value")]
#[serde(rename = "$text")]
pub body: String,
}
Mingun marked this conversation as resolved.
Show resolved Hide resolved
```

### Unflattening structs into verbose XML

If your XML files look like `<root><first>value</first><second>value</second></root>`, you can
(de)serialize them with the special name prefix `$unflatten=`:

```rust,ignore
struct Root {
#[serde(rename = "$unflatten=first")]
first: String,
#[serde(rename = "$unflatten=second")]
other_field: String,
}
```

### Serializing unit variants as primitives

The `$primitive` prefix lets you serialize enum variants without associated values (internally referred to as _unit variants_) as primitive strings rather than self-closing tags. Consider the following definitions:

```rust,ignore
enum Foo {
#[serde(rename = "$primitive=Bar")]
Bar
}

struct Root {
foo: Foo
}
```

Serializing `Root { foo: Foo::Bar }` will then yield `<Root foo="Bar"/>` instead of `<Root><Bar/></Root>`.
Read about the difference in the [documentation](https://docs.rs/quick-xml/latest/quick_xml/de/index.html#difference-between-text-and-value-special-names).

### Performance

Expand Down
4 changes: 2 additions & 2 deletions benches/microbenches.rs
Expand Up @@ -24,7 +24,7 @@ volutpat sed cras ornare arcu dui vivamus arcu. Cursus in hac habitasse platea d
purus. Consequat id porta nibh venenatis cras sed felis.";

/// Benchmarks the `Reader::read_event` function with all XML well-formless
/// checks disabled (with and without trimming content of #text nodes)
/// checks disabled (with and without trimming content of $text nodes)
fn read_event(c: &mut Criterion) {
let mut group = c.benchmark_group("read_event");
group.bench_function("trim_text = false", |b| {
Expand Down Expand Up @@ -70,7 +70,7 @@ fn read_event(c: &mut Criterion) {
}

/// Benchmarks the `NsReader::read_resolved_event_into` function with all XML well-formless
/// checks disabled (with and without trimming content of #text nodes)
/// checks disabled (with and without trimming content of $text nodes)
fn read_resolved_event_into(c: &mut Criterion) {
let mut group = c.benchmark_group("NsReader::read_resolved_event_into");
group.bench_function("trim_text = false", |b| {
Expand Down