-
-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1015 from SageDroid/fix/1014
Apply `excludeFields` only to fields in `Library`
- Loading branch information
Showing
4 changed files
with
74 additions
and
6 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
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
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
33 changes: 33 additions & 0 deletions
33
.../plugin/src/main/kotlin/com/mikepenz/aboutlibraries/plugin/util/PartialObjectConverter.kt
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.mikepenz.aboutlibraries.plugin.util | ||
|
||
import groovy.json.DefaultJsonGenerator | ||
import groovy.json.JsonGenerator | ||
import org.codehaus.groovy.runtime.DefaultGroovyMethods | ||
|
||
/** | ||
* A converter for [JsonGenerator], which allows properties to be excluded from the output. | ||
* The way it works is identical to the serialization of objects in [DefaultJsonGenerator]. | ||
* @property excludedQualifiedPropertyNames The qualified name (class name + property name) | ||
* of the properties that should be excluded from serialization. | ||
*/ | ||
class PartialObjectConverter( | ||
private val excludedQualifiedPropertyNames: Set<String> | ||
) : JsonGenerator.Converter { | ||
|
||
private val targetClassNames: Set<String> = excludedQualifiedPropertyNames.mapTo(mutableSetOf()) { field -> | ||
field.substringBeforeLast('.') | ||
} | ||
|
||
private val excludedPropertyNames = setOf("class", "declaringClass", "metaClass") | ||
|
||
override fun handles(type: Class<*>?): Boolean { | ||
return type != null && targetClassNames.contains(type.simpleName) | ||
} | ||
|
||
override fun convert(value: Any, key: String?): Any { | ||
return DefaultGroovyMethods.getProperties(value).filterKeys { propertyName -> | ||
propertyName !in excludedPropertyNames && "${value::class.simpleName}.$propertyName" !in excludedQualifiedPropertyNames | ||
} | ||
} | ||
|
||
} |