Last active
January 4, 2018 20:50
-
-
Save lepinekong/ae8b7402355297861edd845bb67d029f to your computer and use it in GitHub Desktop.
json parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json/decode read https://poloniex.com/public?command=returnChartData¤cyPair=BTC_XMR&end=9999999999&period=14400&start=1405699200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
Title: "JSON parser" | |
File: %json.red | |
Author: "Nenad Rakocevic, Qingtian Xie, Boleslav Březovský" | |
License: "BSD-3 - https://github.com/red/red/blob/master/BSD-3-License.txt" | |
] | |
json: context [ | |
quoted-char: charset {"\/bfnrt} | |
exponent: charset "eE" | |
sign: charset "+-" | |
digit-nz: charset "123456789" | |
digit: charset [#"0" - #"9"] | |
hexa: union digit charset "ABCDEFabcdef" | |
blank: charset " ^(09)^(0A)^(0D)" | |
ws: [any blank] | |
dbl-quote: #"^"" | |
s: e: none | |
list: none | |
null-value: none ; NOTE: Change this, if you prefer something else than NONE | |
conversion?: no ; EXPERIMENTAL: For numbers in quotes, load them | |
load-str: func [ | |
"Return word if possible, leave untouched when not" | |
str | |
/local out | |
] [ | |
if error? try [out: load str] [out: str] | |
out | |
] | |
decode-str: func [start end /local new rule s t loaded][ | |
new: copy/part start back end ;-- exclude ending quote | |
rule: [ | |
any [ | |
s: remove #"\" [ | |
#"^"" (s/1: #"^"") | |
| #"/" (s/1: #"/") | |
| #"\" (s/1: #"\") | |
| #"b" (s/1: #"^H") | |
| #"f" (s/1: #"^(0C)") | |
| #"n" (s/1: #"^/") | |
| #"r" (s/1: #"^M") | |
| #"t" (s/1: #"^-") | |
| #"u" copy t 4 hexa ( | |
change/part s load rejoin [{#"^^(} t {)"}] 5 | |
s: skip s -4 | |
) :s | |
] | |
| skip | |
] | |
] | |
parse new rule | |
all [ | |
conversion? | |
number? loaded: try [load new] | |
new: loaded | |
] | |
new | |
] | |
encode-str: func [str [string!] buffer [string!] /local start rule s][ | |
append buffer #"^"" | |
start: tail buffer | |
append buffer str | |
rule: [ | |
any [ | |
change #"^H" "\b" | |
| change #"^(0C)" "\f" | |
| change #"^/" "\n" | |
| change #"^M" "\r" | |
| change #"\" "\\" | |
| change #"^-" "\t" | |
| change #"^"" {\"} | |
| skip | |
] | |
] | |
parse start rule | |
append buffer #"^"" | |
] | |
value: [ | |
string keep (decode-str s e) | |
| number keep (load copy/part s e) | |
| "true" keep (true) | |
| "false" keep (false) | |
| "null" keep (null-value) | |
| object-rule | |
| array | |
] | |
number: [ | |
s: opt #"-" | |
some digit | |
opt [dot some digit opt [exponent sign 1 3 digit]] | |
e: | |
] | |
string: [ | |
dbl-quote | |
; TODO: check if any unicode conversion needs to be done here | |
s: any [#"\" [quoted-char | #"u" 4 hexa] | dbl-quote break | skip] | |
e: | |
] | |
couple: [ws string keep (load-str decode-str s e) ws #":" ws value ws] | |
object-rule: [ | |
#"{" | |
collect set list opt [any [couple #","] couple] ws #"}" | |
keep (make map! list) | |
] | |
array: [#"[" collect opt [ws value any [ws #"," ws value]] ws #"]"] | |
decode: function [ | |
data [string!] | |
return: [block! object!] | |
][ | |
output: parse data [collect any [blank | object-rule | array | value]] | |
either equal? 1 length? output [first output] [output] | |
] | |
encode-into: function [ | |
data [any-type!] | |
buffer [string!] | |
][ | |
case [ | |
any [map? data object? data] [ | |
append buffer #"{" | |
either zero? length? words-of data [ | |
append buffer #"}" | |
][ | |
foreach word words-of data [ | |
encode-into word buffer | |
append buffer #":" | |
encode-into data/:word buffer | |
append buffer #"," | |
] | |
change back tail buffer #"}" | |
] | |
] | |
block? data [ | |
append buffer #"[" | |
either empty? data [ | |
append buffer #"]" | |
][ | |
foreach v data [ | |
encode-into v buffer | |
append buffer #"," | |
] | |
change back tail buffer #"]" | |
] | |
] | |
string? data [ | |
encode-str data buffer | |
] | |
any [logic? data number? data][ | |
append buffer mold data | |
] | |
true [ | |
encode-into mold data buffer | |
] | |
] | |
] | |
encode: function [ | |
data | |
return: [string!] | |
][ | |
buffer: make string! 1000 | |
encode-into data buffer | |
buffer | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
do https://raw.githubusercontent.com/rebolek/red-tools/master/json.red
start: to-integer (now - 1)
url: to-url rejoin [{https://poloniex.com/public?command=returnChartData¤cyPair=BTC_XMR&end=9999999999&period=14400&start=} start]
json/decode read url