use of com.serotonin.json.util.SerializableProperty in project ma-core-public by infiniteautomation.
the class JsonSerializableUtility method findProperties.
public List<SerializableProperty> findProperties(Class<?> clazz) throws JsonException {
//
// Introspect the class.
List<SerializableProperty> properties = new ArrayList<>();
boolean jsonSerializable = JsonSerializable.class.isAssignableFrom(clazz);
if (!jsonSerializable) {
// Is it a semi-primative
if (clazz.isAssignableFrom(Boolean.class) || clazz.isAssignableFrom(Byte.class) || clazz.isAssignableFrom(Short.class) || clazz.isAssignableFrom(Integer.class) || clazz.isAssignableFrom(Long.class) || clazz.isAssignableFrom(Float.class) || clazz.isAssignableFrom(Double.class) || clazz.isAssignableFrom(BigInteger.class) || clazz.isAssignableFrom(BigDecimal.class) || clazz.isAssignableFrom(String.class) || clazz.isAssignableFrom(Object.class))
return properties;
}
BeanInfo info;
try {
info = Introspector.getBeanInfo(clazz);
} catch (IntrospectionException e) {
throw new JsonException(e);
}
PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
// Annotations or beans
Class<?> currentClazz = clazz;
while (currentClazz != Object.class) {
boolean annotationsFound = addAnnotatedProperties(currentClazz, descriptors, properties);
// Serotonin JSON searches for POJO properties here, we don't want to.
if (!annotationsFound && !currentClazz.isAnnotationPresent(JsonEntity.class) && !jsonSerializable)
addPojoProperties(currentClazz, descriptors, properties);
currentClazz = currentClazz.getSuperclass();
}
return properties;
}
use of com.serotonin.json.util.SerializableProperty in project ma-core-public by infiniteautomation.
the class JsonContext method addPojoProperties.
private void addPojoProperties(Class<?> clazz, PropertyDescriptor[] descriptors, List<SerializableProperty> properties) {
for (PropertyDescriptor descriptor : descriptors) {
String name = descriptor.getName();
// Don't implicitly marshall getClass()
if (name.equals("class"))
continue;
// Ignore hibernate stuff too
if (name.equals("hibernateLazyInitializer"))
continue;
Method readMethod = descriptor.getReadMethod();
if (readMethod != null && readMethod.getDeclaringClass() != clazz)
readMethod = null;
Method writeMethod = descriptor.getWriteMethod();
if (writeMethod != null && writeMethod.getDeclaringClass() != clazz)
writeMethod = null;
SerializableProperty prop = new SerializableProperty();
prop.setName(name);
prop.setReadMethod(readMethod);
prop.setWriteMethod(writeMethod);
maybeAddProperty(properties, prop);
}
}
use of com.serotonin.json.util.SerializableProperty in project ma-core-public by infiniteautomation.
the class JsonContext method getConverter.
/**
* Returns the class converter for the given class.
*
* @param clazz
* the class to look up
* @return the converter that is bound to the class. May be null if no converter was registered.
* @throws JsonException
*/
public ClassConverter getConverter(Class<?> clazz) throws JsonException {
// First check if the class is already in the map.
ClassConverter converter = classConverters.get(clazz);
if (converter != null)
return converter;
// Check for inheritance
Class<?> sc = clazz.getSuperclass();
while (sc != null && sc != Object.class) {
converter = classConverters.get(sc);
if (converter != null) {
classConverters.put(clazz, converter);
return converter;
}
sc = sc.getSuperclass();
}
// Check for enum
if (Enum.class.isAssignableFrom(clazz)) {
converter = classConverters.get(Enum.class);
if (converter != null) {
classConverters.put(clazz, converter);
return converter;
}
}
// Check for map
if (Map.class.isAssignableFrom(clazz)) {
converter = classConverters.get(Map.class);
if (converter != null) {
classConverters.put(clazz, converter);
return converter;
}
}
// Check for collection
if (Collection.class.isAssignableFrom(clazz)) {
converter = classConverters.get(Collection.class);
if (converter != null) {
classConverters.put(clazz, converter);
return converter;
}
}
// Check for array
if (clazz.isArray()) {
converter = classConverters.get(Array.class);
if (converter != null) {
classConverters.put(clazz, converter);
return converter;
}
}
//
// Introspect the class.
boolean jsonSerializable = JsonSerializable.class.isAssignableFrom(clazz);
boolean jsonEntity = clazz.isAnnotationPresent(JsonEntity.class);
List<SerializableProperty> properties = new ArrayList<>();
BeanInfo info;
try {
info = Introspector.getBeanInfo(clazz);
} catch (IntrospectionException e) {
throw new JsonException(e);
}
PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
// Annotations or beans
Class<?> currentClazz = clazz;
while (currentClazz != Object.class) {
boolean annotationsFound = addAnnotatedProperties(currentClazz, descriptors, properties);
if (!annotationsFound && !currentClazz.isAnnotationPresent(JsonEntity.class) && !jsonSerializable)
// Not annotated and no property annotations were found. Consider it a POJO.
addPojoProperties(currentClazz, descriptors, properties);
currentClazz = currentClazz.getSuperclass();
}
if (properties.isEmpty())
properties = null;
// Create a converter?
if (jsonSerializable || jsonEntity || properties != null) {
converter = new JsonPropertyConverter(jsonSerializable, properties);
classConverters.put(clazz, converter);
return converter;
}
// Give up
throw new JsonException("No converter for class " + clazz);
}
use of com.serotonin.json.util.SerializableProperty in project ma-core-public by infiniteautomation.
the class JsonContext method addAnnotatedProperties.
private boolean addAnnotatedProperties(Class<?> clazz, PropertyDescriptor[] descriptors, List<SerializableProperty> properties) throws JsonException {
Map<String, JsonProperty> jsonProperties = gatherJsonPropertyNames(clazz);
for (PropertyDescriptor descriptor : descriptors) {
String name = descriptor.getName();
// Don't implicitly marshall getClass()
if (name.equals("class"))
continue;
// Ignore hibernate stuff too
if (name.equals("hibernateLazyInitializer"))
continue;
JsonProperty anno = jsonProperties.get(name);
if (anno == null || (!anno.read() && !anno.write()))
continue;
Method readMethod = descriptor.getReadMethod();
if (!anno.write() || (readMethod != null && readMethod.getDeclaringClass() != clazz))
readMethod = null;
Method writeMethod = descriptor.getWriteMethod();
if (!anno.read() || (writeMethod != null && writeMethod.getDeclaringClass() != clazz))
writeMethod = null;
if (readMethod == null && writeMethod == null)
continue;
SerializableProperty prop = new SerializableProperty();
prop.setName(name);
// if (anno.typeFactory() != TypeFactory.class)
// prop.setTypeFactory(anno.typeFactory());
prop.setReadMethod(readMethod);
prop.setWriteMethod(writeMethod);
if (!Utils.isEmpty(anno.alias()))
prop.setAlias(anno.alias());
prop.setSuppressDefaultValue(anno.suppressDefaultValue());
prop.setIncludeHints(anno.includeHints());
maybeAddProperty(properties, prop);
}
return !jsonProperties.isEmpty();
}
use of com.serotonin.json.util.SerializableProperty in project ma-core-public by infiniteautomation.
the class JsonPropertyConverter method jsonRead.
@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object obj, Type type) throws JsonException {
JsonObject jsonObject = (JsonObject) jsonValue;
if (jsonSerializable)
((JsonSerializable) obj).jsonRead(reader, jsonObject);
if (properties != null) {
for (SerializableProperty prop : properties) {
// Check whether the property should be included
if (!prop.include(reader.getIncludeHint()))
continue;
Method writeMethod = prop.getWriteMethod();
if (writeMethod == null)
continue;
String name = prop.getNameToUse();
JsonValue propJsonValue = jsonObject.get(name);
if (propJsonValue == null)
continue;
Type propType = writeMethod.getGenericParameterTypes()[0];
propType = TypeUtils.resolveTypeVariable(type, propType);
Class<?> propClass = TypeUtils.getRawClass(propType);
try {
Object propValue = reader.read(propType, propJsonValue);
if (propClass.isPrimitive() && propValue == null) {
if (propClass == Boolean.TYPE)
propValue = false;
else
propValue = 0;
}
prop.getWriteMethod().invoke(obj, propValue);
} catch (Exception e) {
throw new JsonException("JsonException writing property '" + prop.getName() + "' of class " + propClass.getName(), e);
}
}
}
}
Aggregations