将嵌套的事例类转换为Scala中的嵌套映射

将嵌套的事例类转换为Scala中的嵌套映射,scala,case-class,Scala,Case Class,我有两个嵌套的case类: case class InnerClass(param1: String, param2: String) case class OuterClass(myInt: Int, myInner: InnerClass) val x = OuterClass(11, InnerClass("hello", "world")) 我想将其转换为Map[String,Any]类型的嵌套映射,以便得到如下结果: Map(myInt -> 11, myInner ->

我有两个嵌套的case类:

case class InnerClass(param1: String, param2: String)
case class OuterClass(myInt: Int, myInner: InnerClass)
val x = OuterClass(11, InnerClass("hello", "world"))
我想将其转换为Map[String,Any]类型的嵌套映射,以便得到如下结果:

Map(myInt -> 11, myInner -> Map(param1 -> hello, param2 -> world))
当然,解决方案应该是通用的,并且适用于任何案例类

注: 就如何将单个案例类映射到映射给出了一个很好的答案。但我无法将其调整为嵌套的case类。相反,我得到:

Map(myInt -> 11, myInner -> InnerClass(hello,world)

只要递归地调用它。所以

def getCCParams(cc: AnyRef) =
  (Map[String, Any]() /: cc.getClass.getDeclaredFields) {(a, f) =>
    f.setAccessible(true)
    val value = f.get(cc) match {
      // this covers tuples as well as case classes, so there may be a more specific way
      case caseClassInstance: Product => getCCParams(caseClassInstance)
      case x => x
    }
    a + (f.getName -> value)
  }

正如Luigi Plinge在上面的评论中所指出的那样,这是一个非常糟糕的主意,因为你将类型安全抛到了窗外,并且将被许多难看的类型转换和运行时错误所困扰

这就是说,你可以很容易地使用新的:

然后:

scala> println(anyToMap(x))
Map(myInt -> 11, myInner -> Map(param1 -> hello, param2 -> world))

不过,不要这样做。事实上,您应该尽最大努力避免Scala中的运行时反射,这几乎是不必要的。我之所以发布这个答案,是因为如果您确实决定必须使用运行时反射,那么使用Scala反射API比使用Java反射API更好。

这里有一个基于无形状的更具原则性的解决方案。


这个问题不是很清楚。在地图的上下文中,什么是
myInt
myInner
?它们是从
OuterClass
实例中获取的,还是用作键的
字符串?在任何情况下,使用
any
都表明您可能在使用静态类型语言(如Scala)时出错。明确你想做什么,你会得到一些有用的建议。检查
productIterator
,这是一种迭代
Product
s的所有值的方法。所有案例类都是
Product
s.@luigi plinge不确定我是否正确回答了你的问题。在映射的上下文中,myInt和myInner都取自OuterClass实例,并用作键。这是通用的。为了澄清背景,我对应用程序中的主要对象使用嵌套的case类。此外,我还有一个可以接受字符串、整数和映射的函数。我的目的是将我的对象转换为嵌套映射,并将其提供给Bencode编码器。我想我理解:您希望使用case类中的字段名作为映射中的字符串键。实现这一点的唯一方法是使用反射,因为变量名不是应该在运行时可用的数据。除非你真的需要,否则不要使用反射。如果您需要一个字符串作为键,那么在您的case类中有一个
String
字段用于此目的。那应该很容易。为什么不使用
productIterator
?@pedrofurla:因为我想假装
productIterator
不存在?当您使用
scala.reflect.runtime
时,至少很明显您正在做一些令人讨厌的事情。
scala> println(anyToMap(x))
Map(myInt -> 11, myInner -> Map(param1 -> hello, param2 -> world))
class NoSchemaTest extends FlatSpec with ShapelessProduct.Implicits {

"Marshalling and unmarshalling with Map" should "be successful" in {

val op = NoSchema.of[TestClass]

assert(
  op.operator.marshal(
    Map(
      "v1" -> 10,
      "v5" -> Map("_2" -> 12),
      "v3" -> Iterable(Seq("v21" -> 3)),
      "v6" -> TestClass3(v31 = 5)
    )) == TestClass(
    v1 = 10,
    v5 = (null, 12),
    v3 = Some(Seq(Some(
      TestClass2(
        v21 = 3,
        v22 = null
      )))),
    v6 = Some(TestClass3(v31 = 5)),
    v2 = None,
    v4 = null
  )
)

assert(
  op.operator.unmarshal(
    TestClass(
      v1 = 1,
      v2 = null
    )
  ) == Map(
    "v1" -> 1,
    "v2" -> null,
    // the rest are default values
    "v6" -> null,
    "v5" -> Map("_2" -> 2, "_1" -> "a"),
    "v4" -> null,
    "v3" -> Seq(
      Map(
        "v21" -> 3,
        "v22" -> Map("v" -> Map(), "v32" -> Seq(12.0), "v31" -> 0)
      )
    )
  )
)

 }
}

object NoSchemaTest {

case class TestClass(v1: Int,
v2: Option[Seq[Option[Double]]] = None,
v3: Option[Seq[Option[TestClass2]]] = Some(Seq(Some(TestClass2()))),
v4: Seq[Int] = null,
v5: (String, Int) = ("a", 2),
v6: Option[TestClass3] = None
)

case class TestClass2(v21: Int = 3,
v22: TestClass3 = TestClass3(0)
)

case class TestClass3(v31: Int,
v32: Iterable[Double] = Seq(12),
v: Map[String, Int] = Map.empty
)

}

trait DefaultRule extends Operation.Rule {

override def getOperator[V](operation: Operation[V]): Operation.Operator[V] = {

operation.context.noSchema match {

  case _: Primitive[V] => new PrimitiveOperator[V](operation)

  case shapeless: ShapelessProduct[V, _] =>
    new ShapelessProductMapper[V](operation, shapeless)

  case option: OptionContainer[_] =>
    new OptionOperator[option.Elem](
      option.element, operation.asInstanceOf[Operation[Option[option.Elem]]])
      .asInstanceOf[Operation.Operator[V]]

  case map: MapContainer[_] =>
    new MapOperator[map.Elem](
      map.element, operation.asInstanceOf[Operation[Map[String, map.Elem]]])
      .asInstanceOf[Operation.Operator[V]]

  case seq: SeqContainer[_] =>
    new SeqOperator[seq.Elem](
      seq.element, operation.asInstanceOf[Operation[Seq[seq.Elem]]])
      .asInstanceOf[Operation.Operator[V]]

  case iterable: IterableContainer[_] =>
    new IterableOperator[iterable.Elem](
      iterable.element, operation.asInstanceOf[Operation[Iterable[iterable.Elem]]])
      .asInstanceOf[Operation.Operator[V]]
}}}