Coder Social home page Coder Social logo

Comments (2)

mulias avatar mulias commented on June 17, 2024 1

I'm still chewing this over, but here are some initial thoughts

I think the best way to think about the scope of this project is that decoders take in values of the type returned by JSON.parse and validate to more specific types. Unfortunately the return type of JSON.parse is any. It would be nice if we instead got a more accurate JsonValue type to reflect that functions etc are not included. Maybe it would have been better to support any JS value, but I don't think I'm going to go back on that at this point. Different goals, different project.

So while it's true that the json spec requires object fields to be strings, when we parse a json encoded object into javascript the resulting JS object can have both string and number keys. So objects with number keys are part of the JsonValue type.

Maybe relevant, here's how typescript handles string vs number records:

const c: Record<string, string> = {1: "a"}
c["1"] // just fine
c[1] // also fine

vs

const c: Record<number, string> = {1: "a"}
c["1"] // error
c[1] // ok

@laurence-bird have you experimented with finding a way to get the behavior you want with the existing decoders? I ask because I'm not 100% sure if it is or is not possible to create a Decoder<Record<number, A>> already. That would be my first step before evaluating the best way to add a new feature.

Thanks for the question. I'd look over a PR, but at this point I'm not sure exactly what the API would look like, so I'm not yet confident if this is a feature we can or want to support.

from json-type-validation.

geraldalewis avatar geraldalewis commented on June 17, 2024

@mulias there's an implied expectation that input is valid JSON ("json-type-validation"), but it doesn't technically have to be, right? I can't remember if you explicitly made a determination on that.

Otherwise, JSON object fields must be strings:

member
    ws string ws ':' element

from json-type-validation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.