-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader_json.go
63 lines (49 loc) · 1.45 KB
/
loader_json.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package openapi
import (
"io"
"unicode"
"github.com/go-json-experiment/json"
)
// LoadFromReaderJSON reads an OpenAPI specification in JSON format from an io.Reader and parses it into a structured format.
func (l *loader) LoadFromReaderJSON(r io.Reader) (*Document, error) {
l.reset()
doc := &Document{}
if err := json.UnmarshalRead(r, doc, jsonOpts); err != nil {
return nil, err
}
if err := l.collectResolveRefs(doc); err != nil {
return nil, err
}
return doc, doc.Validate()
}
// LoadFromDataJSON reads an OpenAPI specification from a byte array in JSON format and parses it into a structured format.
func LoadFromDataJSON(data []byte) (*Document, error) {
return newLoader().LoadFromDataJSON(data)
}
// LoadFromDataJSON reads an OpenAPI specification from a byte array in JSON format and parses it into a structured format.
func (l *loader) LoadFromDataJSON(data []byte) (*Document, error) {
l.reset()
doc := &Document{}
if err := json.Unmarshal(data, doc, jsonOpts); err != nil {
return nil, err
}
if err := l.collectResolveRefs(doc); err != nil {
return nil, err
}
return doc, doc.Validate()
}
// isJSONRead checks if the data in the reader is JSON
// NOTE: this is a somewhat naive check, but it should work for most cases
func isJSONRead(r io.Reader) (bool, error) {
for {
var b [1]byte
_, err := r.Read(b[:])
if err != nil {
return false, err
}
if unicode.IsSpace(rune(b[0])) {
continue
}
return b[0] == '{', nil
}
}