src/ObjectMapper.php
public function objectToJson($object, $returnAsString = true)
    {
        $jsonData = [];
        // Reflecting the target object to extract properties etc.
        $class = new \ReflectionObject($object);

        // Iterate over each class property to check if it's mapped
        foreach ($class->getProperties() as $property) {
            // Extract the JsonField Annotation

            /** @var JsonField $field */
            $field = $this->reader->getPropertyAnnotation($property, JsonField::class);

            // Is it not defined, the property is not mapped
            if (null === $field) {
                continue;
            }

            // Check if the property is public accessible or has a setter / adder
            $propertyName = $property->getName();
            $ucw = ucwords($propertyName);
            if (!$property->isPublic() && !($class->hasMethod('get' . $ucw))) {
                throw new PropertyNotAccessibleException($propertyName);
            }

            if ($field->name == null) {
                $field->name = $propertyName;
            }

            $val = null;
            if ($property->isPublic()) {
                $val = $object->{$propertyName};
            } else {
                $val = $object->{'get' . $ucw}();
            }

            // Reverse order on encoding (postTransformer -> transformer -> preTransformer)
            if ($field->postTransformer !== null) {
                /** @var TransformerInterface $postTransformer */
                $postTransformer = $field->postTransformer;
                $val = $postTransformer::reverseTransform($val);
            }

            if ($field->transformer !== null) {
                /** @var TransformerInterface $transformer */
                $transformer = $field->transformer;
                $val = $transformer::reverseTransform($val);
            }

            if (is_null($val)) {
                $jsonData[$field->name] = $val;
                continue;
            }

            if ($field->transformer === null) {
                $types = explode('|', $field->type);
                $type = null;

                foreach ($types as $tString) {
                    $type = $tString;
                    if (!is_object($val) || !in_array(strtolower($tString), $this->primitives)) {
                        break;
                    }
                }
                // Check the type of the field and set the val
                if (in_array($type, $this->primitives)) {
                    $format = 'Y-m-d\TH:i:s';
                    if ($field instanceof DateTimeField && $field->format !== null) {
                        $format = $field->format;
                    }
                    $val = $this->castType($val, $type, $propertyName, $format);
                } elseif ($type != null) {
                    // Check if it's an array of X
                    if (substr($type, -2) == '[]' && is_array($val)) {
                        $tmpVal = [];
                        foreach ($val as $entry) {
                            // Map the data recursive
                            $tmpVal[] = (object)$this->objectToJson($entry, false);
                        }
                        $val = $tmpVal;
                    } elseif (substr($type, -2) != '[]') {
                        // Map the data recursive
                        $val = (object)$this->objectToJson($val, false);
                    }
                }
            }

            if ($field->preTransformer !== null) {
                /** @var TransformerInterface $preTransformer */
                $preTransformer = $field->preTransformer;
                $val = $preTransformer::reverseTransform($val);
            }

            // Assign the JSON data to the object property
            if ($val !== null) {
                // If the property is public accessible, set the value directly
                $jsonData[$field->name] = $val;
            }
        }

        $res = $jsonData;
        if ($returnAsString) {
            $res = json_encode($res, JSON_PRETTY_PRINT);
        }

        return $res;
    }
public function objectToJson($object, $returnAsString = true)
    {
        $jsonData = [];
        // Reflecting the target object to extract properties etc.
        $class = new \ReflectionObject($object);

        // Iterate over each class property to check if it's mapped
        foreach ($class->getProperties() as $property) {
            // Extract the JsonField Annotation

            /** @var JsonField $field */
            $field = $this->reader->getPropertyAnnotation($property, JsonField::class);

            // Is it not defined, the property is not mapped
            if (null === $field) {
                continue;
            }

            // Check if the property is public accessible or has a setter / adder
            $propertyName = $property->getName();
            $ucw = ucwords($propertyName);
            if (!$property->isPublic() && !($class->hasMethod('get' . $ucw))) {
                throw new PropertyNotAccessibleException($propertyName);
            }

            if ($field->name == null) {
                $field->name = $propertyName;
            }

            $val = null;
            if ($property->isPublic()) {
                $val = $object->{$propertyName};
            } else {
                $val = $object->{'get' . $ucw}();
            }

            // Reverse order on encoding (postTransformer -> transformer -> preTransformer)
            if ($field->postTransformer !== null) {
                /** @var TransformerInterface $postTransformer */
                $postTransformer = $field->postTransformer;
                $val = $postTransformer::reverseTransform($val);
            }

            if ($field->transformer !== null) {
                /** @var TransformerInterface $transformer */
                $transformer = $field->transformer;
                $val = $transformer::reverseTransform($val);
            }

            if (is_null($val)) {
                $jsonData[$field->name] = $val;
                continue;
            }

            if ($field->transformer === null) {
                $types = explode('|', $field->type);
                $type = null;

                foreach ($types as $tString) {
                    $type = $tString;
                    if (!is_object($val) || !in_array(strtolower($tString), $this->primitives)) {
                        break;
                    }
                }
                // Check the type of the field and set the val
                if (in_array($type, $this->primitives)) {
                    $format = 'Y-m-d\TH:i:s';
                    if ($field instanceof DateTimeField && $field->format !== null) {
                        $format = $field->format;
                    }
                    $val = $this->castType($val, $type, $propertyName, $format);
                } elseif ($type != null) {
                    // Check if it's an array of X
                    if (substr($type, -2) == '[]' && is_array($val)) {
                        $tmpVal = [];
                        foreach ($val as $entry) {
                            // Map the data recursive
                            $tmpVal[] = (object)$this->objectToJson($entry, false);
                        }
                        $val = $tmpVal;
                    } elseif (substr($type, -2) != '[]') {
                        // Map the data recursive
                        $val = (object)$this->objectToJson($val, false);
                    }
                }
            }

            if ($field->preTransformer !== null) {
                /** @var TransformerInterface $preTransformer */
                $preTransformer = $field->preTransformer;
                $val = $preTransformer::reverseTransform($val);
            }

            // Assign the JSON data to the object property
            if ($val !== null) {
                // If the property is public accessible, set the value directly
                $jsonData[$field->name] = $val;
            }
        }

        $res = $jsonData;
        if ($returnAsString) {
            $res = json_encode($res, JSON_PRETTY_PRINT);
        }

        return $res;
    }
Serializes an object to JSON

@param object $object The object
@param bool $returnAsString For internal usage
@return string|array The JSON-String

@throws ClassNotFoundException If the target class does not exist
@throws PropertyNotAccessibleException If the mapped property is not accessible
@throws TypeMismatchException If the given type in json does not match with the expected type