Android GsonBuilder - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android GsonBuilder class.

This code example shows how to use the following methods:create, registerTypeAdapter, serializeNulls, setFieldNamingPolicy
	 *            whether nulls should be serialized 
	 * 
	 * @return created gson, never null 
	 */ 
	public static final Gson createGson(final boolean serializeNulls) {
		final GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(Date.class, new DateFormatter());
		builder.registerTypeAdapter(Event.class, new EventFormatter());
		builder.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
		if (serializeNulls)
			builder.serializeNulls();
		return builder.create();
	} 
 
	/** 
	 * Get reusable pre-configured {@link Gson} instance 
	 * 
	 * @return Gson instance 
	 */ 
	public static final Gson getGson() {
9
CodeRank
This code example shows how to use the following methods:setDateFormat
 
public class GsonFactory 
{ 
	static public Gson createGson()
	{ 
		final GsonBuilder builder = new GsonBuilder();
		final Gson gson = builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss-'07:00'")
						.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
						.create();
		return gson;
	} 
} 
6
CodeRank
This code example shows how to use the following methods:create, registerTypeAdapter
 
	/** 
	 * @return 
	 */ 
	private Gson buildGson() {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder
				.registerTypeAdapter(Object.class, new NaturalDeserializer());
		Gson gson = gsonBuilder.create();
		return gson;
	} 
} 
6
CodeRank
This code example shows how to use the following methods:create, registerTypeHierarchyAdapter
	public static Gson createGsonWithContentValuesAdapter(Class<?> clazz) {
		return createGsonWithContentValuesAdapter(new ContentValuesAdapter(clazz));
	} 
	 
	public static Gson createGsonWithContentValuesAdapter(ContentValuesAdapter contentValuesAdaper) {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeHierarchyAdapter(ContentValues.class, contentValuesAdaper);
		return gsonBuilder.create();
	} 
	 
	@Override 
	public Result execute(DataSourceRequest dataSourceRequest, IDataSource dataSource, InputStream inputStream) throws Exception {
		InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader, 8192);
		try { 
			return process(getGson(), bufferedReader);	
		} catch (JsonIOException exception){
            throw new IOException(exception);
        } finally { 
			IOUtils.close(inputStream);
6
CodeRank
This code example shows how to use the following methods:create, registerTypeAdapter, setPrettyPrinting
 */ 
@Service 
public final class GoogleGsonSerializerHelper { 
 
    public Gson createGsonBuilder(final boolean prettyPrint) {
        final GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
        builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
        builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
        if (prettyPrint) {
            builder.setPrettyPrinting();
        } 
        return builder.create();
    } 
 
    public Gson createGsonBuilderWithParameterExclusionSerializationStrategy(final Set<String> supportedParameters,
            final boolean prettyPrint, final Set<String> responseParameters) {
 
        final Set<String> parameterNamesToSkip = new HashSet<String>();
 
6
CodeRank
This code example shows how to use the following methods:create, registerTypeAdapter
	 
	private Gson gson;
	 
	@Before 
	public void setUp() throws Exception { 
		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(OpenshiftDataList.class, new OpenshiftDataListDeserializer());
		gson = builder.create();
	} 
 
	 
	@Test 
	public void testSerialization() { 
		UserResource user = new UserResource();
		user.setLogin("openshiftandroid@gmail.com");
		 
		String json = gson.toJson(user);
		assertNotNull(json);
		System.out.println(json);
	} 
6
CodeRank
This code example shows how to use the following methods:create, registerTypeAdapter, serializeNulls
	public void setAuth(Authorization auth) {
		this.auth = auth;
	} 
	 
	private Gson getGson() {
		GsonBuilder gsonBuilder = new GsonBuilder();
		 
		gsonBuilder.serializeNulls();
		 
		//GSON对date的序列化和反序列化处理 
		GMTDateGsonAdapter dateGsonAdapter = new GMTDateGsonAdapter();
		gsonBuilder.registerTypeAdapter(java.util.Date.class, dateGsonAdapter);
		gsonBuilder.registerTypeAdapter(java.sql.Timestamp.class, dateGsonAdapter);
		gsonBuilder.registerTypeAdapter(java.sql.Date.class, dateGsonAdapter);
		 
		return gsonBuilder.create();
	} 
} 
6
CodeRank
This code example shows how to use the following methods:create, registerTypeAdapter, serializeNulls
public final class CommandProcessingResultJsonSerializer { 
 
    private final Gson gson;
 
    public CommandProcessingResultJsonSerializer() { 
        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
        builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter());
        builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter());
        builder.serializeNulls();
         
        this.gson = builder.create();
    } 
 
    public String serialize(final Object result) {
        String returnedResult = null;
        final String serializedResult = this.gson.toJson(result);
        if (!"null".equalsIgnoreCase(serializedResult)) {
            returnedResult = serializedResult;
        } 
6
CodeRank
This code example shows how to use the following methods:create, registerTypeAdapter, setPrettyPrinting
 
   private Gson gson = null;
 
   @Before 
   public void init() { 
      GsonBuilder gsonBuilder = new GsonBuilder();
      gsonBuilder.setPrettyPrinting(); // only for debug/development
      gsonBuilder.registerTypeAdapter(Category.class, new CategorySerializer());
      gsonBuilder.registerTypeAdapter(Category.class, new CategoryDeserializer());
      gsonBuilder.registerTypeAdapter(Tool.class, new ToolSerializer());
      gsonBuilder.registerTypeAdapter(Tool.class, new ToolDeserializer());
      gsonBuilder.registerTypeAdapter(Feature.class, new FeatureSerializer());
      gsonBuilder.registerTypeAdapter(Feature.class, new FeatureDeserializer());
 
//      gsonBuilder.registerTypeAdapter(QueryAllArticles.class, new QueryAllArticlesDeserializer()); 
//      gsonBuilder.registerTypeAdapter(QueryChannel.class, new QueryChannelDeserializer()); 
//      gsonBuilder.registerTypeAdapter(QuerySource.class, new QuerySourceDeserializer()); 
 
      gson = gsonBuilder.create();
   } 

6
CodeRank
This code example shows how to use the following methods:registerTypeAdapter
	 
	@Override 
	public GsonBuilder extendBuilder(GsonBuilder gp) {
		// Serialization: 
		if (null != _dataMap) {
			gp = _dataMap.extendBuilder(gp);
		} 
		// Deserialization: 
		else if ((null != _type1) || (null != _listType1)) {
			gp = gp.registerTypeAdapter(ResponsePojo.class, new ResponsePojoDeserializer(_type1, _listType1));
		} 
		else if ((null != _type2) || (null != _listType2)) {
			gp = gp.registerTypeAdapter(ResponsePojo.class, new ResponsePojoDeserializer(_type2, _listType2, _deserMap));
		} 
		else { 
			gp = gp.registerTypeAdapter(ResponsePojo.class, new ResponsePojoDeserializer());
		} 
		return gp;
	} 
 
	protected static class ResponsePojoDeserializer implements JsonDeserializer<ResponsePojo> 
5
CodeRank

Related Android GsonBuilder Questions & Answers:

See Code Examples for Android GsonBuilder Methods: