@@ -46,13 +46,40 @@ public class SimpleSerializable implements Cloneable {
4646
4747 public static boolean JSON_EXPAND_MODE = true ;
4848
49+ public static int LATEST_SIMPLE_VERSION = 202 ;
50+
4951 @ J2SIgnore
5052 private static Object mutex = new Object ();
5153 @ J2SIgnore
5254 private static Map <String , Map <String , Field >> quickFields = new HashMap <String , Map <String , Field >>();
5355 @ J2SIgnore
5456 private static Set <String > expiredClasses = new HashSet <String >();
5557
58+ @ J2SIgnore
59+ private static Object classMutex = new Object ();
60+ @ J2SIgnore
61+ private static Map <String , String > classMappings = new HashMap <String , String >();
62+ @ J2SIgnore
63+ private static Map <String , String > rClassMappings = new HashMap <String , String >();
64+
65+ private int simpleVersion ;
66+
67+ public int getSimpleVersion () {
68+ if (simpleVersion <= 0 ) {
69+ return 201 ;
70+ }
71+ return simpleVersion ;
72+ }
73+
74+ public void setSimpleVersion (int ver ) {
75+ simpleVersion = ver ;
76+ if (ver < 100 ) {
77+ simpleVersion = 201 ;
78+ } else if (simpleVersion >= 1000 ) {
79+ simpleVersion = 201 ;
80+ }
81+ }
82+
5683 @ J2SIgnore
5784 private static class DeserializeObject {
5885 Object object ;
@@ -68,6 +95,14 @@ public DeserializeObject(Object object, int index, boolean error) {
6895
6996 };
7097
98+ @ J2SIgnore
99+ public static void registerShortClassName (String clazzName , String shortName ) {
100+ synchronized (classMutex ) {
101+ classMappings .put (clazzName , shortName );
102+ rClassMappings .put (shortName , clazzName );
103+ }
104+ }
105+
71106 @ J2SIgnore
72107 static Map <String , Field > getSerializableFields (String clazzName , Class <?> clazz , boolean forceUpdate ) {
73108 Map <String , Field > fields = forceUpdate ? null : quickFields .get (clazzName );
@@ -371,7 +406,8 @@ private String serialize(SimpleFilter filter, List<SimpleSerializable> ssObjs) {
371406 * "WLL" is used to mark Simple RPC, 100 is version 1.0.0,
372407 * # is used to mark the the beginning of serialized data
373408 */
374- buffer .append ("WLL201" );
409+ buffer .append ("WLL" );
410+ buffer .append (getSimpleVersion ());
375411 Class <?> clazz = this .getClass ();
376412 String clazzName = clazz .getName ();
377413 int idx = -1 ;
@@ -388,7 +424,16 @@ private String serialize(SimpleFilter filter, List<SimpleSerializable> ssObjs) {
388424 }
389425 clazzName = clazz .getName ();
390426 }
391- buffer .append (clazzName );
427+ if (getSimpleVersion () >= 202 ) {
428+ String shortClazzName = classMappings .get (clazzName );
429+ if (shortClazzName != null ) {
430+ buffer .append (shortClazzName );
431+ } else {
432+ buffer .append (clazzName );
433+ }
434+ } else {
435+ buffer .append (clazzName );
436+ }
392437 buffer .append ("#00000000$" ); // later the number of size will be updated!
393438 int headSize = buffer .length ();
394439
@@ -1933,6 +1978,10 @@ private boolean deserialize(final String str, int start, List<SimpleSerializable
19331978 int end = str .length ();
19341979 int length = end - start ;
19351980 if (length <= 7 || !("WLL" .equals (str .substring (start , start + 3 )))) return false ; // Should throw exception!
1981+ try {
1982+ setSimpleVersion (Integer .parseInt (str .substring (start + 3 , start + 6 )));
1983+ } catch (NumberFormatException e1 ) {
1984+ }
19361985 int index = str .indexOf ('#' , start );
19371986 if (index == -1 ) return false ; // Should throw exception!
19381987 index ++;
@@ -2105,7 +2154,7 @@ private boolean deserialize(final String str, int start, List<SimpleSerializable
21052154 ss [i ] = new String (Base64 .base64ToByteArray (ss [i ]), "utf-8" );
21062155 } else if (c4 == 'U' ) {
21072156 ss [i ] = new String (ss [i ].getBytes ("iso-8859-1" ), "utf-8" );
2108- } else {
2157+ } else if ( c4 != 'O' ) {
21092158 ss [i ] = new String (ss [i ].getBytes ("iso-8859-1" ), "utf-8" );
21102159 }
21112160 }
@@ -2434,7 +2483,7 @@ private DeserializeObject deserializeArrayItem(String str, int index, int end, L
24342483 ss [i ] = new String (Base64 .base64ToByteArray (ss [i ]), "utf-8" );
24352484 } else if (c4 == 'U' ) {
24362485 ss [i ] = new String (ss [i ].getBytes ("iso-8859-1" ), "utf-8" );
2437- } else {
2486+ } else if ( c4 != 'O' ) {
24382487 ss [i ] = new String (ss [i ].getBytes ("iso-8859-1" ), "utf-8" );
24392488 }
24402489 }
@@ -2867,7 +2916,8 @@ protected boolean bytesCompactMode() {
28672916 */
28682917 @ J2SIgnore
28692918 public Object clone () throws CloneNotSupportedException {
2870- Object clone = super .clone ();
2919+ SimpleSerializable clone = (SimpleSerializable ) super .clone ();
2920+ clone .simpleVersion = simpleVersion ;
28712921
28722922 Class <? extends SimpleSerializable > clazz = this .getClass ();
28732923 Map <String , Field > fields = getSerializableFields (clazz .getName (), clazz , false );
@@ -2991,9 +3041,20 @@ public static SimpleSerializable parseInstance(String str, int start, SimpleFilt
29913041 if (str == null || start < 0 ) return null ;
29923042 int length = str .length () - start ;
29933043 if (length <= 7 || !("WLL" .equals (str .substring (start , start + 3 )))) return null ;
3044+ int v = 0 ;
3045+ try {
3046+ v = Integer .parseInt (str .substring (start + 3 , start + 6 ));
3047+ } catch (NumberFormatException e1 ) {
3048+ }
29943049 int index = str .indexOf ('#' , start );
29953050 if (index == -1 ) return null ;
29963051 String clazzName = str .substring (start + 6 , index );
3052+ if (v >= 202 ) {
3053+ String longClazzName = rClassMappings .get (clazzName );
3054+ if (longClazzName != null ) {
3055+ clazzName = longClazzName ;
3056+ }
3057+ }
29973058 if (filter != null ) {
29983059 if (!filter .accept (clazzName )) return null ;
29993060 }
0 commit comments