Skip to content

Commit c50245e

Browse files
committed
SimpleValueOutput configurable map/list implementations
1 parent df98b30 commit c50245e

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

commons-core/src/main/scala/com/avsystem/commons/serialization/SimpleValueInputOutput.scala

+17-8
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,31 @@ object SimpleValueOutput {
4040
*
4141
* @param consumer consumer of serialized value, which is guaranteed to meet the above rules
4242
*/
43-
class SimpleValueOutput(consumer: Any => Unit) extends Output {
43+
class SimpleValueOutput(
44+
consumer: Any => Unit,
45+
newObjectRepr: => mutable.Builder[(String, Any), BMap[String, Any]],
46+
newListRepr: => mutable.Builder[Any, BSeq[Any]]
47+
) extends Output {
48+
49+
def this(consumer: Any => Unit) =
50+
this(consumer, new MHashMap[String, Any], new ListBuffer[Any])
51+
4452
def writeBinary(binary: Array[Byte]) = consumer(binary)
4553
def writeString(str: String) = consumer(str)
4654
def writeDouble(double: Double) = consumer(double)
4755
def writeInt(int: Int) = consumer(int)
4856

4957
def writeList() = new ListOutput {
50-
private val buffer = new ListBuffer[Any]
51-
def writeElement() = new SimpleValueOutput(buffer += _)
58+
private val buffer = newListRepr
59+
def writeElement() = new SimpleValueOutput(buffer += _, newObjectRepr, newListRepr)
5260
def finish() = consumer(buffer.result())
5361
}
5462

5563
def writeBoolean(boolean: Boolean) = consumer(boolean)
5664

5765
def writeObject() = new ObjectOutput {
58-
private val result = new mutable.HashMap[String, Any]
59-
def writeField(key: String) = new SimpleValueOutput(v => result += ((key, v)))
66+
private val result = newObjectRepr
67+
def writeField(key: String) = new SimpleValueOutput(v => result += ((key, v)), newObjectRepr, newListRepr)
6068
def finish() = consumer(result)
6169
}
6270

@@ -85,8 +93,8 @@ class SimpleValueInput(value: Any) extends Input {
8593

8694
def inputType = value match {
8795
case null => InputType.Null
88-
case _: List[Any] => InputType.List
89-
case _: Map[_, Any] => InputType.Object
96+
case _: BSeq[Any] => InputType.List
97+
case _: BMap[_, Any] => InputType.Object
9098
case _ => InputType.Simple
9199
}
92100

@@ -120,4 +128,5 @@ class SimpleValueInput(value: Any) extends Input {
120128
def skip() = ()
121129
}
122130

123-
class SimpleValueFieldInput(val fieldName: String, value: Any) extends SimpleValueInput(value) with FieldInput
131+
class SimpleValueFieldInput(val fieldName: String, value: Any)
132+
extends SimpleValueInput(value) with FieldInput

version.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version in ThisBuild := "1.27.6"
1+
version in ThisBuild := "1.27.7"

0 commit comments

Comments
 (0)