langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
137
Task/JSON/NetRexx/json-1.netrexx
Normal file
137
Task/JSON/NetRexx/json-1.netrexx
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
import java.util.List
|
||||
import org.json.JSONObject
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONTokener
|
||||
import org.json.JSONException
|
||||
|
||||
/**
|
||||
* Using library from json.org
|
||||
*
|
||||
* @see http://www.json.org/java/index.html
|
||||
*/
|
||||
class RJson01 public
|
||||
|
||||
properties private constant
|
||||
JSON_DWARFS = '' -
|
||||
'{\n' -
|
||||
' "F1937_1" : {\n' -
|
||||
' "title" : "Snow White and the Seven Dwarfs",\n' -
|
||||
' "year" : 1937,\n' -
|
||||
' "medium" : "film",\n' -
|
||||
' "dwarfs" : [ "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey", "Doc" ]\n' -
|
||||
' },\n' -
|
||||
' "F2012_1" : {\n' -
|
||||
' "title" : "Mirror, Mirror",\n' -
|
||||
' "year" : 2012,\n' -
|
||||
' "medium" : "film",\n' -
|
||||
' "dwarfs" : [ "Grimm", "Butcher", "Wolf", "Napoleon", "Half Pint", "Grub", "Chuckles" ]\n' -
|
||||
' },\n' -
|
||||
'}'
|
||||
|
||||
/**
|
||||
* A bean that looks like the following JSON
|
||||
* <pre>
|
||||
* {
|
||||
* "F2012_2" : {
|
||||
* "title" : "Snow White & the Huntsman",
|
||||
* "year" : 2012,
|
||||
* "medium" : "film",
|
||||
* "dwarfs" : [ "Beith", "Quert", "Muir", "Coll", "Duir", "Gus", "Gort", "Nion" ]
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
SAMPLE_BEAN = DwarfBean( -
|
||||
"F2012_2", -
|
||||
"Snow White & the Huntsman", -
|
||||
Long(2012), -
|
||||
"film", -
|
||||
Arrays.asList([String "Beith", "Quert", "Muir", "Coll", "Duir", "Gus", "Gort", "Nion"]) -
|
||||
)
|
||||
|
||||
method main(args = String[]) public static
|
||||
say json2bean(JSON_DWARFS)
|
||||
say
|
||||
say bean2json(SAMPLE_BEAN)
|
||||
say
|
||||
return
|
||||
|
||||
method json2bean(dwarfs) public static returns List
|
||||
say "Make beans from this JSON string:"
|
||||
say dwarfs
|
||||
jsonBeans = ArrayList()
|
||||
do
|
||||
jd = JSONObject(JSONTokener(dwarfs))
|
||||
ns = JSONObject.getNames(jd)
|
||||
name = String
|
||||
loop name over ns
|
||||
dwarves = ArrayList()
|
||||
jn = jd.getJSONObject(name)
|
||||
title = jn.getString('title')
|
||||
year = Long(jn.getLong('year'))
|
||||
medium = jn.getString('medium')
|
||||
dwa = jn.getJSONArray('dwarfs')
|
||||
loop di = 0 to dwa.length() - 1
|
||||
dwarves.add(dwa.getString(di))
|
||||
end di
|
||||
jb = DwarfBean(name, title, year, medium, dwarves)
|
||||
jsonBeans.add(jb.toString())
|
||||
end name
|
||||
catch ex = JSONException
|
||||
ex.printStackTrace()
|
||||
end
|
||||
return jsonBeans
|
||||
|
||||
method bean2json(sb = DwarfBean) public static returns String
|
||||
say "Make JSONObject from this bean:"
|
||||
say sb
|
||||
jsonString = String
|
||||
do
|
||||
jd = JSONObject(sb)
|
||||
jo = JSONObject()
|
||||
jo = jo.put(sb.keyGet(), jd)
|
||||
jsonString = jo.toString(2)
|
||||
catch ex = JSONException
|
||||
ex.printStackTrace()
|
||||
end
|
||||
return jsonString
|
||||
|
||||
-- =============================================================================
|
||||
class RJson01.DwarfBean public binary
|
||||
properties private
|
||||
key = String -- not part of bean
|
||||
properties indirect
|
||||
title = String
|
||||
year = Long
|
||||
medium = String
|
||||
dwarfs = List
|
||||
|
||||
method DwarfBean(key_ = String null, title_ = String null, year_ = Long null, medium_ = String null, dwarfs_ = List null) public
|
||||
keyPut(key_)
|
||||
setTitle(title_)
|
||||
setYear(year_)
|
||||
setMedium(medium_)
|
||||
setDwarfs(dwarfs_)
|
||||
return
|
||||
|
||||
method keyPut(key_ = String) public
|
||||
key = key_
|
||||
return
|
||||
|
||||
method keyGet() returns String
|
||||
return key
|
||||
|
||||
method toString public returns String
|
||||
ts = StringBuilder()
|
||||
ts.append(String.format("%s@%08x ", [Object this.getClass().getSimpleName(), Integer(hashCode())]))
|
||||
ts.append('[')
|
||||
ts.append('key='String.valueOf(keyGet())', ')
|
||||
ts.append('title='String.valueOf(getTitle())', ')
|
||||
ts.append('year='String.valueOf(getYear())', ')
|
||||
ts.append('medium='String.valueOf(getMedium())', ')
|
||||
ts.append('dwarfs='String.valueOf(getDwarfs()))
|
||||
ts.append(']')
|
||||
return ts.toString()
|
||||
81
Task/JSON/NetRexx/json-2.netrexx
Normal file
81
Task/JSON/NetRexx/json-2.netrexx
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
import com.google.gson.
|
||||
import java.util.List
|
||||
|
||||
/**
|
||||
* Using google-gson library
|
||||
*
|
||||
* @see https://code.google.com/p/google-gson/
|
||||
*/
|
||||
class RJson02 public
|
||||
|
||||
properties private constant
|
||||
JSON_DWARFS = '' -
|
||||
'{\n' -
|
||||
' "title" : "Snow White and the Seven Dwarfs",\n' -
|
||||
' "year" : 1937,\n' -
|
||||
' "medium": "film",\n' -
|
||||
' "dwarfs": [ "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey", "Doc" ]\n' -
|
||||
'}'
|
||||
|
||||
/**
|
||||
* A bean that looks like the following JSON
|
||||
* <pre>
|
||||
* {
|
||||
* "title" : "Snow White & the Huntsman",
|
||||
* "year" : 2012,
|
||||
* "medium" : "film",
|
||||
* "dwarfs" : [ "Beith", "Quert", "Muir", "Coll", "Duir", "Gus", "Gort", "Nion" ]
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
SAMPLE_BEAN = RJSON02.DwarfBean( -
|
||||
/*"F2012_2",*/ -
|
||||
"Snow White and the Huntsman", -
|
||||
Long(2012), -
|
||||
"film", -
|
||||
Arrays.asList([String "Beith", "Quert", "Muir", "Coll", "Duir", "Gus", "Gort", "Nion"]) -
|
||||
)
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method main(args = String[]) public static
|
||||
gsonObj = GsonBuilder().setPrettyPrinting().create()
|
||||
jsonBean = RJson02.DwarfBean gsonObj.fromJson(JSON_DWARFS, RJson02.DwarfBean.class)
|
||||
say JSON_DWARFS
|
||||
say jsonBean.toString()
|
||||
say
|
||||
|
||||
json = gsonObj.toJson(SAMPLE_BEAN);
|
||||
say json
|
||||
say SAMPLE_BEAN.toString()
|
||||
say
|
||||
|
||||
return
|
||||
|
||||
-- =============================================================================
|
||||
class RJson02.DwarfBean public binary
|
||||
properties indirect
|
||||
title = String
|
||||
year = Long
|
||||
medium = String
|
||||
dwarfs = List
|
||||
|
||||
method DwarfBean(title_ = String null, year_ = Long null, medium_ = String null, dwarfs_ = List null) public
|
||||
setTitle(title_)
|
||||
setYear(year_)
|
||||
setMedium(medium_)
|
||||
setDwarfs(dwarfs_)
|
||||
return
|
||||
|
||||
method toString public returns String
|
||||
ts = StringBuilder()
|
||||
ts.append(String.format("%s@%08x ", [Object this.getClass().getSimpleName(), Integer(hashCode())]))
|
||||
ts.append('[')
|
||||
ts.append('title='String.valueOf(getTitle())', ')
|
||||
ts.append('year='String.valueOf(getYear())', ')
|
||||
ts.append('medium='String.valueOf(getMedium())', ')
|
||||
ts.append('dwarfs='String.valueOf(getDwarfs()))
|
||||
ts.append(']')
|
||||
return ts.toString()
|
||||
24
Task/JSON/OCaml/json-1.ocaml
Normal file
24
Task/JSON/OCaml/json-1.ocaml
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
type json item =
|
||||
< name "Name": string;
|
||||
kingdom "Kingdom": string;
|
||||
phylum "Phylum": string;
|
||||
class_ "Class": string;
|
||||
order "Order": string;
|
||||
family "Family": string;
|
||||
tribe "Tribe": string
|
||||
>
|
||||
|
||||
let str = "
|
||||
{
|
||||
\"Name\": \"camel\",
|
||||
\"Kingdom\": \"Animalia\",
|
||||
\"Phylum\": \"Chordata\",
|
||||
\"Class\": \"Mammalia\",
|
||||
\"Order\": \"Artiodactyla\",
|
||||
\"Family\": \"Camelidae\",
|
||||
\"Tribe\": \"Camelini\"
|
||||
}"
|
||||
|
||||
let () =
|
||||
let j = Json_io.json_of_string str in
|
||||
print_endline (Json_io.string_of_json j);
|
||||
31
Task/JSON/OCaml/json-2.ocaml
Normal file
31
Task/JSON/OCaml/json-2.ocaml
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
open Yojson.Basic.Util
|
||||
|
||||
let s = "
|
||||
{ \"name\": \"John Doe\",
|
||||
\"pages\": [
|
||||
{ \"id\": 1,
|
||||
\"title\": \"The Art of Flipping Coins\",
|
||||
\"url\": \"http://example.com/398eb027/1\"
|
||||
},
|
||||
{ \"id\": 2, \"deleted\": true },
|
||||
{ \"id\": 3,
|
||||
\"title\": \"Artichoke Salad\",
|
||||
\"url\": \"http://example.com/398eb027/3\"
|
||||
},
|
||||
{ \"id\": 4,
|
||||
\"title\": \"Flying Bananas\",
|
||||
\"url\": \"http://example.com/398eb027/4\"
|
||||
}
|
||||
]
|
||||
}"
|
||||
|
||||
let extract_titles json =
|
||||
[json]
|
||||
|> filter_member "pages"
|
||||
|> flatten
|
||||
|> filter_member "title"
|
||||
|> filter_string
|
||||
|
||||
let () =
|
||||
let json = Yojson.Basic.from_string s in
|
||||
List.iter print_endline (extract_titles json)
|
||||
14
Task/JSON/Objeck/json.objeck
Normal file
14
Task/JSON/Objeck/json.objeck
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
use Struct;
|
||||
use JSON;
|
||||
|
||||
bundle Default {
|
||||
class Json {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
parser := JSONParser->New("{ \"foo\": 1, \"bar\": [10, \"apples\"] }");
|
||||
root := parser->Parse();
|
||||
if(root <> Nil) {
|
||||
root->ToString()->PrintLine();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Task/JSON/Objective-C/json.m
Normal file
17
Task/JSON/Objective-C/json.m
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
NSString *json_string = @"{ \"foo\": 1, \"bar\": [10, \"apples\"] }";
|
||||
id obj = [NSJSONSerialization JSONObjectWithData:[json_string dataUsingEncoding:NSUTF8StringEncoding]
|
||||
options:0
|
||||
error:NULL];
|
||||
NSLog(@"%@", obj);
|
||||
|
||||
id obj2 = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:[NSNumber numberWithInt:1],
|
||||
[NSNumber numberWithInt:2],
|
||||
nil],
|
||||
@"blue",
|
||||
@"water", @"ocean",
|
||||
nil];
|
||||
NSString *json_string2 = [[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:obj2
|
||||
options:0
|
||||
error:NULL]
|
||||
encoding:NSUTF8StringEncoding] autorelease];
|
||||
NSLog(@"%@", json_string2);
|
||||
34
Task/JSON/OpenEdge-Progress/json.openedge
Normal file
34
Task/JSON/OpenEdge-Progress/json.openedge
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* using a longchar to read and write to, can also be file, memptr, stream */
|
||||
DEFINE VARIABLE lcjson AS LONGCHAR NO-UNDO.
|
||||
|
||||
/* temp-table defines object, can also be dataset */
|
||||
DEFINE TEMP-TABLE example
|
||||
FIELD blue AS INTEGER EXTENT 2
|
||||
FIELD ocean AS CHARACTER
|
||||
.
|
||||
CREATE example.
|
||||
ASSIGN
|
||||
example.blue [1] = 1
|
||||
example.blue [2] = 2
|
||||
example.ocean = "water"
|
||||
.
|
||||
/* write-json to put result in lcjson, true indicates formatted */
|
||||
TEMP-TABLE example:DEFAULT-BUFFER-HANDLE:WRITE-JSON( "LONGCHAR", lcjson, TRUE ).
|
||||
|
||||
/* display result */
|
||||
MESSAGE
|
||||
STRING( lcjson )
|
||||
VIEW-AS ALERT-BOX.
|
||||
|
||||
/* empty results */
|
||||
EMPTY TEMP-TABLE example.
|
||||
|
||||
/* read-json to get result from lcjson */
|
||||
TEMP-TABLE example:DEFAULT-BUFFER-HANDLE:READ-JSON( "LONGCHAR", lcjson ).
|
||||
|
||||
FIND example.
|
||||
/* display results */
|
||||
MESSAGE
|
||||
example.blue [1] example.blue [2] SKIP
|
||||
example.ocean
|
||||
VIEW-AS ALERT-BOX.
|
||||
7
Task/JSON/Oz/json.oz
Normal file
7
Task/JSON/Oz/json.oz
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
declare
|
||||
[JSON] = {Module.link ['JSON.ozf']}
|
||||
|
||||
{System.show {JSON.decode "{ \"foo\": 1, \"bar\": [10, \"apples\"] }"}}
|
||||
|
||||
Sample = object(blue:array(1 2) ocean:"water")
|
||||
{System.showInfo {JSON.encode Sample}}
|
||||
6
Task/JSON/Perl-6/json.pl6
Normal file
6
Task/JSON/Perl-6/json.pl6
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
use JSON::Tiny;
|
||||
|
||||
my $data = from-json('{ "foo": 1, "bar": [10, "apples"] }');
|
||||
|
||||
my $sample = { blue => [1,2], ocean => "water" };
|
||||
my $json_string = to-json($sample);
|
||||
13
Task/JSON/Pike/json.pike
Normal file
13
Task/JSON/Pike/json.pike
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
int main() {
|
||||
// Decoding
|
||||
string json = "{\"cake\":[\"desu\",1,2.3],\"foo\":1}";
|
||||
write("%O\n", Standards.JSON.decode(json));
|
||||
|
||||
// Encoding
|
||||
mapping m = ([
|
||||
"foo": ({ 1, 2, 3 }),
|
||||
"bar": "hello"
|
||||
]);
|
||||
|
||||
write("%s\n", Standards.JSON.encode(m));
|
||||
}
|
||||
14
Task/JSON/REBOL/json.rebol
Normal file
14
Task/JSON/REBOL/json.rebol
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
json-str: {{"menu": {
|
||||
"id": "file",
|
||||
"string": "File:",
|
||||
"number": -3,
|
||||
"boolean": true,
|
||||
"boolean2": false,
|
||||
"null": null,
|
||||
"array": [1, 0.13, null, true, false, "\t\r\n"],
|
||||
"empty-string": ""
|
||||
}
|
||||
}}
|
||||
|
||||
res: json-to-rebol json-str
|
||||
js: rebol-to-json res
|
||||
57
Task/JSON/TXR/json-1.txr
Normal file
57
Task/JSON/TXR/json-1.txr
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
@(define value (v))@\
|
||||
@(cases)@\
|
||||
@(string v)@(or)@(num v)@(or)@(object v)@(or)@\
|
||||
@(keyword v)@(or)@(array v)@\
|
||||
@(end)@\
|
||||
@(end)
|
||||
@(define ws)@/[\n\t ]*/@(end)
|
||||
@(define string (g))@\
|
||||
@(local s hex)@\
|
||||
@(ws)@\
|
||||
"@(coll :gap 0 :vars (s))@\
|
||||
@(cases)@\
|
||||
\"@(bind s """)@(or)@\
|
||||
\\@(bind s "\\\\")@(or)@\
|
||||
\/@(bind s "\\/")@(or)@\
|
||||
\b@(bind s "")@(or)@\
|
||||
\f@(bind s "")@(or)@\
|
||||
\n@(bind s " ")@(or)@\
|
||||
\r@(bind s " ")@(or)@\
|
||||
\t@(bind s "	")@(or)@\
|
||||
\u@{hex /[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]/}@\
|
||||
@(bind s `&#x@hex;`)@(or)@\
|
||||
@{s /[^"\\]*/}@(filter :to_html s)@\
|
||||
@(end)@\
|
||||
@(until)"@\
|
||||
@(end)"@\
|
||||
@(ws)@\
|
||||
@(cat s "")@\
|
||||
@(filter :from_html s)@\
|
||||
@(bind g ("S" s))@\
|
||||
@(end)
|
||||
@(define num (v))@\
|
||||
@(local n)@\
|
||||
@(ws)@{n /-?[0-9]+((\.[0-9]+)?([Ee][+\-]?[0-9]+)?)?/}@(ws)@\
|
||||
@(bind v ("N" n))@\
|
||||
@(end)
|
||||
@(define keyword (v))@\
|
||||
@(local k)@\
|
||||
@(all)@(ws)@{k /true|false|null/}@(trailer)@/[^A-Za-z0-9_]/@(end)@(ws)@\
|
||||
@(bind v ("K" k))@\
|
||||
@(end)
|
||||
@(define object (v))@\
|
||||
@(local p e pair)@\
|
||||
@(ws){@(ws)@(coll :gap 0 :vars (pair))@\
|
||||
@(string p):@(value e)@/,?/@\
|
||||
@(bind pair (p e))@\
|
||||
@(until)}@\
|
||||
@(end)}@(ws)@\
|
||||
@(bind v ("O" pair))@\
|
||||
@(end)
|
||||
@(define array (v))@\
|
||||
@(local e)@\
|
||||
@(ws)[@(ws)@(coll :gap 0 :var (e))@(value e)@/,?/@(until)]@(end)]@(ws)@\
|
||||
@(bind v ("A" e))@\
|
||||
@(end)
|
||||
@(freeform)
|
||||
@(maybe)@(value v)@(end)@badsyntax
|
||||
7
Task/JSON/TXR/json-2.txr
Normal file
7
Task/JSON/TXR/json-2.txr
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
$ echo -n '{ "a" : { "b" : 3, "c" : [1,2,3] } }[' | ./txr -l json.txr -
|
||||
(v "O" ((("S" "a") ("O" ((("S" "b") ("N" "3")) (("S" "c") ("A" (("N" "1") ("N" "2") ("N" "3")))))))))
|
||||
(badsyntax . "[\n")
|
||||
|
||||
$ echo -n '"\u1234"' | ./txr -l json.txr -
|
||||
(v "S" "\11064")
|
||||
(badsyntax . "")
|
||||
Loading…
Add table
Add a link
Reference in a new issue