A simple Go package for unmarshalling null-able JSON types
Find a file
dependabot[bot] 8f2c2ef0d4
Bump sonarsource/sonarqube-scan-action from 4.0.0 to 4.1.0
Bumps [sonarsource/sonarqube-scan-action](https://github.com/sonarsource/sonarqube-scan-action) from 4.0.0 to 4.1.0.
- [Release notes](https://github.com/sonarsource/sonarqube-scan-action/releases)
- [Commits](94d4f8ac4a...1b442ee39a)

---
updated-dependencies:
- dependency-name: sonarsource/sonarqube-scan-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-12-03 00:50:45 +00:00
.github Bump sonarsource/sonarqube-scan-action from 4.0.0 to 4.1.0 2024-12-03 00:50:45 +00:00
LICENSES Add CC0-1.0 and MIT license files 2024-09-01 16:49:27 +02:00
.gitignore Fix login method to avoid null pointer exceptions 2024-09-01 16:47:26 +02:00
.golangci.toml Initial checkin 2024-09-01 16:01:58 +02:00
go.mod Downgrade Go version to 1.18 2024-09-01 19:23:55 +02:00
LICENSE Initial checkin 2024-09-01 16:01:58 +02:00
niljson.go Fix typo in Omitted method name 2024-09-11 11:34:59 +02:00
niljson_test.go Add test for omitted field 2024-09-11 11:34:44 +02:00
README.md Fix typo and improve readability in README.md 2024-09-11 11:08:03 +02:00
SECURITY.md Add SECURITY.md for reporting vulnerabilities 2024-09-16 10:05:28 +02:00
sonar-project.properties Add SonarQube integration for code analysis 2024-09-11 10:19:00 +02:00

niljson - A simple Go package for (un-)marshalling null-able JSON types

GoDoc codecov Go Report Card REUSE status buy ma a coffee

niljson provides a simple and efficient way to handle nullable JSON fields during the (un-)marshalling process. In JSON, it's common to encounter fields that can be null, but handling these fields in Go can be cumbersome, especially when dealing with primitive types like int, float64, and bool. These types can all be either 0 (as a value) or null. In Go you can always work with pointers, but these can lead to unhandled nil pointer dereferences.

niljson addresses this challenge by offering a set of types that can seamlessly handle null values during unmarshalling, allowing your Go applications to work with JSON data more naturally and with fewer boilerplate checks for nil values.

Key Features

  • Nullable Types: Provides a range of nullable types (NilString, NilInt, NilFloat, NilBool, etc.) that are easy to use and integrate into your existing Go structs.
  • JSON Unmarshalling Support: Automatically handles the (un-)marshalling of JSON fields, converting null JSON values to Go's nil or zero values, depending on the context.
  • Minimalistic and Lightweight: Designed to be lightweight and unobtrusive, so it won't bloat your application or introduce unnecessary dependencies (only relies on the Go standard library)

Example Usage

package main

import (
    "encoding/json"
    "fmt"
    "os"
    
    "github.com/wneessen/niljson"
)

type JSONType struct {
    Bool       niljson.NilBoolean `json:"bool"`
    Float32    niljson.NilFloat32 `json:"float32,omitempty"`
    Float64    niljson.NilFloat64 `json:"float64"`
    Int        niljson.NilInt     `json:"int"`
    Int64      niljson.NilInt64   `json:"int64"`
    NullString niljson.NilString  `json:"nil"`
    String     niljson.NilString  `json:"string"`
}

func main() {
  data := []byte(`{
 		"bytes": "Ynl0ZXM=",
		"bool": true,
		"float32": null,
		"float64":0,
		"int": 123,
		"int64": 12345678901234,
		"nilvalue": null,
		"string":"test"
	}`)

  var example JSONType
  var output string
  if err := json.Unmarshal(data, &example); err != nil {
    fmt.Println("failed to unmarshal JSON:", err)
    os.Exit(1)
  }

  if example.Bool.NotNil() {
    output += fmt.Sprintf("Bool is: %t, ", example.Bool.Value())
  }
  if example.Float32.IsNil() {
    output += "Float 32 is nil, "
  }
  if example.Float64.NotNil() {
    output += fmt.Sprintf("Float 64 is: %f, ", example.Float64.Value())
  }
  if example.String.NotNil() {
    output += fmt.Sprintf("String is: %s", example.String.Value())
  }
  fmt.Println(output)
  
  data, err := json.Marshal(&example)
  if err != nil {
    fmt.Printf("failed to marshal JSON: %s", err)
    os.Exit(1)
  }
  fmt.Println(data)
}