Skip to content

@JsonInclude content inclusion does not work #346

@jefftt

Description

@jefftt

Jackson version 2.9.1

I would like to serialized Some(List.empty) to []

import java.io.StringWriter

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper

case class Test(opt: Option[List[String]] = Some(List.empty))

val mapper = new ObjectMapper with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.setDefaultPropertyInclusion(JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.ALWAYS))


val nonEmptyList = new StringWriter
mapper.writeValue(nonEmptyList, Test(Some(List("hello", "world"))))
nonEmptyList.toString // {"opt":["hello","world"]}

val emptyList = new StringWriter
mapper.writeValue(emptyList, Test(Some(List())))
emptyList.toString // {} should be {"opt": []} !?

val emptyOption = new StringWriter
mapper.writeValue(emptyOption, Test(None))
emptyOption.toString // {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions