Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package jadex.transformation.jsonserializer.processors;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import jadex.common.SReflect;
import jadex.common.transformation.IStringConverter;
import jadex.common.transformation.traverser.ITraverseProcessor;
import jadex.common.transformation.traverser.Traverser;
import jadex.common.transformation.traverser.Traverser.MODE;
import jadex.transformation.jsonserializer.JsonTraverser;
/**
*
*/
public class JsonCollectionProcessor extends AbstractJsonProcessor
{
/**
* Test if the processor is applicable.
* @param object The object.
* @param targetcl If not null, the traverser should make sure that the result object is compatible with the class loader,
* e.g. by cloning the object using the class loaded from the target class loader.
* @return True, if is applicable.
*/
protected boolean isApplicable(Object object, Type type, ClassLoader targetcl, JsonReadContext context)
{
Class> clazz = SReflect.getClass(type);
return (object instanceof JsonArray && SReflect.isSupertype(Collection.class, clazz) ||
(object instanceof JsonObject && ((JsonObject)object).get(JsonTraverser.COLLECTION_MARKER)!=null));
}
/**
* Test if the processor is applicable.
* @param object The object.
* @param targetcl If not null, the traverser should make sure that the result object is compatible with the class loader,
* e.g. by cloning the object using the class loaded from the target class loader.
* @return True, if is applicable.
*/
protected boolean isApplicable(Object object, Type type, ClassLoader targetcl, JsonWriteContext context)
{
Class> clazz = SReflect.getClass(type);
return SReflect.isSupertype(Collection.class, clazz);
}
/**
* Process an object.
* @param object The object.
* @param targetcl If not null, the traverser should make sure that the result object is compatible with the class loader,
* e.g. by cloning the object using the class loaded from the target class loader.
* @return The processed object.
*/
@SuppressWarnings("unchecked")
protected Object readObject(Object object, Type type, Traverser traverser, List conversionprocessors, List processors, IStringConverter converter, MODE mode, ClassLoader targetcl, JsonReadContext context)
{
JsonArray array;
JsonValue idx = null;
Class> clazz = SReflect.getClass(type);
Class> compclazz = SReflect.unwrapGenericType(type);
if(((JsonValue)object).isArray())
{
array = (JsonArray)object;
}
else
{
JsonObject obj = (JsonObject)object;
// compclazz = JsonTraverser.findClazzOfJsonObject(obj, targetcl);
array = (JsonArray)obj.get(JsonTraverser.COLLECTION_MARKER);
idx = (JsonValue)obj.get(JsonTraverser.ID_MARKER);
}
@SuppressWarnings("rawtypes")
Collection ret = (Collection)getReturnObject(object, clazz);
// traversed.put(object, ret);
// ((JsonReadContext)context).addKnownObject(ret);
if(idx!=null)
((JsonReadContext)context).addKnownObject(ret, idx.asInt());
for(int i=0; i conversionprocessors, List processors, IStringConverter converter, MODE mode, ClassLoader targetcl, JsonWriteContext wr)
{
wr.addObject(wr.getCurrentInputObject());
Class> clazz = SReflect.getClass(type);
if(wr.isWriteClass() || wr.isWriteId())
{
wr.write("{");
if(wr.isWriteClass())
{
wr.writeClass(clazz);
wr.write(",");
}
if(wr.isWriteId())
{
wr.writeId();
wr.write(",");
}
wr.writeString(JsonTraverser.COLLECTION_MARKER);
wr.write(":");
}
wr.write("[");
Collection> col = (Collection>)object;
Iterator> it = col.iterator();
for(int i=0; i0)
wr.write(",");
Object val = it.next();
traverser.doTraverse(val, val.getClass(), conversionprocessors, processors, converter, mode, targetcl, wr);
}
wr.write("]");
if(wr.isWriteClass() || wr.isWriteId())
{
wr.write("}");
}
return object;
}
/**
* Get the return object.
*/
@SuppressWarnings("rawtypes")
protected Object getReturnObject(Object object, Class> clazz)
{
Object ret = object;
try
{
ret = clazz.getDeclaredConstructor().newInstance();
}
catch(Exception e)
{
if(SReflect.isSupertype(Set.class, clazz))
{
ret = new HashSet();
}
else
{
ret = new ArrayList();
}
}
return ret;
}
}