glensargent / diff

JSON Unmarshal that returns a map of fields that aren't unmarshalled in to the target struct, allowing you to handle the difference

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Description

Wrapper for json.Unmarshal that returns the extra fields that weren't unmarshalled. Useful for keeping an eye on, and catching unexpected differences in API responses instead of just ignoring them.

Usage

import "github.com/glensargent/diff"

type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

// this json is adding an extra field, city, that's uncaught by the struct
bytes := []byte(`{"name":"merlin", "age": 30, "city": "new york"}`)
// person will unmarshal as intended
var person Person
// diff returns a map of the fields that weren't unmarshalled in to the struct, in this case, city
difference, err := Unmarshal(bytes, &person)
if err != nil {
    t.Fatal(err)
}

log.Println("person:", person)
log.Println("extra fields:", difference)

How to get

go get github.com/glensargent/diff

About

JSON Unmarshal that returns a map of fields that aren't unmarshalled in to the target struct, allowing you to handle the difference


Languages

Language:Go 100.0%