score:-1

This is a most direct approach, not very scalable, though...

 def toGraph(a: A): Map[String, Any] = 
   a match {
     case A(a1,a2,C(c1,c2,D(d1,d2))) => Map(
       "a1" -> a1,
       "a2" -> a2,
       "a3" -> Map(
         "c1" -> c1,
         "c2" -> c2,
         "c3" -> Map(
           "d1" -> d1,
           "d2" -> d2)
         )
       )
    case _ => Map() 
  }

I suppose that for a more "dynamic" approach you can resort to macros


Related Query

More Query from same tag