1515import net .sf .jsqlparser .parser .ASTNodeAccessImpl ;
1616
1717/**
18+ * Represents a JSON-Function.<br>
19+ * Currently supported are the types in {@link JsonFunctionType}.<br>
20+ * <br>
21+ * For JSON_OBJECT the parameters are available from {@link #getKeyValuePairs()}<br>
22+ * <br>
23+ * For JSON_ARRAY the parameters are availble from {@link #getExpressions()}.<br>
24+ *
1825 * @author <a href="mailto:andreas@manticore-projects.com">Andreas Reichel</a>
1926 */
20-
2127public class JsonFunction extends ASTNodeAccessImpl implements Expression {
2228 private final ArrayList <JsonKeyValuePair > keyValuePairs = new ArrayList <>();
2329 private final ArrayList <JsonFunctionExpression > expressions = new ArrayList <>();
2430 private JsonFunctionType functionType ;
2531 private JsonAggregateOnNullType onNullType ;
2632 private JsonAggregateUniqueKeysType uniqueKeysType ;
2733
34+ private boolean isStrict = false ;
35+
36+ public JsonFunction () {}
37+
38+ public JsonFunction (JsonFunctionType functionType ) {
39+ this .functionType = functionType ;
40+ }
41+
42+ /**
43+ * Returns the Parameters of an JSON_OBJECT<br>
44+ * The KeyValuePairs may not have both key and value set, in some cases only the Key is set.
45+ *
46+ * @see net.sf.jsqlparser.parser.feature.Feature#allowCommaAsKeyValueSeparator
47+ *
48+ * @return A List of KeyValuePairs, never NULL
49+ */
2850 public ArrayList <JsonKeyValuePair > getKeyValuePairs () {
2951 return keyValuePairs ;
3052 }
3153
54+ /**
55+ * Returns the parameters of JSON_ARRAY<br>
56+ *
57+ * @return A List of {@link JsonFunctionExpression}s, never NULL
58+ */
3259 public ArrayList <JsonFunctionExpression > getExpressions () {
3360 return expressions ;
3461 }
@@ -114,6 +141,19 @@ public JsonFunction withType(String typeName) {
114141 return this ;
115142 }
116143
144+ public boolean isStrict () {
145+ return isStrict ;
146+ }
147+
148+ public void setStrict (boolean strict ) {
149+ isStrict = strict ;
150+ }
151+
152+ public JsonFunction withStrict (boolean strict ) {
153+ this .setStrict (strict );
154+ return this ;
155+ }
156+
117157 @ Override
118158 public <T , S > T accept (ExpressionVisitor <T > expressionVisitor , S context ) {
119159 return expressionVisitor .visit (this , context );
@@ -123,13 +163,9 @@ public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
123163 public StringBuilder append (StringBuilder builder ) {
124164 switch (functionType ) {
125165 case OBJECT :
126- appendObject (builder );
127- break ;
128166 case POSTGRES_OBJECT :
129- appendPostgresObject (builder );
130- break ;
131167 case MYSQL_OBJECT :
132- appendMySqlObject (builder );
168+ appendObject (builder );
133169 break ;
134170 case ARRAY :
135171 appendArray (builder );
@@ -148,35 +184,37 @@ public StringBuilder appendObject(StringBuilder builder) {
148184 if (i > 0 ) {
149185 builder .append (", " );
150186 }
151- if (keyValuePair .isUsingValueKeyword ()) {
152- if (keyValuePair .isUsingKeyKeyword ()) {
153- builder .append ("KEY " );
154- }
155- builder .append (keyValuePair .getKey ()).append (" VALUE " )
156- .append (keyValuePair .getValue ());
157- } else {
158- builder .append (keyValuePair .getKey ()).append (":" ).append (keyValuePair .getValue ());
159- }
160-
161- if (keyValuePair .isUsingFormatJson ()) {
162- builder .append (" FORMAT JSON" );
163- }
187+ keyValuePair .append (builder );
164188 i ++;
165189 }
166190
191+ appendOnNullType (builder );
192+ if (isStrict ) {
193+ builder .append (" STRICT" );
194+ }
195+ appendUniqueKeys (builder );
196+
197+ builder .append (" ) " );
198+
199+ return builder ;
200+ }
201+
202+ private void appendOnNullType (StringBuilder builder ) {
167203 if (onNullType != null ) {
168204 switch (onNullType ) {
169205 case NULL :
170206 builder .append (" NULL ON NULL" );
171207 break ;
172208 case ABSENT :
173- builder .append (" ABSENT On NULL" );
209+ builder .append (" ABSENT ON NULL" );
174210 break ;
175211 default :
176212 // this should never happen
177213 }
178214 }
215+ }
179216
217+ private void appendUniqueKeys (StringBuilder builder ) {
180218 if (uniqueKeysType != null ) {
181219 switch (uniqueKeysType ) {
182220 case WITH :
@@ -189,41 +227,6 @@ public StringBuilder appendObject(StringBuilder builder) {
189227 // this should never happen
190228 }
191229 }
192-
193- builder .append (" ) " );
194-
195- return builder ;
196- }
197-
198-
199- @ SuppressWarnings ({"PMD.CyclomaticComplexity" , "PMD.NPathComplexity" })
200- public StringBuilder appendPostgresObject (StringBuilder builder ) {
201- builder .append ("JSON_OBJECT( " );
202- for (JsonKeyValuePair keyValuePair : keyValuePairs ) {
203- builder .append (keyValuePair .getKey ());
204- if (keyValuePair .getValue () != null ) {
205- builder .append (", " ).append (keyValuePair .getValue ());
206- }
207- }
208- builder .append (" ) " );
209-
210- return builder ;
211- }
212-
213- public StringBuilder appendMySqlObject (StringBuilder builder ) {
214- builder .append ("JSON_OBJECT( " );
215- int i = 0 ;
216- for (JsonKeyValuePair keyValuePair : keyValuePairs ) {
217- if (i > 0 ) {
218- builder .append (", " );
219- }
220- builder .append (keyValuePair .getKey ());
221- builder .append (", " ).append (keyValuePair .getValue ());
222- i ++;
223- }
224- builder .append (" ) " );
225-
226- return builder ;
227230 }
228231
229232 @ SuppressWarnings ({"PMD.CyclomaticComplexity" , "PMD.NPathComplexity" })
@@ -239,18 +242,7 @@ public StringBuilder appendArray(StringBuilder builder) {
239242 i ++;
240243 }
241244
242- if (onNullType != null ) {
243- switch (onNullType ) {
244- case NULL :
245- builder .append (" NULL ON NULL " );
246- break ;
247- case ABSENT :
248- builder .append (" ABSENT ON NULL " );
249- break ;
250- default :
251- // "ON NULL" was omitted
252- }
253- }
245+ appendOnNullType (builder );
254246 builder .append (") " );
255247
256248 return builder ;
0 commit comments