@@ -58,9 +58,9 @@ public class SimpleSerializable implements Cloneable {
58
58
@ J2SIgnore
59
59
private static Object classMutex = new Object ();
60
60
@ J2SIgnore
61
- private static Map <String , String > classMappings = new HashMap <String , String >();
61
+ private static Map <String , String > classNameMappings = new HashMap <String , String >();
62
62
@ J2SIgnore
63
- private static Map <String , String > rClassMappings = new HashMap <String , String >();
63
+ private static Map <String , String > classAliasMappings = new HashMap <String , String >();
64
64
65
65
private int simpleVersion ;
66
66
@@ -101,28 +101,28 @@ public static void registerClassShortenName(String clazzName, String shortenName
101
101
System .out .println ("Invalid shorten class name for " + clazzName );
102
102
return ;
103
103
}
104
- String sName = classMappings .get (clazzName );
104
+ String sName = classNameMappings .get (clazzName );
105
105
if (sName != null && !sName .equals (shortenName )) {
106
106
System .out .println ("Already existed shorten name " + sName + " for " + clazzName );
107
107
}
108
- String fName = rClassMappings .get (shortenName );
108
+ String fName = classAliasMappings .get (shortenName );
109
109
if (fName != null && !fName .equals (clazzName )) {
110
110
System .out .println ("Conficted: shorten name " + shortenName + " for " + fName + " and " + clazzName );
111
111
}
112
112
synchronized (classMutex ) {
113
- classMappings .put (clazzName , shortenName );
114
- rClassMappings .put (shortenName , clazzName );
113
+ classNameMappings .put (clazzName , shortenName );
114
+ classAliasMappings .put (shortenName , clazzName );
115
115
}
116
116
}
117
117
118
118
@ J2SIgnore
119
119
public static String getClassShortenName (String clazzName ) {
120
- return classMappings .get (clazzName );
120
+ return classNameMappings .get (clazzName );
121
121
}
122
122
123
123
@ J2SIgnore
124
124
public static String getClassFullName (String clazzName ) {
125
- return rClassMappings .get (clazzName );
125
+ return classAliasMappings .get (clazzName );
126
126
}
127
127
128
128
@ J2SIgnore
@@ -447,7 +447,7 @@ private String serialize(SimpleFilter filter, List<SimpleSerializable> ssObjs) {
447
447
clazzName = clazz .getName ();
448
448
}
449
449
if (getSimpleVersion () >= 202 ) {
450
- String shortClazzName = classMappings .get (clazzName );
450
+ String shortClazzName = classNameMappings .get (clazzName );
451
451
if (shortClazzName != null ) {
452
452
buffer .append (shortClazzName );
453
453
} else {
@@ -461,14 +461,20 @@ private String serialize(SimpleFilter filter, List<SimpleSerializable> ssObjs) {
461
461
462
462
Map <String , Field > fields = getSerializableFields (clazzName , this .getClass (), false );
463
463
boolean ignoring = (filter == null || filter .ignoreDefaultFields ());
464
- String [] fMap = fieldMapping ();
464
+ Map <String , String > fieldNameMap = getSimpleVersion () >= 202 ? fieldNameMapping () : null ;
465
+ String [] fMap = fieldNameMap == null ? fieldMapping () : null ;
465
466
try {
466
467
for (Iterator <Entry <String , Field >> itr = fields .entrySet ().iterator (); itr .hasNext ();) {
467
468
Entry <String , Field > entry = (Entry <String , Field >) itr .next ();
468
469
String name = entry .getKey ();
469
470
Field field = entry .getValue ();
470
471
if (filter != null && !filter .accept (name )) continue ;
471
- if (fMap != null && fMap .length > 1 ) {
472
+ if (fieldNameMap != null ) {
473
+ String alias = fieldNameMap .get (name );
474
+ if (alias != null && alias .length () > 0 ) {
475
+ name = alias ;
476
+ }
477
+ } else if (fMap != null && fMap .length > 1 ) {
472
478
for (int j = 0 ; j < fMap .length / 2 ; j ++) {
473
479
if (name .equals (fMap [j + j ])) {
474
480
String newName = fMap [j + j + 1 ];
@@ -2042,14 +2048,20 @@ private boolean deserialize(final String str, int start, List<SimpleSerializable
2042
2048
Class <?> clazzType = this .getClass ();
2043
2049
Map <String , Field > fieldMap = getSerializableFields (clazzType .getName (), clazzType , false );
2044
2050
int objectEnd = index + size ;
2045
- String [] fMap = fieldMapping ();
2051
+ Map <String , String > fieldAliasMap = getSimpleVersion () >= 202 ? fieldAliasMapping () : null ;
2052
+ String [] fMap = fieldAliasMap == null ? fieldMapping () : null ;
2046
2053
while (index < end && index < objectEnd ) {
2047
2054
char c1 = str .charAt (index ++);
2048
2055
int l1 = c1 - baseChar ;
2049
2056
if (l1 < 0 || index + l1 > end ) return true ; // error
2050
2057
String fieldName = str .substring (index , index + l1 );
2051
2058
index += l1 ;
2052
- if (fMap != null && fMap .length > 1 ) {
2059
+ if (fieldAliasMap != null ) {
2060
+ String trueName = fieldAliasMap .get (fieldName );
2061
+ if (trueName != null && trueName .length () > 0 ) {
2062
+ fieldName = trueName ;
2063
+ }
2064
+ } else if (fMap != null && fMap .length > 1 ) {
2053
2065
for (int i = 0 ; i < fMap .length / 2 ; i ++) {
2054
2066
if (fieldName .equals (fMap [i + i + 1 ])) {
2055
2067
String trueName = fMap [i + i ];
@@ -2910,6 +2922,26 @@ public void deserialize(Map<String, Object> properties) {
2910
2922
}
2911
2923
}
2912
2924
2925
+ /**
2926
+ * Fields with alias names.
2927
+ * Field name -> Alias
2928
+ * @return
2929
+ */
2930
+ @ J2SIgnore
2931
+ protected Map <String , String > fieldNameMapping () {
2932
+ return null ;
2933
+ }
2934
+
2935
+ /**
2936
+ * Fields with alias names.
2937
+ * Alias -> Field name
2938
+ * @return
2939
+ */
2940
+ @ J2SIgnore
2941
+ protected Map <String , String > fieldAliasMapping () {
2942
+ return null ;
2943
+ }
2944
+
2913
2945
/**
2914
2946
* Fields with alias names.
2915
2947
* @return
@@ -2918,6 +2950,34 @@ protected String[] fieldMapping() {
2918
2950
return null ;
2919
2951
}
2920
2952
2953
+ /**
2954
+ * Convert #fieldMapping into #fieldAliasMapping or #fieldNameMapping.
2955
+ *
2956
+ * @param array, array with string in order of field, alias, field, alias ...
2957
+ * @param reversed, true for #fieldAliasMapping, false for fieldNameMapping
2958
+ * @return
2959
+ */
2960
+ @ J2SIgnore
2961
+ public static Map <String , String > mappingFromArray (String [] array , boolean reversed ) {
2962
+ if (array == null || array .length <= 0 ) {
2963
+ return null ;
2964
+ }
2965
+ Map <String , String > mapping = new HashMap <String , String >(array .length );
2966
+ for (int i = 0 ; i < array .length / 2 ; i ++) {
2967
+ String name = array [i + i ];
2968
+ String alias = array [i + i + 1 ];
2969
+ if (name == null || alias == null ) {
2970
+ continue ;
2971
+ }
2972
+ if (reversed ) {
2973
+ mapping .put (alias , name );
2974
+ } else {
2975
+ mapping .put (name , alias );
2976
+ }
2977
+ }
2978
+ return mapping ;
2979
+ }
2980
+
2921
2981
/**
2922
2982
* Fields to be ignored while differences are being calculated.
2923
2983
* @return
@@ -3073,7 +3133,7 @@ public static SimpleSerializable parseInstance(String str, int start, SimpleFilt
3073
3133
if (index == -1 ) return null ;
3074
3134
String clazzName = str .substring (start + 6 , index );
3075
3135
if (v >= 202 ) {
3076
- String longClazzName = rClassMappings .get (clazzName );
3136
+ String longClazzName = classAliasMappings .get (clazzName );
3077
3137
if (longClazzName != null ) {
3078
3138
clazzName = longClazzName ;
3079
3139
}
0 commit comments