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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:08:17+00:00 2026-06-04T21:08:17+00:00

I have an optional field on my requests: case class SearchRequest(url: String, nextAt: Option[Date])

  • 0

I have an optional field on my requests:

case class SearchRequest(url: String, nextAt: Option[Date])

My protocol is:

object SearchRequestJsonProtocol extends DefaultJsonProtocol {
    implicit val searchRequestFormat = jsonFormat(SearchRequest, "url", "nextAt")
}

How do I mark the nextAt field optional, such that the following JSON objects will be correctly read and accepted:

{"url":"..."}
{"url":"...", "nextAt":null}
{"url":"...", "nextAt":"2012-05-30T15:23Z"}

I actually don’t really care about the null case, but if you have details, it would be nice. I’m using spray-json, and was under the impression that using an Option would skip the field if it was absent on the original JSON object.

  • 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-04T21:08:18+00:00Added an answer on June 4, 2026 at 9:08 pm

    You might have to create an explicit format (warning: psuedocodish):

    object SearchRequestJsonProtocol extends DefaultJsonProtocol {
        implicit object SearchRequestJsonFormat extends JsonFormat[SearchRequest] {
            def read(value: JsValue) = value match {
                case JsObject(List(
                        JsField("url", JsString(url)),
                        JsField("nextAt", JsString(nextAt)))) =>
                    SearchRequest(url, Some(new Instant(nextAt)))
    
                case JsObject(List(JsField("url", JsString(url)))) =>
                    SearchRequest(url, None)
    
                case _ =>
                    throw new DeserializationException("SearchRequest expected")
            }
    
            def write(obj: SearchRequest) = obj.nextAt match {
                case Some(nextAt) => 
                    JsObject(JsField("url", JsString(obj.url)),
                             JsField("nextAt", JsString(nextAt.toString)))
                case None => JsObject(JsField("url", JsString(obj.url)))
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this domain object: case class Person ( name : String, age: Option[Int],
I have a lot of optional fields in Mongoid, like: field :key, type: String
I have a model with an optional file field class MyModel(models.Model): name = models.CharField(max_length=50)
I have a simple form with two fields and an optional field which is
I have an field, Address2, which is optional. Thus if it is null, no
Have a form that is emailed with checkboxes. Some checkboxes have an optional field
I have search query that has many optional parameters and then search word field
I have the below protocol buffer. Note that StockStatic is a repeated field. message
I have 4 optional fields, but at least 1 field (any field) must be
The short version is that I have a protocol which has an optional parameter.

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.