EscMeta implements ToJsonCapable { public JsonObject toJson() { final JsonObjectBuilder builder = Json.createObjectBuilder(); builder.add(EL_DATA_TYPE, dataType); builder.add(EL_DATA_CONTENT_TYPE, dataContentTypeStr); if (meta == null) { return builder.build(); } builder.add(EL_META_TYPE, metaType); builder.add(EL_META_CONTENT_TYPE, metaContentTypeStr); if (meta instanceof JsonObject) { final JsonObject jo = (JsonObject) meta; return builder.add(metaType, jo).build(); } if (meta instanceof ToJsonCapable) { final ToJsonCapable tjc = (ToJsonCapable) meta; return builder.add(metaType, tjc.toJson()).build(); } if (meta instanceof Base64Data) { final Base64Data base64data = (Base64Data) meta; return builder.add(metaType, base64data.getEncoded()).build(); } throw new IllegalStateException("Unknown meta object type: " + meta.getClass()); } protected  EscMeta();  EscMeta(@NotNull final String dataType, @NotNull final EnhancedMimeType dataContentType);  EscMeta(@NotNull final String dataType, @NotNull final EnhancedMimeType dataContentType, @Nullable final String metaType,
            @Nullable final EnhancedMimeType metaContentType, @Nullable final Object meta); @Nullable final String getDataType(); @NotNull final EnhancedMimeType getDataContentType(); @Nullable final String getMetaType(); @Nullable final EnhancedMimeType getMetaContentType(); @NotNull final Object getMeta(); JsonObject toJson(); static EscMeta 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-meta.json")); final EscMeta testee = EscMeta.create(Json.createReader(new StringReader(expectedJson)).readObject()); final StringWriter sw = new StringWriter(); Json.createWriter(sw).writeObject(testee.toJson()); final String actualJson = sw.toString(); assertThatJson(expectedJson).isEqualTo(actualJson); }
@Test public final void testMarshalJsonB() throws Exception { final String expectedJson = IOUtils.toString(this.getClass().getResourceAsStream("/esc-meta.json")); final MyMeta myMeta = new MyMeta("abc"); final Map<String, String> params = new HashMap<>(); params.put("transfer-encoding", "base64"); final EnhancedMimeType dataContentType = new EnhancedMimeType("application", "xml", Charset.forName("utf-8"), "1", params); 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 SimpleSerializedDataTypeRegistry typeRegistry = new SimpleSerializedDataTypeRegistry(); typeRegistry.add(EscMeta.SER_TYPE, EscMeta.class); final JsonbDeSerializer jsonbDeSer = JsonbDeSerializer.builder() .withSerializers(EscSpiUtils.createEscJsonbSerializers()) .withPropertyVisibilityStrategy(new FieldAccessStrategy()) .withEncoding(Charset.forName("utf-8")) .build(); jsonbDeSer.init(typeRegistry); try (final Jsonb jsonb = jsonbDeSer.getJsonb()) { final String currentJson = jsonb.toJson(escMeta); assertThatJson(currentJson).isEqualTo(expectedJson); } }