Java Json Parse parse(String json, Class tClass)

Here you can find the source of parse(String json, Class tClass)

Description

parse

License

Open Source License

Declaration

public static <T> T parse(String json, Class<T> tClass) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.text.SimpleDateFormat;

public class Main {
    private static final ThreadLocal<ObjectMapper> mapper = new ThreadLocal<ObjectMapper>() {
        @Override/*  w w w .  j  a  va  2s  .  com*/
        protected ObjectMapper initialValue() {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
            return objectMapper;
        }
    };

    public static <T> T parse(String json, Class<T> tClass) {
        try {
            return mapper.get().readValue(json, tClass);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T parse(String json, TypeReference<T> typeReference) {
        try {
            return mapper.get().readValue(json, typeReference);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. getMapFromGson(String json)
  2. json2Obj(String json, Class clazz)
  3. jsonString(String s)
  4. parse(JSONObject jsonobj)
  5. parseDateFromJsonArray(final JsonArray array)
  6. parseJson(String jsonText, String[] params)
  7. parseJSONDate(String _dateStr)