Skip to content

Commit

Permalink
doc: improve root-mapping documentation
Browse files Browse the repository at this point in the history
Added test to verify both type and field can be mapped absolutely
  • Loading branch information
jonas-grgt committed Feb 11, 2024
1 parent a5c0959 commit 23bef32
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,23 @@ Each `@Tag` annotation must include a `path` property, using an XPath-like expre

Path expressions can be **absolute**, starting with a slash, representing a path from the root tag to the mapped tag.
**Relative** paths, without a starting slash, require a parent to be mapped absolutely.
Root mappings can be placed top-level on the class, all subsequent relative mappings are relative to the root mapping.

```java
import java.math.BigDecimal;

@Tag(path = "/WeatherData")
class Weather {
// not annotated with @Tag hence is ignored
String id;

// example for an absolute mapped tag
@Tag(path = "/WeatherData/Location")
Location location;

// example for a relative-mapped tag based upon the top-level mapping
@Tag(path = "CurrentConditions")
Conditions conditions;

// normally all fields without @Tag are ignored, yet this field is
// taken into account because at least one of its child fields is
Expand All @@ -130,6 +136,16 @@ class Temperature {
}
```

Absolute mapping a field of top-level class containing a top-level root mapping is supported.
```java
@Tag(path = "/WeatherData")
class Weather {
@Tag(path = "/WeatherData/Location")
Location location;
}

```

### Attributes


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,27 @@ static class WhiteSpacePathMappingHolder {
@Tag(path = " /DataTypes/Double ")
Double Double;
}

@Test
void absoluteRootMappingWithTopLevelMappedRootType() {
// given
String data = """
<?xml version="1.0" encoding="UTF-8"?>
<DataTypes>
<Double>5.7</Double>
</DataTypes>
""";

// when
var holder = new XjxSerdes().read(data, AbsoluteRootMappingHolder.class);

// then
assertThat(holder.Double).isEqualTo(5.7D);
}

@Tag(path = "/DataTypes")
static class AbsoluteRootMappingHolder {
@Tag(path = "/DataTypes/Double")
Double Double;
}
}

0 comments on commit 23bef32

Please sign in to comment.