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