Skip to content

Commit

Permalink
fix json.Marshaler interface for JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
SpencerTorres committed Jan 13, 2025
1 parent ce2e024 commit 5ee295b
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions lib/chcol/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
)

// JSON represents a ClickHouse JSON type that can hold multiple possible types
Expand Down Expand Up @@ -48,9 +49,33 @@ func (o *JSON) ValueAtPath(path string) (any, bool) {
return value, ok
}

// NestedMap converts the flattened JSON data into a nested structure
func (o *JSON) NestedMap() map[string]any {
nested := make(map[string]any)

for key, value := range o.valuesByPath {
parts := strings.Split(key, ".")
current := nested

for i := 0; i < len(parts)-1; i++ {
part := parts[i]

if _, exists := current[part]; !exists {
current[part] = make(map[string]any)
}

current = current[part].(map[string]any)
}

current[parts[len(parts)-1]] = value
}

return nested
}

// MarshalJSON implements the json.Marshaler interface
func (o *JSON) MarshalJSON() ([]byte, error) {
return json.Marshal(o.valuesByPath)
func (o JSON) MarshalJSON() ([]byte, error) {
return json.Marshal(o.NestedMap())
}

// Scan implements the sql.Scanner interface
Expand Down

0 comments on commit 5ee295b

Please sign in to comment.