json


How to create encoder/decoder that depends on fields in message for circe


The use case Im trying to get working with using Circle is as follows.
Given a stream of JSON messages I want to match on the op and convert the message from and to the appropriate type. The code below shows all the details. However the code des not compile since it need implicit encoders for the different ResponseMessag:es.
Why is it not enogh to use import io.circe.generic.auto._ to get
encoders and decoders for the different ResponseMessage types and then
only write a custom for ResponseMessage? How can I change the example
below to get the tests to pass?
object OpTypes {
type ResponseOpType = String
val Connection: ResponseOpType = "connection"
val Status: ResponseOpType = "status"
}
import OpTypes._
sealed trait ResponseMessage {
/* The operation type */
def op: ResponseOpType
/* Client generated unique id to link request with response (like json rpc) */
def id: Integer
}
case class ConnectionMessage (
op: ResponseOpType,
id: Integer,
/* The connection id */
connectionId: String
) extends ResponseMessage
case class StatusMessage (
op: ResponseOpType,
id: Integer,
/* Additional message in case of a failure */
errorMessage: String,
/* The type of error in case of a failure */
errorCode: String,
/* The connection id */
connectionId: String,
/* Is the connection now closed */
connectionClosed: Boolean,
/* The status of the last request */
statusCode: String
) extends ResponseMessage
case class UnableToParseStreamResponseMessage (op:String="Error", id:Integer = 0) extends ResponseMessage
test("Circle support") {
import io.circe.{Decoder, Encoder}
import io.circe.generic.auto._
import io.circe.syntax._
import io.circe.parser.decode
implicit val decodeResponseMessage: Decoder[ResponseMessage] = Decoder.instance(c => {
c.get[OpTypes.ResponseOpType]("op").flatMap {
case OpTypes.Connection => c.as[ConnectionMessage]
case OpTypes.Status => c.as[StatusMessage]
case _ => c.as[UnableToParseStreamResponseMessage]
}
})
implicit val encodeResponseMessage: Encoder[ResponseMessage] = new Encoder[ResponseMessage] {
final def apply(a: ResponseMessage): Json = Json.obj(
//Im encoding ResponseMessage wich only have a subset of all the parameters I need???
)
}
val originalJson =
"""
|{"op":"connection",
| "id":1,
| "connectionId":"1"
| }
""".stripMargin
val original = ConnectionMessage(op ="connection", id = 1, connectionId = "1")
decode[ResponseMessage](originalJson) shouldBe original
originalJson shouldBe original.asJson.noSpaces
}

Related Links

SVG datum as a JSON object
In the Grunt.js docs, there is a “this.filessrc” option, is it possible to do “this.filesDest”?
elasticsearch search for elements with specified ID example
Strings maintaining leading and trailing quotes from JSON when using Jerkson
How to get json request in spring controller?
How to restrict results by price in Google products search API
Comparing two json data in extjs
How to replace gwt json with my own json implementation
passing array of json objects from php to android
Spring Web MVC JSON generation
save functionality on a webpage that will present clicked xy points as text?
JSON data error in ColdFusion 9
Camel Route/ActiveMQ unmarshaling JSON and sending to methods
Can't fetching records from nested JSON objects in Sencha Touch Application
In extjs how to make validate json
Add object to array in JSON

Categories

HOME
answer-set-programming
amazon-swf
sed
performancecounter
jasmine
puppet
json-ld
youtube-livestreaming-api
google-sheets-api
terrain
branch.io
google-plus
ipfs
data-analysis
gwtp
activesync
cosmicmind
equalizer
cross-platform
phpbb
ios10.3
rails-activerecord
url.action
apply
fop
jquery-form-validator
kitematic
facebook4j
delete-file
samsung-mobile
pipelinedb
kong
fetch-api
owl-api
configure
bitcoin-testnet
crt
percentage
shapes
modulo
mapguide
jquery-multidatespicker
android-preferences
exiftool
productivity
google-account
rkt
worker
double-buffering
range-v3
chunked-encoding
greenhills
butterknife
uicollectionviewlayout
communication-protocol
sqlexception
rxvt
pspice
libtiff.net
spring-ioc
pydio
ng2-material
void
test-class
schtasks.exe
vips
jericho-html-parser
registrykey
uialertview
abas
urn
jazz
musl
google-earth-plugin
pisa
proximity
cocoascript
left-recursion
tcpreplay
ibmsbt
unison
db4o
internal
bsod
sim900
goinstant
fraud-prevention
database-permissions
transitive-closure-table
autostart
mpmovieplayer
text-services-framework
law-of-demeter
charts4j
posting
collect
weborb

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App