https://github.com/rtmigo/repr_kt
Kotlin/JVM library converting Kotlin objects to strings that are Kotlin code too
https://github.com/rtmigo/repr_kt
code-generation jvm kotlin library python serialization
Last synced: 10 months ago
JSON representation
Kotlin/JVM library converting Kotlin objects to strings that are Kotlin code too
- Host: GitHub
- URL: https://github.com/rtmigo/repr_kt
- Owner: rtmigo
- License: apache-2.0
- Created: 2022-02-21T13:29:46.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-10-23T07:56:43.000Z (about 3 years ago)
- Last Synced: 2025-01-21T15:32:14.565Z (12 months ago)
- Topics: code-generation, jvm, kotlin, library, python, serialization
- Language: Kotlin
- Homepage:
- Size: 166 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://search.maven.org/artifact/io.github.rtmigo/repr)
# [repr](https://github.com/rtmigo/repr_kt#readme)
Kotlin/JVM library. Converts Kotlin objects to strings.
`.toRepr()` is like `.toString()`, but aims to produce a valid Kotlin code.
Let's say we have the following:
```kotlin
data class Planet(val name: String,
val diameter: Int)
val data = mapOf(
"planets" to listOf(
Planet("Venus", 12104),
Planet("Earth", 12742),
Planet("Mars", 6779)
),
"location" to "Solar System"
)
```
Calling `data.toString()` would give us this:
```text
{
planets=[
Planet(name=Venus, diameter=12104),
Planet(name=Earth, diameter=12742),
Planet(name=Mars, diameter=6779)
],
location=Solar System
}
```
Calling `data.toRepr()` would give us this:
```kotlin
mapOf(
"planets" to listOf(
Planet(name="Venus", diameter=12104),
Planet(name="Earth", diameter=12742),
Planet(name="Mars", diameter=6779)
),
"location" to "Solar System"
)
```
*(both examples formatted for ease of reading)*
The library uses the features of Kotlin reflection. It is inspired by Python's built-in
[`repr`]([https://docs.python.org/3/library/functions.html#repr]) function (and `__repr__`
overloads).
# Install [](https://search.maven.org/artifact/io.github.rtmigo/repr)
```kotlin
// build.gradle.kts
dependencies {
implementation("io.github.rtmigo:repr:X.X.X")
// replace X.X.X with actual version
}
```
Find the latest version and instructions for other build systems
at [Maven Central](https://search.maven.org/artifact/io.github.rtmigo/repr).
# Use
```kotlin
import io.github.rtmigo.repr.toRepr
fun main() {
val data = listOf(1, 2, 3)
println(data.toRepr())
}
```
Output:
```kotlin
listOf(1, 2, 3)
```
## Customize .toRepr for your class
The `.toRepr()` extension method automatically converts `Any` objects.
Sometimes you may want to customize the way how your objects are converted. To do this,
define `toRepr(): String` method for your class.
```kotlin
import io.github.rtmigo.repr.toRepr
class TimeDefault(days: Int) {
val hours = days * 24
}
class TimeTweaked(days: Int) {
val hours = days * 24
fun toRepr() = "TimeTweaked(days=${hours / 24})"
}
fun main() {
println(listOf(TimeDefault(days = 1), TimeDefault(days = 7)).toRepr())
println(listOf(TimeTweaked(days = 1), TimeTweaked(days = 7)).toRepr())
}
```
Output:
```text
listOf(TimeDefault(hours=24), TimeDefault(hours=168))
listOf(TimeTweaked(days=1), TimeTweaked(days=7))
```
Specifying the `toRepr` method here is similar to overloading the `__repr__()` method in Python.
# License
Copyright © 2022 [Artsiom iG](https://github.com/rtmigo).
Licensed under the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.txt).