Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6605151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:14:59+00:00 2026-05-25T19:14:59+00:00

I am trying to automatically deserialize json object to a scala class using Lift-Json

  • 0

I am trying to automatically deserialize json object to a scala class using Lift-Json with a coordinate class inside used to store GeoJson information.

case class Request(name:String, geometry:Geometry)

sealed abstract class Geometry

case class Point(coordinates:(Double,Double)) extends Geometry
case class LineString(coordinates:List[Point]) extends Geometry
case class Polygon(coordinates:List[LineString]) extends Geometry

I want to deserialize a json string like this:

{
name:"test",
geometry:{
   "type": "LineString",
   "coordinates": [ [100.0, 0.0], [101.0, 1.0] ]
  }
}

into a Request case class with the right LineString runtime class in the Geometry field. I guess I should use a TypeHint but how?. Is this the correct approach or should I create three different Request (RequestPoint,RequestLineString and RequestPolygon)?
This would be the Scala code to deserialize:

val json = parse(message)
json.extract[Request]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:15:00+00:00Added an answer on May 25, 2026 at 7:15 pm

    Yes, you need to use type hints for sum types like Geometry. Here’s one example:

    implicit val formats = DefaultFormats.withHints(ShortTypeHints(List(classOf[Point], classOf[LineString], classOf[Polygon])))
    
    val r = Request("test", LineString(List(Point(100.0, 0.0), Point(101.0, 1.0))))
    
    Serialization.write(r)
    
    {
    "name":"test",
    "geometry":{
      "jsonClass":"LineString",
      "coordinates":[{"jsonClass":"Point","coordinates":{"_1$mcD$sp":100.0,"_2$mcD$sp":0.0}},{"jsonClass":"Point","coordinates":{"_1$mcD$sp":101.0,"_2$mcD$sp":1.0}}]}
    }
    

    Not quite what you wanted. Since you want to change the default serialization scheme for Points, you need to provide a custom serializer for that type.

    class PointSerializer extends Serializer[Point] {
      private val Class = classOf[Point]
    
      def deserialize(implicit format: Formats) = {
        case (TypeInfo(Class, _), json) => json match {
          case JArray(JDouble(x) :: JDouble(y) :: Nil) => Point(x, y)
          case x => throw new MappingException("Can't convert " + x + " to Point")
        }
      }
    
      def serialize(implicit format: Formats) = {
        case p: Point => JArray(JDouble(p.coordinates._1) :: JDouble(p.coordinates._2) :: Nil)
      }
    }
    
    // Configure it
    implicit val formats = DefaultFormats.withHints(ShortTypeHints(List(classOf[Point], classOf[LineString], classOf[Polygon]))) + new PointSerializer
    
    Serialization.write(r)
    
    {
      "name":"test",
      "geometry":{
        "jsonClass":"LineString",
        "coordinates":[[100.0,0.0],[101.0,1.0]]
      }
    }
    

    Better, but you need one more configuration if you need to change the default field named as ‘jsonClass’ to ‘type’:

    implicit val formats = new DefaultFormats { 
      override val typeHintFieldName = "type" 
      override val typeHints = ShortTypeHints(List(classOf[Point], classOf[LineString], classOf[Polygon]))
    } + new PointSerializer
    
    Serialization.write(r)
    
    {
      "name":"test",
      "geometry":{
        "type":"LineString",
        "coordinates":[[100.0,0.0],[101.0,1.0]]
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to automatically compress both CSS and JS using maven and this plugin
I'm trying to login automatically in a website using Perl with WWW::Mechanize . What
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I'm trying to automatically generate a line chart using VBA and Excel where each
I am trying to replace some div text automatically using the .html command in
Using jQuery, I am trying to automatically replace every... www.mywebsite.com/blog/category/categoryname by www.mywebsite.com/blog/#categoryname ...in my
Im trying to automatically run a script using Cygwin via CMD. I basically created
I am trying to automatically convert object's properties to DataTable (object is array and
I am trying to automatically clone an object without having to instantiate a new
I am trying to make a program that automatically corrects the perspective of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.