JSON http://drupal8.ovh/en/tags/json en YAML or JSON http://drupal8.ovh/en/tutoriels/307/yaml-or-json <span class="field field--name-title field--type-string field--label-hidden">YAML or JSON</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>Is it preferable to use YAML or JSON on drupal 8 ? What are the differents ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Thu, 05/11/2017 - 13:42</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><h2>Yml vs Json</h2> <p>A make a small test to test YAML vs PHP performance. The result is:</p> <table><tbody><tr><td> </td> <td>YAML</td> <td>JSON</td> </tr><tr><td>Encode 30K, x100</td> <td>3s</td> <td>0s</td> </tr><tr><td>Encode 30K, x1000</td> <td>27s</td> <td>1s</td> </tr><tr><td>Decode  30K, x100</td> <td>14s</td> <td>0s</td> </tr><tr><td>Decode  30K, x1000</td> <td>142s</td> <td>2s</td> </tr><tr><td>File Size</td> <td>30K</td> <td>21K</td> </tr></tbody></table><p>   </p> <p>I think no need to explain the result !!!</p> <p>The Test:<br /><code>$file = 'views.view.files.yml';//30KB<br /> $file_yml = file_get_contents($file);<br /> $file_json = file_get_contents($file . '.json');<br /> //Prepare<br /> $data = \Drupal\Component\Serialization\Yaml::decode($file_yml);<br /> //file_put_contents($file . '.json', \Drupal\Component\Serialization\Json::encode($data));<br /> $loops = 1000;<br /> $time = time();<br /> for ($i = 0; $i &lt; $loops; $i++) {<br /> //Decode test<br /> //$temp = \Drupal\Component\Serialization\Yaml::decode($file_yml);<br /> //$temp = \Drupal\Component\Serialization\Json::decode($file_json);<br /> //Encode test<br /> //$temp = \Drupal\Component\Serialization\Yaml::encode($data);<br /> //$temp = \Drupal\Component\Serialization\Json::encode($data);<br /> //Check<br /> //print_r($temp); break;<br /> }<br /> $time = time() - $time;<br /> echo "Time : $time sec\n";</code></p> <p><strong>Nota : On Yaml (.yml) you can appen data without reading (You can't do with JSON). Example:</strong></p> <p><code>$yml = Yaml::encode([$yml_data]);<br /> file_put_contents('file_name.yml', $yml, FILE_APPEND);</code></p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/json" hreflang="en">JSON</a></li> <li><a href="/en/tags/yaml" hreflang="en">YAML</a></li> </ul> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=307&amp;2=comment&amp;3=comment" token="Y6C6rDUbh7mZFTki19bcRrQvXBOTp4GKIlLz3lsgWJc"></drupal-render-placeholder> </section> Thu, 11 May 2017 11:42:12 +0000 editor 307 at http://drupal8.ovh JSON Encode and Decode http://drupal8.ovh/en/tutoriels/145/json-encode-and-decode <span class="field field--name-title field--type-string field--label-hidden">JSON Encode and Decode</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to encode and decode JSON array/string in drupal 8 way like drupal_json_encode and drupal_json_decode in drupal 7 ?</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Wed, 05/18/2016 - 23:02</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Example :</p> <p><code>use Drupal\Component\Serialization\Json;<br /> $json = Json::encode($data);<br /> $data = Json::decode($json);</code></p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/json" hreflang="en">JSON</a></li> </ul> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=145&amp;2=comment&amp;3=comment" token="a33qdQaTba_r0l1vv_FwBZIgC6oqt3O6UNjiRBrveEo"></drupal-render-placeholder> </section> Wed, 18 May 2016 21:02:49 +0000 editor 145 at http://drupal8.ovh Return JSON array as resut - Provide Json Interface http://drupal8.ovh/en/tutoriels/32/return-json-array-as-resut-provide-json-interface <span class="field field--name-title field--type-string field--label-hidden">Return JSON array as resut - Provide Json Interface</span> <div class="clearfix text-formatted field field--name-field-question field--type-text-long field--label-above"> <div class="field__label">Question</div> <div class="field__item"><p>How to provide json array as result of service or as output page.</p> </div> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/en/users/editor" typeof="schema:Person" property="schema:name" datatype="">editor</span></span> <span class="field field--name-created field--type-created field--label-hidden">Fri, 11/20/2015 - 09:47</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>You can do that using Symfony JsonResponse.</p> <p>Exemple 1.<br /><code>return new \Symfony\Component\HttpFoundation\JsonResponse($array);</code></p> <p>Exemple 2.<br /><code>use Symfony\Component\HttpFoundation\JsonResponse;<br />   public function get_result() {<br />     $output = array();<br />     $output[] = array('time' =&gt; date("Y-m-d H:i:s"));<br />     $output[] = array('result'=&gt; "OK");<br />     return new JsonResponse($output);<br />   }</code></p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline clearfix"> <h3 class="field__label inline">Tags</h3> <ul class="links field__items"> <li><a href="/en/tags/drupal-8" hreflang="en">Drupal 8</a></li> <li><a href="/en/tags/code" hreflang="en">Code</a></li> <li><a href="/en/tags/json" hreflang="en">JSON</a></li> </ul> </div> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=32&amp;2=comment&amp;3=comment" token="cBy-k65ZpSZzYuUeFvRtIUCCtXu8GaU6qLNQDMe_kRI"></drupal-render-placeholder> </section> Fri, 20 Nov 2015 08:47:20 +0000 editor 32 at http://drupal8.ovh