• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017-2022 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.json.Json
9 import kotlin.test.*
10 
11 class CollectionSerializerTest {
12 
13     @Serializable
14     data class CollectionWrapper(
15         val collection: Collection<String>
16     )
17 
18     @Test
testListJsonnull19     fun testListJson() {
20         val list = listOf("foo", "bar", "foo", "bar")
21 
22         val string = Json.encodeToString(CollectionWrapper(list))
23         assertEquals("""{"collection":["foo","bar","foo","bar"]}""", string)
24 
25         val wrapper = Json.decodeFromString<CollectionWrapper>(string)
26         assertEquals(list, wrapper.collection)
27     }
28 
29     @Test
testSetJsonnull30     fun testSetJson() {
31         val set = setOf("foo", "bar", "foo", "bar")
32 
33         val string = Json.encodeToString(CollectionWrapper(set))
34         assertEquals("""{"collection":["foo","bar"]}""", string)
35 
36         val wrapper = Json.decodeFromString<CollectionWrapper>(string)
37         assertEquals(set.toList(), wrapper.collection)
38     }
39 }
40