It looks like the value in the "date" field contains a JavaScript Date object. You won't be able to directly decode the JS Date object to a Date in Elm.
You've requested comments_json to come through the port as a Json.Encode.Value, which can (opaquely) contain any JS value (not just JSON primitives); however, only JSON primitives can be "decoded" from a Value to another Elm type. When you log the value to the console, it appears the way it does because Elm converts it to a string and wraps it in angle brackets <https://github.com/elm-lang/core/blob/5.1.1/src/Native/Utils.js#L429>. If you can convert the "date" field of your comment object to a number (using Date.prototype.getTime <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime>) in JS before sending it through the port, then you'll be able to decode it as a Float in Elm and convert it to a Date with Date.fromTime <http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Date#fromTime>. Hope this helps! Justin On Saturday, February 4, 2017 at 9:35:10 AM UTC-5, fxmy wang wrote: > > Thanks Justing. > > What really is confusing me is that when passed through ports, the date > field of JSON *appeared in the form of <Thu Jan 26 2017 11:43:57 GMT+0800 > (CST)>* while the rest of the fields is just old plain string. I’m > probably wrong but it almost seems that Elm is doing some extra work on ISO > date strings, silently parsing them into internal representations when > receiving from ports. > > For your convenience, some relevant code: > > type Msg = > ... > | CommentJson Json.Encode.Value > > port comments_json : ( Json.Encode.Value -> msg) -> Sub msg > > update : Msg -> Model -> ( Model, Cmd Msg) > update msg model = > case msg of > CommentJson value -> > let > _ = Debug.log "value" value > in > ... > > And in console I just got > > value: { 0 = { author = { type = "full", displayName = "fxmy", url = > "https://github.com/fxmy", picture = > "https://avatars.githubusercontent.com/u/3283415?v=3&s=73" }, content = > "another test", date = <Thu Jan 26 2017 11:43:57 GMT+0800 (CST)> } } > > Any ideas how to hunt this down? > > Cheers, > > 在 2017年1月30日星期一 UTC+8下午11:21:34,Justin Mimbs写道: > > If you already have a Date in JS that you need to get into Elm, then >> sending it as an ISO string will certainly work. But another, more direct >> way to pass a date representation through a port is to pass it as a number, >> using the getTime >> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime> >> method >> in JS. In Elm, you can decode it as a Float and then convert it to a Date >> with Date.fromTime >> <http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Date#fromTime>. >> >> There isn't a *Json.Decode.date*, likely because JSON doesn't describe a >> standard way to encode dates. In Elm, the idea of Json encoding/decoding >> extends to all conversions between JS types and Elm types, which is nice in >> that it gives us a unifying concept, but it does leave out the ability to >> pass Date values to and from Elm, without first converting them to a >> JSON-representable type. >> >> Justin >> >> On Sunday, January 29, 2017 at 2:47:43 PM UTC-5, Mickey Vashchinsky wrote: >>> >>> Looks like you already have Date and not String. >>> >>> I am pretty new to Elm myself and I couldn't find a way to decodeValue of >>> Date, but, I think as a workaround, on JavaScript side before sending >>> the json through port, you could convert your Date fields to String >>> with: >>> >>> myJson.date = myJson.date.toISOString() >>> >>> Hope it helps. >>> >>> >>> On Sunday, January 29, 2017 at 4:55:51 PM UTC+2, fxmy wang wrote: >>>> >>>> Hi guys, >>>> So I was trying to decode a field named date inside JSON as String but >>>> without success. And the result is quite confusing. >>>> Could anyone shed some light on me? >>>> >>>> JSON: >>>> >>>> [{"author":{"type":"full","displayName":"fxmy","url":"https://github.com/fxmy","picture":"https://avatars.githubusercontent.com/u/3283415?v=3&s=73"},"content":"another >>>> test","date":"2017-01-26T03:43:57.190Z"}] >>>> >>>> Json.Encode.Value that comes through port ( via Debug.log) : >>>> >>>> value: { 0 = { author = { type = "full", displayName = "fxmy", url = >>>> "https://github.com/fxmy", picture = >>>> "https://avatars.githubusercontent.com/u/3283415?v=3&s=73" }, content = >>>> "another test", date = <Thu Jan 26 2017 11:43:57 GMT+0800 (CST)> } } >>>> >>>> When trying to decode date field as String I got this, >>>> Result after calling Json.Decode.decodeValue ( via Debug.log) : >>>> >>>> Err "Expecting a String at _[0].date but instead got: >>>> \"2017-01-26T03:43:57.190Z\"" >>>> >>>> What am I doing wrong? >>>> >>>> Cheers. >>>> >>>> >>> > -- You received this message because you are subscribed to the Google Groups "Elm Discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.