mafintosh / turbo-json-parse

Turbocharged JSON.parse for type stable JSON data
MIT License
615 stars 31 forks source link

Make `compile.schema` lenient #8

Open aneophyte opened 6 years ago

aneophyte commented 6 years ago

It appears that compile.schema errors out with Cannot infer type when it finds null in any of the field. This renders the function somewhat unusable. Couldn't the function leave null as it is and return the rest of the schema instead?

mafintosh commented 6 years ago

Yea good idea. Wanna PR that change? The name of this function is now inferRawSchema btw

aichholzer commented 6 years ago

@aneophyte -I will implement this request/feature. Just to be clear on the requirement, can you please provide a sample input and the expected output?

aneophyte commented 6 years ago

The library has changed quite a bit since I last posted this issue. But the following is probably what I want:

Input:

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": null
    },
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
}}    

Output:

{ widget:
   { debug: 'string',
     window:
      { title: 'string',
        name: 'string',
        width: 'number',
        height: 'number' },
     image:
      { src: 'string',
        name: 'string',
        hOffset: 'number',
        vOffset: 'number',
        alignment: 'null' // <--- Echo this, instead of crashing },
     text:
      { data: 'string',
        size: 'number',
        style: 'string',
        name: 'string',
        hOffset: 'number',
        vOffset: 'number',
        alignment: 'string',
        onMouseUp: 'string' } } }

By the way, I have not had the time to check the newer version. I am currently using version 1.1.0.

~Edit: Ok, just checked. There is no longer a typeSchema generator. It was very useful for generating schema for existing APIs. But since toString() prints out the entire function, would it make sense to copy paste it as the source code...~

Whoops, just noticed mafintosh's response about inferRawSchema.

Got Error: Unexpected key in object when parsing. Steps:

  1. Generate schema using inferRawSchema for the object I posted above.
  2. Create a parser using the generated schema.
  3. Try parsing the same object.
aichholzer commented 6 years ago

Thank you @aneophyte -I will try to get a PR through ASAP. I am currently moving cities and need to pack an entire house, thus it might take me a little bit longer.

arj03 commented 6 years ago

With the latest version, null is supported. So I think this can be closed?