轻松地将Key = Value对的字符串解析为Scala案例类

时间:2012-10-25 13:30:01

标签: parsing scala key-value case-class

有没有办法轻松地将一串键值对解析为scala案例类?

例如,来自以下字符串:

"consumer_key=1234ABC, consumer_secret=12345ABC"

case class Auth(consumerKey: String, consumerSecret: String)

4 个答案:

答案 0 :(得分:10)

您可以使用正则表达式和模式匹配:

scala> val R = "consumer_key=(.*), consumer_secret=(.*)".r
R: scala.util.matching.Regex = consumer_key=(.*), consumer_secret=(.*)

scala> "consumer_key=1234ABC, consumer_secret=12345ABC" match {
     |   case R(k, v) => Auth(k, v)
     | }
res0: Auth = Auth(1234ABC,12345ABC)

使用JavaTokenParsers进行更灵活的解析:

import scala.util.parsing.combinator._

case class Auth( consumerKey: String, consumerSecret: Option[String])

class AuthParser extends JavaTokenParsers {
  def auth: Parser[Auth] = key ~ opt("," ~> secret) ^^ { case k ~ s => Auth(k, s)}
  def key: Parser[String] = value("consumer_key")
  def secret: Parser[String] = value("consumer_secret")
  def value(k: String): Parser[String] = k ~ "=" ~> "[^,]*".r
  def apply(s: String) = parseAll(auth, s)
}

用法:

scala> val p = new AuthParser
p: AuthParser = AuthParser@433b9799

scala> p("consumer_key=1234ABC, consumer_secret=12345ABC").get
res0: Auth = Auth(1234ABC,Some(12345ABC))

scala> p("consumer_key=1234ABC").get
res1: Auth = Auth(1234ABC,None)

答案 1 :(得分:1)

我喜欢scala Parsers库,但它有点慢。如果你的字符串看起来都像那样,你可以轻松地做到:

case class Auth( consumerKey:String, consumerSecret:String)
object Auth {
  def fromString(string: String): Seq[Auth] = string.split(", ").toSeq map { pair =>
    val lst = pair.split("=")
    Auth(lst(0), lst(1))
  }
}

答案 2 :(得分:0)

我同意nnythm,但该解决方案没有解决问题。这可能会更好:

case class Auth( consumerKey:String, consumerSecret:String)
object Auth {
  def fromString(s: String): Auth = {
    val p = s.split(", ").toSeq.map { pair => {
        val lst = pair.split("=")
        (lst(0), lst(1))
    }}.toMap
    Auth(p("consumer_key"), p("consumer_secret"))
  }
}

这样做的另一个好处是能够拥有包含任意数量键值对的字符串,并且您可以轻松更改您选择使用的字符串。

答案 3 :(得分:0)

Scala 2.13开始,如果您期望的模式足够简单,则可以通过unapplying a string interpolator对其进行模式匹配:

// case class Auth(consumerKey: String, consumerSecret: String)
"consumer_key=1234ABC, consumer_secret=12345ABC" match {
  case s"consumer_key=$key, consumer_secret=$secret" => Auth(key, secret)
}
// Auth("1234ABC", "12345ABC")