-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement JsonCodecConfiguration for scala 3
feat: implement explicitEmptyCollections for JsonCodecConfiguration
- Loading branch information
1 parent
b7795b8
commit a9181d0
Showing
9 changed files
with
270 additions
and
105 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
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.