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 9155633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:37:51+00:00 2026-06-17T12:37:51+00:00

Let’s assume that have a json object Test. It has two formats : {

  • 0

Let’s assume that have a json object Test.
It has two formats :

{
    "name": "Test",
    "id": "41"
}

and

{
    "object": {
        "name": "Test",
        "id": "41"
    }
}

I created Test.scala :

case class Test (name: String, id: String)

object Test {

    implicit object FopReads extends Format[Test] {

        def reads(json: JsValue): Test = {
            Test (
                (json \ "name").as[String],
                (json \ "id").as[String]
            )
        }

        def writes(ts: Test) = JsObject(Seq(
            "name" -> JsString(ts.name),
            "id" -> JsString(ts.id)
        ))

    }
}

As you can see, actually, I only deal with the first format. My question is : How do I easily implement a Format[Test] that it can read and write in both formats ? I know that the json mechanism have changed in play 2.10. Should I change for this version to do what I want ?

Thank you for your time !

  • 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-06-17T12:37:51+00:00Added an answer on June 17, 2026 at 12:37 pm

    In my humble opinion, your approach is not ideal and you have to rethink about your design. Let me explain you why.

    While you could effectively develop a Format[A] which is able to read both formats, you are not able to write a Format[A] which can write the two formats

    trait Format[T]{
        def reads (json: JsValue): T
        def writes (o: T): JsValue
    }
    

    As you see, the writes method has a single parameter, so your Format[Test] won’t be able to know if it should use one or the other output format. If you followed the reasoning you would agree that the Format trait is delegated to write and read from/to a specific Json Format, not to choose between formats. Your logic to choose the right format has nothing to do with reading and writing the right format, and you should decouple it.

    Saying that, you can re-use your Format[Test] in the following way:

    case class ObjectParser(nestedFormatter:Format[T]) extends Format[T]{
        def reads (json: JsValue): T = nestedFormatter.read(json \ "object")
        def writes (o: T): JsValue  = JsObject("object" -> nestedFormatter.write(o))
    }
    

    This class would take any existing formatter and make it able to read and write inside an additional “object” node.

    Imagine now you have a controller which needs to generate or parse two different answers according to the url endpoints:

    object TestController extends Controller {
    
        def endPoint1Action = Action{
            implicit request => 
            generateResponse(FopReads)
        }
    
        def endPoin21Action = Action{
            implicit request => 
            generateResponse(ObjectParser(FopReads))
        }
    
        def generateResponse(formatter:Format[Test])(implicit request:Request[AnyContent]):Response  = BadRequest("!")
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class library project that has any number of supplemental
Let's say I have two objects, Master and Slave . Slave has a method
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I have a Scala project with a bunch of case classes under
Let's say I have a posts table that has many comments (and a comment
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say for a moment that I have the following module in python: class
Let's say I have an actor like this (Using Akka 2.1 with Scala 2.10):
Let's say I have some text as follows: do this, do that, then this,

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.