Skip to content
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

ofJson<'T> fails to deserialize DateTime and returns a string instead #1344

Closed
Zaid-Ajaj opened this issue Feb 18, 2018 · 3 comments
Closed

Comments

@Zaid-Ajaj
Copy link
Member

Description

Given a record of type A that has with a field of type DateTime, then ofJson<seq<A>> fails to deserialize the DateTime field correctly and returns a string instead

Repro code

The following code will not work because formatDate is getting a string as an input and will throw error: Uncaught TypeError: d.getFullYear is not a function because it is applying functions of Date on string

open System
open Fable.Core
open Fable.Core.JsInterop

type NewsItem = { 
    Title : string
    Published : DateTime
}

let serializedNewsItems = """
     [{ 
         "Title": "Breaking News",
         "Published": "2018-02-18T18:44:36.0000000"
     }]
"""
let formatDate (date: DateTime) = 
    // HERE THE ERROR!!!
    sprintf "%d/%d/%d" date.Year date.Month date.Day

let newsItems = ofJson<seq<NewsItem>> serializedNewsItems

for item in newsItems do printfn "%s - %s" item.Title (formatDate item.Published)

Workaround

Change ofJson<seq<NewsItem>> to ofJson<list<NewsItem>> then it works just fine

Expected and actual results

Expected to deserialize correctly but it instead fails

Related information

  • Fable version (dotnet fable --version): 1.3.8
  • Operating system: Ubuntu 17.0
@alfonsogarciacaro
Copy link
Member

This is tricky because ofJson doesn't support interfaces. I think it's the same for Newtonsoft.Json, as it's not possible to know which constructor should be used when deserializing. But maybe they default to arrays for IEnumerable, I haven't checked it.

@Zaid-Ajaj
Copy link
Member Author

@alfonsogarciacaro I don't think the problem is using seq because the JSON is getting deserialized, the issue is that DateTime field of the items is correctly deserialized but it is returned as a string instead. If it is tricky to implement, we can always just let the user know to use list instead of seq either with compiler warnings or a console message during runtime (coming from ofJson<'T>)

@alfonsogarciacaro
Copy link
Member

I need to check if there's a solution, but right now Fable doesn't keep generic info for interfaces. So if you have ofJson<seq<NewsItem>>, at runtime Fable doesn't know that's a sequence of NewsItem. It just works because it deserializes the JSON as a plain JS object, but it doesn't know Published is a date, so it won't try to parse it as such.

I haven't decided yet how to represent reflection info in Fable 2 and how to make it work with ofJson and friends. Maybe we can find a nice way to handle these cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants