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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:37:16+00:00 2026-06-04T13:37:16+00:00

The spray-json library extends basic Scala types with a toJson method. I’d like to

  • 0

The spray-json library extends basic Scala types with a toJson method. I’d like to convert an Any into a JsValue if there is such a pimp for the underlying type. My best attempt works, but is verbose:

import cc.spray._

val maybeJson1: PartialFunction[Any, JsValue] = {
  case x: BigDecimal => x.toJson
  case x: BigInt => x.toJson
  case x: Boolean => x.toJson
  case x: Byte => x.toJson
  case x: Char => x.toJson
  case x: Double => x.toJson
  case x: Float => x.toJson
  case x: Int => x.toJson
  case x: Long => x.toJson
  case x: Short => x.toJson
  case x: String => x.toJson
  case x: Symbol => x.toJson
  case x: Unit => x.toJson
}

Ideally, I’d prefer something (impossible) like this:

def maybeJson2(any: Any): Option[JsValue] = {
  if (pimpExistsFor(any))
    Some(any.toJson)
  else
    None  
}

Is there a way to do this without enumerating every type that has been enriched?

  • 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-04T13:37:17+00:00Added an answer on June 4, 2026 at 1:37 pm

    There is a way, but it requires a lot of reflection and therefore is quite a headache. The basic idea is as follows. The DefaultJsonProtocol object inherits a bunch of traits that contain implicit objects which contain write methods. Each of those will have an accessor function, but you won’t know what it’s called. Basically, you’ll just take all methods that take no parameters and return one object that has a write method that takes the class of your object and returns a JsValue. If you find exactly one such method that returns one such class, use reflection to call it. Otherwise, bail.

    It would look something like this (warning, untested):

    def canWriteMe(writer: java.lang.Class[_], me: java.lang.Class[_]): 
      Option[java.lang.reflect.Method] =
    {
      writer.getMethods.find(_.getName == "write").filter{ m =>
        classOf[JsValue].isAssignableFrom(m.getReturnType) && {
          val parm = m.getParameterTypes()
          m.length == 1 && parm(0).isAssignableFrom(me)
        }
      }
    }
    def maybeJson2(any: Any): Option[JsValue] = {
      val couldWork = {
        DefaultJsonProtocol.getClass.getMethods.
          filter(_.getParameterTypes.length==0).
          flatMap(m => canWriteMe(m.getReturnType, any.getClass).map(_ -> m))
      }
      if (couldWork.length != 1) None else {
        couldWork.headOption.map{ case (wrMeth, obMeth) =>
          val wrObj = obMeth.invoke(DefaultJsonProtocol)
          val answer = wrMeth.invoke(wrObj, any)
        }
      }
    }
    

    Anyway, you’re best off pulling the DefaultJsonProtocol class apart in the REPL step by step and finding out how to reliably identify the objects that define the writers, and then get the write methods out of them.

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

Sidebar

Related Questions

How can i simulate a spray like windows paint ? i think it create
I want to make a background spray painting gradient only on top half of
I tried to pull in external dependency on github project: object HelloBuild extends Build
Very simple thing I'm trying to do here. I would like to have 2
My setting: Compiler: DMD 1.056 Library: Tango 0.99.9 OS: Windows 7 What I have
I have a small set of structured data items that I would like embedded
Basically I started to design my project to be like that: Play! Framework for
Spray framework (spray.cc) seems to be a fun and productive way to build REST
I'm trying to create a standalone JAR containing Akka, Spray, and Jetty. Ideally I
I have chunks of strings within square brackets, like this: [p1 text1/label1] [p2 text2/label2]

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.