javax.json.bind.config.PropertyOrderStrategy Java Examples

The following examples show how to use javax.json.bind.config.PropertyOrderStrategy. 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: RecordImpl.java    From component-runtime with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: JsonbUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@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 #3
Source File: JsonBindingExample.java    From Java-EE-8-Sampler with MIT License 5 votes vote down vote up
public String customizedMapping() {

        JsonbConfig jsonbConfig = new JsonbConfig()
                .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES)
                .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL)
                .withStrictIJSON(true)
                .withFormatting(true)
                .withNullValues(true);

        Jsonb jsonb = JsonbBuilder.create(jsonbConfig);

        return jsonb.toJson(book1);
    }
 
Example #4
Source File: JsonBindingExample.java    From Java-EE-8-Sampler with MIT License 5 votes vote down vote up
public String allCustomizedMapping() {

        PropertyVisibilityStrategy vis = new PropertyVisibilityStrategy() {
            @Override
            public boolean isVisible(Field field) {
                return false;
            }

            @Override
            public boolean isVisible(Method method) {
                return false;
            }
        };

        JsonbConfig jsonbConfig = new JsonbConfig()
                .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES)
                .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL)
                .withPropertyVisibilityStrategy(vis)
                .withStrictIJSON(true)
                .withFormatting(true)
                .withNullValues(true)
                .withBinaryDataStrategy(BinaryDataStrategy.BASE_64_URL)
                .withDateFormat("MM/dd/yyyy", Locale.ENGLISH);

        Jsonb jsonb = JsonbBuilder.create(jsonbConfig);

        return jsonb.toJson(book1);
    }
 
Example #5
Source File: CustomisePropertyOrderStrategyTest.java    From Java-EE-8-Sampler with MIT License 5 votes vote down vote up
@Test
public void givenLEXICOGRAPHICALPropertyOrderStrategy_shouldOrderLexicographically(){

    String expectedJson = "{\"alternativeTitle\":\"01846537\",\"authorName\":{\"firstName\":\"Alex\",\"lastName\":\"Theedom\"},\"title\":\"Fun with JSON binding\"}";
    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL);

    String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine);

    assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example #6
Source File: CustomisePropertyOrderStrategyTest.java    From Java-EE-8-Sampler with MIT License 5 votes vote down vote up
@Test
public void givenANYPropertyOrderStrategy_shouldOrderLexicographically(){

    String expectedJson = "{\"authorName\":{\"firstName\":\"Alex\",\"lastName\":\"Theedom\"},\"alternativeTitle\":\"01846537\",\"title\":\"Fun with JSON binding\"}";
    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyOrderStrategy(PropertyOrderStrategy.ANY);

    String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine);

    assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example #7
Source File: CustomisePropertyOrderStrategyTest.java    From Java-EE-8-Sampler with MIT License 5 votes vote down vote up
@Test
public void givenREVERSEPropertyOrderStrategy_shouldOrderLexicographically(){

    String expectedJson = "{\"title\":\"Fun with JSON binding\",\"authorName\":{\"lastName\":\"Theedom\",\"firstName\":\"Alex\"},\"alternativeTitle\":\"01846537\"}";
    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE);

    String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine);

    assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example #8
Source File: InitTestInfra.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(final Configuration builder) {
    builder.setJsonbOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL);

    if (Boolean.getBoolean("org.talend.sdk.component.server.test.InitTestInfra.skip")) {
        return;
    }
    Locale.setDefault(Locale.ENGLISH);
    builder.setJsonbPrettify(true);
    builder
            .setTempDir(new File(jarLocation(InitTestInfra.class).getParentFile(), getClass().getSimpleName())
                    .getAbsolutePath());
    System.setProperty("component.manager.classpath.skip", "true");
    System.setProperty("component.manager.callers.skip", "true");
    System.setProperty("talend.component.server.maven.repository", createM2(builder.getTempDir()));
    System
            .setProperty("talend.component.server.component.documentation.translations",
                    createI18nDocRepo(builder.getTempDir()));
    System.setProperty("talend.component.server.user.extensions.location", createUserJars(builder.getTempDir()));
    System
            .setProperty("talend.component.server.icon.paths",
                    "icons/%s.svg,icons/svg/%s.svg,%s.svg,%s_icon32.png,icons/%s_icon32.png,icons/png/%s_icon32.png");
    System.setProperty("talend.component.server.locale.mapping", "en*=en\nfr*=fr\ntest=test");
    System.setProperty("talend.component.server.gridlayout.translation.support", "true");

    final String skipLogs = System.getProperty("component.server.test.logging.skip", "true");
    System.setProperty("talend.component.server.request.log", Boolean.toString("false".equals(skipLogs)));
}
 
Example #9
Source File: UiSpecMapperImplTest.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@Test
void createForm() {
    final Supplier<Ui> form =
            new UiSpecMapperImpl(new UiSpecMapperImpl.Configuration(singletonList(new TitleMapProvider() {

                private int it = 0;

                @Override
                public String reference() {
                    return "vendors";
                }

                @Override
                public Collection<UiSchema.NameValue> get() {
                    return asList(new UiSchema.NameValue.Builder().withName("k" + ++it).withValue("v" + it).build(),
                            new UiSchema.NameValue.Builder().withName("k" + ++it).withValue("v" + it).build());
                }
            }))).createFormFor(ComponentModel.class);

    IntStream.of(1, 2).forEach(it -> {
        try (final Jsonb jsonb = JsonbBuilder
                .create(new JsonbConfig()
                        .withFormatting(true)
                        .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL));
                final BufferedReader reader =
                        new BufferedReader(new InputStreamReader(
                                Thread
                                        .currentThread()
                                        .getContextClassLoader()
                                        .getResourceAsStream("component-model-" + it + ".json"),
                                StandardCharsets.UTF_8))) {
            assertEquals(reader.lines().collect(joining("\n")), jsonb.toJson(form.get()));
        } catch (final Exception e) {
            throw new IllegalStateException(e);
        }
    });
}
 
Example #10
Source File: JsonSchemaSerializationTest.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@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 #11
Source File: DefaultResponseLocator.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
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 #12
Source File: RecordConvertersTest.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@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 #13
Source File: RecordConvertersTest.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@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 #14
Source File: Generator.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
private static Jsonb newJsonb() {
    return JsonbBuilder
            .create(new JsonbConfig()
                    .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL)
                    .withFormatting(true));
}
 
Example #15
Source File: JsonbFactory.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Produces
@ComponentServer
@ApplicationScoped
Jsonb jsonb() {
    return JsonbBuilder.create(new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL));
}
 
Example #16
Source File: PropertyOrderStrategyTest.java    From Java-EE-8-Sampler with MIT License 3 votes vote down vote up
@Test
public void givenPropertyOrderStrategy_shouldSerialise() {

    JsonbConfig jsonbConfig = new JsonbConfig()
            .withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE);

    String json = JsonbBuilder.create(jsonbConfig).toJson(new Book());

    assertThat(json).isEqualTo("{\"authorName\":\"Alex Theedom\",\"bookPrice\":19.99,\"bookTitle\":\"Fun with JSON Binding\"}");

}