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

  • Home
  • SEARCH
  • 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 9287911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:47:35+00:00 2026-06-18T19:47:35+00:00

When serializing the following case class, the val element is not included. Why is

  • 0

When serializing the following case class, the val element is not included. Why is that, and can I have it included?

case class Asset(id: Option[Int], description: Option[String]= None) {
  val url = "images/" + id.toString+".png"
}

Update: Added Json library, and specification/intended use of the url “property”.

I am using the Json library shipped with Play 2.1/Scala 2.10.

Actually the url property is meant to be a function which will look up the transformation algorithm according to configuration, for example the image can be available locally, or available from an external host.

  • 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-18T19:47:37+00:00Added an answer on June 18, 2026 at 7:47 pm

    Although you sould really specify which json serialization library you are using, it is pretty much guaranteed that just doing the following will work:

    case class Asset(id: Option[Int], description: Option[String]= None, url = "images/" + id.toString+".png")
    

    Given that url has a default value anyway, turning it into a parameter won’t negatively affect your code (you can still do Asset(None) by example, as before). The only downside is that now client code might create an Asset instance with a different value for url, which might not be what you want.

    If that’s the case, you’ll probably need to create a custom json format for your Asset class, but without knowing which serialization library you use I can’t help much more in this respect.


    UPDATE:

    Ooops, I totally missed the fact that the default value for url depends on another parameter (id) (thanks to @Kristian Domagala for noticing). Thus my above snippet does not compile. One simple solution is to put url in a second parameter list as suggested by @Kristian Domagala:

    case class Asset(id:Option[Int],description:Option[String]=None)(val url:String = "images/" + id.toString+".png")
    

    But this might not be ideal as this changed the equality semantics of Asset (url is not taken into account anymore when comparing instances), and also changes the construction syntax : when explicitly specifying the url value, you’d need to do something like Asset(Some(123))("gfx/myImage.png") instead of by example Asset(Some(123), url="gfx/myImage.png").
    If you can live with these downsides this is certainly the easiest solution.

    Otherwise, there is another work around: we can redefine Asset.apply ourselve (by hand):

    case class AssetImpl( val id: Option[Int], val description: Option[String], val url: Option[String]) {
      override def productPrefix = "Asset"
    }
    type Asset = AssetImpl
    object Asset {
      def apply( id: Option[Int], description: Option[String] = None, url: Option[String] = None ) = {
        new Asset( id, description, url.orElse( id.map( "images/" + _ + ".png") ) )
      }
    }
    

    As you can see I have turned url into an Option with a default value of None (avoiding the previous compilation error, as it does not depend on id anymore) due to , and in def apply... I instantiate Asset with a default value for url (this is where I actually get the value of id) of id.map( "images/" + _ + ".png").

    The rest is basically just noise to be able to redefine Asset.apply: it turns out that in fact you cannot redefine the factory of a case class (you can only add separate overloads). So I renamed the class as AssetImpl, added a type alias so that noone notices ( 😉 ), and created my very own object Asset where I define the apply method (which does not conflict anymore with the auto-generated apply method because this one is in the distinct AssetImpl object.
    I could also simply have turned Asset into a standard class (non case class) but then I would have to redefine equals and hashCode which I find more annoying given that it must be maintained when adding/removing fields to the class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming that I have a Scala case class that is persisted using the Salat/Casbah/Mongo
I have the following class... public class Product { public int Id { get;
I have the following XML which needs deserializing/serializing: <instance> <dog> <items> <item> <label>Spaniel</label> </item>
I have the following PHP code: echo ' <td id='.$metaso['semanaventa'].'td1><form id='.$metaso['semanaventa'].'form name='.$metaso['semanaventa'].'form class=dinamic action=compromiso_funciones.php
I have a problem when serializing a Dictionary<string,Person> to IsolatedStorageSettings. I'm doing the following:
I'm serializing a class like this public MyClass { public int? a { get;
I am having an issue serializing a c# class to an XML file that
I have a object that im serializing to a JSON object using JSON.Net. This
I have a class structure in C#, similar to the following: [DataContract] class Data
I have following XML generated by serializing a boost::posix_time::ptime structure. I want to create

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.