1 package net.minidev.json;
2
3
18 import java.io.IOException;
19
20 import net.minidev.json.JStylerObj.MustProtect;
21 import net.minidev.json.JStylerObj.StringProtector;
22
23
28 public class JSONStyle {
29
34 public final static int FLAG_PROTECT_KEYS = 1;
35 public final static int FLAG_PROTECT_4WEB = 2;
36 public final static int FLAG_PROTECT_VALUES = 4;
37
43 public final static int FLAG_AGRESSIVE = 8;
44
47 public final static int FLAG_IGNORE_NULL = 16;
48
49 public final static JSONStyle NO_COMPRESS = new JSONStyle(0);
50 public final static JSONStyle MAX_COMPRESS = new JSONStyle(-1);
51
54 public final static JSONStyle LT_COMPRESS = new JSONStyle(FLAG_PROTECT_4WEB);
55
56 private boolean _protectKeys;
57 private boolean _protect4Web;
58 private boolean _protectValues;
59 private boolean _ignore_null;
60
61 private MustProtect mpKey;
62 private MustProtect mpValue;
63
64 private StringProtector esc;
65
66 public JSONStyle(int FLAG) {
67 _protectKeys = (FLAG & FLAG_PROTECT_KEYS) == 0;
68 _protectValues = (FLAG & FLAG_PROTECT_VALUES) == 0;
69 _protect4Web = (FLAG & FLAG_PROTECT_4WEB) == 0;
70 _ignore_null = (FLAG & FLAG_IGNORE_NULL) > 0;
71
72 MustProtect mp;
73 if ((FLAG & FLAG_AGRESSIVE) > 0)
74 mp = JStylerObj.MP_AGGRESIVE;
75 else
76 mp = JStylerObj.MP_SIMPLE;
77
78 if (_protectValues)
79 mpValue = JStylerObj.MP_TRUE;
80 else
81 mpValue = mp;
82
83 if (_protectKeys)
84 mpKey = JStylerObj.MP_TRUE;
85 else
86 mpKey = mp;
87
88 if (_protect4Web)
89 esc = JStylerObj.ESCAPE4Web;
90 else
91 esc = JStylerObj.ESCAPE_LT;
92 }
93
94 public JSONStyle() {
95 this(0);
96 }
97
98 public boolean protectKeys() {
99 return _protectKeys;
100 }
101
102 public boolean protectValues() {
103 return _protectValues;
104 }
105
106 public boolean protect4Web() {
107 return _protect4Web;
108 }
109
110 public boolean ignoreNull() {
111 return _ignore_null;
112 }
113
114 public boolean indent() {
115 return false;
116 }
117
118 public boolean mustProtectKey(String s) {
119 return mpKey.mustBeProtect(s);
120 }
121
122 public boolean mustProtectValue(String s) {
123 return mpValue.mustBeProtect(s);
124 }
125
126 public void writeString(Appendable out, String value) throws IOException {
127 if (!this.mustProtectValue(value))
128 out.append(value);
129 else {
130 out.append('"');
131 JSONValue.escape(value, out, this);
132 out.append('"');
133 }
134 }
135
136 public void escape(String s, Appendable out) {
137 esc.escape(s, out);
138 }
139
140
143 public void objectStart(Appendable out) throws IOException {
144 out.append('{');
145 }
146
147
150 public void objectStop(Appendable out) throws IOException {
151 out.append('}');
152 }
153
154
157 public void objectFirstStart(Appendable out) throws IOException {
158 }
159
160
163 public void objectNext(Appendable out) throws IOException {
164 out.append(',');
165 }
166
167
170 public void objectElmStop(Appendable out) throws IOException {
171 }
172
173
176 public void objectEndOfKey(Appendable out) throws IOException {
177 out.append(':');
178 }
179
180
183 public void arrayStart(Appendable out) throws IOException {
184 out.append('[');
185 }
186
187
190 public void arrayStop(Appendable out) throws IOException {
191 out.append(']');
192 }
193
194
197 public void arrayfirstObject(Appendable out) throws IOException {
198 }
199
200
203 public void arrayNextElm(Appendable out) throws IOException {
204 out.append(',');
205 }
206
207
210 public void arrayObjectEnd(Appendable out) throws IOException {
211 }
212 }
213