1

Getting NoSuchMethodError while trying to generate JSON strings from a Scala Map using Lift-JSON . Was trying to execute the following program from scala cookbook

    package com.sample


import net.liftweb.json.JsonAST
import net.liftweb.json.JsonDSL._
import net.liftweb.json.Printer.{compact,pretty}

object LiftJsonWithCollections extends App {  
  val json = List(1, 2, 3) 
  println(compact(JsonAST.render(json)))  
  val map = Map("fname" -> "Alvin", "lname" -> "Alexander") 
  println(compact(JsonAST.render(map)))
}

The following error I am getting

Exception in thread "main" java.lang.NoSuchMethodError: scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object;
    at net.liftweb.json.Printer$class.layout$1(JsonAST.scala:597)
    at net.liftweb.json.Printer$class.compact(JsonAST.scala:605)
    at net.liftweb.json.Printer$.compact(JsonAST.scala:583)
    at net.liftweb.json.Printer$class.compact(JsonAST.scala:590)
    at net.liftweb.json.Printer$.compact(JsonAST.scala:583)
    at com.sample.LiftJsonWithCollections$.delayedEndpoint$com$sample$LiftJsonWithCollections$1(LiftJsonWIthCollection.scala:10)
    at com.sample.LiftJsonWithCollections$delayedInit$body.apply(LiftJsonWIthCollection.scala:8)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:392)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.App$class.main(App.scala:76)
    at com.sample.LiftJsonWithCollections$.main(LiftJsonWIthCollection.scala:8)
    at com.sample.LiftJsonWithCollections.main(LiftJsonWIthCollection.scala)

Can't resolve it.

I have added the following dependency in pom

<dependency>
            <groupId>net.liftweb</groupId>
            <artifactId>lift-json_2.11</artifactId>
            <version>3.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.json4s/json4s-jackson -->
        <dependency>
            <groupId>org.json4s</groupId>
            <artifactId>json4s-jackson_2.11</artifactId>
            <version>3.2.10</version>
        </dependency>

1 Answers1

0

When I removed the JsonAST method and write the following code it works ..

object LiftJsonWithCollections extends App { 
  implicit val formats = DefaultFormats
  val json = List(1, 2, 3) 
  **println(compact(render(json)))**  
  val map = Map("fname" -> "Alvin", "lname" -> "Alexander") 
  **println(compact(render(map)))**
}

However I can find a render method in JsonAST API also. How does it work ?