承接 alexistm/simple-json-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alexistm/simple-json-php

Composer 安装命令:

composer require alexistm/simple-json-php

包简介

Simple JSON for PHP makes you able to construct JSON and send the headers easily. The main purposes are speed and ease to use.

README 文档

README

Introduction

Simple JSON for PHP simplify the json_encode function. Instead of creating a Stdclass and then json_encode it, send, headers and echo the json, you can simply create the object and use $json->send();.

Pros:

  • Easy: As simple as a Stdclass, bundled functions.
  • Fast: JSON are encoded with the native json_encode()
  • Reliable: Headers are sent automatically
  • Complete: You can add objects, properties or arrays
  • Callback/Variable or simply a JSON option
  • JSONP compatible
  • JQuery compatible

Cons:

  • Optimized for objects because JSON is an object notation.

Usage

<?php

    include('../includes/json.php');
  
    use \Simple\json;
    
    $json = new json();
  
    // Objects to send (fetched from the DB for example)
    $object = new stdClass();
    $object->LastLog = '123456789123456';
    $object->Password = 'Mypassword';
    $object->Dramatic = 'Cat';
    $object->Things = array(1,2,3);
    
    // Forge the JSON
    $json->data = $object;
    $json->user = AlexisTM;
    $json->status = 'online';
    
    // Send the JSON
    $json->send();
?>

Sending the json you want

The constructor allow you to send JSON, JSONP with callback or in a variable.

simply a JSON

  $json->send(options);
  > {  ...  }

Callback JSONP

  $json->send_callback('myCallback', options);
  > myCallback({  ...  });

Varibale JSONP

  $json->send_var('myVariable', options);
  > var myVariable = {  ...  };

Options

Options are the default options passed to json_encode.

JSON_HEX_TAG 
echo "Apos: ",    json_encode($a, JSON_HEX_APOS), "\n";
echo "Quot: ",    json_encode($a, JSON_HEX_QUOT), "\n";
echo "Amp: ",     json_encode($a, JSON_HEX_AMP), "\n";
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
echo "All: ",     json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE)

Will output:

Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["<foo>","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["<foo>","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["<foo>","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["<foo>","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

For example:

$json->send(JSON_HEX_APOS | JSON_UNESCAPED_UNICODE);

HTML/JS part example

This library give you a strong JSON API capabilities. But an API is useless if you do not have the front-end. Here are some examples.

Callback with a raw json using JQuery.ajax

$.ajax({
  dataType: "json",
  url: 'http://example.com',
  data: data,
  done: function(json) {
    alert(json);
  }
}); 

Callback with a raw json using JQuery.getJSON

$.getJSON('http://example.com',
data,
function(json) {
  alert(json);
});

Legacy javascript for dynamic loading for JSONP

function load_script(url) {
  var s = document.createElement('script'); 
  s.src = url;
  document.body.appendChild(s);
}

function load_scripts() {
  load_script('http://json.api/users/list');
}

window.onload=load_scripts;

Validating JSON

To validate the JSON, you can grab back the JSON string via the make() method then pass it through an other library.

$jsonString = $json->make();

Knows dumb errors

  • The file format of the PHP script MUST be UTF-8 Without BOM. Else the JSON is corrupted for the JQuery AJAX request.
  • You can bypass the file format by asking text and not JSON type in the JQuery request and using JSON, then parsing it yourself.
  • If you don't use namespaces, you can call the JSON class via new \Simple\json()
  • If you use use \Simple;, you can call the JSON class via new Simple\json()
  • If you use use \Simple\json;, you can call the JSON class via new json()

Contribute

To contribute, just contact me! The first fork will be awesome for me!

NOTE:

The reason it comes in version 4 which changes a bit the API is the speed. I as wondering how fast it was to use the library and after some tests, it shows it was 6 times slower than the native function. Therefore, for my own sake, it has to be reworked.

It now as fast as the native json_encode, without having to think at all.

Licence

This work is under MIT licence. Short version: You have to add Alexis Paques in the credits but you can use it for closed-source commercial project.

What next ?

The next step is obviously to add routes, which is needed to make a powerful API.

References

Informations: https://en.wikipedia.org/wiki/JSONP

Validator: http://json.parser.online.fr

ECMA-404: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf

json_encode: https://php.net/manual/fr/function.json-encode.php

Comparaison of JSON PHP libs: http://gggeek.altervista.org/sw/article_20061113.html

JSON API Standard: http://jsonapi.org/

Credits

Alexis PAQUES (@AlexisTM)

alexistm/simple-json-php 适用场景与选型建议

alexistm/simple-json-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 232 次下载、GitHub Stars 达 45, 最近一次更新时间为 2015 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 alexistm/simple-json-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 alexistm/simple-json-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 alexistm/simple-json-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 232
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 45
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 45
  • Watchers: 4
  • Forks: 37
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-23