Skip to content

Instantly share code, notes, and snippets.

@greenlaw110
Last active August 19, 2017 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenlaw110/e906970a13921fc30adada704468c63f to your computer and use it in GitHub Desktop.
Save greenlaw110/e906970a13921fc30adada704468c63f to your computer and use it in GitHub Desktop.
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