Skip to content

Instantly share code, notes, and snippets.

@mbeale
mbeale / gist:4247991
Created December 10, 2012 02:12
Dynamic JSON sample golang #5
func main(){
dyn, err := MakeJSONContainer(rawData)
if err != nil {
fmt.Println("error:", err)
} else {
for _, v := range dyn.All() {
if val ,err := v.Get("payload.deeper_nesting.return_value");err == nil {
println(val)
} else {
fmt.Println("error:", err)
@mbeale
mbeale / gist:4247971
Created December 10, 2012 02:01
Dynamic JSON sample golang #4
type JSONContainer struct {
data []interface{}
}
func (j *JSONContainer) All() (objects []JSONObject) {
for _, v := range j.data {
objects = append(objects, JSONObject{data: v})
}
return
}
@mbeale
mbeale / gist:4242562
Created December 8, 2012 23:46
Dynamic JSON sample golang #3
val, _ := v.Get("payload.deeper_nesting.return_value")
@mbeale
mbeale / gist:4242532
Created December 8, 2012 23:38
Dynamic JSON sample golang #2
//Document Container
type JSONContainer struct {
data []interface{}
}
//Return all objects
func (j *JSONContainer) All() (objects []JSONObject) {
for _, v := range j.data {
newJSONObject := JSONObject{data: v}
objects = append(objects, newJSONObject)
@mbeale
mbeale / gist:4241224
Created December 8, 2012 18:17
Sample JSON record for Dynamic JSON
{
"type" : "CreateEvent",
"actor" : {
"avatar_url" : "https://secure.gravatar.com/avatar/05abda697c2654a02391248bed3a5f3e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
"url" : "https://api.github.com/users/mbeale",
"id" : 1507647,
"gravatar_id" : "05abda697c2654a02391248bed3a5f3e",
"login" : "mbeale"
},
"repo" : {
@mbeale
mbeale / gist:4241157
Created December 8, 2012 17:47
Dynamic JSON sample1
var jsondata []interface{}
err := json.Unmarshal(rawData, &jsondata)
if err == nil{
m := v.(map[string]interface{})
//the following prints out the type
fmt.Printf("%s \n",m["type"])
} else {
fmt.Println("error:", err)
}
@mbeale
mbeale / gist:4241133
Created December 8, 2012 17:40
Sample Data set for Dynamic JSON
[ {
"type" : "WatchEvent",
"actor" : {
"avatar_url" : "https://secure.gravatar.com/avatar/f873c249ce2812e897cdd3eb8cc697ea?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
"url" : "https://api.github.com/users/pkorotkov",
"id" : 532567,
"gravatar_id" : "f873c249ce2812e897cdd3eb8cc697ea",
"login" : "pkorotkov"
},
"repo" : {
@mbeale
mbeale / gist:4113026
Created November 19, 2012 19:19
Recurly Error Handling PHP Best practices
try{
$subscription = new Recurly_Subscription();
$account = Recurly_Account::get('1'); //this will throw a not found exception if the account_code does not exist
$subscription->account = $account;
$subscription->create(); //this will throw a validation error exception as there are missing fields
}
catch (Exception $e) {
//for a list of possible exception types go to
//https://github.com/recurly/recurly-client-php/blob/master/lib/recurly/errors.php
//you could use send these messages to a log for later analysis
@mbeale
mbeale / gist:2732090
Created May 19, 2012 19:28
GO and XML sample4
//Generic Reader
type nopCloser struct {
io.Reader
}
//update function
func (a *Account) Update() error {
if xmlstring, err := xml.MarshalIndent(a, "", " "); err == nil {
xmlstring = []byte(xml.Header + string(xmlstring))
client := &http.Client{}
@mbeale
mbeale / gist:2731955
Created May 19, 2012 18:55
GO and XML sample3
account Account
//resp is http.Response from http.Client
if body, readerr := ioutil.ReadAll(resp.Body); readerr == nil {
//load object xml
if xmlerr := xml.Unmarshal(body, &account); xmlerr != nil {
return xmlerr
}
} else {
//return read error
return readerr