Skip to content

Instantly share code, notes, and snippets.

@darthberen
Last active August 29, 2015 14:15
Show Gist options
  • Save darthberen/246c8e06ef0b69188d42 to your computer and use it in GitHub Desktop.
Save darthberen/246c8e06ef0b69188d42 to your computer and use it in GitHub Desktop.
POC code demostrating an interesting side effect of Go JSON unmarshalling
package main
import (
"encoding/json"
"fmt"
)
type eventWithBoth struct {
EventId string `json:"_eventId"`
Eventid string `json:"_eventid"`
}
func (e eventWithBoth) String() string {
return fmt.Sprintf("_eventid: %s\n_eventId: %s\n", e.Eventid, e.EventId)
}
type eventLowerOnly struct {
Eventid string `json:"_eventid"`
}
func (e eventLowerOnly) String() string {
return fmt.Sprintf("_eventid: %s\n", e.Eventid)
}
type eventCasedOnly struct {
EventId string `json:"_eventId"`
}
func (e eventCasedOnly) String() string {
return fmt.Sprintf("_eventId: %s\n", e.EventId)
}
func main() {
var testEvent1 *eventWithBoth
sample1 := `{"_eventId": "cased"}`
json.Unmarshal([]byte(sample1), &testEvent1)
fmt.Printf("Example 1 (works as expected with both fields included on struct):\nJSON: %s\n%s\n", sample1, testEvent1.String())
var testEvent2 *eventWithBoth
sample2 := []byte(`{"_eventid": "no case"}`)
json.Unmarshal([]byte(sample2), &testEvent2)
fmt.Printf("Example 2 (works as expected with both fields included on struct):\nJSON: %s\n%s\n", sample2, testEvent2.String())
var testEvent3 *eventWithBoth
sample3 := []byte(`{"_eventid": "no case", "_eventId": "cased"}`)
json.Unmarshal([]byte(sample3), &testEvent3)
fmt.Printf("Example 3 (works as expected with both fields included on struct):\nJSON: %s\n%s\n", sample3, testEvent3.String())
var testEvent4 *eventWithBoth
sample4 := []byte(`{"_eventId": "cased", "_eventid": "no case"}`)
json.Unmarshal([]byte(sample4), &testEvent4)
fmt.Printf("Example 4 (works as expected with both fields included on struct):\nJSON: %s\n%s\n", sample4, testEvent4.String())
var testEvent5 *eventLowerOnly
sample5 := []byte(`{"_eventId": "cased"}`)
json.Unmarshal([]byte(sample5), &testEvent5)
fmt.Printf("Example 5 (expected empty string):\nJSON: %s\n%s\n", sample5, testEvent5.String())
var testEvent6 *eventLowerOnly
sample6 := []byte(`{"_eventid": "no case"}`)
json.Unmarshal([]byte(sample6), &testEvent6)
fmt.Printf("Example 6 (works as expected):\nJSON: %s\n%s\n", sample6, testEvent6.String())
var testEvent7 *eventLowerOnly
sample7 := []byte(`{"_eventid": "no case", "_eventId": "cased"}`)
json.Unmarshal([]byte(sample7), &testEvent7)
fmt.Printf("Example 7 (note how the JSON field ordering matters):\nJSON: %s\n%s\n", sample7, testEvent7.String())
var testEvent8 *eventLowerOnly
sample8 := []byte(`{"_eventId": "cased", "_eventid": "no case"}`)
json.Unmarshal([]byte(sample8), &testEvent8)
fmt.Printf("Example 8 (note how the JSON field ordering matters):\nJSON: %s\n%s\n", sample8, testEvent8.String())
var testEvent9 *eventCasedOnly
sample9 := []byte(`{"_eventId": "cased"}`)
json.Unmarshal([]byte(sample9), &testEvent9)
fmt.Printf("Example 9 (works as expected):\nJSON: %s\n%s\n", sample9, testEvent9.String())
var testEvent10 *eventCasedOnly
sample10 := []byte(`{"_eventid": "no case"}`)
json.Unmarshal([]byte(sample10), &testEvent10)
fmt.Printf("Example 10 (expected empty string):\nJSON: %s\n%s\n", sample10, testEvent10.String())
var testEvent11 *eventCasedOnly
sample11 := []byte(`{"_eventid": "no case", "_eventId": "cased"}`)
json.Unmarshal([]byte(sample11), &testEvent11)
fmt.Printf("Example 11 (note how the JSON field ordering matters):\nJSON: %s\n%s\n", sample11, testEvent11.String())
var testEvent12 *eventCasedOnly
sample12 := []byte(`{"_eventId": "cased", "_eventid": "no case"}`)
json.Unmarshal([]byte(sample12), &testEvent12)
fmt.Printf("Example 12 (note how the JSON field ordering matters):\nJSON: %s\n%s\n", sample12, testEvent12.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment