Play2 Scala: Deserialize Json into a List of objects Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!Safely turning a JSON string into an objectHow do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How do I test for an empty JavaScript object?Why does Google prepend while(1); to their JSON responses?Deserialize JSON into C# dynamic object?Parse JSON in JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?
Why are there no cargo aircraft with "flying wing" design?
What is the role of the transistor and diode in a soft start circuit?
How to tell that you are a giant?
How does the particle を relate to the verb 行く in the structure「A を + B に行く」?
If a contract sometimes uses the wrong name, is it still valid?
Ring Automorphisms that fix 1.
Should I use a zero-interest credit card for a large one-time purchase?
English words in a non-english sci-fi novel
2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?
What does this icon in iOS Stardew Valley mean?
Can a USB port passively 'listen only'?
Using audio cues to encourage good posture
What causes the vertical darker bands in my photo?
Do I really need recursive chmod to restrict access to a folder?
Generate an RGB colour grid
Dating a Former Employee
Seeking colloquialism for “just because”
Is the Standard Deduction better than Itemized when both are the same amount?
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
Output the ŋarâþ crîþ alphabet song without using (m)any letters
Why am I getting the error "non-boolean type specified in a context where a condition is expected" for this request?
How to find out what spells would be useless to a blind NPC spellcaster?
At the end of Thor: Ragnarok why don't the Asgardians turn and head for the Bifrost as per their original plan?
What would be the ideal power source for a cybernetic eye?
Play2 Scala: Deserialize Json into a List of objects
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!Safely turning a JSON string into an objectHow do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How do I test for an empty JavaScript object?Why does Google prepend while(1); to their JSON responses?Deserialize JSON into C# dynamic object?Parse JSON in JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to deserialize a json body response that I get using Play's WSClient
into a list of objects and I think I'm not too close from succeeding, but there's probably the final piece of the puzzle that is missing me.
These are the case classes (with their companion objects) that the incoming json must be deserialized to.
EmployerStoreDTO:
import play.api.libs.json._
case class EmployerStoreDTO (id: String, storeName: String)
object EmployerStoreDTO
implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]
implicit val employerStoreDTOlistReads: Reads[List[EmployerStoreDTO]] = Reads.list[EmployerStoreDTO]
CompanyEmployerDTO:
import java.time.DayOfWeek
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
case class CompanyEmployerDTO(id: String,
companyName: String,
companyUrl: Option[String],
description: Option[String],
startDayOfWeek: DayOfWeek,
logoUrl: Option[String],
stores: List[EmployerStoreDTO] = List(),
supportHelpText: Option[String],
themeId: Option[String])
object CompanyEmployerDTO
import model.EmployerStoreDTO._
implicit val startDayOfWeekReads: Reads[DayOfWeek] = (JsPath "weekStartDay").read[Int].map(DayOfWeek.of)
implicit val reads: Reads[CompanyEmployerDTO] = (
(JsPath "id").read[String] and
(JsPath "companyName").read[String] and
(JsPath "companyUrl").readNullable[String] and
(JsPath "description").readNullable[String] and startDayOfWeekReads and
(JsPath "logoUrl").readNullable[String] and employerStoreDTOlistReads and
(JsPath "supportHelpText").readNullable[String] and
(JsPath "themeId").readNullable[String]
) (CompanyEmployerDTO.apply _)
implicit val companyEmployerDTOListReads: Reads[List[CompanyEmployerDTO]] = Reads.list[CompanyEmployerDTO]
CompanyEmployerCollectionDTO:
import play.api.libs.json.Reads._
import play.api.libs.json._
case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])
object CompanyEmployerCollectionDTO
implicit val reads: Reads[CompanyEmployerCollectionDTO] =
(JsPath "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)
I see that the payload is received by my WsClient
:
But as I'm trying to deserialize the response as follows: response.json.as[CompanyEmployerCollectionDTO]
I get this error:
Caused by: play.api.libs.json.JsResultException: JsResultException(errors:List((/companies(0),List(JsonValidationError(List(error.expected.jsarray),WrappedArray())))))
at play.api.libs.json.JsReadable.$anonfun$as$2(JsReadable.scala:25)
at play.api.libs.json.JsError.fold(JsResult.scala:64)
at play.api.libs.json.JsReadable.as(JsReadable.scala:24)
at play.api.libs.json.JsReadable.as$(JsReadable.scala:23)
at play.api.libs.json.JsObject.as(JsValue.scala:124)
at services.com.mycompany.ApiEmployeeClient.$anonfun$callGetEmployers$2(ApiEmployeeClient.scala:33)
at scala.util.Success.$anonfun$map$1(Try.scala:255)
at scala.util.Success.map(Try.scala:213)
at scala.concurrent.Future.$anonfun$map$1(Future.scala:292)
at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33)
json scala playframework deserialization
add a comment |
I am trying to deserialize a json body response that I get using Play's WSClient
into a list of objects and I think I'm not too close from succeeding, but there's probably the final piece of the puzzle that is missing me.
These are the case classes (with their companion objects) that the incoming json must be deserialized to.
EmployerStoreDTO:
import play.api.libs.json._
case class EmployerStoreDTO (id: String, storeName: String)
object EmployerStoreDTO
implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]
implicit val employerStoreDTOlistReads: Reads[List[EmployerStoreDTO]] = Reads.list[EmployerStoreDTO]
CompanyEmployerDTO:
import java.time.DayOfWeek
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
case class CompanyEmployerDTO(id: String,
companyName: String,
companyUrl: Option[String],
description: Option[String],
startDayOfWeek: DayOfWeek,
logoUrl: Option[String],
stores: List[EmployerStoreDTO] = List(),
supportHelpText: Option[String],
themeId: Option[String])
object CompanyEmployerDTO
import model.EmployerStoreDTO._
implicit val startDayOfWeekReads: Reads[DayOfWeek] = (JsPath "weekStartDay").read[Int].map(DayOfWeek.of)
implicit val reads: Reads[CompanyEmployerDTO] = (
(JsPath "id").read[String] and
(JsPath "companyName").read[String] and
(JsPath "companyUrl").readNullable[String] and
(JsPath "description").readNullable[String] and startDayOfWeekReads and
(JsPath "logoUrl").readNullable[String] and employerStoreDTOlistReads and
(JsPath "supportHelpText").readNullable[String] and
(JsPath "themeId").readNullable[String]
) (CompanyEmployerDTO.apply _)
implicit val companyEmployerDTOListReads: Reads[List[CompanyEmployerDTO]] = Reads.list[CompanyEmployerDTO]
CompanyEmployerCollectionDTO:
import play.api.libs.json.Reads._
import play.api.libs.json._
case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])
object CompanyEmployerCollectionDTO
implicit val reads: Reads[CompanyEmployerCollectionDTO] =
(JsPath "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)
I see that the payload is received by my WsClient
:
But as I'm trying to deserialize the response as follows: response.json.as[CompanyEmployerCollectionDTO]
I get this error:
Caused by: play.api.libs.json.JsResultException: JsResultException(errors:List((/companies(0),List(JsonValidationError(List(error.expected.jsarray),WrappedArray())))))
at play.api.libs.json.JsReadable.$anonfun$as$2(JsReadable.scala:25)
at play.api.libs.json.JsError.fold(JsResult.scala:64)
at play.api.libs.json.JsReadable.as(JsReadable.scala:24)
at play.api.libs.json.JsReadable.as$(JsReadable.scala:23)
at play.api.libs.json.JsObject.as(JsValue.scala:124)
at services.com.mycompany.ApiEmployeeClient.$anonfun$callGetEmployers$2(ApiEmployeeClient.scala:33)
at scala.util.Success.$anonfun$map$1(Try.scala:255)
at scala.util.Success.map(Try.scala:213)
at scala.concurrent.Future.$anonfun$map$1(Future.scala:292)
at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33)
json scala playframework deserialization
add a comment |
I am trying to deserialize a json body response that I get using Play's WSClient
into a list of objects and I think I'm not too close from succeeding, but there's probably the final piece of the puzzle that is missing me.
These are the case classes (with their companion objects) that the incoming json must be deserialized to.
EmployerStoreDTO:
import play.api.libs.json._
case class EmployerStoreDTO (id: String, storeName: String)
object EmployerStoreDTO
implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]
implicit val employerStoreDTOlistReads: Reads[List[EmployerStoreDTO]] = Reads.list[EmployerStoreDTO]
CompanyEmployerDTO:
import java.time.DayOfWeek
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
case class CompanyEmployerDTO(id: String,
companyName: String,
companyUrl: Option[String],
description: Option[String],
startDayOfWeek: DayOfWeek,
logoUrl: Option[String],
stores: List[EmployerStoreDTO] = List(),
supportHelpText: Option[String],
themeId: Option[String])
object CompanyEmployerDTO
import model.EmployerStoreDTO._
implicit val startDayOfWeekReads: Reads[DayOfWeek] = (JsPath "weekStartDay").read[Int].map(DayOfWeek.of)
implicit val reads: Reads[CompanyEmployerDTO] = (
(JsPath "id").read[String] and
(JsPath "companyName").read[String] and
(JsPath "companyUrl").readNullable[String] and
(JsPath "description").readNullable[String] and startDayOfWeekReads and
(JsPath "logoUrl").readNullable[String] and employerStoreDTOlistReads and
(JsPath "supportHelpText").readNullable[String] and
(JsPath "themeId").readNullable[String]
) (CompanyEmployerDTO.apply _)
implicit val companyEmployerDTOListReads: Reads[List[CompanyEmployerDTO]] = Reads.list[CompanyEmployerDTO]
CompanyEmployerCollectionDTO:
import play.api.libs.json.Reads._
import play.api.libs.json._
case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])
object CompanyEmployerCollectionDTO
implicit val reads: Reads[CompanyEmployerCollectionDTO] =
(JsPath "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)
I see that the payload is received by my WsClient
:
But as I'm trying to deserialize the response as follows: response.json.as[CompanyEmployerCollectionDTO]
I get this error:
Caused by: play.api.libs.json.JsResultException: JsResultException(errors:List((/companies(0),List(JsonValidationError(List(error.expected.jsarray),WrappedArray())))))
at play.api.libs.json.JsReadable.$anonfun$as$2(JsReadable.scala:25)
at play.api.libs.json.JsError.fold(JsResult.scala:64)
at play.api.libs.json.JsReadable.as(JsReadable.scala:24)
at play.api.libs.json.JsReadable.as$(JsReadable.scala:23)
at play.api.libs.json.JsObject.as(JsValue.scala:124)
at services.com.mycompany.ApiEmployeeClient.$anonfun$callGetEmployers$2(ApiEmployeeClient.scala:33)
at scala.util.Success.$anonfun$map$1(Try.scala:255)
at scala.util.Success.map(Try.scala:213)
at scala.concurrent.Future.$anonfun$map$1(Future.scala:292)
at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33)
json scala playframework deserialization
I am trying to deserialize a json body response that I get using Play's WSClient
into a list of objects and I think I'm not too close from succeeding, but there's probably the final piece of the puzzle that is missing me.
These are the case classes (with their companion objects) that the incoming json must be deserialized to.
EmployerStoreDTO:
import play.api.libs.json._
case class EmployerStoreDTO (id: String, storeName: String)
object EmployerStoreDTO
implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]
implicit val employerStoreDTOlistReads: Reads[List[EmployerStoreDTO]] = Reads.list[EmployerStoreDTO]
CompanyEmployerDTO:
import java.time.DayOfWeek
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
case class CompanyEmployerDTO(id: String,
companyName: String,
companyUrl: Option[String],
description: Option[String],
startDayOfWeek: DayOfWeek,
logoUrl: Option[String],
stores: List[EmployerStoreDTO] = List(),
supportHelpText: Option[String],
themeId: Option[String])
object CompanyEmployerDTO
import model.EmployerStoreDTO._
implicit val startDayOfWeekReads: Reads[DayOfWeek] = (JsPath "weekStartDay").read[Int].map(DayOfWeek.of)
implicit val reads: Reads[CompanyEmployerDTO] = (
(JsPath "id").read[String] and
(JsPath "companyName").read[String] and
(JsPath "companyUrl").readNullable[String] and
(JsPath "description").readNullable[String] and startDayOfWeekReads and
(JsPath "logoUrl").readNullable[String] and employerStoreDTOlistReads and
(JsPath "supportHelpText").readNullable[String] and
(JsPath "themeId").readNullable[String]
) (CompanyEmployerDTO.apply _)
implicit val companyEmployerDTOListReads: Reads[List[CompanyEmployerDTO]] = Reads.list[CompanyEmployerDTO]
CompanyEmployerCollectionDTO:
import play.api.libs.json.Reads._
import play.api.libs.json._
case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])
object CompanyEmployerCollectionDTO
implicit val reads: Reads[CompanyEmployerCollectionDTO] =
(JsPath "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)
I see that the payload is received by my WsClient
:
But as I'm trying to deserialize the response as follows: response.json.as[CompanyEmployerCollectionDTO]
I get this error:
Caused by: play.api.libs.json.JsResultException: JsResultException(errors:List((/companies(0),List(JsonValidationError(List(error.expected.jsarray),WrappedArray())))))
at play.api.libs.json.JsReadable.$anonfun$as$2(JsReadable.scala:25)
at play.api.libs.json.JsError.fold(JsResult.scala:64)
at play.api.libs.json.JsReadable.as(JsReadable.scala:24)
at play.api.libs.json.JsReadable.as$(JsReadable.scala:23)
at play.api.libs.json.JsObject.as(JsValue.scala:124)
at services.com.mycompany.ApiEmployeeClient.$anonfun$callGetEmployers$2(ApiEmployeeClient.scala:33)
at scala.util.Success.$anonfun$map$1(Try.scala:255)
at scala.util.Success.map(Try.scala:213)
at scala.concurrent.Future.$anonfun$map$1(Future.scala:292)
at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:33)
json scala playframework deserialization
json scala playframework deserialization
asked Mar 8 at 16:57
vasigorcvasigorc
167212
167212
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The problem is the bare and employerStoreDTOlistReads
. Because you haven't specified a path (as for the other CompanyEmployerDTO
members), the decoder will try to decode the current JSON value itself (an object representing the CompanyEmployerDTO
) as a list of EmployerStoreDTO
.
Replacing those two words with and (JsPath "stores").read[List[EmployerStoreDTO]]
should fix your code, but for what it's worth you can also simplify your implementation quite a bit by scrapping the List
instances (which will be provided automatically), dropping an import, etc.:
import play.api.libs.json._
case class EmployerStoreDTO (id: String, storeName: String)
object EmployerStoreDTO
implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]
import java.time.DayOfWeek
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
case class CompanyEmployerDTO(id: String,
companyName: String,
companyUrl: Option[String],
description: Option[String],
startDayOfWeek: DayOfWeek,
logoUrl: Option[String],
stores: List[EmployerStoreDTO] = List(),
supportHelpText: Option[String],
themeId: Option[String])
object CompanyEmployerDTO
implicit val reads: Reads[CompanyEmployerDTO] = (
(JsPath "id").read[String] and
(JsPath "companyName").read[String] and
(JsPath "companyUrl").readNullable[String] and
(JsPath "description").readNullable[String] and
(JsPath "weekStartDay").read[Int].map(DayOfWeek.of) and
(JsPath "logoUrl").readNullable[String] and
(JsPath "stores").read[List[EmployerStoreDTO]] and
(JsPath "supportHelpText").readNullable[String] and
(JsPath "themeId").readNullable[String]
) (CompanyEmployerDTO.apply _)
import play.api.libs.json.Reads._
import play.api.libs.json._
case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])
object CompanyEmployerCollectionDTO
implicit val reads: Reads[CompanyEmployerCollectionDTO] =
(JsPath "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)
This will do exactly the same thing as your code, but is a little more concise and manageable.
You sir made my day! Thank you for the solution and for helping me getting read of some clutter!
– vasigorc
Mar 8 at 17:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067695%2fplay2-scala-deserialize-json-into-a-list-of-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is the bare and employerStoreDTOlistReads
. Because you haven't specified a path (as for the other CompanyEmployerDTO
members), the decoder will try to decode the current JSON value itself (an object representing the CompanyEmployerDTO
) as a list of EmployerStoreDTO
.
Replacing those two words with and (JsPath "stores").read[List[EmployerStoreDTO]]
should fix your code, but for what it's worth you can also simplify your implementation quite a bit by scrapping the List
instances (which will be provided automatically), dropping an import, etc.:
import play.api.libs.json._
case class EmployerStoreDTO (id: String, storeName: String)
object EmployerStoreDTO
implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]
import java.time.DayOfWeek
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
case class CompanyEmployerDTO(id: String,
companyName: String,
companyUrl: Option[String],
description: Option[String],
startDayOfWeek: DayOfWeek,
logoUrl: Option[String],
stores: List[EmployerStoreDTO] = List(),
supportHelpText: Option[String],
themeId: Option[String])
object CompanyEmployerDTO
implicit val reads: Reads[CompanyEmployerDTO] = (
(JsPath "id").read[String] and
(JsPath "companyName").read[String] and
(JsPath "companyUrl").readNullable[String] and
(JsPath "description").readNullable[String] and
(JsPath "weekStartDay").read[Int].map(DayOfWeek.of) and
(JsPath "logoUrl").readNullable[String] and
(JsPath "stores").read[List[EmployerStoreDTO]] and
(JsPath "supportHelpText").readNullable[String] and
(JsPath "themeId").readNullable[String]
) (CompanyEmployerDTO.apply _)
import play.api.libs.json.Reads._
import play.api.libs.json._
case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])
object CompanyEmployerCollectionDTO
implicit val reads: Reads[CompanyEmployerCollectionDTO] =
(JsPath "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)
This will do exactly the same thing as your code, but is a little more concise and manageable.
You sir made my day! Thank you for the solution and for helping me getting read of some clutter!
– vasigorc
Mar 8 at 17:53
add a comment |
The problem is the bare and employerStoreDTOlistReads
. Because you haven't specified a path (as for the other CompanyEmployerDTO
members), the decoder will try to decode the current JSON value itself (an object representing the CompanyEmployerDTO
) as a list of EmployerStoreDTO
.
Replacing those two words with and (JsPath "stores").read[List[EmployerStoreDTO]]
should fix your code, but for what it's worth you can also simplify your implementation quite a bit by scrapping the List
instances (which will be provided automatically), dropping an import, etc.:
import play.api.libs.json._
case class EmployerStoreDTO (id: String, storeName: String)
object EmployerStoreDTO
implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]
import java.time.DayOfWeek
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
case class CompanyEmployerDTO(id: String,
companyName: String,
companyUrl: Option[String],
description: Option[String],
startDayOfWeek: DayOfWeek,
logoUrl: Option[String],
stores: List[EmployerStoreDTO] = List(),
supportHelpText: Option[String],
themeId: Option[String])
object CompanyEmployerDTO
implicit val reads: Reads[CompanyEmployerDTO] = (
(JsPath "id").read[String] and
(JsPath "companyName").read[String] and
(JsPath "companyUrl").readNullable[String] and
(JsPath "description").readNullable[String] and
(JsPath "weekStartDay").read[Int].map(DayOfWeek.of) and
(JsPath "logoUrl").readNullable[String] and
(JsPath "stores").read[List[EmployerStoreDTO]] and
(JsPath "supportHelpText").readNullable[String] and
(JsPath "themeId").readNullable[String]
) (CompanyEmployerDTO.apply _)
import play.api.libs.json.Reads._
import play.api.libs.json._
case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])
object CompanyEmployerCollectionDTO
implicit val reads: Reads[CompanyEmployerCollectionDTO] =
(JsPath "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)
This will do exactly the same thing as your code, but is a little more concise and manageable.
You sir made my day! Thank you for the solution and for helping me getting read of some clutter!
– vasigorc
Mar 8 at 17:53
add a comment |
The problem is the bare and employerStoreDTOlistReads
. Because you haven't specified a path (as for the other CompanyEmployerDTO
members), the decoder will try to decode the current JSON value itself (an object representing the CompanyEmployerDTO
) as a list of EmployerStoreDTO
.
Replacing those two words with and (JsPath "stores").read[List[EmployerStoreDTO]]
should fix your code, but for what it's worth you can also simplify your implementation quite a bit by scrapping the List
instances (which will be provided automatically), dropping an import, etc.:
import play.api.libs.json._
case class EmployerStoreDTO (id: String, storeName: String)
object EmployerStoreDTO
implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]
import java.time.DayOfWeek
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
case class CompanyEmployerDTO(id: String,
companyName: String,
companyUrl: Option[String],
description: Option[String],
startDayOfWeek: DayOfWeek,
logoUrl: Option[String],
stores: List[EmployerStoreDTO] = List(),
supportHelpText: Option[String],
themeId: Option[String])
object CompanyEmployerDTO
implicit val reads: Reads[CompanyEmployerDTO] = (
(JsPath "id").read[String] and
(JsPath "companyName").read[String] and
(JsPath "companyUrl").readNullable[String] and
(JsPath "description").readNullable[String] and
(JsPath "weekStartDay").read[Int].map(DayOfWeek.of) and
(JsPath "logoUrl").readNullable[String] and
(JsPath "stores").read[List[EmployerStoreDTO]] and
(JsPath "supportHelpText").readNullable[String] and
(JsPath "themeId").readNullable[String]
) (CompanyEmployerDTO.apply _)
import play.api.libs.json.Reads._
import play.api.libs.json._
case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])
object CompanyEmployerCollectionDTO
implicit val reads: Reads[CompanyEmployerCollectionDTO] =
(JsPath "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)
This will do exactly the same thing as your code, but is a little more concise and manageable.
The problem is the bare and employerStoreDTOlistReads
. Because you haven't specified a path (as for the other CompanyEmployerDTO
members), the decoder will try to decode the current JSON value itself (an object representing the CompanyEmployerDTO
) as a list of EmployerStoreDTO
.
Replacing those two words with and (JsPath "stores").read[List[EmployerStoreDTO]]
should fix your code, but for what it's worth you can also simplify your implementation quite a bit by scrapping the List
instances (which will be provided automatically), dropping an import, etc.:
import play.api.libs.json._
case class EmployerStoreDTO (id: String, storeName: String)
object EmployerStoreDTO
implicit val reads: Reads[EmployerStoreDTO] = Json.reads[EmployerStoreDTO]
import java.time.DayOfWeek
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
case class CompanyEmployerDTO(id: String,
companyName: String,
companyUrl: Option[String],
description: Option[String],
startDayOfWeek: DayOfWeek,
logoUrl: Option[String],
stores: List[EmployerStoreDTO] = List(),
supportHelpText: Option[String],
themeId: Option[String])
object CompanyEmployerDTO
implicit val reads: Reads[CompanyEmployerDTO] = (
(JsPath "id").read[String] and
(JsPath "companyName").read[String] and
(JsPath "companyUrl").readNullable[String] and
(JsPath "description").readNullable[String] and
(JsPath "weekStartDay").read[Int].map(DayOfWeek.of) and
(JsPath "logoUrl").readNullable[String] and
(JsPath "stores").read[List[EmployerStoreDTO]] and
(JsPath "supportHelpText").readNullable[String] and
(JsPath "themeId").readNullable[String]
) (CompanyEmployerDTO.apply _)
import play.api.libs.json.Reads._
import play.api.libs.json._
case class CompanyEmployerCollectionDTO (companies: List[CompanyEmployerDTO])
object CompanyEmployerCollectionDTO
implicit val reads: Reads[CompanyEmployerCollectionDTO] =
(JsPath "companies").read[List[CompanyEmployerDTO]].map(CompanyEmployerCollectionDTO.apply)
This will do exactly the same thing as your code, but is a little more concise and manageable.
answered Mar 8 at 17:39
Travis BrownTravis Brown
118k9296570
118k9296570
You sir made my day! Thank you for the solution and for helping me getting read of some clutter!
– vasigorc
Mar 8 at 17:53
add a comment |
You sir made my day! Thank you for the solution and for helping me getting read of some clutter!
– vasigorc
Mar 8 at 17:53
You sir made my day! Thank you for the solution and for helping me getting read of some clutter!
– vasigorc
Mar 8 at 17:53
You sir made my day! Thank you for the solution and for helping me getting read of some clutter!
– vasigorc
Mar 8 at 17:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067695%2fplay2-scala-deserialize-json-into-a-list-of-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown