-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] - JsonNode type #59
Comments
This is how the lead architect of TypeScript suggests defining it with a recursive type reference, supported in TS 3.7+: microsoft/TypeScript#33050 type Json = string | number | boolean | null | Json[] | { [key: string]: Json }; TS 3.7 was released in Nov 2019. Here is another solution he posted a few years back for earlier TS versions: microsoft/TypeScript#3496 (comment) type JSONValue = string | number | boolean | JSONObject | JSONArray;
interface JSONObject {
[x: string]: JSONValue;
}
interface JSONArray extends Array<JSONValue> { } |
Hello @ms1111 👋 Thanks for looking into this (I had no idea TBH) type Json = string | number | boolean | null | Json[] | { [key: string]: Json }; Is more along the lines of what I was thinking. Which now I am wondering about the type Json = string | number | boolean | null | Json[] | Record<string, Json>; |
type Json = string | number | boolean | null | Json[] | Record<string, Json>;
For some reason the What would you think about this: export type JsonObject = { [key: string]: JsonValue };
export type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject;
/** Functions used when hydrating data */
export type FromJsonStrategy<T> = (value: JsonObject) => T;
/** Functions used when dehydrating data */
export type ToJsonStrategy <T>= (value: T) => JsonObject; ... explicitly specifying the return type of ToJsonStrategy as JsonObject (an object) rather than just any JSON value. I think that's how it's always used, right? The top-level thing you serialize with ts_serialize has to be an object, not an array or a scalar? |
Yea, the expected usage is for authors to define how an object is serialized to JSON - this does make sense to me. Thanks! |
actually, sorry, this is my mistake, in this use case, we can expect an array or scalar value because we are not serializing the top level object here, this would be the description of how a specific property is serialized.
is correct, however, at this point, we are describing the properties of said object. |
Define explicit types for legal plain JSON values to avoid double-deserialization shenanigans. Fixes #59.
Define explicit types for legal plain JSON values to avoid double-deserialization shenanigans. Fixes #59.
Define explicit types for legal plain JSON values to avoid double-deserialization shenanigans. Fixes #59.
JsonNode type
add a type for
JsonNode
that consists of the correct types supported by the JSON Notation standardtext (see "3. Values")
Note: if updated please follow the
Obsoleted by:
tag in the header:Motivation
To properly type our strategic directions.
ToJsonStrategy
andFromJsonStrategy
make use ofany
, by providing this type we can set up directional strategies.Design Detail
The supported RFC types should be
|
(Unioned) into aJsonNode
typeBecomes
The text was updated successfully, but these errors were encountered: