Browse files

json keyed access enhancements

  • Loading branch information...
1 parent f2badba commit 6746809bc475549155cc21ec9a7e32c9dd52a884 @stoicflame stoicflame committed Jun 26, 2012
View
43 gedcomx-conclusion/src/main/java/org/gedcomx/conclusion/Person.java
@@ -25,6 +25,7 @@
import org.gedcomx.rt.json.JsonElementWrapper;
import org.gedcomx.types.FactType;
import org.gedcomx.types.IdentifierType;
+import org.gedcomx.types.NameType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@@ -172,6 +173,27 @@ public void setGender(Gender gender) {
}
/**
+ * Get the first name of the specified type.
+ *
+ * @param type The type.
+ * @return the first name in the name list of the specified type, or null if none.
+ */
+ @JsonIgnore
+ public Name getFirstNameOfType(NameType type) {
+ if (this.names == null) {
+ return null;
+ }
+
+ for (Name name : this.names) {
+ if (type.equals(name.getKnownType())) {
+ return name;
+ }
+ }
+
+ return null;
+ }
+
+ /**
* The name conclusions for the person.
*
* @param names The name conclusions for the person.
@@ -194,6 +216,27 @@ public void setNames(List<Name> names) {
}
/**
+ * Get the first fact of the specified type.
+ *
+ * @param type The type.
+ * @return the first fact in the fact list of the specified type, or null if none.
+ */
+ @JsonIgnore
+ public Fact getFirstFactOfType(FactType type) {
+ if (this.facts == null) {
+ return null;
+ }
+
+ for (Fact fact : this.facts) {
+ if (type.equals(fact.getKnownType())) {
+ return fact;
+ }
+ }
+
+ return null;
+ }
+
+ /**
* Helper method for obtaining specific fact conclusions.
*
* @param factType The type of facts to return.
View
4 gedcomx-conclusion/src/test/java/org/gedcomx/conclusion/PersonTest.java
@@ -171,7 +171,7 @@ static void assertEquals(Person person) {
AssertJUnit.assertEquals("pal", person.getIdentifiers().get(1).getValue());
AssertJUnit.assertEquals(2, person.getFacts().size());
- fact = person.getFacts().get(0);
+ fact = person.getFirstFactOfType(FactType.Occupation);
AssertJUnit.assertEquals("urn:fact-attribution", fact.getAttribution().getContributor().getResource().toString());
AssertJUnit.assertEquals("original date", fact.getDate().getOriginal());
AssertJUnit.assertEquals("normalized date", fact.getDate().getFormal().getText());
@@ -185,7 +185,7 @@ static void assertEquals(Person person) {
AssertJUnit.assertEquals("urn:date", fact.getDate().getFormal().getDatatype().toString());
AssertJUnit.assertEquals("fact-value", fact.getOriginal());
- event = person.getFacts().get(1);
+ event = person.getFirstFactOfType(FactType.Adoption);
AssertJUnit.assertEquals("urn:event-attribution", event.getAttribution().getContributor().getResource().toString());
AssertJUnit.assertEquals("original date", event.getDate().getOriginal());
AssertJUnit.assertEquals("normalized date", event.getDate().getFormal().getText());
View
8 gedcomx-rt-support/src/main/java/org/gedcomx/rt/json/KeyedListDeserializer.java
@@ -67,15 +67,19 @@ public KeyedListDeserializer(Class<?> itemType) {
}
else {
value = (HasJsonKey) jp.readValueAs(itemType);
- value.setJsonKey(key);
+ if (!KeyedListSerializer.JSON_DEFAULT_KEY.equals(key)) {
+ value.setJsonKey(key);
+ }
}
list.add(value);
}
}
else {
HasJsonKey value = (HasJsonKey) jp.readValueAs(itemType);
- value.setJsonKey(key);
+ if (!KeyedListSerializer.JSON_DEFAULT_KEY.equals(key)) {
+ value.setJsonKey(key);
+ }
list.add(value);
}
}
View
5 gedcomx-rt-support/src/main/java/org/gedcomx/rt/json/KeyedListSerializer.java
@@ -17,7 +17,6 @@
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
-import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
@@ -29,6 +28,8 @@
*/
public class KeyedListSerializer extends JsonSerializer<Collection<? extends HasUniqueJsonKey>> {
+ public static final String JSON_DEFAULT_KEY = "$";
+
@Override
public void serialize(Collection<? extends HasUniqueJsonKey> value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
serializeGeneric(value, jgen, provider);
@@ -44,7 +45,7 @@ static void serializeGeneric(Collection<?> value, JsonGenerator jgen, Serializer
for (Object keyed : value) {
String jsonKey = ((HasJsonKey) keyed).getJsonKey();
if (jsonKey == null) {
- throw new JsonMappingException("Extension element of type " + keyed.getClass().getName() + " returned a null JSON key.");
+ jsonKey = JSON_DEFAULT_KEY;
}
List<Object> keyedList = bykey.get(jsonKey);

0 comments on commit 6746809

Please sign in to comment.