Git Product home page Git Product logo

jsonx-php's Introduction

JSONX for PHP

JSONx is a standard to represent JSON as XML.

You can read the documentation on IBM website.

Features

  • Convert any DOMNode of JSONx into PHP data that can be JSON encoded.

Install

composer require grom/jsonx

Usage

// load jsonx file ...
require_once __DIR__.'/../src/jsonx.php';

// or use composer
require 'vendor/autoload.php';

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<json:object xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
    <json:boolean name="remote">false</json:boolean>
    <json:number name="height">62.4</json:number>
    <json:time name="date">2015-08-29 15:07:00</json:time>
    <json:string name="ficoScore"> > 640</json:string>
</json:object>
XML;

$dom = new \DOMDocument();
$dom->loadXML($xml);

$data = JSONx\parse($dom->documentElement);

var_dump(json_encode($data));
/*
string(69) "{"remote":false,"height":62.4,"date":1440860820,"ficoScore":" > 640"}"
 */

jsonx-php's People

Contributors

gromnan avatar miniche avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

miniche vchabot

jsonx-php's Issues

Convert json:object as an array

Hello,

I would like to convert json:object as an array instead of a \stdClass.

I thought I could pass a second argument to the parse function, called asArray, like this:

function parse(\DOMElement $node, $asArray = false)
{
    switch ($node->nodeName) {
        case 'json:object':
            $data = $asArray ? array() : new \stdClass();
            foreach ($node->childNodes as $childNode) {
                if ($childNode instanceof \DOMElement
                    && null !== $childNode->attributes
                    && $name = $childNode->attributes->getNamedItem('name')) {
                        if ($asArray) {
                            $data[$name->nodeValue] = parse($childNode);
                        } else {
                            $data->{$name->nodeValue} = parse($childNode);
                        }
                }
            }
            break;
// ...
}

Do you think it could be good, or do you have any other idea on this issue?

Vincent

Loosing information with json:time

Hello,

I had an issue yesterday when I ran phpunit after a fresh install.

An error occurred with the complete.test file, with the json:time field.

The issue is, that my PHP is configured with a different timezone than UTC, which causes an error when trying to convert the date specified in my timezone.

I think that globally, you should not convert a json:time to a timestamp, as you loose some information, like the timezone. You should instead return a \DateTime object, which keeps every information.

I know this change could generate a lot of work.

Vincent

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.