All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.gesellix.util.QueryUtil.groovy Maven / Gradle / Ivy

There is a newer version: 2024-09-15T19-40-00-groovy-4
Show newest version
package de.gesellix.util

import com.squareup.moshi.Moshi

class QueryUtil {

  private Moshi moshi = new Moshi.Builder().build()

  void applyDefaults(Map query, Map defaults) {
    defaults.each { String k, Object v ->
      if (!query.containsKey(k)) {
        query.put(k, v)
      }
    }
  }

  void jsonEncodeQueryParameter(Map query, String parameterName) {
    query.each { String k, Object v ->
      if (v != null && k == parameterName) {
        if (v instanceof Map) {
          query.put(k, moshi.adapter(Map).toJson(v))
        }
        else {
          throw new UnsupportedOperationException("Only Maps are supported, but a ${v.class} has been found for key $k")
        }
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy