I am using JSON4S in Scala to get the toString() of my POJO class in JSON format as follows.
//JSON related libraries
import org.json4s.{DefaultFormats, Formats}
//JSON handling support from Scalatra
import org.scalatra.json._
// JSON library for converting the POJO toString as Json
import org.json4s.native.Json
class Customer(val id:Int, var firstName:String, var lastName:String) {
println("Customer - Constructor BEGIN .... ")
def this(firstName:String, lastName:String) {
this(Customer.inc, firstName, lastName)
}
//override def toString = s"Customer [Id=$id, FirstName=$firstName, LastName=$lastName]"
override def toString = Json(DefaultFormats).write(this)
println("Customer - Constructor END .... ")
}
However when I test this with an instance, the order of elements is distorted as follows.
{
"firstName": "Raghavan",
"id": 1,
"lastName": "Muthu"
}
What I really want is
{
"id": 1,
"firstName": "Raghavan",
"lastName": "Muthu"
}
How can I achieve this?