Coder Social home page Coder Social logo

json2php's Introduction

json2php

Installation

To install json2php you could clone the project from Github or use NPM to install it.

$ npm install json2php

Usage

Convert JavaScript object/array/string/number/boolean to string that is the corresponding PHP representation.

String

When the content is just a string the output will be the same string.

s = json2php('Hello World!')
// => s = 'Hello World!'

Number

Numbers are the same.

s = json2php(123)
// => s = '123'

Boolean

s = json2php( true )
// => s = 'true'

Undefined/Null

null and undefined are returned as null

s = json2php(undefined)
// => s = 'null'

Array

s = json2php([1, 2, 3])
// => s = 'array(1, 2, 3)'

Object

s = json2php({a: 1, b: 2, c: 'text', false: true, undefined: null})
// => s = "array('a' => 1, 'b' => 2, 'c' => 'text', 'false': true, 'undefined': null)"

Non-valid JSON

s = json2php(new Date())
// => s = "null"

Pretty printing

Create custom 'printers' with json2php.make:

const printer = json2php.make({linebreak:'\n', indent:'\t', shortArraySyntax: true})
printer({one: 3, two: 20, three: [9, 3, 2]})

/* result:.
[
	'one' => 3, 
	'two' => 20, 
	'set' => [
		9, 
		3, 
		2
	]
]
*/

Compressing the output

To compress the output (e.g. to make the resulting PHP as small as possible), use shortArraySyntax with stripSpaces:

const printer = json2php.make({shortArraySyntax: true, stripSpaces: true})
printer({
  arr: [1, 2, 3, 4, 5, {foo: 'surprise!'}],
  obj: {
    arr: [{foo: 'bar', bar: 'baz', arr2: [1, 2]}]
  }
})

// result:
// ['arr'=>[1,2,3,4,5,['foo'=>'surprise!']],'obj'=>['arr'=>[['foo'=>'bar','bar'=>'baz','arr2'=>[1,2]]]],'test'=>'str']

For Contributors

Tests

To run test we use mocha framework.

$ npm test

CoffeeScript Source

But in any case you will depend on coffee-script

$ npm run build

Changelog

0.0.9

  • Add stripSpaces to pretty print options (thanks to @noahtallen)

0.0.7

  • Add shortArraySyntax to pretty print options

0.0.6

  • Add pretty print capability via json2php.make (thanks to @stokesman)

0.0.5

  • Update and clean up (thanks to @SumoTTo)
  • Add boolean type (thanks to @SumoTTo)

0.0.4

  • Fix for single quotes escaping (thanks to @ksky521)

0.0.3

  • Fixed the case when non-valid JSON is passed
  • Fixing the bug with the object section

0.0.2

  • Adding the package.json to Git repository, also package dependency
  • Changes into the file structure
  • Adding CoffeeScript source ( Not finished yet )
  • Adding Cakefile and task test
  • Adding Mocha for test framework.
  • Adding test, src, lib directory
  • Adding tests

0.0.1

  • Init the project into NPM
  • module.exports for Node.js
  • Added json2php into the global scope with global.json2php

json2php's People

Contributors

bdryanovski avatar bitdeli-chef avatar brodanoel avatar daniel-zahariev avatar ksky521 avatar noahtallen avatar sumotto avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

json2php's Issues

Support short array syntax via pretty printing

Short array syntax was added to PHP in 5.4 and means you can use [] instead of array().
Now that pretty print capability was added via 89082da I'm wondering if we can extend that to also support something like json2php.make({linebreak:'\n', indent:'\t', short: true}) which would export the data with the short array syntax.

What do you think?

Custom printer support for adding spaces

Hello, I'd like to be able to remove spaces from PHP arrays, or at least for the library to maintain the original spacing. (My motivation being to reduce the output data size in bytes -- dealing with some constraints and large-ish data sets.)

For example:

json2php({"some":1,"data":2});
// Current result: "array('some' => 1, 'data' => 2)"
// spaces are not required:
// "array('some'=>1,'data'=>2)"

edit: this seemed straightforward, so I opened a PR :)

Improve usage documentation

When you say:

json2php([1, 2, 3])
// => array(1, 2, 3) 

What is the meaning?
Probably you could say:

var foo = json2php([1, 2, 3])
// foo = 'array(1, 2, 3)'

Because, when you say array(1, 2, 3), it sounds... weird... because it's not a string, but, if it's not a string, what is it?
json2php() function return the String that should be evaluated as a PHP script, right?

Incorrect handling of arrays with gaps

Function cannot handle gaps in arrays

import json2php from "json2php";

console.log(json2php([1, , , , , 3]));

Results in:

array(1, , , , , 3)
// Fatal error: Cannot use empty array elements in arrays in /in/KKbIJ on line 3

https://3v4l.org/KKbIJ

Best would be treat arrays like objects.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.