Java源码示例:javax.json.bind.config.PropertyOrderStrategy
示例1
@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();
}
}
示例2
@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));
}
示例3
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);
}
示例4
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);
}
示例5
@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);
}
示例6
@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);
}
示例7
@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);
}
示例8
@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)));
}
示例9
@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);
}
});
}
示例10
@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));
}
}
示例11
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));
}
示例12
@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);
}
}
示例13
@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);
}
}
示例14
private static Jsonb newJsonb() {
return JsonbBuilder
.create(new JsonbConfig()
.withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL)
.withFormatting(true));
}
示例15
@Produces
@ComponentServer
@ApplicationScoped
Jsonb jsonb() {
return JsonbBuilder.create(new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL));
}
示例16
@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\"}");
}