在Jackson的单个文件中反序列化来自多个YAML文档的POJO

时间:2014-08-09 19:38:09

标签: java jackson yaml

我有一个看起来像这样的YAML文件:

---
name: Sam
tags:
    -   Dev
    -   Java
----
name: Bob
tags:
    -   PM

我想使用Jackson来反序列化文件中的所有文档,但我没有办法使用普通ObjectMapper来执行此操作。 如果我使用YAMLFactory为我的文件创建解析器,我可以遍历所有标记,因此解析器显然能够处理多个文档 - 但是如何将它们绑在一起?看起来我的YAMLFactory创建的解析器只解析文件中的单个文档。

我也尝试使用ObjectMapper#readValue(JsonParser, Class)直接创建YAMLParser,但是ObjectMapper耗尽整个YAMLParser来反序列化单个实例。

3 个答案:

答案 0 :(得分:9)

这是多年以后但值得指出的是,这是支持的。 杰克逊的语义略有不同,可能是因为它的JSON起源。这可以通过使用ObjectMapper中的MappingIterator来实现。

YAMLFactory yaml;
ObjectMapper mapper;

YAMLParser yamlParser = yaml.createParser("file-with-multiple-docs.yaml")
List<ObjectNode> docs = mapper
      .readValues<ObjectNode>(yamlParser, new TypeReference<ObjectNode> {})
      .readAll();

如果需要,将ObjectNode替换为您自己的POJO。

答案 1 :(得分:2)

你可以直接使用SnakeYaml(Jackson YAML解析器在内部使用它):

using System;
using System.Data.SQLite;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelSqlite
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            string cs = "URI=file:test.db";
            string data = String.Empty;

            int i = 0;
            int j = 0;

            using (SQLiteConnection con = new SQLiteConnection(cs))
            {
                con.Open();

                string stm = "SELECT * FROM Contacts";

                using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
                {
                    using (SQLiteDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read()) // Reading Rows
                        {
                            for (j = 0; j <= rdr.FieldCount - 1; j++) // Looping throw colums
                            {
                                data = rdr.GetValue(j).ToString();
                                xlWorkSheet.Cells[i + 1, j + 1] = data;
                            }
                            i++;
                        }
                    }
                }
                con.Close();
            }

            xlWorkBook.SaveAs("sqliteToExcel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        private static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}

将生产:

try (InputStream input = new FileInputStream(file)) {
    Yaml yaml = new Yaml(new SafeConstructor());
    yaml.loadAll(input).forEach( System.out::println );
} catch (Throwable e) {
    System.out.println("ERROR: " + e.getMessage());
}

答案 2 :(得分:1)

此时似乎不支持此功能。以下是the YAMLParser source code的链接。

如果输入的YAML包含多个文档,则Jackson失败。这是一个例子:

public class JacksonYAML {
    public static final String YAML = "---\n" +
            "name: Sam\n" +
            "tags:\n" +
            "    -   Dev\n" +
            "    -   Java\n" +
            "----\n" +
            "name: Bob\n" +
            "tags:\n" +
            "    -   PM";

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        System.out.println(mapper.readValue(YAML, Object.class));
    }
}

异常错误指向输入源中的第二项:

Exception in thread "main" while scanning a simple key
 in 'reader', line 6, column 1:
    ----
    ^
could not found expected ':'
 in 'reader', line 7, column 1:
    name: Bob
    ^

    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.scanner.ScannerImpl.stalePossibleSimpleKeys(ScannerImpl.java:465)
    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.scanner.ScannerImpl.needMoreTokens(ScannerImpl.java:280)
    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:225)
    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserImpl$ParseBlockSequenceEntry.produce(ParserImpl.java:502)
    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158)
    at com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserImpl.getEvent(ParserImpl.java:168)
    at com.fasterxml.jackson.dataformat.yaml.YAMLParser.nextToken(YAMLParser.java:331)
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapArray(UntypedObjectDeserializer.java:529)
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:449)
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:572)
    at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:435)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2146)
    at stackoverflow.JacksonYAML.main(JacksonYAML.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)