Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
pdvrieze committed Nov 4, 2024
2 parents cd11406 + d375efb commit 6ba77c9
Show file tree
Hide file tree
Showing 22 changed files with 1,008 additions and 76 deletions.
15 changes: 15 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# 0.90.3
Changes:
- In pedantic mode check that xml/xmlns are not used as names (they
are always invalid). Note that namespaces can be specified using
`@XmlSerialName` (for tags) and `@XmlNamespaceDeclSpec` (for
additional names).
- The cache to use can now be configured. By default it uses the
threadLocal version, but the thread unsafe default is also available,
as is the `FormatCache.Dummy` cache that does no caching.
Fixes:
- Fix support for multiple namespaces inside a single tag (QNameMap)
as reported in #249.
- Use threadlocals for caching document structures by default. This should avoid
multithreading issues when reusing the format (recommended for speed)

# 0.90.2 Mooor Rocketpower!
Changes:
- Update to Kotlin 2.0.21 / Kotlinx.serialization 1.7.3
Expand Down
51 changes: 27 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ package. The `JVM` and `Android` packages provide the native
implementations and depend on (publishing) the `jvmCommon` package.
#### multiplatform (will default to multiplatform implementation for JVM/Android)
```
implementation("io.github.pdvrieze.xmlutil:core:0.90.2")
implementation("io.github.pdvrieze.xmlutil:core:0.90.3")
```
#### **Optional** JVM – uses the stax API _not available_ on Android
```
implementation("io.github.pdvrieze.xmlutil:core-jvm:0.90.2")
implementation("io.github.pdvrieze.xmlutil:core-jvm:0.90.3")
```
#### **Optional** Android – Uses the android streaming library
```
implementation("io.github.pdvrieze.xmlutil:core-android:0.90.2")
implementation("io.github.pdvrieze.xmlutil:core-android:0.90.3")
```
#### JS – Wraps DOM
```
implementation("io.github.pdvrieze.xmlutil:core-js:0.90.2")
implementation("io.github.pdvrieze.xmlutil:core-js:0.90.3")
```

#### Native
Expand All @@ -108,19 +108,19 @@ that mirrors the Java API)
### Serialization
#### multiplatform
```
implementation("io.github.pdvrieze.xmlutil:serialization:0.90.2")
implementation("io.github.pdvrieze.xmlutil:serialization:0.90.3")
```
#### JVM
```
implementation("io.github.pdvrieze.xmlutil:serialization-jvm:0.90.2")
implementation("io.github.pdvrieze.xmlutil:serialization-jvm:0.90.3")
```
#### Android
```
implementation("io.github.pdvrieze.xmlutil:serialization-android:0.90.2")
implementation("io.github.pdvrieze.xmlutil:serialization-android:0.90.3")
```
#### js
```
implementation("io.github.pdvrieze.xmlutil:serialization-js:0.90.2")
implementation("io.github.pdvrieze.xmlutil:serialization-js:0.90.3")
```

### -Ktor- (Deprecated)
Expand Down Expand Up @@ -165,22 +165,25 @@ val format = XML(mySerialModule) {
```
The following options are available when using the XML format builder:

| Option | Description |
|----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `repairNamespaces` | Should namespaces automatically be repaired. This option will be passed on to the `XmlWriter` |
| `xmlDeclMode` | The mode to use for emitting XML declarations (<?xml ...?>). Replaces omitXmlDecl for more finegrained control |
| `indentString` | The indentation to use. Must be a combination of XML whitespace or comments (this is checked). This is passed to the `XmlWriter` |
| -`autoPolymorphic`- | *Deprecated* Shorcut to `policy.autoPolymorphic` | |
| `isInlineCollapsed` | If `true`(default) the content of an inline type is used directly, with the name of the inline type. |
| `xmlVersion` | Which xml version will be written/declared (default XML 1.1) |
| `isCollectingNSAttributes` | (Attempt to) collect all needed namespace declarations and emit them on the root tag, this does have a performance overhead |
| `policy` | This is a class that can be used to define a custom policy that informs how the kotlin structure is translated to XML. It drives most complex configuration |
| `defaultPolicy {}` | Builder that allows configuring the default policy. This policy is stable, it doesn't change across versions. |
| `recommended_0_86_3 {}` | Builder that sets the policy to the recommended defaults per version 0.86.3, this is stable and includes: autopolymorphic, inlineCollapsed, indent=4, p.pedantic, p.typeDiscriminatorName=xsi:type, encodeDefault=ANNOTATED, throwOnRepeatedElement, isStrictAttributeNames |
| `recommended {}` | Builder that sets the policy to the *currently* recommended defaults, note that this policy is *not stable*. This currently includes: autopolymorphic, inlineCollapsed, indent=4, p.pedantic, p.typeDiscriminatorName=xsi:type, encodeDefault=ANNOTATED, throwOnRepeatedElement, isStrictAttributeNames |
| -`indent`- | *Deprecated for reading*: The indentation level (in spaces) to use. This is backed by `indentString`. Reading is "invalid" for `indentString` values that are not purely string sequences. Writing it will set indentation as the specified amount of spaces. |
| -`omitXmlDecl`- | *Deprecated* (use `xmlDeclMode`). Should the generated XML contain an XML declaration or not. This is passed to the `XmlWriter` |
| -`unknownChildHandler`- | *Deprecated into policy* A function that is called when an unknown child is found. By default an exception is thrown but the function can silently ignore it as well. |
| Option | Description |
|----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `repairNamespaces` | Should namespaces automatically be repaired. This option will be passed on to the `XmlWriter` |
| `xmlDeclMode` | The mode to use for emitting XML declarations (<?xml ...?>). Replaces omitXmlDecl for more finegrained control |
| `indentString` | The indentation to use. Must be a combination of XML whitespace or comments (this is checked). This is passed to the `XmlWriter` |
| -`autoPolymorphic`- | *Deprecated* Shorcut to `policy.autoPolymorphic` | |
| `isInlineCollapsed` | If `true`(default) the content of an inline type is used directly, with the name of the inline type. |
| `xmlVersion` | Which xml version will be written/declared (default XML 1.1) |
| `isCollectingNSAttributes` | (Attempt to) collect all needed namespace declarations and emit them on the root tag, this does have a performance overhead |
| `defaultToGenericParser` | Use the generic parser, rather than the platform specific one. |
| `policy` | This is a class that can be used to define a custom policy that informs how the kotlin structure is translated to XML. It drives most complex configuration |
| `defaultPolicy {}` | Builder that allows configuring the default policy. This policy is stable, it doesn't change across versions. |
| `recommended_0_86_3 {}` | Builder that sets the policy to the recommended defaults per version 0.86.3, this is stable and includes: autopolymorphic, inlineCollapsed, indent=4, p.pedantic, p.typeDiscriminatorName=xsi:type, encodeDefault=ANNOTATED, throwOnRepeatedElement, isStrictAttributeNames |
| `recommended_0_90_2 {}` | Builder that sets the policy to the recommended defaults per version 0.90.2, this is stable and extends from 0.86.3. It uses xml 1.1, strict booleans, and minimal xml declaration |
| `fast_0_90_2 {}` | Builder that sets the policy to the recommended for fast parsing. This reduces error checks, but otherwise has the same semantics as 0.90.2. |
| `recommended {}` | Builder that sets the policy to the *currently* recommended defaults (the 0.90.2 configuration) |
| -`indent`- | *Deprecated for reading*: The indentation level (in spaces) to use. This is backed by `indentString`. Reading is "invalid" for `indentString` values that are not purely string sequences. Writing it will set indentation as the specified amount of spaces. |
| -`omitXmlDecl`- | *Deprecated* (use `xmlDeclMode`). Should the generated XML contain an XML declaration or not. This is passed to the `XmlWriter` |
| -`unknownChildHandler`- | *Deprecated into policy* A function that is called when an unknown child is found. By default an exception is thrown but the function can silently ignore it as well. |

The properties that have been moved into the policy can still be set in the builder,
but are no longer able to be read through the config object.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024.
*
* This file is part of xmlutil.
*
* This file is licenced to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You should have received a copy of the license with the source distribution.
* Alternatively, you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package net.devrieze.serialization.examples.typedUnion

import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass
import nl.adaptivity.xmlutil.XmlDeclMode
import nl.adaptivity.xmlutil.serialization.XML


@Serializable
sealed interface Response<out T> {

@Serializable
data class Success<T>(val data: T) : Response<T>

@Serializable
data class Error(val message: String): Response<Nothing>
}

fun main() {
val xml = XML(SerializersModule {
polymorphic(Any::class) {
subclass(String.serializer())
}
}) {
recommended_0_90_2() {
xmlDeclMode = XmlDeclMode.None
}
}

println(xml.encodeToString<Response<String>>(Response.Success("Good")))
println(xml.encodeToString<Response<String>>(Response.Error("Bad")))

println((xml.decodeFromString<Response<String>>("<Success><xsd:string xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">Better</xsd:string></Success>") as Response.Success<String>).data)
}
Loading

0 comments on commit 6ba77c9

Please sign in to comment.