Last active
December 18, 2015 06:59
-
-
Save zaneli/5743491 to your computer and use it in GitHub Desktop.
「MessagePack for Java を Scala から実行する」ブログ用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zaneli.msgpack | |
import org.msgpack.`type`.{ Value, ValueFactory } | |
object JsonMap2Value { | |
def createValue(content: Any): Value = content match { | |
case (s: String) => ValueFactory.createRawValue(s) | |
case (s: Short) => ValueFactory.createIntegerValue(s) | |
case (i: Int) => ValueFactory.createIntegerValue(i) | |
case (l: Long) => ValueFactory.createIntegerValue(l) | |
case (f: Float) => ValueFactory.createFloatValue(f) | |
case (d: Double) => ValueFactory.createFloatValue(d) | |
case (s: Seq[_]) => ValueFactory.createArrayValue(s.map(createValue).toArray) | |
case (m: Map[String, _]) => | |
ValueFactory.createMapValue(m.collect { | |
case (key, value) => Array(ValueFactory.createRawValue(key), createValue(value)) | |
}.flatten.toArray) | |
case _ => ValueFactory.createNilValue | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zaneli.msgpack | |
import org.msgpack.`type`.ValueFactory | |
trait KeyValueFactory { | |
protected def rawValue(value: String): Value = { | |
ValueFactory.createRawValue(value) | |
} | |
protected def intValue(value: Number): Value = { | |
ValueFactory.createIntegerValue(value.intValue) | |
} | |
protected def arrayValue(values: Seq[Map[String, Any]], func: Map[String, Any] => Value): Value = { | |
ValueFactory.createArrayValue((values.map { value => func(value) }.toArray)) | |
} | |
protected def mapValue(value: Map[String, Any], funcs: Map[String, Any] => Array[Value]*): Value = { | |
ValueFactory.createMapValue(funcs.foldLeft(Array(): Array[Value]) { (values, func) => values ++ func(value) }) | |
} | |
protected def rawKeyValue(key: String)(map: Map[String, Any]): Array[Value] = { | |
map.get(key) match { | |
case Some(value: String) => Array(rawValue(key), rawValue(value)) | |
case _ => Array() | |
} | |
} | |
protected def intKeyValue(key: String)(map: Map[String, Any]): Array[Value] = { | |
map.get(key) match { | |
case Some(value: Number) => Array(rawValue(key), intValue(value.intValue)) | |
case _ => Array() | |
} | |
} | |
protected def arrayKeyValue(key: String)(func: Map[String, Any] => Value)(map: Map[String, Any]): Array[Value] = { | |
map.get(key) match { | |
case Some(values: Seq[Map[String, Any]]) => Array(rawValue(key), arrayValue(values, func)) | |
case _ => Array() | |
} | |
} | |
protected def mapKeyValue(key: String)(funcs: Map[String, Any] => Array[Value]*)(map: Map[String, Any]): Array[Value] = { | |
map.get(key) match { | |
case Some(value: Map[String, Any]) => Array(rawValue(key), mapValue(value, funcs: _*)) | |
case _ => Array() | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zaneli.msgpack | |
import org.msgpack.`type`.{ | |
ArrayValue, | |
BooleanValue, | |
FloatValue, | |
IntegerValue, | |
MapValue, | |
NilValue, | |
RawValue, | |
Value | |
} | |
object Value2JsonMap { | |
def createMap(value: Value): Map[String, Any] = { | |
getValueContent(value) match { | |
case Some(x: Map[String, Any]) => x | |
case _ => Map() | |
} | |
} | |
private def getValueContent(value: Value): Option[Any] = value match { | |
case (r: RawValue) => Some(r.getString) | |
case (i: IntegerValue) => Some(i.getLong) | |
case (f: FloatValue) => Some(f.getDouble) | |
case (b: BooleanValue) => Some(b.getBoolean) | |
case (a: ArrayValue) => Some(a.getElementArray.map(getValueContent).flatten.toList) | |
case (m: MapValue) => | |
Some(m.getKeyValueArray.grouped(2).collect { | |
case Array(key, value) => getValueContent(value).map(key.asRawValue.getString -> _) | |
}.flatten.toMap) | |
case (n: NilValue) => None | |
case _ => None | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment