Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling filtering capabilities #273

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions library/src/main/java/com/dslplatform/json/CompiledJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,11 @@ enum TypeSignature {
* @return deserialization hint or additional serialization info
*/
String name() default "";

/**
* Object attributes can be filtered out during serialization.<br>
* Attributes to serialize can be listed in an attribute (of Collection type) of the object to serialize<br>
* @return name of the attribute that may contain other attribute names to be serialized
*/
String filteringAttribute() default "";
}
14 changes: 14 additions & 0 deletions library/src/main/java/com/dslplatform/json/processor/Analysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -1368,11 +1368,13 @@ private void findStructs(
CompiledJson.Behavior onUnknown = CompiledJson.Behavior.DEFAULT;
CompiledJson.TypeSignature typeSignature = CompiledJson.TypeSignature.DEFAULT;
TypeElement deserializeAs = null;
String filteringAttribute = "";
if (!isJsonObject) {
if (annotation != null) {
onUnknown = onUnknownValue(annotation);
typeSignature = typeSignatureValue(annotation);
deserializeAs = deserializeAs(annotation);
filteringAttribute = filteringAttribute(annotation);
if (deserializeAs != null) {
String error = validateDeserializeAs(element, deserializeAs);
if (error != null) {
Expand Down Expand Up @@ -1453,6 +1455,7 @@ private void findStructs(
objectFormatPolicy,
deserializeAs,
classDiscriminator(annotation),
filteringAttribute,
className(annotation),
type == ObjectType.ENUM ? findEnumConstantNameSource(element) : null,
namingStrategy(element, annotation),
Expand Down Expand Up @@ -2547,6 +2550,17 @@ private static TypeElement deserializeAs(AnnotationMirror annotation) {
}
return null;
}

@Nullable
private static String filteringAttribute(AnnotationMirror annotation) {
Map<? extends ExecutableElement, ? extends AnnotationValue> values = annotation.getElementValues();
for (ExecutableElement ee : values.keySet()) {
if (ee.toString().equals("filteringAttribute()")) {
return (String) values.get(ee).getValue();
}
}
return "";
}

private static final Map<String, NamingStrategy> namingCache = new HashMap<String, NamingStrategy>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ private void writeObject(final StructInfo si, final String className, List<Attri
code.append("\t\tpublic boolean writeContentMinimal(final com.dslplatform.json.JsonWriter writer, final ");
code.append(className).append(" instance) {\n");
code.append("\t\t\tboolean hasWritten = false;\n");
boolean checkFiltering = !si.filteringAttribute.isEmpty();
for (AttributeInfo attr : sortedAttributes) {
if (!attr.canWriteOutput()) continue;
String defaultValue = context.getDefault(attr);
Expand All @@ -697,22 +698,28 @@ private void writeObject(final StructInfo si, final String className, List<Attri
boolean isPrimitive = !attr.typeName.equals(Analysis.objectName(attr.typeName));
String readValue = "instance." + attr.readProperty;

if (checkDefaults) {
if (checkDefaults || checkFiltering) {
code.append("\t\t\tif (");
if ("null".equals(defaultValue) || isPrimitive) {
code.append(readValue).append(" != ").append(defaultValue);
} else if (attr.notNull && attr.isArray) {
code.append(readValue).append(" != null && ").append(readValue).append(".length != 0");
} else if (attr.notNull && (attr.isList || attr.isSet || attr.isMap)) {
code.append(readValue).append(" != null && !").append(readValue).append(".isEmpty()");
} else {
StructInfo target = context.structs.get(attr.typeName);
if (target != null && (target.hasEmptyCtor() || target.hasKnownConversion() || target.annotatedFactory != null)) {
code.append(readValue).append(" != null");
if (checkDefaults) {
if ("null".equals(defaultValue) || isPrimitive) {
code.append(readValue).append(" != ").append(defaultValue);
} else if (attr.notNull && attr.isArray) {
code.append(readValue).append(" != null && ").append(readValue).append(".length != 0");
} else if (attr.notNull && (attr.isList || attr.isSet || attr.isMap)) {
code.append(readValue).append(" != null && !").append(readValue).append(".isEmpty()");
} else {
code.append(readValue).append(" != null && !").append(defaultValue).append(".equals(").append(readValue).append(")");
StructInfo target = context.structs.get(attr.typeName);
if (target != null && (target.hasEmptyCtor() || target.hasKnownConversion() || target.annotatedFactory != null)) {
code.append(readValue).append(" != null");
} else {
code.append(readValue).append(" != null && !").append(defaultValue).append(".equals(").append(readValue).append(")");
}
}
}
if (checkFiltering) {
if (checkDefaults) code.append(" && ");
code.append("(").append("instance.").append(si.filteringAttribute).append("().size()==0 || ").append("instance.").append(si.filteringAttribute).append("().contains(\"").append(attr.name).append("\"))");
}
code.append(") {\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class StructInfo {
public final CompiledJson.ObjectFormatPolicy objectFormatPolicy;
public final TypeElement deserializeAs;
public final String discriminator;
public final String filteringAttribute;
public final String deserializeName;
public final @Nullable NamingStrategy namingStrategy;
public final EnumSet<CompiledJson.Format> formats;
Expand Down Expand Up @@ -65,6 +66,7 @@ public StructInfo(
CompiledJson.ObjectFormatPolicy objectFormatPolicy,
@Nullable TypeElement deserializeAs,
@Nullable String discriminator,
@Nullable String filteringAttribute,
@Nullable String deserializeName,
@Nullable Element enumConstantNameSource,
@Nullable NamingStrategy namingStrategy,
Expand All @@ -86,6 +88,7 @@ public StructInfo(
this.objectFormatPolicy = objectFormatPolicy;
this.deserializeAs = deserializeAs;
this.discriminator = discriminator != null ? discriminator : "";
this.filteringAttribute = filteringAttribute != null ? filteringAttribute : "";
this.deserializeName = deserializeName != null ? deserializeName : "";
this.enumConstantNameSource = enumConstantNameSource;
this.namingStrategy = namingStrategy;
Expand Down Expand Up @@ -134,6 +137,7 @@ public StructInfo(ConverterInfo converter, DeclaredType discoveredBy, TypeElemen
this.objectFormatPolicy = CompiledJson.ObjectFormatPolicy.DEFAULT;
this.deserializeAs = null;
this.discriminator = "";
this.filteringAttribute = "";
this.deserializeName = "";
this.enumConstantNameSource = null;
this.namingStrategy = null;
Expand Down