Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The source code that reproduce https://github.com/alibaba/fastjson/issues/1424
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

This comment has been minimized.

Show comment
Hide comment
@wenshao

wenshao Aug 20, 2017

floatOverflowMap.put("va", floatOverflow + 1);
                     ----
floatOverflowMap.put("v", floatOverflow + 1);

wenshao commented Aug 20, 2017

floatOverflowMap.put("va", floatOverflow + 1);
                     ----
floatOverflowMap.put("v", floatOverflow + 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment