EscEvent implements ToJsonCapable { @Override public final JsonObject toJson() { return Json.createObjectBuilder().add(EL_EVENT_ID, eventId).add(EL_EVENT_TYPE, eventType).add(EL_DATA, data.toJson()) .add(EL_META_DATA, meta.toJson()).build(); } protected  EscEvent();  EscEvent(@NotNull final UUID eventId, @NotNull final String eventType, @NotNull final DataWrapper data);  EscEvent(@NotNull final UUID eventId, @NotNull final String eventType, @NotNull final DataWrapper data,
            @Nullable final DataWrapper meta); final String getEventId(); final String getEventType(); final DataWrapper getData(); final DataWrapper getMeta(); @Override final JsonObject toJson(); static EscEvent create(final JsonObject jsonObj); static final String EL_ROOT_NAME; static final TypeName TYPE; static final SerializedDataType SER_TYPE; }
@Test public final void testToJson() throws Exception { final String expectedJson = IOUtils.toString(this.getClass().getResourceAsStream("/esc-event.json")); final UUID eventId = UUID.fromString("b2a936ce-d479-414f-b67f-3df4da383d47"); final MyEvent myEvent = new MyEvent(UUID.fromString("b2a936ce-d479-414f-b67f-3df4da383d47"), "Hello, JSON!"); final MyMeta myMeta = new MyMeta("abc"); final EnhancedMimeType dataContentType = new EnhancedMimeType("application", "json", Charset.forName("utf-8"), "1"); final EnhancedMimeType metaContentType = new EnhancedMimeType("application", "json", Charset.forName("utf-8"), "1"); final EscMeta escMeta = new EscMeta(MyEvent.SER_TYPE.asBaseType(), dataContentType, MyMeta.SER_TYPE.asBaseType(), metaContentType, myMeta); final DataWrapper dataWrapper = new DataWrapper(myEvent); final DataWrapper metaWrapper = new DataWrapper(escMeta); final EscEvent event = new EscEvent(eventId, MyEvent.TYPE.asBaseType(), dataWrapper, metaWrapper); final StringWriter sw = new StringWriter(); Json.createWriter(sw).writeObject(event.toJson()); final String actualJson = sw.toString(); assertThatJson(expectedJson).isEqualTo(actualJson); }
@Test public void testMarshalJsonBBase64() throws Exception { final String expectedJson = IOUtils.toString(this.getClass().getResourceAsStream("/esc-event-base64.json")); final SimpleSerializedDataTypeRegistry typeRegistry = new SimpleSerializedDataTypeRegistry(); typeRegistry.add(MyEvent.SER_TYPE, MyEvent.class); typeRegistry.add(MyMeta.SER_TYPE, MyMeta.class); final EscEvent event = createEventBase64(); final JsonbDeSerializer jsonbDeSer = JsonbDeSerializer.builder() .withSerializers(EscSpiUtils.createEscJsonbSerializers()) .withDeserializers(EscSpiUtils.createEscJsonbDeserializers()) .withPropertyVisibilityStrategy(new FieldAccessStrategy()) .withEncoding(Charset.forName("utf-8")) .build(); initSerDeserializerRegistry(typeRegistry, jsonbDeSer); try (final Jsonb jsonb = jsonbDeSer.getJsonb()) { final String currentJson = jsonb.toJson(event); assertThatJson(currentJson).isEqualTo(expectedJson); } }