Commit Graph

108 Commits

Author SHA1 Message Date
Ben Luddy
d8e0aa63e6
Add optional encoding support for json.Marshaler.
If the user provides a JSON-to-CBOR transcode function, a value whose type implements json.Marshaler
and not cbor.Marshaler will be encoded by first calling its MarshalJSON method, then transcoding the
result to CBOR.

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2025-06-17 10:44:47 -04:00
Faye Amacker
a8dbe3e5c3
Merge pull request #672 from benluddy/text-un-marshaler
Add options to support TextMarshaler and TextUnmarshaler
2025-06-16 18:23:29 -05:00
Ben Luddy
7a9dbd0b0a
Add option to marshal encoding.TextMarshalers to CBOR text strings.
The default behavior (encoding.TextMarshaler not recognized) remains unchanged.

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2025-06-16 09:43:57 -04:00
adeinega
6d25d3f82f Fix typos here and there.
Signed-off-by: Andrii Deinega andrii.deinega@gmail.com
2025-06-10 13:31:56 -07:00
Faye Amacker
92f7b97a36 Add example of Embedded JSON Tag for CBOR
Some checks failed
CodeQL / Analyze (go) (push) Has been cancelled
govulncheck / Check (push) Has been cancelled
For more info, see
https://github.com/toravir/CBOR-Tag-Specs/blob/master/embeddedJSON.md
2025-04-27 17:38:27 -05:00
Faye Amacker
df7db22e3a Improve docs for CBOR time encoding options
Mention that:
- time encoding options provided by cbor.TimeMode comply with RFC 8949.
- user apps that prefer to encode time in other ways (e.g. not specified by RFC 8949) can use a CBOR tag number not assigned by IANA and implement cbor.Marshal and cbor.Unmarshal interfaces.

Also, update the comments for each time encoding option to be more useful to programmers who did not read RFC 8949.  The old comments were meant to be stop-gaps due to schedule.
2025-04-26 15:34:26 -05:00
Jordan Liggitt
167dc75c19
Add omitzero support
Signed-off-by: Jordan Liggitt <liggitt@google.com>
2025-03-26 09:55:53 -04:00
Faye Amacker
34c6f2b442 Replace reflect.PtrTo with reflect.PointerTo 2025-02-22 14:25:35 -06:00
Faye Amacker
50b362d72f Replace reflect.Ptr with reflect.Pointer
This commit replaces reflect.Ptr with reflect.Pointer,
so go1.18 or newer is required to to build.
2025-02-22 14:20:19 -06:00
Faye Amacker
9fa2077793 Replace interface{} with any
This commit replaces every instance of interface{} with any,
so go1.18 or newer is required to to build.
2025-02-22 14:05:53 -06:00
Ben Luddy
ffab76a44a
Fix panic using SortFastShuffle to encode a struct with no fields.
Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-06-10 11:02:51 -04:00
Faye Amacker
d1b239987b Rename ByteSliceMode to ByteSliceLaterFormatMode
Also renamed related options to be consistent.
2024-06-09 21:16:42 -05:00
Faye Amacker
ed38331613 Add UserBufferEncMode interface with MarshalToBuffer()
This commit adds features related to allowing user to specify
a buffer rather than using built-in buffer pool:

- cbor.MarshalToBuffer() uses codec's default options to encode
  to user provided buffer instead of using built-in buffer pool.

- UserBufferEncMode interface extends EncMode interface with
  MarshalToBuffer() so user can provide buffer for encoding
  instead of using built-in buffer pool.

- EncOptions.UserBufferEncMode() returns UserBufferEncMode

- EncOptions.UserBufferEncModeWithTags() returns UserBufferEncMode

- EncOptions.UserBufferEncModeWithSharedTags() returns UserBufferEncMode
2024-06-09 19:27:59 -05:00
Ben Luddy
0cabb4afed
Disable conflicting encode options when marshaling cbor.Tag.
Encode options, especially those that control the mapping from Go type to CBOR type, can result in
output containing tag validity errors. For tag numbers that are built in, it's possible to "do the
right thing" and override those options on a case-by-case basis. This can't and does not prevent tag
validity errors for unrecognized tag numbers.

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-06-03 11:36:14 -04:00
Faye Amacker
3f436a63b9 Refactor to remove more magic numbers 2024-05-28 14:12:25 -05:00
Faye Amacker
b426c3e8c6 Lint 2024-05-27 21:21:12 -05:00
Faye Amacker
fdf5bd8378 Refactor to remove more magic numbers 2024-05-27 21:14:04 -05:00
Faye Amacker
22175debe5 Extract CBOR vars & funcs to new file cbor.go 2024-05-27 18:52:21 -05:00
Faye Amacker
23ddf31ad6 Refactor to remove magic number 0xff (break flag) 2024-05-27 18:22:34 -05:00
Faye Amacker
b75278bc2e Refactor CBOR head funcs to remove magic numbers 2024-05-27 16:06:38 -05:00
Faye Amacker
03575b4950 Refactor code and lint 2024-05-27 15:33:34 -05:00
Ben Luddy
6396be350f
Refactor sorted map encode to use fewer buffers for nested maps.
Runs a bit faster, but more importantly, only needs a single buffer to encode nested, sorted maps
instead of using multiple temporary buffers.

                                                            │ before.txt  │             after.txt              │
                                                            │   sec/op    │   sec/op     vs base               │
MarshalCanonical/Go_map[string]string_to_CBOR_map_canonical   1.464µ ± 0%   1.395µ ± 0%  -4.68% (p=0.000 n=10)
MarshalCanonical/Go_map[int]int_to_CBOR_map_canonical         192.1n ± 0%   186.2n ± 1%  -3.10% (p=0.000 n=10)
geomean                                                       530.2n        509.6n       -3.89%

                                                            │ before.txt │               after.txt               │
                                                            │    B/op    │    B/op      vs base                  │
MarshalCanonical/Go_map[string]string_to_CBOR_map_canonical   88.00 ± 0%   112.00 ± 0%  +27.27% (p=0.000 n=10)
MarshalCanonical/Go_map[int]int_to_CBOR_map_canonical         3.000 ± 0%    3.000 ± 0%        ~ (p=1.000 n=10) ¹
geomean                                                       16.25         18.33       +12.82%
¹ all samples are equal

                                                            │ before.txt │              after.txt              │
                                                            │ allocs/op  │ allocs/op   vs base                 │
MarshalCanonical/Go_map[string]string_to_CBOR_map_canonical   2.000 ± 0%   2.000 ± 0%       ~ (p=1.000 n=10) ¹
MarshalCanonical/Go_map[int]int_to_CBOR_map_canonical         1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                                                       1.414        1.414       +0.00%
¹ all samples are equal

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-05-19 19:56:11 -04:00
Faye Amacker
8964976b42
Merge branch 'master' into bignum-options 2024-05-12 17:25:19 -05:00
Faye Amacker
fc5f0b6873
Merge branch 'master' into binary-marshaler-unmarshaler-options 2024-05-12 16:13:05 -05:00
Ben Luddy
fcbe98d114
Add options to disable BinaryMarshaler/BinaryUnmarshaler support.
By default, values whose type implements BinaryMarshaler encode to a byte string whose contents are
the result of calling MarshalBinary, and decoding a byte string into a BinaryUnmarshaler calls
UnmarshalBinary on the contents of the byte string. These options make it possible to disable both
behaviors.

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-05-10 15:26:53 -04:00
Ben Luddy
045f722ef3
Add MarshalToBuffer accepting user-provided buffer to *encMode.
The application calling Marshal has more information about the object it wants to marshal than this
library does (e.g. what type is it, what is the 95th-percentile serialized size of similar objects,
etc.). It can use this information to increase the utilization of encode buffers beyond what is
possible with a shared buffer pool for all calls to Marshal.

To avoid the backwards-incompatible change of expanding the method set of the EncMode interface,
invoking this method from an external package will require a type assertion, and there is currently
no exported "optional interface" declaration to make that more convenient.

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-05-10 14:44:00 -04:00
Ben Luddy
8077474e3b
Remove encoderBuffer type in favor of using bytes.Buffer directly.
With its only remaining field being an embedded bytes.Buffer, there's no reason to retain the
encoderBuffer type.

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-05-10 14:43:57 -04:00
Ben Luddy
039409578b
Use local arrays instead of the scratch field of encoderBuffer.
There doesn't seem to be any performance advantage to the [16]byte scratch space on each
encoderBuffer versus stack-allocated local array variables. If anything, there may be a spatial
locality advantage to using the stack. B/op and allocs/op are unchanged, sec/op is somewhere between
a wash and very marginally better:

                                                            │ scratch-withbuffer.txt │         scratch-stack.txt          │
                                                            │         sec/op         │   sec/op     vs base               │
Marshal/Go_bool_to_CBOR_bool                                             46.77n ± 1%   47.19n ± 0%  +0.90% (p=0.000 n=10)
Marshal/Go_uint64_to_CBOR_positive_int                                   66.03n ± 1%   66.15n ± 0%       ~ (p=0.393 n=10)
Marshal/Go_int64_to_CBOR_negative_int                                    52.34n ± 1%   52.44n ± 4%       ~ (p=0.306 n=10)
Marshal/Go_float64_to_CBOR_float                                         62.95n ± 1%   63.70n ± 1%  +1.19% (p=0.000 n=10)
Marshal/Go_[]uint8_to_CBOR_bytes                                         76.89n ± 1%   76.22n ± 0%  -0.86% (p=0.000 n=10)
Marshal/Go_string_to_CBOR_text                                           79.52n ± 1%   78.86n ± 1%  -0.82% (p=0.000 n=10)
Marshal/Go_[]int_to_CBOR_array                                           294.6n ± 0%   292.8n ± 0%  -0.64% (p=0.000 n=10)
Marshal/Go_map[string]string_to_CBOR_map                                 950.4n ± 1%   923.9n ± 1%  -2.79% (p=0.000 n=10)
Marshal/Go_map[string]interface{}_to_CBOR_map                            2.300µ ± 1%   2.310µ ± 0%       ~ (p=0.137 n=10)
Marshal/Go_struct_to_CBOR_map                                            1.422µ ± 1%   1.409µ ± 1%  -0.88% (p=0.006 n=10)
Marshal/Go_map[int]interface{}_to_CBOR_map                               2.207µ ± 0%   2.196µ ± 1%  -0.50% (p=0.004 n=10)
Marshal/Go_struct_keyasint_to_CBOR_map                                   1.394µ ± 0%   1.397µ ± 0%       ~ (p=0.050 n=10)
Marshal/Go_[]interface{}_to_CBOR_map                                     1.676µ ± 0%   1.632µ ± 1%  -2.63% (p=0.000 n=10)
Marshal/Go_struct_toarray_to_CBOR_array                                  1.370µ ± 1%   1.357µ ± 1%  -0.99% (p=0.000 n=10)
MarshalCanonical/Go_map[string]string_to_CBOR_map                        947.9n ± 1%   918.3n ± 1%  -3.12% (p=0.000 n=10)
MarshalCanonical/Go_map[string]string_to_CBOR_map_canonical              1.533µ ± 1%   1.503µ ± 2%  -1.96% (p=0.002 n=10)
MarshalCanonical/Go_struct_to_CBOR_map                                   307.9n ± 0%   309.0n ± 1%  +0.34% (p=0.014 n=10)
MarshalCanonical/Go_struct_to_CBOR_map_canonical                         309.9n ± 0%   309.4n ± 1%       ~ (p=1.000 n=10)
MarshalCanonical/Go_map[int]int_to_CBOR_map                              196.7n ± 0%   198.2n ± 0%  +0.76% (p=0.000 n=10)
MarshalCanonical/Go_map[int]int_to_CBOR_map_canonical                    197.6n ± 1%   198.6n ± 1%  +0.51% (p=0.005 n=10)
MarshalCOSE/128-Bit_Symmetric_Key                                        340.3n ± 0%   341.6n ± 0%  +0.38% (p=0.001 n=10)
MarshalCOSE/256-Bit_Symmetric_Key                                        344.0n ± 0%   344.1n ± 1%       ~ (p=0.540 n=10)
MarshalCOSE/ECDSA_P256_256-Bit_Key                                       433.2n ± 0%   438.1n ± 1%  +1.12% (p=0.000 n=10)
MarshalCWTClaims                                                         296.5n ± 0%   298.9n ± 1%  +0.83% (p=0.003 n=10)
MarshalSenML                                                             1.522µ ± 0%   1.524µ ± 1%       ~ (p=0.697 n=10)
MarshalSenMLShortestFloat16                                              1.534µ ± 1%   1.557µ ± 1%  +1.53% (p=0.001 n=10)
MarshalWebAuthn                                                          406.5n ± 0%   409.4n ± 1%  +0.71% (p=0.007 n=10)
MarshalCOSEMAC                                                           319.1n ± 0%   319.6n ± 1%       ~ (p=0.446 n=10)
MarshalCOSEMACWithTag                                                    399.8n ± 0%   397.9n ± 0%  -0.49% (p=0.007 n=10)
geomean                                                                  404.3n        403.5n       -0.22%

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-05-10 14:25:19 -04:00
Ben Luddy
7f27a44147
Add option to reject decoding bignum tags and encoding big.Int.
Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-05-10 14:22:46 -04:00
Faye Amacker
5a131e1c31
Merge pull request #531 from fxamacker/fxamacker/refactor-to-reuse-code
Refactor to reuse functions and improve code coverage
2024-05-05 18:49:52 -05:00
Faye Amacker
f83a7d00b4 Refactor to combine encodeHead() & encodedHeadLen()
This commit removes encodedHeadLen() and modifies encodeHead()
to return encoded head length to simplify code.
2024-05-05 18:46:04 -05:00
Faye Amacker
ca791949b1
Merge pull request #476 from benluddy/stdlib-json-byteslice-compatibility
Support automatic conversion between text and binary string representations
2024-05-05 17:29:15 -05:00
Faye Amacker
d3153aa3f0 Refactor to remove encodeFixedLengthStruct()
This commit removes encodeFixedLengthStruct() and reuses
encodeStruct() to simplify code.

Previously, encodeStruct() used extra buffer to encode elements
to get actual encoded element count.  To avoid this overhead,
encodeFixedLengthStruct() was created to encode fixed
length struct (struct without any "omitempty" fields) since
encoded element count is always known in this use case.

With PR #519 (https://github.com/fxamacker/cbor/pull/519),
encodeStruct() doesn't use extra buffer any more, and
encodeFixedLengthStruct() isn't necessary.
2024-05-04 20:27:01 -05:00
Ben Luddy
83e9c2bff8
Support auto conversion of byte strings to and from text encodings.
These options improve interoperability with programs that use JSON to encode and decode objects to
and from both struct types and empty interface values.

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-04-29 12:53:24 -04:00
Ben Luddy
d981dece35
Encode structs directly to output buffer.
For variable-length structs (structs with omitempty fields), encoding to the unused capacity at the
end of the output buffer while counting nonempty items is cheaper than using a separate temporary
buffer (no pool interactions and better spatial locality). Copying the items can be avoided entirely
by reserving space in the output buffer for the head if the encoded length of the head can be
predicted before checking optional fields.

                                                                     │ before.txt  │              after.txt              │
                                                                     │   sec/op    │   sec/op     vs base                │
Marshal/Go_struct_to_CBOR_map                                          1.404µ ± 0%   1.408µ ± 1%        ~ (p=0.170 n=10)
Marshal/Go_struct_many_fields_all_omitempty_all_empty_to_CBOR_map      443.8n ± 0%   430.6n ± 0%   -2.99% (p=0.000 n=10)
Marshal/Go_struct_some_fields_all_omitempty_all_empty_to_CBOR_map      181.7n ± 0%   163.5n ± 0%  -10.04% (p=0.000 n=10)
Marshal/Go_struct_many_fields_all_omitempty_all_nonempty_to_CBOR_map   813.5n ± 0%   784.8n ± 0%   -3.53% (p=0.000 n=10)
Marshal/Go_struct_some_fields_all_omitempty_all_nonempty_to_CBOR_map   300.8n ± 0%   275.4n ± 0%   -8.43% (p=0.000 n=10)
Marshal/Go_struct_many_fields_one_omitempty_to_CBOR_map                763.8n ± 0%   727.7n ± 0%   -4.73% (p=0.000 n=10)
Marshal/Go_struct_some_fields_one_omitempty_to_CBOR_map                284.2n ± 0%   257.6n ± 0%   -9.36% (p=0.000 n=10)
Marshal/Go_struct_keyasint_to_CBOR_map                                 1.422µ ± 0%   1.414µ ± 1%   -0.56% (p=0.029 n=10)
Marshal/Go_struct_toarray_to_CBOR_array                                1.341µ ± 1%   1.338µ ± 1%        ~ (p=0.340 n=10)
MarshalCanonical/Go_struct_to_CBOR_map                                 386.4n ± 0%   392.4n ± 0%   +1.57% (p=0.000 n=10)
MarshalCanonical/Go_struct_to_CBOR_map_canonical                       386.9n ± 0%   384.8n ± 0%   -0.52% (p=0.001 n=10)
geomean                                                                560.5n        540.4n        -3.59%

                                                                     │ before.txt │              after.txt              │
                                                                     │    B/op    │    B/op     vs base                 │
Marshal/Go_struct_to_CBOR_map                                          208.0 ± 0%   208.0 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_many_fields_all_omitempty_all_empty_to_CBOR_map      1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_some_fields_all_omitempty_all_empty_to_CBOR_map      1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_many_fields_all_omitempty_all_nonempty_to_CBOR_map   176.0 ± 0%   176.0 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_some_fields_all_omitempty_all_nonempty_to_CBOR_map   48.00 ± 0%   48.00 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_many_fields_one_omitempty_to_CBOR_map                160.0 ± 0%   160.0 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_some_fields_one_omitempty_to_CBOR_map                48.00 ± 0%   48.00 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_keyasint_to_CBOR_map                                 192.0 ± 0%   192.0 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_toarray_to_CBOR_array                                192.0 ± 0%   192.0 ± 0%       ~ (p=1.000 n=10) ¹
MarshalCanonical/Go_struct_to_CBOR_map                                 64.00 ± 0%   64.00 ± 0%       ~ (p=1.000 n=10) ¹
MarshalCanonical/Go_struct_to_CBOR_map_canonical                       64.00 ± 0%   64.00 ± 0%       ~ (p=1.000 n=10) ¹
geomean                                                                46.18        46.18       +0.00%
¹ all samples are equal

                                                                     │ before.txt │              after.txt              │
                                                                     │ allocs/op  │ allocs/op   vs base                 │
Marshal/Go_struct_to_CBOR_map                                          1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_many_fields_all_omitempty_all_empty_to_CBOR_map      1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_some_fields_all_omitempty_all_empty_to_CBOR_map      1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_many_fields_all_omitempty_all_nonempty_to_CBOR_map   1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_some_fields_all_omitempty_all_nonempty_to_CBOR_map   1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_many_fields_one_omitempty_to_CBOR_map                1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_some_fields_one_omitempty_to_CBOR_map                1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_keyasint_to_CBOR_map                                 1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
Marshal/Go_struct_toarray_to_CBOR_array                                1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
MarshalCanonical/Go_struct_to_CBOR_map                                 1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
MarshalCanonical/Go_struct_to_CBOR_map_canonical                       1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                                                                1.000        1.000       +0.00%
¹ all samples are equal

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-04-29 11:49:33 -04:00
Faye Amacker
8ef865cdae
Merge pull request #515 from benluddy/sortmode-determinism
Add SortMode to encode struct fields in a less predictable order.
2024-04-20 18:38:00 -05:00
Ben Luddy
cdc2c42a47
Allow rejection of NaN and Inf float values on encode and decode.
Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-04-14 21:45:02 -04:00
Ben Luddy
4bbff65cd6
Add shuffled SortMode for encoding struct fields less predictably.
The motivation is to prevent programs that consume the output from assuming (incorrectly) that it
was encoded deterministically, even when implementation details make the output apparently stable.

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-04-12 17:04:44 -04:00
Ben Luddy
30de9af692
Bypass sorting overhead for single-entry maps.
The map[int]int case is a single-entry map. This optimization is an easy win for a case that is not
extraordinarily rare.

MarshalCanonical/Go_map[string]string_to_CBOR_map             895.9n ± 2%   899.3n ± 1%        ~ (p=0.912 n=10)
MarshalCanonical/Go_map[string]string_to_CBOR_map_canonical   1.763µ ± 4%   1.763µ ± 2%        ~ (p=0.753 n=10)
MarshalCanonical/Go_struct_to_CBOR_map                        317.0n ± 2%   311.1n ± 0%   -1.88% (p=0.000 n=10)
MarshalCanonical/Go_struct_to_CBOR_map_canonical              315.5n ± 1%   314.6n ± 4%        ~ (p=0.239 n=10)
MarshalCanonical/Go_map[int]int_to_CBOR_map                   194.8n ± 2%   196.2n ± 1%        ~ (p=0.839 n=10)
MarshalCanonical/Go_map[int]int_to_CBOR_map_canonical         326.3n ± 3%   190.9n ± 3%  -41.47% (p=0.000 n=10)
geomean                                                       464.5n        424.0n        -8.71%

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-04-04 14:05:10 -04:00
Faye Amacker
a3a1d711f1 Use custom DecModes to check Marshaler data
This commit checks Marshaler data for well-formedness
(and very limited partial validation) by reusing one
of four DecModes created during startup.  These modes
are safe for parallel use by different Marshalers.
2024-02-19 18:21:28 -06:00
Faye Amacker
accf57c32e Check well-formedness of data from MarshalCBOR
MarshalerError is returned if CBOR data item returned from
MarshalCBOR() fails either:
- well-formedness check, or
- tag validation for builtin tags 0-3
2024-02-04 20:46:52 -06:00
Ben Luddy
7c1f8f3e33
Fix EncOption/DecOption unset fields on mode regurgitation.
Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-01-29 23:33:39 -05:00
Faye Amacker
e5eaf7abb9
Merge pull request #473 from fxamacker/fxamacker/refactor-map-encode-to-prepare-for-go-verion-bump
Refactor map encoding to prep for Go version bump
2024-01-22 18:37:51 -06:00
Ben Luddy
a29413c8fe
Add option to control the output CBOR type of struct field names.
Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-01-22 11:06:33 -05:00
Faye Amacker
49189749df Refactor map encoding to prep for go version bump
This refactor helps reduce duplicate code in
upcoming PRs.
2024-01-21 17:20:14 -06:00
Ben Luddy
30aefa6c0b
Add option to encode Go strings to CBOR byte strings.
The StringType encode option supports use cases that must not produce invalid CBOR (even if
well-formed) and must be able to encode Go strings that do not contain valid UTF-8 sequences,
without the overhead of sanitizing all input Go values. The default value of the option is
TextStringType and encodes Go strings to CBOR major type 3, which is identical to the preexisting
behavior.

Signed-off-by: Ben Luddy <bluddy@redhat.com>
2024-01-08 16:23:59 -05:00
Faye Amacker
c5c410b12c Add another test and fix lint errors 2024-01-07 13:31:52 -06:00
Faye Amacker
0cf56c3dab
Merge pull request #453 from dinhxuanvu/json-omitempty
Add encoding option to specify how omitempty fields are encoded
2024-01-07 13:09:56 -06:00
Vu Dinh
f95914be2b Address feedbacks
Signed-off-by: Vu Dinh <vudinh@outlook.com>
2024-01-04 12:46:44 -05:00
Faye Amacker
bfcaa814f5 Fix cbor.SimpleValue encoding and decoding
This commit resolves two issues:

1.  Encoding cbor.SimpleValue with values 24..31 should fail because
CBOR simple values 24..31 are reserved and they MUST NOT be encoded
according to RFC 8949.

This commit makes encoder return UnsupportedValueError when encoding
cbor.SimpleValue with values 24..31 because that would not be
a well-formed CBOR data item.

2. Decoding other CBOR types to cbor.SimpleValue should fail because
cbor.SimpleValue represents CBOR simple value (major type 7) which is
different from CBOR integers and shouldn't be used interchangeably.

This commit makes decoder return UnmarshalTypeError when decoding
other CBOR types to cbor.SimpleValue.
2024-01-01 18:59:19 -06:00