Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

48
Task/JSON/Fortran/json.f Normal file
View file

@ -0,0 +1,48 @@
program json_fortran
use json_module
implicit none
type phonebook_type
character(len=:),allocatable :: name
character(len=:),allocatable :: phone
end type phonebook_type
type(phonebook_type), dimension(3) :: PhoneBook
integer :: i
type(json_value),pointer :: json_phonebook,p,e
type(json_file) :: json
PhoneBook(1) % name = 'Adam'
PhoneBook(2) % name = 'Eve'
PhoneBook(3) % name = 'Julia'
PhoneBook(1) % phone = '0000001'
PhoneBook(2) % phone = '0000002'
PhoneBook(3) % phone = '6666666'
call json_initialize()
!create the root structure:
call json_create_object(json_phonebook,'')
!create and populate the phonebook array:
call json_create_array(p,'PhoneBook')
do i=1,3
call json_create_object(e,'')
call json_add(e,'name',PhoneBook(i)%name)
call json_add(e,'phone',PhoneBook(i)%phone)
call json_add(p,e) !add this element to array
nullify(e) !cleanup for next loop
end do
call json_add(json_phonebook,p) !add p to json_phonebook
nullify(p) !no longer need this
!write it to a file:
call json_print(json_phonebook,'phonebook.json')
! read directly from a character string
call json%load_from_string('{ "PhoneBook": [ { "name": "Adam", "phone": "0000001" },&
{ "name": "Eve", "phone": "0000002" }, { "name": "Julia", "phone": "6666666" } ]}')
! print it to the console
call json%print_file()
end program json_fortran

View file

@ -18,7 +18,7 @@ words=:(0;(0 10#:10*".;._2]0 :0);classes)&;: NB. states:
)
tokens=. ;:'[ ] , { } :'
actions=: lBra`rBracket`comma`lBra`rBracket`colon`value
actions=: lBra`rBracket`comma`lBra`rBrace`colon`value
NB. action verbs argument conventions:
NB. x -- boxed json word
@ -32,7 +32,7 @@ jsonParse=: 0 {:: (,a:) ,&.> [: actions@.(tokens&i.@[)/ [:|.a:,words
lBra=: a: ,~ ]
rBracket=: _2&}.@], [:< _2&{::@], _1&{@]
comma=: ]
rBrace=: _2&}.@], [:< _2&{::@], [:|: (2,~ [: -:@$ _1&{@]) $ _1&{@]
rBrace=: _2&}.@], [:< _2&{::@](, <) [:|: (2,~ [: -:@$ _1&{::@]) $ _1&{::@]
colon=: ]
value=: _1&}.@], [:< _1&{::@], jsonValue&.>@[
@ -43,7 +43,8 @@ jsonValue=:]
require'strings'
jsonSer2=: jsonSer1@(<"_1^:(0>.#@$-1:))
jsonSer1=: ']' ,~ '[' }:@;@; (',' ,~ jsonSerialize)&.>
jsonSer0=: '"', jsonEsc@:":, '"'"_
jsonEsc=: rplc&(<;._1' \ \\ " \"')
jsonSerialize=:jsonSer0`jsonSer1@.(*@L.)
jsonSerialize=:jsonSer0`jsonSer2@.(*@L.)

View file

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

View file

@ -0,0 +1,4 @@
> JSON:-ParseString("[{\"tree\": \"maple\", \"count\": 21}]");
[table(["tree" = "maple", "count" = 21])]
> JSON:-ToString( [table(["tree" = "maple", "count" = 21])] );
"[{\"count\": 21, \"tree\": \"maple\"}]"

View file

@ -0,0 +1,34 @@
OpenConsole()
If CreateJSON(1)
PB_Team_Members=SetJSONObject(JSONValue(1))
SetJSONString(AddJSONMember(PB_Team_Members,"PB_Team_Member_1"),"Frederic Laboureur")
SetJSONString(AddJSONMember(PB_Team_Members,"PB_Team_Member_2"),"Andre Beer")
SetJSONString(AddJSONMember(PB_Team_Members,"PB_Team_Member_3"),"Timo Harter")
EndIf
If CreateJSON(2)
Former_Team_Members=SetJSONArray(JSONValue(2))
SetJSONString(AddJSONElement(Former_Team_Members),"Richard Andersson")
SetJSONString(AddJSONElement(Former_Team_Members),"Benny Sels")
SetJSONString(AddJSONElement(Former_Team_Members),"Danilo Krahn")
EndIf
PrintN("PureBasic - Team Members:")
PrintN(ComposeJSON(1,#PB_JSON_PrettyPrint)+#CRLF$)
PrintN("PureBasic - Former Team Members:")
PrintN(ComposeJSON(2,#PB_JSON_PrettyPrint)+#CRLF$)
#DL=Chr(34)
PB_Special_thanks$="[ " +#DL+"Gary Willoughby"+#DL+", " +#DL+"Mark James"+#DL+", " +#DL+"Neil Hodgson"+#DL+" ]"
NewList otherpersons.s()
If ParseJSON(3,PB_Special_thanks$)
ExtractJSONList(JSONValue(3),otherpersons())
PrintN("Pure Basic - and others:")
ForEach otherpersons() : PrintN(otherpersons()) : Next
Else
PrintN(JSONErrorMessage() + " : " + Str(JSONErrorPosition()))
PrintN(Left(PB_Special_thanks$,JSONErrorPosition()))
PrintN(Mid(PB_Special_thanks$,JSONErrorPosition()+1))
EndIf
Input()

View file

@ -5,3 +5,4 @@ puts ruby_obj.class
puts ruby_obj["blue"].class
ruby_obj["ocean"] = {"water" => %w{fishy salty}}
puts JSON.generate(ruby_obj)
puts JSON.pretty_generate(ruby_obj)

View file

@ -1,14 +1,16 @@
extern crate serialize;
use serialize::json;
#[deriving(Decodable, Encodable)]
extern crate rustc_serialize;
use rustc_serialize::json;
#[derive(RustcDecodable, RustcEncodable)]
struct Penguin {
name : String,
born : i16
}
fn main() {
let pengu = Penguin { name : "pengu".to_string(), born : 1999 };
println!("{}", json::encode(&pengu));
println!("{}", json::encode(&pengu).unwrap());
let pingu : Penguin = json::decode(r##"{"name":"pingu","born":2001}"##).unwrap();
assert_eq!(pingu.name.as_slice(), "pingu");
assert_eq!(&pingu.name, "pingu");
assert_eq!(pingu.born, 2001);
}

12
Task/JSON/Scheme/json.ss Normal file
View file

@ -0,0 +1,12 @@
(use json)
(define object-example
(with-input-from-string "{\"foo\": \"bar\", \"baz\": [1, 2, 3]}"
json-read))
(pp object-example)
; this prints #(("foo" . "bar") ("baz" 1 2 3))
(json-write #([foo . bar]
[baz 1 2 3]
[qux . #((rosetta . code))]))
; this writes the following:
; {"foo": "bar", "baz": [1, 2, 3], "qux": {"foo": "bar"}}

View file

@ -1,4 +1,5 @@
package require Tcl 8.6
package require json::write
proc tcl2json value {
# Guess the type of the value; deep *UNSUPPORTED* magic!
@ -7,25 +8,14 @@ proc tcl2json value {
switch $type {
string {
# Skip to the mapping code at the bottom
return [json::write string $value]
}
dict {
set result "{"
set pfx ""
dict for {k v} $value {
append result $pfx [tcl2json $k] ": " [tcl2json $v]
set pfx ", "
}
return [append result "}"]
return [json::write object {*}[
dict map {k v} $value {tcl2json $v}]]
}
list {
set result "\["
set pfx ""
foreach v $value {
append result $pfx [tcl2json $v]
set pfx ", "
}
return [append result "\]"]
return [json::write array {*}[lmap v $value {tcl2json $v}]]
}
int - double {
return [expr {$value}]
@ -46,11 +36,7 @@ proc tcl2json value {
} elseif {[string is boolean -strict $value]} {
return [expr {$value ? "true" : "false"}]
}
return [json::write string $value]
}
}
# For simplicity, all "bad" characters are mapped to \u... substitutions
set mapped [subst -novariables [regsub -all {[][\u0000-\u001f\\""]} \
$value {[format "\\\\u%04x" [scan {& } %c]]}]]
return "\"$mapped\""
}