Skip to content

ctx42/jsontype

Repository files navigation

Go Report Card GoDoc Tests

jsontype is small Go module that preserves Go types when marshaling to JSON by embedding the type information in the JSON itself.

Marshaling and Unmarshaling

The workhorse of the module is the jsontype.Value type which implements json.Marshaler and json.Unmarshaler interfaces.

// Create a `jsontype.Value` and marshall it.
jType := jsontype.New(uint(42))
data, _ := json.Marshal(jType)
fmt.Printf("  marshalled: %s\n", string(data))

// Unmarshall the value and show its Go type.
gType := &jsontype.Value{}
_ = json.Unmarshal(data, gType)
fmt.Printf("unmarshalled: %[1]v (%[1]T)\n", gType.GoValue())

// Output:
//   marshalled: {"type":"uint","value":42}
// unmarshalled: 42 (uint)

The module supports also some of the builtin types.

// Create a `jsontype.Value` and marshall it.
jType := jsontype.New(time.Date(2000, 1, 2, 3, 4, 5, 600000000, time.UTC))
data, _ := json.Marshal(jType)
fmt.Printf("  marshalled: %s\n", string(data))

// Unmarshall the value and show its Go type.
gType := &jsontype.Value{}
_ = json.Unmarshal(data, gType)
fmt.Printf("unmarshalled: %[1]v (%[1]T)\n", gType.GoValue())

// Output:
// marshalled: {"type":"time.Time","value":"2000-01-02T03:04:05.6Z"}
// unmarshalled: 2000-01-02 03:04:05.6 +0000 UTC (time.Time)

Installation

To use jsontype in your Go project, install it with:

go get github.com/ctx42/jsontype

Validation

When unmarshaling jsontype checks if the value set in JSON is valid in context the Go type. It will return an error when the value is of invalid type, is in an invalid format, is in an invalid range for the Go type. Blow example shows what happens when you try unmarshal too big value to byte.

data := []byte(`{"type":"byte","value":99999}`)

gType := &jsontype.Value{}
err := json.Unmarshal(data, gType)

fmt.Println(err)
// Output:
// DecodeByte: requires float64 value in range of uint8: invalid range

Supported types

int
int8
int16
int32
rune
int64
uint
uint8
byte
uint16
uint32
uint64
float32
float64
string
bool
time.Time
time.Duration
complex64
complex128

About

A small Go module that preserves Go type information during JSON marshaling.

Topics

Resources

License

Stars

Watchers

Forks

Languages