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

Unable to encode class to JSON? #725

Closed
FilippoPolo opened this Issue Jul 24, 2017 · 5 comments

Comments

Projects
None yet
2 participants
@FilippoPolo

Hi all,
I'm trying to convert an F# class to/from JSON, but I can't make it work. The documentation only refers to C# classes; in theory, it should be the same, but in practice I'm always getting a "type not supported" error. This is my test case, a minimal class with a read/write property:

[<System.SerializableAttribute>]
type test(str:string) =
  class
    let mutable _str = str
    member this.Str
        with get() = _str
        and set(value) = _str <- value
  end
let encodeTest (t:test) = WebSharper.Json.Encode<test> t
let decodeTest (t:obj) = WebSharper.Json.Decode<test> t

What am I doing wrong?

@Jand42

This comment has been minimized.

Show comment
Hide comment
@Jand42

Jand42 Jul 24, 2017

Member

A class with Serializable works through RPC calls, but there are no automatic logic yet to encode/decode it on the client side properly. We might consider adding it. Recommended way now is to have your data structures which you would like to store (for example in localStorage) in records/unions/arrays, and then use WebSharper.Json.Serialize`Deserialize`.

Member

Jand42 commented Jul 24, 2017

A class with Serializable works through RPC calls, but there are no automatic logic yet to encode/decode it on the client side properly. We might consider adding it. Recommended way now is to have your data structures which you would like to store (for example in localStorage) in records/unions/arrays, and then use WebSharper.Json.Serialize`Deserialize`.

@FilippoPolo

This comment has been minimized.

Show comment
Hide comment
@FilippoPolo

FilippoPolo Jul 24, 2017

I see, so Encode/Decode don't work on classes? The documentation at https://developers.websharper.com/docs/server-side_web_development/json_api/heading-2 suggest that they should. Is the doc wrong? Or is it a C#-only feature?

I see, so Encode/Decode don't work on classes? The documentation at https://developers.websharper.com/docs/server-side_web_development/json_api/heading-2 suggest that they should. Is the doc wrong? Or is it a C#-only feature?

@Jand42

This comment has been minimized.

Show comment
Hide comment
@Jand42

Jand42 Jul 25, 2017

Member

Sorry, the documentation is indeed misleading, de/serializing classes is currently only implemented on the server-side. Adding this now to client-side macro.

Member

Jand42 commented Jul 25, 2017

Sorry, the documentation is indeed misleading, de/serializing classes is currently only implemented on the server-side. Adding this now to client-side macro.

@Jand42 Jand42 closed this in 64a1f93 Jul 25, 2017

@Jand42

This comment has been minimized.

Show comment
Hide comment
@Jand42

Jand42 Jul 25, 2017

Member

Added support, releasing soon. Serializable attribute and a default constructor is only needed for server-side use, can be omitted for de/serializing on the client.

Member

Jand42 commented Jul 25, 2017

Added support, releasing soon. Serializable attribute and a default constructor is only needed for server-side use, can be omitted for de/serializing on the client.

@FilippoPolo

This comment has been minimized.

Show comment
Hide comment
@FilippoPolo

FilippoPolo Jul 25, 2017

Great, thanks!

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment