[
https://issues.apache.org/jira/browse/GROOVY-11927?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul King updated GROOVY-11927:
-------------------------------
Description:
Add {{Node.toMap()}} for converting XML node trees to nested Maps, and
{{XmlParser.parseTextAs()}} / {{parseAs()}} for typed XML parsing via Jackson
databinding.
Three levels of support:
* `node.toMap()` — zero-dependency conversion to `Map<String, Object>`, usable
standalone
* `node as MyType` — coercion via `toMap()` + Groovy's named-param constructor
(works for String-typed properties, no additional deps)
* `parser.parseTextAs(Type, xml)` — full typed conversion (String→int, boolean,
etc.) plus `@JsonProperty`/`@JsonFormat` support; requires `jackson-databind`
at runtime, throws clear error if absent
Also adds {{XmlRuntimeException}} and an internal {{JacksonHelper}} utility
using reflective access to avoid a compile-time Jackson dependency on
{{groovy-xml}}.
{code:groovy}
static class ServerConfig {
String host
int port
boolean debug
}
def config = new XmlParser().parseTextAs(ServerConfig, '''
<server>
<host>localhost</host>
<port>8080</port>
<debug>true</debug>
</server>'''.stripIndent())
assert config instanceof ServerConfig
assert config.host == 'localhost'
assert config.port == 8080
assert config.debug == true
{code}
was:
Add `Node.toMap()` for converting XML node trees to nested Maps, and
`XmlParser.parseTextAs()`/`parseAs()` for typed XML parsing via Jackson
databinding.
Three levels of support:
- `node.toMap()` — zero-dependency conversion to `Map<String, Object>`, usable
standalone
- `node as MyType` — coercion via `toMap()` + Groovy's named-param constructor
(works for String-typed properties, no additional deps)
- `parser.parseTextAs(Type, xml)` — full typed conversion (String→int, boolean,
etc.) plus `@JsonProperty`/`@JsonFormat` support; requires `jackson-databind`
at runtime, throws clear error if absent
Also adds `XmlRuntimeException` and an internal `JacksonHelper` utility using
reflective access to avoid a compile-time Jackson dependency on `groovy-xml`.
> Add toMap() and typed parsing support to XmlParser
> --------------------------------------------------
>
> Key: GROOVY-11927
> URL: https://issues.apache.org/jira/browse/GROOVY-11927
> Project: Groovy
> Issue Type: Improvement
> Reporter: Paul King
> Priority: Major
>
> Add {{Node.toMap()}} for converting XML node trees to nested Maps, and
> {{XmlParser.parseTextAs()}} / {{parseAs()}} for typed XML parsing via Jackson
> databinding.
> Three levels of support:
> * `node.toMap()` — zero-dependency conversion to `Map<String, Object>`,
> usable standalone
> * `node as MyType` — coercion via `toMap()` + Groovy's named-param
> constructor (works for String-typed properties, no additional deps)
> * `parser.parseTextAs(Type, xml)` — full typed conversion (String→int,
> boolean, etc.) plus `@JsonProperty`/`@JsonFormat` support; requires
> `jackson-databind` at runtime, throws clear error if absent
> Also adds {{XmlRuntimeException}} and an internal {{JacksonHelper}} utility
> using reflective access to avoid a compile-time Jackson dependency on
> {{groovy-xml}}.
> {code:groovy}
> static class ServerConfig {
> String host
> int port
> boolean debug
> }
> def config = new XmlParser().parseTextAs(ServerConfig, '''
> <server>
> <host>localhost</host>
> <port>8080</port>
> <debug>true</debug>
> </server>'''.stripIndent())
> assert config instanceof ServerConfig
> assert config.host == 'localhost'
> assert config.port == 8080
> assert config.debug == true
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)