Last active
April 6, 2021 16:27
-
-
Save kstrempel/4948351cfe439b93f81c4d1205f675cd to your computer and use it in GitHub Desktop.
Generate v0.2 cloud event and parse it as 1.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//DEPS io.cloudevents:cloudevents-api:1.3.0 | |
import io.cloudevents.CloudEvent; | |
import io.cloudevents.v02.CloudEventBuilder; | |
import io.cloudevents.v1.CloudEventImpl; | |
import io.cloudevents.json.Json; | |
import com.fasterxml.jackson.core.type.TypeReference; | |
import java.net.URI; | |
class CloudEventTest { | |
public String something; | |
public static void main(String[] args) { | |
CloudEventTest myEvent = new CloudEventTest(); | |
myEvent.something = "Something"; | |
CloudEvent event = CloudEventBuilder.<CloudEventTest>builder() | |
.withId("hello") | |
.withType("example") | |
.withSource(URI.create("http://localhost")) | |
.withData(myEvent) | |
.build(); | |
String json = Json.encode(event); | |
System.out.println(json); | |
CloudEventImpl<CloudEventTest> decoded = Json.decodeValue(json, new TypeReference<CloudEventImpl<CloudEventTest>>() {}); | |
System.out.println(decoded.getData().get().something); | |
System.out.println(decoded.getAttributes().getSource()); | |
} | |
} | |
// prints | |
//{"data":{"something":"Something"},"id":"hello","source":"http://localhost","specversion":"0.2","type":"example"} | |
//Something | |
// http://localhost | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment