Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. FOSCKEditorBundle
  5. JsonBuilder

JsonBuilder

Edit this page

Overview

The JSON builder allows you to build your JSON through the Symfony PropertyAccess Component while keeping the control of the value escaping.

Create a builder

To build some JSON, you will need to instantiate a builder:

1
2
3
use FOS\CKEditorBundle\Builder\JsonBuilder;

$builder = new JsonBuilder();

Set your values

To set your values on the builder, you can either use setValues or setValue but be aware they don't behave same. Basically, setValues allows you to append a set of values in the builder without escaping control whereas setValue allows you to append one value in the builder but with escaping control.

Append a set of values

To append a set of values in the builder, just use setValues and pass your values as first argument:

1
$builder->setValues(array('foo' => array('bar')));

Additionally, this method takes as second argument a path prefix (PropertyAccess Component) which allows you to append your values where you want in the builder graph. So, the next sample is basically the equivalent of the precedent:

1
$builder->setValues(array('bar'), '[foo]');

Append one value

To append one value in the builder, just use setValue and pass the path as first argument and the value as second one:

1
$builder->setValue('[foo][0]','bar');

If you want to keep control of the value escaping, this part is for you. Basically, just pass false as third argument:

1
$builder->setValue('[foo][0]','bar', false);

Configure the JSON encode options

By default, the JSON builder uses the native json_encode options. To override it, you can use:

1
2
$builder->setJsonEncodeOptions(JSON_FORCE_OBJECT);
$jsonEncodeOptions = $builder->getJsonEncodeOptions();

Values for those options can be found in PHP documentation for json_decode()

Build your JSON

Once your builder is well configured, you can build your JSON:

1
$json = $builder->build();

Reset the builder

Because the builder is stateful (keep a track of every values), you need to reset it if you want to restart a json build:

1
$builder->reset();

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use FOS\CKEditorBundle\Builder\JsonBuilder;

$builder = new JsonBuilder();

// {"0":"foo","1":bar}
echo $builder
    ->setJsonEncodeOptions(JSON_FORCE_OBJECT)
    ->setValues(array('foo'))
    ->setValue('[1]', 'bar', false)
    ->build();

// {"foo":["bar"],"baz":bat}
echo $builder
    ->reset()
    ->setValues(array('foo' => array('bar')))
    ->setValue('[baz]', 'bat', false)
    ->build();
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Show your Sylius expertise

    Show your Sylius expertise

    The life jacket for your team and your project

    The life jacket for your team and your project

    Version:

    Table of Contents

    • Overview
      • Create a builder
      • Set your values
      • Append a set of values
      • Append one value
      • Configure the JSON encode options
      • Build your JSON
    • Reset the builder
      • Example

    Symfony footer

    Avatar of Simon Sargeant, a Symfony contributor

    Thanks Simon Sargeant for being a Symfony contributor

    1 commit • 8 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony