jsonLoader.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // Copyright 2015 xeipuuv ( https://github.com/xeipuuv )
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // author xeipuuv
  15. // author-github https://github.com/xeipuuv
  16. // author-mail [email protected]
  17. //
  18. // repository-name gojsonschema
  19. // repository-desc An implementation of JSON Schema, based on IETF's draft v4 - Go language.
  20. //
  21. // description Different strategies to load JSON files.
  22. // Includes References (file and HTTP), JSON strings and Go types.
  23. //
  24. // created 01-02-2015
  25. package gojsonschema
  26. import (
  27. "bytes"
  28. "encoding/json"
  29. "errors"
  30. "io"
  31. "io/ioutil"
  32. "net/http"
  33. "os"
  34. "path/filepath"
  35. "runtime"
  36. "strings"
  37. "github.com/xeipuuv/gojsonreference"
  38. )
  39. var osFS = osFileSystem(os.Open)
  40. // JSON loader interface
  41. type JSONLoader interface {
  42. JsonSource() interface{}
  43. LoadJSON() (interface{}, error)
  44. JsonReference() (gojsonreference.JsonReference, error)
  45. LoaderFactory() JSONLoaderFactory
  46. }
  47. type JSONLoaderFactory interface {
  48. New(source string) JSONLoader
  49. }
  50. type DefaultJSONLoaderFactory struct {
  51. }
  52. type FileSystemJSONLoaderFactory struct {
  53. fs http.FileSystem
  54. }
  55. func (d DefaultJSONLoaderFactory) New(source string) JSONLoader {
  56. return &jsonReferenceLoader{
  57. fs: osFS,
  58. source: source,
  59. }
  60. }
  61. func (f FileSystemJSONLoaderFactory) New(source string) JSONLoader {
  62. return &jsonReferenceLoader{
  63. fs: f.fs,
  64. source: source,
  65. }
  66. }
  67. // osFileSystem is a functional wrapper for os.Open that implements http.FileSystem.
  68. type osFileSystem func(string) (*os.File, error)
  69. func (o osFileSystem) Open(name string) (http.File, error) {
  70. return o(name)
  71. }
  72. // JSON Reference loader
  73. // references are used to load JSONs from files and HTTP
  74. type jsonReferenceLoader struct {
  75. fs http.FileSystem
  76. source string
  77. }
  78. func (l *jsonReferenceLoader) JsonSource() interface{} {
  79. return l.source
  80. }
  81. func (l *jsonReferenceLoader) JsonReference() (gojsonreference.JsonReference, error) {
  82. return gojsonreference.NewJsonReference(l.JsonSource().(string))
  83. }
  84. func (l *jsonReferenceLoader) LoaderFactory() JSONLoaderFactory {
  85. return &FileSystemJSONLoaderFactory{
  86. fs: l.fs,
  87. }
  88. }
  89. // NewReferenceLoader returns a JSON reference loader using the given source and the local OS file system.
  90. func NewReferenceLoader(source string) *jsonReferenceLoader {
  91. return &jsonReferenceLoader{
  92. fs: osFS,
  93. source: source,
  94. }
  95. }
  96. // NewReferenceLoaderFileSystem returns a JSON reference loader using the given source and file system.
  97. func NewReferenceLoaderFileSystem(source string, fs http.FileSystem) *jsonReferenceLoader {
  98. return &jsonReferenceLoader{
  99. fs: fs,
  100. source: source,
  101. }
  102. }
  103. func (l *jsonReferenceLoader) LoadJSON() (interface{}, error) {
  104. var err error
  105. reference, err := gojsonreference.NewJsonReference(l.JsonSource().(string))
  106. if err != nil {
  107. return nil, err
  108. }
  109. refToUrl := reference
  110. refToUrl.GetUrl().Fragment = ""
  111. var document interface{}
  112. if reference.HasFileScheme {
  113. filename := strings.Replace(refToUrl.GetUrl().Path, "file://", "", -1)
  114. if runtime.GOOS == "windows" {
  115. // on Windows, a file URL may have an extra leading slash, use slashes
  116. // instead of backslashes, and have spaces escaped
  117. if strings.HasPrefix(filename, "/") {
  118. filename = filename[1:]
  119. }
  120. filename = filepath.FromSlash(filename)
  121. }
  122. document, err = l.loadFromFile(filename)
  123. if err != nil {
  124. return nil, err
  125. }
  126. } else {
  127. document, err = l.loadFromHTTP(refToUrl.String())
  128. if err != nil {
  129. return nil, err
  130. }
  131. }
  132. return document, nil
  133. }
  134. func (l *jsonReferenceLoader) loadFromHTTP(address string) (interface{}, error) {
  135. resp, err := http.Get(address)
  136. if err != nil {
  137. return nil, err
  138. }
  139. // must return HTTP Status 200 OK
  140. if resp.StatusCode != http.StatusOK {
  141. return nil, errors.New(formatErrorDescription(Locale.HttpBadStatus(), ErrorDetails{"status": resp.Status}))
  142. }
  143. bodyBuff, err := ioutil.ReadAll(resp.Body)
  144. if err != nil {
  145. return nil, err
  146. }
  147. return decodeJsonUsingNumber(bytes.NewReader(bodyBuff))
  148. }
  149. func (l *jsonReferenceLoader) loadFromFile(path string) (interface{}, error) {
  150. f, err := l.fs.Open(path)
  151. if err != nil {
  152. return nil, err
  153. }
  154. defer f.Close()
  155. bodyBuff, err := ioutil.ReadAll(f)
  156. if err != nil {
  157. return nil, err
  158. }
  159. return decodeJsonUsingNumber(bytes.NewReader(bodyBuff))
  160. }
  161. // JSON string loader
  162. type jsonStringLoader struct {
  163. source string
  164. }
  165. func (l *jsonStringLoader) JsonSource() interface{} {
  166. return l.source
  167. }
  168. func (l *jsonStringLoader) JsonReference() (gojsonreference.JsonReference, error) {
  169. return gojsonreference.NewJsonReference("#")
  170. }
  171. func (l *jsonStringLoader) LoaderFactory() JSONLoaderFactory {
  172. return &DefaultJSONLoaderFactory{}
  173. }
  174. func NewStringLoader(source string) *jsonStringLoader {
  175. return &jsonStringLoader{source: source}
  176. }
  177. func (l *jsonStringLoader) LoadJSON() (interface{}, error) {
  178. return decodeJsonUsingNumber(strings.NewReader(l.JsonSource().(string)))
  179. }
  180. // JSON bytes loader
  181. type jsonBytesLoader struct {
  182. source []byte
  183. }
  184. func (l *jsonBytesLoader) JsonSource() interface{} {
  185. return l.source
  186. }
  187. func (l *jsonBytesLoader) JsonReference() (gojsonreference.JsonReference, error) {
  188. return gojsonreference.NewJsonReference("#")
  189. }
  190. func (l *jsonBytesLoader) LoaderFactory() JSONLoaderFactory {
  191. return &DefaultJSONLoaderFactory{}
  192. }
  193. func NewBytesLoader(source []byte) *jsonBytesLoader {
  194. return &jsonBytesLoader{source: source}
  195. }
  196. func (l *jsonBytesLoader) LoadJSON() (interface{}, error) {
  197. return decodeJsonUsingNumber(bytes.NewReader(l.JsonSource().([]byte)))
  198. }
  199. // JSON Go (types) loader
  200. // used to load JSONs from the code as maps, interface{}, structs ...
  201. type jsonGoLoader struct {
  202. source interface{}
  203. }
  204. func (l *jsonGoLoader) JsonSource() interface{} {
  205. return l.source
  206. }
  207. func (l *jsonGoLoader) JsonReference() (gojsonreference.JsonReference, error) {
  208. return gojsonreference.NewJsonReference("#")
  209. }
  210. func (l *jsonGoLoader) LoaderFactory() JSONLoaderFactory {
  211. return &DefaultJSONLoaderFactory{}
  212. }
  213. func NewGoLoader(source interface{}) *jsonGoLoader {
  214. return &jsonGoLoader{source: source}
  215. }
  216. func (l *jsonGoLoader) LoadJSON() (interface{}, error) {
  217. // convert it to a compliant JSON first to avoid types "mismatches"
  218. jsonBytes, err := json.Marshal(l.JsonSource())
  219. if err != nil {
  220. return nil, err
  221. }
  222. return decodeJsonUsingNumber(bytes.NewReader(jsonBytes))
  223. }
  224. type jsonIOLoader struct {
  225. buf *bytes.Buffer
  226. }
  227. func NewReaderLoader(source io.Reader) (*jsonIOLoader, io.Reader) {
  228. buf := &bytes.Buffer{}
  229. return &jsonIOLoader{buf: buf}, io.TeeReader(source, buf)
  230. }
  231. func NewWriterLoader(source io.Writer) (*jsonIOLoader, io.Writer) {
  232. buf := &bytes.Buffer{}
  233. return &jsonIOLoader{buf: buf}, io.MultiWriter(source, buf)
  234. }
  235. func (l *jsonIOLoader) JsonSource() interface{} {
  236. return l.buf.String()
  237. }
  238. func (l *jsonIOLoader) LoadJSON() (interface{}, error) {
  239. return decodeJsonUsingNumber(l.buf)
  240. }
  241. func (l *jsonIOLoader) JsonReference() (gojsonreference.JsonReference, error) {
  242. return gojsonreference.NewJsonReference("#")
  243. }
  244. func (l *jsonIOLoader) LoaderFactory() JSONLoaderFactory {
  245. return &DefaultJSONLoaderFactory{}
  246. }
  247. func decodeJsonUsingNumber(r io.Reader) (interface{}, error) {
  248. var document interface{}
  249. decoder := json.NewDecoder(r)
  250. decoder.UseNumber()
  251. err := decoder.Decode(&document)
  252. if err != nil {
  253. return nil, err
  254. }
  255. return document, nil
  256. }