Skip to content

xvar/AsString

 
 

Repository files navigation

AsString

License JitPack Travis Codacy Codacy

This library works with reflection and helps to display an object as a string without using Kotlin datа classes. There are 2 extensions for this: Any.asString and Any.asString(). The extension works only with the current class. So for nested fields, override the toString method with the same extension. Fields from super classes are also displayed.

Setting up the dependency

dependencies {
    implementation "com.github.g000sha256:AsString:1.0"
}
repositories {
    maven { url "https://jitpack.io" }
}

Example

class Action(
    val actionParam: String = "actionParam",
    val data: Data = Data()
) {

    override fun toString(): String {
        return asString()
    }

}

class Data(
    val dataParam: String = "dataParam"
) {

    override fun toString(): String {
        return asString()
    }

}
val action = Action()
println("$action")

displays a

Action(actionParam: actionParam, data: Data(dataParam: dataParam))

An extended example you can see here.

About

Converts object to a string

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 100.0%