gelight / php-json2sml
PHP Library for conversion JSON to SML
0.0.1
2021-08-15 18:51 UTC
Requires
- stenway/sml: ^0.0.3
This package is auto-updated.
Last update: 2024-06-16 01:21:49 UTC
README
php-json2sml
Converts json into SML document.
What is SML?
Using
Given JSON structure
<?php include_once "vendor/autoload.php"; use GELight\conversion\{JsonToSmlConverter}; $json = <<<JSON { "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 27, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "office", "number": "646 555-4567" } ], "children": [ "Aaron" ], "spouse": true } JSON; $converter = new JsonToSmlConverter(); $doc = $converter::convert(json_decode($json)); $root = $doc->getRoot(); // Returns the first value of attribute "lastName" echo $root ->attribute("lastName") ->getValues()[0]; // Returns the first value of attribute "city" in element "address" echo $root ->element("address") ->attribute("city") ->getValues()[0]; // Returns the first value of attribute "number" of second element "phoneNumbers" echo $root ->element("phoneNumbers") ->elements()[1] ->attribute("number") ->getValues()[0]; // Returns the first value of attribute "children" echo $root ->attribute("children") ->getValues()[0];
Result:
Smith New York 646 555-4567 Aaron
Generated SML document structure as string output
# PHP $doc->toString();
sml firstName John lastName Smith isAlive 1 age 27 address streetAddress "21 2nd Street" city "New York" state NY postalCode 10021-3100 end phoneNumbers phoneNumber type home number "212 555-1234" end phoneNumber type office number "646 555-4567" end end children Aaron spouse 1 end
Documentation
JsonToSmlConverter
Creates a new instance of the JSON to SML converter
$converter = new JsonToSmlConverter();
convert
Static method "convert" will convert your JSON in a SML document.
convert(object $jsonObject): SmlDocument
$smlDocument = $converter::convert(json_decode($json));