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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Show comment Hide commentwenshaoAug 20, 2017
wenshao commentedAug 20, 2017
•
Edited 1 time
-
wenshao
edited Aug 20, 2017
edited