63 Commits
Author SHA1 Message Date
Hamilton Chapman aed2b2ad7a Fix some warnings about encoding and decoding Data and Date types 2024-10-28 17:51:13 +00:00
Maciej Krysztofiak 04735e8757 Fix building for Windows 2024-10-28 17:41:31 +00:00
Frank Lehmann 96f6db8e6e Use encodeVarUint method again 2024-10-28 17:41:03 +00:00
zunda bd4a7807e2 add Sendable 2024-10-28 17:38:34 +00:00
zunda c97ffa5ec1 remove Equatable, Hashable has Equable 2024-10-28 17:38:34 +00:00
Dimitri Bouniol 2b771ddd16 Added the ability to specify a maximum depth for CBOR decoding
Closes #92
2024-03-26 16:12:07 +00:00
Hamilton Chapman d84829daa5 Add a test for the nested optional array of a Codable decoding incorrectly 2022-12-04 09:51:20 -08:00
Ahmed Elmoughazy d5f9b644bb * solving crashing problem when decoding optional UnkeyedContainer.
context:
- calling decodeIfPresent on optional arrays, cause the application to crash because of Force unwrap.
2022-12-04 09:51:20 -08:00
Hamilton Chapman 0baf789465 Don't treat [UInt8] as a byte string.
If you want to have something treated as a byte string then you should use
`Data`.

Fixes: #58
2022-08-01 14:37:57 +01:00
Hamilton Chapman d2de0ba4e6 Update the CBOREncodable protocol to require that a toCBOR function be
implemented. The `encode` function can then rely on a default implementation
that will call `toCBOR()` and then encode the resulting `CBOR` object.
2022-06-28 12:11:01 +01:00
Hamilton Chapman 5aa8f6c40b Fix Codable-based decoding of indefinite maps and arrays.
We weren't moving the index along for the extra break byte (0xff), but now we
are.

Fixes: #78
2022-03-11 13:57:44 +00:00
Hamilton Chapman a79e0ca348 Fix types being used in typeMismatch errors 2022-03-11 13:57:44 +00:00
Hamilton Chapman 1fb6c0d326 Fix issue when trying to decode a mismatched type as a dictionary or an array
would lead to an empty container being decoded instead of an error being thrown.

We now correctly throw a `DecodingError.typeMismatch` error in cases like this.

Fixes: #59
2022-03-11 13:57:44 +00:00
Hamilton Chapman 36f89e5dad Fix an issue where decoding would fail if there was a property in a
`Decodable`-conforming type that was an optional array (`Optional<Array<T>>`)
and the value was an empty array.
2022-03-11 13:57:44 +00:00
Hamilton Chapman 80a90c3a60 Restructure and update repository to work with modern Swift Package Manager
conventions.
2021-12-15 18:10:44 +00:00
Hamilton Chapman dca8f0dcbb Add an option to forbid non-String map keys.
The current encoding APIs are generally non-throwing and so in a lot of places
we'd end up with a change to a throwing API if the various `encodeMap`
implementations were turned into throwing functions (some of them already are
though).

For now I've decided to leave this as are and use a `try!` in the cases where
the current `encodeMap` implementation is non-throwing. This will therefore lead
to runtime errors but I think this is fine for now given that the option to
forbid non-String map keys is opt-in.

It might be useful in the future to either change most/all of the
`encode`-relevant functions to be throwing. Either that or perhaps there could
be throwing versions to live alongside the non-throwing versions that currently
exist.
2021-12-15 17:44:19 +00:00
Hamilton Chapman c954801a62 Fix Date encoding and decoding.
Also add support for another encoding and decoding option: `dateStrategy`. This
option accepts a value of type `DateStrategy`. The default value is
`taggedAsEpochTimestamp`, which is what the existing behaviour was. This means
`Date`s are encoded as tagged values, where the tag is `1` and the value is an
epoch timestamp.

The other value possible as a `dateStrategy` is `.annotatedMap`. This will
encode a `Date` as a map that looks like this:

```json
{
  "__type": "date_epoch_timestamp",
  "__value": 123456789
}
```

This is potentially useful for interacting with CBOR implementations that don't
support tagged values.
2021-12-09 18:05:14 +00:00
Hamilton Chapman 316dfe2fbb Introduce CBOROptions which currently is only used to allow a user to specify
if they want map keys to be encoded as strings, where possible. This is relevant
for types such as those found in `Foundation` that conform to `Codable` and
whose coding keys can be encoded as either an `Int` or a `String`.
2021-12-01 15:57:06 +00:00
Hamilton Chapman e0977d1b19 Fixed issues with the encoding and decoding of more types that conform to
`Codable`.

Also now allows options to be specified that will control the encoding of the
keys in maps where they could be encoded as either an `Int` or ` String`. A new
option called `useStringKeys` is exposed which allows the user to request map
keys be encoded as `String`s
2021-12-01 15:50:14 +00:00
Hamilton Chapman 301b3fb8a0 Fix the decoding of Codable enums 2021-11-24 15:08:24 +00:00
Hamilton Chapman c233a2845a Add support for encoding Codable objects to CBOREncoder's encodeAny 2021-10-20 12:43:28 +01:00
Hamilton Chapman 603831258d Ensure that when decoding using a CodableCBORDecoder a CBOR.half(float32Val)
is successfully decoded to a `Double` or a `Float`.
2021-09-22 13:59:38 +01:00
Hamilton Chapman d34a479e7c Remove constraint that to conform to CBOREncodable a type must also conform to
`Hashable`.

This allows us to check at runtime if an `Any` conforms to `CBOREncodable` and
therefore call its `encode()` implementation if it does.

Extra types have had `CBOREncodable` conformance added for them here as well.

Also update `Package.xcconfig` (and `SwiftCBOR.xcodeproj` as a result).
2021-09-22 13:58:47 +01:00
Hamilton Chapman d8a7d6c84e Account for nils in encodeAny implementation when working with arrays/dicts 2021-09-22 13:18:12 +01:00
Hamilton Chapman d4b23da370 Make class-constrained protocols AnyObject-constrained instead 2021-09-22 13:18:07 +01:00
Hamilton Chapman 4dcf8c2cbb Merge pull request #67 from martinreichart/feature/performance-array-slice
Massive performance improvements by switching from using Data to ArraySlice
2021-09-22 09:48:26 +01:00
Martin Fitzka-Reichart bce7eb69d4 Replace remaining usages of mapped Data objects and switch to ArraySlice 2021-09-22 00:15:50 +02:00
Martin Fitzka-Reichart 4a2d49da96 Massive performance improvements by switching from using Data to ArraySlice
Parsing byte per byte leads to massively performance hit because Data objects are constantly recreated

# Conflicts:
#	Sources/SwiftCBOR/Decoder/UnkeyedDecodingContainer.swift
2021-09-22 00:15:40 +02:00
Martin Fitzka-Reichart 9597a43b6b Achieve massive performance improvement by switching to ArraySlice instead of constantly creating new arrays 2021-09-02 12:50:41 +02:00
Hamilton Chapman 94f7511d6c Fix warnings about dangling pointers. 2020-04-08 12:35:15 +01:00
Hamilton Chapman 87b967bb90 Change explicit warnings that just say FIXME to be comments saying FIXME 2020-03-09 19:28:42 +00:00
Hamilton Chapman 34b84c4107 Remove unnecessary whitespace 2020-03-05 17:06:02 +00:00
Hamilton Chapman 8a37f0ca90 Merge pull request #51 from myfreeweb/hc/lower-encode-any-precedence-of-bool
Lower Bool encoding precedence when working with `Any`
2019-10-10 15:50:54 +01:00
Hamilton Chapman b768a73989 When encoding an Any the check for Bool was highest precedence which meant that if you were working with Objective-C and you had an integer value of 0 or 1 that you wanted to encode as a numeric value then it would be encoded as a Bool. This lowers the precedence of checking for a Bool to be lower than the numeric checks. 2019-10-10 15:50:14 +01:00
Hamilton Chapman cc6c03090c Merge pull request #49 from thomaspockrandt/master
Ensure compatibility with Xcode 11
2019-08-21 16:06:51 +01:00
Hamilton Chapman 691fc61d25 Make CBOR.encodeAny public 2019-08-21 15:50:25 +01:00
Thomas Pockrandt c75d21a838 Make switch exhaustive 2019-06-15 00:41:47 +02:00
Hamilton Chapman 565c99f074 Allow nil as value in maps 2019-06-04 12:04:31 +01:00
Hamilton Chapman 8994fa1682 Fix encoding of Data and update Codable roundtrip test to include testing for it 2019-04-26 16:14:16 +01:00
Hamilton Chapman 87c86c06e9 Formatting fix 2019-04-26 12:36:23 +01:00
Hamilton Chapman 4dd8003984 Fix for encoding of Data when using CodableCBOREncoder. Add accompanying test 2019-04-26 12:23:16 +01:00
Hamilton Chapman 3bce68e8a7 Remove some hacks for Date and Data by moving type checks to earlier in decoding process 2019-04-26 12:11:02 +01:00
Hamilton Chapman 863518675b Expanded on comment about indefinite byte string 2019-04-26 10:12:06 +01:00
Hamilton Chapman a3b3af2269 Add FIXME comment about indefinite byte string solution for getting Data through Codable 2019-04-25 18:58:19 +01:00
Hamilton Chapman 0b52024c9b Fix some outstanding FIXMEs about indefinite arrays, maps, and byte strings 2019-04-25 18:39:54 +01:00
Max Alexander 7fce1ac337 Ensured that position setter mutations were mutating the enum 2019-04-25 10:39:34 +01:00
Max Alexander e70a4c09fd adding tests 2019-04-25 10:34:24 +01:00
Max Alexander 49344b92e8 CBOR conforms to Hashable 2019-04-25 10:34:24 +01:00
Hamilton Chapman 7fc1147b33 Fix for longer strings and longer byte strings 2019-04-11 13:24:11 +01:00
Hamilton Chapman 27ca374306 Remove assertion that no container already exists for Decoder 2019-04-10 17:38:01 +01:00