static void toJson(ManifestFile manifestFile, JsonGenerator generator) throws IOException {
    Preconditions.checkArgument(manifestFile != null, "Invalid manifest file: null");
    Preconditions.checkArgument(generator != null, "Invalid JSON generator: null");

    generator.writeStartObject();

    generator.writeStringField(PATH, manifestFile.path());
    generator.writeNumberField(LENGTH, manifestFile.length());
    generator.writeNumberField(SPEC_ID, manifestFile.partitionSpecId());

    if (manifestFile.content() != null) {
      generator.writeNumberField(CONTENT, manifestFile.content().id());
    }

    generator.writeNumberField(SEQUENCE_NUMBER, manifestFile.sequenceNumber());
    generator.writeNumberField(MIN_SEQUENCE_NUMBER, manifestFile.minSequenceNumber());

    if (manifestFile.snapshotId() != null) {
      generator.writeNumberField(ADDED_SNAPSHOT_ID, manifestFile.snapshotId());
    }

    if (manifestFile.addedFilesCount() != null) {
      generator.writeNumberField(ADDED_FILES_COUNT, manifestFile.addedFilesCount());
    }

    if (manifestFile.existingFilesCount() != null) {
      generator.writeNumberField(EXISTING_FILES_COUNT, manifestFile.existingFilesCount());
    }

    if (manifestFile.deletedFilesCount() != null) {
      generator.writeNumberField(DELETED_FILES_COUNT, manifestFile.deletedFilesCount());
    }

    if (manifestFile.addedRowsCount() != null) {
      generator.writeNumberField(ADDED_ROWS_COUNT, manifestFile.addedRowsCount());
    }

    if (manifestFile.existingRowsCount() != null) {
      generator.writeNumberField(EXISTING_ROWS_COUNT, manifestFile.existingRowsCount());
    }

    if (manifestFile.deletedRowsCount() != null) {
      generator.writeNumberField(DELETED_ROWS_COUNT, manifestFile.deletedRowsCount());
    }

    if (manifestFile.partitions() != null) {
      generator.writeArrayFieldStart(PARTITION_FIELD_SUMMARY);
      for (ManifestFile.PartitionFieldSummary summary : manifestFile.partitions()) {
        PartitionFieldSummaryParser.toJson(summary, generator);
      }

      generator.writeEndArray();
    }

    if (manifestFile.keyMetadata() != null) {
      generator.writeFieldName(KEY_METADATA);
      SingleValueParser.toJson(DataFile.KEY_METADATA.type(), manifestFile.keyMetadata(), generator);
    }

    generator.writeEndObject();
  }
@Test
  public void testParser() throws Exception {
    ManifestFile manifest = createManifestFile();
    String jsonStr = JsonUtil.generate(gen -> ManifestFileParser.toJson(manifest, gen), false);
    assertThat(jsonStr).isEqualTo(manifestFileJson());
  }