Exemplo de como analisar a saída do Exiftool JSON em Haskell
Pergunta
Não consigo entender nada da documentação. Alguém pode fornecer um exemplo de como posso analisar o seguinte exiftool
saída usando o módulo Haskell Text.JSON
? Os dados estão gerando usando o comando exiftool -G -j <files.jpg>
.
[{
"SourceFile": "DSC00690.JPG",
"ExifTool:ExifToolVersion": 7.82,
"File:FileName": "DSC00690.JPG",
"Composite:LightValue": 11.6
},
{
"SourceFile": "DSC00693.JPG",
"ExifTool:ExifToolVersion": 7.82,
"File:FileName": "DSC00693.JPG",
"EXIF:Compression": "JPEG (old-style)",
"EXIF:ThumbnailLength": 4817,
"Composite:LightValue": 13.0
},
{
"SourceFile": "DSC00694.JPG",
"ExifTool:ExifToolVersion": 7.82,
"File:FileName": "DSC00694.JPG",
"Composite:LightValue": 3.7
}]
Solução
Bem, a maneira mais fácil é recuperar um jsvalue do JSON Pacote, como assim (assumindo que seus dados estejam em text.json):
Prelude Text.JSON> s <- readFile "test.json"
Prelude Text.JSON> decode s :: Result JSValue
Ok (JSArray [JSObject (JSONObject {fromJSObject = [("SourceFile",JSString (JSONString {fromJSString = "DSC00690.JPG"})),("ExifTool:ExifToolVersion",JSRational False (391 % 50)),("File:FileName",JSString (JSONString {fromJSString = "DSC00690.JPG"})),("Composite:LightValue",JSRational False (58 % 5))]}),JSObject (JSONObject {fromJSObject = [("SourceFile",JSString (JSONString {fromJSString = "DSC00693.JPG"})),("ExifTool:ExifToolVersion",JSRational False (391 % 50)),("File:FileName",JSString (JSONString {fromJSString = "DSC00693.JPG"})),("EXIF:Compression",JSString (JSONString {fromJSString = "JPEG (old-style)"})),("EXIF:ThumbnailLength",JSRational False (4817 % 1)),("Composite:LightValue",JSRational False (13 % 1))]}),JSObject (JSONObject {fromJSObject = [("SourceFile",JSString (JSONString {fromJSString = "DSC00694.JPG"})),("ExifTool:ExifToolVersion",JSRational False (391 % 50)),("File:FileName",JSString (JSONString {fromJSString = "DSC00694.JPG"})),("Composite:LightValue",JSRational False (37 % 10))]})])
Isso apenas fornece um tipo de dados genérico JSON Haskell.
A próxima etapa será definir um tipo de dados Haskell personalizado para seus dados e escrever uma instância do JSON para isso, que converte entre o JSValue como acima e o seu tipo.
Outras dicas
Obrigado a todos. Pelas suas sugestões, pude reunir o seguinte, que traduz o JSON de volta em pares de nomes-valor.
data Exif =
Exif [(String, String)]
deriving (Eq, Ord, Show)
instance JSON Exif where
showJSON (Exif xs) = showJSONs xs
readJSON (JSObject obj) = Ok $ Exif [(n, s v) | (n, JSString v) <- o]
where
o = fromJSObject obj
s = fromJSString
Infelizmente, parece que a biblioteca não consegue traduzir o JSON de volta em uma estrutura de dados Haskell simples. Em Python, é uma linha: json.loads(s)
.