/*
 * KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
 *
 * This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
 * only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
 * arose from modification of the original source, or other redistribution of this source
 * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
 */
package me.chanjar.weixin.mp.util.json;

import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;

import java.lang.reflect.Type;
import java.util.Date;

public class WxMpMaterialNewsGsonAdapter implements JsonSerializer<WxMpMaterialNews>, JsonDeserializer<WxMpMaterialNews> {

  @Override
  public JsonElement serialize(WxMpMaterialNews wxMpMaterialNews, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject newsJson = new JsonObject();

    JsonArray articleJsonArray = new JsonArray();
    for (WxMpMaterialNews.WxMpMaterialNewsArticle article : wxMpMaterialNews.getArticles()) {
      JsonObject articleJson = WxMpGsonBuilder.create().toJsonTree(article, WxMpMaterialNews.WxMpMaterialNewsArticle.class).getAsJsonObject();
      articleJsonArray.add(articleJson);
    }
    newsJson.add("articles", articleJsonArray);

    return newsJson;
  }

  @Override
  public WxMpMaterialNews deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    WxMpMaterialNews wxMpMaterialNews = new WxMpMaterialNews();
    JsonObject json = jsonElement.getAsJsonObject();
    if (json.get("news_item") != null && !json.get("news_item").isJsonNull()) {
      JsonArray articles = json.getAsJsonArray("news_item");
      for (JsonElement article1 : articles) {
        JsonObject articleInfo = article1.getAsJsonObject();
        WxMpMaterialNews.WxMpMaterialNewsArticle article = WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMaterialNews.WxMpMaterialNewsArticle.class);
        wxMpMaterialNews.addArticle(article);
      }
    }

    if (json.get("create_time") != null && !json.get("create_time").isJsonNull()) {
      Date createTime = new Date(GsonHelper.getAsLong(json.get("create_time")));
      wxMpMaterialNews.setCreatedTime(createTime);
    }

    if (json.get("update_time") != null && !json.get("update_time").isJsonNull()) {
      Date updateTime = new Date(GsonHelper.getAsLong(json.get("update_time")));
      wxMpMaterialNews.setUpdatedTime(updateTime);
    }

    return wxMpMaterialNews;
  }
}

@@ -646,16 +646,51 @@
 t.Type;%0A
+import java.text.SimpleDateFormat;%0A
 import j
@@ -704,16 +704,16 @@
 l.Date;%0A
-
 %0Apublic 
@@ -1383,24 +1383,410 @@
 sonArray);%0A%0A
+    if (wxMpMaterialNews.getCreatedTime() != null) %7B%0A      newsJson.addProperty(%22create_time%22,%0A        SimpleDateFormat.getDateTimeInstance().format(wxMpMaterialNews.getCreatedTime()));%0A    %7D%0A%0A    if (wxMpMaterialNews.getUpdatedTime() != null) %7B%0A      newsJson.addProperty(%22update_time%22,%0A        SimpleDateFormat.getDateTimeInstance().format(wxMpMaterialNews.getUpdatedTime()));%0A    %7D%0A%0A
     return n
@@ -2703,32 +2703,38 @@
 (%22create_time%22))
+* 1000
 );%0A      wxMpMat
@@ -2854,32 +2854,32 @@
 isJsonNull()) %7B%0A
-
       Date updat
@@ -2940,16 +2940,22 @@
 _time%22))
+* 1000
 );%0A     

fix time