@transia/ripple-binary-codec 1.4.6-alpha.9 → 2.5.2-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/binary.d.ts +30 -16
- package/dist/binary.js +36 -16
- package/dist/binary.js.map +1 -1
- package/dist/enums/bytes.d.ts +1 -2
- package/dist/enums/bytes.js +3 -2
- package/dist/enums/bytes.js.map +1 -1
- package/dist/enums/constants.d.ts +1 -0
- package/dist/enums/constants.js +6 -5
- package/dist/enums/constants.js.map +1 -1
- package/dist/enums/definitions.json +1604 -969
- package/dist/enums/field.d.ts +1 -2
- package/dist/enums/field.js +1 -2
- package/dist/enums/field.js.map +1 -1
- package/dist/enums/index.d.ts +1 -2
- package/dist/enums/index.js +5 -27
- package/dist/enums/index.js.map +1 -1
- package/dist/enums/src/enums/definitions.json +1606 -971
- package/dist/enums/xrpl-definitions-base.d.ts +5 -3
- package/dist/enums/xrpl-definitions-base.js +26 -6
- package/dist/enums/xrpl-definitions-base.js.map +1 -1
- package/dist/enums/xrpl-definitions.d.ts +2 -1
- package/dist/enums/xrpl-definitions.js +2 -1
- package/dist/enums/xrpl-definitions.js.map +1 -1
- package/dist/hash-prefixes.d.ts +1 -2
- package/dist/hash-prefixes.js +8 -6
- package/dist/hash-prefixes.js.map +1 -1
- package/dist/hashes.d.ts +6 -7
- package/dist/hashes.js +6 -7
- package/dist/hashes.js.map +1 -1
- package/dist/index.d.ts +16 -11
- package/dist/index.js +53 -58
- package/dist/index.js.map +1 -1
- package/dist/ledger-hashes.d.ts +1 -2
- package/dist/ledger-hashes.js +11 -30
- package/dist/ledger-hashes.js.map +1 -1
- package/dist/quality.d.ts +3 -4
- package/dist/quality.js +10 -8
- package/dist/quality.js.map +1 -1
- package/dist/serdes/binary-parser.d.ts +2 -3
- package/dist/serdes/binary-parser.js +16 -32
- package/dist/serdes/binary-parser.js.map +1 -1
- package/dist/serdes/binary-serializer.d.ts +7 -8
- package/dist/serdes/binary-serializer.js +14 -37
- package/dist/serdes/binary-serializer.js.map +1 -1
- package/dist/shamap.d.ts +4 -5
- package/dist/shamap.js +5 -5
- package/dist/shamap.js.map +1 -1
- package/dist/src/binary.d.ts +106 -0
- package/dist/src/binary.js +154 -0
- package/dist/src/binary.js.map +1 -0
- package/dist/src/coretypes.d.ts +9 -0
- package/dist/src/coretypes.js +48 -0
- package/dist/src/coretypes.js.map +1 -0
- package/dist/src/enums/bytes.d.ts +25 -0
- package/dist/src/enums/bytes.js +65 -0
- package/dist/src/enums/bytes.js.map +1 -0
- package/dist/src/enums/constants.d.ts +5 -0
- package/dist/src/enums/constants.js +9 -0
- package/dist/src/enums/constants.js.map +1 -0
- package/dist/src/enums/definitions.json +3404 -0
- package/dist/src/enums/field.d.ts +28 -0
- package/dist/src/enums/field.js +58 -0
- package/dist/src/enums/field.js.map +1 -0
- package/dist/src/enums/index.d.ts +12 -0
- package/dist/src/enums/index.js +31 -0
- package/dist/src/enums/index.js.map +1 -0
- package/dist/src/enums/utils-renumber.d.ts +101 -0
- package/dist/src/enums/utils-renumber.js +127 -0
- package/dist/src/enums/utils-renumber.js.map +1 -0
- package/dist/src/enums/xrpl-definitions-base.d.ts +47 -0
- package/dist/src/enums/xrpl-definitions-base.js +83 -0
- package/dist/src/enums/xrpl-definitions-base.js.map +1 -0
- package/dist/src/enums/xrpl-definitions.d.ts +22 -0
- package/dist/src/enums/xrpl-definitions.js +30 -0
- package/dist/src/enums/xrpl-definitions.js.map +1 -0
- package/dist/src/hash-prefixes.d.ts +5 -0
- package/dist/src/hash-prefixes.js +43 -0
- package/dist/src/hash-prefixes.js.map +1 -0
- package/dist/src/hashes.d.ts +50 -0
- package/dist/src/hashes.js +76 -0
- package/dist/src/hashes.js.map +1 -0
- package/dist/src/index.d.ts +69 -0
- package/dist/src/index.js +134 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/ledger-hashes.d.ts +48 -0
- package/dist/src/ledger-hashes.js +141 -0
- package/dist/src/ledger-hashes.js.map +1 -0
- package/dist/src/quality.d.ts +21 -0
- package/dist/src/quality.js +42 -0
- package/dist/src/quality.js.map +1 -0
- package/dist/src/serdes/binary-parser.d.ts +100 -0
- package/dist/src/serdes/binary-parser.js +194 -0
- package/dist/src/serdes/binary-parser.js.map +1 -0
- package/dist/src/serdes/binary-serializer.d.ts +81 -0
- package/dist/src/serdes/binary-serializer.js +149 -0
- package/dist/src/serdes/binary-serializer.js.map +1 -0
- package/dist/src/shamap.d.ts +102 -0
- package/dist/src/shamap.js +172 -0
- package/dist/src/shamap.js.map +1 -0
- package/dist/src/types/account-id.d.ts +37 -0
- package/dist/src/types/account-id.js +73 -0
- package/dist/src/types/account-id.js.map +1 -0
- package/dist/src/types/amount.d.ts +92 -0
- package/dist/src/types/amount.js +292 -0
- package/dist/src/types/amount.js.map +1 -0
- package/dist/src/types/blob.d.ts +25 -0
- package/dist/src/types/blob.js +46 -0
- package/dist/src/types/blob.js.map +1 -0
- package/dist/src/types/currency.d.ts +30 -0
- package/dist/src/types/currency.js +131 -0
- package/dist/src/types/currency.js.map +1 -0
- package/dist/src/types/data.d.ts +104 -0
- package/dist/src/types/data.js +387 -0
- package/dist/src/types/data.js.map +1 -0
- package/dist/src/types/dataType.d.ts +94 -0
- package/dist/src/types/dataType.js +145 -0
- package/dist/src/types/dataType.js.map +1 -0
- package/dist/src/types/hash-128.d.ts +18 -0
- package/dist/src/types/hash-128.js +36 -0
- package/dist/src/types/hash-128.js.map +1 -0
- package/dist/src/types/hash-160.d.ts +12 -0
- package/dist/src/types/hash-160.js +23 -0
- package/dist/src/types/hash-160.js.map +1 -0
- package/dist/src/types/hash-192.d.ts +12 -0
- package/dist/src/types/hash-192.js +23 -0
- package/dist/src/types/hash-192.js.map +1 -0
- package/dist/src/types/hash-256.d.ts +12 -0
- package/dist/src/types/hash-256.js +20 -0
- package/dist/src/types/hash-256.js.map +1 -0
- package/dist/src/types/hash.d.ts +40 -0
- package/dist/src/types/hash.js +76 -0
- package/dist/src/types/hash.js.map +1 -0
- package/dist/src/types/index.d.ts +21 -0
- package/dist/src/types/index.js +72 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/issue.d.ts +49 -0
- package/dist/src/types/issue.js +107 -0
- package/dist/src/types/issue.js.map +1 -0
- package/dist/src/types/json.d.ts +169 -0
- package/dist/src/types/json.js +529 -0
- package/dist/src/types/json.js.map +1 -0
- package/dist/src/types/path-set.d.ts +37 -0
- package/dist/src/types/path-set.js +236 -0
- package/dist/src/types/path-set.js.map +1 -0
- package/dist/src/types/serialized-type.d.ts +117 -0
- package/dist/src/types/serialized-type.js +242 -0
- package/dist/src/types/serialized-type.js.map +1 -0
- package/dist/src/types/st-array.d.ts +32 -0
- package/dist/src/types/st-array.js +89 -0
- package/dist/src/types/st-array.js.map +1 -0
- package/dist/src/types/st-number.d.ts +55 -0
- package/dist/src/types/st-number.js +212 -0
- package/dist/src/types/st-number.js.map +1 -0
- package/dist/src/types/st-object.d.ts +33 -0
- package/dist/src/types/st-object.js +176 -0
- package/dist/src/types/st-object.js.map +1 -0
- package/dist/src/types/uint-16.d.ts +26 -0
- package/dist/src/types/uint-16.js +49 -0
- package/dist/src/types/uint-16.js.map +1 -0
- package/dist/src/types/uint-32.d.ts +26 -0
- package/dist/src/types/uint-32.js +54 -0
- package/dist/src/types/uint-32.js.map +1 -0
- package/dist/src/types/uint-64.d.ts +40 -0
- package/dist/src/types/uint-64.js +112 -0
- package/dist/src/types/uint-64.js.map +1 -0
- package/dist/src/types/uint-8.d.ts +26 -0
- package/dist/src/types/uint-8.js +50 -0
- package/dist/src/types/uint-8.js.map +1 -0
- package/dist/src/types/uint.d.ts +29 -0
- package/dist/src/types/uint.js +47 -0
- package/dist/src/types/uint.js.map +1 -0
- package/dist/src/types/vector-256.d.ts +31 -0
- package/dist/src/types/vector-256.js +76 -0
- package/dist/src/types/vector-256.js.map +1 -0
- package/dist/src/types/xchain-bridge.d.ts +45 -0
- package/dist/src/types/xchain-bridge.js +105 -0
- package/dist/src/types/xchain-bridge.js.map +1 -0
- package/dist/src/utils.d.ts +79 -0
- package/dist/src/utils.js +181 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/account-id.d.ts +3 -2
- package/dist/types/account-id.js +8 -6
- package/dist/types/account-id.js.map +1 -1
- package/dist/types/amount.d.ts +33 -9
- package/dist/types/amount.js +128 -43
- package/dist/types/amount.js.map +1 -1
- package/dist/types/blob.d.ts +3 -3
- package/dist/types/blob.js +8 -2
- package/dist/types/blob.js.map +1 -1
- package/dist/types/currency.d.ts +3 -2
- package/dist/types/currency.js +17 -13
- package/dist/types/currency.js.map +1 -1
- package/dist/types/data.d.ts +104 -0
- package/dist/types/data.js +387 -0
- package/dist/types/data.js.map +1 -0
- package/dist/types/dataType.d.ts +94 -0
- package/dist/types/dataType.js +145 -0
- package/dist/types/dataType.js.map +1 -0
- package/dist/types/hash-128.d.ts +3 -2
- package/dist/types/hash-128.js +7 -3
- package/dist/types/hash-128.js.map +1 -1
- package/dist/types/hash-160.d.ts +3 -2
- package/dist/types/hash-160.js +5 -2
- package/dist/types/hash-160.js.map +1 -1
- package/dist/types/hash-192.d.ts +12 -0
- package/dist/types/hash-192.js +23 -0
- package/dist/types/hash-192.js.map +1 -0
- package/dist/types/hash-256.d.ts +3 -2
- package/dist/types/hash-256.js +5 -2
- package/dist/types/hash-256.js.map +1 -1
- package/dist/types/hash.d.ts +2 -3
- package/dist/types/hash.js +8 -4
- package/dist/types/hash.js.map +1 -1
- package/dist/types/index.d.ts +4 -1
- package/dist/types/index.js +17 -2
- package/dist/types/index.js.map +1 -1
- package/dist/types/issue.d.ts +49 -0
- package/dist/types/issue.js +107 -0
- package/dist/types/issue.js.map +1 -0
- package/dist/types/json.d.ts +169 -0
- package/dist/types/json.js +529 -0
- package/dist/types/json.js.map +1 -0
- package/dist/types/path-set.d.ts +2 -1
- package/dist/types/path-set.js +15 -12
- package/dist/types/path-set.js.map +1 -1
- package/dist/types/serialized-type.d.ts +70 -16
- package/dist/types/serialized-type.js +153 -8
- package/dist/types/serialized-type.js.map +1 -1
- package/dist/types/st-array.d.ts +7 -3
- package/dist/types/st-array.js +20 -11
- package/dist/types/st-array.js.map +1 -1
- package/dist/types/st-number.d.ts +55 -0
- package/dist/types/st-number.js +212 -0
- package/dist/types/st-number.js.map +1 -0
- package/dist/types/st-object.d.ts +2 -1
- package/dist/types/st-object.js +35 -8
- package/dist/types/st-object.js.map +1 -1
- package/dist/types/uint-16.d.ts +3 -2
- package/dist/types/uint-16.js +10 -5
- package/dist/types/uint-16.js.map +1 -1
- package/dist/types/uint-32.d.ts +3 -2
- package/dist/types/uint-32.js +11 -6
- package/dist/types/uint-32.js.map +1 -1
- package/dist/types/uint-64.d.ts +8 -7
- package/dist/types/uint-64.js +49 -24
- package/dist/types/uint-64.js.map +1 -1
- package/dist/types/uint-8.d.ts +3 -2
- package/dist/types/uint-8.js +11 -5
- package/dist/types/uint-8.js.map +1 -1
- package/dist/types/uint.d.ts +5 -6
- package/dist/types/uint.js +5 -0
- package/dist/types/uint.js.map +1 -1
- package/dist/types/vector-256.d.ts +3 -3
- package/dist/types/vector-256.js +5 -4
- package/dist/types/vector-256.js.map +1 -1
- package/dist/types/xchain-bridge.d.ts +45 -0
- package/dist/types/xchain-bridge.js +105 -0
- package/dist/types/xchain-bridge.js.map +1 -0
- package/dist/utils.d.ts +79 -0
- package/dist/utils.js +181 -0
- package/dist/utils.js.map +1 -0
- package/package.json +21 -17
- package/src/README.md +5 -0
- package/src/binary.ts +235 -0
- package/src/coretypes.ts +31 -0
- package/src/enums/README.md +144 -0
- package/src/enums/bytes.ts +80 -0
- package/src/enums/constants.ts +5 -0
- package/src/enums/definitions.json +3409 -0
- package/src/enums/field.ts +84 -0
- package/src/enums/index.ts +34 -0
- package/src/enums/utils-renumber.ts +134 -0
- package/src/enums/xrpl-definitions-base.ts +150 -0
- package/src/enums/xrpl-definitions.ts +33 -0
- package/src/hash-prefixes.ts +42 -0
- package/src/hashes.ts +75 -0
- package/src/index.ts +166 -0
- package/src/ledger-hashes.ts +191 -0
- package/src/quality.ts +38 -0
- package/src/serdes/binary-parser.ts +228 -0
- package/src/serdes/binary-serializer.ts +166 -0
- package/src/shamap.ts +189 -0
- package/src/types/account-id.ts +91 -0
- package/src/types/amount.ts +366 -0
- package/src/types/blob.ts +50 -0
- package/src/types/currency.ts +145 -0
- package/src/types/data.ts +496 -0
- package/src/types/dataType.ts +178 -0
- package/src/types/hash-128.ts +38 -0
- package/src/types/hash-160.ts +24 -0
- package/src/types/hash-192.ts +24 -0
- package/src/types/hash-256.ts +20 -0
- package/src/types/hash.ts +86 -0
- package/src/types/index.ts +74 -0
- package/src/types/issue.ts +140 -0
- package/src/types/json.ts +641 -0
- package/src/types/path-set.ts +294 -0
- package/src/types/serialized-type.ts +271 -0
- package/src/types/st-array.ts +117 -0
- package/src/types/st-number.ts +238 -0
- package/src/types/st-object.ts +219 -0
- package/src/types/uint-16.ts +58 -0
- package/src/types/uint-32.ts +64 -0
- package/src/types/uint-64.ts +141 -0
- package/src/types/uint-8.ts +57 -0
- package/src/types/uint.ts +60 -0
- package/src/types/vector-256.ts +83 -0
- package/src/types/xchain-bridge.ts +132 -0
- package/src/utils.ts +220 -0
- package/test/amount.test.js +0 -43
- package/test/binary-json.test.js +0 -45
- package/test/binary-parser.test.js +0 -396
- package/test/binary-serializer.test.js +0 -319
- package/test/definitions.test.js +0 -100
- package/test/fixtures/account-tx-transactions.db +0 -0
- package/test/fixtures/codec-fixtures.json +0 -4466
- package/test/fixtures/data-driven-tests.json +0 -2919
- package/test/fixtures/delivermin-tx-binary.json +0 -1
- package/test/fixtures/delivermin-tx.json +0 -98
- package/test/fixtures/deposit-preauth-tx-binary.json +0 -1
- package/test/fixtures/deposit-preauth-tx-meta-binary.json +0 -1
- package/test/fixtures/deposit-preauth-tx.json +0 -58
- package/test/fixtures/escrow-cancel-binary.json +0 -1
- package/test/fixtures/escrow-cancel-tx.json +0 -6
- package/test/fixtures/escrow-create-binary.json +0 -1
- package/test/fixtures/escrow-create-tx.json +0 -10
- package/test/fixtures/escrow-finish-binary.json +0 -1
- package/test/fixtures/escrow-finish-meta-binary.json +0 -1
- package/test/fixtures/escrow-finish-tx.json +0 -95
- package/test/fixtures/ledger-full-38129.json +0 -1
- package/test/fixtures/ledger-full-40000.json +0 -1
- package/test/fixtures/negative-unl.json +0 -12
- package/test/fixtures/nf-token.json +0 -547
- package/test/fixtures/payment-channel-claim-binary.json +0 -1
- package/test/fixtures/payment-channel-claim-tx.json +0 -8
- package/test/fixtures/payment-channel-create-binary.json +0 -1
- package/test/fixtures/payment-channel-create-tx.json +0 -11
- package/test/fixtures/payment-channel-fund-binary.json +0 -1
- package/test/fixtures/payment-channel-fund-tx.json +0 -7
- package/test/fixtures/remit-binary.json +0 -1
- package/test/fixtures/remit-tx.json +0 -39
- package/test/fixtures/signerlistset-tx-binary.json +0 -1
- package/test/fixtures/signerlistset-tx-meta-binary.json +0 -1
- package/test/fixtures/signerlistset-tx.json +0 -94
- package/test/fixtures/ticket-create-binary.json +0 -1
- package/test/fixtures/ticket-create-tx.json +0 -7
- package/test/fixtures/unl-report-binary.json +0 -1
- package/test/fixtures/unl-report-meta-binary.json +0 -1
- package/test/fixtures/unl-report.json +0 -89
- package/test/fixtures/x-codec-fixtures.json +0 -188
- package/test/hash.test.js +0 -135
- package/test/ledger.test.js +0 -29
- package/test/lower-case-hex.test.js +0 -46
- package/test/pseudo-transaction.test.js +0 -38
- package/test/quality.test.js +0 -15
- package/test/shamap.test.js +0 -89
- package/test/signing-data-encoding.test.js +0 -213
- package/test/tx-encode-decode.test.js +0 -119
- package/test/types.test.js +0 -34
- package/test/uint.test.js +0 -148
- package/test/utils.js +0 -30
- package/test/x-address.test.js +0 -181
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { BinaryParser } from '../serdes/binary-parser';
|
|
2
|
+
import { JsonObject, SerializedType, SerializedTypeID } from './serialized-type';
|
|
3
|
+
import { BinarySerializer } from '../serdes/binary-serializer';
|
|
4
|
+
/**
|
|
5
|
+
* STJson: Serialized Type for JSON-like structures (objects or arrays).
|
|
6
|
+
*
|
|
7
|
+
* Supports two modes:
|
|
8
|
+
* - Object: Key-value pairs where keys are VL-encoded strings
|
|
9
|
+
* - Array: Ordered list of values
|
|
10
|
+
*
|
|
11
|
+
* Values are [SType marker][VL-encoded SType serialization].
|
|
12
|
+
* Values can be any SType, including nested STJson.
|
|
13
|
+
*
|
|
14
|
+
* Serialization format: [type_byte][VL_length][data...]
|
|
15
|
+
* - type_byte: 0x00 = Object, 0x01 = Array
|
|
16
|
+
*
|
|
17
|
+
* Depth constraint: Maximum nesting depth of 1 level
|
|
18
|
+
*/
|
|
19
|
+
declare class STJson extends SerializedType {
|
|
20
|
+
private static readonly JsonType;
|
|
21
|
+
private data;
|
|
22
|
+
private jsonType;
|
|
23
|
+
private default_;
|
|
24
|
+
/**
|
|
25
|
+
* Construct STJson from bytes
|
|
26
|
+
* @param bytes - Uint8Array containing serialized JSON
|
|
27
|
+
*/
|
|
28
|
+
constructor(bytes: Uint8Array);
|
|
29
|
+
/**
|
|
30
|
+
* Parse STJson from BinaryParser
|
|
31
|
+
*
|
|
32
|
+
* @param parser - BinaryParser positioned at the start of STJson
|
|
33
|
+
* @returns STJson instance
|
|
34
|
+
*/
|
|
35
|
+
static fromParser(parser: BinaryParser): STJson;
|
|
36
|
+
/**
|
|
37
|
+
* Parse a single key-value pair from the parser
|
|
38
|
+
*/
|
|
39
|
+
private static parsePair;
|
|
40
|
+
/**
|
|
41
|
+
* Factory for SType value from VL blob (with SType marker)
|
|
42
|
+
*/
|
|
43
|
+
private static makeValueFromVLWithType;
|
|
44
|
+
/**
|
|
45
|
+
* Check if this is an array type
|
|
46
|
+
*/
|
|
47
|
+
isArray(): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Check if this is an object type
|
|
50
|
+
*/
|
|
51
|
+
isObject(): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Get the JSON type
|
|
54
|
+
*/
|
|
55
|
+
getType(): number;
|
|
56
|
+
/**
|
|
57
|
+
* Get nesting depth (0 = no nesting, 1 = one level of nesting)
|
|
58
|
+
*/
|
|
59
|
+
getDepth(): number;
|
|
60
|
+
/**
|
|
61
|
+
* Validate nesting depth (max 1 level)
|
|
62
|
+
*/
|
|
63
|
+
private validateDepth;
|
|
64
|
+
/**
|
|
65
|
+
* Set a field in an object
|
|
66
|
+
*/
|
|
67
|
+
setObjectField(key: string, value: SerializedType | null): void;
|
|
68
|
+
/**
|
|
69
|
+
* Get a field from an object
|
|
70
|
+
*/
|
|
71
|
+
getObjectField(key: string): SerializedType | null | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Set a nested object field (one level deep)
|
|
74
|
+
*/
|
|
75
|
+
setNestedObjectField(key: string, nestedKey: string, value: SerializedType | null): void;
|
|
76
|
+
/**
|
|
77
|
+
* Get a nested object field
|
|
78
|
+
*/
|
|
79
|
+
getNestedObjectField(key: string, nestedKey: string): SerializedType | null | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Get the inner data as a Map (for objects)
|
|
82
|
+
*/
|
|
83
|
+
getMap(): Map<string, SerializedType | null>;
|
|
84
|
+
/**
|
|
85
|
+
* Get the inner data as an array
|
|
86
|
+
*/
|
|
87
|
+
getArray(): (SerializedType | null)[];
|
|
88
|
+
/**
|
|
89
|
+
* Push an element to an array
|
|
90
|
+
*/
|
|
91
|
+
pushArrayElement(value: SerializedType | null): void;
|
|
92
|
+
/**
|
|
93
|
+
* Get an array element by index
|
|
94
|
+
*/
|
|
95
|
+
getArrayElement(index: number): SerializedType | null | undefined;
|
|
96
|
+
/**
|
|
97
|
+
* Set an array element by index
|
|
98
|
+
*/
|
|
99
|
+
setArrayElement(index: number, value: SerializedType | null): void;
|
|
100
|
+
/**
|
|
101
|
+
* Set a field within an array element (element must be an object)
|
|
102
|
+
*/
|
|
103
|
+
setArrayElementField(index: number, key: string, value: SerializedType | null): void;
|
|
104
|
+
/**
|
|
105
|
+
* Get a field within an array element
|
|
106
|
+
*/
|
|
107
|
+
getArrayElementField(index: number, key: string): SerializedType | null | undefined;
|
|
108
|
+
/**
|
|
109
|
+
* Get the size of the array
|
|
110
|
+
*/
|
|
111
|
+
arraySize(): number;
|
|
112
|
+
/**
|
|
113
|
+
* Set a nested array element (array stored in object field)
|
|
114
|
+
*/
|
|
115
|
+
setNestedArrayElement(key: string, index: number, value: SerializedType | null): void;
|
|
116
|
+
/**
|
|
117
|
+
* Set a field within a nested array element
|
|
118
|
+
*/
|
|
119
|
+
setNestedArrayElementField(key: string, index: number, nestedKey: string, value: SerializedType | null): void;
|
|
120
|
+
/**
|
|
121
|
+
* Get a nested array element
|
|
122
|
+
*/
|
|
123
|
+
getNestedArrayElement(key: string, index: number): SerializedType | null | undefined;
|
|
124
|
+
/**
|
|
125
|
+
* Get a field within a nested array element
|
|
126
|
+
*/
|
|
127
|
+
getNestedArrayElementField(key: string, index: number, nestedKey: string): SerializedType | null | undefined;
|
|
128
|
+
/**
|
|
129
|
+
* Serialize to binary
|
|
130
|
+
*/
|
|
131
|
+
add(s: BinarySerializer): void;
|
|
132
|
+
/**
|
|
133
|
+
* Encode a key as VL
|
|
134
|
+
*/
|
|
135
|
+
private static addVLKey;
|
|
136
|
+
/**
|
|
137
|
+
* Encode a value as [SType marker][VL]
|
|
138
|
+
*/
|
|
139
|
+
private static addVLValue;
|
|
140
|
+
/**
|
|
141
|
+
* Convert to JSON representation
|
|
142
|
+
*/
|
|
143
|
+
toJSON(): JsonObject | JsonObject[];
|
|
144
|
+
/**
|
|
145
|
+
* Compare with another STJson for equivalence
|
|
146
|
+
*/
|
|
147
|
+
isEquivalent(t: SerializedType): boolean;
|
|
148
|
+
/**
|
|
149
|
+
* Check if this is the default value
|
|
150
|
+
*/
|
|
151
|
+
isDefault(): boolean;
|
|
152
|
+
/**
|
|
153
|
+
* Get blob representation
|
|
154
|
+
*/
|
|
155
|
+
toBlob(): Uint8Array;
|
|
156
|
+
/**
|
|
157
|
+
* Get the size (number of bytes in serialized form)
|
|
158
|
+
*/
|
|
159
|
+
size(): number;
|
|
160
|
+
/**
|
|
161
|
+
* Set the value from another STJson
|
|
162
|
+
*/
|
|
163
|
+
setValue(v: STJson): void;
|
|
164
|
+
/**
|
|
165
|
+
* Get serialized type ID
|
|
166
|
+
*/
|
|
167
|
+
getSType(): SerializedTypeID;
|
|
168
|
+
}
|
|
169
|
+
export { STJson };
|
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.STJson = void 0;
|
|
4
|
+
/* eslint-disable max-lines */
|
|
5
|
+
/* eslint-disable complexity */
|
|
6
|
+
const binary_parser_1 = require("../serdes/binary-parser");
|
|
7
|
+
const serialized_type_1 = require("./serialized-type");
|
|
8
|
+
const utils_1 = require("@transia/isomorphic/utils");
|
|
9
|
+
const binary_serializer_1 = require("../serdes/binary-serializer");
|
|
10
|
+
/**
|
|
11
|
+
* STJson: Serialized Type for JSON-like structures (objects or arrays).
|
|
12
|
+
*
|
|
13
|
+
* Supports two modes:
|
|
14
|
+
* - Object: Key-value pairs where keys are VL-encoded strings
|
|
15
|
+
* - Array: Ordered list of values
|
|
16
|
+
*
|
|
17
|
+
* Values are [SType marker][VL-encoded SType serialization].
|
|
18
|
+
* Values can be any SType, including nested STJson.
|
|
19
|
+
*
|
|
20
|
+
* Serialization format: [type_byte][VL_length][data...]
|
|
21
|
+
* - type_byte: 0x00 = Object, 0x01 = Array
|
|
22
|
+
*
|
|
23
|
+
* Depth constraint: Maximum nesting depth of 1 level
|
|
24
|
+
*/
|
|
25
|
+
class STJson extends serialized_type_1.SerializedType {
|
|
26
|
+
/**
|
|
27
|
+
* Construct STJson from bytes
|
|
28
|
+
* @param bytes - Uint8Array containing serialized JSON
|
|
29
|
+
*/
|
|
30
|
+
constructor(bytes) {
|
|
31
|
+
super(bytes);
|
|
32
|
+
this.default_ = false;
|
|
33
|
+
this.data = new Map();
|
|
34
|
+
this.jsonType = STJson.JsonType.Object;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Parse STJson from BinaryParser
|
|
38
|
+
*
|
|
39
|
+
* @param parser - BinaryParser positioned at the start of STJson
|
|
40
|
+
* @returns STJson instance
|
|
41
|
+
*/
|
|
42
|
+
static fromParser(parser) {
|
|
43
|
+
const dataLength = parser.readVariableLengthLength();
|
|
44
|
+
if (dataLength < 0) {
|
|
45
|
+
throw new Error('Invalid STJson length');
|
|
46
|
+
}
|
|
47
|
+
if (dataLength === 0) {
|
|
48
|
+
const json = new STJson(new Uint8Array());
|
|
49
|
+
json.data = new Map();
|
|
50
|
+
return json;
|
|
51
|
+
}
|
|
52
|
+
// Read type byte
|
|
53
|
+
const typeByte = parser.read(1)[0];
|
|
54
|
+
const type = typeByte;
|
|
55
|
+
const initialBytesLeft = parser.size();
|
|
56
|
+
if (type === STJson.JsonType.Array) {
|
|
57
|
+
const array = [];
|
|
58
|
+
while (parser.size() > 0 &&
|
|
59
|
+
initialBytesLeft - parser.size() < dataLength) {
|
|
60
|
+
const valueVL = parser.readVariableLength();
|
|
61
|
+
if (valueVL.length > 0) {
|
|
62
|
+
const valueSit = new binary_parser_1.BinaryParser((0, utils_1.bytesToHex)(valueVL));
|
|
63
|
+
const value = STJson.makeValueFromVLWithType(valueSit);
|
|
64
|
+
array.push(value);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
array.push(null);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const json = new STJson(new Uint8Array());
|
|
71
|
+
json.data = array;
|
|
72
|
+
json.jsonType = STJson.JsonType.Array;
|
|
73
|
+
return json;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
// JsonType.Object
|
|
77
|
+
const map = new Map();
|
|
78
|
+
while (parser.size() > 0 &&
|
|
79
|
+
initialBytesLeft - parser.size() < dataLength) {
|
|
80
|
+
const [key, value] = STJson.parsePair(parser);
|
|
81
|
+
map.set(key, value);
|
|
82
|
+
}
|
|
83
|
+
const json = new STJson(new Uint8Array());
|
|
84
|
+
json.data = map;
|
|
85
|
+
json.jsonType = STJson.JsonType.Object;
|
|
86
|
+
return json;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Parse a single key-value pair from the parser
|
|
91
|
+
*/
|
|
92
|
+
static parsePair(parser) {
|
|
93
|
+
const keyVL = parser.readVariableLength();
|
|
94
|
+
const key = new TextDecoder().decode(keyVL);
|
|
95
|
+
const valueVL = parser.readVariableLength();
|
|
96
|
+
let value = null;
|
|
97
|
+
if (valueVL.length > 0) {
|
|
98
|
+
const valueSit = new binary_parser_1.BinaryParser((0, utils_1.bytesToHex)(valueVL));
|
|
99
|
+
value = STJson.makeValueFromVLWithType(valueSit);
|
|
100
|
+
}
|
|
101
|
+
return [key, value];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Factory for SType value from VL blob (with SType marker)
|
|
105
|
+
*/
|
|
106
|
+
static makeValueFromVLWithType(parser) {
|
|
107
|
+
if (parser.size() === 0) {
|
|
108
|
+
throw new Error('Empty data when parsing STJson value');
|
|
109
|
+
}
|
|
110
|
+
const typeId = parser.read(1)[0];
|
|
111
|
+
// Delegate to appropriate type's fromParser
|
|
112
|
+
// This is a placeholder - actual implementation would dispatch to concrete types
|
|
113
|
+
// For now, we create an STJson if type is Object or Array
|
|
114
|
+
if (typeId === STJson.JsonType.Object || typeId === STJson.JsonType.Array) {
|
|
115
|
+
return STJson.fromParser(parser);
|
|
116
|
+
}
|
|
117
|
+
throw new Error(`Unsupported type ID in STJson: ${typeId}`);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Check if this is an array type
|
|
121
|
+
*/
|
|
122
|
+
isArray() {
|
|
123
|
+
return this.jsonType === STJson.JsonType.Array;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Check if this is an object type
|
|
127
|
+
*/
|
|
128
|
+
isObject() {
|
|
129
|
+
return this.jsonType === STJson.JsonType.Object;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Get the JSON type
|
|
133
|
+
*/
|
|
134
|
+
getType() {
|
|
135
|
+
return this.jsonType;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Get nesting depth (0 = no nesting, 1 = one level of nesting)
|
|
139
|
+
*/
|
|
140
|
+
getDepth() {
|
|
141
|
+
if (this.isArray()) {
|
|
142
|
+
const array = this.data;
|
|
143
|
+
for (const value of array) {
|
|
144
|
+
if (value && value instanceof STJson) {
|
|
145
|
+
return 1 + value.getDepth();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return 0;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
// isObject()
|
|
152
|
+
const map = this.data;
|
|
153
|
+
for (const value of map.values()) {
|
|
154
|
+
if (value && value instanceof STJson) {
|
|
155
|
+
return 1 + value.getDepth();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return 0;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Validate nesting depth (max 1 level)
|
|
163
|
+
*/
|
|
164
|
+
validateDepth(value, currentDepth) {
|
|
165
|
+
if (!value) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (!(value instanceof STJson)) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const valueDepth = value.getDepth();
|
|
172
|
+
if (currentDepth + valueDepth > 1) {
|
|
173
|
+
throw new Error('STJson nesting depth exceeds maximum of 1');
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Set a field in an object
|
|
178
|
+
*/
|
|
179
|
+
setObjectField(key, value) {
|
|
180
|
+
if (!this.isObject()) {
|
|
181
|
+
throw new Error('STJson::setObjectField called on non-object');
|
|
182
|
+
}
|
|
183
|
+
this.validateDepth(value, 0);
|
|
184
|
+
this.data.set(key, value);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get a field from an object
|
|
188
|
+
*/
|
|
189
|
+
getObjectField(key) {
|
|
190
|
+
if (!this.isObject()) {
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
return this.data.get(key);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Set a nested object field (one level deep)
|
|
197
|
+
*/
|
|
198
|
+
setNestedObjectField(key, nestedKey, value) {
|
|
199
|
+
if (!this.isObject()) {
|
|
200
|
+
throw new Error('STJson::setNestedObjectField called on non-object');
|
|
201
|
+
}
|
|
202
|
+
const map = this.data;
|
|
203
|
+
let nestedObj = map.get(key);
|
|
204
|
+
if (!nestedObj || !(nestedObj instanceof STJson) || !nestedObj.isObject()) {
|
|
205
|
+
const newNested = new STJson(new Uint8Array());
|
|
206
|
+
newNested.data = new Map();
|
|
207
|
+
newNested.jsonType = STJson.JsonType.Object;
|
|
208
|
+
map.set(key, newNested);
|
|
209
|
+
nestedObj = newNested;
|
|
210
|
+
}
|
|
211
|
+
if (nestedObj instanceof STJson) {
|
|
212
|
+
nestedObj.setObjectField(nestedKey, value);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Get a nested object field
|
|
217
|
+
*/
|
|
218
|
+
getNestedObjectField(key, nestedKey) {
|
|
219
|
+
if (!this.isObject()) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
const nestedObj = this.data.get(key);
|
|
223
|
+
if (nestedObj instanceof STJson && nestedObj.isObject()) {
|
|
224
|
+
return nestedObj.getObjectField(nestedKey);
|
|
225
|
+
}
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Get the inner data as a Map (for objects)
|
|
230
|
+
*/
|
|
231
|
+
getMap() {
|
|
232
|
+
if (!this.isObject()) {
|
|
233
|
+
throw new Error('STJson is not an object type');
|
|
234
|
+
}
|
|
235
|
+
return this.data;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Get the inner data as an array
|
|
239
|
+
*/
|
|
240
|
+
getArray() {
|
|
241
|
+
if (!this.isArray()) {
|
|
242
|
+
throw new Error('STJson is not an array type');
|
|
243
|
+
}
|
|
244
|
+
return this.data;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Push an element to an array
|
|
248
|
+
*/
|
|
249
|
+
pushArrayElement(value) {
|
|
250
|
+
if (!this.isArray()) {
|
|
251
|
+
throw new Error('STJson::pushArrayElement called on non-array');
|
|
252
|
+
}
|
|
253
|
+
this.validateDepth(value, 0);
|
|
254
|
+
this.data.push(value);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Get an array element by index
|
|
258
|
+
*/
|
|
259
|
+
getArrayElement(index) {
|
|
260
|
+
if (!this.isArray()) {
|
|
261
|
+
return undefined;
|
|
262
|
+
}
|
|
263
|
+
const array = this.data;
|
|
264
|
+
return array[index];
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Set an array element by index
|
|
268
|
+
*/
|
|
269
|
+
setArrayElement(index, value) {
|
|
270
|
+
if (!this.isArray()) {
|
|
271
|
+
throw new Error('STJson::setArrayElement called on non-array');
|
|
272
|
+
}
|
|
273
|
+
this.validateDepth(value, 0);
|
|
274
|
+
const array = this.data;
|
|
275
|
+
// Auto-resize with nulls if needed
|
|
276
|
+
if (index >= array.length) {
|
|
277
|
+
array.length = index + 1;
|
|
278
|
+
array.fill(null);
|
|
279
|
+
}
|
|
280
|
+
array[index] = value;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Set a field within an array element (element must be an object)
|
|
284
|
+
*/
|
|
285
|
+
setArrayElementField(index, key, value) {
|
|
286
|
+
if (!this.isArray()) {
|
|
287
|
+
throw new Error('STJson::setArrayElementField called on non-array');
|
|
288
|
+
}
|
|
289
|
+
this.validateDepth(value, 1);
|
|
290
|
+
const array = this.data;
|
|
291
|
+
// Auto-resize with nulls if needed
|
|
292
|
+
if (index >= array.length) {
|
|
293
|
+
array.length = index + 1;
|
|
294
|
+
array.fill(null);
|
|
295
|
+
}
|
|
296
|
+
let element = array[index];
|
|
297
|
+
if (!element || !(element instanceof STJson) || !element.isObject()) {
|
|
298
|
+
const newElement = new STJson(new Uint8Array());
|
|
299
|
+
newElement.data = new Map();
|
|
300
|
+
newElement.jsonType = STJson.JsonType.Object;
|
|
301
|
+
array[index] = newElement;
|
|
302
|
+
element = newElement;
|
|
303
|
+
}
|
|
304
|
+
if (element instanceof STJson) {
|
|
305
|
+
element.setObjectField(key, value);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Get a field within an array element
|
|
310
|
+
*/
|
|
311
|
+
getArrayElementField(index, key) {
|
|
312
|
+
if (!this.isArray()) {
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
const array = this.data;
|
|
316
|
+
if (index >= array.length) {
|
|
317
|
+
return undefined;
|
|
318
|
+
}
|
|
319
|
+
const element = array[index];
|
|
320
|
+
if (element instanceof STJson && element.isObject()) {
|
|
321
|
+
return element.getObjectField(key);
|
|
322
|
+
}
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Get the size of the array
|
|
327
|
+
*/
|
|
328
|
+
arraySize() {
|
|
329
|
+
if (!this.isArray()) {
|
|
330
|
+
return 0;
|
|
331
|
+
}
|
|
332
|
+
return this.data.length;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Set a nested array element (array stored in object field)
|
|
336
|
+
*/
|
|
337
|
+
setNestedArrayElement(key, index, value) {
|
|
338
|
+
if (!this.isObject()) {
|
|
339
|
+
throw new Error('STJson::setNestedArrayElement called on non-object');
|
|
340
|
+
}
|
|
341
|
+
this.validateDepth(value, 1);
|
|
342
|
+
const map = this.data;
|
|
343
|
+
let arrayJson = map.get(key);
|
|
344
|
+
if (!arrayJson || !(arrayJson instanceof STJson) || !arrayJson.isArray()) {
|
|
345
|
+
const newArray = new STJson(new Uint8Array());
|
|
346
|
+
newArray.data = [];
|
|
347
|
+
newArray.jsonType = STJson.JsonType.Array;
|
|
348
|
+
map.set(key, newArray);
|
|
349
|
+
arrayJson = newArray;
|
|
350
|
+
}
|
|
351
|
+
if (arrayJson instanceof STJson) {
|
|
352
|
+
arrayJson.setArrayElement(index, value);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Set a field within a nested array element
|
|
357
|
+
*/
|
|
358
|
+
setNestedArrayElementField(key, index, nestedKey, value) {
|
|
359
|
+
if (!this.isObject()) {
|
|
360
|
+
throw new Error('STJson::setNestedArrayElementField called on non-object');
|
|
361
|
+
}
|
|
362
|
+
this.validateDepth(value, 1);
|
|
363
|
+
const map = this.data;
|
|
364
|
+
let arrayJson = map.get(key);
|
|
365
|
+
if (!arrayJson || !(arrayJson instanceof STJson) || !arrayJson.isArray()) {
|
|
366
|
+
const newArray = new STJson(new Uint8Array());
|
|
367
|
+
newArray.data = [];
|
|
368
|
+
newArray.jsonType = STJson.JsonType.Array;
|
|
369
|
+
map.set(key, newArray);
|
|
370
|
+
arrayJson = newArray;
|
|
371
|
+
}
|
|
372
|
+
if (arrayJson instanceof STJson) {
|
|
373
|
+
arrayJson.setArrayElementField(index, nestedKey, value);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Get a nested array element
|
|
378
|
+
*/
|
|
379
|
+
getNestedArrayElement(key, index) {
|
|
380
|
+
if (!this.isObject()) {
|
|
381
|
+
return undefined;
|
|
382
|
+
}
|
|
383
|
+
const arrayJson = this.data.get(key);
|
|
384
|
+
if (arrayJson instanceof STJson && arrayJson.isArray()) {
|
|
385
|
+
return arrayJson.getArrayElement(index);
|
|
386
|
+
}
|
|
387
|
+
return undefined;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Get a field within a nested array element
|
|
391
|
+
*/
|
|
392
|
+
getNestedArrayElementField(key, index, nestedKey) {
|
|
393
|
+
if (!this.isObject()) {
|
|
394
|
+
return undefined;
|
|
395
|
+
}
|
|
396
|
+
const arrayJson = this.data.get(key);
|
|
397
|
+
if (arrayJson instanceof STJson && arrayJson.isArray()) {
|
|
398
|
+
return arrayJson.getArrayElementField(index, nestedKey);
|
|
399
|
+
}
|
|
400
|
+
return undefined;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Serialize to binary
|
|
404
|
+
*/
|
|
405
|
+
add(s) {
|
|
406
|
+
const bytesList = new binary_serializer_1.BytesList();
|
|
407
|
+
const tmp = new binary_serializer_1.BinarySerializer(bytesList);
|
|
408
|
+
// Add type byte
|
|
409
|
+
tmp.put(new Uint8Array([this.jsonType]));
|
|
410
|
+
if (this.isArray()) {
|
|
411
|
+
const array = this.data;
|
|
412
|
+
for (const value of array) {
|
|
413
|
+
STJson.addVLValue(tmp, value);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
// isObject()
|
|
418
|
+
const map = this.data;
|
|
419
|
+
for (const [key, value] of map.entries()) {
|
|
420
|
+
STJson.addVLKey(tmp, key);
|
|
421
|
+
STJson.addVLValue(tmp, value);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
const innerBytes = bytesList.toBytes();
|
|
425
|
+
const lengthBytes = binary_serializer_1.BinarySerializer.encodeVariableLength(innerBytes.length);
|
|
426
|
+
s.put(lengthBytes);
|
|
427
|
+
s.put(innerBytes);
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Encode a key as VL
|
|
431
|
+
*/
|
|
432
|
+
static addVLKey(s, str) {
|
|
433
|
+
const keyBytes = new TextEncoder().encode(str);
|
|
434
|
+
const lengthBytes = binary_serializer_1.BinarySerializer.encodeVariableLength(keyBytes.length);
|
|
435
|
+
s.put(lengthBytes);
|
|
436
|
+
s.put(keyBytes);
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Encode a value as [SType marker][VL]
|
|
440
|
+
*/
|
|
441
|
+
static addVLValue(s, value) {
|
|
442
|
+
if (!value) {
|
|
443
|
+
s.put(binary_serializer_1.BinarySerializer.encodeVariableLength(0));
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
const bytesList = new binary_serializer_1.BytesList();
|
|
447
|
+
const tmp = new binary_serializer_1.BinarySerializer(bytesList);
|
|
448
|
+
tmp.put(new Uint8Array([value.getSType()]));
|
|
449
|
+
value.toBytesSink(bytesList);
|
|
450
|
+
const innerBytes = bytesList.toBytes();
|
|
451
|
+
const lengthBytes = binary_serializer_1.BinarySerializer.encodeVariableLength(innerBytes.length);
|
|
452
|
+
s.put(lengthBytes);
|
|
453
|
+
s.put(innerBytes);
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Convert to JSON representation
|
|
457
|
+
*/
|
|
458
|
+
toJSON() {
|
|
459
|
+
if (this.isArray()) {
|
|
460
|
+
const array = this.data;
|
|
461
|
+
return array.map((item) => (item ? item.toJSON() : null));
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
// isObject()
|
|
465
|
+
const map = this.data;
|
|
466
|
+
const result = {};
|
|
467
|
+
for (const [key, value] of map.entries()) {
|
|
468
|
+
result[key] = value ? value.toJSON() : null;
|
|
469
|
+
}
|
|
470
|
+
return result;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Compare with another STJson for equivalence
|
|
475
|
+
*/
|
|
476
|
+
isEquivalent(t) {
|
|
477
|
+
if (!(t instanceof STJson)) {
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
return (0, utils_1.bytesToHex)(this.bytes) === (0, utils_1.bytesToHex)(t.bytes);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Check if this is the default value
|
|
484
|
+
*/
|
|
485
|
+
isDefault() {
|
|
486
|
+
return this.default_;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Get blob representation
|
|
490
|
+
*/
|
|
491
|
+
toBlob() {
|
|
492
|
+
const bytesList = new binary_serializer_1.BytesList();
|
|
493
|
+
const s = new binary_serializer_1.BinarySerializer(bytesList);
|
|
494
|
+
this.add(s);
|
|
495
|
+
return bytesList.toBytes();
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Get the size (number of bytes in serialized form)
|
|
499
|
+
*/
|
|
500
|
+
size() {
|
|
501
|
+
const bytesList = new binary_serializer_1.BytesList();
|
|
502
|
+
const s = new binary_serializer_1.BinarySerializer(bytesList);
|
|
503
|
+
this.add(s);
|
|
504
|
+
return bytesList.getLength();
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Set the value from another STJson
|
|
508
|
+
*/
|
|
509
|
+
setValue(v) {
|
|
510
|
+
if (!(v instanceof STJson)) {
|
|
511
|
+
throw new Error('setValue: value must be STJson');
|
|
512
|
+
}
|
|
513
|
+
;
|
|
514
|
+
this.data = v.data;
|
|
515
|
+
this.jsonType = v.jsonType;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Get serialized type ID
|
|
519
|
+
*/
|
|
520
|
+
getSType() {
|
|
521
|
+
return serialized_type_1.SerializedTypeID.STI_JSON;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
exports.STJson = STJson;
|
|
525
|
+
STJson.JsonType = {
|
|
526
|
+
Object: 0x00,
|
|
527
|
+
Array: 0x01,
|
|
528
|
+
};
|
|
529
|
+
//# sourceMappingURL=json.js.map
|