简体   繁体   English

需要帮助解决杰克逊的问题

[英]Need help troubleshooting with Jackson

I have a HashMap wich i initialize once in static block from json file.我有一个 HashMap,我在 json 文件的静态块中初始化一次。 Then i work with my local HashMap to save users requests.然后我使用我的本地 HashMap 来保存用户请求。

static {
  TypeFactory typeFactory = mapper.getTypeFactory();
  MapType mapType = typeFactory.constructMapType(ConcurrentHashMap.class, String.class, GooglePlayGame.class);
  try {
       games = mapper.readValue(new File("games.json"), mapType);
        } catch (IOException e) {
          log.error(e.getMessage());
        }
    }

When i decide to stop app and launch it again, I can add new pair of "key-value" to this map, but when i'm trying to append changes into my json file, everything that was already in json file is duplicated + new value.当我决定停止应用程序并再次启动它时,我可以向该地图添加新的“键值”对,但是当我尝试将更改附加到我的 json 文件中时,json 文件中已经存在的所有内容都将被复制 +新值。 This code:这段代码:

try(JsonGenerator g = mapper.getFactory().createGenerator(
                  new PrintWriter(new BufferedWriter(new FileWriter(new File("games.json"), true))))) {
    mapper.writeValue(g, games);
    } catch (IOException e) {
      log.error(e.getMessage());
    }

I understood why it's happening (because of static initialization every new launch, and then i write to file all hashmap again and again), but i dont know how to fix this.我明白为什么会发生这种情况(因为每次新发布都会进行静态初始化,然后我一次又一次地写入所有 hashmap 文件),但我不知道如何解决这个问题。 I want to append new pairs to existing json file.我想将新对附加到现有的 json 文件中。

For example: Adding first request, all fine:例如:添加第一个请求,一切正常:

{"Machinarium":
   {"Title":"Machinarium",
    "Updated":"28 February, 2019",
    "Version":"2.5.6","Requirements":"4.1,
     "Contacts":"support@amanita-design.net"
}

Then i restart app and got another request, my json file now looks like:然后我重新启动应用程序并收到另一个请求,我的 json 文件现在看起来像:

{"Machinarium":
   {"Title":"Machinarium",
    "Updated":"28 February, 2019",
    "Version":"2.5.6","Requirements":"4.1,
     "Contacts":"support@amanita-design.net",
....
},
{"Machinarium":
   {"Title":"Machinarium",
    "Updated":"28 February, 2019",
    "Version":"2.5.6","Requirements":"4.1,
     "Contacts":"support@amanita-design.net",
....
},
"Samorost 3":
   {"Title":"Samorost 3",
    "Updated":"November 14, 2019",
     "Version":"1.0",
     "Requirements":"4.3,
...}

As you can see, duplicate here.如您所见,在此处重复。

So the goal is: create HashMap -> get user request -> write this request(pair "key-value") to local hashmap -> write hashmap (or every pair separately? hmm) to json file.所以目标是:创建 HashMap -> 获取用户请求 -> 将此请求(对“键值”)写入本地哈希图 -> 将哈希图(或每对单独?嗯)写入 json 文件。 Then when app starts again: initialize hashmap from this json file with saved requests -> get new user request -> add new requests to json file.然后当应用程序再次启动时:使用保存的请求从这个 json 文件初始化 hashmap -> 获取新用户请求 -> 将新请求添加到 json 文件。

So kinda i need to write each pair one by one to file (not all map), but how?所以有点我需要将每一对一一写入文件(不是所有地图),但是如何?

Ultimately, your issue is here: new FileWriter(new File("games.json"), true)) .最终,您的问题在这里: new FileWriter(new File("games.json"), true))

The FileWriter constructor is FileWriter(File file, boolean append) so you are appending data to that file every time you write. FileWriter构造函数是FileWriter(File file, boolean append)因此您每次写入时都将数据附加到该文件。

What you want instead is just FileWriter(File file) which overwrites the file by default.你想要的只是FileWriter(File file) ,它默认覆盖文件。 This assumes that your games map is always up to date and has all the data in memory.这假设您的games地图始终是最新的,并且所有数据都在内存中。

This is a bit less efficient since you'd be overwriting the entire file every time a change is made, but this doesn't seem likely to be a lot of data, so I don't think it should be a concern.这有点低效率,因为每次进行更改时您都会覆盖整个文件,但这似乎不是很多数据,所以我认为这不应该是一个问题。 If this turns out to be a very large amount of data that is updated frequently, you may want to look into using a database instead of a JSON file.如果事实证明这是频繁更新的大量数据,您可能需要考虑使用数据库而不是 JSON 文件。

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

相关问题 需要帮助解决我的VigenereCipher Java代码 - Need help troubleshooting my VigenereCipher java code 需要一些帮助来解决安排每日通知的代码 - Need some help troubleshooting code that schedules daily notifications 我需要帮助对我的MergeSort和Merge代码进行故障排除 - I need help troubleshooting my code for MergeSort and Merge Jackson 列表帮助 - Java - Jackson List Help - Java 解决500错误所需的帮助(Java) - Troubleshooting help needed for 500 error (Java) 杰克逊:需要另一种方法来成为传教士 - Jackson: need a another method to be a setter 显示一个名为时间的类。 (帮助故障排除代码) - Display a class named Time. (help troubleshooting code) DJL 找不到 MXNet 引擎,故障排除对我没有帮助 - DJL is unable to find MXNet Engine, troubleshooting doesn't help me 需要使用Jackson来反序列化此JSON-[{},{},{}]-用于JSON数组的Jackson注释是什么? - Need to deserialize this JSON with Jackson - [{}, {}, {}] - What Jackson annotation to use for JSON array? 需要有关在低内存系统上对Runtime.getRuntime.exec()进行故障排除的输入吗? - Need inputs on troubleshooting Runtime.getRuntime.exec() on Low Memory System ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM