Permalink
Browse files

JSON::Tiny frontend module can parse and create JSON

  • Loading branch information...
1 parent 7dddcc9 commit 582830b3bdec11b7418ff85d373a9adc1d8c5f0b @viklund viklund committed Jun 13, 2009
Showing with 83 additions and 12 deletions.
  1. +2 −0 .gitignore
  2. +2 −1 Makefile.in
  3. +31 −0 lib/JSON/Tiny.pm
  4. +3 −3 t/{parse.t → 01-parse.t}
  5. +7 −7 t/{structure.t → 02-structure.t}
  6. +1 −1 t/{unicode.t → 03-unicode.t}
  7. +37 −0 t/04-generate_json.t
View
@@ -1 +1,3 @@
*.swp
+Makefile
+*.pir
View
@@ -2,7 +2,8 @@ PERL6=<PERL6>
RAKUDO_DIR=<RAKUDO_DIR>
PERL6LIB='<PERL6LIB>:$(RAKUDO_DIR)'
-SOURCES=lib/JSON/Tiny/Actions.pm lib/JSON/Tiny/Grammar.pm
+SOURCES=lib/JSON/Tiny/Actions.pm lib/JSON/Tiny/Grammar.pm \
+ lib/JSON/Tiny.pm
PIRS=$(SOURCES:.pm=.pir)
View
@@ -0,0 +1,31 @@
+module JSON::Tiny {
+ use JSON::Tiny::Actions;
+ use JSON::Tiny::Grammar;
+
+ sub from_json($text) is export {
+ my $a = JSON::Tiny::Actions.new();
+ my $o = JSON::Tiny::Grammar.parse($text, :action($a));
+ return $o.ast;
+ }
+
+ multi to_json(Num $d) is export { $d }
+ multi to_json(Int $d) { $d }
+ multi to_json(Str $d) {
+ '"'
+ ~ $d
+ ~ '"'
+ }
+ multi to_json(Array $data) {
+ return '[ '
+ ~ (map { to_json($_) }, $data.values).join(', ')
+ ~ ' ]';
+ }
+ multi to_json(Hash $data) {
+ return '{ '
+ ~ (map { to_json(.key) ~ ' : ' ~ to_json(.value) }, $data.pairs).join(', ')
+ ~ ' }';
+ }
+ multi to_json(Bool $data) { $data ?? 'true' !! 'false'; }
+ multi to_json($s) { die }
+}
+# vim: ft=perl6
@@ -4,7 +4,7 @@ BEGIN { @*INC.push('lib') };
use JSON::Tiny::Grammar;
use Test;
-my @t =
+my @t =
'{ }',
'{ "a" : "b" }',
'{ "a" : null }',
@@ -119,7 +119,7 @@ my @t =
"width": 500,
"height": 500
},
- "image": {
+ "image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
@@ -139,7 +139,7 @@ my @t =
}}>>,
;
-my @n =
+my @n =
'{ ',
'{ 3 : 4 }',
'{ 3 : tru }', # not quite true
@@ -1,10 +1,11 @@
use v6;
BEGIN { @*INC.push('lib') };
-use JSON::Tiny::Grammar;
-use JSON::Tiny::Actions;
+use JSON::Tiny;
+#use JSON::Tiny::Grammar;
+#use JSON::Tiny::Actions;
use Test;
-my @t =
+my @t =
'{ "a" : 1 }' => { a => 1 },
'[]' => [],
'{}' => {},
@@ -61,10 +62,9 @@ my @t =
plan +@t;
for @t -> $p {
- my $a = JSON::Tiny::Actions.new();
- my $o = JSON::Tiny::Grammar.parse($p.key, :action($a));
- is_deeply $o.ast, $p.value, "Correct data structure for «{$p.key}»"
- or say "# Got: {$o.ast.perl}\n# Expected: {$p.value.perl}";
+ my $s = from_json($p.key);
+ is_deeply $s, $p.value, "Correct data structure for «{$p.key}»"
+ or say "# Got: $s\n# Expected: {$p.value.perl}";
}
# vim: ft=perl6
@@ -6,7 +6,7 @@ use JSON::Tiny::Actions;
use Test;
-my @t =
+my @t =
'{ "a" : "b\u00E5" }' => { 'a' => '' },
'[ "\u2685" ]' => [ '' ];
View
@@ -0,0 +1,37 @@
+use JSON::Tiny;
+use Test;
+
+my @s =
+ 'Int' => [ 1 ],
+ 'Num' => [ 3.2 ],
+ 'Str' => [ 'one' ],
+ 'Empty Array' => [ ],
+ 'Array of Int' => [ 1, 2, 3, 123123123 ],
+ 'Array of Num' => [ 1.3, 2.8, 32323423.4, 4.0 ],
+ 'Array of Str' => [ <one two three gazooba> ],
+ 'Empty Hash' => {},
+ 'Hash of Int' => { :one(1), :two(2), :three(3) },
+ 'Hash of Num' => { :one-and-some{1}, :almost-pie(3.3) },
+ 'Hash of Str' => { :one<yes_one>, :two<but_two> },
+ 'Array of Stuff' => [ { 'A hash' => 1 }, [<an array again>], 2],
+ 'Hash of Stuff' =>
+ {
+ keyone => [<an array>],
+ keytwo => "A string",
+ keythree => { "another" => "hash" },
+ keyfour => 4,
+ keyfive => False,
+ keysix => True,
+ keyseven => 3.2,
+ };
+
+plan +@s;
+
+for @s {
+ warn "The json is <{ to_json( .value ) }>";
+ my $r = from_json( to_json( .value ) );
+ is_deeply $r, .value, .key
+ or say "# Got: {$r.perl}\n# Expected: {$_.perl}";
+}
+
+# vim: ft=perl6

0 comments on commit 582830b

Please sign in to comment.