Beispiel #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");
 }
Beispiel #2
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('}');
 }