8 changed files with 237 additions and 16 deletions
@ -0,0 +1,42 @@
@@ -0,0 +1,42 @@
|
||||
/* |
||||
* Copyright 2002-2023 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.http |
||||
|
||||
import kotlinx.serialization.KSerializer |
||||
import kotlinx.serialization.descriptors.PrimitiveKind |
||||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor |
||||
import kotlinx.serialization.encoding.Decoder |
||||
import kotlinx.serialization.encoding.Encoder |
||||
import kotlinx.serialization.json.Json |
||||
import kotlinx.serialization.modules.SerializersModule |
||||
import java.math.BigDecimal |
||||
|
||||
object BigDecimalSerializer : KSerializer<BigDecimal> { |
||||
override val descriptor = PrimitiveSerialDescriptor("BigDecimal", PrimitiveKind.DOUBLE) |
||||
|
||||
override fun deserialize(decoder: Decoder): BigDecimal = BigDecimal.valueOf(decoder.decodeDouble()) |
||||
|
||||
override fun serialize(encoder: Encoder, value: BigDecimal) { |
||||
encoder.encodeDouble(value.toDouble()) |
||||
} |
||||
} |
||||
|
||||
val customJson = Json { |
||||
serializersModule = SerializersModule { |
||||
contextual(BigDecimal::class, BigDecimalSerializer) |
||||
} |
||||
} |
||||
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
/* |
||||
* Copyright 2002-2023 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.http.codec.json |
||||
|
||||
import org.assertj.core.api.Assertions |
||||
import org.junit.jupiter.api.Test |
||||
import org.springframework.core.ResolvableType |
||||
import org.springframework.core.io.buffer.DataBuffer |
||||
import org.springframework.core.testfixture.codec.AbstractDecoderTests |
||||
import org.springframework.http.MediaType |
||||
import org.springframework.http.customJson |
||||
import reactor.core.publisher.Mono |
||||
import reactor.test.StepVerifier |
||||
import java.math.BigDecimal |
||||
import java.nio.charset.Charset |
||||
import java.nio.charset.StandardCharsets |
||||
|
||||
/** |
||||
* Tests for the JSON decoding using kotlinx.serialization with a custom serializer module. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
*/ |
||||
class CustomKotlinSerializationJsonDecoderTests : |
||||
AbstractDecoderTests<KotlinSerializationJsonDecoder>(KotlinSerializationJsonDecoder(customJson)) { |
||||
|
||||
@Test |
||||
override fun canDecode() { |
||||
val bigDecimalType = ResolvableType.forClass(BigDecimal::class.java) |
||||
Assertions.assertThat(decoder.canDecode(bigDecimalType, MediaType.APPLICATION_JSON)).isTrue() |
||||
} |
||||
|
||||
@Test |
||||
override fun decode() { |
||||
val output = decoder.decode(Mono.empty(), |
||||
ResolvableType.forClass(KotlinSerializationJsonDecoderTests.Pojo::class.java), null, emptyMap()) |
||||
StepVerifier |
||||
.create(output) |
||||
.expectError(UnsupportedOperationException::class.java) |
||||
.verify() |
||||
} |
||||
|
||||
@Test |
||||
override fun decodeToMono() { |
||||
val input = stringBuffer("1.0") |
||||
val output = decoder.decodeToMono(input, |
||||
ResolvableType.forClass(BigDecimal::class.java), null, emptyMap()) |
||||
StepVerifier |
||||
.create(output) |
||||
.expectNext(BigDecimal.valueOf(1.0)) |
||||
.expectComplete() |
||||
.verify() |
||||
} |
||||
|
||||
private fun stringBuffer(value: String): Mono<DataBuffer> { |
||||
return stringBuffer(value, StandardCharsets.UTF_8) |
||||
} |
||||
|
||||
private fun stringBuffer(value: String, charset: Charset): Mono<DataBuffer> { |
||||
return Mono.defer { |
||||
val bytes = value.toByteArray(charset) |
||||
val buffer = bufferFactory.allocateBuffer(bytes.size) |
||||
buffer.write(bytes) |
||||
Mono.just(buffer) |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,55 @@
@@ -0,0 +1,55 @@
|
||||
/* |
||||
* Copyright 2002-2023 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.http.codec.json |
||||
|
||||
import org.assertj.core.api.Assertions |
||||
import org.junit.jupiter.api.Test |
||||
import org.springframework.core.ResolvableType |
||||
import org.springframework.core.io.buffer.DataBuffer |
||||
import org.springframework.core.io.buffer.DataBufferUtils |
||||
import org.springframework.core.testfixture.codec.AbstractEncoderTests |
||||
import org.springframework.http.MediaType |
||||
import org.springframework.http.customJson |
||||
import reactor.core.publisher.Mono |
||||
import reactor.test.StepVerifier |
||||
import java.math.BigDecimal |
||||
|
||||
/** |
||||
* Tests for the JSON encoding using kotlinx.serialization with a custom serializer module. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
*/ |
||||
class CustomKotlinSerializationJsonEncoderTests : |
||||
AbstractEncoderTests<KotlinSerializationJsonEncoder>(KotlinSerializationJsonEncoder(customJson)) { |
||||
|
||||
@Test |
||||
override fun canEncode() { |
||||
val bigDecimalType = ResolvableType.forClass(BigDecimal::class.java) |
||||
Assertions.assertThat(encoder.canEncode(bigDecimalType, MediaType.APPLICATION_JSON)).isTrue() |
||||
} |
||||
|
||||
@Test |
||||
override fun encode() { |
||||
val input = Mono.just(BigDecimal(1)) |
||||
testEncode(input, BigDecimal::class.java) { step: StepVerifier.FirstStep<DataBuffer?> -> |
||||
step.consumeNextWith(expectString("1.0") |
||||
.andThen { dataBuffer: DataBuffer? -> DataBufferUtils.release(dataBuffer) }) |
||||
.verifyComplete() |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue