Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stringify event object?

JSON.stringify(eventObject);

gives:

TypeError: Converting circular structure to JSON


dojox.json.ref.toJson(eventObject);

gives:

TypeError: Accessing selectionEnd on an input element that cannot have a selection.


Is there some library/code ready to use to accomplish it ?

like image 408
Tar Avatar asked Jul 18 '12 18:07

Tar


People also ask

Can you Stringify an object?

The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Can you Stringify array of objects?

The JSON. stringify method converts a JavaScript object or value to a JSON string. It can optionally modify or filter values if a replacer function/array is specified.

Does JSON Stringify change the object?

JSON. stringify() takes a JavaScript object and then transforms it into a JSON string. JSON. parse() takes a JSON string and then transforms it into a JavaScript object.

Can JSON Stringify a function?

The JSON. stringify() function, as name suggests, converts a JavaScript value to a serialized JSON string. JSON. stringify() can optionally use a replacer function to replace values using custom logic.


2 Answers

You won't be able to serialize an event object with JSON.stringify, because an event object contains references to DOM nodes, and the DOM has circular references all over the place (e.g. child/parent relationships). JSON can't handle these by default, so you're a bit out of luck there.

I'd suggest to look at How to serialize DOM node to JSON even if there are circular references? which has a few suggestions on how to serialize a DOM node. Also, the following questions seem to have useful information:

  • How to save an object with circular references?
  • Stringify (convert to JSON) a JavaScript object with circular reference

JSON libraries able to handle circular references seem to be

  • JSON-js (see cycle.js)
  • dojox.json.ref

Alternatively, you could delete all references to DOM nodes if you don't need them, and then serialize the object. You shouldn't do this after all. See @PointedEars comment :)

like image 50
fresskoma Avatar answered Sep 25 '22 02:09

fresskoma


Use the "replacer" function to avoid errors:

JSON.stringify(evt, function(k, v) {     if (v instanceof Node) {         return 'Node';     }     if (v instanceof Window) {         return 'Window';     }     return v; }, ' '); 

Update 2019: the browser API has changed some way, here is a method to expose all available keys in Event prototype chain

function stringifyEvent(e) {   const obj = {};   for (let k in e) {     obj[k] = e[k];   }   return JSON.stringify(obj, (k, v) => {     if (v instanceof Node) return 'Node';     if (v instanceof Window) return 'Window';     return v;   }, ' '); } 
like image 33
Alexander Shutau Avatar answered Sep 23 '22 02:09

Alexander Shutau