From 91ede06376e30f1a9ead9f08586d0ab39bf93d21 Mon Sep 17 00:00:00 2001 From: dansiviter Date: Fri, 13 May 2022 06:34:45 +0100 Subject: [PATCH] Use Jsonb more effectively (#5047) --- .../jsonb/internal/JsonBindingProvider.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/media/json-binding/src/main/java/org/glassfish/jersey/jsonb/internal/JsonBindingProvider.java b/media/json-binding/src/main/java/org/glassfish/jersey/jsonb/internal/JsonBindingProvider.java index 98ea299fc6..efc7c814e4 100644 --- a/media/json-binding/src/main/java/org/glassfish/jersey/jsonb/internal/JsonBindingProvider.java +++ b/media/json-binding/src/main/java/org/glassfish/jersey/jsonb/internal/JsonBindingProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -19,9 +19,17 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import org.glassfish.jersey.jsonb.LocalizationMessages; +import org.glassfish.jersey.message.internal.AbstractMessageReaderWriterProvider; +import org.glassfish.jersey.message.internal.EntityInputStream; + +import jakarta.json.bind.Jsonb; +import jakarta.json.bind.JsonbBuilder; +import jakarta.json.bind.JsonbException; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.ProcessingException; import jakarta.ws.rs.Produces; @@ -34,14 +42,6 @@ import jakarta.ws.rs.ext.Provider; import jakarta.ws.rs.ext.Providers; -import jakarta.json.bind.Jsonb; -import jakarta.json.bind.JsonbBuilder; -import jakarta.json.bind.JsonbException; - -import org.glassfish.jersey.jsonb.LocalizationMessages; -import org.glassfish.jersey.message.internal.AbstractMessageReaderWriterProvider; -import org.glassfish.jersey.message.internal.EntityInputStream; - /** * Entity provider (reader and writer) for JSONB. * @@ -100,9 +100,8 @@ public void writeTo(Object o, Class type, Type genericType, OutputStream entityStream) throws IOException, WebApplicationException { Jsonb jsonb = getJsonb(type); try { - entityStream.write(jsonb.toJson(o).getBytes(AbstractMessageReaderWriterProvider.getCharset(mediaType))); - entityStream.flush(); - } catch (IOException e) { + jsonb.toJson(o, genericType, new OutputStreamWriter(entityStream, getCharset(mediaType))); + } catch (JsonbException e) { throw new ProcessingException(LocalizationMessages.ERROR_JSONB_SERIALIZATION(), e); } }