Table of contents
  1. Jackson
  2. Text Manipulation
    1. toString and toMap
      1. Pass a string as JSON
    2. JSON string to JsonNode
  3. Common Config Settings
  4. References
    1. jackson feature docs
      1. micronaut jackson config docs
    2. Examples




Jackson

Text Manipulation

toString and toMap

public class JacksonToStuff {
    @JsonValue
    @Override
    public Map<String, Object> toMap() {
        Map<String, Object> raw = new HashMap<>();
        raw.put("yourField", this.yourField.toPlainString());
        /* more fields */
        return raw;
    }

    @JsonValue
    @Override
    public String toString() {
        // add JSON processing exception handling, dropped for readability
        return new ObjectMapper().writeValueAsString(this.toMap());
    }
}

Pass a string as JSON

public class Data {
    private Map<String, User> record;

    public Map<String, User> getRecord() {
        return record;
    }

    public void setRecord(Map<String, User> record) {
        this.record = record;
    }

    @Override
    public String toString() {
        return "Data{" +
                "record=" + record +
                '}';
    }
}

JSON string to JsonNode

public class StringToNode {
    Map<String, Object> agencyMap = Map.of(
            "name", "Agencia Prueba",
            "phone1", "1198788373",
            "address", "Larrea 45 e/ calligaris y paris",
            "number", 267,
            "enable", true,
            "location", Map.of("id", 54),
            "responsible", Set.of(Map.of("id", 405)),
            "sellers", List.of(Map.of("id", 605))
                                          );

    ObjectNode agencyNode = new ObjectMapper().valueToTree(agencyMap);
}

Common Config Settings

jackson:
  property-naming-strategy: LOWER_CAMEL_CASE
  locale:                  en_US
  date-format:             yyyy-MM-dd'T'HH:mm:ss.SSS
  mapper:
    ACCEPT_CASE_INSENSITIVE_ENUMS: true
  serialization:
    INDENT_OUTPUT:             false
    WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS: false
    WRITE_DATES_AS_TIMESTAMPS: false
  deserialization:
    FAIL_ON_UNKNOWN_PROPERTIES: false
    READ_DATE_TIMESTAMPS_AS_NANOSECONDS: false
  serialization-inclusion: non_null

References

jackson feature docs

micronaut jackson config docs

Examples