JSON: Difference between revisions

3,025 bytes added ,  13 years ago
J draft
m (→‎{{header|Tcl}}: added xref)
(J draft)
Line 21:
}
}</lang>
 
=={{header|J}}==
 
Here is a minimal implementation based on [http://www.jsoftware.com/pipermail/chat/2007-April/000462.html an old email message].
 
<lang j>NB. character classes:
NB. 0: whitespace
NB. 1: "
NB. 2: \
NB. 3: [ ] , { } :
NB. 4: ordinary
classes=.3<. '"\[],{}:' (#@[ |&>: i.) a.
classes=.0 (I.a.e.' ',CRLF,TAB)} (]+4*0=])classes
 
words=:(0;(0 10#:10*".;._2]0 :0);classes)&;: NB. states:
0.0 1.1 2.1 3.1 4.1 NB. 0 whitespace
1.0 5.0 6.0 1.0 1.0 NB. 1 "
4.0 4.0 4.0 4.0 4.0 NB. 2 \
0.3 1.2 2.2 3.2 4.2 NB. 3 { : , } [ ]
0.3 1.2 2.0 3.2 4.0 NB. 4 ordinary
0.3 1.2 2.2 3.2 4.2 NB. 5 ""
1.0 1.0 1.0 1.0 1.0 NB. 6 "\
)
 
tokens=. ;:'[ ] , { } :'
actions=: lBra`rBracket`comma`lBra`rBracket`colon`value
 
NB. action verbs argument conventions:
NB. x -- boxed json word
NB. y -- boxed json state stack
NB. result -- new boxed json state stack
NB.
NB. json state stack is an list of boxes of incomplete lists
NB. (a single box for complete, syntactically valid json)
jsonParse=: 0 {:: (,a:) ,&.> [: actions@.(tokens&i.@[)/ [:|.a:,words
 
lBra=: a: ,~ ]
rBracket=: _2&}.@], [:< _2&{::@], _1&{@]
comma=: ]
rBrace=: _2&}.@], [:< _2&{::@], [:|: (2,~ [: -:@$ _1&{@]) $ _1&{@]
colon=: ]
value=: _1&}.@], [:< _1&{::@], jsonValue&.>@[
 
NB. hypothetically, jsonValue should strip double quotes
NB. interpret back slashes
NB. and recognize numbers
jsonValue=:]
 
 
require'strings'
jsonSer1=: ']' ,~ '[' }:@;@; (',' ,~ jsonSerialize)&.>
jsonSer0=: '"', jsonEsc@:":, '"'"_
jsonEsc=: rplc&(<;._1' \ \\ " \"')
jsonSerialize=:jsonSer0`jsonSer1@.(*@L.)</lang>
 
Example use:
 
<lang> jsonParse'{ "blue": [1,2], "ocean": "water" }'
┌──────────────────────────────┐
│┌──────┬─────┬───────┬───────┐│
││"blue"│┌─┬─┐│"ocean"│"water"││
││ ││1│2││ │ ││
││ │└─┴─┘│ │ ││
│└──────┴─────┴───────┴───────┘│
└──────────────────────────────┘
jsonSerialize jsonParse'{ "blue": [1,2], "ocean": "water" }'
[["\"blue\"",["1","2"],"\"ocean\"","\"water\""]]</lang>
 
Note that these are not strict inverses of each other. These routines allow data to be extracted from json and packed into json format, but only in a minimalistic sense. No attempts are made to preserve the subtleties of type and structure which json can carry. This should be good enough for most applications which are required to deal with json but will not be adequate for ill behaved applications which exploit the typing mechanism to carry significant information.
 
Also, a different serializer will probably be necessary, if you are delivering json to legacy javascript. Nevertheless, these simplifications are probably appropriate for practical cases.
 
=={{header|JavaScript}}==
6,962

edits