SimpleXmlKsp
1.0.0-alpha01indexedGenerates XML serialization bean serializers using annotation processing. Supports serializing/deserializing XML elements, attributes, and paths with annotations for structs like lists and maps.
Generates XML serialization bean serializers using annotation processing. Supports serializing/deserializing XML elements, attributes, and paths with annotations for structs like lists and maps.
Generator of bean serializators for XML serialization. Based on Google KSP
Provide the following arguments to KSP in the module's build.gradle.ksp.
ksp {
arg("simplexml.ksp.modulepackage", "com.my.app")
arg("simplexml.ksp.modulename", "CurrentModuleName")
}
Enrol serializers on app startup:
CurrentModuleNameSerializersEnrolment.enrol()
Required XML:
<Package service="GET_INFO">
<Token>S290bGluIGlzIGF3ZXNvbWU=</Token>
<Location lat="50.004977" lng="36.231117">Ukraine, Kharkiv</Location>
</Package>
Use annotations for construct xml structure:
@Root()
(
serviceName: String,
token: String,
location: String,
latitude: ,
longitude: ,
)
####Serialize:
val bean = PackageDto(
serviceName = "GET_INFO",
token = "S290bGluIGlzIGF3ZXNvbWU=",
location = "Ukraine, Kharkiv",
latitude = 50.004977,
longitude = 36.231117
)
val xml: String = SimpleXml.serialize(bean)
####Deserialize:
val deserializedBean: PackageDto = SimpleXml.deserialize(xml)
The Element annotation is used to represent a field or method that appears as an XML element. Fields or methods that are annotated with this can be either primitive or compound, that is, represent an object that can be serialized and deserialized. Below is an example of the serialized format for a compound object.
The Text annotation is used to represent a field or method that appears as text within an XML element.
This Root annotation is used to annotate classes that need to be serialized.
The Path annotation is used to specify an XML path where an XML element or attribute is located.
The Attribute annotation represents a serializable XML attribute within an XML element. An object annotated with this is typically a primitive or enumerated type. Conversion from the attribute to primitive type is done with a Transform object. If a suitable transform can be found then this will convert the attribute string value to an object instance, which can be assigned to the annotated field, or passed to the annotated method.
The ElementList annotation represents a method or field that is a List for storing entries. However, a class attribute can be used to override the field type, however the type must be assignable.
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string name="appName">The best app</string>
<string name="greetings">Hello!</string>
</resources>
@Root("string")
data class StringResource(
@Attribute(name = "name")
name: String,
value: String
)
(
strings: List<StringResource>,
androidNs: String =
)
The ElementMap annotation represents a method or field that is a Map for storing key value pairs.
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string name="appName">The best app</string>
<string name="greetings">Hello!</string>
</resources>
@Root("resources")
data class StringResourcesMap(
@ElementMap(inline = true, entry = "string", key = "name", attribute = true)
val strings: Map<String, String>,
@Attribute(name = "xmlns:android")
var androidNs: String = "http://schemas.android.com/apk/res/android"
)
build.gradle.ktsplugins {
id("com.google.devtools.ksp") version "1.6.0-1.0.1" apply false
}
plugins {
id("com.google.devtools.ksp")
}
dependencies {
implementation("io.github.valdzx:simplexml-ksp-core:1.0.0-alpha01")
ksp("io.github.valdzx:simplexml-ksp-processor:1.0.0-alpha01")
}
Make sure that you have mavenCentral() in the list of repositories:
repository {
mavenCentral()
}
###Kotlin multiplatform multiverse
Available for JVM, Android, iOS, JavaScript, macosX64
plugins {
kotlin("multiplatform")
id("com.google.devtools.ksp")
}
val commonMain by getting {
dependencies {
implementation("io.github.valdzx:simplexml-ksp-core:1.0.0-alpha01")
configurations["ksp"].dependencies.add(project.dependencies.create("io.github.valdzx:simplexml-ksp-processor:1.0.0-alpha01"))
}
}
You should set manually the source sets of the generated files, like described here.
###Kotlin multiplatform multiverse:
Copyright 2021 Vladislav Khimichenko
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance the License.
You may obtain a copy of the License at
http:
Unless applicable law agreed to writing, software
distributed under the License distributed an BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
See the License the specific language governing permissions
limitations under the License.
kotlin {
...
sourceSets.all {
kotlin.srcDir("build/generated/ksp/$name/kotlin")
}
}
###Android:
applicationVariants.all {
val variantName = name
sourceSets {
getByName("main") {
java.srcDir(File("build/generated/ksp/$variantName/kotlin"))
}
}
}
Surfaced from shared tags and platforms — no rankings paid for.