/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2018 wcm.io
 * %%
 * 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
 *
 *      http://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.
 * #L%
 */
package io.wcm.qa.glnm.sampling.transform;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.wnameless.json.flattener.JsonFlattener;

import io.wcm.qa.glnm.sampling.Sampler;
import io.wcm.qa.glnm.sampling.transform.base.TransformationBasedSampler;

/**
 * Samples JSON formatted data into a flat map.
 *
 * @param <S> type of sampler supplying a JSON string
 * @since 1.0.0
 */
public class JsonSampler<S extends Sampler<String>> extends TransformationBasedSampler<S, String, Map<String, String>> {

  private static final Logger LOG = LoggerFactory.getLogger(JsonSampler.class);

  private static final String STRING_REPRESENTATION_NULL = "null";

  /**
   * <p>Constructor for JsonSampler.</p>
   *
   * @param inputSampler providing the JSON as String
   * @since 3.0.0
   */
  public JsonSampler(S inputSampler) {
    super(inputSampler);
  }

  /**
   * <p>
   * Constructor for JsonSampler which requires using setter to provide input sampler.
   * </p>
   */
  public JsonSampler() {
    super();
  }

  private String asString(Entry<String, Object> entry) {
    Object value = entry.getValue();
    if (value == null) {
      return STRING_REPRESENTATION_NULL;
    }
    return value.toString();
  }

  private Map<String, String> ensureOnlyStringValues(Map<String, Object> mapWithObjects) {
    Map<String, String> stringOnlyMap = new HashMap<String, String>();
    for (Entry<String, Object> entry : mapWithObjects.entrySet()) {
      stringOnlyMap.put(entry.getKey(), asString(entry));
    }
    return stringOnlyMap;
  }

  @Override
  protected Map<String, String> transform(String inputSample) {
    LOG.debug("JsonSampler: attempting to parse '" + inputSample + "'");
    return ensureOnlyStringValues(JsonFlattener.flattenAsMap(inputSample));
  }

}

@@ -895,24 +895,82 @@
 ng.Sampler;%0A
+import io.wcm.qa.glnm.sampling.string.FixedStringSampler;%0A
 import io.wc
@@ -2389,24 +2389,58 @@
 utSample) %7B%0A
+    if (LOG.isDebugEnabled()) %7B%0A  
     LOG.debu
@@ -2500,16 +2500,22 @@
 + %22'%22);%0A
+    %7D%0A
     retu
@@ -2587,10 +2587,473 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Factory method to avoid boiler plate when just wanting to flatten already retrieved JSON.%0A   *%0A   * @param json JSON as string%0A   * @return JSON as a flat String to String map%0A   */%0A  public static Map%3CString, String%3E flatten(String json) %7B%0A    FixedStringSampler inputSampler = new FixedStringSampler(json);%0A    JsonSampler%3CFixedStringSampler%3E jsonSampler = new JsonSampler%3CFixedStringSampler%3E(inputSampler);%0A    return jsonSampler.sampleValue();%0A  %7D%0A
 %7D%0A

add static method for flattening