MarshalJSON implements json.Marshaler interface by marshaling bit array using a custom format: a string of '-' or 'x' where 'x' denotes the 1 bit.
()
| 366 | // MarshalJSON implements json.Marshaler interface by marshaling bit array |
| 367 | // using a custom format: a string of '-' or 'x' where 'x' denotes the 1 bit. |
| 368 | func (bA *BitArray) MarshalJSON() ([]byte, error) { |
| 369 | if bA == nil { |
| 370 | return []byte("null"), nil |
| 371 | } |
| 372 | |
| 373 | bA.mtx.Lock() |
| 374 | defer bA.mtx.Unlock() |
| 375 | |
| 376 | bits := `"` |
| 377 | for i := 0; i < bA.Bits; i++ { |
| 378 | if bA.getIndex(i) { |
| 379 | bits += `x` |
| 380 | } else { |
| 381 | bits += `_` |
| 382 | } |
| 383 | } |
| 384 | bits += `"` |
| 385 | return []byte(bits), nil |
| 386 | } |
| 387 | |
| 388 | var bitArrayJSONRegexp = regexp.MustCompile(`\A"([_x]*)"\z`) |
| 389 |