1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#!/usr/bin/env newlisp
; test JSON translation
(set 'js-1 [text]
{
"name": "John Smith",
"age": 32,
"employed": true,
"address": {
"street": "701 First Ave.",
"city": "Sunnyvale, CA 95125",
"country": "United States"
},
"children": [
{
"name": "Richard",
"age": 7
},
{
"name": "Susan",
"age": 4
},
{
"name": "James",
"age": 3
}
]
}
[/text])
; this from a (public blog post) has empty objects:
; curl https://api.balancedpayments.com/v1/credits \
; -d amount=10000 \
; -d description="Math lesson" \
; -d bank_account[name]="Johann Bernoulli" \
; -d bank_account[account_number]=9900000001 \
; -d bank_account[routing_number]=121000358 \
; -d bank_account[type]=checking \
; -u 7b7a51ccb10c11e19c0a026ba7e239a9:
(set 'js-2 [text]
{
"status": "pending",
"description": "Math lesson",
"created_at": "2013-01-08T18:28:56.979172Z",
"uri": "/v1/credits/CR23hlY1ElsZOFuKiruxPL3W",
"amount": 10000,
"meta": {},
"id": "CR23hlY1ElsZOFuKiruxPL3W",
"bank_account": {
"routing_number": "121000358",
"bank_name": "BANK OF AMERICA, N.A.",
"name": "Johann Bernoulli",
"meta": {},
"account_number": "xxxxxx0001",
"fingerprint": "1eH719hwbYRpEILVKyboPs_pn",
"type": "checking"
}
}
[/text])
; like js-2, but no white space between tokens, just one long line
(set 'js-3 [text]
{"status": "pending","description":"Math lesson","created_at":"2013-01-08T18:28:56.979172Z","uri":"/v1/credits/CR23hlY1ElsZOFuKiruxPL3W","amount":10000,"meta":{},"id":"CR23hlY1ElsZOFuKiruxPL3W","bank_account":{"routing_number":"121000358","bank_name": "BANK OF AMERICA, N.A.","name":"Johann Bernoulli","meta":{},"account_number":"xxxxxx0001","fingerprint": "1eH719hwbYRpEILVKyboPs_pn","type":"checking"}}
[/text])
(if (and
(= (json-parse js-1)
'(("name" "John Smith")
("age" 32)
("employed" true)
("address" (
("street" "701 First Ave.")
("city" "Sunnyvale, CA 95125")
("country" "United States")))
("children" (
(("name" "Richard") ("age" 7))
(("name" "Susan") ("age" 4))
(("name" "James") ("age" 3))))))
; empty objects {}
(= (json-parse js-2) (json-parse js-3)
'(("status" "pending")
("description" "Math lesson")
("created_at" "2013-01-08T18:28:56.979172Z")
("uri" "/v1/credits/CR23hlY1ElsZOFuKiruxPL3W")
("amount" 10000)
("meta" ())
("id" "CR23hlY1ElsZOFuKiruxPL3W")
("bank_account" (
("routing_number" "121000358")
("bank_name" "BANK OF AMERICA, N.A.")
("name" "Johann Bernoulli")
("meta" ())
("account_number" "xxxxxx0001")
("fingerprint" "1eH719hwbYRpEILVKyboPs_pn")
("type" "checking")))))
; from test-json-error qa-dot
(and
(not (json-parse {{"key" : "value" , "hello" "world"}}))
(= (json-error) '("missing : colon" 28))
)
; test-json-parse from qa-dot with arrays
(and
(= (json-parse { {"key" : "value" , "hello" : "world", "array" : [1, 2, 3, 4, 5]} })
'(("key" "value") ("hello" "world") ("array" (1 2 3 4 5))))
(= (json-parse { [1,2,3,4,5,6,{ "nested" : 777}, 8, 9] } )
'(1 2 3 4 5 6 (("nested" 777)) 8 9) )
)
; empty array and empty object
(and (= (json-parse {[]}) '()) (= (json-parse {{}}) '()))
; only for utf8 enabled versions
(if utf8 (= (json-parse { {"greek letters" : "\u03b1\u03b2\u03b3\u03b4"} })
'(("greek letters" "αβγδ")))
true
)
; escaped quotes fixed in 10.5.2
(= (json-parse [text]{"foo": ["bar", "\"baz\""]}[/text]) '(("foo" ("bar" "\"baz\""))))
; escaped back slashes in 10.5.3
(= (json-parse [text]{"a backslash":"\\"}[/text]) '(("a backslash" "\\")))
; trailing , (comma) in arrays for ECMA-404 compliance
(= (json-parse {[1,2,3,]}) '(1 2 3))
)
(println ">>>>> JSON translation tested SUCESSFUL")
(println ">>>>> PROBLEM in JSON translation")
)
(println)
(exit)
;; Other Tests
; one long JSON line without spacesi (~ 700 bytes):
; (json-parse (first (exec "curl -Ls http://webutils.flourishworks.com/dns")))
; The JSON string returned by curl is different every time, but works.
; longer JSON text (~ 3600 bytes), fill in any other ip-number
; (json-parse (get-url "http://apps.db.ripe.net/whois/lookup/ripe/inetnum/208.94.116.204.json"))
; large 10 Mbyte JSON file:
; (write-file "auctions.json"
; (get-url "http://eu.battle.net/auction-data/258993a3c6b974ef3e6f22ea6f822720/auctions.json"))
; (time (set 'S (json-parse (read-file "auctions.json")))) ;=> 240.057 ms
; (length (flat S)) ;=> 1212355 tokens
|