Last active
August 20, 2017 07:12
-
-
Save greenlaw110/ccc7feca57b9de61fa0e507074106d6f to your computer and use it in GitHub Desktop.
The source code that reproduce https://github.com/alibaba/fastjson/issues/1424
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testapp; | |
import com.alibaba.fastjson.JSON; | |
import java.math.BigInteger; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class FastJson37IssueWithOverflowFloatParsing { | |
public static class IntegerVal { | |
private int v; | |
public void setV(int v) { | |
this.v = v; | |
} | |
@Override | |
public String toString() { | |
return String.valueOf(v); | |
} | |
} | |
public static class FloatVal { | |
private float v; | |
public void setV(float v) { | |
this.v = v; | |
} | |
@Override | |
public String toString() { | |
return String.valueOf(v); | |
} | |
} | |
public static void main(String[] args) { | |
String fastJsonVersion = JSON.VERSION; | |
System.out.println("fastjson version: " + fastJsonVersion); | |
Map<String, Long> intOverflowMap = new HashMap<>(); | |
long intOverflow = Integer.MAX_VALUE; | |
intOverflowMap.put("v", intOverflow + 1); | |
String sIntOverflow = JSON.toJSONString(intOverflowMap); | |
System.out.println("prepare to parse overflow int val: " + sIntOverflow); | |
try { | |
JSON.parseObject(sIntOverflow, IntegerVal.class); | |
} catch (Exception e) { | |
System.out.println("We captured the Exception: " + e.getMessage()); | |
} | |
Map<String, Double> floatOverflowMap = new HashMap<>(); | |
double floatOverflow = Float.MAX_VALUE; | |
floatOverflowMap.put("va", floatOverflow + 1); | |
String sFloatOverflow = JSON.toJSONString(floatOverflowMap); | |
System.out.println("prepare to parse overflow float val: " + sIntOverflow); | |
FloatVal floatVal = JSON.parseObject(sFloatOverflow, FloatVal.class); | |
System.out.println("We expect an exception raised, but found it parsed out an invalid value: " + floatVal); | |
} | |
} |
wenshao
commented
Aug 20, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment