Helios for Kotlin,Helios is a library for JSON handling that’s well suited with all of Kotlin’s sorts inclusive of nullable sorts and information lessons, with few Java sorts like UUID and BigDecimal and, also, with some Arrow facts types, Option, Either, and Tuple for example.
Let’s take a short take a look at how you can start the usage of Helios:
Helios for Kotlin,Adding the dependency
First off, we want to add the Helios dependency to our challenge. We can do it by including the subsequent block to our build.Gradle:
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
compile "com.47deg:helios-core:0.1.0"
compile "com.47deg:helios-parser:0.1.0"
compile "com.47deg:helios-optics:0.1.0"
}
Helios for Kotlin,Encoding and Decoding
After including the dependency, we will start growing our little model:
With the @json annotation, we’re capable of mechanically generate encoders and decoders for the Person records class at assemble time as follows:
val person = Person("Cristobal", 37)
val jsonFromPerson: Json = with(Person.encoder()) {
person.encode()
}
Person.decoder().decode(jsonFromPerson)
Helios for KotlinThe DSL
We have a easy DSL that takes gain of the quite kotlin syntax for growing your personal JSON:
import helios.core.Json
val json: Json = JsObject(
"name" to JsString("Simon"),
"age" to JsNumber(26)
)
Parsing JSON
Of route, we can parse a JSON from an String or a File. Using all the electricity from the Arrow library, we’re able to take care of parsing mistakes:
val jsonStr =
"""{
"name": "Cristobal",
"age": 37
}"""
val jsonOrError: Either<Throwable, Json> = Json.parseFromString(jsonStr)
val json : Json = jsonOrError.getOrHandle { JsString("") }
Navigation through JSON
The optics module lets in you to question and modify JSON with collect-time safety and without any boilerplate.
Import helios.Optics.*
Json.Direction.Select("name").String.Alter(json, String::toUpperCase)
Note that the code generation will give you an accessor for each JSON subject.
Json.Course.Call.String.Regulate(json, String::toUpperCase)
Both will produce the equal:
{
"name": "CRISTOBAL",
"age": 37
}
Resources If you need to analyze more, please take a look at Helios’ documentation, we additionally have an examples module on the way to be beneficial.
If you have got any questions or feedback, experience unfastened to attain out to us at the Helios slack channel or post an trouble to the Helios repository.
This is just the primary launch of Helios; we’ve got plans for more high-quality features like Java time help and different integrations.
The active improvement of Helios is proudly subsidized via 47 Degrees, a Functional Programming consultancy with a focus at the Scala, Kotlin, and Swift Programming languages.