Skip to content

Commit

Permalink
SimpleValueOutput configurable map/list implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
ghik committed Jun 1, 2018
1 parent df98b30 commit c50245e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,31 @@ object SimpleValueOutput {
*
* @param consumer consumer of serialized value, which is guaranteed to meet the above rules
*/
class SimpleValueOutput(consumer: Any => Unit) extends Output {
class SimpleValueOutput(
consumer: Any => Unit,
newObjectRepr: => mutable.Builder[(String, Any), BMap[String, Any]],
newListRepr: => mutable.Builder[Any, BSeq[Any]]
) extends Output {

def this(consumer: Any => Unit) =
this(consumer, new MHashMap[String, Any], new ListBuffer[Any])

def writeBinary(binary: Array[Byte]) = consumer(binary)
def writeString(str: String) = consumer(str)
def writeDouble(double: Double) = consumer(double)
def writeInt(int: Int) = consumer(int)

def writeList() = new ListOutput {
private val buffer = new ListBuffer[Any]
def writeElement() = new SimpleValueOutput(buffer += _)
private val buffer = newListRepr
def writeElement() = new SimpleValueOutput(buffer += _, newObjectRepr, newListRepr)
def finish() = consumer(buffer.result())
}

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

def writeObject() = new ObjectOutput {
private val result = new mutable.HashMap[String, Any]
def writeField(key: String) = new SimpleValueOutput(v => result += ((key, v)))
private val result = newObjectRepr
def writeField(key: String) = new SimpleValueOutput(v => result += ((key, v)), newObjectRepr, newListRepr)
def finish() = consumer(result)
}

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

def inputType = value match {
case null => InputType.Null
case _: List[Any] => InputType.List
case _: Map[_, Any] => InputType.Object
case _: BSeq[Any] => InputType.List
case _: BMap[_, Any] => InputType.Object
case _ => InputType.Simple
}

Expand Down Expand Up @@ -120,4 +128,5 @@ class SimpleValueInput(value: Any) extends Input {
def skip() = ()
}

class SimpleValueFieldInput(val fieldName: String, value: Any) extends SimpleValueInput(value) with FieldInput
class SimpleValueFieldInput(val fieldName: String, value: Any)
extends SimpleValueInput(value) with FieldInput
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "1.27.6"
version in ThisBuild := "1.27.7"

0 comments on commit c50245e

Please sign in to comment.