Created
November 11, 2015 05:29
-
-
Save Centaur/a2ca79f44c392b95fd0f to your computer and use it in GitHub Desktop.
play-json to Argonaut
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
import java.io.NotSerializableException | |
import play.api.libs.json._ | |
/** | |
* Created by xiefei on 15/11/11. | |
*/ | |
trait Jsonable | |
case class Config(name: String) | |
object Config { | |
implicit val configFormat = Json.format[Config] | |
} | |
case class ConfigChanged(config: Config, cmd: Jsonable) | |
case object UseDefault | |
object PatternMatchingNeeded { | |
val ConfigChangedClass = classOf[ConfigChanged].getName | |
def playJsonVersion(bytes: Array[Byte]) = { | |
Json.parse(new String(bytes, "UTF-8")) match { | |
case obj: JsObject => obj.fields match { | |
case Seq( | |
("manifest", JsString(ConfigChangedClass)), | |
("config", config: JsValue), | |
("cmd", configcmd: JsValue)) => | |
ConfigChanged(configFromJson(config), jsonableFromJson(configcmd)) | |
case _ => jsonableFromJson(obj) | |
} | |
case JsString("UseDefault") => UseDefault | |
case other => jsonableFromJson(other) | |
} | |
} | |
def configFromJson(value: JsValue): Config = value.as[Config] | |
def jsonableFromJson(value: JsValue): Jsonable = value match { | |
case obj: JsObject => obj.fields match { | |
case Seq( | |
("manifest", JsString(clazzname)), | |
("payload", payload: JsValue)) => | |
reader.apply(payload).apply(clazzname) | |
case _ => throw new NotSerializableException(value.toString()) | |
} | |
case _ => throw new NotSerializableException(value.toString()) | |
} | |
val reader = { payload: JsValue => clazzname: String => | |
new Jsonable{} | |
} | |
} |
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
"com.typesafe.play" %% "play-json" % "2.4.2", |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment