1 package net.minidev.json;
2
3 /*
4  *    Copyright 2011 JSON-SMART authors
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 import java.io.IOException;
19
20 import net.minidev.json.JStylerObj.MustProtect;
21 import net.minidev.json.JStylerObj.StringProtector;
22
23 /**
24  * JSONStyle object configure JSonSerializer reducing output size
25  * 
26  * @author Uriel Chemouni <uchemouni@gmail.com>
27  */

28 public class JSONStyle {
29     /**
30      * for advanced usage sample see
31      * 
32      * #see net.minidev.json.test.TestCompressorFlags
33      */

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     /**
38      * AGRESSIVE have no effect without PROTECT_KEYS or PROTECT_VALUE
39      * 
40      * AGRESSIVE mode allows Json-smart to not protect String containing
41      * special chars
42      */

43     public final static int FLAG_AGRESSIVE = 8;
44     /**
45      * @since 2.1
46      */

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     /**
52      * @since 1.0.9.1
53      */

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     /**
141      * begin Object
142      */

143     public void objectStart(Appendable out) throws IOException {
144         out.append('{');
145     }
146
147     /**
148      * terminate Object
149      */

150     public void objectStop(Appendable out) throws IOException {
151         out.append('}');
152     }
153
154     /**
155      * Start the first Obeject element
156      */

157     public void objectFirstStart(Appendable out) throws IOException {
158     }
159
160     /**
161      * Start a new Object element
162      */

163     public void objectNext(Appendable out) throws IOException {
164         out.append(',');
165     }
166
167     /**
168      * End Of Object element
169      */

170     public void objectElmStop(Appendable out) throws IOException {
171     }
172
173     /**
174      * end of Key in json Object
175      */

176     public void objectEndOfKey(Appendable out) throws IOException {
177         out.append(':');
178     }
179
180     /**
181      * Array start
182      */

183     public void arrayStart(Appendable out) throws IOException {
184         out.append('[');
185     }
186
187     /**
188      * Array Done
189      */

190     public void arrayStop(Appendable out) throws IOException {
191         out.append(']');
192     }
193
194     /**
195      * Start the first Array element
196      */

197     public void arrayfirstObject(Appendable out) throws IOException {
198     }
199
200     /**
201      * Start a new Array element
202      */

203     public void arrayNextElm(Appendable out) throws IOException {
204         out.append(',');
205     }
206
207     /**
208      * End of an Array element
209      */

210     public void arrayObjectEnd(Appendable out) throws IOException {
211     }
212 }
213