daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package net.gtod.cusoon.servicetest
  2.  
  3. import com.fasterxml.jackson.core.JsonFactory
  4. import net.gtod.cusoon.graphql.parser.parseObject
  5. import net.gtod.cusoon.test.toJson
  6.  
  7. fun String.roundTripAsArray() = singleArray(fromJsonObject(execute().toJson()))
  8.  
  9. fun String.roundTripAsScalar() = singleScalar(fromJsonObject(execute().toJson()))
  10.  
  11. typealias JsonObject = LinkedHashMap<String, Any?>
  12.  
  13. typealias JsonArray = ArrayList<Any?>
  14.  
  15. fun fromJsonObject(str: String): JsonObject {
  16.     val parser = JsonFactory().createParser(str)
  17.     parser.nextToken() // Start object
  18.     return parser.parseObject() as JsonObject
  19. }
  20.  
  21. fun JsonArray.firstObject(): JsonObject {
  22.     @Suppress("UNCHECKED_CAST")
  23.     return first() as JsonObject
  24. }
  25.  
  26. fun singleArray(obj: JsonObject): JsonArray {
  27.     if (obj.size != 1) {
  28.         throw IllegalArgumentException("Can only call single() on a map of one element")
  29.     }
  30.     @Suppress("UNCHECKED_CAST")
  31.     return obj.getValue(obj.keys.first()) as JsonArray
  32. }
  33.  
  34. fun singleScalar(obj: JsonObject): JsonObject {
  35.     if (obj.size != 1) {
  36.         throw IllegalArgumentException("Can only call single() on a map of one element")
  37.     }
  38.     @Suppress("UNCHECKED_CAST")
  39.     return obj.getValue(obj.keys.first()) as JsonObject
  40. }
RAW Paste Data
Top