1
1
Red [
2
2
Title: "JSON codec"
3
- Author : " Gabriele Santilli"
4
3
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]
11
45
]
12
46
13
- do [
47
+ ;----------------------------------------------------------------
48
+ ;@@ load-json
14
49
15
- ; -- load - json
16
50
17
- context [
18
51
;-----------------------------------------------------------
19
52
;-- Generic support funcs
20
53
@@ -39,11 +72,11 @@ BOM-UTF-32?: func [data [string! binary!]][
39
72
enquote : func [ str [string! ] "(modified)" ] [append insert str {"} {"} ]
40
73
41
74
high-surrogate? : func [ codepoint [integer! ]] [
42
- all [codepoint > = D800h codepoint < = DBFFh ]
75
+ all [codepoint >= 55296 codepoint <= 56319 ] ;D800h DBFFh
43
76
]
44
77
45
78
low-surrogate? : func [ codepoint [integer! ]] [
46
- all [codepoint > = DC00h codepoint < = DFFFh ]
79
+ all [codepoint >= 56320 codepoint <= 57343 ] ;DC00h DFFFh
47
80
]
48
81
49
82
translit : func [
@@ -55,11 +88,10 @@ translit: func [
55
88
] [
56
89
parse string [
57
90
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])
59
92
| skip
60
93
]
61
94
]
62
- string
63
95
]
64
96
65
97
;-----------------------------------------------------------
@@ -251,7 +283,7 @@ emit: func [value][_res: insert/only _res value]
251
283
;-----------------------------------------------------------
252
284
;-- Main decoder func
253
285
254
- set ' load-json func [
286
+ load-json : func [
255
287
"Convert a JSON string to Red data"
256
288
input [string! ] "The JSON string"
257
289
] [
@@ -264,22 +296,26 @@ set 'load-json func [
264
296
]
265
297
]
266
298
]
267
- ]
268
- ; -- to-json
269
299
270
- context [
300
+
301
+
302
+
303
+
304
+ ;----------------------------------------------------------------
305
+ ;@@ to-json
306
+
271
307
indent: none
272
308
indent-level: 0
273
309
normal-chars: none
274
- escapes: #(
310
+ escapes: #[map! [
275
311
#"^"" {\"}
276
312
#"\" "\\"
277
313
#"^H" "\b"
278
314
#"^L" "\f"
279
315
#"^/" "\n"
280
316
#"^M" "\r"
281
317
#"^-" "\t"
282
- )
318
+ ]]
283
319
284
320
init-state : func [ ind ascii?] [
285
321
indent: ind
@@ -312,8 +348,8 @@ red-to-json-value: function [output value] [
312
348
switch /default type? /word :value [
313
349
none! [append output "null" ]
314
350
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]
317
353
string! [
318
354
append output #"^""
319
355
parse value [
@@ -394,7 +430,7 @@ red-to-json-value: function [output value] [
394
430
output
395
431
]
396
432
397
- set ' to-json function [
433
+ to-json : function [
398
434
"Convert Red data to a JSON string"
399
435
data
400
436
/pretty indent [string! ] "Pretty format the output, using given indentation"
@@ -404,21 +440,20 @@ set 'to-json function [
404
440
init-state indent ascii
405
441
red-to-json-value result data
406
442
]
407
- ]
408
443
409
444
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! ]] [
416
452
to-json data
417
453
]
418
454
decode : func [ text [string! binary! file! ]] [
419
455
if file? text [text: read text]
420
456
if binary? text [text: to string! text]
421
457
load-json text
422
458
]
423
- ]
424
- ]
459
+ ]
0 commit comments