1 /*
2  * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.serialization.builtins
6 
7 import kotlinx.serialization.*
8 import kotlinx.serialization.descriptors.*
9 import kotlinx.serialization.encoding.*
10 import kotlinx.serialization.descriptors.*
11 
12 
13 /**
14  * Serializer that encodes and decodes [Long] as its string representation.
15  *
16  * Intended to be used for interoperability with external clients (mainly JavaScript ones),
17  * where numbers can't be parsed correctly if they exceed
18  * [`abs(2^53-1)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER).
19  */
20 public object LongAsStringSerializer : KSerializer<Long> {
21     override val descriptor: SerialDescriptor =
22         PrimitiveSerialDescriptor("kotlinx.serialization.LongAsStringSerializer", PrimitiveKind.STRING)
23 
serializenull24     override fun serialize(encoder: Encoder, value: Long) {
25         encoder.encodeString(value.toString())
26     }
27 
deserializenull28     override fun deserialize(decoder: Decoder): Long {
29         return decoder.decodeString().toLong()
30     }
31 }
32