code: public static void serialise(Path output, Object json) throws IOException {
  
          // First serialise to a temp file:
          Gson gson = getBuilder().create();
          Path temp = Files.createTempFile(json.getClass().getSimpleName(), ".json");
          try (Writer writer = Files.newBufferedWriter(temp, StandardCharsets.UTF_8)) {
              gson.toJson(json, writer);
          }
  
          // Now do an optimised Channel-to-Channel transfer to the output file:
          long size = Files.size(temp);
          try (FileChannel tempChannel = FileChannel.open(temp, StandardOpenOption.READ);
               FileChannel outputChannel = FileChannel.open(output, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
              // NB the lock will be released when the channel is closed:
              writeLock(outputChannel);
              outputChannel.truncate(0);
              tempChannel.transferTo(0, size, outputChannel);
              outputChannel.truncate(size);
          }
      }
docs: Serialises the given object to Json and writes it to the given
  {@link Path}. This method will acquire a filesystem lock on the
  given path in order to avoid corruption in the event that multple
  threads attempt to write to the same file at the same time.
  
  @param output The Path to serialise to.
  @param json   The Json to be serialised.
  @throws IOException If an error occurs in writing the output.