Skip to content

Commit

Permalink
update docs, IllegalArgumentExc instead of JsonProcessingException
Browse files Browse the repository at this point in the history
  • Loading branch information
patriknw committed Dec 18, 2024
1 parent 1039894 commit c5279ec
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
42 changes: 32 additions & 10 deletions akka-javasdk/src/main/java/akka/javasdk/JsonSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,42 @@ public static <T> ByteString encodeToBytes(T value) throws JsonProcessingExcepti
objectMapper.writerFor(value.getClass()).writeValueAsBytes(value));
}

public static <T> akka.util.ByteString encodeToAkkaByteString(T value) throws JsonProcessingException {
return akka.util.ByteString.fromArrayUnsafe(objectMapper.writerFor(value.getClass()).writeValueAsBytes(value));
/**
* Encode the given value as JSON using Jackson.
*
* @param value the object to encode as JSON, must be an instance of a class properly annotated
* with the needed Jackson annotations.
* @throws IllegalArgumentException if the given value cannot be turned into JSON
*/
public static <T> akka.util.ByteString encodeToAkkaByteString(T value) {
try {
return akka.util.ByteString.fromArrayUnsafe(objectMapper.writerFor(value.getClass()).writeValueAsBytes(value));
} catch (JsonProcessingException ex) {
throw new IllegalArgumentException(
"Could not encode [" + value.getClass().getName() + "] as JSON", ex);
}
}

public static akka.util.ByteString encodeDynamicToAkkaByteString(String key, String value) throws JsonProcessingException {
ObjectNode dynamicJson = objectMapper.createObjectNode().put(key, value);
return akka.util.ByteString.fromArrayUnsafe(objectMapper.writeValueAsBytes(dynamicJson));
public static akka.util.ByteString encodeDynamicToAkkaByteString(String key, String value) {
try {
ObjectNode dynamicJson = objectMapper.createObjectNode().put(key, value);
return akka.util.ByteString.fromArrayUnsafe(objectMapper.writeValueAsBytes(dynamicJson));
} catch (JsonProcessingException ex) {
throw new IllegalArgumentException(
"Could not encode dynamic key/value as JSON", ex);
}
}

public static akka.util.ByteString encodeDynamicCollectionToAkkaByteString(String key, Collection<?> values) throws JsonProcessingException {
ObjectNode objectNode = objectMapper.createObjectNode();
ArrayNode dynamicJson = objectNode.putArray(key);
values.forEach(v -> dynamicJson.add(v.toString()));
return akka.util.ByteString.fromArrayUnsafe(objectMapper.writeValueAsBytes(objectNode));
public static akka.util.ByteString encodeDynamicCollectionToAkkaByteString(String key, Collection<?> values) {
try {
ObjectNode objectNode = objectMapper.createObjectNode();
ArrayNode dynamicJson = objectNode.putArray(key);
values.forEach(v -> dynamicJson.add(v.toString()));
return akka.util.ByteString.fromArrayUnsafe(objectMapper.writeValueAsBytes(objectNode));
} catch (JsonProcessingException ex) {
throw new IllegalArgumentException(
"Could not encode dynamic key/values as JSON", ex);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,8 @@ public static HttpResponse ok(String text) {
*/
public static HttpResponse ok(Object object) {
if (object == null) throw new IllegalArgumentException("object must not be null");
try {
var body = JsonSupport.encodeToAkkaByteString(object);
return HttpResponse.create().withEntity(ContentTypes.APPLICATION_JSON, body);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
var body = JsonSupport.encodeToAkkaByteString(object);
return HttpResponse.create().withEntity(ContentTypes.APPLICATION_JSON, body);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions docs/src/modules/java/pages/serialization.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,4 @@ include::example$event-sourced-customer-registry/src/test/java/customer/domain/C
----
<1> Encodes old class in Base64 String.
<2> Decodes Base64 bytes.
<3> Parses bytes into `Any` object.
<4> Verifies JSON deserialization.
<3> Verifies JSON deserialization.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package customer.domain;

import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import akka.javasdk.JsonSupport;
import akka.util.ByteString;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;

import java.util.Base64;
Expand All @@ -15,9 +14,9 @@
class CustomerEventSerializationTest {

@Test
public void shouldDeserializeWithMandatoryField() {
public void shouldDeserializeWithMandatoryField() throws JsonProcessingException {
//given
Any serialized = JsonSupport.encodeJson(new CustomerEvent.NameChanged("andre"));
ByteString serialized = JsonSupport.encodeToAkkaByteString(new CustomerEvent.NameChanged("andre"));

//when
NameChanged deserialized = JsonSupport.decodeJson(NameChanged.class, serialized);
Expand All @@ -32,7 +31,7 @@ public void shouldDeserializeWithMandatoryField() {
public void shouldDeserializeWithChangedFieldName() {
//given
Address address = new Address("Wall Street", "New York");
Any serialized = JsonSupport.encodeJson(new CustomerEvent.AddressChanged(address));
ByteString serialized = JsonSupport.encodeToAkkaByteString(new CustomerEvent.AddressChanged(address));

//when
AddressChanged deserialized = JsonSupport.decodeJson(AddressChanged.class, serialized);
Expand All @@ -44,7 +43,7 @@ public void shouldDeserializeWithChangedFieldName() {
@Test
public void shouldDeserializeWithStructureMigration() {
//given
Any serialized = JsonSupport.encodeJson(new CustomerCreatedOld("[email protected]", "bob", "Wall Street", "New York"));
ByteString serialized = JsonSupport.encodeToAkkaByteString(new CustomerCreatedOld("[email protected]", "bob", "Wall Street", "New York"));

//when
CustomerEvent.CustomerCreated deserialized = JsonSupport.decodeJson(CustomerEvent.CustomerCreated.class, serialized);
Expand All @@ -58,19 +57,18 @@ public void shouldDeserializeWithStructureMigration() {
@Test
public void shouldDeserializeCustomerCreated_V0() throws InvalidProtocolBufferException {
// tag::testing-deserialization-encoding[]
Any serialized = JsonSupport.encodeJson(new CustomerCreatedOld("[email protected]", "bob", "Wall Street", "New York"));
String encodedBytes = new String(Base64.getEncoder().encode(serialized.toByteArray())); // <1>
ByteString serialized = JsonSupport.encodeToAkkaByteString(new CustomerCreatedOld("[email protected]", "bob", "Wall Street", "New York"));
String encodedBytes = serialized.encodeBase64().utf8String(); // <1>
// end::testing-deserialization-encoding[]

byte[] bytes = Base64.getDecoder().decode(encodedBytes.getBytes()); // <2>
Any serializedAny = Any.parseFrom(ByteString.copyFrom(bytes)); // <3>
ByteString decodedBytes = ByteString.fromString(encodedBytes).decodeBase64(); // <2>

CustomerEvent.CustomerCreated deserialized = JsonSupport.decodeJson(CustomerEvent.CustomerCreated.class,
serializedAny); // <4>
decodedBytes); // <3>

assertEquals("Wall Street", deserialized.address().street());
assertEquals("New York", deserialized.address().city());
}
// end::testing-deserialization[]

}
}

0 comments on commit c5279ec

Please sign in to comment.