Parsing json #69
Replies: 3 comments
-
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. |
Beta Was this translation helpful? Give feedback.
-
I definitely think that this shouldn't be implemented within the language specs itself but a feature of the STL. 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. |
Beta Was this translation helpful? Give feedback.
-
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)
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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
Beta Was this translation helpful? Give feedback.
All reactions