Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,2 +1,5 @@
Load a [[wp:JSON|JSON]] string into a data structure. Also create a new data structure and serialize it into JSON.
Use objects and arrays (as appropriate for your language) and make sure your JSON is valid (http://www.jsonlint.com/).
Load a [[wp:JSON|JSON]] string into a data structure.
Also create a new data structure and serialize it into JSON.
Use objects and arrays (as appropriate for your language)
and make sure your JSON is valid (http://www.jsonlint.com/).

View file

@ -0,0 +1 @@
put$(jsn$(get$("input.json",JSN)),"output.JSN,NEW)

View file

@ -0,0 +1 @@
get$("myfile.json",JSN)

View file

@ -0,0 +1 @@
get$("[1,2,3]",JSN,MEM)

View file

@ -0,0 +1 @@
jsn$(,1 2 3)

View file

@ -0,0 +1 @@
put$("[1,2,3]","array.json",NEW)

View file

@ -0,0 +1,4 @@
( get$("rosetta.json",JSN):?json
& lst$(json,"json.bra",NEW)
& put$(jsn$!json,"rosetta-roundtrip.json",NEW)
)

View file

@ -1,6 +1,6 @@
import std.stdio, std.json;
void main() {
auto j = parseJSON("{ \"foo\": 1, \"bar\": [10, \"apples\"] }");
auto j = parseJSON(`{ "foo": 1, "bar": [10, "apples"] }`);
writeln(toJSON(&j));
}

View file

@ -0,0 +1,16 @@
{-# LANGUAGE TemplateHaskell, OverloadedStrings #-}
import Data.Aeson
import Data.Aeson.TH
data Person = Person { firstName :: String
, lastName :: String
, age :: Maybe Int
} deriving (Show, Eq)
$(deriveJSON defaultOptions ''Person)
main = do
let test1 = "{\"firstName\":\"Bob\", \"lastName\":\"Smith\"}"
test2 = "{\"firstName\":\"Miles\", \"lastName\":\"Davis\", \"age\": 45}"
print (decode test1 :: Maybe Person)
print (decode test2 :: Maybe Person)

38
Task/JSON/Lua/json.lua Normal file
View file

@ -0,0 +1,38 @@
local json = require("json")
local json_data = [=[[
42,
3.14159,
[ 2, 4, 8, 16, 32, 64, "apples", "bananas", "cherries" ],
{ "H": 1, "He": 2, "X": null, "Li": 3 },
null,
true,
false
]]=]
print("Original JSON: " .. json_data)
local data = json.decode(json_data)
json.util.printValue(data, 'Lua')
print("JSON re-encoded: " .. json.encode(data))
local data = {
42,
3.14159,
{
2, 4, 8, 16, 32, 64,
"apples",
"bananas",
"cherries"
},
{
H = 1,
He = 2,
X = json.util.null(),
Li = 3
},
json.util.null(),
true,
false
}
print("JSON from new Lua data: " .. json.encode(data))

View file

@ -0,0 +1,7 @@
# JSON input is being stored in ordered hashtable.
# Ordered hashtable is available in PowerShell v3 and higher.
[ordered]@{ "foo"= 1; "bar"= 10, "apples" } | ConvertTo-Json
# ConvertFrom-Json converts a JSON-formatted string to a custom object.
# If you use the Invoke-RestMethod cmdlet there is not need for the ConvertFrom-Json cmdlet
Invoke-WebRequest -Uri "http://date.jsontest.com" | ConvertFrom-Json

View file

@ -0,0 +1,11 @@
{
"foo": 1,
"bar": [
10,
"apples"
]
}
time milliseconds_since_epoch date
---- ------------------------ ----
12:25:25 PM 1414326325923 10-26-2014

View file

@ -0,0 +1,22 @@
:- use_module([ library(http/json),
library(func) ]).
test_json('{"widget": { "debug": "on", "window": { "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 }, "image": { "src": "Images/Sun.png", "name": "sun1", "hOffset": 250, "vOffset": 250, "alignment": "center" }, "text": { "data": "Click Here", "size": 36, "style": "bold", "name": "text1", "hOffset": 250, "vOffset": 100, "alignment": "center", "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" }}}').
reading_JSON_term :-
atom_json_dict(test_json(~), Dict, []), %% This accomplishes reading in the JSON data
writeln( 'JSON as Prolog dict: ~w~n'
$ Dict),
writeln( 'Access field "widget.text.data": ~s~n'
$ Dict.widget.text.data),
writeln( 'Alter field "widget": ~w~n'
$ Dict.put(widget, "Altered")).
searalize_a_JSON_term :-
Dict = _{book:_{title:"To Mock a Mocking Bird",
author:_{first_name:"Ramond",
last_name:"Smullyan"},
publisher:"Alfred A. Knopf",
year:1985
}},
json_write(current_output, Dict). %% This accomplishes serializing the JSON object.

View file

@ -0,0 +1,19 @@
?- reading_JSON_term.
JSON as Prolog dict: _G5217{widget:_G5207{debug:on,image:_G5123{alignment:center,hOffset:250,name:sun1,src:Images/Sun.png,vOffset:250},text:_G5189{alignment:center,data:Click Here,hOffset:250,name:text1,onMouseUp:sun1.opacity = (sun1.opacity / 100) * 90;,size:36,style:bold,vOffset:100},window:_G5077{height:500,name:main_window,title:Sample Konfabulator Widget,width:500}}}
Access field "widget.text.data": Click Here
Alter field "widget": _G5217{widget:Altered}
true.
?- searalize_a_JSON_term.
{
"book": {
"author": {"first_name":"Ramond", "last_name":"Smullyan"},
"publisher":"Alfred A. Knopf",
"title":"To Mock a Mocking Bird",
"year":1985
}
}
true.

14
Task/JSON/Rust/json.rust Normal file
View file

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

View file

@ -0,0 +1 @@
NeoJSONReader fromString: '{ "foo": 1, "bar": [10, "apples"] }'.