1111namespace WebFiori \Json ;
1212
1313/**
14- * A class to convert Json instance to it's JSON string representation.
14+ * A class to convert objects and Json instances to their JSON string representation.
15+ *
16+ * The conversion of plain objects follows this order of precedence:
17+ * <ol>
18+ * <li>If the object implements {@see JsonI}, its {@see JsonI::toJSON()} method is called.</li>
19+ * <li>If the object is already an instance of {@see Json}, it is returned as-is.</li>
20+ * <li>Otherwise, public getter methods (prefixed with 'get') are called and their return
21+ * values are mapped to properties. The property name is derived by stripping the 'get'
22+ * prefix (e.g. <code>getFullName()</code> becomes <code>FullName</code>). Methods that
23+ * return false or null are skipped.</li>
24+ * <li>Finally, all public properties are extracted via reflection and added to the JSON
25+ * output. Unlike getter-based mapping, public properties with a null value are included.</li>
26+ * </ol>
1527 *
1628 * @author Ibrahim
17- *
29+ *
1830 */
1931class JsonConverter {
2032 private static $ CRLF = "\r\n" ;
@@ -23,25 +35,31 @@ class JsonConverter {
2335 private static $ TabSize = 0 ;
2436 private static $ XmlClosingPool = [];
2537 /**
26- * Convert an object to Json object.
27- *
28- * Note that the properties which will be in the generated Json
29- * object will depend on the public 'get' methods of the object.
30- * The name of the properties will depend on the name of the method. For
31- * example, if the name of one of the methods is 'getFullName', then
32- * property name will be 'FullName'.
33- *
34- * @param object $obj The object that will be converted.
35- *
36- * @return Json
38+ * Converts a plain PHP object to a {@see Json} instance.
39+ *
40+ * The conversion follows this order:
41+ * <ol>
42+ * <li>If the object implements {@see JsonI}, its {@see JsonI::toJSON()} method is
43+ * called and the result is returned directly.</li>
44+ * <li>If the object is already an instance of {@see Json}, it is returned as-is.</li>
45+ * <li>All public methods whose names start with 'get' are called. The portion of the
46+ * method name after 'get' becomes the property name (e.g. <code>getFullName()</code>
47+ * produces the key <code>FullName</code>). Methods returning false or null are
48+ * skipped.</li>
49+ * <li>All public properties are extracted via {@see \ReflectionClass} and added to
50+ * the result. Properties with a null value are included; private and protected
51+ * properties are ignored.</li>
52+ * </ol>
53+ *
54+ * @param object $obj The object to convert.
55+ *
56+ * @return Json A Json instance populated with the object's data.
3757 */
3858 public static function objectToJson ($ obj ) {
3959 if (is_subclass_of ($ obj , 'Webfiori \\Json \\JsonI ' )) {
4060 return $ obj ->toJSON ();
41- } else {
42- if ($ obj instanceof Json) {
43- return $ obj ;
44- }
61+ } else if ($ obj instanceof Json) {
62+ return $ obj ;
4563 }
4664
4765 $ methods = get_class_methods ($ obj );
@@ -61,8 +79,18 @@ public static function objectToJson($obj) {
6179 }
6280 }
6381 }
82+
6483 restore_error_handler ();
6584
85+ $ reflection = new \ReflectionClass ($ obj );
86+ $ publicProps = $ reflection ->getProperties (\ReflectionProperty::IS_PUBLIC );
87+
88+ foreach ($ publicProps as $ prop ) {
89+ $ name = $ prop ->getName ();
90+ $ value = $ prop ->getValue ($ obj );
91+ $ json ->add ($ name , $ value );
92+ }
93+
6694 return $ json ;
6795 }
6896 /**
@@ -330,13 +358,18 @@ private static function getNumberVal($val) {
330358 return $ retVal ;
331359 }
332360 /**
333- *
334- * @param object $probVal
335- *
336- * @param string $style
337- *
338- * @return string
339- *
361+ * Converts an object value to its JSON string representation.
362+ *
363+ * If the value is not already a {@see Json} instance and does not implement
364+ * {@see JsonI}, it is first passed through {@see self::objectToJson()} which
365+ * maps public getter methods and public properties via reflection.
366+ *
367+ * @param object $probVal The object to convert.
368+ * @param string $style The property naming style to apply.
369+ * @param string $lettersCase The letter case to apply to property names.
370+ *
371+ * @return string JSON object string representation.
372+ *
340373 * @since 1.0
341374 */
342375 private static function objToJson ($ probVal , string $ style , string $ lettersCase ) {
0 commit comments