List of usage examples for com.google.gson GsonBuilder create
public Gson create()
From source file:cl.niclabs.tscrypto.common.utils.Util.java
License:Open Source License
public static Gson GsonFactory(boolean pretty) { GsonBuilder gsonBuilder = new GsonBuilder(); if (pretty)/*from www. jav a 2 s . c om*/ gsonBuilder.setPrettyPrinting(); gsonBuilder.registerTypeAdapter(BigInteger.class, new BigIntegerBase64TypeAdapter()); gsonBuilder.registerTypeAdapter(TSMessage.class, new TSMessageParser()); return gsonBuilder.create(); }
From source file:classes.analysis.Analysis.java
License:Open Source License
/** * This static function returns a new object using the data contained in the * given JSON object (as String).//from ww w . j av a2 s. c om * * @param jsonString the JSON object * @return the new Object. */ public static Analysis fromJSON(JsonElement jsonString) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(NonProcessedData.class, getNonProcessedDataDeserializerInstance()); gsonBuilder.registerTypeAdapter(ProcessedData.class, getProcessedDataDeserializerInstance()); Gson gson = gsonBuilder.create(); Analysis analysis = gson.fromJson(jsonString, Analysis.class); //FINALLY, WE HAVE TO ORDER THE NON PROCESSED DATA BY STEP NUMBER Arrays.sort(analysis.getNonProcessedData()); return analysis; }
From source file:classes.analysis.non_processed_data.RAWdata.java
License:Open Source License
/** * This static function returns a new object using the data contained in the * given JSON object (as String)./* www.j a va 2s . c om*/ * * @param jsonString the JSON object * @return the new Object. */ public static RAWdata fromJSON(String jsonString) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(ExtractionMethod.class, getExtractionMethodDeserializerInstance()); Gson gson = gsonBuilder.create(); RAWdata step = gson.fromJson(jsonString, RAWdata.class); if (step.extractionMethod != null) { step.extractionMethod.setRawdataID(step.getStepID()); } return step; }
From source file:classes.analysis.non_processed_data.raw_data.ExtractionMethods.MassSpectrometry.java
License:Open Source License
/** * This static function returns a new object using the data contained in the * given JSON object (as String).//from ww w . j av a2 s . com * * @param jsonString the JSON object * @return the new Object. */ public static MassSpectrometry fromJSON(String jsonString) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(SeparationMethod.class, getSeparationMethodDeserializerInstance()); Gson gson = gsonBuilder.create(); MassSpectrometry massSpectrometry = gson.fromJson(jsonString, MassSpectrometry.class); if (massSpectrometry.getSeparationMethod() != null) { massSpectrometry .setSeparationMethodType(massSpectrometry.getSeparationMethod().getSeparationMethodType()); } return massSpectrometry; }
From source file:classes.analysis.non_processed_data.raw_data.ExtractionMethods.NuclearMagneticResonance.java
License:Open Source License
/** * This static function returns a new object using the data contained in the * given JSON object (as String)./*from w w w . j av a 2 s . c o m*/ * * @param jsonString the JSON object * @return the new Object. */ public static NuclearMagneticResonance fromJSON(String jsonString) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(SeparationMethod.class, getSeparationMethodDeserializerInstance()); Gson gson = gsonBuilder.create(); NuclearMagneticResonance nuclearMagneticResonance = gson.fromJson(jsonString, NuclearMagneticResonance.class); if (nuclearMagneticResonance.getSeparationMethod() != null) { nuclearMagneticResonance.setSeparationMethodType( nuclearMagneticResonance.getSeparationMethod().getSeparationMethodType()); } return nuclearMagneticResonance; }
From source file:cmput301.f13t01.elasticsearch.ESClient.java
License:GNU General Public License
/** * Creates the gson object that knows how to serialize Media. *//*from w w w.ja va 2 s . c o m*/ public ESClient() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Media.class, new InterfaceAdapter<Media<String>>()); gsonBuilder.registerTypeAdapter(Media.class, new InterfaceAdapter<Media<SpannableString>>()); gsonBuilder.registerTypeAdapter(Media.class, new InterfaceAdapter<Media>()); gson = gsonBuilder.create(); }
From source file:cn.taop.utils.GSONUtils.java
License:Apache License
/** * {@code JSON} ??// w ww. j a va 2 s . co m * * @param <T> ?? * @param json {@code JSON} * @param token {@code com.google.gson.reflect.TypeToken} * @param datePattern ?? * @return {@code JSON} * @since 1.0 */ public static <T> T fromJson(String json, TypeToken<T> token, String datePattern) { if (StringUtils.isBlank(json)) { return null; } GsonBuilder builder = new GsonBuilder(); if (StringUtils.isBlank(datePattern)) { datePattern = DEFAULT_DATE_PATTERN; } Gson gson = builder.create(); try { return gson.fromJson(json, token.getType()); } catch (Exception ex) { LOGGER.error(json + " ? " + token.getRawType().getName() + " !", ex); return null; } }
From source file:cn.taop.utils.GSONUtils.java
License:Apache License
/** * {@code JSON} ??<strong>?? {@code JavaBean} </strong> * // ww w . java2s.c o m * @param <T> ?? * @param json {@code JSON} * @param clazz ?? * @param datePattern ?? * @return {@code JSON} * @since 1.0 */ public static <T> T fromJson(String json, Class<T> clazz, String datePattern) { if (StringUtils.isBlank(json)) { return null; } GsonBuilder builder = new GsonBuilder(); if (StringUtils.isBlank(datePattern)) { datePattern = DEFAULT_DATE_PATTERN; } Gson gson = builder.create(); try { return gson.fromJson(json, clazz); } catch (Exception ex) { LOGGER.error(json + " ? " + clazz.getName() + " !", ex); return null; } }
From source file:cn.taop.utils.GSONUtils.java
License:Apache License
/** * ?{@code GsonBuilder} ???? {@code JSON} ? * <p />//from w w w . j a va2 s. c om * ????{@code JavaBean} <code>"{}"</code> ? * <code>"[]"</code> * * @param target * @param targetType * @param builder ?{@code Gson} * @return {@code JSON} ? * @since 1.1 */ public static String toJson(Object target, Type targetType, GsonBuilder builder) { if (target == null) return EMPTY_JSON; Gson gson = null; if (builder == null) { gson = new Gson(); } else { gson = builder.create(); } String result = EMPTY_JSON; try { if (targetType == null) { result = gson.toJson(target); } else { result = gson.toJson(target, targetType); } } catch (Exception ex) { LOGGER.warn( " " + target.getClass().getName() + " ? JSON ??", ex); if (target instanceof Collection<?> || target instanceof Iterator<?> || target instanceof Enumeration<?> || target.getClass().isArray()) { result = EMPTY_JSON_ARRAY; } } return result; }
From source file:co.aurasphere.botmill.fb.internal.util.json.AttachmentDeserializer.java
License:Open Source License
/** * Instantiates a new AttachmentDeserializer. *///from www . j a v a 2 s . com public AttachmentDeserializer() { GsonBuilder builder = new GsonBuilder(); builder.addDeserializationExclusionStrategy(new SkipDeserializationAnnotationExclusionStrategy()); delegateGson = builder.create(); }