hc | 14 Nov 11:22 2013
Picon

Aeson js question

Hello everyone,

I'd use aeson to parse JSON. I did much of the translation JSon in
Haskell data but I'm stuck on one point.

Suppose I have a JSon object that is in this form:

{\"Ports\":{\"22/tcp\":[{\"HostIp\":\"0.0.0.0\",\"HostPort\":\"49154\"}],\"80/tcp\":[{\"HostIp\":\"0.0.0.0\",\"HostPort\":\"49155\"}]}

I do not know how to "bind" the key name and value knowing that the
key changes name "randomly". Example with port 21 and port 8888:

{\"Ports\":{\"21/tcp\":[{\"HostIp\":\"0.0.0.0\",\"HostPort\":\"49154\"}],\"8888/tcp\":[{\"HostIp\":\"0.0.0.0\",\"HostPort\":\"49155\"}]}}

All code : http://lpaste.net/95607

Thank you for your help.
Yuras Shumovich | 14 Nov 13:39 2013
Picon

Re: Aeson js question

Hi,

The "ContainerNetworkSettingsPorts" should actually be a "Data.Map
String ContainerNetworkSettingsPort"

data ContainerNetworkSettingsPort = ContainerNetworkSettingsPort {
  HostIp :: String,
  HostPort :: String
}

type ContainerNetworkSettingsPorts = Map String ContainerNetworkSettingsPort

You can reuse instance FromJSON a => FromJSON (Map String a)

Thanks,
Yuras

On Thu, 2013-11-14 at 11:22 +0100, hc <at> j4.pe wrote:
> Hello everyone,
> 
> I'd use aeson to parse JSON. I did much of the translation JSon in
> Haskell data but I'm stuck on one point.
> 
> Suppose I have a JSon object that is in this form:
> 
> {\"Ports\":{\"22/tcp\":[{\"HostIp\":\"0.0.0.0\",\"HostPort\":\"49154\"}],\"80/tcp\":[{\"HostIp\":\"0.0.0.0\",\"HostPort\":\"49155\"}]}
> 
> I do not know how to "bind" the key name and value knowing that the
> key changes name "randomly". Example with port 21 and port 8888:
> 
(Continue reading)

hc | 14 Nov 19:38 2013
Picon

Re: Aeson js question

Hello,

Thank you for your reply. It really helped me. I mixed response to
this post and this one:
http://stackoverflow.com/questions/17278472/arbitrary-json-keys-with-aeson-haskell

Now it works for those who are interested here is the example:
http://lpaste.net/95671

regards

2013/11/14 Yuras Shumovich <shumovichy <at> gmail.com>:
> Hi,
>
> The "ContainerNetworkSettingsPorts" should actually be a "Data.Map
> String ContainerNetworkSettingsPort"
>
> data ContainerNetworkSettingsPort = ContainerNetworkSettingsPort {
>   HostIp :: String,
>   HostPort :: String
> }
>
> type ContainerNetworkSettingsPorts = Map String ContainerNetworkSettingsPort
>
> You can reuse instance FromJSON a => FromJSON (Map String a)
>
> Thanks,
> Yuras
>
> On Thu, 2013-11-14 at 11:22 +0100, hc <at> j4.pe wrote:
(Continue reading)


Gmane