@@ -14,32 +14,6 @@ class JsonConverter {
1414 private static $ Tab = '' ;
1515 private static $ TabSize = 0 ;
1616 private static $ XmlClosingPool = [];
17- /**
18- * Converts an instance of Json to JSONx string.
19- *
20- * @param Json $json The object that holds the attributes.
21- *
22- * @return string Returns XML string that represents Json schema.
23- *
24- * @since 1.0
25- */
26- public static function toJsonXString (Json $ json ) {
27- if (self ::$ CurrentTab == 0 ) {
28- self ::setIsFormatted (true );
29- }
30- $ retVal = '<?xml version="1.0" encoding="UTF-8"?> ' .self ::$ CRLF ;
31- $ retVal .= '<json:object xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd" '
32- .'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
33- .'xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"> ' .self ::$ CRLF ;
34- self ::push ('json:object ' );
35-
36- foreach ($ json ->getProperties () as $ prop ) {
37- $ retVal .= self ::propertyToJsonXString ($ prop );
38- }
39- $ retVal .= self ::pop ();
40-
41- return $ retVal ;
42- }
4317 /**
4418 * Convert an object to Json object.
4519 *
@@ -158,6 +132,32 @@ public static function toJsonString(Json $jsonObj, $formatted = false) {
158132
159133 return $ jsonString ;
160134 }
135+ /**
136+ * Converts an instance of Json to JSONx string.
137+ *
138+ * @param Json $json The object that holds the attributes.
139+ *
140+ * @return string Returns XML string that represents Json schema.
141+ *
142+ * @since 1.0
143+ */
144+ public static function toJsonXString (Json $ json ) {
145+ if (self ::$ CurrentTab == 0 ) {
146+ self ::setIsFormatted (true );
147+ }
148+ $ retVal = '<?xml version="1.0" encoding="UTF-8"?> ' .self ::$ CRLF ;
149+ $ retVal .= '<json:object xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd" '
150+ .'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
151+ .'xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"> ' .self ::$ CRLF ;
152+ self ::push ('json:object ' );
153+
154+ foreach ($ json ->getProperties () as $ prop ) {
155+ $ retVal .= self ::propertyToJsonXString ($ prop );
156+ }
157+ $ retVal .= self ::pop ();
158+
159+ return $ retVal ;
160+ }
161161
162162 /**
163163 *
@@ -217,11 +217,34 @@ private static function arrayToJsonX(Property $propObj, $value) {
217217
218218 return $ retVal ;
219219 }
220+ private static function checkJsonType ($ val , $ valType , $ propsStyle , $ asObj ) {
221+ $ retVal = '' ;
222+
223+ if ($ valType == JsonTypes::STRING ) {
224+ $ retVal .= '" ' .Json::escapeJSONSpecialChars ($ val ).'" ' ;
225+ } else if ($ valType == JsonTypes::INT || $ valType == JsonTypes::DOUBLE ) {
226+ $ retVal .= self ::getNumberVal ($ val );
227+ } else if ($ valType == JsonTypes::NUL ) {
228+ $ retVal .= 'null ' ;
229+ } else if ($ valType == JsonTypes::BOOL ) {
230+ if ($ val === true ) {
231+ $ retVal .= 'true ' ;
232+ } else {
233+ $ retVal .= 'false ' ;
234+ }
235+ } else if ($ valType == JsonTypes::OBJ ) {
236+ $ retVal .= self ::objToJson ($ val , $ propsStyle );
237+ } else if ($ valType == JsonTypes::ARR ) {
238+ $ retVal .= self ::arrayToJsonString ($ val , $ asObj , $ propsStyle );
239+
240+ }
241+ return $ retVal ;
242+ }
220243 private static function checkJsonXType ($ datatype , $ value , Property $ prop = null , $ isArrayValue = false ) {
221244 $ retVal = self ::$ Tab ;
222245 $ propX = new Property ('x ' , $ value );
223246 $ propX ->setStyle ($ prop ->getStyle ());
224-
247+
225248 if ($ datatype == JsonTypes::STRING ) {
226249 if ($ isArrayValue ) {
227250 $ retVal = self ::propertyToJsonXString ($ propX , false );
@@ -259,42 +282,18 @@ private static function checkJsonXType($datatype, $value, Property $prop = null,
259282 } else if ($ datatype == JsonTypes::ARR ) {
260283 if ($ isArrayValue ) {
261284 $ retVal .= substr (self ::propertyToJsonXString ($ propX , false ), self ::$ CurrentTab * self ::$ TabSize );
262- } else {
263- if ($ prop ->isAsObject ()) {
264- $ jsonObj = new Json ();
265- $ jsonObj ->setPropsStyle ($ prop ->getStyle ());
266-
267- foreach ($ value as $ key => $ val ) {
268- $ jsonObj ->add ($ key , $ val , true );
269- }
270- $ retVal = self ::objToJsonX ($ prop , $ jsonObj );
271- } else {
272- $ retVal = self ::arrayToJsonX ($ prop , $ value );
273- }
274- }
275- }
285+ } else if ($ prop ->isAsObject ()) {
286+ $ jsonObj = new Json ();
287+ $ jsonObj ->setPropsStyle ($ prop ->getStyle ());
276288
277- return $ retVal ;
278- }
279- private static function checkJsonType ($ val , $ valType , $ propsStyle , $ asObj ) {
280- $ retVal = '' ;
281-
282- if ($ valType == JsonTypes::STRING ) {
283- $ retVal .= '" ' .Json::escapeJSONSpecialChars ($ val ).'" ' ;
284- } else if ($ valType == JsonTypes::INT || $ valType == JsonTypes::DOUBLE ) {
285- $ retVal .= self ::getNumberVal ($ val );
286- } else if ($ valType == JsonTypes::NUL ) {
287- $ retVal .= 'null ' ;
288- } else if ($ valType == JsonTypes::BOOL ) {
289- if ($ val === true ) {
290- $ retVal .= 'true ' ;
289+ foreach ($ value as $ key => $ val ) {
290+ $ jsonObj ->add ($ key , $ val , true );
291+ }
292+ $ retVal = self ::objToJsonX ($ prop , $ jsonObj );
291293 } else {
292- $ retVal .= ' false ' ;
294+ $ retVal = self :: arrayToJsonX ( $ prop , $ value ) ;
293295 }
294- } else if ($ valType == JsonTypes::OBJ ) {
295- $ retVal .= self ::objToJson ($ val , $ propsStyle );
296- } else if ($ valType == JsonTypes::ARR ) {
297- $ retVal .= self ::arrayToJsonString ($ val , $ asObj , $ propsStyle );
296+
298297 }
299298
300299 return $ retVal ;
@@ -312,8 +311,10 @@ private static function getNumberVal($val) {
312311
313312 if (is_nan ($ retVal )) {
314313 $ retVal = '"NaN" ' ;
315- } else if ($ val == INF ) {
316- $ retVal = '"Infinity" ' ;
314+ } else {
315+ if ($ val == INF ) {
316+ $ retVal = '"Infinity" ' ;
317+ }
317318 }
318319
319320 return $ retVal ;
0 commit comments