1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.json.util;
18
19 import net.sf.json.JSONArray;
20 import net.sf.json.JSONException;
21 import net.sf.json.JSONObject;
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public abstract class CycleDetectionStrategy {
36 public static final JSONArray IGNORE_PROPERTY_ARR = new JSONArray();
37 public static final JSONObject IGNORE_PROPERTY_OBJ = new JSONObject();
38
39
40 public static final CycleDetectionStrategy LENIENT = new LenientCycleDetectionStrategy();
41
42
43
44
45 public static final CycleDetectionStrategy NOPROP = new LenientNoRefCycleDetectionStrategy();
46
47 public static final CycleDetectionStrategy STRICT = new StrictCycleDetectionStrategy();
48
49
50
51
52
53
54
55 public abstract JSONArray handleRepeatedReferenceAsArray( Object reference );
56
57
58
59
60
61
62
63 public abstract JSONObject handleRepeatedReferenceAsObject( Object reference );
64
65 private static final class LenientCycleDetectionStrategy extends CycleDetectionStrategy {
66 public JSONArray handleRepeatedReferenceAsArray( Object reference ) {
67 return new JSONArray();
68 }
69
70 public JSONObject handleRepeatedReferenceAsObject( Object reference ) {
71 return new JSONObject( true );
72 }
73 }
74
75
76
77
78
79
80
81 private static final class LenientNoRefCycleDetectionStrategy extends CycleDetectionStrategy {
82 public JSONArray handleRepeatedReferenceAsArray( Object reference ) {
83 return IGNORE_PROPERTY_ARR;
84 }
85
86 public JSONObject handleRepeatedReferenceAsObject( Object reference ) {
87 return IGNORE_PROPERTY_OBJ;
88 }
89 }
90
91 private static final class StrictCycleDetectionStrategy extends CycleDetectionStrategy {
92 public JSONArray handleRepeatedReferenceAsArray( Object reference ) {
93 throw new JSONException( "There is a cycle in the hierarchy!" );
94 }
95
96 public JSONObject handleRepeatedReferenceAsObject( Object reference ) {
97 throw new JSONException( "There is a cycle in the hierarchy!" );
98 }
99 }
100 }