Skip to content

Instantly share code, notes, and snippets.

@nikkatsa
Created January 8, 2017 20:51
Show Gist options
  • Save nikkatsa/4751d6bcc0cc0923e14849d97f83cf70 to your computer and use it in GitHub Desktop.
Save nikkatsa/4751d6bcc0cc0923e14849d97f83cf70 to your computer and use it in GitHub Desktop.
package com.nikoskatsanos.examples;
import com.nikoskatsanos.nkjutils.yalf.YalfLogger;
import org.python.core.PyObject;
import org.python.core.PyObjectDerived;
import org.python.core.PyString;
import org.python.modules.cPickle;
import org.python.util.PythonInterpreter;
/**
* @author nikkatsa
*/
public class UnPickler {
private static final YalfLogger log = YalfLogger.getLogger(UnPickler.class);
static {
System.setProperty("python.path", "/src/main/resources/pyObj");
}
private static final String pickle = "ccopy_reg\n" + "_reconstructor\n" + "p1\n" + "(ccom.nikoskatsanos.person\n" + "Person\n" + "p2\n" +
"c__builtin__\n" + "object\n" + "p3\n" + "NtRp4\n" + "(dp5\n" + "S'pAttributes'\n" + "p6\n" + "(dp7\n" + "S'Color'\n" + "p8\n" + "S'red'\n" +
"p9\n" + "sS'Car'\n" + "p10\n" + "S'BMW'\n" + "p11\n" + "ssS'pName'\n" + "p12\n" + "S'John Doe'\n" + "p13\n" + "sS'pAge'\n" + "p14\n" + "I50\n" +
"sS'pAddresses'\n" + "p15\n" + "(lp16\n" + "S'Work'\n" + "p17\n" + "aS'Home'\n" + "p18\n" + "asb.";
public static <T> T unPickle(final String pickle, final Class<T> clazz) {
final PyObjectDerived loads = (PyObjectDerived) cPickle.loads(new PyString(pickle));
return (T) loads.__tojava__(clazz);
}
public static void main(final String... args) {
PersonIfc personIfc = UnPickler.<PersonIfc>unPickle(pickle, PersonIfc.class);
log.info(personIfc.getName());
log.info("%d", personIfc.getAge());
log.info("%s", personIfc.getAddresses());
log.info("%s", personIfc.getAttributes());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment