Git Product home page Git Product logo

jackson-module-caseclass's Introduction

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 FasterXML/jackson-module-scala#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)

jackson-module-caseclass's People

Contributors

changvvb avatar

Watchers

James Cloos avatar

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.