JSON BinPack  0.0.1
A space-efficient open-source binary JSON serialization format based on JSON Schema with both schema-driven and schema-less support.
runtime_plan.h
1 #ifndef SOURCEMETA_JSONBINPACK_RUNTIME_ENCODING_H_
2 #define SOURCEMETA_JSONBINPACK_RUNTIME_ENCODING_H_
3 
4 #include <sourcemeta/jsonbinpack/runtime_numeric.h>
5 #include <sourcemeta/jsontoolkit/json.h>
6 
7 #include <cstdint> // std::int64_t, std::uint64_t
8 #include <memory> // std::shared_ptr
9 #include <variant> // std::variant
10 #include <vector> // std::vector
11 
12 namespace sourcemeta::jsonbinpack {
13 
14 // We cannot directly create an Plan variant type whose values potentially
15 // include other Plan instances. As a workaround, we have a helper
16 // encoding wrapper that we can use as an incomplete type
17 struct __internal_encoding_wrapper;
18 // Use these alias types. Never use the internal wrapper type directly
19 using SinglePlan = std::shared_ptr<__internal_encoding_wrapper>;
20 using MultiplePlans = std::vector<__internal_encoding_wrapper>;
21 
25 
26 // clang-format off
58 // clang-format on
61  const std::int64_t minimum;
63  const std::int64_t maximum;
65  const std::uint64_t multiplier;
66 };
67 
68 // clang-format off
99 // clang-format on
102  const std::int64_t minimum;
104  const std::uint64_t multiplier;
105 };
106 
107 // clang-format off
137 // clang-format on
140  const std::int64_t maximum;
142  const std::uint64_t multiplier;
143 };
144 
145 // clang-format off
172 // clang-format on
175  const std::uint64_t multiplier;
176 };
177 
179 
183 
184 // clang-format off
222 // clang-format on
224 
226 
230 
231 // clang-format off
268 // clang-format on
271  const std::vector<sourcemeta::jsontoolkit::JSON> choices;
272 };
273 
274 // clang-format off
302 // clang-format on
305  const std::vector<sourcemeta::jsontoolkit::JSON> choices;
306 };
307 
308 // clang-format off
341 // clang-format on
344  const std::vector<sourcemeta::jsontoolkit::JSON> choices;
345 };
346 
347 // clang-format off
363 // clang-format on
364 struct CONST_NONE {
366  const sourcemeta::jsontoolkit::JSON value;
367 };
368 
370 
374 
375 // clang-format off
401 // clang-format on
404  const std::uint64_t size;
405 };
406 
407 // clang-format off
455 // clang-format on
458  const std::uint64_t minimum;
459 };
460 
461 // clang-format off
509 // clang-format on
512  const std::uint64_t maximum;
513 };
514 
515 // clang-format off
569 // clang-format on
572  const std::uint64_t minimum;
574  const std::uint64_t maximum;
575 };
576 
577 // clang-format off
612 // clang-format on
614 
615 // clang-format off
657 // clang-format on
659 
661 
665 
666 // clang-format off
700 // clang-format on
703  const std::uint64_t size;
705  const SinglePlan encoding;
707  const MultiplePlans prefix_encodings;
708 };
709 
710 // clang-format off
749 // clang-format on
752  const std::uint64_t minimum;
754  const std::uint64_t maximum;
756  const SinglePlan encoding;
758  const MultiplePlans prefix_encodings;
759 };
760 
761 // clang-format off
797 // clang-format on
800  const std::uint64_t minimum;
802  const SinglePlan encoding;
804  const MultiplePlans prefix_encodings;
805 };
806 
807 // clang-format off
842 // clang-format on
845  const std::uint64_t maximum;
847  const SinglePlan encoding;
849  const MultiplePlans prefix_encodings;
850 };
851 
853 
857 
858 // clang-format off
899 // clang-format on
902  const std::uint64_t size;
904  const SinglePlan key_encoding;
906  const SinglePlan encoding;
907 };
908 
909 // clang-format off
945 // clang-format on
948  const SinglePlan key_encoding;
950  const SinglePlan encoding;
951 };
952 
954 
958 
959 // TODO: Write brief description
961 #ifndef DOXYGEN
962 namespace internal::ANY_PACKED_TYPE_TAG_BYTE_PREFIX {
963 constexpr auto type_size = 3;
964 constexpr std::uint8_t TYPE_SHARED_STRING = 0b00000000;
965 constexpr std::uint8_t TYPE_STRING = 0b00000001;
966 constexpr std::uint8_t TYPE_LONG_STRING = 0b00000010;
967 constexpr std::uint8_t TYPE_OBJECT = 0b00000011;
968 constexpr std::uint8_t TYPE_ARRAY = 0b00000100;
969 constexpr std::uint8_t TYPE_POSITIVE_INTEGER_BYTE = 0b00000101;
970 constexpr std::uint8_t TYPE_NEGATIVE_INTEGER_BYTE = 0b00000110;
971 constexpr std::uint8_t TYPE_OTHER = 0b00000111;
972 static_assert(TYPE_SHARED_STRING <= uint_max<type_size>);
973 static_assert(TYPE_STRING <= uint_max<type_size>);
974 static_assert(TYPE_LONG_STRING <= uint_max<type_size>);
975 static_assert(TYPE_OBJECT <= uint_max<type_size>);
976 static_assert(TYPE_ARRAY <= uint_max<type_size>);
977 static_assert(TYPE_POSITIVE_INTEGER_BYTE <= uint_max<type_size>);
978 static_assert(TYPE_NEGATIVE_INTEGER_BYTE <= uint_max<type_size>);
979 static_assert(TYPE_OTHER <= uint_max<type_size>);
980 
981 constexpr auto subtype_size = 5;
982 constexpr std::uint8_t SUBTYPE_FALSE = 0b00000000;
983 constexpr std::uint8_t SUBTYPE_TRUE = 0b00000001;
984 constexpr std::uint8_t SUBTYPE_NULL = 0b00000010;
985 constexpr std::uint8_t SUBTYPE_POSITIVE_INTEGER = 0b00000011;
986 constexpr std::uint8_t SUBTYPE_NEGATIVE_INTEGER = 0b00000100;
987 constexpr std::uint8_t SUBTYPE_NUMBER = 0b00000101;
988 constexpr std::uint8_t SUBTYPE_LONG_STRING_BASE_EXPONENT_7 = 0b00000111;
989 constexpr std::uint8_t SUBTYPE_LONG_STRING_BASE_EXPONENT_8 = 0b00001000;
990 constexpr std::uint8_t SUBTYPE_LONG_STRING_BASE_EXPONENT_9 = 0b00001001;
991 constexpr std::uint8_t SUBTYPE_LONG_STRING_BASE_EXPONENT_10 = 0b00001010;
992 
993 static_assert(SUBTYPE_FALSE <= uint_max<subtype_size>);
994 static_assert(SUBTYPE_TRUE <= uint_max<subtype_size>);
995 static_assert(SUBTYPE_NULL <= uint_max<subtype_size>);
996 static_assert(SUBTYPE_POSITIVE_INTEGER <= uint_max<subtype_size>);
997 static_assert(SUBTYPE_NEGATIVE_INTEGER <= uint_max<subtype_size>);
998 static_assert(SUBTYPE_NUMBER <= uint_max<subtype_size>);
999 static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_7 <= uint_max<subtype_size>);
1000 static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_8 <= uint_max<subtype_size>);
1001 static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_9 <= uint_max<subtype_size>);
1002 static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_10 <= uint_max<subtype_size>);
1003 
1004 // Note that the binary values actually match the declared exponents
1005 static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_7 == 7);
1006 static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_8 == 8);
1007 static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_9 == 9);
1008 static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_10 == 10);
1009 } // namespace internal::ANY_PACKED_TYPE_TAG_BYTE_PREFIX
1010 #endif
1011 
1013 // clang-format on
1014 
1017 using Plan = std::variant<
1029 
1030 // Helper definitions that rely on the Plan data type
1031 #ifndef DOXYGEN
1032 // Ignore this definition on the documentation
1033 struct __internal_encoding_wrapper {
1034  const Plan value;
1035 };
1036 #endif
1037 
1038 } // namespace sourcemeta::jsonbinpack
1039 
1040 #endif
std::variant< BOUNDED_MULTIPLE_8BITS_ENUM_FIXED, FLOOR_MULTIPLE_ENUM_VARINT, ROOF_MULTIPLE_MIRROR_ENUM_VARINT, ARBITRARY_MULTIPLE_ZIGZAG_VARINT, DOUBLE_VARINT_TUPLE, BYTE_CHOICE_INDEX, LARGE_CHOICE_INDEX, TOP_LEVEL_BYTE_CHOICE_INDEX, CONST_NONE, UTF8_STRING_NO_LENGTH, FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED, ROOF_VARINT_PREFIX_UTF8_STRING_SHARED, BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED, RFC3339_DATE_INTEGER_TRIPLET, PREFIX_VARINT_LENGTH_STRING_SHARED, FIXED_TYPED_ARRAY, BOUNDED_8BITS_TYPED_ARRAY, FLOOR_TYPED_ARRAY, ROOF_TYPED_ARRAY, FIXED_TYPED_ARBITRARY_OBJECT, VARINT_TYPED_ARBITRARY_OBJECT, ANY_PACKED_TYPE_TAG_BYTE_PREFIX > Plan
Definition: runtime_plan.h:1028
The encoding consists of the the integer value divided by the multiplier encoded as a ZigZag-encoded ...
Definition: runtime_plan.h:173
const std::uint64_t multiplier
The multiplier value.
Definition: runtime_plan.h:175
The encoding consists of the byte-length of the string minus minimum plus 1 as an 8-bit fixed-length ...
Definition: runtime_plan.h:570
const std::uint64_t maximum
The inclusive maximum string UTF-8 byte-length.
Definition: runtime_plan.h:574
const std::uint64_t minimum
The inclusive minimum string UTF-8 byte-length.
Definition: runtime_plan.h:572
The encoding consists of the length of the array minus minimum encoded as an 8-bit fixed-length unsig...
Definition: runtime_plan.h:750
const std::uint64_t minimum
The minimum length of the array.
Definition: runtime_plan.h:752
const std::uint64_t maximum
The maximum length of the array.
Definition: runtime_plan.h:754
const MultiplePlans prefix_encodings
Positional encodings.
Definition: runtime_plan.h:758
const SinglePlan encoding
Element encoding.
Definition: runtime_plan.h:756
The encoding consists of the integer value divided by the multiplier, minus the ceil of minimum divid...
Definition: runtime_plan.h:59
const std::uint64_t multiplier
The multiplier value.
Definition: runtime_plan.h:65
const std::int64_t maximum
The inclusive maximum value.
Definition: runtime_plan.h:63
const std::int64_t minimum
The inclusive minimum value.
Definition: runtime_plan.h:61
The encoding consists of an index to the enumeration choices encoded as an 8-bit fixed-length unsigne...
Definition: runtime_plan.h:269
const std::vector< sourcemeta::jsontoolkit::JSON > choices
The set of choice values.
Definition: runtime_plan.h:271
The constant input value is not encoded.
Definition: runtime_plan.h:364
const sourcemeta::jsontoolkit::JSON value
The constant value.
Definition: runtime_plan.h:366
The encoding consists of a sequence of two integers: The signed integer that results from concatenati...
Definition: runtime_plan.h:223
The encoding consists of each pair encoded as the key followed by the value according to key_encoding...
Definition: runtime_plan.h:900
const SinglePlan key_encoding
Key encoding.
Definition: runtime_plan.h:904
const SinglePlan encoding
Value encoding.
Definition: runtime_plan.h:906
const std::uint64_t size
The object size.
Definition: runtime_plan.h:902
The encoding consists of the elements of the fixed array encoded in order. The encoding of the elemen...
Definition: runtime_plan.h:701
const SinglePlan encoding
Element encoding.
Definition: runtime_plan.h:705
const std::uint64_t size
The array length.
Definition: runtime_plan.h:703
const MultiplePlans prefix_encodings
Positional encodings.
Definition: runtime_plan.h:707
The encoding consists of the integer value divided by the multiplier, minus the ceil of minimum divid...
Definition: runtime_plan.h:100
const std::uint64_t multiplier
The multiplier value.
Definition: runtime_plan.h:104
const std::int64_t minimum
The inclusive minimum value.
Definition: runtime_plan.h:102
The encoding consists of the length of the array minus minimum encoded as a Base-128 64-bit Little En...
Definition: runtime_plan.h:798
const SinglePlan encoding
Element encoding.
Definition: runtime_plan.h:802
const MultiplePlans prefix_encodings
Positional encodings.
Definition: runtime_plan.h:804
const std::uint64_t minimum
The minimum length of the array.
Definition: runtime_plan.h:800
The encoding consists of the byte-length of the string minus minimum plus 1 as a Base-128 64-bit Litt...
Definition: runtime_plan.h:456
const std::uint64_t minimum
The inclusive minimum string UTF-8 byte-length.
Definition: runtime_plan.h:458
The encoding consists of an index to the enumeration choices encoded as a Base-128 64-bit Little Endi...
Definition: runtime_plan.h:303
const std::vector< sourcemeta::jsontoolkit::JSON > choices
The set of choice values.
Definition: runtime_plan.h:305
The encoding consists of the byte-length of the string plus 1 as a Base-128 64-bit Little Endian vari...
Definition: runtime_plan.h:658
The encoding consists of an implementation of RFC3339 date expressions as the sequence of 3 integers:...
Definition: runtime_plan.h:613
The encoding consists of the floor of maximum divided by the multiplier, minus the integer value divi...
Definition: runtime_plan.h:138
const std::int64_t maximum
The inclusive maximum value.
Definition: runtime_plan.h:140
const std::uint64_t multiplier
The multiplier value.
Definition: runtime_plan.h:142
The encoding consists of maximum minus the length of the array encoded as a Base-128 64-bit Little En...
Definition: runtime_plan.h:843
const SinglePlan encoding
Element encoding.
Definition: runtime_plan.h:847
const MultiplePlans prefix_encodings
Positional encodings.
Definition: runtime_plan.h:849
const std::uint64_t maximum
The maximum length of the array.
Definition: runtime_plan.h:845
The encoding consists of maximum minus the byte-length of the string plus 1 as a Base-128 64-bit Litt...
Definition: runtime_plan.h:510
const std::uint64_t maximum
The inclusive maximum string UTF-8 byte-length.
Definition: runtime_plan.h:512
If the input value corresponds to the index 0 to the enumeration choices, the encoding stores no data...
Definition: runtime_plan.h:342
const std::vector< sourcemeta::jsontoolkit::JSON > choices
The set of choice values.
Definition: runtime_plan.h:344
The encoding consist in the UTF-8 encoding of the input string.
Definition: runtime_plan.h:402
const std::uint64_t size
The string UTF-8 byte-length.
Definition: runtime_plan.h:404
The encoding consists of the number of key-value pairs in the input object as a Base-128 64-bit Littl...
Definition: runtime_plan.h:946
const SinglePlan encoding
Value encoding.
Definition: runtime_plan.h:950
const SinglePlan key_encoding
Key encoding.
Definition: runtime_plan.h:948