jackson-module-caseclass


License

License

Categories

Categories

Jackson Data JSON
GroupId

GroupId

com.github.changvvb
ArtifactId

ArtifactId

jackson-module-caseclass_2.12
Last Version

Last Version

1.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

jackson-module-caseclass
jackson-module-caseclass
Project URL

Project URL

https://github.com/changvvb/jackson-module-caseclass
Project Organization

Project Organization

com.github.changvvb
Source Code Management

Source Code Management

https://github.com/changvvb/jackson-module-caseclass

Download jackson-module-caseclass_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.changvvb/jackson-module-caseclass_2.12/ -->
<dependency>
    <groupId>com.github.changvvb</groupId>
    <artifactId>jackson-module-caseclass_2.12</artifactId>
    <version>1.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.changvvb/jackson-module-caseclass_2.12/
implementation 'com.github.changvvb:jackson-module-caseclass_2.12:1.1.1'
// https://jarcasting.com/artifacts/com.github.changvvb/jackson-module-caseclass_2.12/
implementation ("com.github.changvvb:jackson-module-caseclass_2.12:1.1.1")
'com.github.changvvb:jackson-module-caseclass_2.12:jar:1.1.1'
<dependency org="com.github.changvvb" name="jackson-module-caseclass_2.12" rev="1.1.1">
  <artifact name="jackson-module-caseclass_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.changvvb', module='jackson-module-caseclass_2.12', version='1.1.1')
)
libraryDependencies += "com.github.changvvb" % "jackson-module-caseclass_2.12" % "1.1.1"
[com.github.changvvb/jackson-module-caseclass_2.12 "1.1.1"]

Dependencies

compile (7)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.10
org.scala-lang : scala-reflect jar 2.12.10
com.fasterxml.jackson.core : jackson-core jar 2.9.8
com.fasterxml.jackson.core : jackson-annotations jar 2.9.8
com.fasterxml.jackson.core : jackson-databind jar 2.9.8
com.fasterxml.jackson.module : jackson-module-paranamer jar 2.9.8
com.fasterxml.jackson.module : jackson-module-scala_2.12 jar 2.9.8

test (5)

Group / Artifact Type Version
com.fasterxml.jackson.datatype : jackson-datatype-joda jar 2.9.8
com.fasterxml.jackson.datatype : jackson-datatype-guava jar 2.9.8
com.fasterxml.jackson.module : jackson-module-jsonSchema jar 2.9.8
org.scalatest : scalatest_2.12 jar 3.0.6-SNAP3
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

jackson-module-caseclass

Scala CI

Features

  • If one field present in json, just deserialize it.
  • If one field not present in json, but the case class has default value, deserialize it as the case class default value.
  • If one field not present in json, and the case class doesn't has default value, deserialize it as a zero value.
  • Use scala reflect instead of java reflect to constract JavaType, so that jackson-module-caseclass can extract type parameter correctly https://github.com/FasterXML/jackson-module-scala/issues/62.

Zero value

For some scala type, if jackson-module-caseclass doesn't know what a field value should be set, it will be deserialized as a zero value so that we can avoid NullPointerException at most case.

  • Number(Int, Long, Char ...): 0
  • Boolean: false
  • Option: None
  • collection.Map: Map.empty(you should set it as default value in case class definition)
  • Iterable: Nil(same as above)

Dependency

sbt

libraryDependencies += "com.github.changvvb" %% "jackson-module-caseclass" % "1.1.1"

Usage

1. Use @CaseClassDeserialize

import com.fasterxml.jackson.module.caseclass.annotation.CaseClassDeserialize
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.caseclass.mapper.CaseClassObjectMapper

@CaseClassDeserialize()
case class TestCaseClass(
  intValue:Int,
  stringValue:String,
  seqValue:Seq[_],
  optionValue:Option[_] = None)

val mapper = new ObjectMapper with ScalaObjectMapper with CaseClassObjectMapper

val json =
  """
    |{
    | "intValue": 3,
    | "stringValue": "some strings"
    |}
  """.stripMargin


mapper.readValue[TestCaseClass](json)

2. Use @JsonDeserialize

import com.fasterxml.jackson.module.caseclass.deser.CaseClassDeserializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.caseclass.mapper.CaseClassObjectMapper

class MyDeserializer extends CaseClassDeserializer[TestCaseClass]

@JsonDeserialize(using = classOf[MyDeserializer])
case class TestCaseClass(
  intValue:Int,
  stringValue:String,
  seqValue:Seq[_],
  optionValue:Option[_] = None)
  
val mapper = new ObjectMapper with ScalaObjectMapper with CaseClassObjectMapper
  
val json =
  """
    |{
    | "intValue": 3,
    | "stringValue": "some strings"
    |}
  """.stripMargin


mapper.readValue[TestCaseClass](json)

3. Use registerCaseClassDeserializer()

import com.fasterxml.jackson.module.caseclass.deser.CaseClassDeserializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.caseclass.mapper.CaseClassObjectMapper

case class TestCaseClass(
  intValue:Int,
  stringValue:String,
  seqValue:Seq[_],
  optionValue:Option[_] = None)
  
val mapper = new ObjectMapper with ScalaObjectMapper with CaseClassObjectMapper
mapper.registerCaseClassDeserializer[TestCaseClass]()
  
val json =
  """
    |{
    | "intValue": 3,
    | "stringValue": "some strings"
    |}
  """.stripMargin


mapper.readValue[TestCaseClass](json)

4. Enable all case class

import com.fasterxml.jackson.module.caseclass.deser.CaseClassDeserializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.caseclass.mapper.CaseClassObjectMapper

case class TestCaseClass(
  intValue:Int,
  stringValue:String,
  seqValue:Seq[_],
  optionValue:Option[_] = None)
  
val mapper = new ObjectMapper with ScalaObjectMapper with CaseClassObjectMapper
mapper.setAllCaseClassEnabled(true)
val json =
  """
    |{
    | "intValue": 3,
    | "stringValue": "some strings"
    |}
  """.stripMargin

mapper.readValue[TestCaseClass](json)

Versions

Version
1.1.1
1.1.0
1.0.0
0.0.2
0.0.1