mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-26 15:14:12 +00:00
added Json serialization
This commit is contained in:
parent
871df1f691
commit
599ebd303f
@ -203,6 +203,13 @@ file? | `(file? "test.txt")` | `true` | Return true if the file exists.
|
|||||||
fread | `(fread "test.txt")` | `"content"` | Read the contents of a file.
|
fread | `(fread "test.txt")` | `"content"` | Read the contents of a file.
|
||||||
fwrite | `(fwrite "test.txt" "content")` | `true` | Write string to file. Give optional third parameter as `true` to append.
|
fwrite | `(fwrite "test.txt" "content")` | `true` | Write string to file. Give optional third parameter as `true` to append.
|
||||||
|
|
||||||
|
### Json functions
|
||||||
|
|
||||||
|
Name | Example | Example result | Description
|
||||||
|
------- | ------- | -------------- | -----------
|
||||||
|
to-json | `(to-json { "a" [1 2 3] "b" [4 5 6] })` | `"{\"a\":[1,2,3],\"b\":[4,5,6]}"` | Encode the argument as a JSON string.
|
||||||
|
from-json | `(from-json "{\"a\":[1,2,3],\"b\":[4,5,6]}")` | `{"a":[1 2 3] "b":[4 5 6]}` | Decode the JSON string given as argument.
|
||||||
|
|
||||||
### Math functions
|
### Math functions
|
||||||
|
|
||||||
Name | Example | Example result | Description
|
Name | Example | Example result | Description
|
||||||
@ -293,7 +300,7 @@ pi | M_PI
|
|||||||
|
|
||||||
## Extending
|
## Extending
|
||||||
|
|
||||||
The project is easy to extend because it is trivial to add new functions whether the implementation is defined on the PHP or Lisp side. If the language ends up being used in the future, first plans are to add support for JSON serialization and a HTTP client.
|
The project is easy to extend because it is trivial to add new functions whether the implementation is defined on the PHP or Lisp side.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
51
src/Lib/Json.php
Normal file
51
src/Lib/Json.php
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
namespace MadLisp\Lib;
|
||||||
|
|
||||||
|
use MadLisp\Collection;
|
||||||
|
use MadLisp\CoreFunc;
|
||||||
|
use MadLisp\Env;
|
||||||
|
use MadLisp\Func;
|
||||||
|
use MadLisp\Hash;
|
||||||
|
use MadLisp\MadLispException;
|
||||||
|
use MadLisp\Symbol;
|
||||||
|
use MadLisp\Vector;
|
||||||
|
|
||||||
|
class Json implements ILib
|
||||||
|
{
|
||||||
|
public function register(Env $env): void
|
||||||
|
{
|
||||||
|
$env->set('to-json', new CoreFunc('to-json', 'Encode the argument as a JSON string.', 1, 1,
|
||||||
|
fn ($a) => json_encode($this->getJsonData($a))
|
||||||
|
));
|
||||||
|
|
||||||
|
$env->set('from-json', new CoreFunc('from-json', 'Decode the JSON string given as argument.', 1, 1,
|
||||||
|
fn ($a) => $this->parseJsonData(json_decode($a))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getJsonData($a)
|
||||||
|
{
|
||||||
|
if ($a instanceof Func) {
|
||||||
|
throw new MadLispException("unable to encode function as json");
|
||||||
|
} elseif ($a instanceof Collection) {
|
||||||
|
return array_map([$this, 'getJsonData'], $a->getData());
|
||||||
|
} elseif ($a instanceof Symbol) {
|
||||||
|
// Does this make sense, as we cannot reverse this operation?
|
||||||
|
return $a->getName();
|
||||||
|
} else {
|
||||||
|
return $a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function parseJsonData($a)
|
||||||
|
{
|
||||||
|
if (is_object($a)) {
|
||||||
|
return new Hash(array_map([$this, 'parseJsonData'], (array) $a));
|
||||||
|
} elseif (is_array($a)) {
|
||||||
|
// We have to choose between a List and a Vector...
|
||||||
|
return new Vector(array_map([$this, 'parseJsonData'], $a));
|
||||||
|
} else {
|
||||||
|
return $a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -45,6 +45,7 @@ class LispFactory
|
|||||||
(new Lib\Collections())->register($env);
|
(new Lib\Collections())->register($env);
|
||||||
(new Lib\Compare())->register($env);
|
(new Lib\Compare())->register($env);
|
||||||
(new Lib\IO())->register($env);
|
(new Lib\IO())->register($env);
|
||||||
|
(new Lib\Json())->register($env);
|
||||||
(new Lib\Math())->register($env);
|
(new Lib\Math())->register($env);
|
||||||
(new Lib\Strings())->register($env);
|
(new Lib\Strings())->register($env);
|
||||||
(new Lib\Time())->register($env);
|
(new Lib\Time())->register($env);
|
||||||
|
Loading…
Reference in New Issue
Block a user