Skip to content

Commit 27d7a60

Browse files
committed
TEMP: reverting back to Red's version with removed indentation, so is more clear what was changed in the next commit
1 parent 796f30e commit 27d7a60

File tree

1 file changed

+32
-67
lines changed

1 file changed

+32
-67
lines changed

src/mezz/codec-json.r

+32-67
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,20 @@
11
Red [
22
Title: "JSON codec"
3+
Author: "Gabriele Santilli"
34
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+
}
4511
]
4612

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

15+
; -- load-json
5016

17+
context [
5118
;-----------------------------------------------------------
5219
;-- Generic support funcs
5320

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

7441
high-surrogate?: func [codepoint [integer!]][
75-
all [codepoint >= 55296 codepoint <= 56319] ;D800h DBFFh
42+
all [codepoint >= D800h codepoint <= DBFFh]
7643
]
7744

7845
low-surrogate?: func [codepoint [integer!]][
79-
all [codepoint >= 56320 codepoint <= 57343] ;DC00h DFFFh
46+
all [codepoint >= DC00h codepoint <= DFFFh]
8047
]
8148

8249
translit: func [
@@ -88,10 +55,11 @@ translit: func [
8855
][
8956
parse string [
9057
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])
9259
| skip
9360
]
9461
]
62+
string
9563
]
9664

9765
;-----------------------------------------------------------
@@ -283,7 +251,7 @@ emit: func [value][_res: insert/only _res value]
283251
;-----------------------------------------------------------
284252
;-- Main decoder func
285253
286-
load-json: func [
254+
set 'load-json func [
287255
"Convert a JSON string to Red data"
288256
input [string!] "The JSON string"
289257
] [
@@ -296,26 +264,22 @@ load-json: func [
296264
]
297265
]
298266
]
267+
]
268+
; -- to-json
299269
300-
301-
302-
303-
304-
;----------------------------------------------------------------
305-
;@@ to-json
306-
270+
context [
307271
indent: none
308272
indent-level: 0
309273
normal-chars: none
310-
escapes: #[map! [
274+
escapes: #(
311275
#"^"" {\"}
312276
#"\" "\\"
313277
#"^H" "\b"
314278
#"^L" "\f"
315279
#"^/" "\n"
316280
#"^M" "\r"
317281
#"^-" "\t"
318-
]]
282+
)
319283
320284
init-state: func [ind ascii?] [
321285
indent: ind
@@ -348,8 +312,8 @@ red-to-json-value: function [output value] [
348312
switch/default type?/word :value [
349313
none! [append output "null"]
350314
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]
353317
string! [
354318
append output #"^""
355319
parse value [
@@ -430,7 +394,7 @@ red-to-json-value: function [output value] [
430394
output
431395
]
432396

433-
to-json: function [
397+
set 'to-json function [
434398
"Convert Red data to a JSON string"
435399
data
436400
/pretty indent [string!] "Pretty format the output, using given indentation"
@@ -440,20 +404,21 @@ to-json: function [
440404
init-state indent ascii
441405
red-to-json-value result data
442406
]
407+
]
443408
444409
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!]] [
452416
to-json data
453417
]
454418
decode: func [text [string! binary! file!]] [
455419
if file? text [text: read text]
456420
if binary? text [text: to string! text]
457421
load-json text
458422
]
459-
]
423+
]
424+
]

0 commit comments

Comments
 (0)