json-builder.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. JsonBuilder
  2. ===========
  3. Overview
  4. --------
  5. The JSON builder allows you to build your JSON through the Symfony
  6. `PropertyAccess Component`_ while keeping the control of the value escaping.
  7. Create a builder
  8. ~~~~~~~~~~~~~~~~
  9. To build some JSON, you will need to instantiate a builder::
  10. use FOS\CKEditorBundle\Builder\JsonBuilder;
  11. $builder = new JsonBuilder();
  12. Set your values
  13. ~~~~~~~~~~~~~~~
  14. To set your values on the builder, you can either use ``setValues`` or
  15. ``setValue`` but be aware they don't behave same. Basically, ``setValues``
  16. allows you to append a set of values in the builder without escaping
  17. control whereas ``setValue`` allows you to append one value in the builder
  18. but with escaping control.
  19. Append a set of values
  20. ~~~~~~~~~~~~~~~~~~~~~~
  21. To append a set of values in the builder, just use ``setValues`` and
  22. pass your values as first argument:
  23. .. code-block:: php
  24. $builder->setValues(array('foo' => array('bar')));
  25. Additionally, this method takes as second argument a path prefix (`PropertyAccess Component`_)
  26. which allows you to append your values where you want in the builder graph.
  27. So, the next sample is basically the equivalent of the precedent::
  28. $builder->setValues(array('bar'), '[foo]');
  29. Append one value
  30. ~~~~~~~~~~~~~~~~
  31. To append one value in the builder, just use ``setValue`` and pass the
  32. path as first argument and the value as second one::
  33. $builder->setValue('[foo][0]','bar');
  34. If you want to keep control of the value escaping, this part is for you.
  35. Basically, just pass ``false`` as third argument::
  36. $builder->setValue('[foo][0]','bar', false);
  37. Configure the JSON encode options
  38. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39. By default, the JSON builder uses the native ``json_encode`` options.
  40. To override it, you can use::
  41. $builder->setJsonEncodeOptions(JSON_FORCE_OBJECT);
  42. $jsonEncodeOptions = $builder->getJsonEncodeOptions();
  43. Values for those options can be found in `PHP documentation for json_decode()`_
  44. Build your JSON
  45. ~~~~~~~~~~~~~~~
  46. Once your builder is well configured, you can build your JSON::
  47. $json = $builder->build();
  48. Reset the builder
  49. -----------------
  50. Because the builder is stateful (keep a track of every values), you
  51. need to reset it if you want to restart a json build::
  52. $builder->reset();
  53. Example
  54. ~~~~~~~
  55. .. code-block:: php
  56. use FOS\CKEditorBundle\Builder\JsonBuilder;
  57. $builder = new JsonBuilder();
  58. // {"0":"foo","1":bar}
  59. echo $builder
  60. ->setJsonEncodeOptions(JSON_FORCE_OBJECT)
  61. ->setValues(array('foo'))
  62. ->setValue('[1]', 'bar', false)
  63. ->build();
  64. // {"foo":["bar"],"baz":bat}
  65. echo $builder
  66. ->reset()
  67. ->setValues(array('foo' => array('bar')))
  68. ->setValue('[baz]', 'bat', false)
  69. ->build();
  70. .. _`PHP documentation for json_decode()`: http://php.net/manual/en/function.json-encode.php
  71. .. _`PropertyAccess Component`: http://symfony.com/doc/current/components/property_access/index.html