Last active
August 19, 2017 23:37
-
-
Save greenlaw110/e906970a13921fc30adada704468c63f to your computer and use it in GitHub Desktop.
The source code to reproduce https://github.com/alibaba/fastjson/issues/1423
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 FastJson37IssueWithLongParsing { | |
public static class LongVal { | |
private long v; | |
public void setV(long 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); | |
BigInteger n = new BigInteger(String.valueOf(Long.MAX_VALUE)).add(new BigInteger("1")); | |
Map<String, BigInteger> map = new HashMap<>(); | |
map.put("v", n); | |
String strBad = JSON.toJSONString(map); | |
System.out.println("prepare to parse: " + strBad); | |
System.out.println("We expect the following line to raise NumberFormatException, but it will print out something:"); | |
System.out.println(JSON.parseObject(strBad, LongVal.class)); | |
System.out.println("While Long.parseLong(String) call does raise NumberFormatException:"); | |
System.out.println(Long.parseLong(n.toString())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment