MCPcopy Create free account
hub / github.com/tendermint/tendermint / UnmarshalJSON

Method UnmarshalJSON

libs/bits/bit_array.go:397–424  ·  view source on GitHub ↗

UnmarshalJSON implements json.Unmarshaler interface by unmarshaling a custom JSON description.

(bz []byte)

Source from the content-addressed store, hash-verified

395// UnmarshalJSON implements json.Unmarshaler interface by unmarshaling a custom
396// JSON description.
397func (bA *BitArray) UnmarshalJSON(bz []byte) error {
398 b := string(bz)
399 if b == "null" {
400 // This is required e.g. for encoding/json when decoding
401 // into a pointer with pre-allocated BitArray.
402 bA.Bits = 0
403 bA.Elems = nil
404 return nil
405 }
406
407 // Validate 'b'.
408 match := bitArrayJSONRegexp.FindStringSubmatch(b)
409 if match == nil {
410 return fmt.Errorf("bitArray in JSON should be a string of format %q but got %s", bitArrayJSONRegexp.String(), b)
411 }
412 bits := match[1]
413
414 // Construct new BitArray and copy over.
415 numBits := len(bits)
416 bA2 := NewBitArray(numBits)
417 for i := 0; i < numBits; i++ {
418 if bits[i] == 'x' {
419 bA2.SetIndex(i, true)
420 }
421 }
422 *bA = *bA2 //nolint:govet
423 return nil
424}
425
426// ToProto converts BitArray to protobuf. It returns nil if BitArray is
427// nil/empty.

Callers

nothing calls this directly

Calls 3

SetIndexMethod · 0.95
NewBitArrayFunction · 0.85
StringMethod · 0.65

Tested by

no test coverage detected