data_type Enum
#include <tkcrypt/common.hpp>
Namespace: tkc
Overview
The data_type
enum provides metadata for serialized data, indicating the type of content stored within serialized_data
objects. This enables type-safe deserialization and proper handling of different data formats.
Since: v3.0
Primitive Types
Value | Numeric | Description |
---|---|---|
|
0 |
Binary byte array (binary string) |
|
1 |
UTF8 string |
|
2 |
32-bit integer |
|
3 |
64-bit integer |
|
4 |
32-bit unsigned integer |
|
5 |
64-bit unsigned integer |
|
6 |
Boolean value |
|
12 |
Single-precision floating point number |
|
13 |
Double-precision floating point number |
Keychain Object Types
Value | Numeric | Description |
---|---|---|
|
7 |
Decentralized identifier |
|
8 |
Digitally signed verifiable data |
|
9 |
W3C verifiable credential |
|
10 |
Consensus transaction |
|
11 |
Hybrid encrypted data |
|
30 |
Encrypted symmetric key |
|
31 |
Digital signature with metadata |
|
34 |
Public key data |
|
36 |
Generic serializable object |
Array Types
Value | Numeric | Description |
---|---|---|
|
15 |
Array of byte arrays |
|
16 |
Array of UTF8 strings |
|
17 |
Array of 32-bit integers |
|
18 |
Array of 64-bit integers |
|
19 |
Array of 32-bit unsigned integers |
|
20 |
Array of 64-bit unsigned integers |
|
21 |
Array of Boolean values |
|
27 |
Array of single-precision floats |
|
28 |
Array of double-precision floats |
|
22 |
Array of DIDs |
|
23 |
Array of verifiable data objects |
|
24 |
Array of credential objects |
|
25 |
Array of transaction objects |
|
26 |
Array of encrypted data objects |
|
32 |
Array of key lock objects |
|
33 |
Array of attestation objects |
|
37 |
Array of public key objects |
|
35 |
Array of serializable objects |
Deprecated Types
Value | Numeric | Description |
---|---|---|
|
14 |
URI (deprecated in v3.0) |
|
29 |
Array of URI objects (deprecated in v3.0) |
Usage
#include <keychain/keychain_headers.hpp>
// Creating typed serialized data
kc::serialized_data text_data("Hello, World!");
assert(text_data.data_type() == kc::data_type::utf8_string);
kc::serialized_data number_data(42);
assert(number_data.data_type() == kc::data_type::int32);
kc::serialized_data binary_data(some_bytes);
assert(binary_data.data_type() == kc::data_type::bytes);
// Type checking before deserialization
void process_data(const kc::serialized_data& data) {
switch (data.data_type()) {
case kc::data_type::utf8_string:
std::string text = data.utf8_string();
break;
case kc::data_type::int32:
int32_t number = data.int32();
break;
case kc::data_type::verifiable_data:
// Correct: deserialize from serialized_data
kc::verifiable_data vd(data);
break;
default:
// Handle unsupported type
break;
}
}
// Working with arrays
std::vector<std::string> messages = {"msg1", "msg2", "msg3"};
kc::serialized_data array_data(messages);
assert(array_data.data_type() == kc::data_type::array_utf8_string);
std::vector<std::string> recovered = array_data.array_utf8_string();
Type Safety
The data_type
enum enables compile-time and runtime type safety:
// Compile-time type checking through method overloads
kc::serialized_data create_data(const std::string& value) {
return kc::serialized_data(value); // Automatically sets utf8_string type
}
kc::serialized_data create_data(int32_t value) {
return kc::serialized_data(value); // Automatically sets int32 type
}
// Runtime type validation
bool is_string_data(const kc::serialized_data& data) {
return data.data_type() == kc::data_type::utf8_string;
}
bool is_array_type(kc::data_type type) {
return static_cast<int>(type) >= 15 && static_cast<int>(type) <= 37 &&
type != kc::data_type::key_lock && type != kc::data_type::attestation;
}
Serialization and Metadata
// Data type is preserved during serialization
kc::verifiable_data signed_doc = gateway.sign(persona, document);
kc::serialized_data serialized(signed_doc.serialize());
// Type information travels with the data
assert(serialized.data_type() == kc::data_type::verifiable_data);
// Can reconstruct original object type
// Correct: deserialize from serialized_data, not bytes()
kc::verifiable_data reconstructed(serialized);
See Also
-
serialized_data - Type-safe serialized data container
-
serialization_format - Data serialization formats
-
verifiable_data - Signed data objects
-
encrypted_data - Encrypted data objects
-
credential - Verifiable credentials
-
transaction - Consensus transactions