JsonView extends AbstractView
A JSON view
Table of Contents
Constants
- EXPOSE_CLASSNAME_FULLY_QUALIFIED = 1
- Definition for the class name exposure configuration, that is, if the class name of an object should also be part of the output JSON, if configured.
- EXPOSE_CLASSNAME_UNQUALIFIED = 2
- Puts out only the actual class name without namespace.
Properties
- $configuration : array<string|int, mixed>
- The rendering configuration for this JSON view which determines which properties of each variable to render.
- $currentVariable : string
- $persistenceManager : PersistenceManagerInterface
- $variables : array<string|int, mixed>
- View variables and their values
- $variablesToRender : array<string|int, string>
- Only variables whose name is contained in this array will be rendered
Methods
- assign() : self
- Add a variable to $this->viewData.
- assignMultiple() : self
- Add multiple variables to $this->viewData.
- injectPersistenceManager() : void
- render() : string
- Transforms the value view variable to a serializable array representation using a YAML view configuration and JSON encodes the result.
- renderPartial() : mixed
- renderSection() : mixed
- setConfiguration() : void
- setVariablesToRender() : void
- Specifies which variables this JsonView should render By default only the variable 'value' will be rendered
- renderArray() : mixed
- Loads the configuration and transforms the value to a serializable array.
- transformObject() : array<string|int, mixed>|string
- Traverses the given object structure in order to transform it into an array structure.
- transformValue() : mixed
- Transforms a value depending on type recursively using the supplied configuration.
Constants
EXPOSE_CLASSNAME_FULLY_QUALIFIED
Definition for the class name exposure configuration, that is, if the class name of an object should also be part of the output JSON, if configured.
public
mixed
EXPOSE_CLASSNAME_FULLY_QUALIFIED
= 1
Setting this value, the object's class name is fully put out, including the namespace.
EXPOSE_CLASSNAME_UNQUALIFIED
Puts out only the actual class name without namespace.
public
mixed
EXPOSE_CLASSNAME_UNQUALIFIED
= 2
See EXPOSE_CLASSNAME_FULL for the meaning of the constant at all.
Properties
$configuration
The rendering configuration for this JSON view which determines which properties of each variable to render.
protected
array<string|int, mixed>
$configuration
= []
The configuration array must have the following structure:
Example 1:
[ 'variable1' => [ '_only' => ['property1', 'property2', ...] ], 'variable2' => [ '_exclude' => ['property3', 'property4, ...] ], 'variable3' => [ '_exclude' => ['secretTitle'], '_descend' => [ 'customer' => [ '_only' => ['firstName', 'lastName'] ] ] ], 'somearrayvalue' => [ '_descendAll' => [ '_only' => ['property1'] ] ] ]
Of variable1 only property1 and property2 will be included. Of variable2 all properties except property3 and property4 are used. Of variable3 all properties except secretTitle are included.
If a property value is an array or object, it is not included by default. If, however, such a property is listed in a "_descend" section, the renderer will descend into this sub structure and include all its properties (of the next level).
The configuration of each property in "_descend" has the same syntax like at the top level. Therefore - theoretically - infinitely nested structures can be configured.
To export indexed arrays the "_descendAll" section can be used to include all array keys for the output. The configuration inside a "_descendAll" will be applied to each array element.
Example 2: exposing object identifier
[ 'variableFoo' => [ '_exclude' => ['secretTitle'], '_descend' => [ 'customer' => [ // consider 'customer' being a persisted entity '_only' => ['firstName'], '_exposeObjectIdentifier' => TRUE, '_exposedObjectIdentifierKey' => 'guid' ] ] ] ]
Note for entity objects you are able to expose the object's identifier also, just add an "_exposeObjectIdentifier" directive set to TRUE and an additional property '__identity' will appear keeping the persistence identifier. Renaming that property name instead of '__identity' is also possible with the directive "_exposedObjectIdentifierKey". Example 2 above would output (summarized): {"customer":{"firstName":"John","guid":"892693e4-b570-46fe-af71-1ad32918fb64"}}
Example 3: exposing object's class name
[ 'variableFoo' => [ '_exclude' => ['secretTitle'], '_descend' => [ 'customer' => [ // consider 'customer' being an object '_only' => ['firstName'], '_exposeClassName' => \TYPO3\CMS\Extbase\Mvc\View\JsonView::EXPOSE_CLASSNAME_FULLY_QUALIFIED ] ] ] ]
The _exposeClassName
is similar to the objectIdentifier one, but the class name is added to the
JSON object output, for example (summarized):
{"customer":{"firstName":"John","__class":"Acme\Foo\Domain\Model\Customer"}}
The other option is EXPOSE_CLASSNAME_UNQUALIFIED which only will give the last part of the class without the namespace, for example (summarized): {"customer":{"firstName":"John","__class":"Customer"}} This might be of interest to not provide information about the package or domain structure behind.
$currentVariable
protected
string
$currentVariable
= ''
$persistenceManager
protected
PersistenceManagerInterface
$persistenceManager
$variables
View variables and their values
protected
array<string|int, mixed>
$variables
= []
Tags
$variablesToRender
Only variables whose name is contained in this array will be rendered
protected
array<string|int, string>
$variablesToRender
= ['value']
Methods
assign()
Add a variable to $this->viewData.
public
assign(string $key, mixed $value) : self
Can be chained, so $this->view->assign(..., ...)->assign(..., ...); is possible
Parameters
- $key : string
-
Key of variable
- $value : mixed
-
Value of object
Return values
self —an instance of $this, to enable chaining
assignMultiple()
Add multiple variables to $this->viewData.
public
assignMultiple(array<string|int, mixed> $values) : self
Parameters
- $values : array<string|int, mixed>
-
array in the format array(key1 => value1, key2 => value2).
Return values
self —an instance of $this, to enable chaining
injectPersistenceManager()
public
injectPersistenceManager(PersistenceManagerInterface $persistenceManager) : void
Parameters
- $persistenceManager : PersistenceManagerInterface
render()
Transforms the value view variable to a serializable array representation using a YAML view configuration and JSON encodes the result.
public
render() : string
Return values
string —The JSON encoded variables
renderPartial()
public
renderPartial(mixed $partialName, mixed $sectionName, array<string|int, mixed> $variables[, mixed $ignoreUnknown = false ]) : mixed
Parameters
- $partialName : mixed
- $sectionName : mixed
- $variables : array<string|int, mixed>
- $ignoreUnknown : mixed = false
renderSection()
public
renderSection(mixed $sectionName[, array<string|int, mixed> $variables = [] ][, mixed $ignoreUnknown = false ]) : mixed
Parameters
- $sectionName : mixed
- $variables : array<string|int, mixed> = []
- $ignoreUnknown : mixed = false
setConfiguration()
public
setConfiguration(array<string|int, mixed> $configuration) : void
Parameters
- $configuration : array<string|int, mixed>
-
The rendering configuration for this JSON view
setVariablesToRender()
Specifies which variables this JsonView should render By default only the variable 'value' will be rendered
public
setVariablesToRender(array<string|int, mixed> $variablesToRender) : void
Parameters
- $variablesToRender : array<string|int, mixed>
renderArray()
Loads the configuration and transforms the value to a serializable array.
protected
renderArray() : mixed
transformObject()
Traverses the given object structure in order to transform it into an array structure.
protected
transformObject(object $object, array<string|int, mixed> $configuration) : array<string|int, mixed>|string
Parameters
- $object : object
-
Object to traverse
- $configuration : array<string|int, mixed>
-
Configuration for transforming the given object or NULL
Return values
array<string|int, mixed>|string —Object structure as an array or as a rendered string (for a DateTime instance)
transformValue()
Transforms a value depending on type recursively using the supplied configuration.
protected
transformValue(mixed $value, array<string|int, mixed> $configuration[, bool $firstLevel = false ]) : mixed
Parameters
- $value : mixed
-
The value to transform
- $configuration : array<string|int, mixed>
-
Configuration for transforming the value
- $firstLevel : bool = false
Return values
mixed —The transformed value