package com.sharefile.api.gson.auto;

import java.lang.reflect.Type;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sharefile.api.SFSDKDefaultAccessScope;
import com.sharefile.api.constants.SFKeywords;
import com.sharefile.api.models.SFGroup;
import com.sharefile.api.models.SFItem;
import com.sharefile.api.models.SFODataObject;
import com.sharefile.api.models.SFUser;
import com.sharefile.api.utils.Utils;
import com.sharefile.java.log.SLog;

/**
 *  Class to group the customized serialization of SFObjects to json.
 */
public class SFCustomSerializer 
{
	private static void addIfNonEmpty(JsonObject jsonObject , String key, String value)
	{
		if( !Utils.isEmpty(key) && !Utils.isEmpty(value))
		{
			jsonObject.addProperty(key, value);
		}
	}
	
	@SFSDKDefaultAccessScope static JsonElement serialize(SFODataObject sfODataObject, Type typeOfObject)
	{
		String str = SFDefaultGsonParser.serialize(typeOfObject, sfODataObject);
		
		if(str==null)
		{
			return null;
		}
		
		JsonParser parser = new JsonParser();
		return parser.parse(str);
	}
	
	@SFSDKDefaultAccessScope static JsonObject serialize(SFItem sfItem)
	{		
		if(sfItem == null)
		{			
			return null;
		}
								
		JsonObject jsonObject = new JsonObject();
		
		addIfNonEmpty(jsonObject, SFKeywords.Id, sfItem.getId());
						
		return jsonObject;
	}
	
	@SFSDKDefaultAccessScope static JsonObject serialize(SFUser sfuser)
	{				
		if(sfuser == null)
		{						
			return null;
		}
				
		JsonObject jsonObject = new JsonObject();
				
		addIfNonEmpty(jsonObject, SFKeywords.Id, sfuser.getId());
		addIfNonEmpty(jsonObject, "Email", sfuser.getEmail());			
		addIfNonEmpty(jsonObject, "FirstName", sfuser.getFirstName());				
		addIfNonEmpty(jsonObject, "LastName", sfuser.getLastName());				
		addIfNonEmpty(jsonObject, "Company", sfuser.getCompany());
								
		return jsonObject;
	}
	
	@SFSDKDefaultAccessScope static JsonObject serialize(SFGroup sfgroup)
	{				
		if(sfgroup == null)
		{						
			return null;
		}
				
		JsonObject jsonObject = new JsonObject();
		
		addIfNonEmpty(jsonObject, SFKeywords.Id, sfgroup.getId());
		addIfNonEmpty(jsonObject, "Email", sfgroup.getEmail());				
		
		return jsonObject;
	}
}
@@ -61,16 +61,37 @@
 ct.Type;
+%0Aimport java.net.URI;
 %0A%0Aimport
@@ -829,25 +829,672 @@
 ue);%0A%09%09%7D%0A%09%7D%0A
-%09
+%0A    private static void addIfNonEmpty(JsonObject jsonObject , String key, URI value)%0A    %7B%0A        if( !Utils.isEmpty(key) && value!=null && !Utils.isEmpty(value.toString()))%0A        %7B%0A            jsonObject.addProperty(key, value.toString());%0A        %7D%0A    %7D%0A%0A    private static void add(JsonObject jsonObject , String key, String value, String defaultValue)%0A    %7B%0A        if( !Utils.isEmpty(key))%0A        %7B%0A            if(!Utils.isEmpty(value))%0A            %7B%0A                jsonObject.addProperty(key, value);%0A            %7D%0A            else%0A            %7B%0A                jsonObject.addProperty(key, defaultValue);%0A            %7D%0A        %7D%0A    %7D%0A
 %0A%09@SFSDKDefa
@@ -1594,20 +1594,25 @@
 %7B%0A%09%09
-String str =
+/*%0A%09%09   dont call
  SFD
@@ -1674,117 +1674,208 @@
 ;%0A%09%09
-%0A%09%09if(str==null)%0A%09%09%7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09%0A%09%09JsonParser parser = new JsonParser();%0A%09%09return parser.parse(str
+   or it leads to StackOverflow due to infinite recursive calls back here.%0A%09%09*/%0A        throw new RuntimeException(%22Implement the custom serializer for: %22 + sfODataObject.getClass().getCanonicalName()
 );%0A%09
@@ -2112,24 +2112,168 @@
 m.getId());%0A
+        add(jsonObject, SFKeywords.Description, sfItem.getDescription(), %22%22);%0A        addIfNonEmpty(jsonObject,SFKeywords.URL,sfItem.geturl());%0A
 %09%09%09%09%09%09%0A%09%09ret

Fix the stack overflow in SFCustomSerializer by throwing an exception explaining which class needs to implement custom serialize.