-
Notifications
You must be signed in to change notification settings - Fork 473
Add serializeAnyToJson and unserializeAnyFromJsonUnsafe
#3058
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
Conversation
See docs for more info. This also retains debug information if bsc was with with `-bs-g`. ``` [anyToJson value] turns any [value] into a JSON object This will throw an error if there are any functions anywhere in the value. [unsafeAnyFromJson json] converts a serialized JSON object back into the bucklescript runtime value. Warning: marshaling is currently not type-safe. The type of marshaled data is not transmitted along the value of the data, making it impossible to check that the data read back possesses the type expected by the context. The return type of this function is given as 'a, but this is misleading: the returned OCaml value does not possess type 'a for all 'a; it has one, unique type which cannot be determined at compile-time. The programmer should explicitly give the expected type of the returned value, using the following syntax: (Js.Json.unserializeAnyFromJsonUnsafe json : type) Anything can happen at run-time if the object does not correspond to the assumed type. ``` Test Plan: ``` make libs make -C jscomp/tests all ./node_modules/.bin/mocha jscomp/test/js_json_test.js ```
|
I remember that @cristianoc had something similar. |
|
I think it's important that it be distributed with the compiler because it's dependent on the runtime representation of types (as defined by the compiler). So if the representation changes, then this would need to change. Also this is a thing that everyone asks for, and it would be great to bake it in. |
|
@jaredly here’s the serialization code I have written: https://github.com/cristianoc/REInfer/blob/master/src/Serialize.re There are a couple of special cases there: such as respecting the distinction between null and undefined. |
|
I suggest we may consolidate our work in a third party library(with full coverage test, since it is fragile code). I am okay to have it merged if someone is willing to provide committed maintenance |
|
What can we do to prevent accidentally forgetting the annotation for unserialize? |
|
I called it |
|
@jaredly Is this solution to provide a native/portable version of |
|
@htzh yes, this would preserve the types on both ends because Bucklescript does some things in the compiled code that isn't part of JSON stringify. For example it augments arrays with a As soon as you try to send a Variant to the server with By using the proposed functions, you could share types between client and server and not have to write any bindings on either side since these functions would convert these data types to the same JS format that the compiler uses to identify types. |
|
note something of similar functionalities are added into Js.Json. |
See docs for more info. This also retains debug information if bsc was
with with
-bs-g.This is basically parallel in spirit to OCaml's "input_value" and "output_value", but specialized to the javascript use-case.
Test Plan: