Android Gson - 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 Gson class.

This code example shows how to use the following methods:
    // CONSTRUCTOR 
    // ============================================================================================ 
    public GsonObjectPersister(Application application, Class<T> clazz,
        String factoryPrefix) {
        super(application, clazz, factoryPrefix);
        this.gson = new Gson();
    } 
 
    // ============================================================================================ 
    // METHODS 
    // ============================================================================================ 
 
    @Override 
    protected T deserializeData(String json) {
        return gson.fromJson(json, getHandledClass());
    } 
 
    @Override 
    protected void saveData(T data, Object cacheKey) throws IOException,
        CacheSavingException { 
9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:fromJson, toJson
		systemDataModel.setFreeMemory(20);
		systemDataModel.setCpuUsedPercentage(20);
		List<SystemDataModel> lists = Lists.newArrayList();
		lists.add(systemDataModel);
		lists.add(systemDataModel);
		Gson gson = new Gson();
		String json = gson.toJson(lists);
		ArrayList<SystemDataModel> fromJson = gson.fromJson(json, getTypeToken());
		System.out.println(fromJson.get(0).getClass());
	} 
 
	private Type getTypeToken() {
		return new TypeToken<ArrayList<SystemDataModel>>() {
			private static final long serialVersionUID = 1L;
		}.getType(); 
	} 
} 
9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:fromJson
	 * @param json 
	 * @param type 
	 * @return instance of type 
	 */ 
	public static final <V> V fromJson(String json, Type type) {
		return GSON.fromJson(json, type);
	} 
 
	/** 
	 * Convert content of reader to given type 
	 * 
	 * @param reader 
	 * @param type 
	 * @return instance of type 
	 */ 
	public static final <V> V fromJson(Reader reader, Class<V> type) {
		return GSON.fromJson(reader, type);
	} 
 
	/** 
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:
public class Handshake { 
    private final ClientStub clientStub;
 
    protected static final Logger logger = Logger.getLogger("adapter"); 
 
    protected final Gson gson = new Gson();
 
    public Handshake(ClientStub clientStub) {
        this.clientStub = clientStub;
    } 
 
    // execute handshake with client. 
    // 1. discovery: issue uuid; 
    // 2. main connect: create connection 
    public ClientConnection execute(Socket s) {
        byte[] v;
 
        try { 
            ClientConnection conn = null;
 
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:fromJson
        } 
    } 
 
    private Map<String, Object> getModelMap(String testName) {
        String json = readFile(testName + ".json");
        Gson gson = new Gson();
        Type type = new TypeToken<Map<String, Object>>() {
        }.getType();
        Map<String, Object> model = gson.fromJson(json, type);
        if (model == null) {
            model = new HashMap<String, Object>();
        } 
        return model;
    } 
 
    private String readFile(String fileName) {
        try { 
            return FileUtils.readFileToString(new File(TestFileHelper.getCompilerErrorsResourcePath(fileName)));
        } catch (Exception e) {
            // e.printStackTrace(); 
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:
        // noop 
        return null; 
    } 
 
    public String toJson() {
        return new Gson().toJson(this);
    } 
} 
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:fromJson
		monitor.stop();
 
		// JSON parsing 
		JsonObject obj = (JsonObject)  resp.getJson();
		JsonArray monitors = obj.getAsJsonObject("play.CorePlugin").getAsJsonArray("monitors");
		Gson gson = new Gson();
        Type collectionType = new TypeToken<Collection<StatMonitor>>(){}.getType();
        List<StatMonitor> stats = gson.fromJson(monitors, collectionType);
         
        // Into cache 
        Cache.set("MonitorStats", stats);
	} 
} 
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:fromJson
        byte[] result = EncryptionUtils.AES.encrypt(json, symmetricKeyBytes);
        ciphertext = CMAccountUtils.encodeHex(result);
    } 
 
    public static EncryptedMessage fromJson(String json) {
        return new Gson().fromJson(json, EncryptedMessage.class);
    } 
} 
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:toJson
		return current;
	} 
	 
	public void save(Context context)
	{ 
		Gson gson = new Gson();
		String json = gson.toJson(this);
		 
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		//SharedPreferences prefs = activity.getSharedPreferences("settings", 0); 
	    SharedPreferences.Editor editor = prefs.edit();
	    //editor.putString("json", json); 
	    editor.putString("settings", json);
	    editor.commit();
	} 
	 
	public static void load(Context context)
	{ 
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String json = prefs.getString("settings","");
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:fromJson
	/** 
	 * Verify repository with no generated id 
	 */ 
	@Test 
	public void noGeneratedId() { 
		SearchRepository repo = gson.fromJson("{}", SearchRepository.class);
		assertNull(repo.generateId());
		repo = gson.fromJson("{name:\"\", owner:\"use\"}",
				SearchRepository.class); 
		assertNull(repo.generateId());
		repo = gson.fromJson("{owner:\"\"}", SearchRepository.class);
		assertNull(repo.generateId());
	} 
 
	/** 
	 * Test non-mutable created at date 
	 */ 
	@Test 
	public void getCreatedAtReferenceMutableObject() { 
		SearchRepository repository = gson.fromJson(
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java

Related Android Gson Questions & Answers:

See Code Examples for Android Gson Methods: