Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit d78f980

Browse files
authored
Merge pull request #5 from wajda/master
Fix for #6 - Add scala.collection.Seq SerDe support
2 parents c8b3a65 + d60149d commit d78f980

File tree

4 files changed

+130
-50
lines changed

4 files changed

+130
-50
lines changed

src/main/scala/com/arangodb/velocypack/module/scala/VPackScalaModule.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ class VPackScalaModule extends VPackModule {
1010

1111
def setup[C <: VPackSetupContext[C]](context: C): Unit = {
1212
context.registerDeserializer(classOf[Option[Any]], VPackScalaDeserializers.OPTION, true)
13+
context.registerDeserializer(classOf[Seq[Any]], VPackScalaDeserializers.SEQ)
1314
context.registerDeserializer(classOf[List[Any]], VPackScalaDeserializers.LIST)
1415
context.registerDeserializer(classOf[Map[Any, Any]], VPackScalaDeserializers.MAP)
1516
context.registerDeserializer(classOf[BigInt], VPackScalaDeserializers.BIG_INT)
1617
context.registerDeserializer(classOf[BigDecimal], VPackScalaDeserializers.BIG_DECIMAL)
1718

1819
context.registerSerializer(classOf[Option[Any]], VPackScalaSerializers.OPTION)
20+
context.registerSerializer(classOf[Seq[Any]], VPackScalaSerializers.SEQ)
1921
context.registerSerializer(classOf[List[Any]], VPackScalaSerializers.LIST)
2022
context.registerSerializer(classOf[Map[Any, Any]], VPackScalaSerializers.MAP)
2123
context.registerEnclosingSerializer(classOf[Map[Any, Any]], VPackScalaSerializers.MAP)
Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,61 @@
1-
package com.arangodb.velocypack.module.scala.internal
2-
3-
import com.arangodb.velocypack.VPackDeserializer
4-
import com.arangodb.velocypack.VPackDeserializationContext
5-
import com.arangodb.velocypack.VPackSlice
6-
import com.arangodb.velocypack.VPackDeserializerParameterizedType
7-
import java.lang.reflect.ParameterizedType
8-
import scala.collection.JavaConversions._
9-
import scala.collection.JavaConverters._
10-
11-
object VPackScalaDeserializers {
12-
13-
val OPTION = new VPackDeserializerParameterizedType[Option[Any]] {
14-
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): Option[Any] =
15-
throw new UnsupportedOperationException
16-
17-
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext, t: ParameterizedType): Option[Any] = {
18-
val value = context.deserialize(vpack, t.getActualTypeArguments()(0).asInstanceOf[Class[Any]])
19-
value match {
20-
case null => None
21-
case _ => Some(value)
22-
}
23-
}
24-
}
25-
26-
val LIST = new VPackDeserializerParameterizedType[List[Any]] {
27-
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): List[Any] =
28-
throw new UnsupportedOperationException
29-
30-
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext, t: ParameterizedType): List[Any] = {
31-
val clazz = t.getActualTypeArguments()(0).asInstanceOf[Class[Any]]
32-
vpack.arrayIterator().map { slice => context.deserialize(slice, clazz) }.toList
33-
}
34-
}
35-
36-
val MAP = new VPackDeserializer[Map[Any, Any]] {
37-
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): Map[Any, Any] =
38-
context.deserialize(vpack, classOf[java.util.Map[Any, Any]]).toMap
39-
}
40-
41-
val BIG_INT = new VPackDeserializer[BigInt] {
42-
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): BigInt =
43-
BigInt.javaBigInteger2bigInt(context.deserialize(vpack, classOf[java.math.BigInteger]))
44-
}
45-
46-
val BIG_DECIMAL = new VPackDeserializer[BigDecimal] {
47-
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): BigDecimal =
48-
BigDecimal.javaBigDecimal2bigDecimal(context.deserialize(vpack, classOf[java.math.BigDecimal]))
49-
}
50-
1+
package com.arangodb.velocypack.module.scala.internal
2+
3+
import com.arangodb.velocypack.VPackDeserializer
4+
import com.arangodb.velocypack.VPackDeserializationContext
5+
import com.arangodb.velocypack.VPackSlice
6+
import com.arangodb.velocypack.VPackDeserializerParameterizedType
7+
import java.lang.reflect.ParameterizedType
8+
import scala.collection.JavaConversions._
9+
import scala.collection.JavaConverters._
10+
11+
object VPackScalaDeserializers {
12+
13+
val OPTION = new VPackDeserializerParameterizedType[Option[Any]] {
14+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): Option[Any] =
15+
throw new UnsupportedOperationException
16+
17+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext, t: ParameterizedType): Option[Any] = {
18+
val value = context.deserialize[Any](vpack, t.getActualTypeArguments()(0))
19+
value match {
20+
case null => None
21+
case _ => Some(value)
22+
}
23+
}
24+
}
25+
26+
val SEQ = new VPackDeserializerParameterizedType[Seq[Any]] {
27+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): Seq[Any] =
28+
throw new UnsupportedOperationException
29+
30+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext, t: ParameterizedType): Seq[Any] = {
31+
val clazz = t.getActualTypeArguments()(0).asInstanceOf[Class[Any]]
32+
vpack.arrayIterator().map { slice: VPackSlice => context.deserialize[Any](slice, clazz) }.toSeq
33+
}
34+
}
35+
36+
val LIST = new VPackDeserializerParameterizedType[List[Any]] {
37+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): List[Any] =
38+
throw new UnsupportedOperationException
39+
40+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext, t: ParameterizedType): List[Any] = {
41+
val clazz = t.getActualTypeArguments()(0).asInstanceOf[Class[Any]]
42+
vpack.arrayIterator().map { slice: VPackSlice => context.deserialize[Any](slice, clazz) }.toList
43+
}
44+
}
45+
46+
val MAP = new VPackDeserializer[Map[Any, Any]] {
47+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): Map[Any, Any] =
48+
context.deserialize[java.util.Map[Any, Any]](vpack, classOf[java.util.Map[Any, Any]]).toMap
49+
}
50+
51+
val BIG_INT = new VPackDeserializer[BigInt] {
52+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): BigInt =
53+
BigInt.javaBigInteger2bigInt(context.deserialize(vpack, classOf[java.math.BigInteger]))
54+
}
55+
56+
val BIG_DECIMAL = new VPackDeserializer[BigDecimal] {
57+
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): BigDecimal =
58+
BigDecimal.javaBigDecimal2bigDecimal(context.deserialize(vpack, classOf[java.math.BigDecimal]))
59+
}
60+
5161
}

src/main/scala/com/arangodb/velocypack/module/scala/internal/VPackScalaSerializers.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ object VPackScalaSerializers {
1313
context.serialize(builder, attribute, value.orNull)
1414
}
1515

16+
val SEQ = new VPackSerializer[Seq[Any]] {
17+
def serialize(builder: VPackBuilder, attribute: String, value: Seq[Any], context: VPackSerializationContext): Unit = {
18+
val list: _root_.java.util.List[Any] = ListBuffer(value: _*)
19+
context.serialize(builder, attribute, list)
20+
}
21+
}
22+
1623
val LIST = new VPackSerializer[List[Any]] {
1724
def serialize(builder: VPackBuilder, attribute: String, value: List[Any], context: VPackSerializationContext): Unit = {
1825
val list: _root_.java.util.List[Any] = ListBuffer(value: _*)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.arangodb.velocypack.module.scala
2+
3+
import org.scalatest.Matchers
4+
import org.scalatest.FunSuite
5+
import scala.beans.BeanProperty
6+
import com.arangodb.velocypack.VPack
7+
import com.arangodb.velocypack.VPackBuilder
8+
import com.arangodb.velocypack.ValueType
9+
10+
case class SeqTestEntity(@BeanProperty var s: Seq[String] = Seq(), @BeanProperty var i: Seq[Int] = Seq(), @BeanProperty var o: Seq[SeqTestEntity] = Seq()) {
11+
def this() = this(s = Seq())
12+
}
13+
14+
class VPackSeqTest extends FunSuite with Matchers {
15+
16+
test("serialize seq") {
17+
val vp = new VPack.Builder().registerModule(new VPackScalaModule).build()
18+
val vpack = vp.serialize(SeqTestEntity(Seq("hello world"), Seq(69), Seq(new SeqTestEntity)))
19+
vpack should not be null
20+
vpack.isObject should be(true)
21+
vpack.get("s").isArray should be(true)
22+
vpack.get("s").size should be(1)
23+
vpack.get("s").get(0).isString should be(true)
24+
vpack.get("s").get(0).getAsString should be("hello world")
25+
vpack.get("i").isArray should be(true)
26+
vpack.get("i").size should be(1)
27+
vpack.get("i").get(0).isInteger should be(true)
28+
vpack.get("i").get(0).getAsInt should be(69)
29+
vpack.get("o").isArray should be(true)
30+
vpack.get("o").size should be(1)
31+
vpack.get("o").get(0).isObject should be(true)
32+
}
33+
34+
test("deserialize seq") {
35+
val builder = new VPackBuilder()
36+
builder add ValueType.OBJECT
37+
builder add ("s", ValueType.ARRAY)
38+
builder add "hello world"
39+
builder.close
40+
builder add ("i", ValueType.ARRAY)
41+
builder add new Integer(69)
42+
builder.close
43+
builder add ("o", ValueType.ARRAY)
44+
builder add ValueType.OBJECT
45+
builder.close
46+
builder.close
47+
builder.close
48+
49+
val vp = new VPack.Builder().registerModule(new VPackScalaModule).build()
50+
val entity: SeqTestEntity = vp.deserialize(builder.slice, classOf[SeqTestEntity])
51+
entity should not be null
52+
entity.s.size should be(1)
53+
entity.s(0) should be("hello world")
54+
entity.i.size should be(1)
55+
entity.i(0) should be(69)
56+
entity.o.size should be(1)
57+
entity.o(0) should not be null
58+
entity.o(0) shouldBe a [SeqTestEntity]
59+
}
60+
61+
}

0 commit comments

Comments
 (0)