Permalink
Browse files

support map and listmap

  • Loading branch information...
1 parent c12d789 commit 256995fd0c889bed1b22c0c9d983a7230b2e40a1 @OlegYch committed Apr 10, 2012
@@ -9,6 +9,7 @@ import org.yaml.snakeyaml.serializer.Serializer
import org.yaml.snakeyaml.emitter.Emitter
import org.yaml.snakeyaml.nodes._
import org.yaml.snakeyaml.constructor.{AbstractConstruct, Construct, Constructor}
+import collection.immutable.ListMap
/**
*/
@@ -25,8 +26,9 @@ class ScalaYaml extends Yaml {
}
}
})
- val defaultSeqConstruct = yamlClassConstructors.get(NodeId.sequence)
yamlClassConstructors.put(NodeId.sequence, new Construct {
+ val defaultSeqConstruct = yamlClassConstructors.get(NodeId.sequence)
+
def construct(node: Node) = {
def default = defaultSeqConstruct.construct(node)
node match {
@@ -48,6 +50,25 @@ class ScalaYaml extends Yaml {
defaultSeqConstruct.construct2ndStep(node, `object`)
}
})
+ yamlClassConstructors.put(NodeId.mapping, new Construct {
+ val defaultMapConstruct = yamlClassConstructors.get(NodeId.mapping)
+
+ def construct(node: Node) = {
+ def default = defaultMapConstruct.construct(node)
+ node match {
+ case mn: MappingNode => if (classOf[Map[_, _]].isAssignableFrom(node.getType)) {
+ ListMap.empty ++ constructMapping(mn).asScala
+ } else {
+ default
+ }
+ case _ => default
+ }
+ }
+
+ def construct2ndStep(node: Node, `object`: AnyRef) {
+ defaultMapConstruct.construct2ndStep(node, `object`)
+ }
+ })
}
representer = new Representer() {
def fixTag(seq: Iterable[AnyRef], snode: SequenceNode, t: Class[_]) {
@@ -84,6 +105,10 @@ class ScalaYaml extends Yaml {
def representData(data: AnyRef) = representScalar(org.yaml.snakeyaml.nodes.Tag.STR,
data.asInstanceOf[Symbol].name)
})
+ multiRepresenters.put(classOf[Map[_, _]], new Represent {
+ def representData(data: AnyRef) = representMapping(org.yaml.snakeyaml.nodes.Tag.MAP,
+ data.asInstanceOf[Map[AnyRef, AnyRef]].asJava, null)
+ })
}
setBeanAccess(BeanAccess.FIELD)
@@ -2,11 +2,13 @@ package com.olegych.config.beans.yaml
import org.specs2.mutable.Specification
import java.io.StringReader
+import org.specs2.specification.AllExpectations
+import collection.immutable.ListMap
-class ScalaYamlTest extends Specification {
+class ScalaYamlTest extends Specification with AllExpectations {
def yaml = new ScalaYaml
- def check[T: Manifest](t: T) = {yaml.represent(t).pp; yaml.load(yaml.dump(t).pp) should_== t}
+ def check[T: Manifest](t: T) = {yaml.represent(t).pp; yaml.load(yaml.dump(t).pp).pp should_== t}
// def check[T:Manifest](t:T) = yaml.loadAs[T](yaml.dump(t).pp,
// manifest[T].erasure.asInstanceOf[Class[T]]) should_== t
@@ -21,6 +23,13 @@ class ScalaYamlTest extends Specification {
"dump option" in {
check(C(Some(A(51, List(7, 8)))))
}
+ "dump map" in {
+ check(AMap(Map(1 -> 1, 2 -> 2)))
+ check(AMap(Map(1 -> 1, 2 -> 2, 3 -> 3)))
+ check(AMap(Map(1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4)))
+ check(AMap(ListMap((1 to 10).zip(1 to 10): _*)))
+ check(AMap())
+ }
"dump none" in {
check(C())
}
@@ -61,3 +70,7 @@ case class C(optionA: Option[A] = None) {
case class A(a: Int = 5, b: List[Int] = 1 :: Nil, listB: List[B] = Nil) {
def this() = this(7)
}
+
+case class AMap(a: Map[Int, Int] = Map(1 -> 1), listMap: ListMap[Int, Int] = ListMap(1 -> 1)) {
+ def this() = this(Map(1 -> 1))
+}

0 comments on commit 256995f

Please sign in to comment.