简体   繁体   中英

Need help troubleshooting with Jackson

I have a HashMap wich i initialize once in static block from json file. Then i work with my local HashMap to save users requests.

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. 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. I want to append new pairs to existing json file.

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:

{"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. Then when app starts again: initialize hashmap from this json file with saved requests -> get new user request -> add new requests to json file.

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)) .

The FileWriter constructor is FileWriter(File file, boolean append) so you are appending data to that file every time you write.

What you want instead is just FileWriter(File file) which overwrites the file by default. This assumes that your games map is always up to date and has all the data in memory.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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