Skip to content

Commit f45e86c

Browse files
committed
FEAT: ported JSON codec from Red language
Main credits belongs to: Gabriele Santilli and Gregg Irwin. See https://github.com/giesse/red-json for more details.
1 parent 27d7a60 commit f45e86c

File tree

1 file changed

+67
-32
lines changed

1 file changed

+67
-32
lines changed

src/mezz/codec-json.r

+67-32
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,53 @@
11
Red [
22
Title: "JSON codec"
3-
Author: "Gabriele Santilli"
43
File: %json.red
5-
Purpose: "Adds JSON as a valid data type to use with LOAD/AS and SAVE/AS"
6-
Rights: "Copyright (C) 2019 Red Foundation. All rights reserved."
7-
License: {
8-
Distributed under the Boost Software License, Version 1.0.
9-
See https://github.com/red/red/blob/master/BSL-License.txt
10-
}
4+
Version: 0.1.0
5+
Author: [
6+
"Gregg Irwin" {
7+
Ported from %json.r by Romano Paolo Tenca, Douglas Crockford,
8+
and Gregg Irwin.
9+
Further research: json libs by Chris Ross-Gill, Kaj de Vos, and
10+
@WiseGenius.
11+
}
12+
"Gabriele Santilli" {
13+
See History.
14+
}
15+
"Oldes" {
16+
Slightly modified Red's version (0.0.4) for use in Rebol (Oldes' branch).
17+
}
18+
]
19+
History: [
20+
0.0.1 10-Sep-2016 "Gregg" "First release. Based on %json.r"
21+
0.0.2 9-Aug-2018 "Gabriele" "Refactoring and minor improvements"
22+
0.0.3 31-Aug-2018 "Gabriele" "Converted to non-recursive version"
23+
0.0.4 9-Oct-2018 "Gabriele" "Back to an easier to read recursive version"
24+
0.1.0 13-Feb-2020 "Oldes" "Ported Red's version back to Rebol"
25+
]
26+
27+
Purpose: "Convert Rebol value into JSON format and back."
28+
License: [
29+
http://www.apache.org/licenses/LICENSE-2.0
30+
and "The Software shall be used for Good, not Evil."
31+
]
32+
33+
Repository: https://github.com/giesse/red-json
34+
References: [
35+
http://www.json.org/
36+
https://www.ietf.org/rfc/rfc4627.txt
37+
http://www.rfc-editor.org/rfc/rfc7159.txt
38+
http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
39+
https://github.com/rebolek/red-tools/blob/master/json.red
40+
https://github.com/rgchris/Scripts/blob/master/red/altjson.red
41+
]
42+
43+
Type: 'module
44+
Exports: [to-json load-json]
1145
]
1246

13-
do [
47+
;----------------------------------------------------------------
48+
;@@ load-json
1449

15-
; -- load-json
1650

17-
context [
1851
;-----------------------------------------------------------
1952
;-- Generic support funcs
2053

@@ -39,11 +72,11 @@ BOM-UTF-32?: func [data [string! binary!]][
3972
enquote: func [str [string!] "(modified)"][append insert str {"} {"}]
4073

4174
high-surrogate?: func [codepoint [integer!]][
42-
all [codepoint >= D800h codepoint <= DBFFh]
75+
all [codepoint >= 55296 codepoint <= 56319] ;D800h DBFFh
4376
]
4477

4578
low-surrogate?: func [codepoint [integer!]][
46-
all [codepoint >= DC00h codepoint <= DFFFh]
79+
all [codepoint >= 56320 codepoint <= 57343] ;DC00h DFFFh
4780
]
4881

4982
translit: func [
@@ -55,11 +88,10 @@ translit: func [
5588
][
5689
parse string [
5790
some [
58-
change copy val rule (val either block? :xlat [xlat/:val][xlat val])
91+
change copy val rule (either block? :xlat [xlat/:val][xlat val])
5992
| skip
6093
]
6194
]
62-
string
6395
]
6496

6597
;-----------------------------------------------------------
@@ -251,7 +283,7 @@ emit: func [value][_res: insert/only _res value]
251283
;-----------------------------------------------------------
252284
;-- Main decoder func
253285

254-
set 'load-json func [
286+
load-json: func [
255287
"Convert a JSON string to Red data"
256288
input [string!] "The JSON string"
257289
] [
@@ -264,22 +296,26 @@ set 'load-json func [
264296
]
265297
]
266298
]
267-
]
268-
; -- to-json
269299

270-
context [
300+
301+
302+
303+
304+
;----------------------------------------------------------------
305+
;@@ to-json
306+
271307
indent: none
272308
indent-level: 0
273309
normal-chars: none
274-
escapes: #(
310+
escapes: #[map! [
275311
#"^"" {\"}
276312
#"\" "\\"
277313
#"^H" "\b"
278314
#"^L" "\f"
279315
#"^/" "\n"
280316
#"^M" "\r"
281317
#"^-" "\t"
282-
)
318+
]]
283319

284320
init-state: func [ind ascii?] [
285321
indent: ind
@@ -312,8 +348,8 @@ red-to-json-value: function [output value] [
312348
switch/default type?/word :value [
313349
none! [append output "null"]
314350
logic! [append output pick ["true" "false"] value]
315-
integer! float! [append output value]
316-
percent! [append output to float! value]
351+
integer! decimal! [append output value]
352+
percent! [append output to decimal! value]
317353
string! [
318354
append output #"^""
319355
parse value [
@@ -394,7 +430,7 @@ red-to-json-value: function [output value] [
394430
output
395431
]
396432

397-
set 'to-json function [
433+
to-json: function [
398434
"Convert Red data to a JSON string"
399435
data
400436
/pretty indent [string!] "Pretty format the output, using given indentation"
@@ -404,21 +440,20 @@ set 'to-json function [
404440
init-state indent ascii
405441
red-to-json-value result data
406442
]
407-
]
408443

409444

410-
put system/codecs 'json context [
411-
Title: "JSON codec"
412-
Name: 'JSON
413-
Mime-Type: [application/json]
414-
Suffixes: [%.json]
415-
encode: func [data [any-type!] where [file! url! none!]] [
445+
446+
register-codec [
447+
name: 'JSON
448+
title: "JavaScript Object Notation"
449+
suffixes: [%.json]
450+
451+
encode: func [data [any-type!]] [
416452
to-json data
417453
]
418454
decode: func [text [string! binary! file!]] [
419455
if file? text [text: read text]
420456
if binary? text [text: to string! text]
421457
load-json text
422458
]
423-
]
424-
]
459+
]

0 commit comments

Comments
 (0)