jsonem.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package jsonem
  2. import (
  3. "io"
  4. "v2ray.com/core"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/cmdarg"
  7. "v2ray.com/core/infra/conf"
  8. "v2ray.com/core/infra/conf/serial"
  9. "v2ray.com/core/main/confloader"
  10. )
  11. func init() {
  12. common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
  13. Name: "JSON",
  14. Extension: []string{"json"},
  15. Loader: func(input interface{}) (*core.Config, error) {
  16. switch v := input.(type) {
  17. case cmdarg.Arg:
  18. cf := &conf.Config{}
  19. for i, arg := range v {
  20. newError("Reading config: ", arg).AtInfo().WriteToLog()
  21. r, err := confloader.LoadConfig(arg)
  22. common.Must(err)
  23. c, err := serial.DecodeJSONConfig(r)
  24. common.Must(err)
  25. if i == 0 {
  26. // This ensure even if the muti-json parser do not support a setting,
  27. // It is still respected automatically for the first configure file
  28. *cf = *c
  29. continue
  30. }
  31. cf.Override(c, arg)
  32. }
  33. return cf.Build()
  34. case io.Reader:
  35. return serial.LoadJSONConfig(v)
  36. default:
  37. return nil, newError("unknow type")
  38. }
  39. },
  40. }))
  41. }