-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cbor features for COSE compliance (#2412)
This PR contains all features required to serialize and parse COSE-compliant CBOR (thanks to @nodh). While some canonicalization steps (such as sorting keys) still need to be performed manually. It does get the job done quite well. Namely, we have successfully used the features introduced here to create and validate ISO/IEC 18013-5:2021-compliant mobile driving license data. This PR introduces the following features to the CBOR format: - Serial Labels - Tagging of keys and values - Definite length encoding (this is the largest change, as it effectively makes the cbor encoder two-pass) - Option to globally prefer major type 2 for byte array encoding - Various QoL changes, such as public CborEncoder/CborDecoder interfaces and separate CborConfiguration class. This PR obsoletes #2371 and #2359 as it contains the features of both PRs and many more. Fixes #1955 Fixes #1560 Co-authored-by: Christian Kollmann <[email protected]> Co-authored-by: Leonid Startsev <[email protected]>
- Loading branch information
1 parent
af5095e
commit 2017084
Showing
31 changed files
with
4,230 additions
and
1,472 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
benchmark/src/jmh/kotlin/kotlinx/benchmarks/cbor/CborBaseLine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.benchmarks.cbor | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.cbor.* | ||
import org.openjdk.jmh.annotations.* | ||
import java.util.concurrent.* | ||
|
||
@Serializable | ||
data class KTestAllTypes( | ||
val i32: Int, | ||
val i64: Long, | ||
val f: Float, | ||
val d: Double, | ||
val s: String, | ||
val b: Boolean = false, | ||
) | ||
|
||
@Serializable | ||
data class KTestOuterMessage( | ||
val a: Int, | ||
val b: Double, | ||
val inner: KTestAllTypes, | ||
val s: String, | ||
val ss: List<String> | ||
) | ||
|
||
@Warmup(iterations = 5, time = 1) | ||
@Measurement(iterations = 10, time = 1) | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Benchmark) | ||
@Fork(1) | ||
open class CborBaseline { | ||
val baseMessage = KTestOuterMessage( | ||
42, | ||
256123123412.0, | ||
s = "string", | ||
ss = listOf("a", "b", "c"), | ||
inner = KTestAllTypes(-123124512, 36253671257312, Float.MIN_VALUE, -23e15, "foobarbaz") | ||
) | ||
|
||
val cbor = Cbor { | ||
encodeDefaults = true | ||
encodeKeyTags = false | ||
encodeValueTags = false | ||
useDefiniteLengthEncoding = false | ||
preferCborLabelsOverNames = false | ||
} | ||
|
||
val baseBytes = cbor.encodeToByteArray(KTestOuterMessage.serializer(), baseMessage) | ||
|
||
@Benchmark | ||
fun toBytes() = cbor.encodeToByteArray(KTestOuterMessage.serializer(), baseMessage) | ||
|
||
@Benchmark | ||
fun fromBytes() = cbor.decodeFromByteArray(KTestOuterMessage.serializer(), baseBytes) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.