Skip to content

monitor-adapter.js -- implement JSON support #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/lib/monitor-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const isUndefined = a => typeof a === 'undefined';

const circularReplacer = () => {
const stack = new Set();
return function replacer(_, value) {
return function replacer(key, value) {
if (typeof value === "object" && value !== null) {
if (stack.has(value)) return "{...}";
if (stack.has(value)) return Array.isArray(value) ? "[...]" : "{...}";
stack.add(value);
}
return value;
};
}
};

/**
* Convert monitors from VM format to what the GUI needs to render.
Expand Down Expand Up @@ -48,9 +48,9 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
value = value.toString();
}

// Turn the value to a string, to handle Object values
// arrays will be confused for lists if we use 'typeof'
if (value.constructor.name === 'Object') {
// Turn the value to a string, to handle JSON values
// do not convert arrays as it will be confused for lists
if (typeof value === 'object' && !Array.isArray(value)) {
value = JSON.stringify(value, circularReplacer);
}

Expand All @@ -61,7 +61,7 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
const item = value[i];
if (typeof item === 'boolean') {
value[i] = item.toString();
} else if (value[i].constructor.name === 'Object') {
} else if (typeof value[i] === 'object' && !Array.isArray(value[i])) {
value[i] = JSON.stringify(item, circularReplacer);
}
}
Expand Down
Loading