// Path: cloud-spanner-r2dbc/src/main/java/com/google/cloud/spanner/r2dbc/v2/JsonWrapper.java
// public class JsonWrapper {
//   private String jsonString;
// 
//   public JsonWrapper(String jsonString) {
//     this.jsonString = jsonString;
//   }
// 
//   protected Value getJsonVal() {
//     return Value.json(this.jsonString);
//   }
// 
//   public static JsonWrapper of(String jsonString) {
//     return new JsonWrapper(jsonString);
//   }
// 
//   @Override
//   public String toString() {
//     return this.jsonString;
//   }
// 
//   @Override
//   public boolean equals(Object o) {
//     if (this == o) {
//       return true;
//     }
//     if (o == null || getClass() != o.getClass()) {
//       return false;
//     }
//     JsonWrapper that = (JsonWrapper) o;
//     return Objects.equal(this.jsonString, that.jsonString);
//   }
// 
//   @Override
//   public int hashCode() {
//     return Objects.hashCode(this.jsonString);
//   }
// }


import java.util.ArrayList;
import java.util.List;
import com.google.cloud.spanner.r2dbc.v2.JsonWrapper;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.convert.R2dbcCustomConversions;
import org.springframework.stereotype.Component;
/*
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example;



@Configuration
public class CustomConfiguration extends AbstractR2dbcConfiguration {

  @Autowired
  ApplicationContext applicationContext;

  @Override
  public ConnectionFactory connectionFactory() {
    return null;
  }

  @Bean
  @Override
  public R2dbcCustomConversions r2dbcCustomConversions() {
    List<Converter<?, ?>> converters = new ArrayList<>();
    converters.add(this.applicationContext.getBean(JsonToReviewsConverter.class));
    converters.add(this.applicationContext.getBean(ReviewsToJsonConverter.class));
    return new R2dbcCustomConversions(getStoreConversions(), converters);
  }

  @Component
  @ReadingConverter
  public class JsonToReviewsConverter implements Converter<JsonWrapper, Review> {

    private final Gson gson;

    @Autowired
    public JsonToReviewsConverter(Gson gson) {
      this.gson = gson;
    }

    @Override
    public Review convert(JsonWrapper json) {
      try {
        return this.gson.fromJson(json.toString(), Review.class);
      } catch (JsonParseException e) {
// Path: cloud-spanner-r2dbc/src/main/java/com/google/cloud/spanner/r2dbc/v2/JsonWrapper.java
// public class JsonWrapper {
//   private String jsonString;
// 
//   public JsonWrapper(String jsonString) {
//     this.jsonString = jsonString;
//   }
// 
//   protected Value getJsonVal() {
//     return Value.json(this.jsonString);
//   }
// 
//   public static JsonWrapper of(String jsonString) {
//     return new JsonWrapper(jsonString);
//   }
// 
//   @Override
//   public String toString() {
//     return this.jsonString;
//   }
// 
//   @Override
//   public boolean equals(Object o) {
//     if (this == o) {
//       return true;
//     }
//     if (o == null || getClass() != o.getClass()) {
//       return false;
//     }
//     JsonWrapper that = (JsonWrapper) o;
//     return Objects.equal(this.jsonString, that.jsonString);
//   }
// 
//   @Override
//   public int hashCode() {
//     return Objects.hashCode(this.jsonString);
//   }
// }


// Path: cloud-spanner-r2dbc-samples/cloud-spanner-spring-data-r2dbc-sample/src/main/java/com/example/CustomConfiguration.java
import java.util.ArrayList;
import java.util.List;
import com.google.cloud.spanner.r2dbc.v2.JsonWrapper;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.convert.R2dbcCustomConversions;
import org.springframework.stereotype.Component;

/*
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example;



@Configuration
public class CustomConfiguration extends AbstractR2dbcConfiguration {

  @Autowired
  ApplicationContext applicationContext;

  @Override
  public ConnectionFactory connectionFactory() {
    return null;
  }

  @Bean
  @Override
  public R2dbcCustomConversions r2dbcCustomConversions() {
    List<Converter<?, ?>> converters = new ArrayList<>();
    converters.add(this.applicationContext.getBean(JsonToReviewsConverter.class));
    converters.add(this.applicationContext.getBean(ReviewsToJsonConverter.class));
    return new R2dbcCustomConversions(getStoreConversions(), converters);
  }

  @Component
  @ReadingConverter
  public class JsonToReviewsConverter implements Converter<JsonWrapper, Review> {

    private final Gson gson;

    @Autowired
    public JsonToReviewsConverter(Gson gson) {
      this.gson = gson;
    }

    @Override
    public Review convert(JsonWrapper json) {
      try {
        return this.gson.fromJson(json.toString(), Review.class);
      } catch (JsonParseException e) {
        return new Review();