This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,10 @@
USING: json.writer json.reader ;
SYMBOL: foo
! Load a JSON string into a data structure
"[[\"foo\",1],[\"bar\",[10,\"apples\"]]]" json> foo set
! Create a new data structure and serialize into JSON
{ { "blue" { "ocean" "water" } } >json

View file

@ -0,0 +1,18 @@
using util
class Json
{
public static Void main ()
{
Str input := """{"blue": [1, 2], "ocean": "water"}"""
Map jsonObj := JsonInStream(input.in).readJson
echo ("Value for 'blue' is: " + jsonObj["blue"])
jsonObj["ocean"] = ["water":["cold", "blue"]]
Map ocean := jsonObj["ocean"]
echo ("Value for 'ocean/water' is: " + ocean["water"])
output := JsonOutStream(Env.cur.out)
output.writeJson(jsonObj)
echo ()
}
}

View file

@ -0,0 +1,13 @@
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText('''
{
"people":[
{"name":{"family":"Flintstone","given":"Frederick"},"age":35,"relationships":{"wife":"people[1]","child":"people[4]"}},
{"name":{"family":"Flintstone","given":"Wilma"},"age":32,"relationships":{"husband":"people[0]","child":"people[4]"}},
{"name":{"family":"Rubble","given":"Barnard"},"age":30,"relationships":{"wife":"people[3]","child":"people[5]"}},
{"name":{"family":"Rubble","given":"Elisabeth"},"age":32,"relationships":{"husband":"people[2]","child":"people[5]"}},
{"name":{"family":"Flintstone","given":"Pebbles"},"age":1,"relationships":{"mother":"people[1]","father":"people[0]"}},
{"name":{"family":"Rubble","given":"Bam-Bam"},"age":1,"relationships":{"mother":"people[3]","father":"people[2]"}},
]
}
''')

View file

@ -0,0 +1,8 @@
result.each { println it.key; it.value.each {person -> println person} }
assert result.people[0].name == [family:'Flintstone', given:'Frederick']
assert result.people[4].age == 1
assert result.people[2].relationships.wife == 'people[3]'
assert result.people[3].name == [family:'Rubble', given:'Elisabeth']
assert Eval.x(result, 'x.' + result.people[2].relationships.wife + '.name') == [family:'Rubble', given:'Elisabeth']
assert Eval.x(result, 'x.' + result.people[1].relationships.husband + '.name') == [family:'Flintstone', given:'Frederick']

49
Task/JSON/J/json-1.j Normal file
View file

@ -0,0 +1,49 @@
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.)

10
Task/JSON/J/json-2.j Normal file
View file

@ -0,0 +1,10 @@
jsonParse'{ "blue": [1,2], "ocean": "water" }'
┌──────────────────────────────┐
│┌──────┬─────┬───────┬───────┐│
││"blue"│┌─┬─┐│"ocean"│"water"││
││ ││1│2││ │ ││
││ │└─┴─┘│ │ ││
│└──────┴─────┴───────┴───────┘│
└──────────────────────────────┘
jsonSerialize jsonParse'{ "blue": [1,2], "ocean": "water" }'
[["\"blue\"",["1","2"],"\"ocean\"","\"water\""]]

View file

@ -0,0 +1,2 @@
data = ImportString["{ \"foo\": 1, \"bar\": [10, \"apples\"] }","JSON"]
ExportString[data, "JSON"]