Skip to content

Commit

Permalink
While inline serialization itself shouldn't be deferred, the inlineEn…
Browse files Browse the repository at this point in the history
…coder serialization should then defer the serialization.
  • Loading branch information
pdvrieze committed Feb 9, 2024
1 parent c6de193 commit 920153a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2018.
* Copyright (c) 2024.
*
* This file is part of XmlUtil.
* 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
Expand Down Expand Up @@ -409,20 +409,24 @@ internal open class XmlEncoderBase internal constructor(
) : XmlEncoder(parent.xmlDescriptor.getElementDescriptor(childIndex), childIndex, null) {
override fun encodeString(value: String) {
val d = xmlDescriptor.getElementDescriptor(0)
parent.encodeStringElement(d, childIndex, value)
parent.defer(childIndex, forceDefer = true) {
parent.encodeStringElement(d, childIndex, value)
}
}

override fun <T> encodeSerializableValue(
serializer: SerializationStrategy<T>,
value: T
) {
val d = xmlDescriptor.getElementDescriptor(0)
parent.encodeSerializableElement(
d,
childIndex,
serializer,
value
)
parent.defer(childIndex, forceDefer = true) {
parent.encodeSerializableElement(
d,
childIndex,
serializer,
value
)
}
}

@ExperimentalSerializationApi
Expand Down Expand Up @@ -528,13 +532,16 @@ internal open class XmlEncoderBase internal constructor(
}

@OptIn(ExperimentalSerializationApi::class)
open fun defer(index: Int, deferred: CompositeEncoder.() -> Unit) {
if (xmlDescriptor.getElementDescriptor(index).doInline) {
deferred() // Don't defer inline values as it has a problem with the value serializer deferring
open fun defer(index: Int, forceDefer: Boolean = false, deferred: CompositeEncoder.() -> Unit) {
if (! forceDefer && xmlDescriptor.getElementDescriptor(index).doInline) {
// Don't defer inline values as it has a problem with the value serializer deferring
deferred()
} else if (!deferring) { // We should never defer if we are processing deferred elements
deferred()
} else if (reorderInfo != null) {
deferredBuffer.add(reorderInfo[index] to deferred)
} else if (forceDefer) {
deferredBuffer.add(index to deferred)
} else {
val outputKind =
xmlDescriptor.getElementDescriptor(index).outputKind
Expand Down Expand Up @@ -902,8 +909,10 @@ internal open class XmlEncoderBase internal constructor(

override fun defer(
index: Int,
forceDefer: Boolean,
deferred: CompositeEncoder.() -> Unit
) {
// Polymorphic types are "hardcoded" so don't need deferring
deferred()
}

Expand Down Expand Up @@ -1000,7 +1009,8 @@ internal open class XmlEncoderBase internal constructor(
TagEncoder<XmlDescriptor>(xmlDescriptor, null) {

private lateinit var entryKey: QName
override fun defer(index: Int, deferred: CompositeEncoder.() -> Unit) {
override fun defer(index: Int, forceDefer: Boolean, deferred: CompositeEncoder.() -> Unit) {
// a map, where deferring is never needed
deferred()
}

Expand Down Expand Up @@ -1078,7 +1088,10 @@ internal open class XmlEncoderBase internal constructor(
}
}

override fun defer(index: Int, deferred: CompositeEncoder.() -> Unit) = deferred()
override fun defer(index: Int, forceDefer: Boolean, deferred: CompositeEncoder.() -> Unit) {
// This is a list, deferring is not relevant.
deferred()
}

override fun writeBegin() {}

Expand Down Expand Up @@ -1133,8 +1146,10 @@ internal open class XmlEncoderBase internal constructor(

override fun defer(
index: Int,
forceDefer: Boolean,
deferred: CompositeEncoder.() -> Unit
) {
// Deferring in a list is not needed (this is for list elements, not the list itself.
deferred()
}

Expand Down Expand Up @@ -1193,7 +1208,8 @@ internal open class XmlEncoderBase internal constructor(
private lateinit var keySerializer: SerializationStrategy<*>
private var keyValue: Any? = null

override fun defer(index: Int, deferred: CompositeEncoder.() -> Unit) {
override fun defer(index: Int, forceDefer: Boolean, deferred: CompositeEncoder.() -> Unit) {
// deferring is never valid in a map, ther should not be any reordering either
deferred()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 nl.adaptivity.xml.serialization.regressions

import kotlinx.serialization.Serializable
import nl.adaptivity.xmlutil.serialization.XML
import nl.adaptivity.xmlutil.serialization.XmlElement
import nl.adaptivity.xmlutil.serialization.XmlSerialName
import kotlin.jvm.JvmInline
import kotlin.test.Test
import kotlin.test.assertEquals

/**
* Value members should not be ordered differently.
*/
class ValueMemberSerialOrder195 {

@Test
fun testOrder() {
val foo = Foo("a", Foo.Code("b"))
assertEquals(
expected = "<Foo><ServiceName>a</ServiceName><SendingSystem>b</SendingSystem></Foo>",
actual = XML.encodeToString(foo) // <Foo><SendingSystem>b</SendingSystem><ServiceName>a</ServiceName></Foo>
)
}

@Serializable
@XmlSerialName("Foo")
data class Foo(
@XmlElement(true)
@XmlSerialName("ServiceName","", "")
val serviceName: String? = null,

@XmlElement
@XmlSerialName("SendingSystem","", "")
val sendingSystem: Code? = null,
) {
@JvmInline
@Serializable
@XmlSerialName("Code","", "")
value class Code(
val `value`: String,
)
}
}

0 comments on commit 920153a

Please sign in to comment.