Esempio n. 1
0
 public void append_json_repr(StringBuffer buf, PyObject py_obj) throws JSONEncodeError {
   if (py_obj instanceof PyString) append_json_string_repr(buf, ((PyString) py_obj).toString());
   // Must test for PyBoolean before PyInteger because former is a subclass of latter.
   else if (py_obj instanceof PyBoolean)
     buf.append(((PyBoolean) py_obj).getBooleanValue() ? "true" : "false");
   else if (py_obj instanceof PyInteger)
     buf.append(Integer.toString(((PyInteger) py_obj).getValue()));
   else if (py_obj instanceof PyLong) {
     String repr = ((PyLong) py_obj).__repr__().toString();
     buf.append(repr.substring(0, repr.length() - 1));
   } else if (py_obj instanceof PyFloat)
     buf.append(Double.toString(((PyFloat) py_obj).getValue()));
   else if (py_obj instanceof PyStringMap) append_json_string_map_repr(buf, (PyStringMap) py_obj);
   else if (py_obj instanceof PyDictionary)
     append_json_dictionary_repr(buf, (PyDictionary) py_obj);
   else if (py_obj instanceof PySequence) append_json_sequence_repr(buf, (PySequence) py_obj);
   else if (py_obj instanceof PyNone) buf.append("null");
   else if (py_obj.__findattr__("__json__") != null
       && py_obj.__findattr__("__json__").isCallable())
     buf.append(((PyMethod) py_obj.__findattr__("__json__")).__call__().toString());
   else
     throw new JSONEncodeError(
         "Python '"
             + ((PyType) py_obj.fastGetClass()).fastGetName()
             + "' object '"
             + py_obj.__repr__()
             + "' is not encodable in JSON");
 }
 public PyObject _doget(PyObject container, PyObject wherefound) {
   /* Only if classes are compatible */
   if (container == null || im_self != null) {
     return this;
   } else if (__builtin__.issubclass(container.fastGetClass(), im_class)) {
     if (im_func instanceof PyFunction) {
       return new PyMethod(container, (PyFunction) im_func, im_class);
     } else if (im_func instanceof PyReflectedFunction) {
       return new PyMethod(container, (PyReflectedFunction) im_func, im_class);
     } else {
       return new PyMethod(container, im_func, im_class);
     }
   } else {
     return this;
   }
 }
Esempio n. 3
0
 protected void append_json_map_repr(StringBuffer buf, PyObject map, PyList keys)
     throws JSONEncodeError {
   int num_keys = keys.__len__();
   buf.append('{');
   for (int ix = 0; ix < num_keys; ix++) {
     PyObject k = keys.__getitem__(ix);
     if (!(k instanceof PyString))
       throw new JSONEncodeError(
           ((PyType) k.fastGetClass()).fastGetName()
               + " objects are not permitted as JSON object keys.");
     append_json_string_repr(buf, ((PyString) k).toString());
     buf.append(':');
     buf.append(json_repr(map.__finditem__(k)));
     if (ix < num_keys - 1) buf.append(',');
   }
   buf.append('}');
 }
 public String toString() {
   String classname = "?";
   if (im_class != null) classname = class_name(im_class);
   if (im_self == null)
     // this is an unbound method
     return "<unbound method " + classname + "." + __name__ + ">";
   else
     return "<method "
         + classname
         + "."
         + __name__
         + " of "
         + class_name(im_self.fastGetClass())
         + " instance "
         + Py.idstr(im_self)
         + ">";
 }