[alibaba/fastjson]JSONObject.parseObject解析float类型的小数时,解析值不对,有溢出现象

2024-08-28 811 views
7

"{\"angle\":-89.701210021972656,\"height\":649.35546875,\"left\":811.79644775390625,\"top\":-227.79908752441406,\"width\":51.572391510009766}" 上述属性全都为float,解析上述字符串时,height的值是5.1***

回答

8

可以提供一个简单的用例吗?在本地用下面这个用例跑在1.2.30和最新的1.2.61都是是没问题的:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Issue2769 {
    public static void main(String[] args) {
        String a = "{\"angle\":-89.701210021972656,\"height\":649.35546875,\"left\":811.79644775390625,\"top\":-227.79908752441406,\"width\":51.572391510009766}";
        JSONObject jsonObject = JSONObject.parseObject(a);
        TestBean testBean = JSONObject.parseObject(a, TestBean.class);
        System.out.println(jsonObject);
        System.out.println(testBean);
    }

    public static class TestBean {
        private float angle;
        private float height;
        private float left;
        private float top;
        private float width;

        public float getAngle() {
            return angle;
        }

        public void setAngle(float angle) {
            this.angle = angle;
        }

        public float getHeight() {
            return height;
        }

        public void setHeight(float height) {
            this.height = height;
        }

        public float getLeft() {
            return left;
        }

        public void setLeft(float left) {
            this.left = left;
        }

        public float getTop() {
            return top;
        }

        public void setTop(float top) {
            this.top = top;
        }

        public float getWidth() {
            return width;
        }

        public void setWidth(float width) {
            this.width = width;
        }

        @Override
        public String toString() {
            return "TestBean{" +
                    "angle=" + angle +
                    ", height=" + height +
                    ", left=" + left +
                    ", top=" + top +
                    ", width=" + width +
                    '}';
        }
    }
}

分别输出 "{"top":-227.79908752441406,"left":811.79644775390625,"width":51.572391510009766,"angle":-89.701210021972656,"height":649.35546875}"

"TestBean{angle=-89.70121, height=649.35547, left=811.79645, top=-227.79909, width=51.57239}"

9

不好意思,我没看版本号,我用的1.2.47版本,更新到最新的maven版本后没这问题了

6

我用的1.2.60也有这个问题啊,升级到1.2.70解决了