Skip to content

Question: Should flatJSON produce strict JSON? #10

Description

@jtrumbull

Url aside, as it is already pre-validated, using flatJSON presently allows for invalid JSON (function, new String(), new RegExp(), new Date(), etc..)

I.e.

function someObj() {
    this.prop1= "a";
    this.prop2= "b";
    this.prop3= "c";
    this.abc = function(){
        return this.prop1 + this.prop2 + this.prop3;
    }
}

var data = [
    {
        "deep": new someObj(),
        "date": new Date(),
        "string": new String('str'),
        "null": null
    }
]

would produce

[
    {
        "deep.prop1": "a",
        "deep.prop2": "b",
        "deep.prop3": "c",
        "deep.abc": function( ){ },
        "date": Date(),
        "string": {
            0: "s",
            1: "t",
            2: "r"
        },
        "null": null
    }
]

because

if (Object(cur) !== cur) {
    // Primitives and null
} else if ($.isArray(cur)) {
    // Array
} else {
    // Object and everything else
}

switching to

if (typeof cur !== 'object') { 
    // Primitives
} else if (cur.constructor === Array){
    // Array
} else if (cur.constructor === Object){
    // Object
} 
// null and everything else is ignored.

would produce valid JSON. In addition, you gain efficiency because you know what constructors to type check.

Efficiency not being the driver, do you want this extension to allow for invalid JSON? I'm torn personalty.

Valid
Pro: Efficiency
Con: End user doesn't have option for invalid.
Not valid
Pro: Gives end user option for invalid.
Con: Extension name is misnomer.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions