-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parsing and serializing CQL annotations in ELM XML and JSON (Kotlin f…
…eature branch) (#1493) * XML and JSON serialization for mixed content in ELM * Fix detekt checks
- Loading branch information
Showing
10 changed files
with
228 additions
and
695 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 45 additions & 4 deletions
49
...-xmlutil/src/main/java/org/cqframework/cql/elm/serializing/xmlutil/ElmXmlLibraryCommon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,61 @@ | ||
package org.cqframework.cql.elm.serializing.xmlutil | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.plus | ||
import kotlinx.serialization.modules.polymorphic | ||
import kotlinx.serialization.modules.serializersModuleOf | ||
import nl.adaptivity.xmlutil.QName | ||
import nl.adaptivity.xmlutil.XMLConstants | ||
import nl.adaptivity.xmlutil.serialization.DefaultXmlSerializationPolicy | ||
import nl.adaptivity.xmlutil.serialization.XML | ||
import nl.adaptivity.xmlutil.serialization.structure.SafeParentInfo | ||
import org.hl7.elm_modelinfo.r1.serializing.BigDecimalXmlSerializer | ||
|
||
val builder = | ||
DefaultXmlSerializationPolicy.Builder().apply { | ||
// Use xsi:type for handling polymorphism | ||
typeDiscriminatorName = QName(XMLConstants.XSI_NS_URI, "type", XMLConstants.XSI_PREFIX) | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
val customPolicy = | ||
object : DefaultXmlSerializationPolicy(builder) { | ||
override fun isTransparentPolymorphic( | ||
serializerParent: SafeParentInfo, | ||
tagParent: SafeParentInfo | ||
): Boolean { | ||
// Switch on transparent polymorphic mode for mixed content | ||
if ( | ||
serializerParent.elementSerialDescriptor.serialName == | ||
"kotlinx.serialization.Polymorphic<Any>" | ||
) { | ||
return true | ||
} | ||
return super.isTransparentPolymorphic(serializerParent, tagParent) | ||
} | ||
} | ||
|
||
// Mixed content can include text and Narrative elements | ||
val mixedContentSerializersModule = SerializersModule { | ||
polymorphic(Any::class) { | ||
polymorphic(Any::class, String::class, String.serializer()) | ||
polymorphic( | ||
Any::class, | ||
org.hl7.cql_annotations.r1.Narrative::class, | ||
org.hl7.cql_annotations.r1.Narrative.serializer() | ||
) | ||
} | ||
} | ||
|
||
val xml = | ||
XML( | ||
serializersModuleOf(BigDecimalXmlSerializer) + | ||
mixedContentSerializersModule + | ||
org.hl7.elm.r1.serializersModule + | ||
org.hl7.cql_annotations.r1.serializersModule | ||
) { | ||
policy = customPolicy | ||
xmlDeclMode = nl.adaptivity.xmlutil.XmlDeclMode.Charset | ||
defaultPolicy { | ||
typeDiscriminatorName = | ||
QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi") | ||
} | ||
} |
Oops, something went wrong.