Repository: johnzon Updated Branches: refs/heads/master 3cb484c17 -> 12b25ba79
JOHNZON-118 fixing JsonbPropertyOrder handling Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/12b25ba7 Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/12b25ba7 Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/12b25ba7 Branch: refs/heads/master Commit: 12b25ba79bd7113099d79aa0a102879d46f3a34f Parents: 3cb484c Author: rmannibucau <[email protected]> Authored: Thu May 11 18:15:10 2017 +0200 Committer: rmannibucau <[email protected]> Committed: Thu May 11 18:15:10 2017 +0200 ---------------------------------------------------------------------- .../apache/johnzon/jsonb/JsonbAccessMode.java | 20 ++++- .../johnzon/jsonb/AnnotationOrderTest.java | 77 ++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/johnzon/blob/12b25ba7/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java ---------------------------------------------------------------------- diff --git a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java index 28c7211..c785f03 100644 --- a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java +++ b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbAccessMode.java @@ -612,10 +612,28 @@ public class JsonbAccessMode implements AccessMode, Closeable { 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; @@ -629,7 +647,7 @@ public class JsonbAccessMode implements AccessMode, Closeable { keyComparator = String::compareTo; break; case PropertyOrderStrategy.REVERSE: - keyComparator = (o1, o2) -> o2.compareTo(o1); + keyComparator = Comparator.reverseOrder(); break; default: keyComparator = null; http://git-wip-us.apache.org/repos/asf/johnzon/blob/12b25ba7/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/AnnotationOrderTest.java ---------------------------------------------------------------------- diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/AnnotationOrderTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/AnnotationOrderTest.java new file mode 100644 index 0000000..3c75869 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/AnnotationOrderTest.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.johnzon.jsonb; + +import org.junit.Test; + +import javax.json.bind.JsonbBuilder; +import javax.json.bind.JsonbConfig; +import javax.json.bind.annotation.JsonbPropertyOrder; +import javax.json.bind.config.PropertyNamingStrategy; + +import static org.junit.Assert.assertEquals; + +public class AnnotationOrderTest { + /** + * Test that @JsonbPropertyOrder takes java names and pushes at the end not mentionned properties. + */ + @Test + public void run() { + final Person p = new Person(); + p.setPersonAge(12); + p.setPersonName("David"); + p.setPersonGender("Male"); + assertEquals( + "{\"person_gender\":\"Male\",\"person_name\":\"David\",\"person_age\":12}", + JsonbBuilder.create(new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES)) + .toJson(p)); + } + + @JsonbPropertyOrder({"personGender", "personName"}) + public class Person { + private String personName; + private int personAge; + private String personGender; + + public String getPersonName() { + return personName; + } + + public void setPersonName(String name) { + this.personName = name; + } + + public int getPersonAge() { + return personAge; + } + + public void setPersonAge(int age) { + this.personAge = age; + } + + public String getPersonGender() { + return personGender; + } + + public void setPersonGender(String personGender) { + this.personGender = personGender; + } + } + +}
