mirror of
https://github.com/ilyakooo0/urbit.git
synced 2024-12-18 12:22:10 +03:00
Replace +en:json:html code with cord version
Encode JSON to cord instead of tape. Code copied from @joemfb 's version.
This commit is contained in:
parent
ac1a0ffdee
commit
e61d84d57b
@ -4317,66 +4317,69 @@
|
||||
:: ::
|
||||
:::: ++json:html :: (2e2) JSON
|
||||
:: ::::
|
||||
++ json
|
||||
++ json ^?
|
||||
|%
|
||||
:: :: ++en:json:html
|
||||
++ en :: encode JSON to tape
|
||||
|^ |= val=^json
|
||||
^- tape
|
||||
(apex val "")
|
||||
:: :: ++apex:en:json:html
|
||||
++ apex
|
||||
|= [val=^json rez=tape]
|
||||
^- tape
|
||||
?~ val (weld "null" rez)
|
||||
|^ |= jon=^json
|
||||
^- cord
|
||||
(rap 3 (flop (onto jon ~)))
|
||||
:: :: ++onto:en:json:html
|
||||
++ onto
|
||||
|= [val=^json out=(list @t)]
|
||||
^+ out
|
||||
?~ val ['null' out]
|
||||
?- -.val
|
||||
%a
|
||||
:- '['
|
||||
=. rez [']' rez]
|
||||
?~ p.val ['[]' out]
|
||||
=. out ['[' out]
|
||||
!.
|
||||
?~ p.val rez
|
||||
|-
|
||||
?~ t.p.val ^$(val i.p.val)
|
||||
^$(val i.p.val, rez [',' $(p.val t.p.val)])
|
||||
|- ^+ out
|
||||
=. out ^$(val i.p.val)
|
||||
?~(t.p.val [']' out] $(p.val t.p.val, out [',' out]))
|
||||
::
|
||||
%b
|
||||
(weld ?:(p.val "true" "false") rez)
|
||||
[?:(p.val 'true' 'false') out]
|
||||
::
|
||||
%n
|
||||
(weld (trip p.val) rez)
|
||||
[p.val out]
|
||||
::
|
||||
%s
|
||||
:- '"'
|
||||
=. rez ['"' rez]
|
||||
=+ viz=(trip p.val)
|
||||
!.
|
||||
|- ^- tape
|
||||
?~ viz rez
|
||||
=+ hed=(jesc i.viz)
|
||||
?: ?=([@ ~] hed)
|
||||
[i.hed $(viz t.viz)]
|
||||
(weld hed $(viz t.viz))
|
||||
[(scap p.val) out]
|
||||
::
|
||||
%o
|
||||
:- '{'
|
||||
=. rez ['}' rez]
|
||||
=+ viz=~(tap by p.val)
|
||||
?~ viz rez
|
||||
=/ viz ~(tap by p.val)
|
||||
?~ viz ['{}' out]
|
||||
=. out ['{' out]
|
||||
!.
|
||||
|- ^+ rez
|
||||
?~ t.viz ^$(val [%s p.i.viz], rez [':' ^$(val q.i.viz)])
|
||||
=. rez [',' $(viz t.viz)]
|
||||
^$(val [%s p.i.viz], rez [':' ^$(val q.i.viz)])
|
||||
==
|
||||
:: :: ++jesc:en:json:html
|
||||
++ jesc :: escaped characters
|
||||
=+ utf=|=(a=@ ['\\' 'u' ((x-co 4):co a)])
|
||||
|= a=@ ^- tape
|
||||
?+ a ?:(&((gth a 0x1f) !=(a 0x7f)) [a ~] (utf a))
|
||||
%10 "\\n"
|
||||
%34 "\\\""
|
||||
%92 "\\\\"
|
||||
|- ^+ out
|
||||
=. out ^$(val q.i.viz, out [':' [(scap p.i.viz) out]])
|
||||
?~(t.viz ['}' out] $(viz t.viz, out [',' out]))
|
||||
==
|
||||
:: :: ++scap:en:json:html
|
||||
++ scap
|
||||
|= val=@t
|
||||
^- @t
|
||||
=/ out=(list @t) ['"' ~]
|
||||
=/ len (met 3 val)
|
||||
=| [i=@ud pos=@ud]
|
||||
|- ^- @t
|
||||
?: =(len i)
|
||||
(rap 3 (flop ['"' (rsh [3 pos] val) out]))
|
||||
=/ car (cut 3 [i 1] val)
|
||||
?: ?& (gth car 0x1f)
|
||||
!=(car 0x22)
|
||||
!=(car 0x5C)
|
||||
!=(car 0x7F)
|
||||
==
|
||||
$(i +(i))
|
||||
=/ cap
|
||||
?+ car (crip '\\' 'u' ((x-co 4):co car))
|
||||
%10 '\\n'
|
||||
%'"' '\\"'
|
||||
%'\\' '\\\\'
|
||||
==
|
||||
$(i +(i), pos +(i), out [cap (cut 3 [pos (sub i pos)] val) out])
|
||||
-- ::en
|
||||
:: :: ++de:json:html
|
||||
++ de :: parse cord to JSON
|
||||
@ -4686,7 +4689,7 @@
|
||||
++ en-json
|
||||
|= jon=^json
|
||||
^- tape
|
||||
(en:json jon)
|
||||
(trip (en:json jon))
|
||||
:: +de-json:html: parse cord to (unit json)
|
||||
::
|
||||
:: XX: deprecated; use +de:json:html
|
||||
|
@ -13,7 +13,7 @@
|
||||
++ grow :: convert to
|
||||
|%
|
||||
++ mime [/application/json (as-octs:mimes -:txt)] :: convert to %mime
|
||||
++ txt [(crip (en:json jon))]~
|
||||
++ txt [(en:json jon)]~
|
||||
--
|
||||
++ grab
|
||||
|% :: convert from
|
||||
|
@ -13,7 +13,7 @@
|
||||
::
|
||||
+$ json-decode-spec [name=^tape input=cord expected=^json]
|
||||
+$ json-decode-rejection-spec [input=cord name=^tape]
|
||||
+$ json-encode-spec [name=^tape input=^json expected=^tape]
|
||||
+$ json-encode-spec [name=^tape input=^json expected=cord]
|
||||
::
|
||||
++ run-decode-specs
|
||||
:: legend tells of a man who made the Kessel run in less than 12
|
||||
@ -1505,43 +1505,43 @@
|
||||
:~
|
||||
:* "e_array_empty"
|
||||
[%a ~]
|
||||
"[]"
|
||||
'[]'
|
||||
==
|
||||
:* "e_array_single_boolean_false"
|
||||
[%a ~[[%b |]]]
|
||||
"[false]"
|
||||
'[false]'
|
||||
==
|
||||
:* "e_array_single_boolean_true"
|
||||
[%a ~[[%b &]]]
|
||||
"[true]"
|
||||
'[true]'
|
||||
==
|
||||
:* "e_array_single_null"
|
||||
[%a ~[~]]
|
||||
"[null]"
|
||||
'[null]'
|
||||
==
|
||||
:* "e_array_single_number"
|
||||
[%a ~[[%n '123.456']]]
|
||||
"[123.456]"
|
||||
'[123.456]'
|
||||
==
|
||||
:* "e_array_single_object"
|
||||
[%a ~[[%o ~]]]
|
||||
"[\{}]"
|
||||
'[{}]'
|
||||
==
|
||||
:* "e_array_single_string"
|
||||
[%a ~[[%s 'abc']]]
|
||||
"[\"abc\"]"
|
||||
'["abc"]'
|
||||
==
|
||||
:* "e_array_nested_array"
|
||||
[%a ~[[%a ~]]]
|
||||
"[[]]"
|
||||
'[[]]'
|
||||
==
|
||||
:* "e_array_multiple_homogenous"
|
||||
[%a ~[[%n '1'] [%n '2'] [%n '3'] [%n '4'] [%n '5']]]
|
||||
"[1,2,3,4,5]"
|
||||
'[1,2,3,4,5]'
|
||||
==
|
||||
:* "e_array_multiple_heterogenous"
|
||||
[%a ~[[%b |] ~ [%n '1'] [%s 'a'] [%o ~] [%a ~]]]
|
||||
"[false,null,1,\"a\",\{},[]]"
|
||||
'[false,null,1,"a",{},[]]'
|
||||
==
|
||||
==
|
||||
:: Encode numbers
|
||||
@ -1554,7 +1554,7 @@
|
||||
:~
|
||||
:* "e_number_sanity_check"
|
||||
[%n '-123.456e789']
|
||||
"-123.456e789"
|
||||
'-123.456e789'
|
||||
==
|
||||
==
|
||||
:: Encode objects
|
||||
@ -1564,52 +1564,52 @@
|
||||
:~
|
||||
:* "e_object_empty"
|
||||
[%o ~]
|
||||
"\{}"
|
||||
'{}'
|
||||
==
|
||||
:* "e_object_single_boolean_false"
|
||||
(frond 'x' [%b |])
|
||||
"\{\"x\":false}"
|
||||
'{"x":false}'
|
||||
==
|
||||
:* "e_object_single_boolean_true"
|
||||
(frond 'x' [%b &])
|
||||
"\{\"x\":true}"
|
||||
'{"x":true}'
|
||||
==
|
||||
:* "e_object_single_null"
|
||||
(frond 'x' ~)
|
||||
"\{\"x\":null}"
|
||||
'{"x":null}'
|
||||
==
|
||||
:* "e_object_single_number"
|
||||
(frond 'x' [%n '123.456'])
|
||||
"\{\"x\":123.456}"
|
||||
'{"x":123.456}'
|
||||
==
|
||||
:* "e_object_single_string"
|
||||
(frond 'x' [%s 'abc'])
|
||||
"\{\"x\":\"abc\"}"
|
||||
'{"x":"abc"}'
|
||||
==
|
||||
:* "e_object_single_array"
|
||||
(frond 'x' [%a ~])
|
||||
"\{\"x\":[]}"
|
||||
'{"x":[]}'
|
||||
==
|
||||
:* "e_object_nested_object"
|
||||
(frond 'x' [%o ~])
|
||||
"\{\"x\":\{}}"
|
||||
'{"x":{}}'
|
||||
==
|
||||
:: the order of elements for the below two tests is determined by hash, and
|
||||
:: therefore "random" but consistent
|
||||
:* "e_object_multiple_homogenous"
|
||||
(pairs ~[['a' [%n '97']] ['b' [%n '98']] ['c' [%n '99']] ['d' [%n '100']] ['e' [%n '101']] ['f' [%n '102']]])
|
||||
"\{\"c\":99,\"a\":97,\"f\":102,\"d\":100,\"b\":98,\"e\":101}"
|
||||
'{"c":99,"a":97,"f":102,"d":100,"b":98,"e":101}'
|
||||
==
|
||||
:* "e_object_multiple_heterogenous"
|
||||
(pairs ~[['a' [%b |]] ['b' ~] ['c' [%n '1']] ['d' [%s 'a']] ['e' [%a ~]] ['f' [%o ~]]])
|
||||
"\{\"c\":1,\"a\":false,\"f\":\{},\"d\":\"a\",\"b\":null,\"e\":[]}"
|
||||
'{"c":1,"a":false,"f":{},"d":"a","b":null,"e":[]}'
|
||||
==
|
||||
==
|
||||
:: Encode strings
|
||||
::
|
||||
:: Summary of .en-json:html string encoding rules:
|
||||
:: - '"', '\', and newlines are escaped
|
||||
:: - bytes \00 to \1F are UTF16 escaped
|
||||
:: - bytes \00 to \1F and \7F are UTF16 escaped
|
||||
:: - everything else (ASCII & unicode) is written directly as UTF8
|
||||
::
|
||||
++ test-en-json-strings
|
||||
@ -1617,69 +1617,67 @@
|
||||
:~
|
||||
:* "e_string_empty"
|
||||
[%s '']
|
||||
"\"\""
|
||||
'""'
|
||||
==
|
||||
:: XX: .en-json:html will ignore the trailing null bytes of strings due
|
||||
:: to the behaviour of .trip
|
||||
:* "e_string_null_wrapped"
|
||||
[%s 'a\00a']
|
||||
"\"a\\u0000a\""
|
||||
'"a\\u0000a"'
|
||||
==
|
||||
:* "e_string_ascii"
|
||||
[%s 'a']
|
||||
"\"a\""
|
||||
'"a"'
|
||||
==
|
||||
:* "e_string_escape_backslash"
|
||||
[%s 'Delete C:\\Windows\\System32']
|
||||
"\"Delete C:\\\\Windows\\\\System32\""
|
||||
'"Delete C:\\\\Windows\\\\System32"'
|
||||
==
|
||||
:* "e_string_escape_delete"
|
||||
[%s 'delete\7Fme']
|
||||
"\"delete\\u007fme\""
|
||||
'"delete\\u007fme"'
|
||||
==
|
||||
:* "e_string_escape_newline"
|
||||
[%s 'new\0Aline']
|
||||
"\"new\\nline\""
|
||||
'"new\\nline"'
|
||||
==
|
||||
:* "e_string_escape_quotes"
|
||||
[%s 'he said "wow"']
|
||||
"\"he said \\\"wow\\\"\""
|
||||
'"he said \\"wow\\""'
|
||||
==
|
||||
:* "e_string_escape_whitespace"
|
||||
[%s 'add\01some\09space\1Fbetween here']
|
||||
"\"add\\u0001some\\u0009space\\u001fbetween here\""
|
||||
'"add\\u0001some\\u0009space\\u001fbetween here"'
|
||||
==
|
||||
:* "e_string_unicode_bytes"
|
||||
[%s '\F0\9D\84\9E']
|
||||
"\"𝄞\""
|
||||
'"𝄞"'
|
||||
==
|
||||
:* "e_string_unicode_bytes_invalid"
|
||||
[%s '\F0\9B\BF\BF']
|
||||
"\"\""
|
||||
'""'
|
||||
==
|
||||
:* "e_string_unicode_bytes_invisible"
|
||||
[%s '\E2\81\A4']
|
||||
"\"\""
|
||||
'""'
|
||||
==
|
||||
:* "e_string_unicode_inline"
|
||||
[%s '𝄞']
|
||||
"\"𝄞\""
|
||||
'"𝄞"'
|
||||
==
|
||||
:* "e_string_utf16_inline"
|
||||
[%s '\\uD83C\\uDDFA\\uD83C\\uDDF2']
|
||||
"\"\\\\uD83C\\\\uDDFA\\\\uD83C\\\\uDDF2\""
|
||||
'"\\\\uD83C\\\\uDDFA\\\\uD83C\\\\uDDF2"'
|
||||
==
|
||||
:* "e_string_multiple_ascii"
|
||||
[%s '123456789 abcdefghijklmnop']
|
||||
"\"123456789 abcdefghijklmnop\""
|
||||
'"123456789 abcdefghijklmnop"'
|
||||
==
|
||||
:* "e_string_multiple_escapes"
|
||||
[%s '\09"wow!"\0Ahe said\\']
|
||||
"\"\\u0009\\\"wow!\\\"\\nhe said\\\\\""
|
||||
'"\\u0009\\"wow!\\"\\nhe said\\\\"'
|
||||
==
|
||||
:* "e_string_multiple_unicode"
|
||||
[%s '𝄞\F0\9D\84\9E']
|
||||
"\"𝄞𝄞\""
|
||||
'"𝄞𝄞"'
|
||||
==
|
||||
==
|
||||
:: Encode naked values
|
||||
@ -1689,15 +1687,15 @@
|
||||
:~
|
||||
:* "e_value_bool_false"
|
||||
[%b |]
|
||||
"false"
|
||||
'false'
|
||||
==
|
||||
:* "e_value_bool_true"
|
||||
[%b &]
|
||||
"true"
|
||||
'true'
|
||||
==
|
||||
:* "e_value_null"
|
||||
~
|
||||
"null"
|
||||
'null'
|
||||
==
|
||||
==
|
||||
:: Encoding stress-test
|
||||
@ -1707,7 +1705,7 @@
|
||||
::
|
||||
++ test-en-json-complex-structure
|
||||
%+ expect-eq
|
||||
!> "\{\"data\":\{\"user\":\{\"result\":\{\"has_nft_avatar\":false,\"rest_id\":\"826261914\",\"affiliates_highlighted_label\":\{},\"__typename\":\"User\",\"id\":\"VXNlcjo4MjYyNjE5MTQ=\",\"is_profile_translatable\":false,\"legacy_extended_profile\":\{},\"legacy\":\{\"protected\":false,\"pinned_tweet_ids_str\":[\"1238111933905190913\"],\"entities\":\{\"url\":\{\"urls\":[\{\"indices\":[0,23],\"display_url\":\"NewDiscourses.com\",\"url\":\"https://t.co/RDZVUxIOWN\",\"expanded_url\":\"http://NewDiscourses.com\"}]},\"description\":\{\"urls\":[]}},\"name\":\"James Lindsay, pro-freedom\",\"default_profile\":true,\"location\":\"Knoxville, TN\",\"url\":\"https://t.co/RDZVUxIOWN\",\"verified\":false,\"profile_interstitial_type\":\"\",\"fast_followers_count\":0,\"description\":\"Not-NYT Bestselling author. Math PhD. Founder of New Discourses. Apolitical. Against totalitarianism and supremacy of all kinds. For freedom. 🇺🇲🇺🇲🇺🇲\",\"created_at\":\"Sun Sep 16 01:32:21 +0000 2012\",\"followers_count\":284274,\"withheld_in_countries\":[],\"listed_count\":1867,\"profile_image_url_https\":\"https://pbs.twimg.com/profile_images/1459175734602350593/cW3fs5lR_normal.jpg\",\"has_custom_timelines\":true,\"normal_followers_count\":284274,\"favourites_count\":193322,\"default_profile_image\":false,\"media_count\":11074,\"statuses_count\":137296,\"profile_banner_extensions\":\{\"mediaColor\":\{\"r\":\{\"ok\":\{\"palette\":[\{\"rgb\":\{\"green\":36,\"blue\":41,\"red\":33},\"percentage\":95.91},\{\"rgb\":\{\"green\":115,\"blue\":118,\"red\":113},\"percentage\":3.65},\{\"rgb\":\{\"green\":201,\"blue\":202,\"red\":200},\"percentage\":0.44}]}}}},\"profile_banner_url\":\"https://pbs.twimg.com/profile_banners/826261914/1582653360\",\"screen_name\":\"ConceptualJames\",\"translator_type\":\"none\",\"friends_count\":338,\"profile_image_extensions\":\{\"mediaColor\":\{\"r\":\{\"ok\":\{\"palette\":[\{\"rgb\":\{\"green\":22,\"blue\":16,\"red\":35},\"percentage\":72.12},\{\"rgb\":\{\"green\":123,\"blue\":100,\"red\":146},\"percentage\":17.13},\{\"rgb\":\{\"green\":153,\"blue\":129,\"red\":193},\"percentage\":10.57},\{\"rgb\":\{\"green\":149,\"blue\":125,\"red\":222},\"percentage\":2.03},\{\"rgb\":\{\"green\":53,\"blue\":55,\"red\":131},\"percentage\":1.58}]}}}},\"is_translator\":false}}}}}"
|
||||
!> '{"data":{"user":{"result":{"has_nft_avatar":false,"rest_id":"826261914","affiliates_highlighted_label":{},"__typename":"User","id":"VXNlcjo4MjYyNjE5MTQ=","is_profile_translatable":false,"legacy_extended_profile":{},"legacy":{"protected":false,"pinned_tweet_ids_str":["1238111933905190913"],"entities":{"url":{"urls":[{"indices":[0,23],"display_url":"NewDiscourses.com","url":"https://t.co/RDZVUxIOWN","expanded_url":"http://NewDiscourses.com"}]},"description":{"urls":[]}},"name":"James Lindsay, pro-freedom","default_profile":true,"location":"Knoxville, TN","url":"https://t.co/RDZVUxIOWN","verified":false,"profile_interstitial_type":"","fast_followers_count":0,"description":"Not-NYT Bestselling author. Math PhD. Founder of New Discourses. Apolitical. Against totalitarianism and supremacy of all kinds. For freedom. 🇺🇲🇺🇲🇺🇲","created_at":"Sun Sep 16 01:32:21 +0000 2012","followers_count":284274,"withheld_in_countries":[],"listed_count":1867,"profile_image_url_https":"https://pbs.twimg.com/profile_images/1459175734602350593/cW3fs5lR_normal.jpg","has_custom_timelines":true,"normal_followers_count":284274,"favourites_count":193322,"default_profile_image":false,"media_count":11074,"statuses_count":137296,"profile_banner_extensions":{"mediaColor":{"r":{"ok":{"palette":[{"rgb":{"green":36,"blue":41,"red":33},"percentage":95.91},{"rgb":{"green":115,"blue":118,"red":113},"percentage":3.65},{"rgb":{"green":201,"blue":202,"red":200},"percentage":0.44}]}}}},"profile_banner_url":"https://pbs.twimg.com/profile_banners/826261914/1582653360","screen_name":"ConceptualJames","translator_type":"none","friends_count":338,"profile_image_extensions":{"mediaColor":{"r":{"ok":{"palette":[{"rgb":{"green":22,"blue":16,"red":35},"percentage":72.12},{"rgb":{"green":123,"blue":100,"red":146},"percentage":17.13},{"rgb":{"green":153,"blue":129,"red":193},"percentage":10.57},{"rgb":{"green":149,"blue":125,"red":222},"percentage":2.03},{"rgb":{"green":53,"blue":55,"red":131},"percentage":1.58}]}}}},"is_translator":false}}}}}'
|
||||
!> %- en:json
|
||||
:- %o
|
||||
%- malt
|
||||
|
Loading…
Reference in New Issue
Block a user