-
Notifications
You must be signed in to change notification settings - Fork 146
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
feat: implement JsonCodecConfiguration for scala 3 and explicitEmptyCollections for JsonCodecConfiguration #1193
Merged
987Nabil
merged 3 commits into
zio:series/2.x
from
ThijsBroersen:feat/explicit-empty-collections-config
Dec 13, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion
2
zio-json/shared/src/main/scala-3/zio/json/JsonCodecVersionSpecific.scala
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package zio.json | ||
|
||
private[json] trait JsonCodecVersionSpecific { | ||
inline def derived[A: deriving.Mirror.Of]: JsonCodec[A] = DeriveJsonCodec.gen[A] | ||
inline def derived[A: deriving.Mirror.Of](using config: JsonCodecConfiguration): JsonCodec[A] = DeriveJsonCodec.gen[A] | ||
|
||
} |
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
57 changes: 57 additions & 0 deletions
57
zio-json/shared/src/main/scala/zio/json/JsonCodecConfiguration.scala
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,57 @@ | ||
package zio.json | ||
|
||
import zio.json.JsonCodecConfiguration.SumTypeHandling | ||
import zio.json.JsonCodecConfiguration.SumTypeHandling.WrapperWithClassNameField | ||
|
||
/** | ||
* Implicit codec derivation configuration. | ||
* | ||
* @param sumTypeHandling see [[jsonDiscriminator]] | ||
* @param fieldNameMapping see [[jsonMemberNames]] | ||
* @param allowExtraFields see [[jsonNoExtraFields]] | ||
* @param sumTypeMapping see [[jsonHintNames]] | ||
*/ | ||
final case class JsonCodecConfiguration( | ||
sumTypeHandling: SumTypeHandling = WrapperWithClassNameField, | ||
fieldNameMapping: JsonMemberFormat = IdentityFormat, | ||
allowExtraFields: Boolean = true, | ||
sumTypeMapping: JsonMemberFormat = IdentityFormat, | ||
explicitNulls: Boolean = false, | ||
explicitEmptyCollections: Boolean = true | ||
) | ||
|
||
object JsonCodecConfiguration { | ||
implicit val default: JsonCodecConfiguration = JsonCodecConfiguration() | ||
|
||
sealed trait SumTypeHandling { | ||
def discriminatorField: Option[String] | ||
} | ||
|
||
object SumTypeHandling { | ||
|
||
/** | ||
* Use an object with a single key that is the class name. | ||
*/ | ||
case object WrapperWithClassNameField extends SumTypeHandling { | ||
override def discriminatorField: Option[String] = None | ||
} | ||
|
||
/** | ||
* For sealed classes, will determine the name of the field for | ||
* disambiguating classes. | ||
* | ||
* The default is to not use a typehint field and instead | ||
* have an object with a single key that is the class name. | ||
* See [[WrapperWithClassNameField]]. | ||
* | ||
* Note that using a discriminator is less performant, uses more memory, and may | ||
* be prone to DOS attacks that are impossible with the default encoding. In | ||
* addition, there is slightly less type safety when using custom product | ||
* encoders (which must write an unenforced object type). Only use this option | ||
* if you must model an externally defined schema. | ||
*/ | ||
final case class DiscriminatorField(name: String) extends SumTypeHandling { | ||
override def discriminatorField: Option[String] = Some(name) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see some inconsistencies on the priority of JsonCodecConfiguration vs annotations, sometimes the config take priority (e.g. explicit nulls), sometimes the annotations (e.g. discriminator).
For the new config I chose to use the same pattern as explicitNulls as it also has to do with missing values.
But I think it would make more sense if annotations take priority.The config first is nicer because it can overwrite conditionally.