List of usage examples for com.google.gson GsonBuilder GsonBuilder
public GsonBuilder()
From source file:com.ab.util.AbJsonUtil.java
License:Apache License
/** * /*from ww w . ja v a 2 s . com*/ * ??json. * @param json * @param typeToken new TypeToken<ArrayList<?>>() {}; * @return */ public static List<?> fromJson(String json, TypeToken typeToken) { List<?> list = null; try { GsonBuilder gsonb = new GsonBuilder(); Gson gson = gsonb.create(); Type type = typeToken.getType(); list = gson.fromJson(json, type); } catch (Exception e) { e.printStackTrace(); } return list; }
From source file:com.ab.util.AbJsonUtil.java
License:Apache License
/** * /* www. j av a 2s.c o m*/ * ??json. * @param json * @param clazz * @return */ public static Object fromJson(String json, Class clazz) { Object obj = null; try { GsonBuilder gsonb = new GsonBuilder(); Gson gson = gsonb.create(); obj = gson.fromJson(json, clazz); } catch (Exception e) { e.printStackTrace(); } return obj; }
From source file:com.acc.java.manager.base.BaseJsonManager.java
License:Open Source License
public BaseJsonManager() { this.gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); }
From source file:com.accuweather.bean.InitBean.java
@PostConstruct public void init() { System.out.println("en iniiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiit"); Client client;/* ww w .java2 s. c om*/ WebTarget target; LocationDto locationDto; String region, regionCode, pais, paisCode, ciudad, ciudadCode; RegionBean regionBean = new RegionBean(); PaisBean paisBean = new PaisBean(); CiudadBean ciudadBean = new CiudadBean(); Gson transformer = new GsonBuilder().create(); client = ClientBuilder.newClient(); target = client.target( "http://dataservice.accuweather.com/locations/v1/regions?apikey=UgipgbtgQCzFDQvsCHCpmlE3s0UDH5nG"); JsonArray responseRegiones = target.request(MediaType.APPLICATION_JSON).get(JsonArray.class); for (int i = 0; i < /*responseRegiones.size()*/1; i++) { regionBean = transformer.fromJson(responseRegiones.getJsonObject(i).toString(), RegionBean.class); regionCode = "NAM";//regionBean.getID(); region = "North America";//regionBean.getLocalizedName(); target = client.target("http://dataservice.accuweather.com/locations/v1/countries/" + regionCode + "?apikey=UgipgbtgQCzFDQvsCHCpmlE3s0UDH5nG"); JsonArray responsePaises = target.request(MediaType.APPLICATION_JSON).get(JsonArray.class); for (int j = 0; j < responsePaises.size(); j++) { paisBean = transformer.fromJson(responsePaises.getJsonObject(j).toString(), PaisBean.class); paisCode = paisBean.getID(); pais = paisBean.getLocalizedName(); target = client.target("http://dataservice.accuweather.com/locations/v1/adminareas/" + paisCode + "?apikey=UgipgbtgQCzFDQvsCHCpmlE3s0UDH5nG"); JsonArray responseCiudades = target.request(MediaType.APPLICATION_JSON).get(JsonArray.class); for (int k = 0; k < responseCiudades.size(); k++) { ciudadBean = transformer.fromJson(responseCiudades.getJsonObject(k).toString(), CiudadBean.class); ciudadCode = ciudadBean.getID(); ciudad = ciudadBean.getLocalizedName(); locationDto = new LocationDto(); locationDto.setRegion_code(regionCode); locationDto.setRegion(region); locationDto.setPais_code(paisCode); locationDto.setPais(pais); locationDto.setArea_code(ciudadCode); locationDto.setArea(ciudad); System.out.println(locationDto.getArea()); locationBean.createLocation(locationDto); } } } }
From source file:com.acentera.utils.ProjectsHelpers.java
License:Open Source License
public static JSONObject getProjectsAsJson(Project projects) { //Make sure to do an hibernate commit before we get the projectasjson.. if (SecurityController.canViewProject(projects.getId())) { Gson g = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); Set<ProjectProviders> providers = projects.getProviders(); String projectAsJson = g.toJson(projects); JSONObject res = new JSONObject(); res.put("projects", projectAsJson); res.getJSONObject("projects").put("created_just_now", 1); return res; } else {//from www . ja v a 2s. c o m JSONObject res = new JSONObject(); res.put("projects", new JSONObject()); return res; } }
From source file:com.actimem.blog.gson.basic.PrettyPrintWriteDemo.java
License:Apache License
public static void main(String[] args) throws IOException { Company company = createCompany();//from w w w. j a v a 2 s .c o m Gson gson = new GsonBuilder().setPrettyPrinting().create(); gson.toJson(company, System.out); }
From source file:com.actimem.blog.gson.customtypeadapters.TypeAdapterDemo.java
License:Apache License
public static void main(String[] args) throws IOException { Company company = createCompany();/*from w ww. j av a2 s.co m*/ Gson gson = new GsonBuilder().registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter()) .registerTypeAdapter(CompanyId.class, new CompanyIdTypeAdapter()).create(); String json = gson.toJson(company); System.out.println(json); Company fromJson = gson.fromJson(json, Company.class); System.out.println(fromJson); }
From source file:com.actimem.blog.gson.customtypeadapters.TypeAdapterFactoryDemo.java
License:Apache License
public static void main(String[] args) throws IOException { Company company = createCompany();/*from www . ja v a 2 s . c o m*/ CustomTypeAdapterFactory factory = new CustomTypeAdapterFactory(); factory.add(DateTime.class, new DateTimeTypeAdapter()); factory.add(CompanyId.class, new CompanyIdTypeAdapter()); Gson gson = new GsonBuilder().registerTypeAdapterFactory(factory).create(); String json = gson.toJson(company); System.out.println(json); Company fromJson = gson.fromJson(json, Company.class); System.out.println(fromJson); }
From source file:com.actimem.blog.gson.customtypes.CustomTypesDemo.java
License:Apache License
public static void main(String[] args) throws IOException { Company company = createCompany();//from ww w .jav a 2 s. co m Gson gson = new GsonBuilder().registerTypeAdapter(DateTime.class, new DateTimeAdapter()) .registerTypeAdapter(CompanyId.class, new CompanyIdAdapter()).create(); String json = gson.toJson(company); System.out.println(json); Company fromJson = gson.fromJson(json, Company.class); System.out.println(fromJson); }
From source file:com.actimem.blog.gson.dates.FormattedDateDemo.java
License:Apache License
public static void main(String[] args) throws IOException { Company company = new Company(); company.setName("Actimem"); company.setFounded(new GregorianCalendar(2015, 5, 20).getTime()); company.setUpdatedTs(new Date()); Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create(); String json = gson.toJson(company); System.out.println(json);//from ww w. java2 s. c o m Company company2 = gson.fromJson(json, Company.class); System.out.println(company2.toString()); }