gt22

Untitled

Jun 12th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.05 KB | None | 0 0
  1. package com.uadaf.uadab.utils
  2.  
  3. import com.google.gson.JsonArray
  4. import com.google.gson.JsonElement
  5. import com.google.gson.JsonObject
  6. import com.gt22.uadam.utils.set
  7.  
  8. @DslMarker
  9. annotation class JOBMarker
  10.  
  11. @JOBMarker
  12. class JsonObjectBuilder {
  13.  
  14.     val obj = JsonObject()
  15.  
  16.     infix fun String.to(value: String) {
  17.         obj[this] = value
  18.     }
  19.  
  20.     infix fun String.to(value: Char) {
  21.         obj[this] = value
  22.     }
  23.  
  24.     infix fun String.to(value: Number) {
  25.         obj[this] = value
  26.     }
  27.  
  28.     infix fun String.to(value: Boolean) {
  29.         obj[this] = value
  30.     }
  31.  
  32.     infix fun String.to(value: JsonElement) {
  33.         obj[this] = value
  34.     }
  35.  
  36.     infix fun String.to(builder: JsonObjectBuilder.() -> Unit) {
  37.         obj[this] = json(builder)
  38.     }
  39.  
  40.     infix fun String.to(seq: Sequence<JsonElement>) {
  41.         val arr = JsonArray()
  42.         seq.forEach(arr::add)
  43.         obj[this] = arr
  44.     }
  45.  
  46. }
  47.  
  48. fun json(block: JsonObjectBuilder.() -> Unit): JsonObject {
  49.     val b = JsonObjectBuilder()
  50.     b.block()
  51.     return b.obj
  52. }
Advertisement
Add Comment
Please, Sign In to add comment