package kr.co.swh.lecture.opensource.gson;

import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * <pre>
 * kr.co.swh.lecture.opensource.gson
 * Company.java
 *
 * 설명 :GSON 예제
 * 	ref. http://1004lucifer.blogspot.kr/2015/04/javagson-gson-java-json.html
 * </pre>
 * 
 * @since : 2018. 1. 29.
 * @author : tobby48
 * @version : v1.0
 */
public class GsonMain {
	public static void main(String[] argv) {
		Company company = Company.getCompanyDummy();

		System.out.println("========= Object => Json ==========");
		String company2Json = new Gson().toJson(company);
		System.out.println(company2Json);

		System.out.println("========= Json => Object =========");
		Company json2Company = new Gson().fromJson(company2Json, Company.class);
		printCompanyObject(json2Company);

		System.out.println("========= Object => Json =========");
		String company2JsonIsNull = new GsonBuilder().serializeNulls().create().toJson(company);
		System.out.println(company2JsonIsNull);

		System.out.println("========= Json => Object =========");
		Company json2CompanyIsNull = new Gson().fromJson(company2Json, Company.class);
		printCompanyObject(json2CompanyIsNull);
	}

	private static void printCompanyObject(Company company) {
		List<Person> personList = company.getEmployees();
		System.out.println("userName: " + company.getName());
		for (Person person : personList) {
			System.out.println(person);
		}
	}
}
