Skip to content

Parsing json #35

Closed
Closed

Description

@Isaac-Leonard

JSON is pretty much key to javascript and typescript and without proper handling of it BorrowScript would be rather limited.
Parsing json in a type safe way in a statically compiled language almost seems impossible to me without a lot of boilerplate code.
Would it be possible to include something like Zod in the STL to allow for type safe parsing of external JSON while keeping type safety?
https://github.com/colinhacks/zod

Activity

  1. mindplay-dk commented on Oct 24, 2021

    @mindplay-dk

    I don't know Zod, but V does a nice job of decoding JSON with static type-safety.

    This is implemented at the language level, which is not something I'm wild about - I think a language should be powerful enough that something like JSON decoding can actually be implemented in the language itself, because that's just one use-case, and what happens when somebody wants XML or protocol buffers or any other binary/text format? These either become second rate citizens, or the language ends up taking on far too many responsibilities that could have been solved in userland.

    Also, the limitation of this approach is you can't actually work with JSON without defining types - there are probably use-cases where you want to work with unstructured data and unknown JSON types, such as a log, message bus, document database, etc. (It's likely you would need another kind of API for working with untyped JSON documents.)

    So this issue probably opens a longer debate about type-hints and compile-time capabilities - V has some nice ideas in this territory as well, mainly compile-time reflection, which I suppose is really a kind of hygienic macros. Something like that might work for a JSON parser/encoder, since that's based on type reflection.

  2. Isaac-Leonard commented on Oct 24, 2021

    @Isaac-Leonard
    ContributorAuthor

    I definitely think that this shouldn't be implemented within the language specs itself but a feature of the STL.
    What zod does looks pretty similar to the V documentation you linked although it uses chained methods such as:

    z.object({
    str: z.string().optional(),
      reccord:z.reccord( z.number()) 
    }).parse( someObj);

    While Zod doesn't directly parse json, rather it validates an object conforms to a certain shape, its regularly used for validating json and would make for a good api for parsing json in borrowScript in my opinion.
    Its got full support for dynamic json values with the record and map types so parsing unknown json shouldn't be a major possible assuming similar techniques are transferable into a compiled language.
    Given that json is central to javascript and typescript I'd argue that providing out of the box support for parsing and validating it would be helpful for onboarding new developers to the language and the more features in the STL the better.

  3. alshdavid commented on Nov 11, 2021

    @alshdavid
    Collaborator

    This would be a standard library feature. We may not have structural types so explicit typing and casting will be required.

    import json from '@std/json'
    
    type TargetJson = {
      hello: string
    }
    
    function main() {
      const target = '{ "hello": "world" }'
      const parsed = json.fromString<TargetJson>(target)
    }
  4. locked and limited conversation to collaborators on Feb 17, 2023
  5. converted this issue into a discussion #69 on Feb 17, 2023
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions