To be clear, the problem is that "Json.Encode.string" isn't saying "take the string you give me and parse it into JSON", it's saying "take the string you gave me and make it a JSON String".
On Fri, Jul 29, 2016 at 12:51 PM, Nick H <[email protected]> wrote: > You are on the right track with looking in the Json.Encode module. What > you tried in the REPL is almost correct. Try this instead: > > > foo = Json.Encode.list [] > > bar = Json.Decode.decodeValue (Json.Decode.list Json.Decode.string) > > On Fri, Jul 29, 2016 at 12:29 PM, Paul Blair <[email protected]> wrote: > >> Spoke too soon; that doesn't work. For example: >> >> > foo = Json.Encode.string "[]" >> "[]" : Json.Encode.Value >> >> > bar = Json.Decode.decodeValue (Json.Decode.list Json.Decode.string) foo >> Err "Expecting a List but instead got: \"[]\"" >> : Result.Result String (List String) >> >> >> On Friday, July 29, 2016 at 3:10:01 PM UTC-4, Paul Blair wrote: >>> >>> To answer my own question -- it looks like Native.Json has an identity >>> function which does just what I want, but which I can't import. However, it >>> also looks like many of Json.Encode's functions just call >>> Native.Json.identity. So this works: >>> >>> data = Encode.string complicatedNestedJson >>> >>> >>> >>> On Friday, July 29, 2016 at 2:52:32 PM UTC-4, Paul Blair wrote: >>>> >>>> I'm using the nested Elm architecture pattern in an application. The >>>> nested components respond to messages coming in on a port. The messages are >>>> handled by the top-level component before being handed on to the nested >>>> components. So the outer data structure looks something like this: >>>> >>>> type alias FromServer = >>>> { message_type: MessageType >>>> , data: Json.Decode.Value >>>> } >>>> >>>> The data field is a Json.Decode.Value because the top-level component >>>> doesn't know about the message types defined by the nested components; it >>>> does a lookup on the incoming message type and decides to which inner >>>> component it will forward the message. So the top level looks like this: >>>> >>>> subscriptions : Model.Model -> Sub Message >>>> subscriptions model = >>>> theIncomingPort toMessage >>>> >>>> >>>> toMessage : FromServer -> Message >>>> toMessage incoming = >>>> case Dict.get incoming.message_type componentDictionary of >>>> Just MyInnerComponent -> >>>> InnerComponentModule.toMessage incoming |> >>>> MyInnerComponentMessageWrapper >>>> Nothing -> >>>> ... >>>> >>>> The inner component's toMessage function also uses the message_type >>>> field to choose the decoder to apply to the Json.Decode.Value in the data >>>> field. >>>> >>>> I want to test the inner component's toMessage function. This means >>>> that I need to create a FromServer record with the data contained in a >>>> Json.Decode.Value -- ideally created from a Json string. >>>> >>>> However, from what I can tell the only way to create this kind of raw >>>> Json.Decode.Value is by passing the string in through a port. This seems >>>> like a pretty complicated way to set up a test. Is there some other way to >>>> accomplish this? >>>> >>>> -- >> 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 [email protected]. >> For more options, visit https://groups.google.com/d/optout. >> > > -- > 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 [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
