mirror of
https://github.com/gchq/CyberChef
synced 2025-03-14 05:46:56 +00:00
dish: properly serialize Javascript Maps as JSON
When decoding CBOR it may happen that a Map is returned instead of an object when the keys are not all strings. Encode such Maps as array of key-value-pairs.
This commit is contained in:
parent
ae1b12c120
commit
b9abc1886f
1 changed files with 12 additions and 1 deletions
|
@ -7,6 +7,17 @@
|
|||
import DishType from "./DishType.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
|
||||
/**
|
||||
* Serialize Maps which are not natively compatible with JSON as an array of
|
||||
* key-value-paris.
|
||||
*/
|
||||
function jsonStringifyReplacer(k, v) {
|
||||
if (v instanceof Map) {
|
||||
return [...v];
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translation methods for JSON dishes
|
||||
*/
|
||||
|
@ -17,7 +28,7 @@ class DishJSON extends DishType {
|
|||
*/
|
||||
static toArrayBuffer() {
|
||||
DishJSON.checkForValue(this.value);
|
||||
this.value = this.value !== undefined ? Utils.strToArrayBuffer(JSON.stringify(this.value, null, 4)) : new ArrayBuffer;
|
||||
this.value = this.value !== undefined ? Utils.strToArrayBuffer(JSON.stringify(this.value, jsonStringifyReplacer, 4)) : new ArrayBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue