gt22 icon

Untitled

gt22 | PRO | 06/12/19 03:04:02 PM UTC | 0 ⭐ | 11679 👁️ | Never ⏰ | []
Kotlin |

1.05 KB

|

None

|

0 👍

/

0 👎

package com.uadaf.uadab.utils
 
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.gt22.uadam.utils.set
 
@DslMarker
annotation class JOBMarker
 
@JOBMarker
class JsonObjectBuilder {
 
    val obj = JsonObject()
 
    infix fun String.to(value: String) {
        obj[this] = value
    }
 
    infix fun String.to(value: Char) {
        obj[this] = value
    }
 
    infix fun String.to(value: Number) {
        obj[this] = value
    }
 
    infix fun String.to(value: Boolean) {
        obj[this] = value
    }
 
    infix fun String.to(value: JsonElement) {
        obj[this] = value
    }
 
    infix fun String.to(builder: JsonObjectBuilder.() -> Unit) {
        obj[this] = json(builder)
    }
 
    infix fun String.to(seq: Sequence<JsonElement>) {
        val arr = JsonArray()
        seq.forEach(arr::add)
        obj[this] = arr
    }
 
}
 
fun json(block: JsonObjectBuilder.() -> Unit): JsonObject {
    val b = JsonObjectBuilder()
    b.block()
    return b.obj
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎