func handleMap(m map[string]interface{}, p interface{}) interface{} {
	c, ok := p.(map[string]interface{})
	if !ok {
		return p
	}
	for k, v := range c {
		if _, exists := m[k]; !exists {
			if v == nil {
				continue
			}
			if n, ok := v.(map[string]interface{}); ok {
				m[k] = removeNull(n)
			} else {
				m[k] = v
			}
			continue
		}
		if v == nil {
			delete(m, k)
			continue
		}
		if n, ok := v.(map[string]interface{}); ok {
			m[k] = patch(m[k], n)
		} else {
			m[k] = v
		}
	}
	return m
}
handleMap handles patching of map values.
package jsonmp
import (
	"encoding/json"
	"io"
)
func Patch(a, b []byte) ([]byte, error) {
	a = append(a, 0)
	copy(a[1:], a[0:])
	a[0] = '['
	a = append(a, ',')
	a = append(a, b...)
	a = append(a, ']')
	var i []interface{}
	if err := json.Unmarshal(a, &i); err != nil {
		return nil, err
	}
	return json.Marshal(patch(i[0], i[1]))
}
func PatchValue(a, b, dst interface{}) error {
	var err error
	if a, err = coerce(a); err != nil {
		return err
	}
	if b, err = coerce(b); err != nil {
		return err
	}
	return marshalValue(a, b, dst)
}
func PatchValueWithBytes(a interface{}, c []byte, dst interface{}) error {
	var err error
	if a, err = coerce(a); err != nil {
		return err
	}
	var b interface{}
	if err = json.Unmarshal(c, &b); err != nil {
		return err
	}
	return marshalValue(a, b, dst)
}
func PatchValueWithReader(a interface{}, r io.Reader, dst interface{}) error {
	var err error
	if a, err = coerce(a); err != nil {
		return err
	}
	var b interface{}
	if err = json.NewDecoder(r).Decode(&b); err != nil {
		return err
	}
	return marshalValue(a, b, dst)
}
func marshalValue(a, b, dst interface{}) error {
	c, err := json.Marshal(patch(a, b))
	if err != nil {
		return err
	}
	return json.Unmarshal(c, &dst)
}
type Patcher struct {
	r io.Reader
	w io.Writer
}
func NewPatcher(r io.Reader, w io.Writer) *Patcher {
	return &Patcher{r, w}
}
func (p *Patcher) Patch(c []byte) error {
	b, err := read(p.r)
	if err != nil {
		return err
	}
	var a interface{}
	if err := json.Unmarshal(c, &a); err != nil {
		return err
	}
	return p.write(a, b)
}
func (p *Patcher) PatchValue(a interface{}) error {
	var err error
	if a, err = coerce(a); err != nil {
		return err
	}
	b, err := read(p.r)
	if err != nil {
		return err
	}
	return p.write(a, b)
}
func read(r io.Reader) (interface{}, error) {
	var i interface{}
	err := json.NewDecoder(r).Decode(&i)
	return i, err
}
func (p *Patcher) write(a, b interface{}) error {
	return json.NewEncoder(p.w).Encode(patch(a, b))
}
func coerce(i interface{}) (interface{}, error) {
	if i == nil {
		return i, nil
	}
	switch i.(type) {
	case map[string]interface{}:
		return i, nil
	case []interface{}:
		return i, nil
	}
	b, err := json.Marshal(i)
	if err != nil {
		return nil, err
	}
	var v interface{}
	return v, json.Unmarshal(b, &v)
}
func patch(a, b interface{}) interface{} {
	if m, ok := a.(map[string]interface{}); ok {
		return handleMap(m, b)
	}
	if m, ok := b.(map[string]interface{}); ok {
		return removeNull(m)
	}
	return b
}
func removeNull(m map[string]interface{}) map[string]interface{} {
	for k, v := range m {
		if v == nil {
			delete(m, k)
			continue
		}
		if c, ok := v.(map[string]interface{}); ok {
			m[k] = removeNull(c)
		}
	}
	return m
}