Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Array named properties are not included in json string #62

Closed
Krinkle opened this issue May 23, 2014 · 3 comments
Closed

Array named properties are not included in json string #62

Krinkle opened this issue May 23, 2014 · 3 comments

Comments

@Krinkle
Copy link
Owner

Krinkle commented May 23, 2014

From Huixian.Cui on August 03, 2012 07:43:50

I changed the code:

if (Object.prototype.toString.call(o[k]) === '[object Array]') {
    var arrayItem = [];

    for (var i = 0; i < o[k].length; i++) {
       arrayItem.push($.toJSON(o[k][i]));
    }

    val = '[' + arrayItem.join(',') + ']';
    pairs.push(name + ':' + val);
}
else {
    val = $.toJSON(o[k]);
    pairs.push(name + ':' + val);
}

Original issue: http://code.google.com/p/jquery-json/issues/detail?id=62

@Krinkle
Copy link
Owner Author

Krinkle commented May 23, 2014

From krinklemail on November 13, 2012 17:41:23

Please provide a test case for the test suite (which would fail against the current code).

@Krinkle
Copy link
Owner Author

Krinkle commented May 23, 2014

From dzlygin on May 19, 2013 05:34:04

var conf = new Array();
conf['key1'] = {text: 'sometext', posx: 50, posy: 50};
conf['key2'] = {text: 'othertext', posx: 50, posy: 80};

var a = $.toJSON(conf);

this code fails

@Krinkle Krinkle changed the title if javascript object has sub-Array, then json string is wrong. If object has sub-Array, then json string is wrong May 26, 2014
@Krinkle Krinkle added the invalid label Jul 3, 2014
@Krinkle
Copy link
Owner Author

Krinkle commented Jul 3, 2014

This is not a valid bug for the native JSON.stringify method exhibits the same behaviour and we're just polyfilling that. We can't take feature requests.

Per the ES5 specification, when serialising an Array object:

https://es5.github.io/#x15.12.3

For each value a property [that] has an array index property name. The properties are enumerated in the ascending array index order of their names.

https://es5.github.io/#JA

The representation of arrays includes only the elements between zero and array.length – 1 inclusive. Named properties are excluded from the stringification.

I recommend you do not be add named properties to array objects in your program. You're probably confusing arrays with objects (other programming languages sometimes call them arrays).

var conf = {};
conf.key1 = { text: 'sometext', posx: 50, posy: 50 };
conf.key2 = { text: 'othertext', posx: 50, posy: 80 };

var a = $.toJSON(conf);
// {"key1":{"text":"sometext","posx":50,"posy":50},"key2":{"text":"othertext","posx":50,"posy":80}}

@Krinkle Krinkle closed this as completed Jul 3, 2014
@Krinkle Krinkle changed the title If object has sub-Array, then json string is wrong Object properties if an Array are not included in json string Jul 3, 2014
@Krinkle Krinkle changed the title Object properties if an Array are not included in json string Array named properties are not included in json string Jul 3, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant