if(value == null){
/*
* Step 5.10.2.4.2: Read property value from object. If value null attempt to get it from default value.
*/
try{
Field declaredField = getDeclaredField(clazz,name);
XmlElement xmlElm = declaredField.getAnnotation(XmlElement.class);
if(xmlElm != null){
if(!xmlElm.defaultValue().equals(NULL) && isJSONPrimitive(propertyType)){
value = xmlElm.defaultValue();
} else if(!xmlElm.nillable() && createDefaultOnNonNullable){
if(!isJSONPrimitive(propertyType)){
if(!stackNillableInstances.contains(propertyType)){
stackNillableInstances.push(propertyType);
value = getNewInstance(propertyType);
hasData = this.add(name, value, null, hasData, property) || hasData;
stackNillableInstances.pop();
}
continue nextProperty;
}else{
// Primitive don't have any default WHAT TODO . E.g. Integer object can't be instantiated.
}
}
}
}catch(Throwable th){}
// Value still null
if(value == null && JSONCodec.excludeNullProperties && !this.metaDataMode){
continue nextProperty;
} else if(this.metaDataMode) {
// In meta data mode always get data via meta provider.
value = getMetaDataInstance(propertyType, accessor.getAnnotation(JSONWebService.class),
getDeclaredField(clazz,name));
}
}else if(this.metaDataMode && propertyType.isPrimitive()){
// Primitive meta data. In case like int value become 0. But it may be from default
value = getMetaDataInstance(propertyType, accessor.getAnnotation(JSONWebService.class),
getDeclaredField(clazz,name));
}
/*
* Step 5.10.2.4.3: read property custom config. If it is serializable continue process with specified name if any
*/
JSONWebService properyConfig = accessor.getAnnotation(JSONWebService.class);
if (properyConfig != null) {
if (!properyConfig.serialize())
continue;
else if (properyConfig.name().length() > 0)
name = properyConfig.name();
}else{
/*
* Step 5.10.2.4.4: JSON config not present. Then read XML configuration.
*/
try{
// XML annotation present at field level.
Field declaredField = getDeclaredField(clazz,name);
XmlElement xmlElm = declaredField.getAnnotation(XmlElement.class);
/*
* Step 5.10.2.4.5: If XML transient ignore property.
*/
if (declaredField.isAnnotationPresent(XmlMimeType.class)){
if(this.metaDataMode){
value = declaredField.getAnnotation(XmlMimeType.class).value();
}else{
Map<String,Object> attachment = new HashMap<String, Object>();;
attachment .put("name", name);
attachment.put("value", value);
attachment.put("mimeType",declaredField.getAnnotation(XmlMimeType.class).value());
attachments .add(attachment);
continue nextProperty;
}
} else if(xmlElm != null) {// MAjor hits are here
/*
* Step 5.10.2.4.4.2: Xml elements .
*/
if(!xmlElm.name().equals(XML_DEFAULT)){
name = xmlElm.name();
}
}else if(declaredField.isAnnotationPresent(XmlElements.class) && Collection.class.isAssignableFrom(declaredField.getType())
&& value instanceof Collection){
// XML choice list
/*
* Step 5.10.2.4.4.1: Is it XML choice list?. If assign find right element name.
*/
XmlElements xmlElms = declaredField.getAnnotation(XmlElements.class);
Collection<?> valueList = (Collection<?>)value;
if(!valueList.isEmpty()){
// use first object to identify type
ArrayList<Map<String,Object>> group = new ArrayList<Map<String,Object>>();
for(Object ob : valueList){
for(XmlElement elm : xmlElms.value()){
// TODO JAXBElement
if(((Class<?>)elm.type()).isAssignableFrom(ob.getClass())){
Map<String,Object> objectMap = new HashMap<String, Object>();
objectMap.put(elm.name(), ob);
group.add(objectMap);
break;// XmlElement list break
}
}
}
hasData = this.add(name, group, null, hasData, property) || hasData;
continue nextProperty;
} else {
// If choice element id empty, don't print it at all
if(!this.metaDataMode){
continue nextProperty;
} else {
name = "CHOICE";
Map<String,Object> choices = new HashMap<String, Object>();
for(XmlElement elm : xmlElms.value()){
try{
choices.put(elm.name(), getMetaDataInstance(elm.type(),null,null));
}catch(Throwable th){
//
choices.put(elm.name(),"object");
}
}
value = choices;
}
}
}else if(declaredField.isAnnotationPresent(XmlElementRefs.class)
&& Collection.class.isAssignableFrom(declaredField.getType())
&& value instanceof Collection){
// XML choice list
/*
* Step 5.10.2.4.4.1: Is it XML choice list?. If assign find right element name.
*/
XmlElementRefs xmlElms = declaredField.getAnnotation(XmlElementRefs.class);
Collection<?> valueList = (Collection<?>)value;
if(!valueList.isEmpty()){
// use first object to identify type
Map<String,List<Object>> group = new HashMap<String,List<Object>>();
for(Object ob : valueList){
for(XmlElementRef elm : xmlElms.value()) {
if(ob instanceof JAXBElement<?>) {
JAXBElement<?> element = (JAXBElement<?>)ob;
if(element.getName().getLocalPart().equals(elm.name())){// namespace too
name = elm.name();
if(!group.containsKey(name))
group.put(name, new ArrayList<Object>());
group.get(name).add(element.getValue());
break;// Break element list
}
} else {
if(((Class<?>)elm.type()).isAssignableFrom(ob.getClass())){
name = elm.name();
if(!group.containsKey(name))
group.put(name, new ArrayList<Object>());
group.get(name).add(ob);
break;// Break element list
}
}
}
}
for(Map.Entry<String, List<Object>> entry : group.entrySet()){
hasData = this.add(entry.getKey(), entry.getValue(), null, hasData, property) || hasData;
// TODO this.setExprStack(expr);
}
continue nextProperty;
} else {
// If choice element id empty, don't print it at all
if(!this.metaDataMode){
continue nextProperty;
} else {
name = "CHOICE";
Map<String,Object> choices = new HashMap<String, Object>();
for(XmlElementRef elm : xmlElms.value()){
try{
choices.put(elm.name(), getMetaDataInstance(elm.type(),null,null));
}catch(Throwable th){
//
choices.put(elm.name(),"object");
}
}
value = choices;
}
}
} else if(declaredField.isAnnotationPresent(XmlAttribute.class)){
/*
* Step 5.10.2.4.4.3: Xml attribute.
*/
if(!declaredField.getAnnotation(XmlAttribute.class).name().equals(XML_DEFAULT)){
name = declaredField.getAnnotation(XmlAttribute.class).name();
}
}else if(accessor.isAnnotationPresent(XmlTransient.class) ||
(declaredField != null && declaredField.isAnnotationPresent(XmlTransient.class))){
/**
* XmlTransient contains lower periority than XMLElementX annotation.
* Transient fields should not have XMLElement,XMLElements or XmlAttribute
*/
continue nextProperty;