RoaringBitmap / RoaringBitmap/roaring

Support for json encoding

Open
#204 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
2.9k
Forks
262
Avg merge
2h 34m
Merged PRs (30d)
8

Description

When *roaring.Bitmap is part of a struct that is serialized to JSON, it is empty {} and everything silently fails. Could this patch be included:

@@ -9,6 +9,7 @@ import (
 	"bufio"
 	"bytes"
 	"encoding/base64"
+	"encoding/json"
 	"fmt"
 	"io"
 	"strconv"
@@ -124,6 +125,14 @@ func (rb *Bitmap) MarshalBinary() ([]byte, error) {
 	return buf.Bytes(), nil
 }
 
+func (rb *Bitmap) MarshalJSON() ([]byte, error) {
+	b, err := rb.MarshalBinary()
+	if err != nil {
+		return nil, err
+	}
+	return json.Marshal(b)
+}
+
 // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface for the bitmap
 func (rb *Bitmap) UnmarshalBinary(data []byte) error {
 	var buf bytes.Buffer
@@ -136,6 +145,15 @@ func (rb *Bitmap) UnmarshalBinary(data []byte) error {
 	return err
 }
 
+func (rb *Bitmap) UnmarshalJSON(data []byte) error {
+	var b []byte
+	err := json.Unmarshal(data, &b)
+	if err != nil {
+		return err
+	}
+	return rb.UnmarshalBinary(b)
+}
+
 // NewBitmap creates a new empty Bitmap (see also New)
 func NewBitmap() *Bitmap {
 	return &Bitmap{}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Bitmap MarshalBinary and UnmarshalBinary methods shown in the issue and inspect how encoding/json handles a *roaring.Bitmap inside a struct. Verify JSON round-tripping for bitmap data and confirm that the proposed behavior prevents an empty object from being emitted; the issue does not name a test file.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.