2

I need it because I recently made an app that saves an object containing all the user-generated data to localStorage, and encodes/decodes it with JSON.

The bizarre thing is that for some reason, Internet Explorer has poor, if not zero, support for JSON ("JSON is not defined"), and I'm not up to trying to re-create the entire function.

stringify:function(x){
    y='{'
    for(i in x){
        reg=RegExp('\'','g')
        y+=',\''+i.replace(reg,'\\\'')+'\':\''+x[i].replace(reg,'\\\'')+'\''
    }
    y=y.replace(',','')
    y+='}'
    return y
}

This was my first attempt, but I had forgotten that the object has other objects inside it, which themselves contain objects, and kept getting an error which basically stemmed from trying to call the method String.prototype.replace() of an Object.

Since I was kinda OCD with my code at the time, I actually do have the structure of the object saved in the source code:

/*
Link Engine.data: Object: { 
    X: Object: {    [Each is a Paradigm, contains links]
        link.0:{
            link:[link],
            title:[title],
            removed:[true/false],
            starred:[true/false]
        },
        ...
    },
    LSPAR: [Reserved] Object: { [Paradigm list and pointer contained here]
        key:[key], (this controls X)
        list:{
            [listitem]:[listitem],
            ...
        }
    },
    #CONFIG: [Reserved] Object: { [contains miscellaneous Config data]
        property:boolean/number/string,
        ...
    }
*/

That's the basic data structure, ... represents a repeating pattern.


Edit 2019

This whole question is an abomination, but I want to at least attempt to fix the bothersome documentation I wrote for my poorly-designed data structure so that it's more coherent:

Link {
    string  link
    string  title
    boolean removed
    boolean starred
}

Config {
    ...

    /* Just has a bunch of arbitrary fields; not important */
}

WArray {
    string... [paradigm-name]

    /* Wasteful Array; an object of the form
     * { "a":"a", "b":"b", ... }
     */
}

Paradigm { /* analogous to above "X: Object: {..." nonsense */
    Link... [paradigm-name].[id]

    /* each key is of the form [paradigm-name].[id] and stores a Link
     * e.g. the first link in the "Example" paradigm would
     * be identified by the key "Example.0"
     */
}

ParadigmList {
    string key  /* name of selected paradigm */
    WArray list /* list of paradigm names */
}

LinkEngineData {
    Paradigm...   [paradigm-name]
    ParadigmList  LSPAR
    Config        #CONFIG /* actual field name */
}

Hopefully now you can sort of parse what's going on. This syntax:

type... format

is meant to convey that objects of type type appear many times, like an array, except it isn't an array. As such, the fields don't have a name that is set-in-stone, hence

format: [descriptor1]text[descriptor2]text...

a format is used in place of an actual field name. This is what happens when you try to create a data structure without knowing what a data structure is. I did use the words "data" and "structure" adjacently in the original question, but it was pure coincidence. I didn't mean it like "this is the data structure I used"; I meant it like "this is the structure of my data".

Anyways, here's how I would design it today:

Link {
    string  url
    string  title
    boolean starred
}

LinkGroup {
    string name
    Link[] links
}

Config {
    ... /* has whatever it needs to have */
}

Data {
    int         selGroup
    LinkGroup[] groups
    Config      config
}

That is all.


If someone has the sourcecode of the actual JSON.stringify function, or knows a way to replicate it, then please put your answer.


EDIT (2013, probably)

I ended up dropping IE support and completely redesigning the app from the ground up; the new version is hosted here. And it works with IE9 out of the box!

6
  • What version of IE are you using? Commented Oct 10, 2012 at 21:13
  • muirbot: I'm testing it on IE9 :/ Commented Oct 10, 2012 at 21:13
  • github.com/douglascrockford/JSON-js/blob/master/json2.js Commented Oct 10, 2012 at 21:14
  • user1689607: I already checked json.org, but they do not actually have the JSON.stringify() method. I also checked the new link you put, it was the exact place I checked :/ No actual function; each file assumes that JSON already exists Commented Oct 10, 2012 at 21:14
  • @B1KMusic: Sure they do. You need to scroll down to the bottom and check out the links in the JavaScript area. I updated my link above. Commented Oct 10, 2012 at 21:16

3 Answers 3

3

I think this is the best replacement: http://bestiejs.github.com/json3/

It claims to be better than Crockford's JSON 2 for the following reasons (from their site):

JSON 3...

  • Correctly serializes primitive wrapper objects (Issue #28).
  • Throws a TypeError when serializing cyclic structures (JSON 2 recurses until the call stack overflows).
  • Utilizes feature tests to detect broken or incomplete native JSON implementations (JSON 2 only checks for the presence of the native functions). The tests are only executed once at runtime, so there is no additional performance cost when parsing or serializing values.

In contrast to JSON 2, JSON 3 does not...

  • Add toJSON() methods to the Boolean, Number, and String prototypes. These are not part of any standard, and are made redundant by the design of the stringify() implementation.
  • Add toJSON() or toISOString() methods to Date.prototype. See the note about date serialization below.
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Trevor. I tested it in the app and it works; the data is being saved exactly as it should. I just need to figure out how to award best answer now :/
I figured out how to do the "best answer" and am awarding it to you for the following reasons: 1) You were the first to give a .js source, 2) It is a better implementation as it doesn't conflict with the various for..in loops throughout the code with prototypes.
3

Try https://github.com/douglascrockford/JSON-js

10 Comments

Unfortunately, the actual method JSON.stringify() does not exist in any of those files, as each one assumes it already exists via ECMA specifications.
@B1KMusic: Yes it does! Didn't you click the link I gave you below your question? It's below the code comment that reads // If the JSON object does not yet have a stringify method, give it one.
@B1KMusic json2.js contains both of them
Oh. Well unfortunately the source is not ideal, as I intended to implement the workaround in an ie if statement, adding a <script> tag containing a link to an actual js file, github puts html around it. Because Trevor gave a source that is .js and worked first try, I am awarding the best answer to him.
There's also the fact that when I did try to use the code from the only direct .js source, the prototypes spammed in there by the implementation conflicted with a lot of my code. No bueno :(
|
1

I think you should use the json2.js library: https://github.com/douglascrockford/JSON-js

1 Comment

There are two reasons why I didn't go with that: 1) I have never successfully gotten github to work for me; too much terminal BS to deal with for a single file, not to mention I don't like making accounts/downloading clients for sites I know I won't be using a lot. 2) That file contains prototypes, which would break my page since it makes extensive use of for..in loops to read through objects. I'll still vote you up as thanks for the answer, though. But I decided to drop IE support altogether.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.