-
Notifications
You must be signed in to change notification settings - Fork 48
support Jackson v3 #837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
support Jackson v3 #837
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
...tp-jackson3/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson3/Jackson.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* license agreements; and to You under the Apache License, version 2.0: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This file is part of the Apache Pekko project, which was derived from Akka. | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package org.apache.pekko.http.javadsl.marshallers.jackson3; | ||
|
||
import java.io.IOException; | ||
|
||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
import org.apache.pekko.http.javadsl.model.HttpEntity; | ||
import org.apache.pekko.http.javadsl.model.MediaTypes; | ||
import org.apache.pekko.http.javadsl.model.RequestEntity; | ||
import org.apache.pekko.http.javadsl.marshalling.Marshaller; | ||
import org.apache.pekko.http.javadsl.unmarshalling.Unmarshaller; | ||
import org.apache.pekko.http.scaladsl.model.ExceptionWithErrorInfo; | ||
import org.apache.pekko.http.scaladsl.model.ErrorInfo; | ||
import org.apache.pekko.util.ByteString; | ||
|
||
import tools.jackson.core.JacksonException; | ||
import tools.jackson.core.StreamReadConstraints; | ||
import tools.jackson.core.StreamWriteConstraints; | ||
import tools.jackson.core.json.JsonFactory; | ||
import tools.jackson.core.util.BufferRecycler; | ||
import tools.jackson.core.util.JsonRecyclerPools; | ||
import tools.jackson.core.util.RecyclerPool; | ||
import tools.jackson.databind.DeserializationFeature; | ||
import tools.jackson.databind.ObjectMapper; | ||
import tools.jackson.databind.json.JsonMapper; | ||
|
||
/** A JSON marshaller/unmarshaller using the Jackson library. */ | ||
public class Jackson { | ||
private static final ObjectMapper defaultObjectMapper = createMapper(); | ||
|
||
/** INTERNAL API */ | ||
public static class JacksonUnmarshallingException extends ExceptionWithErrorInfo { | ||
public JacksonUnmarshallingException(Class<?> expectedType, Exception cause) { | ||
super( | ||
new ErrorInfo( | ||
"Cannot unmarshal JSON as " + expectedType.getSimpleName(), cause.getMessage()), | ||
cause); | ||
} | ||
} | ||
|
||
public static <T> Marshaller<T, RequestEntity> marshaller() { | ||
return marshaller(defaultObjectMapper); | ||
} | ||
|
||
public static <T> Marshaller<T, RequestEntity> marshaller(ObjectMapper mapper) { | ||
return Marshaller.wrapEntity( | ||
u -> toJSON(mapper, u), Marshaller.stringToEntity(), MediaTypes.APPLICATION_JSON); | ||
} | ||
|
||
public static <T> Unmarshaller<ByteString, T> byteStringUnmarshaller(Class<T> expectedType) { | ||
return byteStringUnmarshaller(defaultObjectMapper, expectedType); | ||
} | ||
|
||
public static <T> Unmarshaller<HttpEntity, T> unmarshaller(Class<T> expectedType) { | ||
return unmarshaller(defaultObjectMapper, expectedType); | ||
} | ||
|
||
public static <T> Unmarshaller<HttpEntity, T> unmarshaller( | ||
ObjectMapper mapper, Class<T> expectedType) { | ||
return Unmarshaller.forMediaType(MediaTypes.APPLICATION_JSON, Unmarshaller.entityToString()) | ||
.thenApply(s -> fromJSON(mapper, s, expectedType)); | ||
} | ||
|
||
public static <T> Unmarshaller<ByteString, T> byteStringUnmarshaller( | ||
ObjectMapper mapper, Class<T> expectedType) { | ||
return Unmarshaller.sync(s -> fromJSON(mapper, s.utf8String(), expectedType)); | ||
} | ||
|
||
private static String toJSON(ObjectMapper mapper, Object object) { | ||
try { | ||
return mapper.writeValueAsString(object); | ||
} catch (JacksonException e) { | ||
throw new IllegalArgumentException("Cannot marshal to JSON: " + object, e); | ||
} | ||
} | ||
|
||
private static <T> T fromJSON(ObjectMapper mapper, String json, Class<T> expectedType) { | ||
try { | ||
return mapper.readerFor(expectedType).readValue(json); | ||
} catch (JacksonException e) { | ||
throw new JacksonUnmarshallingException(expectedType, e); | ||
} | ||
} | ||
|
||
private static ObjectMapper createMapper() { | ||
return createMapper(ConfigFactory.load().getConfig("pekko.http.marshallers.jackson3")); | ||
} | ||
|
||
static JsonFactory createJsonFactory(final Config config) { | ||
StreamReadConstraints streamReadConstraints = | ||
StreamReadConstraints.builder() | ||
.maxNestingDepth(config.getInt("read.max-nesting-depth")) | ||
.maxNumberLength(config.getInt("read.max-number-length")) | ||
.maxStringLength(config.getInt("read.max-string-length")) | ||
.maxNameLength(config.getInt("read.max-name-length")) | ||
.maxDocumentLength(config.getLong("read.max-document-length")) | ||
.maxTokenCount(config.getLong("read.max-token-count")) | ||
.build(); | ||
StreamWriteConstraints streamWriteConstraints = | ||
StreamWriteConstraints.builder() | ||
.maxNestingDepth(config.getInt("write.max-nesting-depth")) | ||
.build(); | ||
return JsonFactory.builder() | ||
.streamReadConstraints(streamReadConstraints) | ||
.streamWriteConstraints(streamWriteConstraints) | ||
.recyclerPool(getBufferRecyclerPool(config)) | ||
.build(); | ||
} | ||
|
||
static ObjectMapper createMapper(final Config config) { | ||
return JsonMapper.builder(createJsonFactory(config)) | ||
// Jackson 3 changed FAIL_ON_UNKNOWN_PROPERTIES default to false | ||
// we keep it false because it is more secure to fail on unknown properties | ||
// and it is consistent with previous pekko-http-jackson behavior | ||
// and there are tests depending on this behavior | ||
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
.build(); | ||
} | ||
|
||
private static RecyclerPool<BufferRecycler> getBufferRecyclerPool(final Config cfg) { | ||
final String poolType = cfg.getString("buffer-recycler.pool-instance"); | ||
switch (poolType) { | ||
case "thread-local": | ||
return JsonRecyclerPools.threadLocalPool(); | ||
case "concurrent-deque": | ||
return JsonRecyclerPools.newConcurrentDequePool(); | ||
case "shared-concurrent-deque": | ||
return JsonRecyclerPools.sharedConcurrentDequePool(); | ||
case "bounded": | ||
return JsonRecyclerPools.newBoundedPool(cfg.getInt("buffer-recycler.bounded-pool-size")); | ||
case "non-recycling": | ||
return JsonRecyclerPools.nonRecyclingPool(); | ||
default: | ||
throw new IllegalArgumentException("Unknown recycler-pool: " + poolType); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
http-marshallers-java/http-jackson3/src/main/resources/reference.conf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
################################### | ||
# Pekko HTTP Jackson3 Config File # | ||
################################### | ||
|
||
# This is the reference config file that contains all the default settings. | ||
# Make your edits/overrides in your application.conf. | ||
|
||
pekko.http.marshallers.jackson3 { | ||
read { | ||
# see https://www.javadoc.io/doc/tools.jackson.core/jackson-core/3.0.0/tools.jackson.core/tools/jackson/core/StreamReadConstraints.html | ||
# these defaults are the same as the defaults in `StreamReadConstraints` | ||
max-nesting-depth = 1000 | ||
max-number-length = 1000 | ||
max-string-length = 20000000 | ||
max-name-length = 50000 | ||
# max-document-length of -1 means unlimited | ||
max-document-length = -1 | ||
# max-token-count of -1 means unlimited | ||
max-token-count = -1 | ||
} | ||
|
||
write { | ||
# see https://www.javadoc.io/doc/tools.jackson.core/jackson-core/3.0.0/tools.jackson.core/tools/jackson/core/StreamWriteConstraints.html | ||
# these defaults are the same as the defaults in `StreamWriteConstraints` | ||
max-nesting-depth = 1000 | ||
} | ||
|
||
# Controls the Buffer Recycler Pool implementation used by Jackson. | ||
# https://www.javadoc.io/doc/tools.jackson.core/jackson-core/3.0.0/tools.jackson.core/tools/jackson/core/util/JsonRecyclerPools.html | ||
# The default is "thread-local" which is the same as the default in Jackson 2.18. | ||
buffer-recycler { | ||
# the supported values are "thread-local", "concurrent-deque", "shared-concurrent-deque", "bounded", "non-recycling" | ||
pool-instance = "thread-local" | ||
# the maximum size of bounded recycler pools - must be >=1 or an IllegalArgumentException will occur | ||
# only applies to pool-instance type "bounded" | ||
bounded-pool-size = 100 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to make it overrideable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API methods allow you to provide your own ObjectMapper so this code is just for the case where we need to create an ObjectMapper instance because the user hasn't provided one.
If we were to open this up, I would prefer if users were requesting it. This is a copy of the code from pekko-http-jackson and noone was requesting any changes to this for that module.