[closed] encodeArray with isArray

March 13th, 2010
  • I don't know that this is necessarily a bug, but I'll post here just in case it's worthy of thought.

    Further info here (http://extjs.com/forum/showthread.php?t=32252): http://extjs.com/forum/showthread.php?t=32252

    I was going to just go silent into the night thinking that the problem was all on me, but after further reflection I thought maybe the ext code could be tweaked.

    My thought is this:

    While encoding to json, ext classifies my 'array-like' object as an array. But then when it comes time to encodeArray, the length test fails and the length of my (array-like) object is zero, as a result the zero length 'array' (as ext classified it) will encode to an empty array "".

    Seems like there's a few plausible modifications that would enable it to still work for example:

    if (isArray(o) && o.length) {
    //encode it like an array
    }

    ...

    else {
    //if other classifications fail and it smells like an object
    //encode it like an object
    }



    this.encode = function(o){
    if(typeof o == "undefined" o === null){
    return "null";
    }else if(Ext.isArray(o)){
    return encodeArray(o);
    }else if(Ext.isDate(o)){
    return encodeDate(o);
    }else if(typeof o == "string"){
    return encodeString(o);
    }else if(typeof o == "number"){
    return isFinite(o) ? String(o) : "null";
    }else if(typeof o == "boolean"){
    return String(o);
    }else {//should end up here not in encodeArray if using an 'array-like' object
    var a = ["{"], b, i, v;
    for (i in o) {
    if(!useHasOwn o.hasOwnProperty(i)) {
    v = o[i];
    switch (typeof v) {
    case "undefined":
    case "function":
    case "unknown":
    break;
    default:
    if(b){
    a.push(',');
    }
    a.push(this.encode(i), ":",
    v === null ? "null" : this.encode(v));
    b = true;
    }
    }
    }
    a.push("}");
    return a.join("");
    }
    };

    var encodeArray = function(o){
    var a = ["["], b, i, l = o.length, v;
    for (i = 0; i < l; i += 1) {
    v = o[i];
    switch (typeof v) {
    case "undefined":
    case "function":
    case "unknown":
    break;
    default:
    if (b) {
    a.push(',');
    }
    a.push(v === null ? "null" : Ext.util.JSON.encode(v));
    b = true;
    }
    }
    a.push("]");
    return a.join("");
    };

    isArray: function(v){
    return v&&typeof v.pop=="function" &&v.length//maybe test for length property?}


  • A zero length array is still a valid array and in many places we would still need to recognize it as an array.

    If you have an array-like clone that you would like to have encoded as an object, you may wish to add some kind of flag to it's prototype (e.g. preventArray: true) and then override the isArray function to check for your flag.


  • Yeah, I see what you mean. I thought there might be some other additional check to see if number of properties or something were zero.

    I see that this:
    var a;
    a =
    a['something']=3;
    a['another']=5
    a.length;//0 instead of undefined
    I thought that last line might evaluate to undefined. So like you say should seek other means.







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about [closed] encodeArray with isArray , Please add it free.