Java 类javax.json.bind.config.PropertyOrderStrategy 实例源码

项目:yasson    文件:JsonbConfigProperties.java   
private PropOrderStrategy initOrderStrategy() {
    final Optional<Object> property = jsonbConfig.getProperty(JsonbConfig.PROPERTY_ORDER_STRATEGY);
    if (property.isPresent()) {
        final Object strategy = property.get();
        if (!(strategy instanceof String)) {
            throw new JsonbException(Messages.getMessage(MessageKeys.PROPERTY_ORDER, strategy));
        }
        switch ((String) strategy) {
            case PropertyOrderStrategy.LEXICOGRAPHICAL:
                return new LexicographicalOrderStrategy();
            case PropertyOrderStrategy.REVERSE:
                return new ReverseOrderStrategy();
            case PropertyOrderStrategy.ANY:
                return new AnyOrderStrategy();
            default:
                throw new JsonbException(Messages.getMessage(MessageKeys.PROPERTY_ORDER, strategy));
        }
    }
    //default by spec
    return new LexicographicalOrderStrategy();
}
项目:JSON-framework-comparison    文件:RuntimeSampler.java   
public static JsonbConfig jsonbConfig() {
    return new JsonbConfig()

            // Property visibility
            .withNullValues(true)
            .withPropertyVisibilityStrategy(new PropertyVisibilityStrategy() {
                @Override
                public boolean isVisible(Field field) {
                    return false;
                }

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

            // Property naming and order
            .withPropertyNamingStrategy(PropertyNamingStrategy.CASE_INSENSITIVE)
            .withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE)

            // Customised de/serializers
            .withAdapters(new ClassAdapter())
            .withDeserializers(new CustomDeserializer())
            .withSerializers(new CustomSerializer())

            // Formats, locals, encoding, binary data
            .withBinaryDataStrategy(BinaryDataStrategy.BASE_64_URL)
            .withDateFormat("MM/dd/yyyy", Locale.ENGLISH)
            .withLocale(Locale.CANADA)
            .withEncoding("UTF-8")
            .withStrictIJSON(true)
            .withFormatting(true);
}
项目:Java-EE-8-Sampler    文件:JsonBindingExample.java   
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);
    }
项目:Java-EE-8-Sampler    文件:JsonBindingExample.java   
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);
    }
项目:Java-EE-8-Sampler    文件:CustomisePropertyOrderStrategyTest.java   
@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);
}
项目:Java-EE-8-Sampler    文件:CustomisePropertyOrderStrategyTest.java   
@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);
}
项目:Java-EE-8-Sampler    文件:CustomisePropertyOrderStrategyTest.java   
@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);
}
项目:yasson    文件:PropertyOrderTest.java   
@Test
public void testPropertySorting() {
    FieldOrder fieldOrder = new FieldOrder();
    Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL));
    String expectedLexicographical = "{\"aField\":\"aValue\",\"bField\":\"bValue\",\"cField\":\"cValue\",\"dField\":\"dValue\"}";
    assertEquals(expectedLexicographical, jsonb.toJson(fieldOrder));

    jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE));
    String expectedReverse = "{\"dField\":\"dValue\",\"cField\":\"cValue\",\"bField\":\"bValue\",\"aField\":\"aValue\"}";
    assertEquals(expectedReverse, jsonb.toJson(fieldOrder));
}
项目:yasson    文件:PropertyOrderTest.java   
@Test
public void testPropertyCustomOrder() {
    FieldCustomOrder fieldCustomOrder = new FieldCustomOrder();
    Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL));
    String expectedCustomOrder = "{\"aField\":\"aValue\",\"cField\":\"cValue\",\"dField\":\"dValue\",\"bField\":\"bValue\"}";
    assertEquals(expectedCustomOrder, jsonb.toJson(fieldCustomOrder));

    FieldCustomOrderWrapper fieldCustomOrderWrapper = new FieldCustomOrderWrapper();
    String expectedOrder = "{\"fieldCustomOrder\":{\"aField\":\"aValue\",\"cField\":\"cValue\",\"dField\":\"dValue\",\"bField\":\"bValue\"},\"intField\":1,\"stringField\":\"stringValue\"}";
    assertEquals(expectedOrder, jsonb.toJson(fieldCustomOrderWrapper));
}
项目:yasson    文件:PropertyOrderTest.java   
@Test
public void testPropertySortingWithNamingAnnotation() {
    FieldOrderNameAnnotation fieldOrderNameAnnotation = new FieldOrderNameAnnotation();
    Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL));
    String expectedLexicographical = "{\"bField\":\"bValue\",\"cField\":\"cValue\",\"dField\":\"dValue\",\"zField\":\"aValue\"}";
    assertEquals(expectedLexicographical, jsonb.toJson(fieldOrderNameAnnotation));

    jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE));
    String expectedReverse = "{\"zField\":\"aValue\",\"dField\":\"dValue\",\"cField\":\"cValue\",\"bField\":\"bValue\"}";
    assertEquals(expectedReverse, jsonb.toJson(fieldOrderNameAnnotation));
}
项目:yasson    文件:PropertyOrderTest.java   
@Test
public void testLexicographicalPropertyOrderRenamedProperties() {
    JsonbConfig config = new JsonbConfig();
    config.setProperty(JsonbConfig.PROPERTY_ORDER_STRATEGY, PropertyOrderStrategy.LEXICOGRAPHICAL);
    Jsonb jsonb = JsonbBuilder.create(config);

    String jsonString = jsonb.toJson(new RenamedPropertiesContainer() {{ setStringInstance("Test String"); setLongInstance(1); }});
    Assert.assertTrue(jsonString.matches("\\{\\s*\"first\"\\s*\\:\\s*0\\s*,\\s*\"second\"\\s*\\:\\s*\"Test String\"\\s*,\\s*\"third\"\\s*\\:\\s*1\\s*\\}"));

    RenamedPropertiesContainer unmarshalledObject = jsonb.fromJson("{ \"first\" : 1, \"second\" : \"Test String\", \"third\" : 1 }", RenamedPropertiesContainer.class);
    Assert.assertEquals(3, unmarshalledObject.getIntInstance());
}
项目:johnzon    文件:AdapterTest.java   
@Test
public void notYetPloymorphism() { // we run it since it checked list/item conversion
    final Bar bar = new Bar();
    bar.value = 11;

    final Bar2 bar2 = new Bar2();
    bar2.value = 21;
    bar2.value2 = 22;

    final Polymorphism foo = new Polymorphism();
    foo.bars = new ArrayList<>(asList(bar, bar2));

    final Jsonb jsonb = JsonbBuilder.create(
            new JsonbConfig()
                    .setProperty("johnzon.readAttributeBeforeWrite", true)
                    .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL) /* assertEquals() order */);

    final String toString = jsonb.toJson(foo);
    assertEquals("{\"bars\":[" +
            "{\"type\":\"org.apache.johnzon.jsonb.AdapterTest$Bar\",\"value\":{\"value\":11}}," +
            "{\"type\":\"org.apache.johnzon.jsonb.AdapterTest$Bar2\",\"value\":{\"value\":21,\"value2\":22}}]}", toString);

    final Polymorphism read = jsonb.fromJson(toString, Polymorphism.class);
    assertEquals(2, read.bars.size());
    assertEquals(11, read.bars.get(0).value);
    assertTrue(Bar.class == read.bars.get(0).getClass());
    assertEquals(21, read.bars.get(1).value);
    assertTrue(Bar2.class == read.bars.get(1).getClass());
    assertEquals(22, Bar2.class.cast(read.bars.get(1)).value2);
}
项目:johnzon    文件:JsonbAccessMode.java   
private Comparator<String> orderComparator(final Class<?> clazz) {
    final Comparator<String> keyComparator;
    final JsonbPropertyOrder orderAnnotation = Meta.getAnnotation(clazz, JsonbPropertyOrder.class);
    if (orderAnnotation != null) {
        final List<String> indexed = new ArrayList<>(asList(orderAnnotation.value()));
        if (naming != null) { // JsonbPropertyOrder applies on java names
            for (int i = 0; i < indexed.size(); i++) {
                indexed.set(i, naming.translateName(indexed.get(i)));
            }
        }
        keyComparator = (o1, o2) -> {
            final int i1 = indexed.indexOf(o1);
            final int i2 = indexed.indexOf(o2);
            if (i1 < 0) {
                if (i2 < 0) {
                    if (order != null) {
                        switch (order) {
                            case PropertyOrderStrategy.LEXICOGRAPHICAL:
                                return o1.compareTo(o2);
                            case PropertyOrderStrategy.REVERSE:
                                return o2.compareTo(o1);
                            case PropertyOrderStrategy.ANY:
                            default:
                                return 1;
                        }
                    }
                }
                return 1;
            }
            return i1 - i2;
        };
    } else if (order != null) {
        switch (order) {
            case PropertyOrderStrategy.ANY:
                keyComparator = null;
                break;
            case PropertyOrderStrategy.LEXICOGRAPHICAL:
                keyComparator = String::compareTo;
                break;
            case PropertyOrderStrategy.REVERSE:
                keyComparator = Comparator.reverseOrder();
                break;
            default:
                keyComparator = null;
        }
    } else {
        keyComparator = null;
    }
    return keyComparator;
}
项目:Java-EE-8-Sampler    文件:PropertyOrderStrategyTest.java   
@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\"}");

}