1 /* 2 * Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.serialization.features 6 7 import kotlinx.serialization.* 8 import kotlinx.serialization.builtins.* 9 import kotlinx.serialization.json.Json 10 import kotlinx.serialization.test.* 11 import kotlin.test.* 12 13 class ByteArraySerializerTest { 14 15 @Serializable 16 class ByteArrayCarrier(@Id(2) val data: ByteArray) { equalsnull17 override fun equals(other: Any?): Boolean { 18 if (this === other) return true 19 if (other == null || this::class != other::class) return false 20 21 other as ByteArrayCarrier 22 23 if (!data.contentEquals(other.data)) return false 24 25 return true 26 } 27 hashCodenull28 override fun hashCode(): Int { 29 return data.contentHashCode() 30 } 31 toStringnull32 override fun toString(): String { 33 return "ByteArrayCarrier(data=${data.contentToString()})" 34 } 35 } 36 37 @Test testByteArrayJsonnull38 fun testByteArrayJson() { 39 val bytes = byteArrayOf(42, 43, 44, 45) 40 val s = Json.encodeToString(ByteArraySerializer(), bytes) 41 assertEquals(s, """[42,43,44,45]""") 42 val bytes2 = Json.decodeFromString(ByteArraySerializer(), s) 43 assertTrue(bytes.contentEquals(bytes2)) 44 } 45 46 @Test testWrappedByteArrayJsonnull47 fun testWrappedByteArrayJson() { 48 val obj = ByteArrayCarrier(byteArrayOf(42, 100)) 49 val s = Json.encodeToString(ByteArrayCarrier.serializer(), obj) 50 assertEquals("""{"data":[42,100]}""", s) 51 val obj2 = Json.decodeFromString(ByteArrayCarrier.serializer(), s) 52 assertEquals(obj, obj2) 53 } 54 } 55