1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.json;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import net.sf.json.processors.DefaultDefaultValueProcessor;
30 import net.sf.json.processors.DefaultValueProcessor;
31 import net.sf.json.processors.DefaultValueProcessorMatcher;
32 import net.sf.json.processors.JsonBeanProcessor;
33 import net.sf.json.processors.JsonBeanProcessorMatcher;
34 import net.sf.json.processors.JsonValueProcessor;
35 import net.sf.json.processors.JsonValueProcessorMatcher;
36 import net.sf.json.processors.PropertyNameProcessor;
37 import net.sf.json.processors.PropertyNameProcessorMatcher;
38 import net.sf.json.util.CycleDetectionStrategy;
39 import net.sf.json.util.JavaIdentifierTransformer;
40 import net.sf.json.util.JsonEventListener;
41 import net.sf.json.util.NewBeanInstanceStrategy;
42 import net.sf.json.util.PropertyExclusionClassMatcher;
43 import net.sf.json.util.PropertyFilter;
44 import net.sf.json.util.PropertySetStrategy;
45
46 import org.apache.commons.collections.map.MultiKeyMap;
47 import org.apache.commons.lang.StringUtils;
48
49
50
51
52
53
54 public class JsonConfig {
55 public static final DefaultValueProcessorMatcher DEFAULT_DEFAULT_VALUE_PROCESSOR_MATCHER = DefaultValueProcessorMatcher.DEFAULT;
56 public static final JsonBeanProcessorMatcher DEFAULT_JSON_BEAN_PROCESSOR_MATCHER = JsonBeanProcessorMatcher.DEFAULT;
57 public static final JsonValueProcessorMatcher DEFAULT_JSON_VALUE_PROCESSOR_MATCHER = JsonValueProcessorMatcher.DEFAULT;
58 public static final NewBeanInstanceStrategy DEFAULT_NEW_BEAN_INSTANCE_STRATEGY = NewBeanInstanceStrategy.DEFAULT;
59 public static final PropertyExclusionClassMatcher DEFAULT_PROPERTY_EXCLUSION_CLASS_MATCHER = PropertyExclusionClassMatcher.DEFAULT;
60 public static final PropertyNameProcessorMatcher DEFAULT_PROPERTY_NAME_PROCESSOR_MATCHER = PropertyNameProcessorMatcher.DEFAULT;
61 public static final int MODE_LIST = 1;
62 public static final int MODE_OBJECT_ARRAY = 2;
63 public static final int MODE_SET = 2;
64 private static final Class DEFAULT_COLLECTION_TYPE = List.class;
65 private static final CycleDetectionStrategy DEFAULT_CYCLE_DETECTION_STRATEGY = CycleDetectionStrategy.STRICT;
66 private static final String[] DEFAULT_EXCLUDES = new String[] { "class", "declaringClass", "metaClass" };
67 private static final JavaIdentifierTransformer DEFAULT_JAVA_IDENTIFIER_TRANSFORMER = JavaIdentifierTransformer.NOOP;
68 private static final DefaultValueProcessor DEFAULT_VALUE_PROCESSOR = new DefaultDefaultValueProcessor();
69 private static final String[] EMPTY_EXCLUDES = new String[0];
70
71
72 private int arrayMode = MODE_LIST;
73 private MultiKeyMap beanKeyMap = new MultiKeyMap();
74 private Map beanProcessorMap = new HashMap();
75 private MultiKeyMap beanTypeMap = new MultiKeyMap();
76
77 private Map classMap;
78 private Class collectionType = DEFAULT_COLLECTION_TYPE;
79 private CycleDetectionStrategy cycleDetectionStrategy = DEFAULT_CYCLE_DETECTION_STRATEGY;
80 private Map defaultValueMap = new HashMap();
81 private DefaultValueProcessorMatcher defaultValueProcessorMatcher = DEFAULT_DEFAULT_VALUE_PROCESSOR_MATCHER;
82 private Class enclosedType;
83 private List eventListeners = new ArrayList();
84 private String[] excludes = EMPTY_EXCLUDES;
85 private Map exclusionMap = new HashMap();
86 private boolean handleJettisonEmptyElement;
87 private boolean handleJettisonSingleElementArray;
88 private boolean ignoreDefaultExcludes;
89
90 private boolean ignoreTransientFields;
91 private boolean ignorePublicFields = true;
92 private boolean javascriptCompliant;
93 private JavaIdentifierTransformer javaIdentifierTransformer = DEFAULT_JAVA_IDENTIFIER_TRANSFORMER;
94 private PropertyFilter javaPropertyFilter;
95 private Map javaPropertyNameProcessorMap = new HashMap();
96 private PropertyNameProcessorMatcher javaPropertyNameProcessorMatcher = DEFAULT_PROPERTY_NAME_PROCESSOR_MATCHER;
97 private JsonBeanProcessorMatcher jsonBeanProcessorMatcher = DEFAULT_JSON_BEAN_PROCESSOR_MATCHER;
98 private PropertyFilter jsonPropertyFilter;
99 private Map jsonPropertyNameProcessorMap = new HashMap();
100 private PropertyNameProcessorMatcher jsonPropertyNameProcessorMatcher = DEFAULT_PROPERTY_NAME_PROCESSOR_MATCHER;
101 private JsonValueProcessorMatcher jsonValueProcessorMatcher = DEFAULT_JSON_VALUE_PROCESSOR_MATCHER;
102 private Map keyMap = new HashMap();
103 private NewBeanInstanceStrategy newBeanInstanceStrategy = DEFAULT_NEW_BEAN_INSTANCE_STRATEGY;
104 private PropertyExclusionClassMatcher propertyExclusionClassMatcher = DEFAULT_PROPERTY_EXCLUSION_CLASS_MATCHER;
105 private PropertySetStrategy propertySetStrategy;
106
107 private Class rootClass;
108 private boolean skipJavaIdentifierTransformationInMapKeys;
109 private boolean triggerEvents;
110 private Map typeMap = new HashMap();
111 private List ignoreFieldAnnotations = new ArrayList();
112 private boolean allowNonStringKeys = false;
113
114 public JsonConfig() {
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128 public synchronized void addJsonEventListener( JsonEventListener listener ) {
129 if( !eventListeners.contains( listener ) ) {
130 eventListeners.add( listener );
131 }
132 }
133
134
135
136
137
138 public void clearJavaPropertyNameProcessors() {
139 javaPropertyNameProcessorMap.clear();
140 }
141
142
143
144
145
146 public void clearJsonBeanProcessors() {
147 beanProcessorMap.clear();
148 }
149
150
151
152
153
154 public synchronized void clearJsonEventListeners() {
155 eventListeners.clear();
156 }
157
158
159
160
161
162 public void clearJsonPropertyNameProcessors() {
163 jsonPropertyNameProcessorMap.clear();
164 }
165
166
167
168
169
170 public void clearJsonValueProcessors() {
171 beanKeyMap.clear();
172 beanTypeMap.clear();
173 keyMap.clear();
174 typeMap.clear();
175 }
176
177
178
179
180
181 public void clearPropertyExclusions() {
182 exclusionMap.clear();
183 }
184
185
186
187
188
189
190
191 public void clearPropertyNameProcessors() {
192 clearJavaPropertyNameProcessors();
193 }
194
195 public JsonConfig copy() {
196 JsonConfig jsc = new JsonConfig();
197 jsc.beanKeyMap.putAll( beanKeyMap );
198 jsc.beanTypeMap.putAll( beanTypeMap );
199 jsc.classMap = new HashMap();
200 if( classMap != null ) {
201 jsc.classMap.putAll( classMap );
202 }
203 jsc.cycleDetectionStrategy = cycleDetectionStrategy;
204 if( eventListeners != null ) {
205 jsc.eventListeners.addAll( eventListeners );
206 }
207 if( excludes != null ) {
208 jsc.excludes = new String[excludes.length];
209 System.arraycopy( excludes, 0, jsc.excludes, 0, excludes.length );
210 }
211 jsc.handleJettisonEmptyElement = handleJettisonEmptyElement;
212 jsc.handleJettisonSingleElementArray = handleJettisonSingleElementArray;
213 jsc.ignoreDefaultExcludes = ignoreDefaultExcludes;
214 jsc.ignoreTransientFields = ignoreTransientFields;
215 jsc.ignorePublicFields = ignorePublicFields;
216 jsc.javaIdentifierTransformer = javaIdentifierTransformer;
217 jsc.javascriptCompliant = javascriptCompliant;
218 jsc.keyMap.putAll( keyMap );
219 jsc.beanProcessorMap.putAll( beanProcessorMap );
220 jsc.rootClass = rootClass;
221 jsc.skipJavaIdentifierTransformationInMapKeys = skipJavaIdentifierTransformationInMapKeys;
222 jsc.triggerEvents = triggerEvents;
223 jsc.typeMap.putAll( typeMap );
224 jsc.jsonPropertyFilter = jsonPropertyFilter;
225 jsc.javaPropertyFilter = javaPropertyFilter;
226 jsc.jsonBeanProcessorMatcher = jsonBeanProcessorMatcher;
227 jsc.newBeanInstanceStrategy = newBeanInstanceStrategy;
228 jsc.defaultValueProcessorMatcher = defaultValueProcessorMatcher;
229 jsc.defaultValueMap.putAll( defaultValueMap );
230 jsc.propertySetStrategy = propertySetStrategy;
231
232 jsc.collectionType = collectionType;
233 jsc.enclosedType = enclosedType;
234 jsc.jsonValueProcessorMatcher = jsonValueProcessorMatcher;
235 jsc.javaPropertyNameProcessorMatcher = javaPropertyNameProcessorMatcher;
236 jsc.javaPropertyNameProcessorMap.putAll( javaPropertyNameProcessorMap );
237 jsc.jsonPropertyNameProcessorMatcher = jsonPropertyNameProcessorMatcher;
238 jsc.jsonPropertyNameProcessorMap.putAll( jsonPropertyNameProcessorMap );
239 jsc.propertyExclusionClassMatcher = propertyExclusionClassMatcher;
240 jsc.exclusionMap.putAll( exclusionMap );
241 jsc.ignoreFieldAnnotations.addAll( ignoreFieldAnnotations );
242 jsc.allowNonStringKeys = allowNonStringKeys;
243 return jsc;
244 }
245
246
247
248
249
250 public void disableEventTriggering() {
251 triggerEvents = false;
252 }
253
254
255
256
257
258 public void enableEventTriggering() {
259 triggerEvents = true;
260 }
261
262
263
264
265
266
267
268
269 public DefaultValueProcessor findDefaultValueProcessor( Class target ) {
270 if( !defaultValueMap.isEmpty() ) {
271 Object key = defaultValueProcessorMatcher.getMatch( target, defaultValueMap.keySet() );
272 DefaultValueProcessor processor = (DefaultValueProcessor) defaultValueMap.get( key );
273 if( processor != null ) {
274 return processor;
275 }
276 }
277 return DEFAULT_VALUE_PROCESSOR;
278 }
279
280
281
282
283
284
285
286
287 public PropertyNameProcessor findJavaPropertyNameProcessor( Class beanClass ) {
288 if( !javaPropertyNameProcessorMap.isEmpty() ) {
289 Object key = javaPropertyNameProcessorMatcher.getMatch( beanClass, javaPropertyNameProcessorMap.keySet() );
290 return (PropertyNameProcessor) javaPropertyNameProcessorMap.get( key );
291
292 }
293 return null;
294 }
295
296
297
298
299
300
301
302
303 public JsonBeanProcessor findJsonBeanProcessor( Class target ) {
304 if( !beanProcessorMap.isEmpty() ) {
305 Object key = jsonBeanProcessorMatcher.getMatch( target, beanProcessorMap.keySet() );
306 return (JsonBeanProcessor) beanProcessorMap.get( key );
307 }
308 return null;
309 }
310
311
312
313
314
315
316
317
318 public PropertyNameProcessor findJsonPropertyNameProcessor( Class beanClass ) {
319 if( !jsonPropertyNameProcessorMap.isEmpty() ) {
320 Object key = jsonPropertyNameProcessorMatcher.getMatch( beanClass, jsonPropertyNameProcessorMap.keySet() );
321 return (PropertyNameProcessor) jsonPropertyNameProcessorMap.get( key );
322
323 }
324 return null;
325 }
326
327
328
329
330
331
332
333
334 public JsonValueProcessor findJsonValueProcessor( Class propertyType ) {
335 if( !typeMap.isEmpty() ) {
336 Object key = jsonValueProcessorMatcher.getMatch( propertyType, typeMap.keySet() );
337 return (JsonValueProcessor) typeMap.get( key );
338
339 }
340 return null;
341 }
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359 public JsonValueProcessor findJsonValueProcessor( Class beanClass, Class propertyType, String key ) {
360 JsonValueProcessor jsonValueProcessor = null;
361 jsonValueProcessor = (JsonValueProcessor) beanKeyMap.get( beanClass, key );
362 if( jsonValueProcessor != null ) {
363 return jsonValueProcessor;
364 }
365
366 jsonValueProcessor = (JsonValueProcessor) beanTypeMap.get( beanClass, propertyType );
367 if( jsonValueProcessor != null ) {
368 return jsonValueProcessor;
369 }
370
371 jsonValueProcessor = (JsonValueProcessor) keyMap.get( key );
372 if( jsonValueProcessor != null ) {
373 return jsonValueProcessor;
374 }
375
376 Object tkey = jsonValueProcessorMatcher.getMatch( propertyType, typeMap.keySet() );
377 jsonValueProcessor = (JsonValueProcessor) typeMap.get( tkey );
378 if( jsonValueProcessor != null ) {
379 return jsonValueProcessor;
380 }
381
382 return null;
383 }
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398 public JsonValueProcessor findJsonValueProcessor( Class propertyType, String key ) {
399 JsonValueProcessor jsonValueProcessor = null;
400 jsonValueProcessor = (JsonValueProcessor) keyMap.get( key );
401 if( jsonValueProcessor != null ) {
402 return jsonValueProcessor;
403 }
404
405 Object tkey = jsonValueProcessorMatcher.getMatch( propertyType, typeMap.keySet() );
406 jsonValueProcessor = (JsonValueProcessor) typeMap.get( tkey );
407 if( jsonValueProcessor != null ) {
408 return jsonValueProcessor;
409 }
410
411 return null;
412 }
413
414
415
416
417
418
419
420
421
422
423 public PropertyNameProcessor findPropertyNameProcessor( Class beanClass ) {
424 return findJavaPropertyNameProcessor( beanClass );
425 }
426
427
428
429
430
431
432
433 public int getArrayMode() {
434 return arrayMode;
435 }
436
437
438
439
440
441
442
443 public Map getClassMap() {
444 return classMap;
445 }
446
447
448
449
450
451
452
453 public Class getCollectionType() {
454 return collectionType;
455 }
456
457
458
459
460
461
462 public CycleDetectionStrategy getCycleDetectionStrategy() {
463 return cycleDetectionStrategy;
464 }
465
466
467
468
469
470
471 public DefaultValueProcessorMatcher getDefaultValueProcessorMatcher() {
472 return defaultValueProcessorMatcher;
473 }
474
475
476
477
478
479
480
481 public Class getEnclosedType() {
482 return enclosedType;
483 }
484
485
486
487
488
489 public String[] getExcludes() {
490 return excludes;
491 }
492
493
494
495
496
497
498 public JavaIdentifierTransformer getJavaIdentifierTransformer() {
499 return javaIdentifierTransformer;
500 }
501
502
503
504
505
506 public PropertyFilter getJavaPropertyFilter() {
507 return javaPropertyFilter;
508 }
509
510
511
512
513
514
515 public PropertyNameProcessorMatcher getJavaPropertyNameProcessorMatcher() {
516 return javaPropertyNameProcessorMatcher;
517 }
518
519
520
521
522
523
524 public JsonBeanProcessorMatcher getJsonBeanProcessorMatcher() {
525 return jsonBeanProcessorMatcher;
526 }
527
528
529
530
531
532 public synchronized List getJsonEventListeners() {
533 return eventListeners;
534 }
535
536
537
538
539
540 public PropertyFilter getJsonPropertyFilter() {
541 return jsonPropertyFilter;
542 }
543
544
545
546
547
548
549 public PropertyNameProcessorMatcher getJsonPropertyNameProcessorMatcher() {
550 return javaPropertyNameProcessorMatcher;
551 }
552
553
554
555
556
557
558 public JsonValueProcessorMatcher getJsonValueProcessorMatcher() {
559 return jsonValueProcessorMatcher;
560 }
561
562
563
564
565
566 public Collection getMergedExcludes() {
567 Collection exclusions = new HashSet();
568 for( int i = 0; i < excludes.length; i++ ) {
569 String exclusion = excludes[i];
570 if( !StringUtils.isBlank( excludes[i] ) ) {
571 exclusions.add( exclusion.trim() );
572 }
573 }
574
575 if( !ignoreDefaultExcludes ) {
576 for( int i = 0; i < DEFAULT_EXCLUDES.length; i++ ) {
577 if( !exclusions.contains( DEFAULT_EXCLUDES[i] ) ) {
578 exclusions.add( DEFAULT_EXCLUDES[i] );
579 }
580 }
581 }
582
583 return exclusions;
584 }
585
586
587
588
589
590
591 public Collection getMergedExcludes( Class target ) {
592 if( target == null ) {
593 return getMergedExcludes();
594 }
595
596 Collection exclusionSet = getMergedExcludes();
597 if( !exclusionMap.isEmpty() ) {
598 Object key = propertyExclusionClassMatcher.getMatch( target, exclusionMap.keySet() );
599 Set set = (Set) exclusionMap.get( key );
600 if( set != null && !set.isEmpty() ) {
601 for( Iterator i = set.iterator(); i.hasNext(); ) {
602 Object e = i.next();
603 if( !exclusionSet.contains( e ) ) {
604 exclusionSet.add( e );
605 }
606 }
607 }
608 }
609
610 return exclusionSet;
611 }
612
613
614
615
616
617
618 public NewBeanInstanceStrategy getNewBeanInstanceStrategy() {
619 return newBeanInstanceStrategy;
620 }
621
622
623
624
625
626
627 public PropertyExclusionClassMatcher getPropertyExclusionClassMatcher() {
628 return propertyExclusionClassMatcher;
629 }
630
631
632
633
634
635
636
637
638 public PropertyNameProcessorMatcher getPropertyNameProcessorMatcher() {
639 return getJavaPropertyNameProcessorMatcher();
640 }
641
642
643
644
645
646
647 public PropertySetStrategy getPropertySetStrategy() {
648 return propertySetStrategy;
649 }
650
651
652
653
654
655
656
657 public Class getRootClass() {
658 return rootClass;
659 }
660
661
662
663
664
665
666 public boolean isAllowNonStringKeys() {
667 return allowNonStringKeys;
668 }
669
670
671
672
673
674
675 public boolean isEventTriggeringEnabled() {
676 return triggerEvents;
677 }
678
679
680
681
682
683
684
685 public boolean isHandleJettisonEmptyElement() {
686 return handleJettisonEmptyElement;
687 }
688
689
690
691
692
693
694
695 public boolean isHandleJettisonSingleElementArray() {
696 return handleJettisonSingleElementArray;
697 }
698
699
700
701
702
703
704 public boolean isIgnoreDefaultExcludes() {
705 return ignoreDefaultExcludes;
706 }
707
708
709
710
711
712
713 public boolean isIgnoreJPATransient() {
714 return ignoreFieldAnnotations.contains("javax.persistence.Transient");
715 }
716
717
718
719
720
721
722 public boolean isIgnoreTransientFields() {
723 return ignoreTransientFields;
724 }
725
726
727
728
729
730
731 public boolean isIgnorePublicFields() {
732 return ignorePublicFields;
733 }
734
735
736
737
738
739
740 public boolean isJavascriptCompliant() {
741 return javascriptCompliant;
742 }
743
744
745
746
747
748
749 public boolean isSkipJavaIdentifierTransformationInMapKeys() {
750 return skipJavaIdentifierTransformationInMapKeys;
751 }
752
753
754
755
756
757
758
759
760 public void registerDefaultValueProcessor( Class target, DefaultValueProcessor defaultValueProcessor ) {
761 if( target != null && defaultValueProcessor != null ) {
762 defaultValueMap.put( target, defaultValueProcessor );
763 }
764 }
765
766
767
768
769
770
771
772
773 public void registerJavaPropertyNameProcessor( Class target, PropertyNameProcessor propertyNameProcessor ) {
774 if( target != null && propertyNameProcessor != null ) {
775 javaPropertyNameProcessorMap.put( target, propertyNameProcessor );
776 }
777 }
778
779
780
781
782
783
784
785
786 public void registerJsonBeanProcessor( Class target, JsonBeanProcessor jsonBeanProcessor ) {
787 if( target != null && jsonBeanProcessor != null ) {
788 beanProcessorMap.put( target, jsonBeanProcessor );
789 }
790 }
791
792
793
794
795
796
797
798
799 public void registerJsonPropertyNameProcessor( Class target, PropertyNameProcessor propertyNameProcessor ) {
800 if( target != null && propertyNameProcessor != null ) {
801 jsonPropertyNameProcessorMap.put( target, propertyNameProcessor );
802 }
803 }
804
805
806
807
808
809
810
811
812
813 public void registerJsonValueProcessor( Class beanClass, Class propertyType, JsonValueProcessor jsonValueProcessor ) {
814 if( beanClass != null && propertyType != null && jsonValueProcessor != null ) {
815 beanTypeMap.put( beanClass, propertyType, jsonValueProcessor );
816 }
817 }
818
819
820
821
822
823
824
825
826 public void registerJsonValueProcessor( Class propertyType, JsonValueProcessor jsonValueProcessor ) {
827 if( propertyType != null && jsonValueProcessor != null ) {
828 typeMap.put( propertyType, jsonValueProcessor );
829 }
830 }
831
832
833
834
835
836
837
838
839
840 public void registerJsonValueProcessor( Class beanClass, String key, JsonValueProcessor jsonValueProcessor ) {
841 if( beanClass != null && key != null && jsonValueProcessor != null ) {
842 beanKeyMap.put( beanClass, key, jsonValueProcessor );
843 }
844 }
845
846
847
848
849
850
851
852
853 public void registerJsonValueProcessor( String key, JsonValueProcessor jsonValueProcessor ) {
854 if( key != null && jsonValueProcessor != null ) {
855 keyMap.put( key, jsonValueProcessor );
856 }
857 }
858
859
860
861
862
863
864
865
866 public void registerPropertyExclusion( Class target, String propertyName ) {
867 if( target != null && propertyName != null ) {
868 Set set = (Set) exclusionMap.get( target );
869 if( set == null ){
870 set = new HashSet();
871 exclusionMap.put( target, set );
872 }
873 if( !set.contains( propertyName )){
874 set.add(propertyName );
875 }
876 }
877 }
878
879
880
881
882
883
884
885
886 public void registerPropertyExclusions( Class target, String[] properties ) {
887 if( target != null && properties != null && properties.length > 0 ) {
888 Set set = (Set) exclusionMap.get( target );
889 if( set == null ) {
890 set = new HashSet();
891 exclusionMap.put( target, set );
892 }
893 for( int i = 0; i < properties.length; i++ ) {
894 if( !set.contains( properties[i] ) ) {
895 set.add( properties[i] );
896 }
897 }
898 }
899 }
900
901
902
903
904
905
906
907
908
909
910 public void registerPropertyNameProcessor( Class target, PropertyNameProcessor propertyNameProcessor ) {
911 registerJavaPropertyNameProcessor( target, propertyNameProcessor );
912 }
913
914
915
916
917
918
919
920
921 public synchronized void removeJsonEventListener( JsonEventListener listener ) {
922 eventListeners.remove( listener );
923 }
924
925
926
927
928 public void reset() {
929 excludes = EMPTY_EXCLUDES;
930 ignoreDefaultExcludes = false;
931 ignoreTransientFields = false;
932 ignorePublicFields = true;
933 javascriptCompliant = false;
934 javaIdentifierTransformer = DEFAULT_JAVA_IDENTIFIER_TRANSFORMER;
935 cycleDetectionStrategy = DEFAULT_CYCLE_DETECTION_STRATEGY;
936 skipJavaIdentifierTransformationInMapKeys = false;
937 triggerEvents = false;
938 handleJettisonEmptyElement = false;
939 handleJettisonSingleElementArray = false;
940 arrayMode = MODE_LIST;
941 rootClass = null;
942 classMap = null;
943 keyMap.clear();
944 typeMap.clear();
945 beanKeyMap.clear();
946 beanTypeMap.clear();
947 jsonPropertyFilter = null;
948 javaPropertyFilter = null;
949 jsonBeanProcessorMatcher = DEFAULT_JSON_BEAN_PROCESSOR_MATCHER;
950 newBeanInstanceStrategy = DEFAULT_NEW_BEAN_INSTANCE_STRATEGY;
951 defaultValueProcessorMatcher = DEFAULT_DEFAULT_VALUE_PROCESSOR_MATCHER;
952 defaultValueMap.clear();
953 propertySetStrategy = null
954
955 collectionType = DEFAULT_COLLECTION_TYPE;
956 enclosedType = null;
957 jsonValueProcessorMatcher = DEFAULT_JSON_VALUE_PROCESSOR_MATCHER;
958 javaPropertyNameProcessorMap.clear();
959 javaPropertyNameProcessorMatcher = DEFAULT_PROPERTY_NAME_PROCESSOR_MATCHER;
960 jsonPropertyNameProcessorMap.clear();
961 jsonPropertyNameProcessorMatcher = DEFAULT_PROPERTY_NAME_PROCESSOR_MATCHER;
962 beanProcessorMap.clear();
963 propertyExclusionClassMatcher = DEFAULT_PROPERTY_EXCLUSION_CLASS_MATCHER;
964 exclusionMap.clear();
965 ignoreFieldAnnotations.clear();
966 allowNonStringKeys = false;
967 }
968
969
970
971
972
973 public void setAllowNonStringKeys( boolean allowNonStringKeys ) {
974 this.allowNonStringKeys = allowNonStringKeys;
975 }
976
977
978
979
980
981
982
983
984 public void setArrayMode( int arrayMode ) {
985 if( arrayMode == MODE_OBJECT_ARRAY ) {
986 this.arrayMode = arrayMode;
987 } else if( arrayMode == MODE_SET ) {
988 this.arrayMode = arrayMode;
989 this.collectionType = Set.class;
990 } else {
991 this.arrayMode = MODE_LIST;
992 this.enclosedType = DEFAULT_COLLECTION_TYPE;
993 }
994 }
995
996
997
998
999
1000
1001
1002 public void setClassMap( Map classMap ) {
1003 this.classMap = classMap;
1004 }
1005
1006
1007
1008
1009
1010
1011
1012 public void setCollectionType( Class collectionType ) {
1013 if( collectionType != null ) {
1014 if( !Collection.class.isAssignableFrom( collectionType ) ) {
1015 throw new JSONException( "The configured collectionType is not a Collection: " + collectionType.getName() );
1016 }
1017 this.collectionType = collectionType;
1018 } else {
1019 collectionType = DEFAULT_COLLECTION_TYPE;
1020 }
1021 }
1022
1023
1024
1025
1026
1027
1028 public void setCycleDetectionStrategy( CycleDetectionStrategy cycleDetectionStrategy ) {
1029 this.cycleDetectionStrategy = cycleDetectionStrategy == null ? DEFAULT_CYCLE_DETECTION_STRATEGY
1030 : cycleDetectionStrategy;
1031 }
1032
1033
1034
1035
1036
1037
1038 public void setDefaultValueProcessorMatcher( DefaultValueProcessorMatcher defaultValueProcessorMatcher ) {
1039 this.defaultValueProcessorMatcher = defaultValueProcessorMatcher == null ? DEFAULT_DEFAULT_VALUE_PROCESSOR_MATCHER
1040 : defaultValueProcessorMatcher;
1041 }
1042
1043
1044
1045
1046
1047
1048
1049 public void setEnclosedType( Class enclosedType ) {
1050 this.enclosedType = enclosedType;
1051 }
1052
1053
1054
1055
1056
1057
1058 public void setExcludes( String[] excludes ) {
1059 this.excludes = excludes == null ? EMPTY_EXCLUDES : excludes;
1060 }
1061
1062
1063
1064
1065
1066
1067
1068 public void setHandleJettisonEmptyElement( boolean handleJettisonEmptyElement ) {
1069 this.handleJettisonEmptyElement = handleJettisonEmptyElement;
1070 }
1071
1072
1073
1074
1075
1076
1077
1078 public void setHandleJettisonSingleElementArray( boolean handleJettisonSingleElementArray ) {
1079 this.handleJettisonSingleElementArray = handleJettisonSingleElementArray;
1080 }
1081
1082
1083
1084
1085
1086 public void setIgnoreDefaultExcludes( boolean ignoreDefaultExcludes ) {
1087 this.ignoreDefaultExcludes = ignoreDefaultExcludes;
1088 }
1089
1090
1091
1092
1093
1094 public void setIgnoreJPATransient( boolean ignoreJPATransient ) {
1095 if(ignoreJPATransient) {
1096 addIgnoreFieldAnnotation("javax.persistence.Transient");
1097 } else {
1098 removeIgnoreFieldAnnotation("javax.persistence.Transient");
1099 }
1100 }
1101
1102
1103
1104
1105
1106 public void addIgnoreFieldAnnotation( String annotationClassName ) {
1107 if( annotationClassName != null && !ignoreFieldAnnotations.contains( annotationClassName )) {
1108 ignoreFieldAnnotations.add(annotationClassName);
1109 }
1110 }
1111
1112
1113
1114
1115
1116 public void removeIgnoreFieldAnnotation( String annotationClassName ) {
1117 if( annotationClassName != null ) ignoreFieldAnnotations.remove(annotationClassName);
1118 }
1119
1120
1121
1122
1123
1124 public void addIgnoreFieldAnnotation( Class annotationClass ) {
1125 if( annotationClass != null && !ignoreFieldAnnotations.contains( annotationClass.getName() )) {
1126 ignoreFieldAnnotations.add(annotationClass.getName());
1127 }
1128 }
1129
1130
1131
1132
1133
1134 public void removeIgnoreFieldAnnotation( Class annotationClass ) {
1135 if( annotationClass != null ) ignoreFieldAnnotations.remove(annotationClass.getName());
1136 }
1137
1138
1139
1140
1141
1142 public List getIgnoreFieldAnnotations() {
1143 return Collections.unmodifiableList(ignoreFieldAnnotations);
1144 }
1145
1146
1147
1148
1149
1150 public void setIgnoreTransientFields( boolean ignoreTransientFields ) {
1151 this.ignoreTransientFields = ignoreTransientFields;
1152 }
1153
1154
1155
1156
1157
1158 public void setIgnorePublicFields( boolean ignorePublicFields ) {
1159 this.ignorePublicFields = ignorePublicFields;
1160 }
1161
1162
1163
1164
1165
1166 public void setJavascriptCompliant( boolean javascriptCompliant ) {
1167 this.javascriptCompliant = javascriptCompliant;
1168 }
1169
1170
1171
1172
1173
1174
1175 public void setJavaIdentifierTransformer( JavaIdentifierTransformer javaIdentifierTransformer ) {
1176 this.javaIdentifierTransformer = javaIdentifierTransformer == null ? DEFAULT_JAVA_IDENTIFIER_TRANSFORMER
1177 : javaIdentifierTransformer;
1178 }
1179
1180
1181
1182
1183
1184
1185
1186 public void setJavaPropertyFilter( PropertyFilter javaPropertyFilter ) {
1187 this.javaPropertyFilter = javaPropertyFilter;
1188 }
1189
1190
1191
1192
1193
1194
1195 public void setJavaPropertyNameProcessorMatcher( PropertyNameProcessorMatcher propertyNameProcessorMatcher ) {
1196 this.javaPropertyNameProcessorMatcher = propertyNameProcessorMatcher == null ? DEFAULT_PROPERTY_NAME_PROCESSOR_MATCHER
1197 : propertyNameProcessorMatcher;
1198 }
1199
1200
1201
1202
1203
1204
1205 public void setJsonBeanProcessorMatcher( JsonBeanProcessorMatcher jsonBeanProcessorMatcher ) {
1206 this.jsonBeanProcessorMatcher = jsonBeanProcessorMatcher == null ? DEFAULT_JSON_BEAN_PROCESSOR_MATCHER
1207 : jsonBeanProcessorMatcher;
1208 }
1209
1210
1211
1212
1213
1214
1215
1216 public void setJsonPropertyFilter( PropertyFilter jsonPropertyFilter ) {
1217 this.jsonPropertyFilter = jsonPropertyFilter;
1218 }
1219
1220
1221
1222
1223
1224
1225 public void setJsonPropertyNameProcessorMatcher( PropertyNameProcessorMatcher propertyNameProcessorMatcher ) {
1226 this.jsonPropertyNameProcessorMatcher = propertyNameProcessorMatcher == null ? DEFAULT_PROPERTY_NAME_PROCESSOR_MATCHER
1227 : propertyNameProcessorMatcher;
1228 }
1229
1230
1231
1232
1233
1234
1235 public void setJsonValueProcessorMatcher( JsonValueProcessorMatcher jsonValueProcessorMatcher ) {
1236 this.jsonValueProcessorMatcher = jsonValueProcessorMatcher == null ? DEFAULT_JSON_VALUE_PROCESSOR_MATCHER
1237 : jsonValueProcessorMatcher;
1238 }
1239
1240
1241
1242
1243
1244
1245 public void setNewBeanInstanceStrategy( NewBeanInstanceStrategy newBeanInstanceStrategy ) {
1246 this.newBeanInstanceStrategy = newBeanInstanceStrategy == null ? DEFAULT_NEW_BEAN_INSTANCE_STRATEGY
1247 : newBeanInstanceStrategy;
1248 }
1249
1250
1251
1252
1253
1254
1255 public void setPropertyExclusionClassMatcher( PropertyExclusionClassMatcher propertyExclusionClassMatcher ) {
1256 this.propertyExclusionClassMatcher = propertyExclusionClassMatcher == null ? DEFAULT_PROPERTY_EXCLUSION_CLASS_MATCHER
1257 : propertyExclusionClassMatcher;
1258 }
1259
1260
1261
1262
1263
1264
1265
1266
1267 public void setPropertyNameProcessorMatcher( PropertyNameProcessorMatcher propertyNameProcessorMatcher ) {
1268 setJavaPropertyNameProcessorMatcher( propertyNameProcessorMatcher );
1269 }
1270
1271
1272
1273
1274
1275
1276 public void setPropertySetStrategy( PropertySetStrategy propertySetStrategy ) {
1277 this.propertySetStrategy = propertySetStrategy;
1278 }
1279
1280
1281
1282
1283
1284
1285
1286 public void setRootClass( Class rootClass ) {
1287 this.rootClass = rootClass;
1288 }
1289
1290
1291
1292
1293
1294 public void setSkipJavaIdentifierTransformationInMapKeys( boolean skipJavaIdentifierTransformationInMapKeys ) {
1295 this.skipJavaIdentifierTransformationInMapKeys = skipJavaIdentifierTransformationInMapKeys;
1296 }
1297
1298
1299
1300
1301
1302
1303
1304 public void unregisterDefaultValueProcessor( Class target ) {
1305 if( target != null ) {
1306 defaultValueMap.remove( target );
1307 }
1308 }
1309
1310
1311
1312
1313
1314
1315
1316 public void unregisterJavaPropertyNameProcessor( Class target ) {
1317 if( target != null ) {
1318 javaPropertyNameProcessorMap.remove( target );
1319 }
1320 }
1321
1322
1323
1324
1325
1326
1327
1328 public void unregisterJsonBeanProcessor( Class target ) {
1329 if( target != null ) {
1330 beanProcessorMap.remove( target );
1331 }
1332 }
1333
1334
1335
1336
1337
1338
1339
1340 public void unregisterJsonPropertyNameProcessor( Class target ) {
1341 if( target != null ) {
1342 jsonPropertyNameProcessorMap.remove( target );
1343 }
1344 }
1345
1346
1347
1348
1349
1350
1351
1352 public void unregisterJsonValueProcessor( Class propertyType ) {
1353 if( propertyType != null ) {
1354 typeMap.remove( propertyType );
1355 }
1356 }
1357
1358
1359
1360
1361
1362
1363
1364
1365 public void unregisterJsonValueProcessor( Class beanClass, Class propertyType ) {
1366 if( beanClass != null && propertyType != null ) {
1367 beanTypeMap.remove( beanClass, propertyType );
1368 }
1369 }
1370
1371
1372
1373
1374
1375
1376
1377
1378 public void unregisterJsonValueProcessor( Class beanClass, String key ) {
1379 if( beanClass != null && key != null ) {
1380 beanKeyMap.remove( beanClass, key );
1381 }
1382 }
1383
1384
1385
1386
1387
1388
1389
1390 public void unregisterJsonValueProcessor( String key ) {
1391 if( key != null ) {
1392 keyMap.remove( key );
1393 }
1394 }
1395
1396
1397
1398
1399
1400
1401
1402
1403 public void unregisterPropertyExclusion( Class target, String propertyName ) {
1404 if( target != null && propertyName != null ) {
1405 Set set = (Set) exclusionMap.get( target );
1406 if( set == null ) {
1407 set = new HashSet();
1408 exclusionMap.put( target, set );
1409 }
1410 set.remove( propertyName );
1411 }
1412 }
1413
1414
1415
1416
1417
1418
1419
1420 public void unregisterPropertyExclusions( Class target ) {
1421 if( target != null ) {
1422 Set set = (Set) exclusionMap.get( target );
1423 if( set != null ) {
1424 set.clear();
1425 }
1426 }
1427 }
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437 public void unregisterPropertyNameProcessor( Class target ) {
1438 unregisterJavaPropertyNameProcessor( target );
1439 }
1440 }