Skip to content

Generator of bean serializators for XML serialization. Based on KSP

License

Notifications You must be signed in to change notification settings

ValdZX/SimpleXmlKsp

Repository files navigation

Maven Central GitHub license

SimpleXmlKsp

Generator of bean serializators for XML serialization. Based on Google KSP

Usage

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("Package")
data class PackageDto(
    @Attribute(name = "service")
    val serviceName: String,
    @Element(name = "Token")
    val token: String,
    @Element(name = "Location")
    val location: String,
    @Path("Location")
    @Attribute(name = "lat")
    val latitude: Double,
    @Path("Location")
    @Attribute(name = "lng")
    val longitude: Double,
)

####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)

Annotations

@Element

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.

@Text

The Text annotation is used to represent a field or method that appears as text within an XML element.

@Root

This Root annotation is used to annotate classes that need to be serialized.

@Path

The Path annotation is used to specify an XML path where an XML element or attribute is located.

@Attribute

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.

@ElementList

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")
    val name: String,
    @Text
    val value: String
)

@Root("resources")
data class StringResources(
    @ElementList(inline = true, entry = "string")
    val strings: List<StringResource>,
    @Attribute(name = "xmlns:android")
    var androidNs: String = "http://schemas.android.com/apk/res/android"
)

@ElementMap

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"
)

Add to your project

  1. Importing the KSP plugin in the project's build.gradle.kts
plugins {
    id("com.google.devtools.ksp") version "1.6.0-1.0.1" apply false
}

Jvm / Android

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"))
    }
}

Troubleshooting

Can't use the generated code on my IDE

You should set manually the source sets of the generated files, like described here.

###Kotlin multiplatform multiverse:

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"))
      }
   }
}

License

Copyright 2021 Vladislav Khimichenko

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
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.