javax.json.bind.JsonbConfig Java Examples
The following examples show how to use
javax.json.bind.JsonbConfig.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EventSerializer.java From scalable-coffee-shop with Apache License 2.0 | 6 votes |
@Override public byte[] serialize(final String topic, final CoffeeEvent event) { try { if (event == null) return null; final JsonbConfig config = new JsonbConfig() .withAdapters(new UUIDAdapter()) .withSerializers(new EventJsonbSerializer()); final Jsonb jsonb = JsonbBuilder.create(config); return jsonb.toJson(event, CoffeeEvent.class).getBytes(StandardCharsets.UTF_8); } catch (Exception e) { logger.severe("Could not serialize event: " + e.getMessage()); throw new SerializationException("Could not serialize event", e); } }
Example #2
Source File: JsonbUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void givenPersonObject_whenNamingStrategy_thenGetCustomPersonJson() { JsonbConfig config = new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES); Jsonb jsonb = JsonbBuilder.create(config); Person person = new Person(1, "Jhon", "[email protected]", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000)); String jsonPerson = jsonb.toJson(person); // @formatter:off String jsonExpected = "{\"email\":\"[email protected]\"," + "\"id\":1," + "\"person-name\":\"Jhon\"," + "\"registered_date\":\"07-09-2019\"," + "\"salary\":\"1000.0\"}"; // @formatter:on assertTrue(jsonExpected.equals(jsonPerson)); }
Example #3
Source File: TestResource.java From quarkus with Apache License 2.0 | 6 votes |
@GET @Path("/from-json") @Produces("application/json") public MyEntity fromJson() throws Exception { MyEntity entity = new MyEntity(); entity.name = "my entity name"; entity.value = "my entity value"; JsonbConfig config = new JsonbConfig(); try (Jsonb jsonb = JsonbBuilder.create(config)) { String json = jsonb.toJson(entity); MyEntity fromJsonEntity = jsonb.fromJson(json, MyEntity.class); return fromJsonEntity; } }
Example #4
Source File: RecordImpl.java From component-runtime with Apache License 2.0 | 6 votes |
@Override // for debug purposes, don't use it for anything else public String toString() { try (final Jsonb jsonb = JsonbBuilder .create(new JsonbConfig() .withFormatting(true) .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL) .setProperty("johnzon.cdi.activated", false))) { return new RecordConverters() .toType(new RecordConverters.MappingMetaRegistry(), this, JsonObject.class, () -> Json.createBuilderFactory(emptyMap()), JsonProvider::provider, () -> jsonb, () -> new RecordBuilderFactoryImpl("tostring")) .toString(); } catch (final Exception e) { return super.toString(); } }
Example #5
Source File: LoopState.java From component-runtime with Apache License 2.0 | 6 votes |
private Record toRecord(final Object value) { if (recordConverters == null) { synchronized (this) { if (recordConverters == null) { final ComponentManager manager = ComponentManager.instance(); jsonb = manager .getJsonbProvider() .create() .withProvider(manager.getJsonpProvider()) .withConfig(new JsonbConfig().setProperty("johnzon.cdi.activated", false)) .build(); recordConverters = new RecordConverters(); registry = new RecordConverters.MappingMetaRegistry(); recordBuilderFactory = manager.getRecordBuilderFactoryProvider().apply(null); } } } return recordConverters.toRecord(registry, value, () -> jsonb, () -> recordBuilderFactory); }
Example #6
Source File: JoinInputFactory.java From component-runtime with Apache License 2.0 | 6 votes |
private Object map(final Object next) { if (next == null || Record.class.isInstance(next)) { // directly ok return next; } if (String.class.isInstance(next) || next.getClass().isPrimitive()) { // primitives return next; } if (jsonb == null) { synchronized (this) { if (jsonb == null) { jsonb = JsonbBuilder.create(new JsonbConfig().setProperty("johnzon.cdi.activated", false)); registry = new RecordConverters.MappingMetaRegistry(); } } } return new RecordConverters() .toRecord(registry, next, () -> jsonb, () -> ComponentManager.instance().getRecordBuilderFactoryProvider().apply(null)); }
Example #7
Source File: EventSerializer.java From scalable-coffee-shop with Apache License 2.0 | 6 votes |
@Override public byte[] serialize(final String topic, final CoffeeEvent event) { try { if (event == null) return null; final JsonbConfig config = new JsonbConfig() .withAdapters(new UUIDAdapter()) .withSerializers(new EventJsonbSerializer()); final Jsonb jsonb = JsonbBuilder.create(config); return jsonb.toJson(event, CoffeeEvent.class).getBytes(StandardCharsets.UTF_8); } catch (Exception e) { logger.severe("Could not serialize event: " + e.getMessage()); throw new SerializationException("Could not serialize event", e); } }
Example #8
Source File: MyJsonBContextResolver.java From microprofile-rest-client with Apache License 2.0 | 6 votes |
@Override public Jsonb getContext(Class<?> type) { JsonbConfig config = new JsonbConfig(). withPropertyVisibilityStrategy(new PropertyVisibilityStrategy(){ @Override public boolean isVisible(Field f) { return true; } @Override public boolean isVisible(Method m) { return false; } }); return JsonbBuilder.newBuilder(). withConfig(config). build(); }
Example #9
Source File: EventSerializer.java From scalable-coffee-shop with Apache License 2.0 | 6 votes |
@Override public byte[] serialize(final String topic, final CoffeeEvent event) { try { if (event == null) return null; final JsonbConfig config = new JsonbConfig() .withAdapters(new UUIDAdapter()) .withSerializers(new EventJsonbSerializer()); final Jsonb jsonb = JsonbBuilder.create(config); return jsonb.toJson(event, CoffeeEvent.class).getBytes(StandardCharsets.UTF_8); } catch (Exception e) { logger.severe("Could not serialize event: " + e.getMessage()); throw new SerializationException("Could not serialize event", e); } }
Example #10
Source File: JsonbUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void givenPersonObject_whenWithPropertyOrderStrategy_thenGetReversePersonJson() { JsonbConfig config = new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE); Jsonb jsonb = JsonbBuilder.create(config); Person person = new Person(1, "Jhon", "[email protected]", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000)); String jsonPerson = jsonb.toJson(person); // @formatter:off String jsonExpected = "{\"salary\":\"1000.0\","+ "\"registeredDate\":\"07-09-2019\"," + "\"person-name\":\"Jhon\"," + "\"id\":1," + "\"email\":\"[email protected]\"}"; // @formatter:on assertTrue(jsonExpected.equals(jsonPerson)); }
Example #11
Source File: MagazineSerializerTest.java From Java-EE-8-Sampler with MIT License | 6 votes |
@Test public void givenSerialize_shouldSerialiseMagazine() { Magazine magazine = new Magazine(); magazine.setId("1234-QWERT"); magazine.setTitle("Fun with Java"); magazine.setAuthor(new Author("Alex","Theedom")); String expectedJson = "{\"name\":{\"firstName\":\"Alex\",\"lastName\":\"Theedom\"}}"; JsonbConfig config = new JsonbConfig().withSerializers(new MagazineSerializer()); Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(config).build(); String actualJson = jsonb.toJson(magazine); assertThat(actualJson).isEqualTo(expectedJson); }
Example #12
Source File: CustomisePropertyNamingStrategyTest.java From Java-EE-8-Sampler with MIT License | 6 votes |
@Test public void givenIDENTITYStrategy_shouldNotChangeAnyPropertyName() { /* { "alternativetitle": "Fun with JSON-B", "authorName": { "firstName": "Alex", "lastName": "Theedom" }, "title": "Fun with JSON binding" } */ String expectedJson = "{\"alternativetitle\":\"Fun with JSON-B\",\"authorName\":{\"firstName\":\"Alex\",\"lastName\":\"Theedom\"},\"title\":\"Fun with JSON binding\"}"; JsonbConfig jsonbConfig = new JsonbConfig() .withPropertyNamingStrategy(PropertyNamingStrategy.IDENTITY); String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine); assertThat(actualJson).isEqualTo(expectedJson); }
Example #13
Source File: CustomisePropertyNamingStrategyTest.java From Java-EE-8-Sampler with MIT License | 6 votes |
@Test public void givenLOWER_CASE_WITH_DASHESStrategy_shouldDelimitPropertyNameWithDashes() { /* { "alternativetitle": "Fun with JSON-B", "author-name": { "first-name": "Alex", "last-name": "Theedom" }, "title": "Fun with JSON binding" } */ String expectedJson = "{\"alternativetitle\":\"Fun with JSON-B\",\"author-name\":{\"first-name\":\"Alex\",\"last-name\":\"Theedom\"},\"title\":\"Fun with JSON binding\"}"; JsonbConfig jsonbConfig = new JsonbConfig() .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES); String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine); assertThat(actualJson).isEqualTo(expectedJson); }
Example #14
Source File: CustomisePropertyNamingStrategyTest.java From Java-EE-8-Sampler with MIT License | 6 votes |
@Test public void givenLOWER_CASE_WITH_DASHESStrategy_shouldDeserialiseCorrectly() { /* { "alternativetitle": "Fun with JSON-B", "author-name": { "first-name": "Alex", "last-name": "Theedom" }, "title": "Fun with JSON binding" } */ String expectedJson = "{\"alternativetitle\":\"Fun with JSON-B\",\"author-name\":{\"first-name\":\"Alex\",\"last-name\":\"Theedom\"},\"title\":\"Fun with JSON binding\"}"; JsonbConfig jsonbConfig = new JsonbConfig() .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES); Magazine magazine = JsonbBuilder.create(jsonbConfig).fromJson(expectedJson, Magazine.class); assertThat(magazine.getAlternativetitle()).isEqualTo("Fun with JSON-B"); assertThat(magazine.getAuthorName().getFirstName()).isEqualTo("Alex"); assertThat(magazine.getAuthorName().getLastName()).isEqualTo("Theedom"); assertThat(magazine.getTitle()).isEqualTo("Fun with JSON binding"); }
Example #15
Source File: CustomisePropertyNamingStrategyTest.java From Java-EE-8-Sampler with MIT License | 6 votes |
@Test public void givenUPPER_CAMEL_CASEStrategy_shouldDelimitLowercasePropertyNameWithUnderscore() { /* { "Alternativetitle": "Fun with JSON-B", "AuthorName": { "FirstName": "Alex", "LastName": "Theedom" }, "Title": "Fun with JSON binding" } */ String expectedJson = "{\"Alternativetitle\":\"Fun with JSON-B\",\"AuthorName\":{\"FirstName\":\"Alex\",\"LastName\":\"Theedom\"},\"Title\":\"Fun with JSON binding\"}"; JsonbConfig jsonbConfig = new JsonbConfig() .withPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE); String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine); assertThat(actualJson).isEqualTo(expectedJson); }
Example #16
Source File: CustomisePropertyNamingStrategyTest.java From Java-EE-8-Sampler with MIT License | 6 votes |
@Test public void givenUPPER_CAMEL_CASE_WITH_SPACESStrategy_shouldDelimitLowercasePropertyNameWithUnderscore() { /* { "Alternativetitle": "Fun with JSON-B", "Author Name": { "First Name": "Alex", "Last Name": "Theedom" }, "Title": "Fun with JSON binding" } */ String expectedJson = "{\"Alternativetitle\":\"Fun with JSON-B\",\"Author Name\":{\"First Name\":\"Alex\",\"Last Name\":\"Theedom\"},\"Title\":\"Fun with JSON binding\"}"; JsonbConfig jsonbConfig = new JsonbConfig() .withPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE_WITH_SPACES); String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine); assertThat(actualJson).isEqualTo(expectedJson); }
Example #17
Source File: CustomisePropertyNamingStrategyTest.java From Java-EE-8-Sampler with MIT License | 6 votes |
@Test public void givenCASE_INSENSITIVEStrategy_shouldDelimitLowercasePropertyNameWithUnderscore() { /* { "alternativetitle": "Fun with JSON-B", "authorName": { "firstName": "Alex", "lastName": "Theedom" }, "title": "Fun with JSON binding" } */ String expectedJson = "{\"alternativetitle\":\"Fun with JSON-B\",\"authorName\":{\"firstName\":\"Alex\",\"lastName\":\"Theedom\"},\"title\":\"Fun with JSON binding\"}"; JsonbConfig jsonbConfig = new JsonbConfig() .withPropertyNamingStrategy(PropertyNamingStrategy.CASE_INSENSITIVE); String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine); assertThat(actualJson).isEqualTo(expectedJson); }
Example #18
Source File: MultipleFormatDateAdapterTest.java From component-runtime with Apache License 2.0 | 5 votes |
@Test void withJsonb() throws Exception { final ZonedDateTime zdtString = ZonedDateTime.ofInstant(new Date(0).toInstant(), ZoneId.of("UTC")); try (final Jsonb jsonb = JsonbBuilder.create(new JsonbConfig())) { final DateHolder holder = jsonb.fromJson("{\"date\":\"" + zdtString + "\"}", DateHolder.class); assertEquals(new Date(0).getTime(), holder.date.getTime()); } }
Example #19
Source File: ProcessorImpl.java From component-runtime with Apache License 2.0 | 5 votes |
private Jsonb jsonb() { if (jsonb == null) { synchronized (this) { if (jsonb == null) { jsonb = ContainerFinder.Instance.get().find(plugin()).findService(Jsonb.class); } if (jsonb == null) { // for tests mainly jsonb = JsonbBuilder.create(new JsonbConfig().withBinaryDataStrategy(BinaryDataStrategy.BASE_64)); } } } return jsonb; }
Example #20
Source File: JobImpl.java From component-runtime with Apache License 2.0 | 5 votes |
private Supplier<Jsonb> jsonb() { return () -> { if (jsonb == null) { synchronized (this) { if (jsonb == null) { jsonb = JsonbBuilder.create(new JsonbConfig().setProperty("johnzon.cdi.activated", false)); } } } return jsonb; }; }
Example #21
Source File: DefaultResponseLocator.java From component-runtime with Apache License 2.0 | 5 votes |
public DefaultResponseLocator(final String prefix, final String test) { this.prefix = prefix; this.test = test; this.jsonb = JsonbBuilder .create(new JsonbConfig() .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL) .withFormatting(true) .setProperty("johnzon.cdi.activated", false)); }
Example #22
Source File: RecordConvertersTest.java From component-runtime with Apache License 2.0 | 5 votes |
@Test void studioTypes(final JsonBuilderFactory jsonBuilderFactory, final JsonProvider jsonProvider, final RecordBuilderFactory recordBuilderFactory, final RecordConverters converter) throws Exception { final SimpleRowStruct record = new SimpleRowStruct(); record.character = 'a'; record.character2 = 'a'; record.notLong = 100; record.notLong2 = 100; record.binary = 100; record.binary2 = 100; record.bd = BigDecimal.TEN; record.today = new Date(0); try (final Jsonb jsonb = JsonbBuilder .create(new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL))) { final Record recordModel = Record.class .cast(converter .toType(new RecordConverters.MappingMetaRegistry(), record, Record.class, () -> jsonBuilderFactory, () -> jsonProvider, () -> jsonb, () -> recordBuilderFactory)); assertEquals( "{\"bd\":10.0,\"binary\":100.0,\"binary2\":100.0," + "\"character\":\"a\",\"character2\":\"a\"," + "\"notLong\":100.0,\"notLong2\":100.0," + "\"today\":\"1970-01-01T00:00:00Z[UTC]\"}", recordModel.toString()); final SimpleRowStruct deserialized = SimpleRowStruct.class .cast(converter .toType(new RecordConverters.MappingMetaRegistry(), recordModel, SimpleRowStruct.class, () -> jsonBuilderFactory, () -> jsonProvider, () -> jsonb, () -> recordBuilderFactory)); if (record.bd.doubleValue() == deserialized.bd.doubleValue()) { // equals fails on this one deserialized.bd = record.bd; } assertEquals(record, deserialized); } }
Example #23
Source File: JsonSchemaSerializationTest.java From component-runtime with Apache License 2.0 | 5 votes |
@Test void toJson() throws Exception { final Schema schema = new AvroSchemaBuilder() .withType(RECORD) .withEntry(new SchemaImpl.EntryImpl("array", "array", Schema.Type.ARRAY, true, null, new AvroSchemaBuilder().withType(STRING).build(), null)) .build(); try (final Jsonb jsonb = JsonbBuilder .create(new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL))) { assertEquals( "{\"entries\":[{\"elementSchema\":{\"entries\":[],\"type\":\"STRING\"},\"name\":\"array\",\"nullable\":true,\"rawName\":\"array\",\"type\":\"ARRAY\"}],\"type\":\"RECORD\"}", jsonb.toJson(schema)); } }
Example #24
Source File: NativeWrappedIOTest.java From component-runtime with Apache License 2.0 | 5 votes |
synchronized Jsonb jsonb() { if (jsonb == null) { jsonb = manager .getJsonbProvider() .create() .withProvider(new PreComputedJsonpProvider("test", manager.getJsonpProvider(), manager.getJsonpParserFactory(), manager.getJsonpWriterFactory(), manager.getJsonpBuilderFactory(), manager.getJsonpGeneratorFactory(), manager.getJsonpReaderFactory())) // reuses the same memory buffers .withConfig(new JsonbConfig().setProperty("johnzon.cdi.activated", false)) .build(); } return jsonb; }
Example #25
Source File: BaseComponentsHandler.java From component-runtime with Apache License 2.0 | 5 votes |
synchronized Jsonb jsonb() { if (jsonb == null) { jsonb = manager .getJsonbProvider() .create() .withProvider(new PreComputedJsonpProvider("test", manager.getJsonpProvider(), manager.getJsonpParserFactory(), manager.getJsonpWriterFactory(), manager.getJsonpBuilderFactory(), manager.getJsonpGeneratorFactory(), manager.getJsonpReaderFactory())) // reuses the same memory buffers .withConfig(new JsonbConfig().setProperty("johnzon.cdi.activated", false)) .build(); } return jsonb; }
Example #26
Source File: RecordConvertersTest.java From component-runtime with Apache License 2.0 | 5 votes |
@Test void studioTypes2(final JsonBuilderFactory jsonBuilderFactory, final JsonProvider jsonProvider, final RecordBuilderFactory recordBuilderFactory, final RecordConverters converter) throws Exception { final RowStruct rowStruct = new RowStruct(); rowStruct.col1int = 10; rowStruct.col2string = "stringy"; rowStruct.col3char = 'a'; rowStruct.col4bool = Boolean.TRUE; rowStruct.col5byte = 100; rowStruct.col6short = Short.MAX_VALUE; rowStruct.col7long = 1971L; rowStruct.col8float = 19.71f; rowStruct.col9double = 19.234; rowStruct.col10bigdec = java.math.BigDecimal.TEN; rowStruct.col11date = new java.util.Date(); try (final Jsonb jsonb = JsonbBuilder .create(new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL))) { final Record record = Record.class .cast(converter .toType(new RecordConverters.MappingMetaRegistry(), rowStruct, Record.class, () -> jsonBuilderFactory, () -> jsonProvider, () -> jsonb, () -> recordBuilderFactory)); final RowStruct deserialized = RowStruct.class .cast(converter .toType(new RecordConverters.MappingMetaRegistry(), record, RowStruct.class, () -> jsonBuilderFactory, () -> jsonProvider, () -> jsonb, () -> recordBuilderFactory)); if (rowStruct.col10bigdec.doubleValue() == deserialized.col10bigdec.doubleValue()) { deserialized.col10bigdec = rowStruct.col10bigdec; } if (rowStruct.col11date.equals(deserialized.col11date)) { deserialized.col11date = rowStruct.col11date; } assertEquals(rowStruct, deserialized); } }
Example #27
Source File: BookSerializerTest.java From Java-EE-8-Sampler with MIT License | 5 votes |
@Test public void givenSerialize_shouldSerialiseBook() { Book book = new Book("SHDUJ-4532", "Fun with Java", "Alex Theedom"); String expectedJson = "{\"id\":\"QWE-123-RTS\",\"title\":\"Fun with Java\",\"firstName\":\"Alex\",\"lastName\":\"Theedom\"}"; JsonbConfig config = new JsonbConfig().withSerializers(new BookSerializer()); Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(config).build(); String actualJson = jsonb.toJson(book); assertThat(actualJson).isEqualTo(expectedJson); }
Example #28
Source File: BookDeserializerTest.java From Java-EE-8-Sampler with MIT License | 5 votes |
@Test public void givenJSON_shouldDeserializeToBook() { String json = "{\"id\":\"QWE-123-RTS\",\"lastName\":\"Theedom\",\"firstName\":\"Alex\",\"title\":\"Fun with Java\"}"; JsonbConfig config = new JsonbConfig().withDeserializers(new BookDeserializer()); Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(config).build(); String id = jsonb.fromJson(json, String.class); assertThat(id).isEqualTo("QWE-123-RTS"); }
Example #29
Source File: CustomiseNullValuesTest.java From Java-EE-8-Sampler with MIT License | 5 votes |
@Test public void givenNullValuesCustomisation_shouldPreserverNulls(){ String expectedJson = "{\"author\":{\"firstName\":null,\"lastName\":null},\"title\":\"Fun with Java EE\"}"; Booklet booklet = new Booklet("Fun with Java EE", null, null); JsonbConfig jsonbConfig = new JsonbConfig().withNullValues(true); String actualJson = JsonbBuilder.create(jsonbConfig).toJson(booklet); assertThat(actualJson).isEqualTo(expectedJson); }
Example #30
Source File: MiscellaneousTest.java From Java-EE-8-Sampler with MIT License | 5 votes |
@Test public void prettyJsonFormat(){ JsonbConfig jsonbConfig = new JsonbConfig().withFormatting(true); Book book = new Book("ABC-123-XYZ", "Fun with JSON-B", "Alex Theedom"); String actualJson = JsonbBuilder.create(jsonbConfig).toJson(book); System.out.println(actualJson); }