Skip to content

Commit

Permalink
move skip logic to serializer
Browse files Browse the repository at this point in the history
Signed-off-by: MregXN <[email protected]>
  • Loading branch information
MregXN committed May 23, 2023
1 parent 34f015a commit 76afb4b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
12 changes: 1 addition & 11 deletions sdk/src/main/java/io/dapr/client/DaprClientHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,13 @@ public Mono<Void> publishEvent(PublishEventRequest request) {
Object data = request.getData();
Map<String, String> metadata = request.getMetadata();

byte[] serializedEvent = objectSerializer.serialize(data);
// Content-type can be overwritten on a per-request basis.
// It allows CloudEvents to be handled differently, for example.
String contentType = request.getContentType();
if (contentType == null || contentType.isEmpty()) {
contentType = objectSerializer.getContentType();
}

byte[] serializedEvent;
if (contentType.equals("text/plain"))
{
serializedEvent = ((String)data).getBytes();
}
else
{
serializedEvent = objectSerializer.serialize(data);
}

Map<String, String> headers = Collections.singletonMap("content-type", contentType);

String[] pathSegments = new String[]{ DaprHttp.API_VERSION, "publish", pubsubName, topic };
Expand Down
4 changes: 4 additions & 0 deletions sdk/src/main/java/io/dapr/client/ObjectSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public byte[] serialize(Object state) throws IOException {
return ((MessageLite) state).toByteArray();
}

if(state instanceof String) {
return ((String)state).getBytes();
}

// Not string, not primitive, so it is a complex type: we use JSON for that.
return OBJECT_MAPPER.writeValueAsBytes(state);
}
Expand Down

0 comments on commit 76afb4b

Please sign in to comment.