use of com.serotonin.json.spi.JsonProperty in project ma-core-public by infiniteautomation.
the class JsonContext method gatherJsonPropertyNames.
private Map<String, JsonProperty> gatherJsonPropertyNames(Class<?> clazz) throws JsonException {
// Ignore Object.
if (clazz == Object.class)
return null;
Map<String, JsonProperty> jsonProperties = new HashMap<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
JsonProperty anno = field.getAnnotation(JsonProperty.class);
if (anno != null)
jsonProperties.put(field.getName(), anno);
}
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
JsonProperty anno = method.getAnnotation(JsonProperty.class);
if (anno == null)
continue;
// Convert the method name to a property name using the JavaBean rules.
String name = method.getName();
if (method.getReturnType() == Boolean.TYPE) {
if (!name.startsWith("is"))
throw new JsonException("Non-JavaBean get methods cannot be marked with JsonRemoteProperty: " + clazz.getName() + "." + name);
name = Character.toLowerCase(name.charAt(2)) + name.substring(3);
} else {
if (!name.startsWith("get") && !name.startsWith("set"))
throw new JsonException("Non-JavaBean get methods cannot be marked with JsonRemoteProperty: " + clazz.getName() + "." + name);
name = Character.toLowerCase(name.charAt(3)) + name.substring(4);
}
jsonProperties.put(name, anno);
}
return jsonProperties;
}
use of com.serotonin.json.spi.JsonProperty in project ma-core-public by infiniteautomation.
the class JsonSerializableUtility 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);
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.spi.JsonProperty in project ma-core-public by infiniteautomation.
the class JsonSerializableUtility method gatherJsonPropertyNames.
private Map<String, JsonProperty> gatherJsonPropertyNames(Class<?> clazz) throws JsonException {
// Ignore Object.
if (clazz == Object.class)
return null;
Map<String, JsonProperty> jsonProperties = new HashMap<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
JsonProperty anno = field.getAnnotation(JsonProperty.class);
if (anno != null)
jsonProperties.put(field.getName(), anno);
}
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
JsonProperty anno = method.getAnnotation(JsonProperty.class);
if (anno == null)
continue;
// Convert the method name to a property name using the JavaBean rules.
String name = method.getName();
if (method.getReturnType() == Boolean.TYPE) {
if (!name.startsWith("is"))
throw new JsonException("Non-JavaBean get methods cannot be marked with JsonRemoteProperty: " + clazz.getName() + "." + name);
name = Character.toLowerCase(name.charAt(2)) + name.substring(3);
} else {
if (!name.startsWith("get") && !name.startsWith("set"))
throw new JsonException("Non-JavaBean get methods cannot be marked with JsonRemoteProperty: " + clazz.getName() + "." + name);
name = Character.toLowerCase(name.charAt(3)) + name.substring(4);
}
jsonProperties.put(name, anno);
}
return jsonProperties;
}
use of com.serotonin.json.spi.JsonProperty 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();
}
Aggregations