| 1 | package jsonscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.25 bytes/" |
| 5 | "errorsscip-go gomod github.com/golang/go/src go1.25 errors/" |
| 6 | "fmtscip-go gomod github.com/golang/go/src go1.25 fmt/" |
| 7 | jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/ "github.com/goccy/go-jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/" |
| 8 | "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/" |
| 9 | "github.com/tomwright/dasel/v3/parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/" |
| 10 | "stringsscip-go gomod github.com/golang/go/src go1.25 strings/" |
| 11 | ) |
| 12 | |
| 13 | // ErrJSONMaxDepthExceeded is returned when JSON nesting depth exceeds maxJSONDepth. |
| 14 | var ErrJSONMaxDepthExceededscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/ErrJSONMaxDepthExceeded. = errorsscip-go gomod github.com/golang/go/src go1.25 errors/.Newscip-go gomod github.com/golang/go/src go1.25 errors/New().("json nesting depth exceeded") |
| 15 | |
| 16 | const maxJSONDepthscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/maxJSONDepth. = 10_000 |
| 17 | |
| 18 | var _ parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Readerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader# = (*jsonReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#)(nil) |
| 19 | |
| 20 | func newJSONReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/newJSONReader().(options parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.ReaderOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/ReaderOptions#) (parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Readerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#, error) { |
| 21 | return &jsonReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#{}, nil |
| 22 | } |
| 23 | |
| 24 | type jsonReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader# struct{} |
| 25 | |
| 26 | // Read reads a value from a byte slice. |
| 27 | // When the input contains multiple JSON values (NDJSON), they are returned |
| 28 | // as a branch-marked slice, mirroring the YAML multi-document behaviour. |
| 29 | func (j *jsonReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#) Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#Read().(data []byte) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 30 | decoder := jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/.NewDecoderscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/NewDecoder().(bytesscip-go gomod github.com/golang/go/src go1.25 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.25 bytes/NewReader().(data)) |
| 31 | decoder.UseNumberscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Decoder#UseNumber().() |
| 32 | |
| 33 | var results []*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value# |
| 34 | |
| 35 | for decoder.Morescip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Decoder#More().() { |
| 36 | v, err := j.decodeValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeValue().(decoder, 0) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | results = append(results, v) |
| 41 | } |
| 42 | |
| 43 | switch len(results) { |
| 44 | case 0: |
| 45 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewNullValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewNullValue().(), nil |
| 46 | case 1: |
| 47 | return results[0], nil |
| 48 | default: |
| 49 | slice := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().() |
| 50 | slice.MarkAsBranchscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MarkAsBranch().() |
| 51 | for _, v := range results { |
| 52 | if err := slice.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(v); err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | } |
| 56 | return slice, nil |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func (j *jsonReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#) decodeValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeValue().(decoder *jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/.Decoderscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Decoder#, depth int) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 61 | t, err := decoder.Tokenscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Decoder#Token().() |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | switch t { |
| 67 | case jsonOpenObjectscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonOpenObject.: |
| 68 | res, err := j.decodeObjectscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeObject().(decoder, depth+1) |
| 69 | if err != nil { |
| 70 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("could not decode object: %w", err) |
| 71 | } |
| 72 | return res, nil |
| 73 | case jsonOpenArrayscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonOpenArray.: |
| 74 | res, err := j.decodeArrayscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeArray().(decoder, depth+1) |
| 75 | if err != nil { |
| 76 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("could not decode array: %w", err) |
| 77 | } |
| 78 | return res, nil |
| 79 | default: |
| 80 | return j.decodeTokenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeToken().(decoder, t) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func (j *jsonReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#) decodeObjectscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeObject().(decoder *jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/.Decoderscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Decoder#, depth int) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 85 | if depth > maxJSONDepthscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/maxJSONDepth. { |
| 86 | return nil, ErrJSONMaxDepthExceededscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/ErrJSONMaxDepthExceeded. |
| 87 | } |
| 88 | |
| 89 | res := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewMapValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewMapValue().() |
| 90 | |
| 91 | var key any = nil |
| 92 | |
| 93 | for { |
| 94 | t, err := decoder.Tokenscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Decoder#Token().() |
| 95 | if err != nil { |
| 96 | // We don't expect an EOF here since we're in the middle of processing an object. |
| 97 | return res, err |
| 98 | } |
| 99 | |
| 100 | switch t { |
| 101 | case jsonOpenArrayscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonOpenArray.: |
| 102 | if key == nil { |
| 103 | return res, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("unexpected token: %v", t) |
| 104 | } |
| 105 | value, err := j.decodeArrayscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeArray().(decoder, depth+1) |
| 106 | if err != nil { |
| 107 | return res, err |
| 108 | } |
| 109 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().(key.(string), value); err != nil { |
| 110 | return res, err |
| 111 | } |
| 112 | key = nil |
| 113 | case jsonCloseArrayscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonCloseArray.: |
| 114 | return res, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("unexpected token: %v", t) |
| 115 | case jsonCloseObjectscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonCloseObject.: |
| 116 | return res, nil |
| 117 | case jsonOpenObjectscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonOpenObject.: |
| 118 | if key == nil { |
| 119 | return res, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("unexpected token: %v", t) |
| 120 | } |
| 121 | value, err := j.decodeObjectscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeObject().(decoder, depth+1) |
| 122 | if err != nil { |
| 123 | return res, err |
| 124 | } |
| 125 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().(key.(string), value); err != nil { |
| 126 | return res, err |
| 127 | } |
| 128 | key = nil |
| 129 | default: |
| 130 | if key == nil { |
| 131 | if tStr, ok := t.(string); ok { |
| 132 | key = tStr |
| 133 | } else { |
| 134 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("unexpected token: %v", t) |
| 135 | } |
| 136 | } else { |
| 137 | value, err := j.decodeTokenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeToken().(decoder, t) |
| 138 | if err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().(key.(string), value); err != nil { |
| 142 | return res, err |
| 143 | } |
| 144 | key = nil |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func (j *jsonReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#) decodeArrayscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeArray().(decoder *jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/.Decoderscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Decoder#, depth int) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 151 | if depth > maxJSONDepthscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/maxJSONDepth. { |
| 152 | return nil, ErrJSONMaxDepthExceededscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/ErrJSONMaxDepthExceeded. |
| 153 | } |
| 154 | |
| 155 | res := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().() |
| 156 | for { |
| 157 | t, err := decoder.Tokenscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Decoder#Token().() |
| 158 | if err != nil { |
| 159 | // We don't expect an EOF here since we're in the middle of processing an object. |
| 160 | return res, err |
| 161 | } |
| 162 | |
| 163 | switch t { |
| 164 | case jsonOpenArrayscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonOpenArray.: |
| 165 | value, err := j.decodeArrayscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeArray().(decoder, depth+1) |
| 166 | if err != nil { |
| 167 | return res, err |
| 168 | } |
| 169 | if err := res.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(value); err != nil { |
| 170 | return res, err |
| 171 | } |
| 172 | case jsonCloseArrayscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonCloseArray.: |
| 173 | return res, nil |
| 174 | case jsonCloseObjectscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonCloseObject.: |
| 175 | return res, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("unexpected token: %t", t) |
| 176 | case jsonOpenObjectscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonOpenObject.: |
| 177 | value, err := j.decodeObjectscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeObject().(decoder, depth+1) |
| 178 | if err != nil { |
| 179 | return res, err |
| 180 | } |
| 181 | if err := res.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(value); err != nil { |
| 182 | return res, err |
| 183 | } |
| 184 | default: |
| 185 | value, err := j.decodeTokenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeToken().(decoder, t) |
| 186 | if err != nil { |
| 187 | return nil, err |
| 188 | } |
| 189 | if err := res.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(value); err != nil { |
| 190 | return res, err |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | func (j *jsonReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#) decodeTokenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/jsonReader#decodeToken().(decoder *jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/.Decoderscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Decoder#, t jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/.Tokenscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Token#) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 197 | switch tv := t.(type) { |
| 198 | case jsonscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/.Numberscip-go gomod github.com/goccy/go-json v0.10.6 `github.com/goccy/go-json`/Number#: |
| 199 | strNum := tv.Stringscip-go gomod github.com/golang/go/src go1.25 `encoding/json`/Number#String().() |
| 200 | if stringsscip-go gomod github.com/golang/go/src go1.25 strings/.Containsscip-go gomod github.com/golang/go/src go1.25 strings/Contains().(strNum, ".") { |
| 201 | floatNum, err := tv.Float64scip-go gomod github.com/golang/go/src go1.25 `encoding/json`/Number#Float64().() |
| 202 | if err == nil { |
| 203 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewFloatValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewFloatValue().(floatNum), nil |
| 204 | } |
| 205 | return nil, err |
| 206 | } |
| 207 | intNum, err := tv.Int64scip-go gomod github.com/golang/go/src go1.25 `encoding/json`/Number#Int64().() |
| 208 | if err == nil { |
| 209 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewIntValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewIntValue().(intNum), nil |
| 210 | } |
| 211 | |
| 212 | return nil, err |
| 213 | default: |
| 214 | return modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewValue().(tv), nil |
| 215 | } |
| 216 | } |