dungnn / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Broken deserialization of java.util.regex.Pattern objects #380

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Serialize and deserialize Pattern object.
2. Example code:
     Pattern org = Pattern.compile("te.t");
     String gson = new Gson().toJson(org);
     Pattern back = new Gson().fromJson(gson, Pattern.class);

     Matcher orgMatcher = org.matcher("XtestX");
     Matcher backMatcher = back.matcher("XtestX");

What is the expected output? What do you see instead?
Calling matcher method forces deserialized pattern compilation. In this case 
pattern group index count is not reset ie. pattern field capturingGroupCount is 
not set to 1. In the example above orgMatcher and backMatcher should behave the 
same. Executing following code 
     backMatcher.find();
     backMatcher.group();
causes java.lang.IndexOutOfBoundsException: No group 0. The same methods called 
on orgMatcher, result in group() method returning proper string "test".

What version of the product are you using? On what operating system?
Gson 1.7.1 and 2.0. Windows 7 Enterprise 64bit. Java 1.6.0_27 32bit.

Please provide any additional information below.
Execute provided code, all lines from top to bottom, to reproduce the problem.

Original issue reported on code.google.com by copter01 on 24 Nov 2011 at 5:07

GoogleCodeExporter commented 9 years ago
You'll have to create a custom type adapter for Pattern. It should take about 
12 lines of code.
https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-
Deserializ

Original comment by limpbizkit on 24 Nov 2011 at 8:32