From 1c5b40eaf72fcb914e0402d1967b26e731d323fc Mon Sep 17 00:00:00 2001 From: Marc Knaup Date: Sat, 4 Nov 2017 23:27:53 +0100 Subject: [PATCH] Added codecs for java.time classes and cleaned up codec setup logic --- Examples/0030-TypeEncoderCodecs.kt | 11 +--- Examples/0031-TypeDecoderCodecs.kt | 17 +----- Examples/0032-TypeCodecs.kt | 25 +------- Examples/0033-CodingAsStream.kt | 25 +------- README.md | 25 +++++++- .../Codecs/{ => Basic}/AnyJSONDecoderCodec.kt | 0 Sources/Codecs/{ => Basic}/ArrayJSONCodec.kt | 0 .../{ => Basic}/BooleanArrayJSONCodec.kt | 0 .../Codecs/{ => Basic}/BooleanJSONCodec.kt | 0 .../Codecs/{ => Basic}/ByteArrayJSONCodec.kt | 0 Sources/Codecs/{ => Basic}/ByteJSONCodec.kt | 0 .../{ => Basic}/DoubleArrayJSONCodec.kt | 0 Sources/Codecs/{ => Basic}/DoubleJSONCodec.kt | 0 .../Codecs/{ => Basic}/FloatArrayJSONCodec.kt | 0 Sources/Codecs/{ => Basic}/FloatJSONCodec.kt | 0 .../Codecs/{ => Basic}/IntArrayJSONCodec.kt | 0 Sources/Codecs/{ => Basic}/IntJSONCodec.kt | 0 .../{ => Basic}/IterableJSONEncoderCodec.kt | 0 .../{ => Basic}/ListJSONDecoderCodec.kt | 0 .../Codecs/{ => Basic}/LongArrayJSONCodec.kt | 0 Sources/Codecs/{ => Basic}/LongJSONCodec.kt | 0 Sources/Codecs/{ => Basic}/MapJSONCodec.kt | 0 .../{ => Basic}/NonRecursiveJSONCodec.kt | 0 .../NonRecursiveJSONDecoderCodec.kt | 0 .../NonRecursiveJSONEncoderCodec.kt | 0 Sources/Codecs/{ => Basic}/NumberJSONCodec.kt | 0 .../Codecs/{ => Basic}/SequenceJSONCodec.kt | 0 .../Codecs/{ => Basic}/ShortArrayJSONCodec.kt | 0 Sources/Codecs/{ => Basic}/ShortJSONCodec.kt | 0 Sources/Codecs/{ => Basic}/StringJSONCodec.kt | 0 Sources/Codecs/DefaultJSONCodecs.kt | 59 +++++++++++++++++++ Sources/Codecs/Extended/DayOfWeekJSONCodec.kt | 34 +++++++++++ Sources/Codecs/Extended/DurationJSONCodec.kt | 22 +++++++ Sources/Codecs/Extended/InstantJSONCodec.kt | 22 +++++++ Sources/Codecs/Extended/LocalDateJSONCodec.kt | 22 +++++++ .../Codecs/Extended/LocalDateTimeJSONCodec.kt | 22 +++++++ Sources/Codecs/Extended/LocalTimeJSONCodec.kt | 22 +++++++ Sources/Codecs/Extended/MonthDayJSONCodec.kt | 22 +++++++ Sources/Codecs/Extended/MonthJSONCodec.kt | 44 ++++++++++++++ .../Extended/OffsetDateTimeJSONCodec.kt | 22 +++++++ .../Codecs/Extended/OffsetTimeJSONCodec.kt | 22 +++++++ Sources/Codecs/Extended/PeriodJSONCodec.kt | 22 +++++++ Sources/Codecs/Extended/YearJSONCodec.kt | 22 +++++++ Sources/Codecs/Extended/YearMonthJSONCodec.kt | 22 +++++++ Sources/Codecs/Extended/ZoneIdJSONCodec.kt | 22 +++++++ .../Codecs/Extended/ZoneOffsetJSONCodec.kt | 22 +++++++ .../Codecs/Extended/ZonedDateTimeJSONCodec.kt | 22 +++++++ Sources/JSONCodecProvider.kt | 51 ++-------------- Sources/JSONDecoder.kt | 19 +++--- Sources/JSONEncoder.kt | 19 +++--- Sources/JSONParser.kt | 26 ++++---- Sources/JSONSerializer.kt | 24 ++++---- Tests/Sources/Data/{ => Basic}/arrayData.kt | 0 .../Data/{ => Basic}/bigDecimalData.kt | 0 .../Data/{ => Basic}/booleanArrayData.kt | 0 Tests/Sources/Data/{ => Basic}/booleanData.kt | 0 .../Sources/Data/{ => Basic}/byteArrayData.kt | 0 Tests/Sources/Data/{ => Basic}/byteData.kt | 0 Tests/Sources/Data/{ => Basic}/complexData.kt | 0 .../Data/{ => Basic}/doubleArrayData.kt | 0 Tests/Sources/Data/{ => Basic}/doubleData.kt | 0 .../Data/{ => Basic}/floatArrayData.kt | 0 Tests/Sources/Data/{ => Basic}/floatData.kt | 0 .../Sources/Data/{ => Basic}/intArrayData.kt | 0 Tests/Sources/Data/{ => Basic}/intData.kt | 0 .../Sources/Data/{ => Basic}/iterableData.kt | 0 Tests/Sources/Data/{ => Basic}/listData.kt | 0 .../Sources/Data/{ => Basic}/longArrayData.kt | 0 Tests/Sources/Data/{ => Basic}/longData.kt | 0 Tests/Sources/Data/{ => Basic}/mapData.kt | 0 .../{ => Basic}/mapDataWithNonStringKeys.kt | 0 Tests/Sources/Data/{ => Basic}/numberData.kt | 0 Tests/Sources/Data/{ => Basic}/plainData.kt | 0 .../Sources/Data/{ => Basic}/sequenceData.kt | 0 .../Data/{ => Basic}/shortArrayData.kt | 0 Tests/Sources/Data/{ => Basic}/shortData.kt | 0 Tests/Sources/Data/{ => Basic}/stringData.kt | 0 Tests/Sources/Data/Extended/dayOfWeekData.kt | 16 +++++ Tests/Sources/Data/Extended/durationData.kt | 13 ++++ Tests/Sources/Data/Extended/instantData.kt | 13 ++++ Tests/Sources/Data/Extended/localDateData.kt | 10 ++++ .../Data/Extended/localDateTimeData.kt | 10 ++++ Tests/Sources/Data/Extended/localTimeData.kt | 10 ++++ Tests/Sources/Data/Extended/monthData.kt | 21 +++++++ Tests/Sources/Data/Extended/monthDayData.kt | 10 ++++ .../Data/Extended/offsetDateTimeData.kt | 10 ++++ Tests/Sources/Data/Extended/offsetTimeData.kt | 10 ++++ Tests/Sources/Data/Extended/periodData.kt | 13 ++++ Tests/Sources/Data/Extended/yearData.kt | 10 ++++ Tests/Sources/Data/Extended/yearMonthData.kt | 10 ++++ Tests/Sources/Data/Extended/zoneIdData.kt | 10 ++++ Tests/Sources/Data/Extended/zoneOffsetData.kt | 10 ++++ .../Data/Extended/zonedDateTimeData.kt | 10 ++++ Tests/Sources/JSONDecoderSpec.kt | 31 +--------- Tests/Sources/JSONEncoderSpec.kt | 28 +-------- Tests/Sources/JSONParserSpec.kt | 14 +---- Tests/Sources/JSONSerializerSpec.kt | 10 +--- Tests/Sources/StandardCodecsSpec.kt | 23 +++++++- Tests/Sources/StandardParserRejectSpec.kt | 2 +- Tests/Sources/StandardSerializerRejectSpec.kt | 2 +- Tests/Sources/Utility/Coding.kt | 6 +- 101 files changed, 739 insertions(+), 250 deletions(-) rename Sources/Codecs/{ => Basic}/AnyJSONDecoderCodec.kt (100%) rename Sources/Codecs/{ => Basic}/ArrayJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/BooleanArrayJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/BooleanJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/ByteArrayJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/ByteJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/DoubleArrayJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/DoubleJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/FloatArrayJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/FloatJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/IntArrayJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/IntJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/IterableJSONEncoderCodec.kt (100%) rename Sources/Codecs/{ => Basic}/ListJSONDecoderCodec.kt (100%) rename Sources/Codecs/{ => Basic}/LongArrayJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/LongJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/MapJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/NonRecursiveJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/NonRecursiveJSONDecoderCodec.kt (100%) rename Sources/Codecs/{ => Basic}/NonRecursiveJSONEncoderCodec.kt (100%) rename Sources/Codecs/{ => Basic}/NumberJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/SequenceJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/ShortArrayJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/ShortJSONCodec.kt (100%) rename Sources/Codecs/{ => Basic}/StringJSONCodec.kt (100%) create mode 100644 Sources/Codecs/DefaultJSONCodecs.kt create mode 100644 Sources/Codecs/Extended/DayOfWeekJSONCodec.kt create mode 100644 Sources/Codecs/Extended/DurationJSONCodec.kt create mode 100644 Sources/Codecs/Extended/InstantJSONCodec.kt create mode 100644 Sources/Codecs/Extended/LocalDateJSONCodec.kt create mode 100644 Sources/Codecs/Extended/LocalDateTimeJSONCodec.kt create mode 100644 Sources/Codecs/Extended/LocalTimeJSONCodec.kt create mode 100644 Sources/Codecs/Extended/MonthDayJSONCodec.kt create mode 100644 Sources/Codecs/Extended/MonthJSONCodec.kt create mode 100644 Sources/Codecs/Extended/OffsetDateTimeJSONCodec.kt create mode 100644 Sources/Codecs/Extended/OffsetTimeJSONCodec.kt create mode 100644 Sources/Codecs/Extended/PeriodJSONCodec.kt create mode 100644 Sources/Codecs/Extended/YearJSONCodec.kt create mode 100644 Sources/Codecs/Extended/YearMonthJSONCodec.kt create mode 100644 Sources/Codecs/Extended/ZoneIdJSONCodec.kt create mode 100644 Sources/Codecs/Extended/ZoneOffsetJSONCodec.kt create mode 100644 Sources/Codecs/Extended/ZonedDateTimeJSONCodec.kt rename Tests/Sources/Data/{ => Basic}/arrayData.kt (100%) rename Tests/Sources/Data/{ => Basic}/bigDecimalData.kt (100%) rename Tests/Sources/Data/{ => Basic}/booleanArrayData.kt (100%) rename Tests/Sources/Data/{ => Basic}/booleanData.kt (100%) rename Tests/Sources/Data/{ => Basic}/byteArrayData.kt (100%) rename Tests/Sources/Data/{ => Basic}/byteData.kt (100%) rename Tests/Sources/Data/{ => Basic}/complexData.kt (100%) rename Tests/Sources/Data/{ => Basic}/doubleArrayData.kt (100%) rename Tests/Sources/Data/{ => Basic}/doubleData.kt (100%) rename Tests/Sources/Data/{ => Basic}/floatArrayData.kt (100%) rename Tests/Sources/Data/{ => Basic}/floatData.kt (100%) rename Tests/Sources/Data/{ => Basic}/intArrayData.kt (100%) rename Tests/Sources/Data/{ => Basic}/intData.kt (100%) rename Tests/Sources/Data/{ => Basic}/iterableData.kt (100%) rename Tests/Sources/Data/{ => Basic}/listData.kt (100%) rename Tests/Sources/Data/{ => Basic}/longArrayData.kt (100%) rename Tests/Sources/Data/{ => Basic}/longData.kt (100%) rename Tests/Sources/Data/{ => Basic}/mapData.kt (100%) rename Tests/Sources/Data/{ => Basic}/mapDataWithNonStringKeys.kt (100%) rename Tests/Sources/Data/{ => Basic}/numberData.kt (100%) rename Tests/Sources/Data/{ => Basic}/plainData.kt (100%) rename Tests/Sources/Data/{ => Basic}/sequenceData.kt (100%) rename Tests/Sources/Data/{ => Basic}/shortArrayData.kt (100%) rename Tests/Sources/Data/{ => Basic}/shortData.kt (100%) rename Tests/Sources/Data/{ => Basic}/stringData.kt (100%) create mode 100644 Tests/Sources/Data/Extended/dayOfWeekData.kt create mode 100644 Tests/Sources/Data/Extended/durationData.kt create mode 100644 Tests/Sources/Data/Extended/instantData.kt create mode 100644 Tests/Sources/Data/Extended/localDateData.kt create mode 100644 Tests/Sources/Data/Extended/localDateTimeData.kt create mode 100644 Tests/Sources/Data/Extended/localTimeData.kt create mode 100644 Tests/Sources/Data/Extended/monthData.kt create mode 100644 Tests/Sources/Data/Extended/monthDayData.kt create mode 100644 Tests/Sources/Data/Extended/offsetDateTimeData.kt create mode 100644 Tests/Sources/Data/Extended/offsetTimeData.kt create mode 100644 Tests/Sources/Data/Extended/periodData.kt create mode 100644 Tests/Sources/Data/Extended/yearData.kt create mode 100644 Tests/Sources/Data/Extended/yearMonthData.kt create mode 100644 Tests/Sources/Data/Extended/zoneIdData.kt create mode 100644 Tests/Sources/Data/Extended/zoneOffsetData.kt create mode 100644 Tests/Sources/Data/Extended/zonedDateTimeData.kt diff --git a/Examples/0030-TypeEncoderCodecs.kt b/Examples/0030-TypeEncoderCodecs.kt index 86fff5a..bd19b75 100644 --- a/Examples/0030-TypeEncoderCodecs.kt +++ b/Examples/0030-TypeEncoderCodecs.kt @@ -2,7 +2,6 @@ package examples import com.github.fluidsonic.fluid.json.* import java.time.Instant -import java.time.format.DateTimeFormatter object EncodingExample { @@ -12,7 +11,7 @@ object EncodingExample { // Using a codec for encoding specific Kotlin types simplifies JSON serialization a lot val serializer = JSONSerializer.builder() - .encodingWith(EventCodec, InstantCodec) + .encodingWith(EventCodec) .build() val json = serializer.serializeValue(listOf( @@ -45,12 +44,4 @@ object EncodingExample { } } } - - - private object InstantCodec : AbstractJSONEncoderCodec() { - - override fun encode(value: Instant, encoder: JSONEncoder) { - encoder.writeString(DateTimeFormatter.ISO_INSTANT.format(value)) - } - } } diff --git a/Examples/0031-TypeDecoderCodecs.kt b/Examples/0031-TypeDecoderCodecs.kt index f0a8be8..9c608f5 100644 --- a/Examples/0031-TypeDecoderCodecs.kt +++ b/Examples/0031-TypeDecoderCodecs.kt @@ -2,7 +2,6 @@ package examples import com.github.fluidsonic.fluid.json.* import java.time.Instant -import java.time.format.DateTimeParseException object DecodingExample { @@ -12,7 +11,7 @@ object DecodingExample { // Using a codec for decoding specific classes simplifies JSON parsing a lot val parser = JSONParser.builder() - .decodingWith(EventCodec, InstantCodec) + .decodingWith(EventCodec) .build() val json = parser.parseValueOfType>(""" @@ -83,18 +82,4 @@ object DecodingExample { ) } } - - - private object InstantCodec : AbstractJSONDecoderCodec() { - - override fun decode(valueType: JSONCodableType, decoder: JSONDecoder): Instant = - decoder.readString().let { - try { - Instant.parse(it) - } - catch (e: DateTimeParseException) { - throw JSONException("Cannot parse Instant '$it'", e) - } - } - } } diff --git a/Examples/0032-TypeCodecs.kt b/Examples/0032-TypeCodecs.kt index d7f1857..f160b6b 100644 --- a/Examples/0032-TypeCodecs.kt +++ b/Examples/0032-TypeCodecs.kt @@ -2,8 +2,6 @@ package examples import com.github.fluidsonic.fluid.json.* import java.time.Instant -import java.time.format.DateTimeFormatter -import java.time.format.DateTimeParseException object CodingExample { @@ -22,13 +20,13 @@ object CodingExample { ) val serializer = JSONSerializer.builder() - .encodingWith(EventCodec, InstantCodec) + .encodingWith(EventCodec) .build() val json = serializer.serializeValue(input) val parser = JSONParser.builder() - .decodingWith(EventCodec, InstantCodec) + .decodingWith(EventCodec) .build() val output = parser.parseValueOfType>(json) @@ -78,23 +76,4 @@ object CodingExample { } } } - - - private object InstantCodec : AbstractJSONCodec() { - - override fun decode(valueType: JSONCodableType, decoder: JSONDecoder): Instant = - decoder.readString().let { - try { - Instant.parse(it) - } - catch (e: DateTimeParseException) { - throw JSONException("Cannot parse Instant '$it'", e) - } - } - - - override fun encode(value: Instant, encoder: JSONEncoder) { - encoder.writeString(DateTimeFormatter.ISO_INSTANT.format(value)) - } - } } diff --git a/Examples/0033-CodingAsStream.kt b/Examples/0033-CodingAsStream.kt index c8c6333..2cc13cb 100644 --- a/Examples/0033-CodingAsStream.kt +++ b/Examples/0033-CodingAsStream.kt @@ -3,8 +3,6 @@ package examples import com.github.fluidsonic.fluid.json.* import java.io.StringWriter import java.time.Instant -import java.time.format.DateTimeFormatter -import java.time.format.DateTimeParseException object CodingAsStreamExample { @@ -34,7 +32,7 @@ object CodingAsStreamExample { private fun decode(json: String) = JSONDecoder.builder() - .codecs(EventCodec, InstantCodec) + .codecs(EventCodec) .source(json) .build() .use { decoder -> @@ -54,7 +52,7 @@ object CodingAsStreamExample { val writer = StringWriter() return JSONEncoder.builder() - .codecs(EventCodec, InstantCodec) + .codecs(EventCodec) .destination(writer) .build() .use { encoder -> @@ -108,23 +106,4 @@ object CodingAsStreamExample { } } } - - - private object InstantCodec : AbstractJSONCodec() { - - override fun decode(valueType: JSONCodableType, decoder: JSONDecoder): Instant = - decoder.readString().let { - try { - Instant.parse(it) - } - catch (e: DateTimeParseException) { - throw JSONException("Cannot parse Instant '$it'", e) - } - } - - - override fun encode(value: Instant, encoder: JSONEncoder) { - encoder.writeString(DateTimeFormatter.ISO_INSTANT.format(value)) - } - } } diff --git a/README.md b/README.md index c3a4349..a12653f 100644 --- a/README.md +++ b/README.md @@ -324,6 +324,29 @@ The default implementations of `JSONReader` and `JSONParser` decode JSON types a | `object` | `Map` | | `string` | `String` | +### Extended Types + +The following classes of the `java.time` package can also be decoded and encoded out of the box: + +| Kotlin | JSON | Remarks +| ---------------- | -------- | ------- +| `DayOfWeek` | `string` | `"monday"`, …, `"friday"` +| `Duration` | `string` | using `.parse()` / `.toString()` +| `Instant` | `string` | using `.parse()` / `.toString()` +| `LocalDate` | `string` | using `.parse()` / `.toString()` +| `LocalDateTime` | `string` | using `.parse()` / `.toString()` +| `LocalTime` | `string` | using `.parse()` / `.toString()` +| `MonthDay` | `string` | using `.parse()` / `.toString()` +| `Month` | `string` | `"january"`, …, `"december"` +| `OffsetDateTime` | `string` | using `.parse()` / `.toString()` +| `OffsetTime` | `string` | using `.parse()` / `.toString()` +| `Period` | `string` | using `.parse()` / `.toString()` +| `Year` | `int` | using `.value` +| `YearMonth` | `string` | using `.parse()` / `.toString()` +| `ZonedDateTime` | `string` | using `.parse()` / `.toString()` +| `ZoneId` | `string` | using `.of()` / `.id` +| `ZoneOffset` | `string` | using `.of()` / `.id` + Architecture @@ -371,6 +394,7 @@ parser/serializer. | `AbstractJSONCodec` | Abstract base class which simplifies implementing `JSONCodec`. | `AbstractJSONDecoderCodec` | Abstract base class which simplifies implementing `JSONDecoderCodec`. | `AbstractJSONEncoderCodec` | Abstract base class which simplifies implementing `JSONEncoderCodec`. +| `DefaultJSONCodecs` | Contains lists of default codecs which can be used when contructing custom `JSONCodecProvider`s. | `JSONCodableType` | Roughly describes a Kotlin type which can be decoded from JSON. It includes relevant generic information which allows decoding for example `List` instead of just `List<*>`. Also known as [type token](http://gafter.blogspot.de/2006/12/super-type-tokens.html)). | `JSONCodec` | Interface for classes which implement both, `JSONEncoderCodec` and `JSONDecoderCodec`. Also simplifies creating such codecs. | `JSONCodecProvider` | Interface for classes which when given a `JSONCodableType` (for decoding) or `KClass` (for encoding) return a codec which is able to decode/encode values of that type. @@ -395,7 +419,6 @@ Future Planning This is on the backlog for later consideration, in no specific order: - [Add KDoc to all public API](https://github.com/fluidsonic/fluid-json/issues/28) -- [Add extended set of standard codecs (e.g. for `java.time`)](https://github.com/fluidsonic/fluid-json/issues/17) - [Add annotation-based preprocessor for automatic create codecs](https://github.com/fluidsonic/fluid-json/issues/16) - [Add performance testing](https://github.com/fluidsonic/fluid-json/issues/4) - [Add low-level support for `BigDecimal` / `BigInteger`](https://github.com/fluidsonic/fluid-json/issues/18) diff --git a/Sources/Codecs/AnyJSONDecoderCodec.kt b/Sources/Codecs/Basic/AnyJSONDecoderCodec.kt similarity index 100% rename from Sources/Codecs/AnyJSONDecoderCodec.kt rename to Sources/Codecs/Basic/AnyJSONDecoderCodec.kt diff --git a/Sources/Codecs/ArrayJSONCodec.kt b/Sources/Codecs/Basic/ArrayJSONCodec.kt similarity index 100% rename from Sources/Codecs/ArrayJSONCodec.kt rename to Sources/Codecs/Basic/ArrayJSONCodec.kt diff --git a/Sources/Codecs/BooleanArrayJSONCodec.kt b/Sources/Codecs/Basic/BooleanArrayJSONCodec.kt similarity index 100% rename from Sources/Codecs/BooleanArrayJSONCodec.kt rename to Sources/Codecs/Basic/BooleanArrayJSONCodec.kt diff --git a/Sources/Codecs/BooleanJSONCodec.kt b/Sources/Codecs/Basic/BooleanJSONCodec.kt similarity index 100% rename from Sources/Codecs/BooleanJSONCodec.kt rename to Sources/Codecs/Basic/BooleanJSONCodec.kt diff --git a/Sources/Codecs/ByteArrayJSONCodec.kt b/Sources/Codecs/Basic/ByteArrayJSONCodec.kt similarity index 100% rename from Sources/Codecs/ByteArrayJSONCodec.kt rename to Sources/Codecs/Basic/ByteArrayJSONCodec.kt diff --git a/Sources/Codecs/ByteJSONCodec.kt b/Sources/Codecs/Basic/ByteJSONCodec.kt similarity index 100% rename from Sources/Codecs/ByteJSONCodec.kt rename to Sources/Codecs/Basic/ByteJSONCodec.kt diff --git a/Sources/Codecs/DoubleArrayJSONCodec.kt b/Sources/Codecs/Basic/DoubleArrayJSONCodec.kt similarity index 100% rename from Sources/Codecs/DoubleArrayJSONCodec.kt rename to Sources/Codecs/Basic/DoubleArrayJSONCodec.kt diff --git a/Sources/Codecs/DoubleJSONCodec.kt b/Sources/Codecs/Basic/DoubleJSONCodec.kt similarity index 100% rename from Sources/Codecs/DoubleJSONCodec.kt rename to Sources/Codecs/Basic/DoubleJSONCodec.kt diff --git a/Sources/Codecs/FloatArrayJSONCodec.kt b/Sources/Codecs/Basic/FloatArrayJSONCodec.kt similarity index 100% rename from Sources/Codecs/FloatArrayJSONCodec.kt rename to Sources/Codecs/Basic/FloatArrayJSONCodec.kt diff --git a/Sources/Codecs/FloatJSONCodec.kt b/Sources/Codecs/Basic/FloatJSONCodec.kt similarity index 100% rename from Sources/Codecs/FloatJSONCodec.kt rename to Sources/Codecs/Basic/FloatJSONCodec.kt diff --git a/Sources/Codecs/IntArrayJSONCodec.kt b/Sources/Codecs/Basic/IntArrayJSONCodec.kt similarity index 100% rename from Sources/Codecs/IntArrayJSONCodec.kt rename to Sources/Codecs/Basic/IntArrayJSONCodec.kt diff --git a/Sources/Codecs/IntJSONCodec.kt b/Sources/Codecs/Basic/IntJSONCodec.kt similarity index 100% rename from Sources/Codecs/IntJSONCodec.kt rename to Sources/Codecs/Basic/IntJSONCodec.kt diff --git a/Sources/Codecs/IterableJSONEncoderCodec.kt b/Sources/Codecs/Basic/IterableJSONEncoderCodec.kt similarity index 100% rename from Sources/Codecs/IterableJSONEncoderCodec.kt rename to Sources/Codecs/Basic/IterableJSONEncoderCodec.kt diff --git a/Sources/Codecs/ListJSONDecoderCodec.kt b/Sources/Codecs/Basic/ListJSONDecoderCodec.kt similarity index 100% rename from Sources/Codecs/ListJSONDecoderCodec.kt rename to Sources/Codecs/Basic/ListJSONDecoderCodec.kt diff --git a/Sources/Codecs/LongArrayJSONCodec.kt b/Sources/Codecs/Basic/LongArrayJSONCodec.kt similarity index 100% rename from Sources/Codecs/LongArrayJSONCodec.kt rename to Sources/Codecs/Basic/LongArrayJSONCodec.kt diff --git a/Sources/Codecs/LongJSONCodec.kt b/Sources/Codecs/Basic/LongJSONCodec.kt similarity index 100% rename from Sources/Codecs/LongJSONCodec.kt rename to Sources/Codecs/Basic/LongJSONCodec.kt diff --git a/Sources/Codecs/MapJSONCodec.kt b/Sources/Codecs/Basic/MapJSONCodec.kt similarity index 100% rename from Sources/Codecs/MapJSONCodec.kt rename to Sources/Codecs/Basic/MapJSONCodec.kt diff --git a/Sources/Codecs/NonRecursiveJSONCodec.kt b/Sources/Codecs/Basic/NonRecursiveJSONCodec.kt similarity index 100% rename from Sources/Codecs/NonRecursiveJSONCodec.kt rename to Sources/Codecs/Basic/NonRecursiveJSONCodec.kt diff --git a/Sources/Codecs/NonRecursiveJSONDecoderCodec.kt b/Sources/Codecs/Basic/NonRecursiveJSONDecoderCodec.kt similarity index 100% rename from Sources/Codecs/NonRecursiveJSONDecoderCodec.kt rename to Sources/Codecs/Basic/NonRecursiveJSONDecoderCodec.kt diff --git a/Sources/Codecs/NonRecursiveJSONEncoderCodec.kt b/Sources/Codecs/Basic/NonRecursiveJSONEncoderCodec.kt similarity index 100% rename from Sources/Codecs/NonRecursiveJSONEncoderCodec.kt rename to Sources/Codecs/Basic/NonRecursiveJSONEncoderCodec.kt diff --git a/Sources/Codecs/NumberJSONCodec.kt b/Sources/Codecs/Basic/NumberJSONCodec.kt similarity index 100% rename from Sources/Codecs/NumberJSONCodec.kt rename to Sources/Codecs/Basic/NumberJSONCodec.kt diff --git a/Sources/Codecs/SequenceJSONCodec.kt b/Sources/Codecs/Basic/SequenceJSONCodec.kt similarity index 100% rename from Sources/Codecs/SequenceJSONCodec.kt rename to Sources/Codecs/Basic/SequenceJSONCodec.kt diff --git a/Sources/Codecs/ShortArrayJSONCodec.kt b/Sources/Codecs/Basic/ShortArrayJSONCodec.kt similarity index 100% rename from Sources/Codecs/ShortArrayJSONCodec.kt rename to Sources/Codecs/Basic/ShortArrayJSONCodec.kt diff --git a/Sources/Codecs/ShortJSONCodec.kt b/Sources/Codecs/Basic/ShortJSONCodec.kt similarity index 100% rename from Sources/Codecs/ShortJSONCodec.kt rename to Sources/Codecs/Basic/ShortJSONCodec.kt diff --git a/Sources/Codecs/StringJSONCodec.kt b/Sources/Codecs/Basic/StringJSONCodec.kt similarity index 100% rename from Sources/Codecs/StringJSONCodec.kt rename to Sources/Codecs/Basic/StringJSONCodec.kt diff --git a/Sources/Codecs/DefaultJSONCodecs.kt b/Sources/Codecs/DefaultJSONCodecs.kt new file mode 100644 index 0000000..a20abc1 --- /dev/null +++ b/Sources/Codecs/DefaultJSONCodecs.kt @@ -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 + ) +} diff --git a/Sources/Codecs/Extended/DayOfWeekJSONCodec.kt b/Sources/Codecs/Extended/DayOfWeekJSONCodec.kt new file mode 100644 index 0000000..b5bfc76 --- /dev/null +++ b/Sources/Codecs/Extended/DayOfWeekJSONCodec.kt @@ -0,0 +1,34 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DayOfWeek + + +// TODO use Enum codec once implemented +object DayOfWeekJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + 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) = + 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" + }) +} diff --git a/Sources/Codecs/Extended/DurationJSONCodec.kt b/Sources/Codecs/Extended/DurationJSONCodec.kt new file mode 100644 index 0000000..f40d4bc --- /dev/null +++ b/Sources/Codecs/Extended/DurationJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.Duration + + +object DurationJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + 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) = + encoder.writeString(value.toString()) +} diff --git a/Sources/Codecs/Extended/InstantJSONCodec.kt b/Sources/Codecs/Extended/InstantJSONCodec.kt new file mode 100644 index 0000000..8b1a354 --- /dev/null +++ b/Sources/Codecs/Extended/InstantJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.Instant + + +object InstantJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + 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) = + encoder.writeString(value.toString()) +} diff --git a/Sources/Codecs/Extended/LocalDateJSONCodec.kt b/Sources/Codecs/Extended/LocalDateJSONCodec.kt new file mode 100644 index 0000000..a41f61b --- /dev/null +++ b/Sources/Codecs/Extended/LocalDateJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.LocalDate + + +object LocalDateJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + 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) = + encoder.writeString(value.toString()) +} diff --git a/Sources/Codecs/Extended/LocalDateTimeJSONCodec.kt b/Sources/Codecs/Extended/LocalDateTimeJSONCodec.kt new file mode 100644 index 0000000..0dc4398 --- /dev/null +++ b/Sources/Codecs/Extended/LocalDateTimeJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.LocalDateTime + + +object LocalDateTimeJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + 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) = + encoder.writeString(value.toString()) +} diff --git a/Sources/Codecs/Extended/LocalTimeJSONCodec.kt b/Sources/Codecs/Extended/LocalTimeJSONCodec.kt new file mode 100644 index 0000000..74570d5 --- /dev/null +++ b/Sources/Codecs/Extended/LocalTimeJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.LocalTime + + +object LocalTimeJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + decoder.readString().let { raw -> + try { + LocalTime.parse(raw)!! + } + catch (e: DateTimeException) { + throw JSONException("Cannot parse LocalTime value: $raw") + } + } + + + override fun encode(value: LocalTime, encoder: JSONEncoder) = + encoder.writeString(value.toString()) +} diff --git a/Sources/Codecs/Extended/MonthDayJSONCodec.kt b/Sources/Codecs/Extended/MonthDayJSONCodec.kt new file mode 100644 index 0000000..1bebaa5 --- /dev/null +++ b/Sources/Codecs/Extended/MonthDayJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.MonthDay + + +object MonthDayJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + decoder.readString().let { raw -> + try { + MonthDay.parse(raw)!! + } + catch (e: DateTimeException) { + throw JSONException("Cannot parse MonthDay value: $raw") + } + } + + + override fun encode(value: MonthDay, encoder: JSONEncoder) = + encoder.writeString(value.toString()) +} diff --git a/Sources/Codecs/Extended/MonthJSONCodec.kt b/Sources/Codecs/Extended/MonthJSONCodec.kt new file mode 100644 index 0000000..4de8ac1 --- /dev/null +++ b/Sources/Codecs/Extended/MonthJSONCodec.kt @@ -0,0 +1,44 @@ +package com.github.fluidsonic.fluid.json + +import java.time.Month + + +// TODO use Enum codec once implemented +object MonthJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + decoder.readString().let { raw -> + when (raw) { + "january" -> Month.JANUARY + "february" -> Month.FEBRUARY + "march" -> Month.MARCH + "april" -> Month.APRIL + "may" -> Month.MAY + "june" -> Month.JUNE + "july" -> Month.JULY + "august" -> Month.AUGUST + "september" -> Month.SEPTEMBER + "october" -> Month.OCTOBER + "november" -> Month.NOVEMBER + "december" -> Month.DECEMBER + else -> throw JSONException("Invalid Month value: $raw") + } + } + + + override fun encode(value: Month, encoder: JSONEncoder) = + encoder.writeString(when (value) { + Month.JANUARY -> "january" + Month.FEBRUARY -> "february" + Month.MARCH -> "march" + Month.APRIL -> "april" + Month.MAY -> "may" + Month.JUNE -> "june" + Month.JULY -> "july" + Month.AUGUST -> "august" + Month.SEPTEMBER -> "september" + Month.OCTOBER -> "october" + Month.NOVEMBER -> "november" + Month.DECEMBER -> "december" + }) +} diff --git a/Sources/Codecs/Extended/OffsetDateTimeJSONCodec.kt b/Sources/Codecs/Extended/OffsetDateTimeJSONCodec.kt new file mode 100644 index 0000000..230cb31 --- /dev/null +++ b/Sources/Codecs/Extended/OffsetDateTimeJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.OffsetDateTime + + +object OffsetDateTimeJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + decoder.readString().let { raw -> + try { + OffsetDateTime.parse(raw)!! + } + catch (e: DateTimeException) { + throw JSONException("Cannot parse OffsetDateTime value: $raw") + } + } + + + override fun encode(value: OffsetDateTime, encoder: JSONEncoder) = + encoder.writeString(value.toString()) +} diff --git a/Sources/Codecs/Extended/OffsetTimeJSONCodec.kt b/Sources/Codecs/Extended/OffsetTimeJSONCodec.kt new file mode 100644 index 0000000..4460ff6 --- /dev/null +++ b/Sources/Codecs/Extended/OffsetTimeJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.OffsetTime + + +object OffsetTimeJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + decoder.readString().let { raw -> + try { + OffsetTime.parse(raw)!! + } + catch (e: DateTimeException) { + throw JSONException("Cannot parse OffsetTime value: $raw") + } + } + + + override fun encode(value: OffsetTime, encoder: JSONEncoder) = + encoder.writeString(value.toString()) +} diff --git a/Sources/Codecs/Extended/PeriodJSONCodec.kt b/Sources/Codecs/Extended/PeriodJSONCodec.kt new file mode 100644 index 0000000..c063023 --- /dev/null +++ b/Sources/Codecs/Extended/PeriodJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.Period + + +object PeriodJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + decoder.readString().let { raw -> + try { + Period.parse(raw)!! + } + catch (e: DateTimeException) { + throw JSONException("Cannot parse Period value: $raw") + } + } + + + override fun encode(value: Period, encoder: JSONEncoder) = + encoder.writeString(value.toString()) +} diff --git a/Sources/Codecs/Extended/YearJSONCodec.kt b/Sources/Codecs/Extended/YearJSONCodec.kt new file mode 100644 index 0000000..b616260 --- /dev/null +++ b/Sources/Codecs/Extended/YearJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.Year + + +object YearJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + decoder.readInt().let { raw -> + try { + Year.of(raw)!! + } + catch (e: DateTimeException) { + throw JSONException("Invalid Year value: $raw") + } + } + + + override fun encode(value: Year, encoder: JSONEncoder) = + encoder.writeInt(value.value) +} diff --git a/Sources/Codecs/Extended/YearMonthJSONCodec.kt b/Sources/Codecs/Extended/YearMonthJSONCodec.kt new file mode 100644 index 0000000..011c5db --- /dev/null +++ b/Sources/Codecs/Extended/YearMonthJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.YearMonth + + +object YearMonthJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + decoder.readString().let { raw -> + try { + YearMonth.parse(raw)!! + } + catch (e: DateTimeException) { + throw JSONException("Cannot parse YearMonth value: $raw") + } + } + + + override fun encode(value: YearMonth, encoder: JSONEncoder) = + encoder.writeString(value.toString()) +} diff --git a/Sources/Codecs/Extended/ZoneIdJSONCodec.kt b/Sources/Codecs/Extended/ZoneIdJSONCodec.kt new file mode 100644 index 0000000..996001b --- /dev/null +++ b/Sources/Codecs/Extended/ZoneIdJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.ZoneId + + +object ZoneIdJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + decoder.readString().let { raw -> + try { + ZoneId.of(raw)!! + } + catch (e: DateTimeException) { + throw JSONException("Cannot parse ZoneId value: $raw") + } + } + + + override fun encode(value: ZoneId, encoder: JSONEncoder) = + encoder.writeString(value.id) +} diff --git a/Sources/Codecs/Extended/ZoneOffsetJSONCodec.kt b/Sources/Codecs/Extended/ZoneOffsetJSONCodec.kt new file mode 100644 index 0000000..2d45115 --- /dev/null +++ b/Sources/Codecs/Extended/ZoneOffsetJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.ZoneOffset + + +object ZoneOffsetJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + decoder.readString().let { raw -> + try { + ZoneOffset.of(raw)!! + } + catch (e: DateTimeException) { + throw JSONException("Cannot parse ZoneOffset value: $raw") + } + } + + + override fun encode(value: ZoneOffset, encoder: JSONEncoder) = + encoder.writeString(value.id) +} diff --git a/Sources/Codecs/Extended/ZonedDateTimeJSONCodec.kt b/Sources/Codecs/Extended/ZonedDateTimeJSONCodec.kt new file mode 100644 index 0000000..436d72f --- /dev/null +++ b/Sources/Codecs/Extended/ZonedDateTimeJSONCodec.kt @@ -0,0 +1,22 @@ +package com.github.fluidsonic.fluid.json + +import java.time.DateTimeException +import java.time.ZonedDateTime + + +object ZonedDateTimeJSONCodec : AbstractJSONCodec() { + + override fun decode(valueType: JSONCodableType, decoder: JSONDecoder) = + decoder.readString().let { raw -> + try { + ZonedDateTime.parse(raw)!! + } + catch (e: DateTimeException) { + throw JSONException("Cannot parse ZonedDateTime value: $raw") + } + } + + + override fun encode(value: ZonedDateTime, encoder: JSONEncoder) = + encoder.writeString(value.toString()) +} diff --git a/Sources/JSONCodecProvider.kt b/Sources/JSONCodecProvider.kt index eeba1a0..de862d4 100644 --- a/Sources/JSONCodecProvider.kt +++ b/Sources/JSONCodecProvider.kt @@ -11,61 +11,22 @@ interface JSONCodecProvider { companion object { - val basic by lazy { - of( - 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 - appendBasic = false - ) - } - - - val default by lazy { - of( - ArrayJSONCodec.nonRecursive, - ListJSONDecoderCodec.nonRecursive, - MapJSONCodec.nonRecursive, - SequenceJSONCodec.nonRecursive, - IterableJSONEncoderCodec.nonRecursive // after subclasses - ) - } + val basic = of(DefaultJSONCodecs.basic + DefaultJSONCodecs.nonRecursive) + val extended = of(DefaultJSONCodecs.extended + DefaultJSONCodecs.basic + DefaultJSONCodecs.nonRecursive) fun of( vararg providers: JSONCodecProvider, - appendBasic: Boolean = true + base: JSONCodecProvider? = extended ) = - of(providers.asIterable(), appendBasic = appendBasic) + of(providers.asIterable(), base = base) fun of( providers: Iterable>, - appendBasic: Boolean = true + base: JSONCodecProvider? = extended ): JSONCodecProvider = - if (appendBasic) - StandardCodecProvider(providers = providers + basic) - else - StandardCodecProvider(providers = providers) + StandardCodecProvider(providers = base?.let { providers + it } ?: providers) } } diff --git a/Sources/JSONDecoder.kt b/Sources/JSONDecoder.kt index 79414fd..989714b 100644 --- a/Sources/JSONDecoder.kt +++ b/Sources/JSONDecoder.kt @@ -28,21 +28,17 @@ interface JSONDecoder : JSONReader { interface BuilderForCodecs { - fun codecs(provider: JSONCodecProvider): BuilderForSource - - fun codecs( vararg providers: JSONCodecProvider, - appendDefault: Boolean = true + base: JSONCodecProvider? = JSONCodecProvider.extended ) = - codecs(JSONCodecProvider.of(providers = *providers, appendBasic = appendDefault)) + codecs(providers = providers.toList(), base = base) fun codecs( providers: Iterable>, - appendDefault: Boolean = true - ) = - codecs(JSONCodecProvider.of(providers = providers, appendBasic = appendDefault)) + base: JSONCodecProvider? = JSONCodecProvider.extended + ): BuilderForSource } @@ -50,10 +46,13 @@ interface JSONDecoder : JSONReader { private val context: Context ) : BuilderForCodecs { - override fun codecs(provider: JSONCodecProvider) = + override fun codecs( + providers: Iterable>, + base: JSONCodecProvider? + ) = BuilderForSourceImpl( context = context, - codecProvider = provider + codecProvider = JSONCodecProvider.of(providers = providers, base = base) ) } diff --git a/Sources/JSONEncoder.kt b/Sources/JSONEncoder.kt index db198bc..ae58266 100644 --- a/Sources/JSONEncoder.kt +++ b/Sources/JSONEncoder.kt @@ -20,21 +20,17 @@ interface JSONEncoder : JSONWriter { interface BuilderForCodecs { - fun codecs(provider: JSONCodecProvider): BuilderForDestination - - fun codecs( vararg providers: JSONCodecProvider, - appendBasic: Boolean = true + base: JSONCodecProvider? = JSONCodecProvider.extended ) = - codecs(JSONCodecProvider.of(providers = *providers, appendBasic = appendBasic)) + codecs(providers = providers.toList(), base = base) fun codecs( providers: Iterable>, - appendBasic: Boolean = true - ) = - codecs(JSONCodecProvider.of(providers = providers, appendBasic = appendBasic)) + base: JSONCodecProvider? = JSONCodecProvider.extended + ): BuilderForDestination } @@ -42,10 +38,13 @@ interface JSONEncoder : JSONWriter { private val context: Context ) : BuilderForCodecs { - override fun codecs(provider: JSONCodecProvider) = + override fun codecs( + providers: Iterable>, + base: JSONCodecProvider? + ) = BuilderForDestinationImpl( context = context, - codecProvider = provider + codecProvider = JSONCodecProvider.of(providers = providers, base = base) ) } diff --git a/Sources/JSONParser.kt b/Sources/JSONParser.kt index e04ec51..aea3695 100644 --- a/Sources/JSONParser.kt +++ b/Sources/JSONParser.kt @@ -20,7 +20,7 @@ interface JSONParser { val default = builder() - .decodingWith(JSONCodecProvider.default, appendBasic = false) + .decodingWith(DefaultJSONCodecs.nonRecursive) .build() @@ -29,29 +29,23 @@ interface JSONParser { fun decodingWith(factory: (source: Reader, context: Context) -> JSONDecoder): Builder - private fun decodingWith( - provider: JSONCodecProvider - ): Builder = - decodingWith { source, context -> - JSONDecoder.builder(context) - .codecs(provider) - .source(source) - .build() - } - - fun decodingWith( vararg providers: JSONCodecProvider, - appendBasic: Boolean = true + base: JSONCodecProvider? = JSONCodecProvider.extended ) = - decodingWith(JSONCodecProvider.of(providers = *providers, appendBasic = appendBasic)) + decodingWith(providers = providers.toList(), base = base) fun decodingWith( providers: Iterable>, - appendBasic: Boolean = true + base: JSONCodecProvider? = JSONCodecProvider.extended ) = - decodingWith(JSONCodecProvider.of(providers = providers, appendBasic = appendBasic)) + decodingWith { source, context -> + JSONDecoder.builder(context) + .codecs(JSONCodecProvider.of(providers = providers, base = base)) + .source(source) + .build() + } } diff --git a/Sources/JSONSerializer.kt b/Sources/JSONSerializer.kt index b12207f..b56baa7 100644 --- a/Sources/JSONSerializer.kt +++ b/Sources/JSONSerializer.kt @@ -21,7 +21,7 @@ interface JSONSerializer { val default = builder() - .encodingWith(JSONCodecProvider.default) + .encodingWith(DefaultJSONCodecs.nonRecursive) .build() @@ -30,27 +30,23 @@ interface JSONSerializer { fun encodingWith(factory: (destination: Writer, context: Context) -> JSONEncoder): Builder - private fun encodingWith(provider: JSONCodecProvider): Builder = - encodingWith { destination, context -> - JSONEncoder.builder(context) - .codecs(provider) - .destination(destination) - .build() - } - - fun encodingWith( vararg providers: JSONCodecProvider, - appendBasic: Boolean = true + base: JSONCodecProvider? = JSONCodecProvider.extended ) = - encodingWith(JSONCodecProvider.of(providers = *providers, appendBasic = appendBasic)) + encodingWith(providers = providers.toList(), base = base) fun encodingWith( providers: Iterable>, - appendBasic: Boolean = true + base: JSONCodecProvider? = JSONCodecProvider.extended ) = - encodingWith(JSONCodecProvider.of(providers = providers, appendBasic = appendBasic)) + encodingWith { destination, context -> + JSONEncoder.builder(context) + .codecs(JSONCodecProvider.of(providers = providers, base = base)) + .destination(destination) + .build() + } } diff --git a/Tests/Sources/Data/arrayData.kt b/Tests/Sources/Data/Basic/arrayData.kt similarity index 100% rename from Tests/Sources/Data/arrayData.kt rename to Tests/Sources/Data/Basic/arrayData.kt diff --git a/Tests/Sources/Data/bigDecimalData.kt b/Tests/Sources/Data/Basic/bigDecimalData.kt similarity index 100% rename from Tests/Sources/Data/bigDecimalData.kt rename to Tests/Sources/Data/Basic/bigDecimalData.kt diff --git a/Tests/Sources/Data/booleanArrayData.kt b/Tests/Sources/Data/Basic/booleanArrayData.kt similarity index 100% rename from Tests/Sources/Data/booleanArrayData.kt rename to Tests/Sources/Data/Basic/booleanArrayData.kt diff --git a/Tests/Sources/Data/booleanData.kt b/Tests/Sources/Data/Basic/booleanData.kt similarity index 100% rename from Tests/Sources/Data/booleanData.kt rename to Tests/Sources/Data/Basic/booleanData.kt diff --git a/Tests/Sources/Data/byteArrayData.kt b/Tests/Sources/Data/Basic/byteArrayData.kt similarity index 100% rename from Tests/Sources/Data/byteArrayData.kt rename to Tests/Sources/Data/Basic/byteArrayData.kt diff --git a/Tests/Sources/Data/byteData.kt b/Tests/Sources/Data/Basic/byteData.kt similarity index 100% rename from Tests/Sources/Data/byteData.kt rename to Tests/Sources/Data/Basic/byteData.kt diff --git a/Tests/Sources/Data/complexData.kt b/Tests/Sources/Data/Basic/complexData.kt similarity index 100% rename from Tests/Sources/Data/complexData.kt rename to Tests/Sources/Data/Basic/complexData.kt diff --git a/Tests/Sources/Data/doubleArrayData.kt b/Tests/Sources/Data/Basic/doubleArrayData.kt similarity index 100% rename from Tests/Sources/Data/doubleArrayData.kt rename to Tests/Sources/Data/Basic/doubleArrayData.kt diff --git a/Tests/Sources/Data/doubleData.kt b/Tests/Sources/Data/Basic/doubleData.kt similarity index 100% rename from Tests/Sources/Data/doubleData.kt rename to Tests/Sources/Data/Basic/doubleData.kt diff --git a/Tests/Sources/Data/floatArrayData.kt b/Tests/Sources/Data/Basic/floatArrayData.kt similarity index 100% rename from Tests/Sources/Data/floatArrayData.kt rename to Tests/Sources/Data/Basic/floatArrayData.kt diff --git a/Tests/Sources/Data/floatData.kt b/Tests/Sources/Data/Basic/floatData.kt similarity index 100% rename from Tests/Sources/Data/floatData.kt rename to Tests/Sources/Data/Basic/floatData.kt diff --git a/Tests/Sources/Data/intArrayData.kt b/Tests/Sources/Data/Basic/intArrayData.kt similarity index 100% rename from Tests/Sources/Data/intArrayData.kt rename to Tests/Sources/Data/Basic/intArrayData.kt diff --git a/Tests/Sources/Data/intData.kt b/Tests/Sources/Data/Basic/intData.kt similarity index 100% rename from Tests/Sources/Data/intData.kt rename to Tests/Sources/Data/Basic/intData.kt diff --git a/Tests/Sources/Data/iterableData.kt b/Tests/Sources/Data/Basic/iterableData.kt similarity index 100% rename from Tests/Sources/Data/iterableData.kt rename to Tests/Sources/Data/Basic/iterableData.kt diff --git a/Tests/Sources/Data/listData.kt b/Tests/Sources/Data/Basic/listData.kt similarity index 100% rename from Tests/Sources/Data/listData.kt rename to Tests/Sources/Data/Basic/listData.kt diff --git a/Tests/Sources/Data/longArrayData.kt b/Tests/Sources/Data/Basic/longArrayData.kt similarity index 100% rename from Tests/Sources/Data/longArrayData.kt rename to Tests/Sources/Data/Basic/longArrayData.kt diff --git a/Tests/Sources/Data/longData.kt b/Tests/Sources/Data/Basic/longData.kt similarity index 100% rename from Tests/Sources/Data/longData.kt rename to Tests/Sources/Data/Basic/longData.kt diff --git a/Tests/Sources/Data/mapData.kt b/Tests/Sources/Data/Basic/mapData.kt similarity index 100% rename from Tests/Sources/Data/mapData.kt rename to Tests/Sources/Data/Basic/mapData.kt diff --git a/Tests/Sources/Data/mapDataWithNonStringKeys.kt b/Tests/Sources/Data/Basic/mapDataWithNonStringKeys.kt similarity index 100% rename from Tests/Sources/Data/mapDataWithNonStringKeys.kt rename to Tests/Sources/Data/Basic/mapDataWithNonStringKeys.kt diff --git a/Tests/Sources/Data/numberData.kt b/Tests/Sources/Data/Basic/numberData.kt similarity index 100% rename from Tests/Sources/Data/numberData.kt rename to Tests/Sources/Data/Basic/numberData.kt diff --git a/Tests/Sources/Data/plainData.kt b/Tests/Sources/Data/Basic/plainData.kt similarity index 100% rename from Tests/Sources/Data/plainData.kt rename to Tests/Sources/Data/Basic/plainData.kt diff --git a/Tests/Sources/Data/sequenceData.kt b/Tests/Sources/Data/Basic/sequenceData.kt similarity index 100% rename from Tests/Sources/Data/sequenceData.kt rename to Tests/Sources/Data/Basic/sequenceData.kt diff --git a/Tests/Sources/Data/shortArrayData.kt b/Tests/Sources/Data/Basic/shortArrayData.kt similarity index 100% rename from Tests/Sources/Data/shortArrayData.kt rename to Tests/Sources/Data/Basic/shortArrayData.kt diff --git a/Tests/Sources/Data/shortData.kt b/Tests/Sources/Data/Basic/shortData.kt similarity index 100% rename from Tests/Sources/Data/shortData.kt rename to Tests/Sources/Data/Basic/shortData.kt diff --git a/Tests/Sources/Data/stringData.kt b/Tests/Sources/Data/Basic/stringData.kt similarity index 100% rename from Tests/Sources/Data/stringData.kt rename to Tests/Sources/Data/Basic/stringData.kt diff --git a/Tests/Sources/Data/Extended/dayOfWeekData.kt b/Tests/Sources/Data/Extended/dayOfWeekData.kt new file mode 100644 index 0000000..006f787 --- /dev/null +++ b/Tests/Sources/Data/Extended/dayOfWeekData.kt @@ -0,0 +1,16 @@ +package tests + +import java.time.DayOfWeek + + +internal val dayOfWeekData: TestData = TestData( + symmetric = mapOf( + DayOfWeek.MONDAY to "\"monday\"", + DayOfWeek.TUESDAY to "\"tuesday\"", + DayOfWeek.WEDNESDAY to "\"wednesday\"", + DayOfWeek.THURSDAY to "\"thursday\"", + DayOfWeek.FRIDAY to "\"friday\"", + DayOfWeek.SATURDAY to "\"saturday\"", + DayOfWeek.SUNDAY to "\"sunday\"" + ) +) diff --git a/Tests/Sources/Data/Extended/durationData.kt b/Tests/Sources/Data/Extended/durationData.kt new file mode 100644 index 0000000..30619a7 --- /dev/null +++ b/Tests/Sources/Data/Extended/durationData.kt @@ -0,0 +1,13 @@ +package tests + +import java.time.Duration + + +internal val durationData: TestData = TestData( + symmetric = mapOf( + Duration.parse("PT51H4M") to "\"PT51H4M\"" + ), + decodableOnly = mapOf( + "\"P2DT3H4M\"" to Duration.parse("P2DT3H4M") + ) +) diff --git a/Tests/Sources/Data/Extended/instantData.kt b/Tests/Sources/Data/Extended/instantData.kt new file mode 100644 index 0000000..8d8a1af --- /dev/null +++ b/Tests/Sources/Data/Extended/instantData.kt @@ -0,0 +1,13 @@ +package tests + +import java.time.Instant + + +internal val instantData: TestData = TestData( + symmetric = mapOf( + Instant.parse("2007-12-03T10:15:30Z") to "\"2007-12-03T10:15:30Z\"" + ), + decodableOnly = mapOf( + "\"2007-12-03T10:15:30.00Z\"" to Instant.parse("2007-12-03T10:15:30.00Z") + ) +) diff --git a/Tests/Sources/Data/Extended/localDateData.kt b/Tests/Sources/Data/Extended/localDateData.kt new file mode 100644 index 0000000..a9f919b --- /dev/null +++ b/Tests/Sources/Data/Extended/localDateData.kt @@ -0,0 +1,10 @@ +package tests + +import java.time.LocalDate + + +internal val localDateData: TestData = TestData( + symmetric = mapOf( + LocalDate.parse("2007-12-03") to "\"2007-12-03\"" + ) +) diff --git a/Tests/Sources/Data/Extended/localDateTimeData.kt b/Tests/Sources/Data/Extended/localDateTimeData.kt new file mode 100644 index 0000000..b0798e8 --- /dev/null +++ b/Tests/Sources/Data/Extended/localDateTimeData.kt @@ -0,0 +1,10 @@ +package tests + +import java.time.LocalDateTime + + +internal val localDateTimeData: TestData = TestData( + symmetric = mapOf( + LocalDateTime.parse("2007-12-03T10:15:30") to "\"2007-12-03T10:15:30\"" + ) +) diff --git a/Tests/Sources/Data/Extended/localTimeData.kt b/Tests/Sources/Data/Extended/localTimeData.kt new file mode 100644 index 0000000..d063211 --- /dev/null +++ b/Tests/Sources/Data/Extended/localTimeData.kt @@ -0,0 +1,10 @@ +package tests + +import java.time.LocalTime + + +internal val localTimeData: TestData = TestData( + symmetric = mapOf( + LocalTime.parse("10:15:30") to "\"10:15:30\"" + ) +) diff --git a/Tests/Sources/Data/Extended/monthData.kt b/Tests/Sources/Data/Extended/monthData.kt new file mode 100644 index 0000000..0256588 --- /dev/null +++ b/Tests/Sources/Data/Extended/monthData.kt @@ -0,0 +1,21 @@ +package tests + +import java.time.Month + + +internal val monthData: TestData = TestData( + symmetric = mapOf( + Month.JANUARY to "\"january\"", + Month.FEBRUARY to "\"february\"", + Month.MARCH to "\"march\"", + Month.APRIL to "\"april\"", + Month.MAY to "\"may\"", + Month.JUNE to "\"june\"", + Month.JULY to "\"july\"", + Month.AUGUST to "\"august\"", + Month.SEPTEMBER to "\"september\"", + Month.OCTOBER to "\"october\"", + Month.NOVEMBER to "\"november\"", + Month.DECEMBER to "\"december\"" + ) +) diff --git a/Tests/Sources/Data/Extended/monthDayData.kt b/Tests/Sources/Data/Extended/monthDayData.kt new file mode 100644 index 0000000..af9012d --- /dev/null +++ b/Tests/Sources/Data/Extended/monthDayData.kt @@ -0,0 +1,10 @@ +package tests + +import java.time.MonthDay + + +internal val monthDayData: TestData = TestData( + symmetric = mapOf( + MonthDay.parse("--12-03") to "\"--12-03\"" + ) +) diff --git a/Tests/Sources/Data/Extended/offsetDateTimeData.kt b/Tests/Sources/Data/Extended/offsetDateTimeData.kt new file mode 100644 index 0000000..b1e8c2c --- /dev/null +++ b/Tests/Sources/Data/Extended/offsetDateTimeData.kt @@ -0,0 +1,10 @@ +package tests + +import java.time.OffsetDateTime + + +internal val offsetDateTimeData: TestData = TestData( + symmetric = mapOf( + OffsetDateTime.parse("2007-12-03T10:15:30+01:00") to "\"2007-12-03T10:15:30+01:00\"" + ) +) diff --git a/Tests/Sources/Data/Extended/offsetTimeData.kt b/Tests/Sources/Data/Extended/offsetTimeData.kt new file mode 100644 index 0000000..31464af --- /dev/null +++ b/Tests/Sources/Data/Extended/offsetTimeData.kt @@ -0,0 +1,10 @@ +package tests + +import java.time.OffsetTime + + +internal val offsetTimeData: TestData = TestData( + symmetric = mapOf( + OffsetTime.parse("10:15:30+01:00") to "\"10:15:30+01:00\"" + ) +) diff --git a/Tests/Sources/Data/Extended/periodData.kt b/Tests/Sources/Data/Extended/periodData.kt new file mode 100644 index 0000000..dfeeda3 --- /dev/null +++ b/Tests/Sources/Data/Extended/periodData.kt @@ -0,0 +1,13 @@ +package tests + +import java.time.Period + + +internal val periodData: TestData = TestData( + symmetric = mapOf( + Period.parse("P1Y2M25D") to "\"P1Y2M25D\"" + ), + decodableOnly = mapOf( + "\"P1Y2M3W4D\"" to Period.parse("P1Y2M3W4D") + ) +) diff --git a/Tests/Sources/Data/Extended/yearData.kt b/Tests/Sources/Data/Extended/yearData.kt new file mode 100644 index 0000000..d36cc4d --- /dev/null +++ b/Tests/Sources/Data/Extended/yearData.kt @@ -0,0 +1,10 @@ +package tests + +import java.time.Year + + +internal val yearData: TestData = TestData( + symmetric = mapOf( + Year.of(2007) to "2007" + ) +) diff --git a/Tests/Sources/Data/Extended/yearMonthData.kt b/Tests/Sources/Data/Extended/yearMonthData.kt new file mode 100644 index 0000000..cffa804 --- /dev/null +++ b/Tests/Sources/Data/Extended/yearMonthData.kt @@ -0,0 +1,10 @@ +package tests + +import java.time.YearMonth + + +internal val yearMonthData: TestData = TestData( + symmetric = mapOf( + YearMonth.parse("2007-12") to "\"2007-12\"" + ) +) diff --git a/Tests/Sources/Data/Extended/zoneIdData.kt b/Tests/Sources/Data/Extended/zoneIdData.kt new file mode 100644 index 0000000..539bf59 --- /dev/null +++ b/Tests/Sources/Data/Extended/zoneIdData.kt @@ -0,0 +1,10 @@ +package tests + +import java.time.ZoneId + + +internal val zoneIdData: TestData = TestData( + symmetric = mapOf( + ZoneId.of("Europe/Berlin") to "\"Europe/Berlin\"" + ) +) diff --git a/Tests/Sources/Data/Extended/zoneOffsetData.kt b/Tests/Sources/Data/Extended/zoneOffsetData.kt new file mode 100644 index 0000000..538c32b --- /dev/null +++ b/Tests/Sources/Data/Extended/zoneOffsetData.kt @@ -0,0 +1,10 @@ +package tests + +import java.time.ZoneOffset + + +internal val zoneOffsetData: TestData = TestData( + symmetric = mapOf( + ZoneOffset.of("+01:00") to "\"+01:00\"" + ) +) diff --git a/Tests/Sources/Data/Extended/zonedDateTimeData.kt b/Tests/Sources/Data/Extended/zonedDateTimeData.kt new file mode 100644 index 0000000..7915f4f --- /dev/null +++ b/Tests/Sources/Data/Extended/zonedDateTimeData.kt @@ -0,0 +1,10 @@ +package tests + +import java.time.ZonedDateTime + + +internal val zonedDateTimeData: TestData = TestData( + symmetric = mapOf( + ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]") to "\"2007-12-03T10:15:30+01:00[Europe/Paris]\"" + ) +) diff --git a/Tests/Sources/JSONDecoderSpec.kt b/Tests/Sources/JSONDecoderSpec.kt index 9a293ab..8778b70 100644 --- a/Tests/Sources/JSONDecoderSpec.kt +++ b/Tests/Sources/JSONDecoderSpec.kt @@ -13,33 +13,6 @@ internal object JSONDecoderSpec : Spek({ describe("JSONDecoder") { it(".builder()") { - JSONDecoder.builder() - .codecs(JSONCodecProvider.default) - .source(JSONReader.build(StringReader("true"))) - .build() - .apply { - context.should.equal(JSONCoderContext.empty) - readBoolean().should.be.`true` - } - - JSONDecoder.builder() - .codecs(JSONCodecProvider.default) - .source(StringReader("true")) - .build() - .apply { - context.should.equal(JSONCoderContext.empty) - readBoolean().should.be.`true` - } - - JSONDecoder.builder() - .codecs(JSONCodecProvider.default) - .source("true") - .build() - .apply { - context.should.equal(JSONCoderContext.empty) - readBoolean().should.be.`true` - } - JSONDecoder.builder() .codecs(BooleanJSONCodec) .source("true") @@ -51,7 +24,7 @@ internal object JSONDecoderSpec : Spek({ JSONDecoder.builder() .codecs(listOf(BooleanJSONCodec)) - .source("true") + .source(StringReader("true")) .build() .apply { context.should.equal(JSONCoderContext.empty) @@ -61,7 +34,7 @@ internal object JSONDecoderSpec : Spek({ val testContext = TestCoderContext() JSONDecoder.builder(testContext) - .codecs(JSONCodecProvider.default) + .codecs() .source(JSONReader.build(StringReader("true"))) .build() .apply { diff --git a/Tests/Sources/JSONEncoderSpec.kt b/Tests/Sources/JSONEncoderSpec.kt index 4c93f00..94b10f5 100644 --- a/Tests/Sources/JSONEncoderSpec.kt +++ b/Tests/Sources/JSONEncoderSpec.kt @@ -13,30 +13,6 @@ internal object JSONEncoderSpec : Spek({ describe("JSONEncoder") { it(".builder()") { - StringWriter().let { writer -> - JSONEncoder.builder() - .codecs(JSONCodecProvider.default) - .destination(JSONWriter.build(writer)) - .build() - .apply { - context.should.equal(JSONCoderContext.empty) - writeBoolean(true) - writer.toString().should.equal("true") - } - } - - StringWriter().let { writer -> - JSONEncoder.builder() - .codecs(JSONCodecProvider.default) - .destination(writer) - .build() - .apply { - context.should.equal(JSONCoderContext.empty) - writeBoolean(true) - writer.toString().should.equal("true") - } - } - StringWriter().let { writer -> JSONEncoder.builder() .codecs(BooleanJSONCodec) @@ -65,7 +41,7 @@ internal object JSONEncoderSpec : Spek({ StringWriter().let { writer -> JSONEncoder.builder(testContext) - .codecs(JSONCodecProvider.default) + .codecs() .destination(JSONWriter.build(writer)) .build() .apply { @@ -77,7 +53,7 @@ internal object JSONEncoderSpec : Spek({ StringWriter().let { writer -> JSONEncoder.builder(testContext) - .codecs(JSONCodecProvider.default) + .codecs() .destination(writer) .build() .apply { diff --git a/Tests/Sources/JSONParserSpec.kt b/Tests/Sources/JSONParserSpec.kt index f3c4e3a..c0a173a 100644 --- a/Tests/Sources/JSONParserSpec.kt +++ b/Tests/Sources/JSONParserSpec.kt @@ -15,7 +15,7 @@ internal object JSONParserSpec : Spek({ it(".builder()") { JSONParser.builder() - .decodingWith(JSONCodecProvider.default, appendBasic = false) + .decodingWith(BooleanJSONCodec) .build() .apply { // TODO check correct context @@ -23,15 +23,7 @@ internal object JSONParserSpec : Spek({ } JSONParser.builder() - .decodingWith(JSONCodecProvider.default, appendBasic = false) - .build() - .apply { - // TODO check correct context - parseValue(StringReader("true")).should.equal(true) - } - - JSONParser.builder() - .decodingWith(listOf(JSONCodecProvider.default), appendBasic = false) + .decodingWith(listOf(BooleanJSONCodec)) .build() .apply { // TODO check correct context @@ -41,7 +33,7 @@ internal object JSONParserSpec : Spek({ val testContext = TestCoderContext() JSONParser.builder(testContext) - .decodingWith(JSONCodecProvider.default) + .decodingWith() .build() .apply { // TODO check correct context diff --git a/Tests/Sources/JSONSerializerSpec.kt b/Tests/Sources/JSONSerializerSpec.kt index c4d6852..b7adaa6 100644 --- a/Tests/Sources/JSONSerializerSpec.kt +++ b/Tests/Sources/JSONSerializerSpec.kt @@ -12,14 +12,6 @@ internal object JSONSerializerSpec : Spek({ describe("JSONSerializer") { it(".builder()") { - JSONSerializer.builder() - .encodingWith(JSONCodecProvider.default) - .build() - .apply { - // TODO check correct context - serializeValue(true).should.equal("true") - } - JSONSerializer.builder() .encodingWith(BooleanJSONCodec) .build() @@ -39,7 +31,7 @@ internal object JSONSerializerSpec : Spek({ val testContext = TestCoderContext() JSONSerializer.builder(testContext) - .encodingWith(JSONCodecProvider.default) + .encodingWith() .build() .apply { // TODO check correct context diff --git a/Tests/Sources/StandardCodecsSpec.kt b/Tests/Sources/StandardCodecsSpec.kt index c7f4b56..6c46ce7 100644 --- a/Tests/Sources/StandardCodecsSpec.kt +++ b/Tests/Sources/StandardCodecsSpec.kt @@ -11,6 +11,7 @@ import org.jetbrains.spek.api.dsl.it internal object StandardCodecsSpec : Spek({ listOf( + // basic Test("Any", AnyJSONTestDecoderCodec, anyData), Test("Array", ArrayJSONTestCodec, arrayData), Test("Array (non-recursive)", ArrayJSONTestCodec.NonRecursive, arrayData), @@ -38,7 +39,25 @@ internal object StandardCodecsSpec : Spek({ Test("Sequence (non-recursive)", SequenceJSONTestCodec.NonRecursive, sequenceData), Test("Short", ShortJSONCodec, shortData), Test("ShortArray", ShortArrayJSONCodec, shortArrayData), - Test("String", StringJSONCodec, stringData) + Test("String", StringJSONCodec, stringData), + + // extended + Test("DayOfWeek", DayOfWeekJSONCodec, dayOfWeekData), + Test("Duration", DurationJSONCodec, durationData), + Test("Instant", InstantJSONCodec, instantData), + Test("LocalDate", LocalDateJSONCodec, localDateData), + Test("LocalDateTime", LocalDateTimeJSONCodec, localDateTimeData), + Test("LocalTime", LocalTimeJSONCodec, localTimeData), + Test("Month", MonthJSONCodec, monthData), + Test("MonthDay", MonthDayJSONCodec, monthDayData), + Test("OffsetDateTime", OffsetDateTimeJSONCodec, offsetDateTimeData), + Test("OffsetTime", OffsetTimeJSONCodec, offsetTimeData), + Test("Period", PeriodJSONCodec, periodData), + Test("Year", YearJSONCodec, yearData), + Test("YearMonth", YearMonthJSONCodec, yearMonthData), + Test("ZonedDateTime", ZonedDateTimeJSONCodec, zonedDateTimeData), + Test("ZoneId", ZoneIdJSONCodec, zoneIdData), + Test("ZoneOffset", ZoneOffsetJSONCodec, zoneOffsetData) ) .forEach { test -> describe(test.name) { @@ -75,7 +94,7 @@ internal object StandardCodecsSpec : Spek({ it("Map maintains entry order") { MapJSONTestCodec.parse("""{ "3": 3, "1": 1, "2": 2, "0": 0 }""", jsonCodableType()) - ?.entries?.toList() + .entries.toList() .should.equal(mapOf("3" to 3, "1" to 1, "2" to 2, "0" to 0).entries.toList()) } }) { diff --git a/Tests/Sources/StandardParserRejectSpec.kt b/Tests/Sources/StandardParserRejectSpec.kt index 6cb538e..b39085f 100644 --- a/Tests/Sources/StandardParserRejectSpec.kt +++ b/Tests/Sources/StandardParserRejectSpec.kt @@ -11,7 +11,7 @@ internal object StandardParserRejectSpec : SubjectSpek({ subject { StandardParser(JSONCoderContext.empty) { source, context -> JSONDecoder.builder(context) - .codecs(JSONCodecProvider.default) + .codecs() .source(source) .build() } diff --git a/Tests/Sources/StandardSerializerRejectSpec.kt b/Tests/Sources/StandardSerializerRejectSpec.kt index 06a80a1..c5f5dcc 100644 --- a/Tests/Sources/StandardSerializerRejectSpec.kt +++ b/Tests/Sources/StandardSerializerRejectSpec.kt @@ -11,7 +11,7 @@ internal object StandardSerializerRejectSpec : SubjectSpek({ subject { StandardSerializer(JSONCoderContext.empty) { destination, context -> JSONEncoder.builder(context) - .codecs(JSONCodecProvider.default) + .codecs() .destination(destination) .build() } diff --git a/Tests/Sources/Utility/Coding.kt b/Tests/Sources/Utility/Coding.kt index 016e5f7..4843956 100644 --- a/Tests/Sources/Utility/Coding.kt +++ b/Tests/Sources/Utility/Coding.kt @@ -3,15 +3,15 @@ package tests import com.github.fluidsonic.fluid.json.* -internal fun JSONDecoderCodec.parse(source: String, type: JSONCodableType): Value? = +internal fun JSONDecoderCodec.parse(source: String, type: JSONCodableType) = JSONParser.builder() - .decodingWith(this, appendBasic = false) + .decodingWith(this, base = null) .build() .parseValueOfType(source, type) internal fun JSONEncoderCodec.serialize(value: Value) = JSONSerializer.builder() - .encodingWith(this, appendBasic = false) + .encodingWith(this, base = null) .build() .serializeValue(value)