简体   繁体   English

如何使用Text.JSON解析从Haskell中的文件读取的字符串

[英]How do I use Text.JSON to parse a string read from a file in Haskell

I'm writing an application in Haskell in which as part of my persistence system, I am converting my data structures to JSON format and then writing them to disk. 我正在Haskell中编写一个应用程序,作为我的持久性系统的一部分,我将我的数据结构转换为JSON格式,然后将它们写入磁盘。 Later, I want to load them by reading the file and running it through a JSON parser to reconstitute the objects. 稍后,我想通过读取文件并通过JSON解析器运行它来重新构建对象来加载它们。

However, something goes wrong in the read portion that prevents the JSON module from parsing the string. 但是,在读取部分出现问题,导致JSON模块无法解析字符串。

First, I am using Text.JSON (json-0.5) for my work. 首先,我正在使用Text.JSON(json-0.5)来完成我的工作。 The datatype that I am writing to file is called the WorkoutEvent , and I have verified that it encodes and decodes to JSON perfectly using this command: 我写入文件的数据类型称为WorkoutEvent ,我已经验证它使用此命令完美地编码和解码为JSON:

decode $ encode evt :: Result WorkoutEvent

So, with that in mind, I create a bunch of events and write them to a file, and the resulting file looks like this (when I open it in my text editor): 因此,考虑到这一点,我创建了一堆事件并将它们写入文件,结果文件看起来像这样(当我在文本编辑器中打开它时):

{"type":"AddWorkout","data":{"uuid":"473b7964-8197-49ac-be3b-2b3804f1fbb5","date":"2012-03-30","workout_type":"Pushups","description":"","sets":[6]}}
{"type":"AddWorkout","data":{"uuid":"9f83363a-5298-4778-9c80-7dfa0d2ea6f9","date":"2012-04-02","workout_type":"Pushups","description":"","sets":[6,6]}}
{"type":"AddWorkout","data":{"uuid":"a60806a0-efd6-4647-bc62-a48298ce55cd","date":"2012-04-04","workout_type":"Pushups","description":"","sets":[]}}

And so on and so forth. 等等等等。 Each line in the file represents one full object that I have serialized. 文件中的每一行代表一个我已序列化的完整对象。

Then I load up the file using readFile and I see no problems: 然后我使用readFile加载文件,我发现没有问题:

*Main> f <- readFile "/home/savanni/Documents/workouts.json-stream"
*Main> take 50 f
"{\"type\":\"AddWorkout\",\"data\":{\"uuid\":\"473b7964-8197"
*Main> putStrLn $ take 50 f
{"type":"AddWorkout","data":{"uuid":"473b7964-8197

So far, so good. 到现在为止还挺好。 The catch comes when I try to decode the data: 当我尝试解码数据时出现问题:

*Main> decode f :: Result [WorkoutEvent]
Error "Invalid tokens at end of JSON string: \"{\\\"type\\\":\\\"A\""

If I say putStrLn f I see the output and see that there are no \\ sequences anywhere in the output. 如果我说putStrLn f我看到输出,并看到输出中的任何地方都没有\\ sequence。 If I just enter f at the command line, I see escape sequences as above, but I never see any multiple-\\ escape sequences anywhere. 如果我只是在命令行输入f ,我会看到如上所述的转义序列,但我从未在任何地方看到任何多个\\ _转义序列。

So, in the end, how exactly am I supposed to read JSON data from a file using the Text.JSON module? 那么,最后,我究竟应该如何使用Text.JSON模块从文件中读取JSON数据?

Each line is valid JSON, but the entire file isn't, so you need to decode it line by line. 每一行都是有效的JSON,但整个文件不是,所以你需要逐行解码。 Something like (untested): 像(未经测试)的东西:

map (decode :: String -> Result WorkoutEvent) $ lines f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM