Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added codecs for java.time classes and cleaned up codec setup logic
- Loading branch information
Marc Knaup
committed
Nov 4, 2017
1 parent
a306a10
commit 1c5b40e
Showing
101 changed files
with
739 additions
and
250 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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,59 @@ | ||
package com.github.fluidsonic.fluid.json | ||
|
||
|
||
object DefaultJSONCodecs { | ||
|
||
val basic = listOf( | ||
AnyJSONDecoderCodec, | ||
ArrayJSONCodec, | ||
BooleanArrayJSONCodec, | ||
BooleanJSONCodec, | ||
ByteArrayJSONCodec, | ||
ByteJSONCodec, | ||
DoubleArrayJSONCodec, | ||
DoubleJSONCodec, | ||
FloatArrayJSONCodec, | ||
FloatJSONCodec, | ||
IntArrayJSONCodec, | ||
IntJSONCodec, | ||
ListJSONDecoderCodec, | ||
LongArrayJSONCodec, | ||
LongJSONCodec, | ||
MapJSONCodec, | ||
SequenceJSONCodec, | ||
ShortArrayJSONCodec, | ||
ShortJSONCodec, | ||
StringJSONCodec, | ||
IterableJSONEncoderCodec, // after subclasses | ||
NumberJSONCodec // after subclasses | ||
) | ||
|
||
|
||
val extended = listOf( | ||
DayOfWeekJSONCodec, | ||
DurationJSONCodec, | ||
InstantJSONCodec, | ||
LocalDateJSONCodec, | ||
LocalDateTimeJSONCodec, | ||
LocalTimeJSONCodec, | ||
MonthDayJSONCodec, | ||
MonthJSONCodec, | ||
OffsetTimeJSONCodec, | ||
OffsetTimeJSONCodec, | ||
PeriodJSONCodec, | ||
YearJSONCodec, | ||
YearMonthJSONCodec, | ||
ZonedDateTimeJSONCodec, | ||
ZoneIdJSONCodec, | ||
ZoneOffsetJSONCodec | ||
) | ||
|
||
|
||
val nonRecursive = listOf( | ||
ArrayJSONCodec.nonRecursive, | ||
ListJSONDecoderCodec.nonRecursive, | ||
MapJSONCodec.nonRecursive, | ||
SequenceJSONCodec.nonRecursive, | ||
IterableJSONEncoderCodec.nonRecursive // after subclasses | ||
) | ||
} |
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,34 @@ | ||
package com.github.fluidsonic.fluid.json | ||
|
||
import java.time.DayOfWeek | ||
|
||
|
||
// TODO use Enum codec once implemented | ||
object DayOfWeekJSONCodec : AbstractJSONCodec<DayOfWeek, JSONCoderContext>() { | ||
|
||
override fun decode(valueType: JSONCodableType<in DayOfWeek>, decoder: JSONDecoder<JSONCoderContext>) = | ||
decoder.readString().let { raw -> | ||
when (raw) { | ||
"monday" -> DayOfWeek.MONDAY | ||
"tuesday" -> DayOfWeek.TUESDAY | ||
"wednesday" -> DayOfWeek.WEDNESDAY | ||
"thursday" -> DayOfWeek.THURSDAY | ||
"friday" -> DayOfWeek.FRIDAY | ||
"saturday" -> DayOfWeek.SATURDAY | ||
"sunday" -> DayOfWeek.SUNDAY | ||
else -> throw JSONException("Invalid DayOfWeek value: $raw") | ||
} | ||
} | ||
|
||
|
||
override fun encode(value: DayOfWeek, encoder: JSONEncoder<JSONCoderContext>) = | ||
encoder.writeString(when (value) { | ||
DayOfWeek.MONDAY -> "monday" | ||
DayOfWeek.TUESDAY -> "tuesday" | ||
DayOfWeek.WEDNESDAY -> "wednesday" | ||
DayOfWeek.THURSDAY -> "thursday" | ||
DayOfWeek.FRIDAY -> "friday" | ||
DayOfWeek.SATURDAY -> "saturday" | ||
DayOfWeek.SUNDAY -> "sunday" | ||
}) | ||
} |
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,22 @@ | ||
package com.github.fluidsonic.fluid.json | ||
|
||
import java.time.DateTimeException | ||
import java.time.Duration | ||
|
||
|
||
object DurationJSONCodec : AbstractJSONCodec<Duration, JSONCoderContext>() { | ||
|
||
override fun decode(valueType: JSONCodableType<in Duration>, decoder: JSONDecoder<JSONCoderContext>) = | ||
decoder.readString().let { raw -> | ||
try { | ||
Duration.parse(raw)!! | ||
} | ||
catch (e: DateTimeException) { | ||
throw JSONException("Cannot parse Duration value: $raw") | ||
} | ||
} | ||
|
||
|
||
override fun encode(value: Duration, encoder: JSONEncoder<JSONCoderContext>) = | ||
encoder.writeString(value.toString()) | ||
} |
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,22 @@ | ||
package com.github.fluidsonic.fluid.json | ||
|
||
import java.time.DateTimeException | ||
import java.time.Instant | ||
|
||
|
||
object InstantJSONCodec : AbstractJSONCodec<Instant, JSONCoderContext>() { | ||
|
||
override fun decode(valueType: JSONCodableType<in Instant>, decoder: JSONDecoder<JSONCoderContext>) = | ||
decoder.readString().let { raw -> | ||
try { | ||
Instant.parse(raw)!! | ||
} | ||
catch (e: DateTimeException) { | ||
throw JSONException("Cannot parse Instant value: $raw") | ||
} | ||
} | ||
|
||
|
||
override fun encode(value: Instant, encoder: JSONEncoder<JSONCoderContext>) = | ||
encoder.writeString(value.toString()) | ||
} |
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,22 @@ | ||
package com.github.fluidsonic.fluid.json | ||
|
||
import java.time.DateTimeException | ||
import java.time.LocalDate | ||
|
||
|
||
object LocalDateJSONCodec : AbstractJSONCodec<LocalDate, JSONCoderContext>() { | ||
|
||
override fun decode(valueType: JSONCodableType<in LocalDate>, decoder: JSONDecoder<JSONCoderContext>) = | ||
decoder.readString().let { raw -> | ||
try { | ||
LocalDate.parse(raw)!! | ||
} | ||
catch (e: DateTimeException) { | ||
throw JSONException("Cannot parse LocalDate value: $raw") | ||
} | ||
} | ||
|
||
|
||
override fun encode(value: LocalDate, encoder: JSONEncoder<JSONCoderContext>) = | ||
encoder.writeString(value.toString()) | ||
} |
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,22 @@ | ||
package com.github.fluidsonic.fluid.json | ||
|
||
import java.time.DateTimeException | ||
import java.time.LocalDateTime | ||
|
||
|
||
object LocalDateTimeJSONCodec : AbstractJSONCodec<LocalDateTime, JSONCoderContext>() { | ||
|
||
override fun decode(valueType: JSONCodableType<in LocalDateTime>, decoder: JSONDecoder<JSONCoderContext>) = | ||
decoder.readString().let { raw -> | ||
try { | ||
LocalDateTime.parse(raw)!! | ||
} | ||
catch (e: DateTimeException) { | ||
throw JSONException("Cannot parse LocalDateTime value: $raw") | ||
} | ||
} | ||
|
||
|
||
override fun encode(value: LocalDateTime, encoder: JSONEncoder<JSONCoderContext>) = | ||
encoder.writeString(value.toString()) | ||
} |
Oops, something went wrong.