package com.sync.changsha.gson;

import com.alibaba.fastjson.JSON;
import com.sync.changsha.Utils.GsonHelper;
import com.sync.changsha.gson.bean.MapBean;
import com.sync.changsha.gson.bean.MapBeanWrapper;
import com.sync.changsha.gson.bean.MockBean;
import com.sync.changsha.gson.bean.ResultResponse;
import com.sync.sz.core.common.utils.JsonUtil;
import java.io.IOException;
import java.util.Map;
import org.junit.Test;

/**
 * Created by Administrator on 2017/12/5.
 */
public class GsonTest {

  @Test
  public void testCastObject() {

    //Map<String, Object> map = new HashMap<>();
    //map.put("name", "李四");
    //map.put("age", "18");
    //map.put("nick", "20");
    //map.put("phone", "18664569168");

    //String jsonStr = GsonHelper.object2JsonStr(map);

    //String jsonStr = "{\"nick\":\"20\",\"phone\":\"18664569168\",\"name\":\"李四\",\"age\":\"18\"}";
    //
    //System.out.println(jsonStr);
    //
    //Object o = GsonHelper.convertEntity(jsonStr, Object.class);
    //System.out.println(o);
    //
    //MockBean mockBean = (MockBean) o;
    //
    //System.out.println(mockBean);

    String json = "{   \"data\": {"
        + "        \"momeyTotal\": 0,"
        + "        \"volunteerTotal\": 0,"
        + "        \"volunteerMap\": null,"
        + "        \"gainTotal\": 0,"
        + "        \"activityTotal\": 0,"
        + "        \"publishTotal\": 0"
        + "    },"
        + "    \"msg\": \"\","
        + "    \"pages\": 0,"
        + "    \"status\": 200,"
        + "    \"total\": 0"
        + "}";

    System.out.println(json);


    //ResultResponse<MapBean> map = GsonHelper.convertEntity(json,ResultResponse.class);

    MapBeanWrapper wrapper = GsonHelper.convertEntity(json, MapBeanWrapper.class);


    System.out.println(wrapper);
    System.out.println(wrapper.getData().getGainTotal());
    System.out.println(wrapper.getData().getMomeyTotal());
    System.out.println(wrapper.getData().getVolunteerMap());

  }

  public static void main(String[] args) {
    String jsonStr = "{\"nick\":\"20\",\"phone\":\"18664569168\",\"name\":\"李四\",\"age\":\"18\"}";

    System.out.println(jsonStr);

    Object o = GsonHelper.convertEntity(jsonStr, Object.class);
    System.out.println(o);

    MockBean mockBean = (MockBean) o;

    System.out.println(mockBean);
  }

  @Test
  public void jacksonTest() {
    String jsonStr = "{\"nick\":\"20\",\"phone\":\"18664569168\",\"name\":\"李四\",\"age\":\"18\"}";
    try {
      Object o = JsonUtil.jsonToObject(jsonStr);
      System.out.printf(o.toString());

      MockBean mockBean = (MockBean) o;

      System.out.println(mockBean);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

@@ -41,116 +41,38 @@
 com.
-alibaba.fastjson.JSON;%0Aimport com.sync.changsha.Utils.GsonHelper;%0Aimport com.sync.changsha.gson.bean.MapBean
+sync.changsha.Utils.GsonHelper
 ;%0Aim
@@ -291,16 +291,39 @@
 eption;%0A
+import java.util.List;%0A
 import j
@@ -1511,25 +1511,24 @@
 tln(json);%0A%0A
-%0A
     //Result
@@ -1689,17 +1689,16 @@
 lass);%0A%0A
-%0A
     Syst
@@ -1900,17 +1900,16 @@
 Map());%0A
-%0A
   %7D%0A%0A  p
@@ -2618,24 +2618,24 @@
 ackTrace();%0A
-
     %7D%0A  %7D%0A%7D%0A
@@ -2631,11 +2631,609 @@
    %7D%0A  %7D
+%0A%0A  @Test%0A  public void testArray() %7B%0A    String str = %22%7B%5Cn%22%0A        + %22    %5C%22data%5C%22: %5B%5Cn%22%0A        + %22        %7B%5Cn%22%0A        + %22            %5C%222017-12-13%5C%22: %5C%22A %E9%93%B6%E6%A1%A5%E7%A4%BE%E5%8C%BA%E5%B1%85%E5%A7%94%E4%BC%9A%E5%B1%85%E5%AE%B6%E5%85%BB%E8%80%81%E6%9C%8D%E5%8A%A1%E4%B8%AD%E5%BF%83%5C%5Cn%E5%9C%B0%E5%9D%80%EF%BC%9A %E9%93%B6%E6%9D%89%E8%B7%AF2%E9%99%84%E8%BF%91%5C%5CnB %E9%95%BF%5C%22%5Cn%22%0A        + %22        %7D%5Cn%22%0A        + %22    %5D,%5Cn%22%0A        + %22    %5C%22msg%5C%22: %5C%22%5C%22,%5Cn%22%0A        + %22    %5C%22pages%5C%22: 0,%5Cn%22%0A        + %22    %5C%22status%5C%22: 200,%5Cn%22%0A        + %22    %5C%22total%5C%22: 0%5Cn%22%0A        + %22%7D%22;%0A%0A    System.out.println(str);%0A%0A    ResultResponse%3CList%3CMap%3CString, String%3E%3E%3E resultResponse = GsonHelper.convertEntity(str,%0A        ResultResponse.class);%0A    System.out.printf(resultResponse.toString());%0A%0A  %7D
 %0A%7D%0A

test json2bean