All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.opendevl.OrderJson Maven / Gradle / Ivy

The newest version!
package com.github.opendevl;

import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

public class OrderJson {
	
	Type type = new TypeToken>(){}.getType();
	
	Map origMap = null;
	
	Map jsonPre = null;
	Map jsonArr = null;
	Map jsonObj = null;
	
	Gson jsonToFro = null;
	
	public OrderJson(){
		
		jsonToFro = new Gson();
	}
	
	public JsonElement orderJson(JsonElement ele){
		
		origMap  = new LinkedHashMap();
		
		jsonPre = new LinkedHashMap();
		jsonArr = new LinkedHashMap();
		jsonObj = new LinkedHashMap();
		
		//converting JsonElement to Map
		origMap = jsonToFro.fromJson(ele, type);
		
		//Iterating the Map object to to get type of Object
		for(Map.Entry entry : origMap.entrySet()){
			
			try{
				//adding check if value of key in json is null
				if(entry.getValue() == null || 
						entry.getValue().getClass().getSimpleName().equals("ArrayList")){
					
					//if Object is of type ArrayList push it to jsonArr Map
					jsonArr.put(entry.getKey(), entry.getValue());
					
				}else{
					
					//if Object is of type Premitive push it to jsonPre.
					jsonPre.put(entry.getKey(), entry.getValue());
				}
			}catch(Exception ex){
				ex.printStackTrace();
			}
			//Yet to descide about if type is of JsonObject
			
		}
		
		/* Keeping Order - 
		 * 		1) JSON premitive
		 * 		2) JSON Array
		 * 		3) JSON Object ( order of JSON Object is yet to be descided)
		 * */
		
		//appending jsonArr map to jsonPre map in order to mantain order.
		jsonPre.putAll(jsonArr);
		
		//reconstructing the JSON from Map Objects and returning
		
		return jsonToFro.toJsonTree(jsonPre, LinkedHashMap.class);
		
	}
	
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy