stringify

Serialization of RegExp

假装没事ソ 提交于 2019-11-27 23:27:10
So, I was interested to find that JSON.stringify reduces a RegExp to an empty object-literal ( fiddle ): JSON.stringify(/^[0-9]+$/) // "{}" Is this behavior expected? I realize that a RegExp is an object with no properties to serialize. That said, dates are objects too; yet JSON.stringify() manages to produce a meaningful string: JSON.stringify(new Date) // "2014-07-03T13:42:47.905Z" I would have hoped that JSON would give RegExp the same consideration by using RegExp.prototype. toString() . Yes, because there's no canonical representation for a RegExp object in JSON. Thus, it's just an empty

JSON stringify ES6 class property with getter/setter

↘锁芯ラ 提交于 2019-11-27 16:00:50
I have a JavaScript ES6 class that has a property set with set and accessed with get functions. It is also a constructor parameter so the class can be instantiated with said property. class MyClass { constructor(property) { this.property = property } set property(prop) { // Some validation etc. this._property = prop } get property() { return this._property } } I use _property to escape the JS gotcha of using get/set that results in an infinite loop if I set directly to property . Now I need to stringify an instance of MyClass to send it with a HTTP request. The stringified JSON is an object

JSON.stringify output to div in pretty print way

半腔热情 提交于 2019-11-27 10:38:02
I JSON.stringify a json object by result = JSON.stringify(message, my_json, 2) The 2 in the argument above is supposed to pretty print the result. It does this if i do something like alert(result) . However, I want to output this to the user by appending it inside a div. When I do this I get just a single line showing up. (I don't think it is working because the breaks and spaces are not being interpreted as html?) { "data": { "x": "1", "y": "1", "url": "http://url.com" }, "event": "start", "show": 1, "id": 50 } Is there a way to output the result of JSON.stringify to a div in a pretty print

JSON.stringify converting Infinity to null

走远了吗. 提交于 2019-11-27 09:04:10
I have JavaScript Object say: var a = {b: Infinity, c: 10}; When I do var b = JSON.stringify(a); it returns the following b = "{"b":null, "c":10}"; How is the JSON.stringify converts the object to strings? I tried MDN Solution . function censor(key, value) { if (value == Infinity) { return "Infinity"; } return value; } var b = JSON.stringify(a, censor); But in this case I have to return the string "Infinity" not Infinity . If I return Infinity it again converts Infinity to null. How do I solve this problem. Like the other answers stated, Infintity is not part of the values JSON can store as

Javascript: stringify object (including members of type function)

三世轮回 提交于 2019-11-27 08:03:24
I'm looking for a solution to serialize (and unserialize) Javascript objects to a string across browsers, including members of the object that happen to be functions. A typical object will look like this: { color: 'red', doSomething: function (arg) { alert('Do someting called with ' + arg); } } doSomething() will only contain local variables (no need to also serialize the calling context!). JSON.stringify() will ignore the 'doSomething' member because it's a function. I known the toSource() method will do what I want but it's FF specific. You can use JSON.stringify with a replacer like: JSON

convert invalid JSON string to JSON

馋奶兔 提交于 2019-11-26 21:54:12
问题 I have an invalid json string like following, "{one: 'one', two: 'two'}" I tried to use JSON.parse to convert it to an object. however, this is not valid json string. Is there any functions can convert this invalid format into a valid json string or directly convert into an object? 回答1: IF your example syntax is the same as your real JSON, JSONLint says you need double quote for the name AND the value. In this case only, use these replace calls: var jsontemp = yourjson.replace((/([\w]+)(:)/g)

Proper Way to Convert JSON Date to .NET DateTime During Deserialization

谁说我不能喝 提交于 2019-11-26 20:57:51
问题 I have a javascript function that calls an MVC controller with JSON data: var specsAsJson = JSON.stringify(specs); $.post('/Home/Save', { jsonData: specsAsJson }); On the server side, within the controller, I can't seem to get past this error: /Date(1347992529530)/ is not a valid value for DateTime. That exception happens when I call Deserialize() (third line in method below): public ActionResult Save(string jsonData) { var serializer = new JavaScriptSerializer(); serializer

How to stringify event object?

一笑奈何 提交于 2019-11-26 17:43:17
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 ? fresskoma 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

JSON stringify ES6 class property with getter/setter

萝らか妹 提交于 2019-11-26 17:22:56
问题 I have a JavaScript ES6 class that has a property set with set and accessed with get functions. It is also a constructor parameter so the class can be instantiated with said property. class MyClass { constructor(property) { this.property = property } set property(prop) { // Some validation etc. this._property = prop } get property() { return this._property } } I use _property to escape the JS gotcha of using get/set that results in an infinite loop if I set directly to property . Now I need

JSON.stringify converting Infinity to null

我怕爱的太早我们不能终老 提交于 2019-11-26 14:28:32
问题 I have JavaScript Object say: var a = {b: Infinity, c: 10}; When I do var b = JSON.stringify(a); it returns the following b = "{"b":null, "c":10}"; How is the JSON.stringify converts the object to strings? I tried MDN Solution. function censor(key, value) { if (value == Infinity) { return "Infinity"; } return value; } var b = JSON.stringify(a, censor); But in this case I have to return the string "Infinity" not Infinity . If I return Infinity it again converts Infinity to null. How do I solve