Skip to content

Instantly share code, notes, and snippets.

@goldsail
Created March 26, 2023 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldsail/37a48b6ee4fb2bf363dac0012fa0263b to your computer and use it in GitHub Desktop.
Save goldsail/37a48b6ee4fb2bf363dac0012fa0263b to your computer and use it in GitHub Desktop.
IonStruct object does not match instanceof dom.Value when used inside JSON stringify replacer
const { load, dom } = require('ion-js');
const ionStruct = load(`$ion_1_0
{
foo:"bar"
}`);
const ionList = load(`$ion_1_0
[
"hello",
"world"
]`);
console.log(ionStruct instanceof dom.Value); // returns true
console.log(ionStruct['foo'] instanceof dom.Value); // returns true
console.log(ionList instanceof dom.Value); // returns true
console.log(ionList[0] instanceof dom.Value); // returns true
console.log(ionList[1] instanceof dom.Value); // returns true
// So far, it all looks good.
const replacer = (key, value) => {
if (value instanceof dom.Value) {
console.log('instance of dom.Value:', value);
} else {
console.log('not instance of dom.Value:', value, 'Actual type:', typeof value);
}
return value;
}
const jsonObject = {
ionStruct: ionStruct,
ionList: ionList,
}
JSON.stringify(jsonObject, replacer, 2);
// Output:
`
true
true
true
true
true
not instance of dom.Value: {
ionStruct: Struct { _fields: [Object: null prototype] { foo: [Array] } },
ionList: List(2) [ [String: 'hello'], [String: 'world'] ]
} Actual type: object
not instance of dom.Value: [Object: null prototype] { foo: [String: 'bar'] } Actual type: object
instance of dom.Value: [String: 'bar']
instance of dom.Value: List(2) [ [String: 'hello'], [String: 'world'] ]
instance of dom.Value: [String: 'hello']
instance of dom.Value: [String: 'world']
`
@goldsail
Copy link
Author

Line 50 expected (because line 41 returns true):

instance of dom.Value: ...

Line 50 actual:

not instance of dom.Value: ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment