0

Scala 2.12 here. I'm trying to use Lift-JSON to deserialize some JSON into a Scala object and am having trouble navigating the Lift API. Please note: I'm not married to Lift-JSON, any other working solution will be accepted so long as I don't have to bring any heavy/core Play dependencies into my project.


Here's the JSON file I'm trying to read:

{
  "fizz" : "buzz",
  "foo" : [
    "123",
    "456",
    "789"
  ],
  "bar" : {
    "whistle" : 1,
    "feather" : true
  }
}

Here's my Scala object hierarchy:

case class Bar(whistle : Integer, feather : Boolean)
case class MyConfig(fizz : String, foo : Array[String], bar : Bar)

And finally my best attempt at the codeup for this:

def loadConfig(configFilePath : String) : MyConfig = {
  val configJson = Source.fromFile(configFilePath)

  val parsedJson = parse(configJson.mkString)

  MyConfig(???)
}

I need validation in place so that if the JSON is not valid an exception is thrown. Any ideas how I can extract fields out of parsedJson and use them to set values for my MyConfig instance? And how to perform the validation?

1 Answer 1

1

Have you tried parsedJson.extract[MyConfig]? That is straight out of the Extracting values documentation. If you haven't already, you will need to specify an implicit reference to the default formats:

implicit val formats = DefaultFormats
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Karl (+1) but adding that yields a compiler error: /Users/myuser/workspace/myapp/src/main/scala/com/myapp/MyConfigLoader.scala:13: could not find implicit value for parameter formats: net.liftweb.json.Formats

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.