com.mapr.data.sputnik.JsonGenerator.java Source code

Java tutorial

Introduction

Here is the source code for com.mapr.data.sputnik.JsonGenerator.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mapr.data.sputnik;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mapr.data.sputnik.config.JSONConfigReader;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * 
 */
public class JsonGenerator {

    private static final Logger log = LogManager.getLogger(JsonGenerator.class);

    public JsonGenerator() throws IOException {
    }

    public Map<String, Object> testMapGenerator(String config) throws IOException {
        Map<String, Object> props = JSONConfigReader
                .readConfig(this.getClass().getClassLoader().getResourceAsStream(config), Map.class);
        Map<String, Object> wrapper = new LinkedHashMap<>();
        wrapper.put(null, props);
        RandomJsonGenerator generator = new RandomJsonGenerator(wrapper);
        Map<String, Object> map = generator.generateJsonMap();
        return map;
    }

    public String testFlatJsonGenerator(String config) throws IOException {
        Map<String, Object> props = JSONConfigReader
                .readConfig(this.getClass().getClassLoader().getResourceAsStream(config), Map.class);
        Map<String, Object> wrapper = new LinkedHashMap<>();
        wrapper.put(null, props);
        RandomJsonGenerator generator = new RandomJsonGenerator(wrapper);
        String json = generator.generateFlattnedJson();
        return json;
    }

    public List<Map<String, Object>> testListGenerator(String config) throws IOException {
        List<Map<String, Object>> props = JSONConfigReader
                .readConfig(this.getClass().getClassLoader().getResourceAsStream(config), List.class);
        Map<String, Object> wrapper = new LinkedHashMap<>();
        wrapper.put(null, props);
        RandomJsonGenerator generator = new RandomJsonGenerator(wrapper);
        List<Map<String, Object>> list = generator.generateJsonList();
        return list;
    }

    public static void main(String... args) {
        String config = "config6.json";
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonGenerator gen = new JsonGenerator();
            log.info("Generated json Map: " + mapper.writeValueAsString(gen.testMapGenerator(config)));
            log.info("Generated flattened json Map: " + gen.testFlatJsonGenerator(config));
            JsonGenerator gen2 = new JsonGenerator();
            log.info("Generated json List: " + gen2.testListGenerator("config3.json"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}