-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
trying to make basic functionality to intercept writes to json via an…
… attached policy on the writer
- Loading branch information
Mike Skells
committed
Sep 10, 2024
1 parent
eb6d894
commit 93e4f14
Showing
19 changed files
with
1,950 additions
and
1,567 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.dslplatform.json; | ||
|
||
import java.util.List; | ||
|
||
public class ClassInfo<C> { | ||
private final Class<C> type; | ||
private final CompiledJson.ObjectFormatPolicy objectFormatPolicy; | ||
private final List<PropertyInfo> propertyInfos; | ||
private final PropertyAccessor<C> propertyAccessor; | ||
|
||
public ClassInfo(Class<C> type, CompiledJson.ObjectFormatPolicy objectFormatPolicy, List<PropertyInfo> propertyInfos, PropertyAccessor<C> propertyAccessor) { | ||
this.type = type; | ||
this.objectFormatPolicy = objectFormatPolicy; | ||
this.propertyInfos = propertyInfos; | ||
this.propertyAccessor = propertyAccessor; | ||
} | ||
|
||
public Class<C> getType() { | ||
return type; | ||
} | ||
|
||
public CompiledJson.ObjectFormatPolicy getObjectFormatPolicy() { | ||
return objectFormatPolicy; | ||
} | ||
|
||
public List<PropertyInfo> getPropertyInfos() { | ||
return propertyInfos; | ||
} | ||
|
||
public PropertyAccessor<C> getPropertyAccessor() { | ||
return propertyAccessor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.dslplatform.json; | ||
|
||
public abstract class ControlInfo{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
library/src/main/java/com/dslplatform/json/JsonAttributeInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.dslplatform.json; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; /** | ||
* Information captured from the annotation | ||
*/ | ||
public final class JsonAttributeInfo { | ||
public final String name; | ||
public final int index; | ||
public final List<String> alternativeNames; | ||
public final boolean hasConverter; | ||
public final JsonAttribute.IncludePolicy includeToMinimal; | ||
|
||
public JsonAttributeInfo(String name, | ||
int index, String[] alternativeNames, boolean hasConverter, | ||
JsonAttribute.IncludePolicy includeToMinimal) { | ||
this.name = name; | ||
this.index = index; | ||
this.alternativeNames = Arrays.asList(alternativeNames); | ||
this.hasConverter = hasConverter; | ||
this.includeToMinimal = includeToMinimal; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
library/src/main/java/com/dslplatform/json/JsonControls.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.dslplatform.json; | ||
|
||
import java.util.List; | ||
|
||
public abstract class JsonControls<T extends ControlInfo> { | ||
|
||
protected JsonControls() { | ||
} | ||
|
||
public abstract <C> T controlledInfo(C instance, ClassInfo<C> classInfo); | ||
|
||
public abstract <C> List<PropertyInfo> controlledProperties(C instance, ClassInfo<C> classInfo, T controlledInfo); | ||
|
||
|
||
public abstract <C> PropertyWriteControl shouldWrite(C instance, ClassInfo<C> classInfo, T controlledInfo, PropertyInfo propertyInfo, JsonWriter writer, final Object value, final boolean checkDefaults, final boolean isNotDefaultValue); | ||
|
||
public abstract <C> void afterPropertyWrite(C instance, ClassInfo<C> classInfo, T controlledInfo, PropertyInfo propertyInfo, JsonWriter writer, long positionBefore); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
library/src/main/java/com/dslplatform/json/MinimalControls.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.dslplatform.json; | ||
|
||
import java.util.List; | ||
|
||
public class MinimalControls extends JsonControls<ControlInfo> { | ||
public final static MinimalControls INSTANCE = new MinimalControls(); | ||
private final static ControlInfo NO_CONTROLS = new ControlInfo() {}; | ||
|
||
MinimalControls() { | ||
} | ||
|
||
public <C> ControlInfo controlledInfo(C instance, ClassInfo<C> classInfo) { | ||
return NO_CONTROLS; | ||
} | ||
|
||
public <C> List<PropertyInfo> controlledProperties(C instance, ClassInfo<C> classInfo, ControlInfo controlledInfo) { | ||
return classInfo.getPropertyInfos(); | ||
} | ||
|
||
|
||
public <C> PropertyWriteControl shouldWrite(C instance, ClassInfo<C> classInfo, ControlInfo controlledInfo, PropertyInfo propertyInfo, JsonWriter writer, final Object value, final boolean checkDefaults, final boolean isNotDefaultValue) { | ||
switch (classInfo.getObjectFormatPolicy()) { | ||
case DEFAULT: | ||
case EXPLICIT: | ||
case CONTROLLED: | ||
case MINIMAL: | ||
return isNotDefaultValue || | ||
(propertyInfo.getAttribute().includeToMinimal == JsonAttribute.IncludePolicy.ALWAYS) | ||
? PropertyWriteControl.WRITE_NORMALLY | ||
: PropertyWriteControl.IGNORED; | ||
case FULL: | ||
return PropertyWriteControl.WRITE_NORMALLY; | ||
default: | ||
throw new IllegalArgumentException("Unsupported object format policy: " + classInfo.getObjectFormatPolicy()); | ||
} | ||
} | ||
|
||
public <C> void afterPropertyWrite(C instance, ClassInfo<C> classInfo, ControlInfo controlInfo, PropertyInfo propertyInfo, JsonWriter writer, long positionBefore) { | ||
} | ||
} |
Oops, something went wrong.