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

com.github.kondaurovdev.json_extra.objParsers.QueryObjectParser.scala Maven / Gradle / Ivy

The newest version!
package com.github.kondaurovdev.json_extra.objParsers

import com.github.kondaurovdev.json_extra.mutators.obj.CreateProp
import play.api.libs.json.{JsObject, JsString, JsValue, Json}

object QueryObjectParser {

  val prefixReg = """^.*?\.""".r

  def getProps(s: String, op: String = "="): RawProp = {

    val parts = s.split(op).toList

    val key = parts.head
    val value = parts.lift(1).getOrElse("")

    RawProp(key, value)
  }


}

case class QueryObjectParser(propDel: String = "&", inDel: String = ";") {

  import QueryObjectParser._

  def parse(raw: String): Either[JsValue, JsObject] = {
    var result = Json.obj()

    raw.split(propDel).foreach(p => {
      val prop = getProps(p)
      result = CreateProp(prop.key, JsString(prop.value)).mutate(result)
    })

    Right(result)
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy