This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

14
Task/JSON/C++/json.cpp Normal file
View file

@ -0,0 +1,14 @@
#include "Core/Core.h"
using namespace Upp;
CONSOLE_APP_MAIN
{
JsonArray a;
a << Json("name", "John")("phone", "1234567") << Json("name", "Susan")("phone", "654321");
String txt = ~a;
Cout() << txt << '\n';
Value v = ParseJSON(txt);
for(int i = 0; i < v.GetCount(); i++)
Cout() << v[i]["name"] << ' ' << v[i]["phone"] << '\n';
}

23
Task/JSON/C-sharp/json.cs Normal file
View file

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
class Program
{
static void Main()
{
var people = new Dictionary<string, object> {{"1", "John"}, {"2", "Susan"}};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(people);
Console.WriteLine(json);
var deserialized = serializer.Deserialize<Dictionary<string, object>>(json);
Console.WriteLine(deserialized["2"]);
var jsonObject = serializer.DeserializeObject(@"{ ""foo"": 1, ""bar"": [10, ""apples""] }");
var data = jsonObject as Dictionary<string, object>;
var array = data["bar"] as object[];
Console.WriteLine(array[1]);
}
}

View file

@ -0,0 +1,10 @@
Class Sample.JSON [ Abstract ]
{
ClassMethod GetPerson(ByRef pParms, Output pObject As %RegisteredObject) As %Status
{
Set pObject=##class(Sample.Person).%OpenId(pParms("oid"))
Quit $$$OK
}
}

View file

@ -0,0 +1,9 @@
(ql:quickload '("cl-json"))
(json:encode-json
'#( ((foo . (1 2 3)) (bar . t) (baz . #\!))
"quux" 4/17 4.25))
(print (with-input-from-string
(s "{\"foo\": [1, 2, 3], \"bar\": true, \"baz\": \"!\"}")
(json:decode-json s)))

6
Task/JSON/D/json.d Normal file
View file

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

15
Task/JSON/EGL/json-1.egl Normal file
View file

@ -0,0 +1,15 @@
record familyMember
person person;
relationships relationship[]?;
end
record person
firstName string;
lastName string;
age int;
end
record relationship
relationshipType string;
id int;
end

22
Task/JSON/EGL/json-2.egl Normal file
View file

@ -0,0 +1,22 @@
people Person[]; // Array of people
people.appendElement(new Person { firstName = "Frederick", lastName = "Flintstone", age = 35} );
people.appendElement(new Person { firstName = "Wilma", lastName = "Flintstone", age = 34} );
people.appendElement(new Person { firstName = "Pebbles", lastName = "Flintstone", age = 2} );
people.appendElement(new Person { firstName = "Bernard", lastName = "Rubble", age = 32} );
people.appendElement(new Person { firstName = "Elizabeth", lastName = "Rubble", age = 29} );
people.appendElement(new Person { firstName = "Bam Bam", lastName = "Rubble", age = 2} );
family Dictionary; // A dictionary of family members using a uid as key
family["1"] = new FamilyMember{ person = people[1], relationships = [new Relationship{ relationshipType="spouse", id = 2 }, new Relationship{ relationshipType="child", id = 3}] };
family["2"] = new FamilyMember{ person = people[2], relationships = [new Relationship{ relationshipType="spouse", id = 1 }, new Relationship{ relationshipType="child", id = 3}] };
family["3"] = new FamilyMember{ person = people[3], relationships = [new Relationship{ relationshipType="mother", id = 2 }, new Relationship{ relationshipType="father", id = 1}] };
family["4"] = new FamilyMember{ person = people[4], relationships = [new Relationship{ relationshipType="spouse", id = 5 }, new Relationship{ relationshipType="child", id = 6}] };
family["5"] = new FamilyMember{ person = people[5], relationships = [new Relationship{ relationshipType="spouse", id = 4 }, new Relationship{ relationshipType="child", id = 6}] };
family["6"] = new FamilyMember{ person = people[6], relationships = [new Relationship{ relationshipType="mother", id = 5 }, new Relationship{ relationshipType="father", id = 4}] };
// Convert dictionary of family members to JSON string
jsonString string = jsonLib.convertToJSON(family);
// Show JSON string
SysLib.writeStdout(jsonString);

25
Task/JSON/EGL/json-3.egl Normal file
View file

@ -0,0 +1,25 @@
// Convert JSON string into dictionary of family members
family Dictionary;
jsonLib.convertFromJSON(jsonString, family);
// List family members and their relationships
familyMember FamilyMember;
relation FamilyMember;
keys string[] = family.getKeys();
for(i int from 1 to keys.getSize())
SysLib.writeStdout("----------------------------------------------------");
familyMember = family[keys[i]];
SysLib.writeStdout(familyMember.person.lastName + ", " + familyMember.person.firstName + " - " + familyMember.person.age);
for(j int from 1 to familyMember.relationships.getSize())
id string = familyMember.relationships[j].id;
relation = family[id];
SysLib.writeStdout(familyMember.relationships[j].relationshipType + ": " +
relation.person.lastName + ", " + relation.person.firstName);
end
end

15
Task/JSON/EGL/json-4.egl Normal file
View file

@ -0,0 +1,15 @@
// Service function definition
function geocode(address String) returns (GoogleGeocoding) {
@Resource{uri = "binding:GoogleGeocodingBinding"},
@Rest{method = _GET, uriTemplate = "/json?address={address}&sensor=false",
requestFormat = None, responseFormat = JSON}
}
// Invoke service function
call geocode("111 Maple Street, Somewhere, CO") returning to callback;
function callBack(result GoogleGeocoding in)
SysLib.writeStdout(result.status);
SysLib.writeStdout(result.results[1].geometry.location.lat);
SysLib.writeStdout(result.results[1].geometry.location.lng);
end