You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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:
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.
Activity
mindplay-dk commentedon Oct 24, 2021
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.
Isaac-Leonard commentedon Oct 24, 2021
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:
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.
alshdavid commentedon Nov 11, 2021
This would be a standard library feature. We may not have structural types so explicit typing and casting will be required.