1
votes

I have a Scala model

case class Model(
  name:          String,
  site:          String,
  phones:        Seq[String],
  duration:      String,
  pricePerMonth: Double,
  address:       String,
  benefits:      Seq[String],
  rating:        Int
)

for json

{
  "name": "a",
  "site": "b",
  "phones": ["c"],
  "duration": "d",
  "price-per-month": 10.00,
  "address": "e",
  "benefits": ["f"],
  "rating": 1
}

Here is my SBT dependencies:

"com.typesafe.play" %% "play-json" % "2.3.4"

I want to parse json using Play. And here is my code:

val model = fromInputStream { getClass getResourceAsStream "/model.json"
                            } getLines() toString

val json: JsValue = Json.parse(model)

But I get an error

Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'non': was expecting 'null', 'true', 'false' or NaN
 at [Source: java.io.StringReader@a7e666; line: 1, column: 4]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1524)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:557)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2042)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2018)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._matchToken(ReaderBasedJsonParser.java:1870)
    at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:657)
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3031)
    at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:2951)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1587)
    at play.api.libs.json.JacksonJson$.parseJsValue(JsValue.scala:486)
    at play.api.libs.json.Json$.parse(Json.scala:19)

Any ideas how can I fix this error?

1
What exactly is the value of model given to Json.parse. Use a debugger or simply println and I bet you quickly see what's wrong with your JSON and why Jackson blew up. - cbley
What's the actual content ofmodel.json? - The Archetypal Paul
I found the cause of the error. I need to use mkString instead of getLines().toString() to make actually a string but not an iterator. - Alex

1 Answers

0
votes

It's working for me. From the REPL:

Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import play.api.libs.json._
import play.api.libs.json._

scala> val json = """
     | {
     |   "name": "a",
     |   "site": "b",
     |   "phones": ["c"],
     |   "duration": "d",
     |   "price-per-month": 10.00,
     |   "address": "e",
     |   "benefits": ["f"],
     |   "rating": 1
     | }
     | """
json: String =
"
{
  "name": "a",
  "site": "b",
  "phones": ["c"],
  "duration": "d",
  "price-per-month": 10.00,
  "address": "e",
  "benefits": ["f"],
  "rating": 1
}
"

scala> Json.parse(json)
res0: play.api.libs.json.JsValue = {"name":"a","site":"b","phones":["c"],"duration":"d","price-per-month":10,"address":"e","benefits":["f"],"rating":1}

So probably the problem is from your method:

val model = fromInputStream { getClass getResourceAsStream "/model.json" } getLines() toString