score:8
this should do it, with the caveat that it doesn't handle numberformatexception
in the reads
case:
//
// scala> json.tojson(map(1 -> 2l, 2 -> 3l))
// res0: play.api.libs.json.jsvalue = {"1":2,"2":3}
//
implicit val formatter: format[map[int, long]] = {
new format[map[int, long]] {
def writes(m: map[int, long]) = {
json.tojson(m.map {
case (key, value) => key.tostring -> value
})
}
def reads(json: jsvalue) = {
json.validate[map[string, long]].map(_.map {
case (key, value) => key.toint -> value
})
}
}
}
as separate reads
and writes
instances:
implicit val readsinstance: reads[map[int, long]] = {
new reads[map[int, long]] {
def reads(json: jsvalue) = {
json.validate[map[string, long]].map(_.map {
case (key, value) => key.toint -> value
})
}
}
}
implicit val writesinstance: writes[map[int, long]] = {
def writes(m: map[int, long]) = {
json.tojson(m.map {
case (key, value) => key.tostring -> value
})
}
}
}
Source: stackoverflow.com
Related Query
- How to write a Json Reads Combinator for a Map[Int, Long]
- How to write JSON Reads for models without a matching constructor signature?
- How do I write a JSON Format for an object in the Java library that doesn't have an apply method?
- How to write a Play JSON writes converter for a case class with a single nullable member
- In Scala, how to write an efficient json formatter for Map[IndexedSeq[String], Int]?
- JSON Reads combinator in Play scala 2.x doesn't work for Map[String, _]
- How to write a symmetric Play Json formatter for a case class with one field in scala?
- How to write less code for a long if-else statement in Scala (code review)
- How to write unit test for Play Framework JSON readers
- How write Reads function for Scala Play
- How to write a play-json Reads for Map[(Int, Int), A]?
- How to write Unit Test for spark app reading from json file
- How do I write a generic JSON parser in Play 2.7 for Scala that validates inbound requests?
- How to encode/decode Timestamp for json in circe?
- How to write copy() method for Simple Class in Scala
- Play Scala No Json deserializer found for type (String, String). Try to implement an implicit Reads or Format for this type
- How to write JDBC Sink for Spark Structured Streaming [SparkException: Task not serializable]?
- How to Use Circe for Decoding JSON Lists/Arrays in Scala
- How to validate for nullable types using json schema validator?
- Play2 does not find my implicit Reads or Format for JSON
- Play 2.2 JSON Reads with combinators: how to deal with nested optional objects?
- How should I write unit tests in Spark, for a basic data frame creation example?
- How to write dataframe as json array to a file? (scala)
- How to write for comprehension in cats with IO monad
- How do I exclude values when doing json Reads and Writes in my Play2 scala application
- How to write parser for unified diff syntax
- How to write a while() loop for an Iterable<Object> in Scala?
- How to parse json list or array in scala for play framework 2.2
- how to add custom ValidationError in Json Reads in PlayFramework
- How to write lambda for java.UnaryOperator<T> interface in Scala?
More Query from same tag
- scala foreach with one augment, cannot take any operation?
- How can I catch an interrupted java process in Scala
- Scala type of statements?
- Partially filter on a map to get key : Scala
- Is this a good return-type pattern for designing Scala API's?
- No execution context even when I have import ExecutionContex.global
- Datanucleus Enhancer not working with GAE + Scala + Eclipse
- What is the base collection class in scala?
- How to accumulate aggregation results into a collection using MongoDB Scala Driver
- Lift, Flot, & JQuery
- Override setter for `var` inherited from trait
- Reverse of Future.sequence
- Date-Time sorting in Spark using Dataframes
- The "lower bound" will reverse the variance of a type, but why?
- How to update nested JSON array in scala
- How to copy a file from local to s3 using spark as a single file with the given name?
- From Scala, access static inner class of Java class
- How can I combine 2 validators together in play2?
- How to create a JSON of a List of List of Any in Scala
- Getting subtype from nested type
- How case class inherent class(not abstract) method and value? (scala)
- Reducing code repetition with recurring code in if-else
- Aggregating case class by attribute and limit the group list by certain number
- Product of Array Except Self with tail recursion
- How to send requirement failed exception back to sender?
- Generate Random Hexidecimal in Scala?
- Access field from trait in companion object
- Do JVMs create significant overhead in distributed/ parallel processing?
- Why type any is returned?
- Paste code containing tabs into scala repl