Skip to content

[BUG] Support @JsonInclude(Include.NON_EMPTY) #2641

@jorgerod

Description

@jorgerod

Hi

We are integrating FastJson2 as the serializer of our SpringBoot based project.
The performance is much better than Jackson but I have encountered a problem that is blocking us and preventing me from continuing.

SpringBoot uses the SystemHealth object for the actuator endpoint and we need the response from FastJson2 and Jackson to be identical.

This object has fields annotated with @JsonInclude(Include.NON_EMPTY) and FastJson2 is ignoring this property (it is not possible to change the annotation to one of FastJson2's own)

Test:

package org.springframework.boot.actuate.health;

import java.util.Collections;
import java.util.HashMap;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.endpoint.ApiVersion;

import static org.assertj.core.api.Assertions.assertThat;

class FastJson2Test {

  @Test
  void test() throws JsonProcessingException {
    //given
    final SystemHealth systemHealth =
        new SystemHealth(ApiVersion.V3, Status.UP, new HashMap<>(), Collections.emptySet());

    //when
    String jackson = new ObjectMapper().writeValueAsString(systemHealth);   
    String fastjson2 = JSON.toJSONString(systemHealth, JSONWriter.Feature.WriteNonStringValueAsString);
    
    //then
    assertThat(jackson).doesNotContain("components\":{}");
    assertThat(fastjson2).doesNotContain("components\":{}"); // don't work
  }

Result jackson

{"status":"UP"}

Result fastjson2

{"components":{},"groups":[],"status":{"description":"","status":"UP"}}

Related issue:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions