He-Pin commented on code in PR #837: URL: https://github.com/apache/pekko-http/pull/837#discussion_r2446608070
########## http-marshallers-java/http-jackson3/src/main/java/org/apache/pekko/http/javadsl/marshallers/jackson3/Jackson.java: ########## @@ -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")); Review Comment: is it possible to make it overrideable? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
