Giter Site home page Giter Site logo

Comments (9)

christophercurrie avatar christophercurrie commented on May 25, 2024

Jackson Databind has BigIntegerDeserializer for java.math.BigInteger. I would start there, customizing as needed to handle the hexidecimal radix. Similarly, you can base your serializer on LongSerializer, but probably without the schema and formatVisitor overrides. You'd create a Module class to register your serializers, and then register the Module on the ObjectMapper instance.

That said, I'd actually recommend avoiding this route unless you have a lot of BigInteger fields you're working with. For just a few, it's far simpler to have a String field that Jackson uses for serialization, and a couple of conversion methods to read and write BigInt values to and from the string.

from jackson-module-scala.

jans23 avatar jans23 commented on May 25, 2024

Thanks for your help. The BigIntegerDeserializer and LongSerializer you mentioned are both for Java types. Instead a similar example for a Scala type would be really helpful. But I guess there isn't any such example or easy way which is why you advise not to use this approach. To me this seems to be an important difference to the predecessor Jerkson where custom de/serializer were quick to do. Do you plan to make this easier for Jackson Module Scala in the future?

from jackson-module-scala.

christophercurrie avatar christophercurrie commented on May 25, 2024

There really isn't much different in implementing serializers for Scala types. The simplest example would probably be OptionSerializer and OptionDeserializer, but they also have logic that's specific to option types, so the Java integer serializers are probably a closer match.

I haven't looked at Jerkson very deeply, so I can't really compare the two. The infrastructure for custom serialization comes from Jackson, and the Scala module uses it as best it can; implementing a different model is currently beyond the scope of the project, though if you had ideas for an improved extension API, I'd be willing to consider it.

For now, though, I'm looking into contributing a patch to the core Jackson library to add a radix attribute to the @JsonFormat annotation. Once it's done, I can add support for it in the Scala module, such that you'd be able to do something like:

class BigIntHolder 
{
  @JsonFormat(shape=JsonFormat.Shape.STRING, radix=16)
  var bigInt: BigInt = 0
}

and have it be able to parse and generate hexadecimal strings for BigInt values. I'm hopeful this feature will be in the 2.2 release.

from jackson-module-scala.

jans23 avatar jans23 commented on May 25, 2024

I got it!
First I define my custom serializer and deserializer:

class BigIntDeserializer extends JsonDeserializer[BigInt] {
  def deserialize(jp: JsonParser, context: DeserializationContext) = {
    BigInt(jp.getText, 16)
  }
  override def isCachable = true //not sure what this is for but found it in an example somewhere
}

class BigIntSerializer extends JsonSerializer[BigInt] {
  def serialize(value: BigInt, json: JsonGenerator, provider: SerializerProvider) {
    json.writeString(value.toString(16))
  }
}

Then -- as you explained -- I create a module and include the de/serializers. Eventually I register the module at the mapper. During this I also specify my own date format:

private val dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")

val module = new SimpleModule("CustomJson", Version.unknownVersion())
module.addSerializer(classOf[BigInt], new BigIntSerializer)
module.addDeserializer(classOf[BigInt], new BigIntDeserializer)

val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.setDateFormat(dateFormat)
mapper.registerModule(module)

Now since I figured this out, Jackson Module Scala works very well for me and even better than Jerkson which causes some errors. Thanks a lot for your good work.

I'm just wondering why you don't provide such a wrapper for better convenience?

from jackson-module-scala.

jans23 avatar jans23 commented on May 25, 2024

In case the radix attribute makes it into version 2.2, how would I define the radix for all occurrence of BigInt rather than for each particular field?

from jackson-module-scala.

christophercurrie avatar christophercurrie commented on May 25, 2024

I haven't used Jerkson, so the convenience wrapper never came up. The majority of my use of Jackson has been integrated with frameworks like Jersey where most of the JSON conversion happened at the infrastructure level. It's a trivial enough piece of code that if folks want it, they can add it to their projects.

When the radix parameter is plumbed through, it will have a default that you can set in the ObjectMapper. I'll post a relevant code sample when its done.

from jackson-module-scala.

jans23 avatar jans23 commented on May 25, 2024

Thanks Christopher.

from jackson-module-scala.

jans23 avatar jans23 commented on May 25, 2024

Since version 2.2 has been released already, does it contain the radix feature now?

from jackson-module-scala.

christophercurrie avatar christophercurrie commented on May 25, 2024

Unfortunately not, it required a larger scope of change to the supporting Jackson libraries (databind, in particular) that I wasn't able to complete the changes in time for 2.2.

I've opened FasterXML/jackson-databind#221 against myself to remind me to work on this feature for 2.3.

from jackson-module-scala.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.