title: Case class to map in Scala
body: <p>Is there a nice way I can convert a Scala <code>case class</code> instance, e.g.</p>
  
  <pre><code>case class MyClass(param1: String, param2: String)
  val x = MyClass("hello", "world")
  </code></pre>
  
  <p>into a mapping of some kind, e.g.</p>
  
  <pre><code>getCCParams(x) returns "param1" -&gt; "hello", "param2" -&gt; "world"
  </code></pre>
  
  <p>Which works for any case class, not just predefined ones. I've found you can pull the case class name out by writing a method that interrogates the underlying Product class, e.g.</p>
  
  <pre><code>def getCCName(caseobj: Product) = caseobj.productPrefix 
  getCCName(x) returns "MyClass"
  </code></pre>
  
  <p>So I'm looking for a similar solution but for the case class fields. I'd imagine a solution might have to use Java reflection, but I'd hate to write something that might break in a future release of Scala if the underlying implementation of case classes changes.</p>
  
  <p>Currently I'm working on a Scala server and defining the protocol and all its messages and exceptions using case classes, as they are such a beautiful, concise construct for this. But I then need to translate them into a Java map to send over the messaging layer for any client implementation to use. My current implementation just defines a translation for each case class separately, but it would be nice to find a generalised solution.</p>