Just a helper to return a serialized object with jSON.

Specially useful dealing with encoding problems
504593
protected void jSONSuccess(PMStrutsContext ctx, Object object) throws PMForwardException {
        final String toJson = new Gson().toJson(object);
        ctx.put(PM_VOID_TEXT, toJson);
        success(ctx, "converters/void.jsp", false);
    }
388169
def _encode_json(obj):
    '''
    
    '''
    def _dump_obj(obj):
        if isinstance(obj, dict):
            return obj
        d = dict()
        for k in dir(obj):
            if not k.startswith('_'):
                d[k] = getattr(obj, k)
        return d
    return json.dumps(obj, default=_dump_obj)
602824
def to_json(value):
    
    if type(value) in JSON_TYPES:
        return value
    elif hasattr(value, "to_json"):
        return value.to_json()
    elif isinstance(value, list) or isinstance(value, set) or \
            isinstance(value, deque) or isinstance(value, tuple):
        return [to_json(v) for v in value]
    elif isinstance(value, dict):
        return {str(k): to_json(v) for k, v in value.items()}
    else:
        raise TypeError("{0} is not json serializable.".format(type(value)))
381232
def _decode(self, obj):
        
        if self.decode_responses and isinstance(obj, bytes):
            try:
                return obj.decode(self.encoding)
            except UnicodeDecodeError:
                return obj
        return obj
959103
def json_dump(obj,       # type: Any
              fp,        # type: IO[str]
              **kwargs   # type: Any
             ):  # type: (...) -> None
    
    if six.PY2:
        kwargs['encoding'] = 'utf-8'
    json.dump(convert_to_dict(obj), fp, **kwargs)
1241952
def to_json(obj, indent=4, sort_keys=True, **kwargs):
    
    obj_dict = to_dict(obj, **kwargs)
    return json.dumps(obj_dict, indent=indent, sort_keys=sort_keys)
305248
def json(
        self,
        *,
        include: 'SetStr' = None,
        exclude: 'SetStr' = None,
        by_alias: bool = False,
        skip_defaults: bool = False,
        encoder: Optional[Callable[[Any], Any]] = None,
        **dumps_kwargs: Any,
    ) -> str:
        
        encoder = cast(Callable[[Any], Any], encoder or self._json_encoder)
        return json.dumps(
            self.dict(include=include, exclude=exclude, by_alias=by_alias, skip_defaults=skip_defaults),
            default=encoder,
            **dumps_kwargs,
        )
138889
def to_json(self, pretty=True):
        
        data_dict = self.to_dict()
        if pretty:
            return json.dumps(
                data_dict, sort_keys=True, indent=2)
        return json.dumps(data_dict, sort_keys=True)
1127941
def _serialize_function(obj):
    
    try:
        obj = inspect.getsource(obj)
    except (TypeError, IOError):
        try:
            obj = marshal.dumps(obj)
        except ValueError:
            if hasattr(obj, '__dict__'):
                obj = _serialize_dict(obj.__dict__)
    return obj
1003404
def pretty_json(obj):
    
    return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)
314079
def class_to_json(obj):
    

    obj_raw = serialize(obj)

    return json.dumps(obj_raw, indent=_JSON_INDENT, sort_keys=True)
218495
def _convert_dict_to_json(array):
    
    return json.dumps(
        array,
        skipkeys=False,
        allow_nan=False,
        indent=None,
        separators=(",", ":"),
        sort_keys=True,
        default=lambda o: o.__dict__,
    )
929447
def jsonify(obj):  # pylint: disable=too-many-return-statements
    
    # Call the `to_json` method if available and add metadata.
    if hasattr(obj, 'to_json'):
        d = obj.to_json()
        _push_metadata(d, obj)
        return jsonify(d)

    # If we have a numpy array, convert it to a list.
    if isinstance(obj, np.ndarray):
        return obj.tolist()

    # If we have NumPy datatypes, convert them to native types.
    if isinstance(obj, (np.int32, np.int64)):
        return int(obj)
    if isinstance(obj, np.float64):
        return float(obj)

    # Recurse over dictionaries.
    if isinstance(obj, dict):
        return _jsonify_dict(obj)

    # Recurse over object dictionaries.
    if hasattr(obj, '__dict__'):
        return _jsonify_dict(obj.__dict__)

    # Recurse over lists and tuples.
    if isinstance(obj, (list, tuple)):
        return [jsonify(item) for item in obj]

    # Otherwise, give up and hope it's serializable.
    return obj
431923
def to_json(value, **kwargs):
        """"""
        if isinstance(value, HasProperties):
            return value.serialize(**kwargs)
        try:
            return json.loads(json.dumps(value))
        except TypeError:
            raise TypeError(
                "Cannot convert type {} to JSON without calling 'serialize' "
                "on an instance of Instance Property and registering a custom "
                "serializer".format(value.__class__.__name__)
            )
330454
def _bytes_to_json(value):
    """"""
    if isinstance(value, bytes):
        value = base64.standard_b64encode(value).decode("ascii")
    return value
1143963
def json(self):
        

        if six.PY3:
            return json.loads(self.body.decode(self.charset))
        else:
            return json.loads(self.body)
1022587
def loads(s, encoding=None, cls=JSONTreeDecoder, object_hook=None,
         parse_float=None, parse_int=None, parse_constant=None,
         object_pairs_hook=None, **kargs):
    
    return json.loads(s, encoding, cls, object_hook,
         parse_float, parse_int, parse_constant,
         object_pairs_hook, **kargs)