Android Open Source - doubanbook4android Json Util






From Project

Back to project page doubanbook4android.

License

The source code is released under:

Eclipse Public License - v 1.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECI...

If you think the Android project doubanbook4android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.study.doubanbook_for_android.utils;
/*w w w.ja va 2s .  c  o  m*/
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.net.ParseException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;

/**
 * Gson?????????????????json?????????Gson???????
 * 
 * @author tezuka-pc
 */
public class JsonUtil {

  private static Gson gson = null;

  static {
    if (gson == null) {
      gson = new Gson();
    }
  }

  private JsonUtil() {

  }

  public static <T> T fromJsonObject(String json, Class<T> classOfT) {
    return gson.fromJson(json, classOfT);
  }

  public static <T> String toJsonObject(T t) {
    return gson.toJson(t);
  }

  public static <T> List<T> fromJsonArray(String json, Type type) {
    return gson.fromJson(json, type);
  }

  public static <T> String toJsonArray(List<T> list, Type type) {
    return gson.toJson(list, type);
  }

  /**
   * ?????????json???
   * 
   * @param ts
   * @return
   */
  public static String objectToJson(Object ts) {
    String jsonStr = null;
    if (gson != null) {
      jsonStr = gson.toJson(ts);
    }
    return jsonStr;
  }

  /**
   * ?????????json???(?????????)
   * 
   * @param ts
   * @return
   */
  public static String objectToJsonDateSerializer(Object ts,
      final String dateformat) {
    String jsonStr = null;
    gson = new GsonBuilder()
        .registerTypeHierarchyAdapter(Date.class,
            new JsonSerializer<Date>() {
              public JsonElement serialize(Date src,
                  Type typeOfSrc,
                  JsonSerializationContext context) {
                SimpleDateFormat format = new SimpleDateFormat(
                    dateformat);
                return new JsonPrimitive(format.format(src));
              }
            }).setDateFormat(dateformat).create();
    if (gson != null) {
      jsonStr = gson.toJson(ts);
    }
    return jsonStr;
  }

  /**
   * ?json?????????list??
   * 
   * @param jsonStr
   * @return
   */
  public static List<?> jsonToList(String jsonStr) {
    List<?> objList = null;
    if (gson != null) {
      Type type = new TypeToken<List<?>>() {
      }.getType();
      objList = gson.fromJson(jsonStr, type);
    }
    return objList;
  }

  /**
   * ?json?????????list??????????
   * 
   * @param jsonStr
   * @param type
   * @return
   */
  public static List<?> jsonToList(String jsonStr, Type type) {
    List<?> objList = null;
    if (gson != null) {
      objList = gson.fromJson(jsonStr, type);
    }
    return objList;
  }

  /**
   * ?json?????????map??
   * 
   * @param jsonStr
   * @return
   */
  public static Map<?, ?> jsonToMap(String jsonStr) {
    Map<?, ?> objMap = null;
    if (gson != null) {
      Type type = new TypeToken<Map<?, ?>>() {
      }.getType();
      objMap = gson.fromJson(jsonStr, type);
    }
    return objMap;
  }

  /**
   * ?json??????bean??
   * 
   * @param jsonStr
   * @return
   */
  public static Object jsonToBean(String jsonStr, Class<?> cl) {
    Object obj = null;
    if (gson != null) {
      obj = gson.fromJson(jsonStr, cl);
    }
    return obj;
  }

  /**
   * ?json??????bean??
   * 
   * @param jsonStr
   * @param cl
   * @return
   */
  @SuppressWarnings("unchecked")
  public static <T> T jsonToBeanDateSerializer(String jsonStr, Class<T> cl,
      final String pattern) {
    Object obj = null;
    gson = new GsonBuilder()
        .registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
          public Date deserialize(JsonElement json, Type typeOfT,
              JsonDeserializationContext context)
              throws JsonParseException {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            String dateStr = json.getAsString();
            try {
              return format.parse(dateStr);
            } catch (ParseException e) {
              e.printStackTrace();
            } catch (java.text.ParseException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
            return null;
          }
        }).setDateFormat(pattern).create();
    if (gson != null) {
      obj = gson.fromJson(jsonStr, cl);
    }
    return (T) obj;
  }

  /**
   * ????
   * 
   * @param jsonStr
   * @param key
   * @return
   */
  public static Object getJsonValue(String jsonStr, String key) {
    Object rulsObj = null;
    Map<?, ?> rulsMap = jsonToMap(jsonStr);
    if (rulsMap != null && rulsMap.size() > 0) {
      rulsObj = rulsMap.get(key);
    }
    return rulsObj;
  }

}




Java Source Code List

.UserListAdapter.java
com.study.doubanbook_for_android.activity.BaseActivity.java
com.study.doubanbook_for_android.activity.BaseP2RActivity.java
com.study.doubanbook_for_android.activity.BookDetailActivity.java
com.study.doubanbook_for_android.activity.BookListsActivity.java
com.study.doubanbook_for_android.activity.BookNoteListActivity.java
com.study.doubanbook_for_android.activity.CollectDetailActivity.java
com.study.doubanbook_for_android.activity.NoteAndUserDetailActivity.java
com.study.doubanbook_for_android.activity.SerchInputActivity.java
com.study.doubanbook_for_android.activity.TextActivity1.java
com.study.doubanbook_for_android.activity.UserDetailActivity.java
com.study.doubanbook_for_android.activity.UserListActivity.java
com.study.doubanbook_for_android.activity.UserNoteActivity.java
com.study.doubanbook_for_android.adapter.BookAdapter.java
com.study.doubanbook_for_android.adapter.CommentAdapter.java
com.study.doubanbook_for_android.adapter.UserNoteAdapter.java
com.study.doubanbook_for_android.api.NetUtils.java
com.study.doubanbook_for_android.api.SSLSocketFactoryEx.java
com.study.doubanbook_for_android.api.WrongMsg.java
com.study.doubanbook_for_android.auth.AccessToken.java
com.study.doubanbook_for_android.auth.DoubanDialogError.java
com.study.doubanbook_for_android.auth.DoubanDialog.java
com.study.doubanbook_for_android.auth.DoubanException.java
com.study.doubanbook_for_android.auth.DoubanOAuthListener.java
com.study.doubanbook_for_android.auth.DoubanParameters.java
com.study.doubanbook_for_android.auth.DoubanRequest.java
com.study.doubanbook_for_android.auth.Douban.java
com.study.doubanbook_for_android.auth.KeepToken.java
com.study.doubanbook_for_android.auth.SimpleDoubanOAuthListener.java
com.study.doubanbook_for_android.auth.Token.java
com.study.doubanbook_for_android.business.DoubanBusiness.java
com.study.doubanbook_for_android.callback.AsynCallback.java
com.study.doubanbook_for_android.imagedownloader.FileCache.java
com.study.doubanbook_for_android.imagedownloader.INetImageDownloader.java
com.study.doubanbook_for_android.imagedownloader.ImageCache.java
com.study.doubanbook_for_android.imagedownloader.ImageDownloaderListener.java
com.study.doubanbook_for_android.imagedownloader.ImageDownloader.java
com.study.doubanbook_for_android.model.Annotations.java
com.study.doubanbook_for_android.model.AuthorUser.java
com.study.doubanbook_for_android.model.BookItem.java
com.study.doubanbook_for_android.model.CollectBookMsg.java
com.study.doubanbook_for_android.model.CollectSuccessResult.java
com.study.doubanbook_for_android.model.CommentItem.java
com.study.doubanbook_for_android.model.Comment.java
com.study.doubanbook_for_android.model.CurrenUserCollection.java
com.study.doubanbook_for_android.model.GeneralNoteResult.java
com.study.doubanbook_for_android.model.GeneralResult.java
com.study.doubanbook_for_android.model.GeneralUserResult.java
com.study.doubanbook_for_android.model.ImageItem.java
com.study.doubanbook_for_android.model.Rating.java
com.study.doubanbook_for_android.model.RequestGrantScope.java
com.study.doubanbook_for_android.model.TagItem.java
com.study.doubanbook_for_android.model.URLMananeger.java
com.study.doubanbook_for_android.utils.EncodeUtils.java
com.study.doubanbook_for_android.utils.JsonParser.java
com.study.doubanbook_for_android.utils.JsonUtil.java
com.study.doubanbook_for_android.utils.UriUtils.java