Comments (9)
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.
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.
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.
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.
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.
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.
Thanks Christopher.
from jackson-module-scala.
Since version 2.2 has been released already, does it contain the radix feature now?
from jackson-module-scala.
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)
- Release 2.15.2 HOT 2
- Dependency on .class and .tasty files causes footprint bloat for GraalVM NativeImage HOT 5
- Failure to deserialise scala Map when default typing is activated HOT 10
- DefaultScalaModule breaks deserialization of java classes HOT 13
- 2.16 jackson-databind deserialization changes have broken support for the scala.util.Either class HOT 1
- Collection of case classes deserialized as Collection of Map2 HOT 4
- Release 2.15.3 HOT 4
- Release 2.16.0-rc1 HOT 1
- error when deserializing yaml HOT 2
- Release 2.16.0 HOT 2
- release jackson-module-scala v2.12 for Scala 2.10 HOT 2
- Release 2.16.1 HOT 1
- Update `2.17` branch to `2.17.0-rc1-SNAPSHOT` HOT 2
- Release 2.15.4 HOT 1
- Release 2.17.0-rc1 HOT 1
- Release 2.16.2 HOT 2
- Release 2.17.0 HOT 2
- Performance regression with 2.17.x HOT 32
- Update 2.17 release note wiki with Scala module 2.17 fixes HOT 4
- Release 2.17.1 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jackson-module-scala.