public static void toJson(ViewVersion version, JsonGenerator generator) throws IOException {
    Preconditions.checkArgument(version != null, "Cannot serialize null view version");
    generator.writeStartObject();

    generator.writeNumberField(VERSION_ID, version.versionId());
    generator.writeNumberField(TIMESTAMP_MS, version.timestampMillis());
    generator.writeNumberField(SCHEMA_ID, version.schemaId());
    JsonUtil.writeStringMap(SUMMARY, version.summary(), generator);

    if (version.defaultCatalog() != null) {
      generator.writeStringField(DEFAULT_CATALOG, version.defaultCatalog());
    }

    JsonUtil.writeStringArray(
        DEFAULT_NAMESPACE, Arrays.asList(version.defaultNamespace().levels()), generator);

    generator.writeArrayFieldStart(REPRESENTATIONS);
    for (ViewRepresentation representation : version.representations()) {
      ViewRepresentationParser.toJson(representation, generator);
    }
    generator.writeEndArray();

    generator.writeEndObject();
  }
@Test
  public void testSerializeViewVersion() {
    SQLViewRepresentation firstRepresentation =
        ImmutableSQLViewRepresentation.builder()
            .sql("select * from foo")
            .dialect("spark-sql")
            .build();
    SQLViewRepresentation secondRepresentation =
        ImmutableSQLViewRepresentation.builder()
            .sql("select a, b, c from foo")
            .dialect("some-sql")
            .build();

    ViewVersion viewVersion =
        ImmutableViewVersion.builder()
            .versionId(1)
            .timestampMillis(12345)
            .addRepresentations(firstRepresentation, secondRepresentation)
            .summary(ImmutableMap.of("user", "some-user"))
            .defaultNamespace(Namespace.of("one", "two"))
            .defaultCatalog("catalog")
            .schemaId(1)
            .build();

    String expectedRepresentations =
        "[{\"type\":\"sql\",\"sql\":\"select * from foo\",\"dialect\":\"spark-sql\"},"
            + "{\"type\":\"sql\",\"sql\":\"select a, b, c from foo\",\"dialect\":\"some-sql\"}]";

    String expectedViewVersion =
        String.format(
            "{\"version-id\":1,\"timestamp-ms\":12345,\"schema-id\":1,\"summary\":{\"user\":\"some-user\"},"
                + "\"default-catalog\":\"catalog\",\"default-namespace\":[\"one\",\"two\"],\"representations\":%s}",
            expectedRepresentations);

    assertThat(ViewVersionParser.toJson(viewVersion))
        .as("Should be able to serialize valid view version")
        .isEqualTo(expectedViewVersion);
  }