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

net.razorvine.pyro.serializer.PyroExceptionSerpent Maven / Gradle / Ivy

There is a newer version: 4.4
Show newest version
package net.razorvine.pyro.serializer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.razorvine.pyro.PyroException;
import net.razorvine.serpent.IClassSerializer;

/**
 * Serpent extension to be able to serialize PyroException objects with Serpent.
 *  
 * @author Irmen de Jong ([email protected])
 */
public class PyroExceptionSerpent implements IClassSerializer {

	@SuppressWarnings("unchecked")
	public static Object FromSerpentDict(Map dict) {
		Object[] args = (Object[]) dict.get("args");
		PyroException ex = new PyroException((String)args[0]);
		Map attrs = (Map)dict.get("attributes");
		// we can only deal with a possible _pyroTraceback attribute
		if(attrs.containsKey("_pyroTraceback"))
		{
			Object tb = attrs.get("_pyroTraceback");
			// if the traceback is a list of strings, create one string from it
			if(tb instanceof List) {
				StringBuilder sb=new StringBuilder();
				for(Object line: (List)tb) {
					sb.append(line);
				}	
				ex._pyroTraceback=sb.toString();
			} else {
				ex._pyroTraceback=(String)tb;
			}
		}
		return ex;
	}

	public Map convert(Object obj) {
		PyroException ex = (PyroException) obj;
		Map dict = new HashMap();
		// {'attributes':{},'__exception__':True,'args':('hello',),'__class__':'PyroError'}
		dict.put("__class__", "PyroError");
		dict.put("__exception__", true);
		dict.put("args", new Object[] {ex.getMessage()});
		Map attrMap = new HashMap();
		if(ex._pyroTraceback!=null)
			attrMap.put("_pyroTraceback", new String[]{ ex._pyroTraceback} );   // transform single string back into list
		dict.put("attributes", attrMap);
		return dict;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy