How do I read a data from a JSON file with high efficiency in Java with Jackson?

后端 未结 2 1550
情书的邮戳
情书的邮戳 2021-01-22 10:36

I store all static data in the JSON file. This JSON file has up to 1000 rows. How to get the desired data without storing all rows as

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-22 11:00

    You can use DSM stream parsing library for memory, CPU efficiency and fast development. DSM uses YAML based mapping file and reads the whole data only once.

    Here is the solution of your question:

    Mapping File:

    params:
       colorsToFilter: ['Blue','Red']  # parameteres can be passed programmatically
    result:
       type: array
       path: /.*colors  # path is regex
       filter: params.colorsToFilter.contains(self.data.color)  # select only color that exist in colorsToFilter list
       fields:
          color: 
          code:
             type: array
    

    Usage of DSM to parse json:

    DSM dsm = new DSMBuilder(new File("path/maping.yaml")).create(Colors.class);
    List object = (List) dsm.toObject(jsonData);
    
    System.out.println(object);
    

    Output:

    [Colors{color='Blue', code=[012, 0324, 15478, 7412]}, Colors{color='Red', code=[145, 001, 1, 7879, 123984, 89]}]
    

提交回复
热议问题