-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathClob.ts
45 lines (42 loc) · 1.38 KB
/
Clob.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { IonTypes, Writer } from "../Ion";
import { Lob } from "./Lob";
/**
* Represents a clob[1] value in an Ion stream.
*
* [1] https://amazon-ion.github.io/ion-docs/docs/spec.html#clob
*/
export class Clob extends Lob(IonTypes.CLOB) {
/**
* Constructor.
* @param bytes Raw, unsigned bytes to represent as a clob.
* @param annotations An optional array of strings to associate with `bytes`.
*/
constructor(bytes: Uint8Array, annotations: string[] = []) {
super(bytes, annotations);
}
writeTo(writer: Writer): void {
writer.setAnnotations(this.getAnnotations());
writer.writeClob(this);
}
toJSON() {
// Because the text encoding of the bytes stored in this Clob is unknown,
// we write each byte out as a Unicode escape (e.g. 127 -> 0x7f -> \u007f)
// unless it happens to fall within the ASCII range.
// See the Ion cookbook's guide to down-converting to JSON for details:
// https://amazon-ion.github.io/ion-docs/guides/cookbook.html#down-converting-to-json
let encodedText = "";
for (const byte of this) {
if (byte >= 32 && byte <= 126) {
encodedText += String.fromCharCode(byte);
continue;
}
const hex = byte.toString(16);
if (hex.length == 1) {
encodedText += "\\u000" + hex;
} else {
encodedText += "\\u00" + hex;
}
}
return encodedText;
}
}