How can I ignore empty objects with play-json?












0















I am new to Scala and functional programming so this question might have a very simple answer. However, I was not able to figure it out so here it is:



I have a case-class hierarchy that represents an abstract syntax tree in a compiler. At some point I have a list of statements of the form List[Statement]. This will get serialized with a Json array of objects where each object represents a statement. However, some kinds of statements are redundant so I don't want to serialize them. For example, returning void. The following code hopefully clarifies this:



sealed trait Statement

sealed trait Expression

case object Empty extends Expression

case class Return(e: Expression) extends Statement


What I want is to not serialize Return(Empty) at all. Right now I get something like:



"Return": {}


How can I simply ignore Return(Empty) ?
To make it more general. I don't want to have empty objects in my json like {}. How can I make sure they don't make it to my json ?



Here is what I tried so far (and didn't work):



implicit val statementListWrites = StatementListWrites

object StatementListWrites extends Writes[List[Statement]] {
override def writes(stms: List[Statement]) =
JsArray(stms.filter(stm => {
stm match {
case Return(Empty) => false
case _ => true
}
}).map(s => Json.toJson(s)))
}


Perhaps the above technique works but for some reason it is not even called to begin with as far as I understand.










share|improve this question



























    0















    I am new to Scala and functional programming so this question might have a very simple answer. However, I was not able to figure it out so here it is:



    I have a case-class hierarchy that represents an abstract syntax tree in a compiler. At some point I have a list of statements of the form List[Statement]. This will get serialized with a Json array of objects where each object represents a statement. However, some kinds of statements are redundant so I don't want to serialize them. For example, returning void. The following code hopefully clarifies this:



    sealed trait Statement

    sealed trait Expression

    case object Empty extends Expression

    case class Return(e: Expression) extends Statement


    What I want is to not serialize Return(Empty) at all. Right now I get something like:



    "Return": {}


    How can I simply ignore Return(Empty) ?
    To make it more general. I don't want to have empty objects in my json like {}. How can I make sure they don't make it to my json ?



    Here is what I tried so far (and didn't work):



    implicit val statementListWrites = StatementListWrites

    object StatementListWrites extends Writes[List[Statement]] {
    override def writes(stms: List[Statement]) =
    JsArray(stms.filter(stm => {
    stm match {
    case Return(Empty) => false
    case _ => true
    }
    }).map(s => Json.toJson(s)))
    }


    Perhaps the above technique works but for some reason it is not even called to begin with as far as I understand.










    share|improve this question

























      0












      0








      0








      I am new to Scala and functional programming so this question might have a very simple answer. However, I was not able to figure it out so here it is:



      I have a case-class hierarchy that represents an abstract syntax tree in a compiler. At some point I have a list of statements of the form List[Statement]. This will get serialized with a Json array of objects where each object represents a statement. However, some kinds of statements are redundant so I don't want to serialize them. For example, returning void. The following code hopefully clarifies this:



      sealed trait Statement

      sealed trait Expression

      case object Empty extends Expression

      case class Return(e: Expression) extends Statement


      What I want is to not serialize Return(Empty) at all. Right now I get something like:



      "Return": {}


      How can I simply ignore Return(Empty) ?
      To make it more general. I don't want to have empty objects in my json like {}. How can I make sure they don't make it to my json ?



      Here is what I tried so far (and didn't work):



      implicit val statementListWrites = StatementListWrites

      object StatementListWrites extends Writes[List[Statement]] {
      override def writes(stms: List[Statement]) =
      JsArray(stms.filter(stm => {
      stm match {
      case Return(Empty) => false
      case _ => true
      }
      }).map(s => Json.toJson(s)))
      }


      Perhaps the above technique works but for some reason it is not even called to begin with as far as I understand.










      share|improve this question














      I am new to Scala and functional programming so this question might have a very simple answer. However, I was not able to figure it out so here it is:



      I have a case-class hierarchy that represents an abstract syntax tree in a compiler. At some point I have a list of statements of the form List[Statement]. This will get serialized with a Json array of objects where each object represents a statement. However, some kinds of statements are redundant so I don't want to serialize them. For example, returning void. The following code hopefully clarifies this:



      sealed trait Statement

      sealed trait Expression

      case object Empty extends Expression

      case class Return(e: Expression) extends Statement


      What I want is to not serialize Return(Empty) at all. Right now I get something like:



      "Return": {}


      How can I simply ignore Return(Empty) ?
      To make it more general. I don't want to have empty objects in my json like {}. How can I make sure they don't make it to my json ?



      Here is what I tried so far (and didn't work):



      implicit val statementListWrites = StatementListWrites

      object StatementListWrites extends Writes[List[Statement]] {
      override def writes(stms: List[Statement]) =
      JsArray(stms.filter(stm => {
      stm match {
      case Return(Empty) => false
      case _ => true
      }
      }).map(s => Json.toJson(s)))
      }


      Perhaps the above technique works but for some reason it is not even called to begin with as far as I understand.







      scala serialization playframework case-class play-json






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 19:01









      Than21Than21

      76117




      76117
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You should be careful in such cases because you have sealed traits, so you should work in an organized way.



          Let's divide the problem into smaller problems, Let's assume that you just want to write your objects into json. then we can solve the problem of removing empty objects.



          I added more classes to clarify the idea:



          sealed trait Expression
          case object Empty extends Expression
          case class NonEmptyExpression(x: Int) extends Expression

          sealed trait Statement
          case class Return(e: Expression) extends Statement
          case class NoReturn(x: Int) extends Statement


          First Step (provide writer for Expresion):



          to do this you have to create writer for Empty and NonEmptyExpression, so we can do it this way:



          val emptyExpressionWrites = new Writes[Empty.type] {
          override def writes(e: Empty.type) = Json.obj()
          }

          val nonEmptyExpressionWrites = Json.writes[NonEmptyExpression]

          implicit val expressionWrites = new Writes[Expression] {
          override def writes(exp: Expression) = {
          exp match {
          case Empty => emptyExpressionWrites.writes(Empty)
          case nonEmptyExpression: NonEmptyExpression => nonEmptyExpressionWrites.writes(nonEmptyExpression)
          }
          }
          }


          Second Step (provide writer for Statement):



          You have to provide writer for Return and NoReturn. please note that play was able to know how to create writer for Return and it has Expression, because I defined expressionWriter as implicit. so it knows how to serialize Expression now.



          val returnWrites = Json.writes[Return]
          val noReturnWrites = Json.writes[NoReturn]

          val statementWrites = new Writes[Statement] {
          override def writes(s: Statement) = {
          s match {
          case r: Return => returnWrites.writes(r)
          case nr: NoReturn => noReturnWrites.writes(nr)
          }
          }
          }


          Let's test this to make sure it works fine:



          val statementWithNonEmpty = Return(NonEmptyExpression(100))
          println(s"statementWithNonEmpty Json: ${statementWrites.writes(statementWithNonEmpty).toString()}")

          val statementWithEmpty = Return(Empty)
          println(s"statementWithEmpty Json: ${statementWrites.writes(statementWithEmpty).toString()}")


          Output is:




          statementWithNonEmpty Json: {"e":{"x":100}}



          statementWithEmpty Json: {"e":{}}




          Final Step (create List writer with no Empty):



          val listStatementWrites = new Writes[Seq[Statement]] {
          override def writes(o: Seq[Statement]) = {
          val listWithoutEmpty = o.filter {
          case Return(Empty) => false
          case _ => true
          }

          JsArray(listWithoutEmpty.map(statementWrites.writes))
          }
          }


          let's try it:



          val listOfStatements = List(Return(NonEmptyExpression(100)), Return(Empty), Return(NonEmptyExpression(200)))
          println(s"listOfStatements: ${listStatementWrites.writes(listOfStatements).toString()}")


          the output is:




          listOfStatements: [{"e":{"x":100}},{"e":{"x":200}}]







          share|improve this answer
























          • Thank you for your detailed response. However, if you read my question carefully you would notice that I tried something that is very close to what you suggest at the end, right ? So I am not sure why you had to go over all the other stuff :). May I ask if you could elaborate on why your approach works and mine doesn't ? I am curious about why you had to use Seq and not List and also why you passed statementWrites.writes and not Json.toJson. Isn't the latter going to invoke the former internally ?

            – Than21
            Nov 23 '18 at 16:18











          • What you tried is not close to what I answered. I was too verbose because such cases are not trivial, and a short answer may not be enough.

            – Mahmoud Hanafy
            Nov 23 '18 at 16:23






          • 1





            If you see Json.toJson needs a Writes[Statement] as an implicit parameter and you don't have it in your code.

            – Mahmoud Hanafy
            Nov 23 '18 at 16:25











          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418925%2fhow-can-i-ignore-empty-objects-with-play-json%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









          0














          You should be careful in such cases because you have sealed traits, so you should work in an organized way.



          Let's divide the problem into smaller problems, Let's assume that you just want to write your objects into json. then we can solve the problem of removing empty objects.



          I added more classes to clarify the idea:



          sealed trait Expression
          case object Empty extends Expression
          case class NonEmptyExpression(x: Int) extends Expression

          sealed trait Statement
          case class Return(e: Expression) extends Statement
          case class NoReturn(x: Int) extends Statement


          First Step (provide writer for Expresion):



          to do this you have to create writer for Empty and NonEmptyExpression, so we can do it this way:



          val emptyExpressionWrites = new Writes[Empty.type] {
          override def writes(e: Empty.type) = Json.obj()
          }

          val nonEmptyExpressionWrites = Json.writes[NonEmptyExpression]

          implicit val expressionWrites = new Writes[Expression] {
          override def writes(exp: Expression) = {
          exp match {
          case Empty => emptyExpressionWrites.writes(Empty)
          case nonEmptyExpression: NonEmptyExpression => nonEmptyExpressionWrites.writes(nonEmptyExpression)
          }
          }
          }


          Second Step (provide writer for Statement):



          You have to provide writer for Return and NoReturn. please note that play was able to know how to create writer for Return and it has Expression, because I defined expressionWriter as implicit. so it knows how to serialize Expression now.



          val returnWrites = Json.writes[Return]
          val noReturnWrites = Json.writes[NoReturn]

          val statementWrites = new Writes[Statement] {
          override def writes(s: Statement) = {
          s match {
          case r: Return => returnWrites.writes(r)
          case nr: NoReturn => noReturnWrites.writes(nr)
          }
          }
          }


          Let's test this to make sure it works fine:



          val statementWithNonEmpty = Return(NonEmptyExpression(100))
          println(s"statementWithNonEmpty Json: ${statementWrites.writes(statementWithNonEmpty).toString()}")

          val statementWithEmpty = Return(Empty)
          println(s"statementWithEmpty Json: ${statementWrites.writes(statementWithEmpty).toString()}")


          Output is:




          statementWithNonEmpty Json: {"e":{"x":100}}



          statementWithEmpty Json: {"e":{}}




          Final Step (create List writer with no Empty):



          val listStatementWrites = new Writes[Seq[Statement]] {
          override def writes(o: Seq[Statement]) = {
          val listWithoutEmpty = o.filter {
          case Return(Empty) => false
          case _ => true
          }

          JsArray(listWithoutEmpty.map(statementWrites.writes))
          }
          }


          let's try it:



          val listOfStatements = List(Return(NonEmptyExpression(100)), Return(Empty), Return(NonEmptyExpression(200)))
          println(s"listOfStatements: ${listStatementWrites.writes(listOfStatements).toString()}")


          the output is:




          listOfStatements: [{"e":{"x":100}},{"e":{"x":200}}]







          share|improve this answer
























          • Thank you for your detailed response. However, if you read my question carefully you would notice that I tried something that is very close to what you suggest at the end, right ? So I am not sure why you had to go over all the other stuff :). May I ask if you could elaborate on why your approach works and mine doesn't ? I am curious about why you had to use Seq and not List and also why you passed statementWrites.writes and not Json.toJson. Isn't the latter going to invoke the former internally ?

            – Than21
            Nov 23 '18 at 16:18











          • What you tried is not close to what I answered. I was too verbose because such cases are not trivial, and a short answer may not be enough.

            – Mahmoud Hanafy
            Nov 23 '18 at 16:23






          • 1





            If you see Json.toJson needs a Writes[Statement] as an implicit parameter and you don't have it in your code.

            – Mahmoud Hanafy
            Nov 23 '18 at 16:25
















          0














          You should be careful in such cases because you have sealed traits, so you should work in an organized way.



          Let's divide the problem into smaller problems, Let's assume that you just want to write your objects into json. then we can solve the problem of removing empty objects.



          I added more classes to clarify the idea:



          sealed trait Expression
          case object Empty extends Expression
          case class NonEmptyExpression(x: Int) extends Expression

          sealed trait Statement
          case class Return(e: Expression) extends Statement
          case class NoReturn(x: Int) extends Statement


          First Step (provide writer for Expresion):



          to do this you have to create writer for Empty and NonEmptyExpression, so we can do it this way:



          val emptyExpressionWrites = new Writes[Empty.type] {
          override def writes(e: Empty.type) = Json.obj()
          }

          val nonEmptyExpressionWrites = Json.writes[NonEmptyExpression]

          implicit val expressionWrites = new Writes[Expression] {
          override def writes(exp: Expression) = {
          exp match {
          case Empty => emptyExpressionWrites.writes(Empty)
          case nonEmptyExpression: NonEmptyExpression => nonEmptyExpressionWrites.writes(nonEmptyExpression)
          }
          }
          }


          Second Step (provide writer for Statement):



          You have to provide writer for Return and NoReturn. please note that play was able to know how to create writer for Return and it has Expression, because I defined expressionWriter as implicit. so it knows how to serialize Expression now.



          val returnWrites = Json.writes[Return]
          val noReturnWrites = Json.writes[NoReturn]

          val statementWrites = new Writes[Statement] {
          override def writes(s: Statement) = {
          s match {
          case r: Return => returnWrites.writes(r)
          case nr: NoReturn => noReturnWrites.writes(nr)
          }
          }
          }


          Let's test this to make sure it works fine:



          val statementWithNonEmpty = Return(NonEmptyExpression(100))
          println(s"statementWithNonEmpty Json: ${statementWrites.writes(statementWithNonEmpty).toString()}")

          val statementWithEmpty = Return(Empty)
          println(s"statementWithEmpty Json: ${statementWrites.writes(statementWithEmpty).toString()}")


          Output is:




          statementWithNonEmpty Json: {"e":{"x":100}}



          statementWithEmpty Json: {"e":{}}




          Final Step (create List writer with no Empty):



          val listStatementWrites = new Writes[Seq[Statement]] {
          override def writes(o: Seq[Statement]) = {
          val listWithoutEmpty = o.filter {
          case Return(Empty) => false
          case _ => true
          }

          JsArray(listWithoutEmpty.map(statementWrites.writes))
          }
          }


          let's try it:



          val listOfStatements = List(Return(NonEmptyExpression(100)), Return(Empty), Return(NonEmptyExpression(200)))
          println(s"listOfStatements: ${listStatementWrites.writes(listOfStatements).toString()}")


          the output is:




          listOfStatements: [{"e":{"x":100}},{"e":{"x":200}}]







          share|improve this answer
























          • Thank you for your detailed response. However, if you read my question carefully you would notice that I tried something that is very close to what you suggest at the end, right ? So I am not sure why you had to go over all the other stuff :). May I ask if you could elaborate on why your approach works and mine doesn't ? I am curious about why you had to use Seq and not List and also why you passed statementWrites.writes and not Json.toJson. Isn't the latter going to invoke the former internally ?

            – Than21
            Nov 23 '18 at 16:18











          • What you tried is not close to what I answered. I was too verbose because such cases are not trivial, and a short answer may not be enough.

            – Mahmoud Hanafy
            Nov 23 '18 at 16:23






          • 1





            If you see Json.toJson needs a Writes[Statement] as an implicit parameter and you don't have it in your code.

            – Mahmoud Hanafy
            Nov 23 '18 at 16:25














          0












          0








          0







          You should be careful in such cases because you have sealed traits, so you should work in an organized way.



          Let's divide the problem into smaller problems, Let's assume that you just want to write your objects into json. then we can solve the problem of removing empty objects.



          I added more classes to clarify the idea:



          sealed trait Expression
          case object Empty extends Expression
          case class NonEmptyExpression(x: Int) extends Expression

          sealed trait Statement
          case class Return(e: Expression) extends Statement
          case class NoReturn(x: Int) extends Statement


          First Step (provide writer for Expresion):



          to do this you have to create writer for Empty and NonEmptyExpression, so we can do it this way:



          val emptyExpressionWrites = new Writes[Empty.type] {
          override def writes(e: Empty.type) = Json.obj()
          }

          val nonEmptyExpressionWrites = Json.writes[NonEmptyExpression]

          implicit val expressionWrites = new Writes[Expression] {
          override def writes(exp: Expression) = {
          exp match {
          case Empty => emptyExpressionWrites.writes(Empty)
          case nonEmptyExpression: NonEmptyExpression => nonEmptyExpressionWrites.writes(nonEmptyExpression)
          }
          }
          }


          Second Step (provide writer for Statement):



          You have to provide writer for Return and NoReturn. please note that play was able to know how to create writer for Return and it has Expression, because I defined expressionWriter as implicit. so it knows how to serialize Expression now.



          val returnWrites = Json.writes[Return]
          val noReturnWrites = Json.writes[NoReturn]

          val statementWrites = new Writes[Statement] {
          override def writes(s: Statement) = {
          s match {
          case r: Return => returnWrites.writes(r)
          case nr: NoReturn => noReturnWrites.writes(nr)
          }
          }
          }


          Let's test this to make sure it works fine:



          val statementWithNonEmpty = Return(NonEmptyExpression(100))
          println(s"statementWithNonEmpty Json: ${statementWrites.writes(statementWithNonEmpty).toString()}")

          val statementWithEmpty = Return(Empty)
          println(s"statementWithEmpty Json: ${statementWrites.writes(statementWithEmpty).toString()}")


          Output is:




          statementWithNonEmpty Json: {"e":{"x":100}}



          statementWithEmpty Json: {"e":{}}




          Final Step (create List writer with no Empty):



          val listStatementWrites = new Writes[Seq[Statement]] {
          override def writes(o: Seq[Statement]) = {
          val listWithoutEmpty = o.filter {
          case Return(Empty) => false
          case _ => true
          }

          JsArray(listWithoutEmpty.map(statementWrites.writes))
          }
          }


          let's try it:



          val listOfStatements = List(Return(NonEmptyExpression(100)), Return(Empty), Return(NonEmptyExpression(200)))
          println(s"listOfStatements: ${listStatementWrites.writes(listOfStatements).toString()}")


          the output is:




          listOfStatements: [{"e":{"x":100}},{"e":{"x":200}}]







          share|improve this answer













          You should be careful in such cases because you have sealed traits, so you should work in an organized way.



          Let's divide the problem into smaller problems, Let's assume that you just want to write your objects into json. then we can solve the problem of removing empty objects.



          I added more classes to clarify the idea:



          sealed trait Expression
          case object Empty extends Expression
          case class NonEmptyExpression(x: Int) extends Expression

          sealed trait Statement
          case class Return(e: Expression) extends Statement
          case class NoReturn(x: Int) extends Statement


          First Step (provide writer for Expresion):



          to do this you have to create writer for Empty and NonEmptyExpression, so we can do it this way:



          val emptyExpressionWrites = new Writes[Empty.type] {
          override def writes(e: Empty.type) = Json.obj()
          }

          val nonEmptyExpressionWrites = Json.writes[NonEmptyExpression]

          implicit val expressionWrites = new Writes[Expression] {
          override def writes(exp: Expression) = {
          exp match {
          case Empty => emptyExpressionWrites.writes(Empty)
          case nonEmptyExpression: NonEmptyExpression => nonEmptyExpressionWrites.writes(nonEmptyExpression)
          }
          }
          }


          Second Step (provide writer for Statement):



          You have to provide writer for Return and NoReturn. please note that play was able to know how to create writer for Return and it has Expression, because I defined expressionWriter as implicit. so it knows how to serialize Expression now.



          val returnWrites = Json.writes[Return]
          val noReturnWrites = Json.writes[NoReturn]

          val statementWrites = new Writes[Statement] {
          override def writes(s: Statement) = {
          s match {
          case r: Return => returnWrites.writes(r)
          case nr: NoReturn => noReturnWrites.writes(nr)
          }
          }
          }


          Let's test this to make sure it works fine:



          val statementWithNonEmpty = Return(NonEmptyExpression(100))
          println(s"statementWithNonEmpty Json: ${statementWrites.writes(statementWithNonEmpty).toString()}")

          val statementWithEmpty = Return(Empty)
          println(s"statementWithEmpty Json: ${statementWrites.writes(statementWithEmpty).toString()}")


          Output is:




          statementWithNonEmpty Json: {"e":{"x":100}}



          statementWithEmpty Json: {"e":{}}




          Final Step (create List writer with no Empty):



          val listStatementWrites = new Writes[Seq[Statement]] {
          override def writes(o: Seq[Statement]) = {
          val listWithoutEmpty = o.filter {
          case Return(Empty) => false
          case _ => true
          }

          JsArray(listWithoutEmpty.map(statementWrites.writes))
          }
          }


          let's try it:



          val listOfStatements = List(Return(NonEmptyExpression(100)), Return(Empty), Return(NonEmptyExpression(200)))
          println(s"listOfStatements: ${listStatementWrites.writes(listOfStatements).toString()}")


          the output is:




          listOfStatements: [{"e":{"x":100}},{"e":{"x":200}}]








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 7:29









          Mahmoud HanafyMahmoud Hanafy

          94811528




          94811528













          • Thank you for your detailed response. However, if you read my question carefully you would notice that I tried something that is very close to what you suggest at the end, right ? So I am not sure why you had to go over all the other stuff :). May I ask if you could elaborate on why your approach works and mine doesn't ? I am curious about why you had to use Seq and not List and also why you passed statementWrites.writes and not Json.toJson. Isn't the latter going to invoke the former internally ?

            – Than21
            Nov 23 '18 at 16:18











          • What you tried is not close to what I answered. I was too verbose because such cases are not trivial, and a short answer may not be enough.

            – Mahmoud Hanafy
            Nov 23 '18 at 16:23






          • 1





            If you see Json.toJson needs a Writes[Statement] as an implicit parameter and you don't have it in your code.

            – Mahmoud Hanafy
            Nov 23 '18 at 16:25



















          • Thank you for your detailed response. However, if you read my question carefully you would notice that I tried something that is very close to what you suggest at the end, right ? So I am not sure why you had to go over all the other stuff :). May I ask if you could elaborate on why your approach works and mine doesn't ? I am curious about why you had to use Seq and not List and also why you passed statementWrites.writes and not Json.toJson. Isn't the latter going to invoke the former internally ?

            – Than21
            Nov 23 '18 at 16:18











          • What you tried is not close to what I answered. I was too verbose because such cases are not trivial, and a short answer may not be enough.

            – Mahmoud Hanafy
            Nov 23 '18 at 16:23






          • 1





            If you see Json.toJson needs a Writes[Statement] as an implicit parameter and you don't have it in your code.

            – Mahmoud Hanafy
            Nov 23 '18 at 16:25

















          Thank you for your detailed response. However, if you read my question carefully you would notice that I tried something that is very close to what you suggest at the end, right ? So I am not sure why you had to go over all the other stuff :). May I ask if you could elaborate on why your approach works and mine doesn't ? I am curious about why you had to use Seq and not List and also why you passed statementWrites.writes and not Json.toJson. Isn't the latter going to invoke the former internally ?

          – Than21
          Nov 23 '18 at 16:18





          Thank you for your detailed response. However, if you read my question carefully you would notice that I tried something that is very close to what you suggest at the end, right ? So I am not sure why you had to go over all the other stuff :). May I ask if you could elaborate on why your approach works and mine doesn't ? I am curious about why you had to use Seq and not List and also why you passed statementWrites.writes and not Json.toJson. Isn't the latter going to invoke the former internally ?

          – Than21
          Nov 23 '18 at 16:18













          What you tried is not close to what I answered. I was too verbose because such cases are not trivial, and a short answer may not be enough.

          – Mahmoud Hanafy
          Nov 23 '18 at 16:23





          What you tried is not close to what I answered. I was too verbose because such cases are not trivial, and a short answer may not be enough.

          – Mahmoud Hanafy
          Nov 23 '18 at 16:23




          1




          1





          If you see Json.toJson needs a Writes[Statement] as an implicit parameter and you don't have it in your code.

          – Mahmoud Hanafy
          Nov 23 '18 at 16:25





          If you see Json.toJson needs a Writes[Statement] as an implicit parameter and you don't have it in your code.

          – Mahmoud Hanafy
          Nov 23 '18 at 16:25


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418925%2fhow-can-i-ignore-empty-objects-with-play-json%23new-answer', 'question_page');
          }
          );

          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







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen