โ Not released yet
A collection of simple JsonAdapters for Moshi.
This library acts as an extension to Moshi by providing general purpose, yet useful JsonAdapter
s that are not present in the main library. Most provided adapters are linked via specialized JsonQualifier
annotations that alter serialization/deserialization strategies.
This library is not forcing any of it's own adapters by default. To leverage from any of the provided annotations/adapters add their respective factory to your Moshi.Builder
:
val moshi = Moshi.Builder()
.add(DecimalMax.ADAPTER_FACTORY)
.build()
Every declared annotation/adapter in this library, supports adapter composition. Which means that no JsonAdapter.Factory
will short circuit adapter construction on it's own annotation, and instead will delegate to the next adapter returned by Moshi
. Meaning that given the following type declaration:
data class Double(
@DecimalMax(value = "100", inclusive = true)
@DoubleIntValue val x: Int
)
The resulting JsonAdapter
will validate x
from the json response and double the value of it.
One important concept to keep in mind, that the order of declared annotations in the example
above does not have any influence on the way how the final adapter will be constructed.
Instead the order of the JsonAdapter.Factory
's added to the Moshi
instance is what plays
a major role in overall behavior. Meaning that in order for the example above to satisfy the
expected result, one must add the factories in the following order:
val moshi = Moshi.Builder()
.add(DecimalMax.ADAPTER_FACTORY)
.add(DoubleIntValue)
.build()
@DecimalMax(value=, inclusive=)
- Checks whether the annotated value is less than the specified maximum, when
inclusive
=false. Otherwise whether the value is less than or equal to the specified maximum. - The parameter
value
is the string representation of the max value according to theBigDecimal
string representation. - Supported data types:
Int
,Byte
,Short
andLong
- Example:
@DecimalMax(value = "100", inclusive = false) val value: Int
- Checks whether the annotated value is less than the specified maximum, when
@DecimalMin(value=, inclusive=)
Checks whether the annotated value is larger than the specified minimum, wheninclusive
=false. Otherwise whether the value is larger than or equal to the specified minimum.- The parameter
value
is the string representation of the min value according to theBigDecimal
string representation.- Supported data types:
Int
,Byte
,Short
andLong
- Example:
@DecimalMin(value = "100", inclusive = false) val value: Int
- Supported data types:
@Digits(integer=, fraction=)
- Checks whether the annotated value is a number having up to
integer
digits andfraction
fractional digits - Supported data types:
Float
andDouble
. - Example:
@Digits(integer = 1, fraction = 1) val value: Float
- Checks whether the annotated value is a number having up to
@NotEmpty
- Checks that the annotated string is not empty.
- Supported data types:
String
. - Example:
@NotEmpty val string: String
@NotBlank
- Checks that the annotated string is not blank.
- The difference to
@NotEmpty
is and that trailing white-spaces are ignored. - Supported data types:
String
. - Example:
@NotBlank val string: String
@AssertFalse
- Checks that the annotated element is
false
. - Supported data types:
Boolean
. - Example:
@AssertFalse val b: Boolean
- Checks that the annotated element is
@AssertTrue
- Checks that the annotated element is
true
. - Supported data types:
Boolean
. - Example:
@AssertTrue val b: Boolean
- Checks that the annotated element is