NNKL.COM
welcome to my space
X
Search:  
 HOME   JSON encode bug
JSON encode bug
Published by: cfz 2009-01-09
Welcome to:nnkl.com

  • Trying to encode an object which has nested object + array structures, I came across a condition where sometimes an array type is encoded like an object type.

    [Note: Happens only sometimes.. couldn't exactly find out when.. might be something related to my system settings
    - Windows xp sp2
    - FireFox 2.0.0.1]

    Bug description:

    Occassionally the line of code inside "encode" method of YAHOO JSON namespace
    that detects whether an object is an Array or not .. misses to detect that its indeed an array.

    Correction that resolved the issue.

    1. Add an additional arrayChecking function in the "encode" method as below

    else if(o instanceof Array adl_ArrObjChk(o)){

    2. Additional check implementation

    function adl_ArrObjChk(obj)
    {
    if (obj.length)
    {
    try{
    tmpTest = obj.join(',');
    return true;
    }
    catch(tmpErr)
    {
    return false;
    }
    }

    return false;
    }


  • Douglas Crockford (creator of JSON) has it's own isArray function:

    ok. but i didnt find such a method inside YAHOO.JSON... does YAHOO.JSON use the same code as crockford's JSON... I feel YAHOO.JSON was improved and better than the original because crockford's json convertor didn't convert all of my nested object structure...it just kind of ignored some inner structures.

    Can you post an example? Are you sure this isn't a case of a mis-declared empty array, for example

    no. its not a mis-declared empty array. i verified it debugging inside the encode() method, trying its length and a few other tests. also, instead of making a string like [ {...}, {...} ], it made { 0: {...}, 1: {...} } for the array when it misinterpreted it as an object.

    I will see how to post a sample.. since its a large object thats being converted.. and only during the conversion i find the problem.. the object structure in itself is the same.
    AKBK home - Smoking toooooo much PHP - JSON based calendar ::
    Where people can submit bug's by signing up to a calendar/todo list and just add it as a task. . $json = new Services_JSON(); echo $json->encode( $ret);
    http://www.akbkhome.com/blog.php/View/174/JSON_based_calendar_provider_for_thunderbird_and_lightning.html
    HOME
    i think i will have to post one proper, and one misconverted object string...will that help compare.


  • Might have use the test


    if (obj.constructor.name == "Array")


    to see if it's an Array.


  • If that is your return value, then it's not an array. Check your declaration and be certain.

    I don't know what crockford is doing, unless he is trying to support "Array-like" structures, or people who have extended Array.

    The check looks like this:

    o instanceof Array

    and it comes right after the null check - so there's no way that can fail to detect an array. :)


  • I found a thread in another forum that addresses when instanceOf Array can fail..


    at: http://www.thescripts.com/forum/post305236-2.html

    When you say 'Array', you are talking about 'window.Array'. 'window' is the
    browser's context object, and you get one per page (or frame). All of the arrays
    created within a context will have their constructor property set to
    'window.Array'.

    An array created in a different context has a different window.Array, so your
    test

    myArray instanceof Array

    fails.

    I think that's exactly my problem. I have an iframe window inside my main window, through which the parent array object's value is set (sometimes set from the parent window itself.. that explains why it happened sometimes)

    Interesting.. more interesting was that the thread message's author was crockford himself :)


  • Can you post an example? Are you sure this isn't a case of a mis-declared empty array, for example

    {myArr:null} // wrong
    {myArr:} // correct


  • Douglas Crockford (creator of JSON) has it's own isArray function:

    function isArray(v) {
    return v && typeof v === 'object' && typeof v.length === 'number' &&
    !(v.propertyIsEnumerable('length'));
    }

    (I assume he knows best...)





  • I Am a Sinner – What About You?
    Global Sourcing and Supplier Online by Dylan

    You are looking at:nnkl.com's JSON encode bug, click nnkl.com to home
  • the importance of team motivation
  • tea types brewing instructions and other info
  • live richly on fisher island
  • how to lose weight fast 5 quick and easy tips
  • high blood pressure dieting avoid medication with hypertension natural alternative
  • the importance of google adsense
  • the lazy mans way to weight loss
  • best spyware removers what you need to know to keep your computer safe
  • the pros and cons of locally hosted automatic responders
  • women diet should include 4 6 meals a day
  • find a wise investment in the horse racing system
  • can anyone make money from home online
  • getting help with your next email marketing campaign
  •  
  • calculating life insurance where to live if you want to die old
  • heres how to advertise and not spam
  • view blocked sites with random ip proxy
  • a few email marketing strategies
  • finding current research on congestive heart failure
  • gulf shores condos
  • downtown l a evolves with evo
  • cutting dating expenses
  • top 5 questions to ask when hiring wedding music
  • effective way to evaluate your email marketing
  • the sensible use of perfume
  • powerful science of kundalini yoga explained
  • bring home the dome
  • a few suggestions for email marketing with e newsletters
  •  Homepage | Add to favorites | Contact us | Exchange links | LOGIN | Site map | 
    Copyright© 2008 nnkl.com        Site made:CFZ