Skip to content

Commit

Permalink
Use Jsonb more effectively (#5047)
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiviter committed May 13, 2022
1 parent 0e24be9 commit 91ede06
Showing 1 changed file with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 91ede06

Please sign in to comment.