@@ -22,22 +22,32 @@ data class Team(val members: List<Person>, val colors: List<String> = listOf())
2222``` kotlin
2323/* *
2424 * Example policy that (very crudely) mimicks the way that Jackson serializes xml. It starts by eliding defaults.
25- * Note that this version doesn't handle the jackson annotations.
25+ * Note that this version doesn't handle the jackson annotations, and is not configurable .
2626 */
27- object JacksonPolicy :
28- DefaultXmlSerializationPolicy (false , encodeDefault = XmlSerializationPolicy .XmlEncodeDefault .NEVER ) {
27+ class JacksonPolicy (formatCache : FormatCache = defaultSharedFormatCache(), config : Builder .() -> Unit = {}) :
28+ DefaultXmlSerializationPolicy (formatCache, {
29+ pedantic = false
30+ encodeDefault = XmlSerializationPolicy .XmlEncodeDefault .NEVER
31+ config()
32+ }) {
33+
34+ constructor (config: Builder .() -> Unit ): this (defaultSharedFormatCache(), config)
35+
2936 /*
3037 * Rather than replacing the method wholesale, just make attributes into elements unless the [XmlElement] annotation
3138 * is present with a `false` value on the value attribute.
3239 */
33- override fun effectiveOutputKind (serializerParent : SafeParentInfo , tagParent : SafeParentInfo ): OutputKind {
34- val r = super .effectiveOutputKind(serializerParent, tagParent)
40+ override fun effectiveOutputKind (
41+ serializerParent : SafeParentInfo ,
42+ tagParent : SafeParentInfo ,
43+ canBeAttribute : Boolean
44+ ): OutputKind {
45+ val r = super .effectiveOutputKind(serializerParent, tagParent, canBeAttribute)
3546 return when {
3647 // Do take into account the XmlElement annotation
3748 r == OutputKind .Attribute &&
38- serializerParent.elementUseAnnotations.mapNotNull { it as ? XmlElement }
39- .firstOrNull()?.value != false
40- -> OutputKind .Element
49+ serializerParent.useAnnIsElement != false ->
50+ OutputKind .Element
4151
4252 else -> r
4353 }
@@ -53,14 +63,28 @@ object JacksonPolicy :
5363 tagParent : SafeParentInfo ,
5464 outputKind : OutputKind ,
5565 useName : XmlSerializationPolicy .DeclaredNameInfo
56- ): QName {
66+ ): QName {
5767 return useName.annotatedName
58- ? : serializerParent.elemenTypeDescriptor .typeQname
59- ? : serialNameToQName (useName.serialName , tagParent.namespace)
68+ ? : serializerParent.elementTypeDescriptor .typeQname
69+ ? : serialUseNameToQName (useName, tagParent.namespace)
6070 }
6171
6272}
6373```
74+ For allowing elegant configuration, the below code prvoides for configuration.
75+ Note that this function's implementation could be adjusted to allow for
76+ a class (not object) policy that would also allow for further configuration.
77+
78+ ``` kotlin
79+ fun XmlConfig.Builder.jacksonPolicy (config : Builder .() -> Unit = {}) {
80+ @OptIn(ExperimentalXmlUtilApi ::class )
81+ policy = JacksonPolicy {
82+ setDefaults_0_91_0()
83+ encodeDefault = XmlSerializationPolicy .XmlEncodeDefault .NEVER
84+ config()
85+ }
86+ }
87+ ```
6488
6589
6690## Example usage
@@ -69,15 +93,15 @@ object JacksonPolicy :
6993fun main () {
7094 val t = Team (listOf (Person (" Joe" , 15 )))
7195 val xml = XML {
72- policy = JacksonPolicy
96+ jacksonPolicy()
7397 }
7498
7599 val encodedString = xml.encodeToString(t) // both versions are available
76100 println (" jackson output:\n ${encodedString.prependIndent(" " )} \n " )
77101
78- // the inline reified version is is also available
102+ // the inline reified version is also available
79103 val reparsedData = xml.decodeFromString<Team >(encodedString)
80104 println (" jackson input: $reparsedData " )
81105
82106}
83- ```
107+ ```
0 commit comments