shallowDecode decodes only the keys of an object, and doesn't decode any elements of an array. It can be used to save a decode-encode cycle for certain operations (say, key deletion).
()
| 454 | // elements of an array. It can be used to save a decode-encode cycle for |
| 455 | // certain operations (say, key deletion). |
| 456 | func (j *jsonEncoded) shallowDecode() (JSON, error) { |
| 457 | if dec := j.alreadyDecoded(); dec != nil { |
| 458 | return dec, nil |
| 459 | } |
| 460 | |
| 461 | switch j.typ { |
| 462 | case NumberJSONType, StringJSONType, TrueJSONType, FalseJSONType, NullJSONType: |
| 463 | return j.decode() |
| 464 | case ArrayJSONType: |
| 465 | iter := j.iterArrayValues() |
| 466 | result := make(jsonArray, j.containerLen) |
| 467 | for i := 0; i < j.containerLen; i++ { |
| 468 | entry, next, _, err := iter.nextEncoded() |
| 469 | if err != nil { |
| 470 | return nil, err |
| 471 | } |
| 472 | result[i], err = newEncoded(entry, next) |
| 473 | if err != nil { |
| 474 | return nil, err |
| 475 | } |
| 476 | } |
| 477 | return result, nil |
| 478 | case ObjectJSONType: |
| 479 | iter, err := j.iterObject() |
| 480 | if err != nil { |
| 481 | return nil, err |
| 482 | } |
| 483 | result := make(jsonObject, j.containerLen) |
| 484 | for i := 0; i < j.containerLen; i++ { |
| 485 | nextKey, entry, nextValue, _, err := iter.nextEncoded() |
| 486 | if err != nil { |
| 487 | return nil, err |
| 488 | } |
| 489 | v, err := newEncoded(entry, nextValue) |
| 490 | if err != nil { |
| 491 | return nil, err |
| 492 | } |
| 493 | result[i] = jsonKeyValuePair{ |
| 494 | k: jsonString(nextKey), |
| 495 | v: v, |
| 496 | } |
| 497 | } |
| 498 | j.mu.Lock() |
| 499 | defer j.mu.Unlock() |
| 500 | if j.mu.cachedDecoded == nil { |
| 501 | j.mu.cachedDecoded = result |
| 502 | } |
| 503 | return result, nil |
| 504 | default: |
| 505 | return nil, errors.AssertionFailedf("unknown json type: %v", errors.Safe(j.typ)) |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | func (j *jsonEncoded) mustDecode() JSON { |
| 510 | decoded, err := j.shallowDecode() |
no test coverage detected