Skip to content

Instantly share code, notes, and snippets.

@paradoja
Created June 6, 2017 10:03

Revisions

  1. paradoja created this gist Jun 6, 2017.
    12 changes: 12 additions & 0 deletions caseClassToMap.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    import shapeless._, syntax.std.product._, spray.json.{JsNumber, JsString}

    case class Person(name: String, age: Int)

    val p = Person("Pepe", 54)

    p.toMap[Symbol, Any] // res0: Map[Symbol,Any] = Map('age -> 54, 'name -> Pepe)

    p.toMap[Symbol, Any].map {
    case (s, i: Int) => s.toString -> JsNumber(i)
    case (s, x: Any) => s.toString -> JsString(x.toString)
    } // res1: Map[String,Product with Serializable with spray.json.JsValue] = Map('age -> 54, 'name -> "Pepe")