<commit_before>package io.flow.play.util

import play.api.libs.json.{JsObject, Json, JsValue}


object FormData {
  def convertFormDataValuesDotNotation(data: Map[String, Seq[String]]): Map[String, JsValue] = {
    data.map { case (key, value) =>
      key -> (value match {
        case a: Seq[Any] => {
          a.toList match {
            case one :: Nil => {
              // Normally we just have single parameters -
              // we default to NON array json value.
              Json.toJson(one)
            }
            case _ => {
              Json.toJson(a)
            }
          }
        }
        case _ => {
          Json.toJson(value)
        }
      })
    }
  }

  def convertFormDataValuesBracketNoations(data: Map[String, Seq[String]]): Iterable[JsValue] = {
    data.map{ case (key, value) =>
      key.split("\\[").foldRight(
        if(key.contains("[]"))
          Json.toJson(value)  //take seq for arrays
        else
          Json.toJson(value.head)
      ){ case (newKey, v) =>
        val newVal = {
          val js = (v \ "").getOrElse(v)
          if(newKey == "]"){
            if(!js.toString.startsWith("[")) {
              val s = (v \ "").getOrElse(v).toString.
                replaceFirst("\\{", "[{").
                reverse.
                replaceFirst("\\}", "]}").   //because its reversed
                reverse

              Json.toJson(Json.parse(s))
            }
            else
              js
          }
          else
            js
        }

        Json.obj(newKey.replace("]", "") -> newVal)
      }
    }
  }

  def formDataToJson(data: Map[String, Seq[String]]): JsValue = {
    val nested =
      if (data.keySet.exists(k => k.contains(".")))
        convertFormDataValuesDotNotation(data).map(fd =>
          fd._1.split("\\.").foldRight(fd._2) { case (key, value) => Json.obj(key -> value) })
      else
        convertFormDataValuesBracketNoations(data)

    Json.toJson(nested.foldLeft(Json.obj()){ case (a, b) => a.deepMerge(b.as[JsObject]) })
  }
}
<commit_msg>Update FormData.scala<commit_after>package io.flow.play.util

import play.api.libs.json.{JsObject, Json, JsValue}


object FormData {
  def convertFormDataValuesDotNotation(data: Map[String, Seq[String]]): Map[String, JsValue] = {
    data.map { case (key, value) =>
      key -> (value match {
        case a: Seq[Any] => {
          a.toList match {
            case one :: Nil => {
              // Normally we just have single parameters -
              // we default to NON array json value.
              Json.toJson(one)
            }
            case _ => {
              Json.toJson(a)
            }
          }
        }
        case _ => {
          Json.toJson(value)
        }
      })
    }
  }

  def convertFormDataValuesBracketNotations(data: Map[String, Seq[String]]): Iterable[JsValue] = {
    data.map{ case (key, value) =>
      key.split("\\[").foldRight(
        if(key.contains("[]"))
          Json.toJson(value)  //take seq for arrays
        else
          Json.toJson(value.head)
      ){ case (newKey, v) =>
        val newVal = {
          val js = (v \ "").getOrElse(v)
          if(newKey == "]"){
            if(!js.toString.startsWith("[")) {
              val s = (v \ "").getOrElse(v).toString.
                replaceFirst("\\{", "[{").
                reverse.
                replaceFirst("\\}", "]}").   //because its reversed
                reverse

              Json.toJson(Json.parse(s))
            }
            else
              js
          }
          else
            js
        }

        Json.obj(newKey.replace("]", "") -> newVal)
      }
    }
  }

  def formDataToJson(data: Map[String, Seq[String]]): JsValue = {
    val nested =
      if (data.keySet.exists(k => k.contains(".")))
        convertFormDataValuesDotNotation(data).map(fd =>
          fd._1.split("\\.").foldRight(fd._2) { case (key, value) => Json.obj(key -> value) })
      else
        convertFormDataValuesBracketNotations(data)

    Json.toJson(nested.foldLeft(Json.obj()){ case (a, b) => a.deepMerge(b.as[JsObject]) })
  }
}

{'fraction_non_alphanumeric': 0.155517578125, 'fraction_numerical': 0.0009765625, 'mean_word_length': 1.5848580441640379, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1063911', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1268, 'n_words': 237}