File: json.go

package info (click to toggle)
golang-github-twin-deepmerge 0.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 108 kB
  • sloc: makefile: 2
file content (31 lines) | stat: -rw-r--r-- 683 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package deepmerge

import (
	"encoding/json"
)

// JSON merges the contents of src into dst
func JSON(dst, src []byte, optionalConfig ...Config) ([]byte, error) {
	var cfg Config
	if len(optionalConfig) > 0 {
		cfg = optionalConfig[0]
	} else {
		cfg = Config{PreventMultipleDefinitionsOfKeysWithPrimitiveValue: true}
	}
	var dstMap, srcMap map[string]interface{}
	err := json.Unmarshal(dst, &dstMap)
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal(src, &srcMap)
	if err != nil {
		return nil, err
	}
	if dstMap == nil {
		dstMap = make(map[string]interface{})
	}
	if err = DeepMerge(dstMap, srcMap, cfg); err != nil {
		return nil, err
	}
	return json.Marshal(dstMap)
}