Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions multiapps-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,5 @@
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
</dependencies>
</project>
5 changes: 2 additions & 3 deletions multiapps-common/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
exports org.cloudfoundry.multiapps.common.util;
exports org.cloudfoundry.multiapps.common.util.yaml;

requires transitive com.fasterxml.jackson.core;
requires transitive com.fasterxml.jackson.databind;
requires transitive org.yaml.snakeyaml;
requires transitive tools.jackson.core;
requires transitive tools.jackson.databind;

requires com.fasterxml.jackson.annotation;
requires com.fasterxml.jackson.datatype.jsr310;
requires org.apache.commons.io;
requires org.apache.commons.lang3;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.cloudfoundry.multiapps.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
Expand All @@ -16,13 +15,15 @@
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import tools.jackson.core.JacksonException;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.JavaType;
import tools.jackson.databind.MapperFeature;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.ObjectWriter;
import tools.jackson.databind.cfg.EnumFeature;
import tools.jackson.databind.json.JsonMapper;

public class JsonUtil {

Expand All @@ -34,19 +35,48 @@ private JsonUtil() {
}

private static ObjectMapper createObjectMapper() {
return new ObjectMapper().setVisibility(PropertyAccessor.ALL, Visibility.NONE)
.setVisibility(PropertyAccessor.FIELD, Visibility.ANY)
.setSerializationInclusion(Include.NON_NULL)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.registerModule(new JavaTimeModule());
return JsonMapper.builder()
.changeDefaultVisibility(vc -> vc.withVisibility(PropertyAccessor.ALL, Visibility.NONE)
.withVisibility(PropertyAccessor.FIELD, Visibility.ANY))
.changeDefaultPropertyInclusion(incl -> incl.withValueInclusion(Include.NON_NULL)
.withContentInclusion(Include.NON_NULL))
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
// Preserve Jackson 2 behavior: these defaults flipped in Jackson 3 and would change the
// byte output / read leniency of already-persisted DB-column and Flowable-variable JSON.
.disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.enable(MapperFeature.EXTERNAL_TYPE_ID_ALWAYS_VISIBLE)
// Jackson 3 defaults to (de)serializing enums via toString(); Jackson 2 used name(). Several
// domain enums override toString() to lowercase, so keep name() to stay wire/persistence
// compatible with data written by the Jackson 2 version of the service.
.disable(EnumFeature.WRITE_ENUMS_USING_TO_STRING)
.disable(EnumFeature.READ_ENUMS_USING_TO_STRING)
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.build();
}

private static ObjectMapper createObjectMapperWithNulls() {
// Visibilities are required to parse properly "immutables" objects
return new ObjectMapper().setVisibility(PropertyAccessor.ALL, Visibility.NONE)
.setVisibility(PropertyAccessor.FIELD, Visibility.ANY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.registerModule(new JavaTimeModule());
return JsonMapper.builder()
.changeDefaultVisibility(vc -> vc.withVisibility(PropertyAccessor.ALL, Visibility.NONE)
.withVisibility(PropertyAccessor.FIELD, Visibility.ANY))
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
// Preserve Jackson 2 behavior: these defaults flipped in Jackson 3 and would change the
// byte output / read leniency of already-persisted DB-column and Flowable-variable JSON.
.disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
.enable(MapperFeature.EXTERNAL_TYPE_ID_ALWAYS_VISIBLE)
// Jackson 3 defaults to (de)serializing enums via toString(); Jackson 2 used name(). Several
// domain enums override toString() to lowercase, so keep name() to stay wire/persistence
// compatible with data written by the Jackson 2 version of the service.
.disable(EnumFeature.WRITE_ENUMS_USING_TO_STRING)
.disable(EnumFeature.READ_ENUMS_USING_TO_STRING)
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.build();
}

public static Map<String, Object> convertJsonToMap(InputStream json) {
Expand Down Expand Up @@ -128,7 +158,7 @@ private static <T> T fromJson(InputStream json, JsonSerializationStrategy jsonSe
}
try {
return getObjectMapper(jsonSerializationStrategy).readValue(json, typeReference);
} catch (IOException e) {
} catch (JacksonException e) {
throw new ParsingException(e, errorMessage, e.getMessage(), json);
}
}
Expand All @@ -140,7 +170,7 @@ private static <T> T fromJson(String json, JsonSerializationStrategy jsonSeriali
}
try {
return getObjectMapper(jsonSerializationStrategy).readValue(json, typeReference);
} catch (IOException e) {
} catch (JacksonException e) {
throw new ParsingException(e, errorMessage, e.getMessage(), json.substring(0, Math.min(json.length(), MAX_LENGTH)));
}
}
Expand Down Expand Up @@ -169,7 +199,7 @@ public static String toJson(Object object, boolean indentedOutput) {
public static String toJson(Object object, JsonSerializationStrategy jsonSerializationStrategy, boolean indentedOutput) {
try {
return getObjectWriter(jsonSerializationStrategy, indentedOutput).writeValueAsString(object);
} catch (JsonProcessingException e) {
} catch (JacksonException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
Expand Down Expand Up @@ -215,7 +245,7 @@ public static <T> T fromJson(String json, JsonSerializationStrategy jsonSerializ
private static <T> T fromJson(ObjectMapper objectMapper, String json, JavaType javaType) {
try {
return objectMapper.readValue(json, javaType);
} catch (IOException e) {
} catch (JacksonException e) {
throw new ParsingException(e,
Messages.CANNOT_PARSE_JSON_STRING_TO_TYPE,
json.substring(0, Math.min(json.length(), MAX_LENGTH)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.cloudfoundry.multiapps.common.ParsingException;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.type.TypeReference;
import tools.jackson.core.type.TypeReference;

class JsonUtilTest {

Expand Down
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
<semver4j.version>6.0.0</semver4j.version>
<immutables.version>2.12.1</immutables.version>
<jackson.version>3.2.3</jackson.version>
<jackson.datatype.jsr310.version>2.21.2</jackson.datatype.jsr310.version>
<jakarta.xml.bind-api.version>4.0.5</jakarta.xml.bind-api.version>
<apache.compress.version>1.28.0</apache.compress.version>
</properties>
Expand Down Expand Up @@ -243,12 +242,6 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.datatype.jsr310.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Loading