Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Fast-Json解析gzip压缩的数据,不抛出异常,反而是直接返回null,对于定位问题很难定位,希望解决此BUG #3614

@Anthony-Dong

Description

@Anthony-Dong

version:1.2.72
问题代码: com.alibaba.fastjson.JSON#parseObject, line 435

public static <T> T parseObject(byte[] bytes, int offset, int len,
                                    Charset charset,
                                    Type clazz,
                                    ParserConfig config,
                                    ParseProcess processor,
                                    int featureValues,
                                    Feature... features) {
        if (charset == null) {
            charset = IOUtils.UTF8;
        }

        String strVal;
        if (charset == IOUtils.UTF8) {
            char[] chars = allocateChars(bytes.length);
           // 如果使用gzip编码,那么读取出来的len=-1,所以直接返回null
            int chars_len = IOUtils.decodeUTF8(bytes, offset, len, chars);
            if (chars_len < 0) {
                return null;
            }
            strVal = new String(chars, 0, chars_len);
        } else {
            if (len < 0) {
                return null;
            }
            strVal = new String(bytes, offset, len, charset);
        }
        return (T) parseObject(strVal, clazz, config, processor, featureValues, features);
    }

解决方式,希望直接抛出异常:
理由:1)fastjson对于非json的符合编码字符串,直接抛出异常,为什么不对不符合字符编码的抛出异常
2)由于业务中经常遇到编码问题,这种隐式问题还希望及时抛出问题,即时定位问题

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions