|
| 1 | +// Adapted from https://github.com/hms-dbmi/vizarr/blob/5b0e3ea6fbb42d19d0e38e60e49bb73d1aca0693/src/utils.ts#L26 |
| 2 | +import type { Chunk, ObjectType } from "../metadata.js"; |
| 3 | +import { get_strides, json_decode_object } from "../util.js"; |
| 4 | + |
| 5 | +type EncoderConfig = { |
| 6 | + encoding?: "utf-8"; |
| 7 | + skipkeys?: boolean; |
| 8 | + ensure_ascii?: boolean; |
| 9 | + check_circular?: boolean; |
| 10 | + allow_nan?: boolean; |
| 11 | + sort_keys?: boolean; |
| 12 | + indent?: number; |
| 13 | + separators?: [string, string]; |
| 14 | +}; |
| 15 | +type DecoderConfig = { |
| 16 | + strict?: boolean; |
| 17 | +}; |
| 18 | + |
| 19 | +type JsonCodecConfig = EncoderConfig & DecoderConfig; |
| 20 | + |
| 21 | +// Reference: https://stackoverflow.com/a/21897413 |
| 22 | +function throw_on_nan_replacer(_key: string | number, value: any): any { |
| 23 | + if (value !== value) { |
| 24 | + throw new Error( |
| 25 | + "JsonCodec allow_nan is false but NaN was encountered during encoding.", |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + if (value === Infinity) { |
| 30 | + throw new Error( |
| 31 | + "JsonCodec allow_nan is false but Infinity was encountered during encoding.", |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + if (value === -Infinity) { |
| 36 | + throw new Error( |
| 37 | + "JsonCodec allow_nan is false but -Infinity was encountered during encoding.", |
| 38 | + ); |
| 39 | + } |
| 40 | + return value; |
| 41 | +} |
| 42 | + |
| 43 | +// Reference: https://gist.github.com/davidfurlong/463a83a33b70a3b6618e97ec9679e490 |
| 44 | +function sort_keys_replacer(_key: string | number, value: any): any { |
| 45 | + return value instanceof Object && !(value instanceof Array) |
| 46 | + ? Object.keys(value) |
| 47 | + .sort() |
| 48 | + .reduce((sorted: any, key: string | number) => { |
| 49 | + sorted[key] = value[key]; |
| 50 | + return sorted; |
| 51 | + }, {}) |
| 52 | + : value; |
| 53 | +} |
| 54 | + |
| 55 | +export class JsonCodec { |
| 56 | + kind = "array_to_bytes"; |
| 57 | + |
| 58 | + #encoder_config: EncoderConfig; |
| 59 | + #decoder_config: DecoderConfig; |
| 60 | + |
| 61 | + constructor( |
| 62 | + public configuration: JsonCodecConfig, |
| 63 | + ) { |
| 64 | + // Reference: https://github.com/zarr-developers/numcodecs/blob/0878717a3613d91a453fe3d3716aa9c67c023a8b/numcodecs/json.py#L36 |
| 65 | + const { |
| 66 | + encoding = "utf-8", |
| 67 | + skipkeys = false, |
| 68 | + ensure_ascii = true, |
| 69 | + check_circular = true, |
| 70 | + allow_nan = true, |
| 71 | + sort_keys = true, |
| 72 | + indent, |
| 73 | + strict = true, |
| 74 | + } = configuration; |
| 75 | + |
| 76 | + let separators = configuration.separators; |
| 77 | + if (!separators) { |
| 78 | + // ensure separators are explicitly specified, and consistent behaviour across |
| 79 | + // Python versions, and most compact representation if indent is None |
| 80 | + if (!indent) { |
| 81 | + separators = [",", ":"]; |
| 82 | + } else { |
| 83 | + separators = [", ", ": "]; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + this.#encoder_config = { |
| 88 | + encoding, |
| 89 | + skipkeys, |
| 90 | + ensure_ascii, |
| 91 | + check_circular, |
| 92 | + allow_nan, |
| 93 | + indent, |
| 94 | + separators, |
| 95 | + sort_keys, |
| 96 | + }; |
| 97 | + this.#decoder_config = { strict }; |
| 98 | + } |
| 99 | + static fromConfig( |
| 100 | + configuration: JsonCodecConfig, |
| 101 | + ) { |
| 102 | + return new JsonCodec(configuration); |
| 103 | + } |
| 104 | + |
| 105 | + encode(buf: Chunk<ObjectType>): Uint8Array { |
| 106 | + const { |
| 107 | + indent, |
| 108 | + encoding, |
| 109 | + ensure_ascii, |
| 110 | + check_circular, |
| 111 | + allow_nan, |
| 112 | + sort_keys, |
| 113 | + } = this.#encoder_config; |
| 114 | + if (encoding !== "utf-8") { |
| 115 | + throw new Error("JsonCodec does not yet support non-utf-8 encoding."); |
| 116 | + } |
| 117 | + const replacer_functions: Function[] = []; |
| 118 | + if (!check_circular) { |
| 119 | + // By default, for JSON.stringify, |
| 120 | + // a TypeError will be thrown if one attempts to encode an object with circular references |
| 121 | + throw new Error( |
| 122 | + "JsonCodec does not yet support skipping the check for circular references during encoding.", |
| 123 | + ); |
| 124 | + } |
| 125 | + if (!allow_nan) { |
| 126 | + // Throw if NaN/Infinity/-Infinity are encountered during encoding. |
| 127 | + replacer_functions.push(throw_on_nan_replacer); |
| 128 | + } |
| 129 | + if (sort_keys) { |
| 130 | + // We can ensure keys are sorted but not really the opposite since |
| 131 | + // there is no guarantee of key ordering in JS. |
| 132 | + replacer_functions.push(sort_keys_replacer); |
| 133 | + } |
| 134 | + |
| 135 | + const items = Array.from(buf.data); |
| 136 | + items.push("|O"); |
| 137 | + items.push(buf.shape); |
| 138 | + |
| 139 | + let replacer = undefined; |
| 140 | + if (replacer_functions.length) { |
| 141 | + replacer = function (key: string | number, value: any): any { |
| 142 | + let new_value = value; |
| 143 | + replacer_functions.forEach((sub_replacer) => { |
| 144 | + new_value = sub_replacer(key, new_value); |
| 145 | + }); |
| 146 | + return new_value; |
| 147 | + }; |
| 148 | + } |
| 149 | + let json_str = JSON.stringify(items, replacer, indent); |
| 150 | + |
| 151 | + if (ensure_ascii) { |
| 152 | + // If ensure_ascii is true (the default), the output is guaranteed |
| 153 | + // to have all incoming non-ASCII characters escaped. |
| 154 | + // If ensure_ascii is false, these characters will be output as-is. |
| 155 | + // Reference: https://stackoverflow.com/a/31652607 |
| 156 | + json_str = json_str.replace(/[\u007F-\uFFFF]/g, function (chr) { |
| 157 | + const full_str = "0000" + chr.charCodeAt(0).toString(16); |
| 158 | + const sub_str = full_str.substring(full_str.length - 4); |
| 159 | + return "\\u" + sub_str; |
| 160 | + }); |
| 161 | + } |
| 162 | + return new TextEncoder().encode(json_str); |
| 163 | + } |
| 164 | + |
| 165 | + decode(bytes: Uint8Array): Chunk<ObjectType> { |
| 166 | + const { strict } = this.#decoder_config; |
| 167 | + if (!strict) { |
| 168 | + // (i.e., allowing control characters inside strings) |
| 169 | + throw new Error( |
| 170 | + "JsonCodec does not yet support non-strict decoding.", |
| 171 | + ); |
| 172 | + } |
| 173 | + const items = json_decode_object(bytes); |
| 174 | + const shape = items.pop(); |
| 175 | + items.pop(); // Pop off dtype (unused) |
| 176 | + if (!shape) { |
| 177 | + // O-d case |
| 178 | + throw new Error("0D not implemented for JsonCodec."); |
| 179 | + } else { |
| 180 | + const stride = get_strides(shape, "C"); |
| 181 | + const data = items; |
| 182 | + return { data, shape, stride }; |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments