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