Skip to content
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

Fluent API for explicit media readers and writers #1906

Closed
romain-grecourt opened this issue May 29, 2020 · 1 comment · Fixed by #2010
Closed

Fluent API for explicit media readers and writers #1906

romain-grecourt opened this issue May 29, 2020 · 1 comment · Fixed by #2010
Assignees
Labels
Milestone

Comments

@romain-grecourt
Copy link
Contributor

Example:

Explicitly use the JSON-P writer to send a JsonObject instance as a server response payload:

res.send(JsonpSupport.writer().marshall(Json.createObjectBuilder().add("foo", "bar").build()));

Use the JSON-P reader to explicitly consume a server request payload:

JsonpSupport.reader().unmarshall(req.content()).thenAccept(System.out::println);

This can be implemented with default methods on MessageBodyReader and MessageBodyWriter:

MessageBodyReader:

default Single<T> unmarshall(MessageBodyReadableContent content) {
    return content.readerContext().unmarshall(content, this);
}

MessageBodyWriter:

default Function<MessageBodyWriterContext, Publisher<DataChunk>> marshall(T value) {
    return ctx -> ctx.marshall(Single.just(value), this);
}

The marshall method added on MessageBodyWriter requires a new variant of ServerResponse.send:

<T> Single<ServerResponse> send(Function<MessageBodyWriterContext, Publisher<DataChunk> fn);

It would also require a new variant of WebClientRequestBuilder.submit:

Single<WebClientResponse> submit(Function<MessageBodyWriterContext, Publisher<DataChunk> fn);

These changes are now feasible with #1905, it provides static accessors for reader and writer instances and replaces the old marshall / unmarshall variants on the reader and writer context that take an operator class with an operator instance.

Note that this change does not break any existing API, so it can be added later.
It seems that it should also be the default way the Helidon built-in service deal with media types.

@romain-grecourt
Copy link
Contributor Author

@tomas-langer FYI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants