jsonkit

package
v0.297.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

README

jsonkit

The jsonkit package enhances the convenience of working with JSON Data Transfer Objects (DTOs) in Go, particularly focusing on the use of standard library's json.Marshal and json.Unmarshal functions.

It does not replace these functions but offers tools to simplify their usage.

Key Features

  • Enhanced Array Handling: Facilitates the representation and manipulation of arrays containing elements of interface types, enabling easy conversion to and from JSON.
  • Compatibility with Standard Library: Designed to complement the Go standard library's JSON handling capabilities, making it easier to use with existing codebase and dtokit.
jsonkit.Interface[I]

jsonkit.Interface allow you to marshal an interface type, and then unmarshal them back with ease.

jsonkit.Array[T]

jsonkit.Array allow you to marshal any types, and then unmarshal them back.

jsonkit.Register[T]

Register allows you to register a type so that when it is used as an interface value type, it can be identified in a deterministic manner.

type MyDTO struct {
	V string `json:"v"`
}

var _ = jsonkit.Register[MyDTO]("my_dto")

Documentation

Index

Examples

Constants

View Source
const ErrNotInterfaceType errorkit.Error = "jsonkit.ErrNotInterfaceType"

Variables

This section is empty.

Functions

func Register

func Register[T any](id TypeID, aliases ...TypeID) func()
Example
package main

import (
	"go.llib.dev/frameless/pkg/jsonkit"
)

func main() {
	type MyDTO struct {
		V string `json:"v"`
	}
	var ( // register types
		_ = jsonkit.Register[MyDTO]("my_dto")
	)
}

Types

type Array

type Array[T any] []T
Example
package main

import (
	"encoding/json"

	"go.llib.dev/frameless/pkg/jsonkit"
)

func main() {
	var greeters = jsonkit.Array[Greeter]{
		TypeA{V: "42"},
		TypeB{V: 42},
	}

	data, err := json.Marshal(greeters)
	if err != nil {
		panic(err)
	}

	var result jsonkit.Array[Greeter]
	if err := json.Unmarshal(data, &result); err != nil {
		panic(err)
	}

	// "result" will contain the same as the "greeters".
}

type Greeter interface{ Hello() }

type TypeA struct{ V string }

func (TypeA) Hello() {}

type TypeB struct{ V int }

func (TypeB) Hello() {}

func (Array[T]) MarshalJSON

func (ary Array[T]) MarshalJSON() ([]byte, error)

func (*Array[T]) UnmarshalJSON

func (ary *Array[T]) UnmarshalJSON(data []byte) error

type Codec

type Codec struct{}

func (Codec) MakeListDecoder

func (s Codec) MakeListDecoder(r io.Reader) codec.ListDecoder

func (Codec) MakeListEncoder

func (s Codec) MakeListEncoder(w io.Writer) codec.ListEncoder

func (Codec) Marshal

func (s Codec) Marshal(v any) ([]byte, error)

func (Codec) Unmarshal

func (s Codec) Unmarshal(data []byte, dtoPtr any) error

type Interface

type Interface[I any] struct{ V I }
Example
package main

import (
	"encoding/json"

	"go.llib.dev/frameless/pkg/jsonkit"
)

func main() {
	var exp = jsonkit.Interface[Greeter]{
		V: &TypeC{V: 42.24},
	}

	data, err := json.Marshal(exp)
	if err != nil {
		panic(err)
	}
	// {"__type":"type_c","v":42.24}

	var got jsonkit.Interface[Greeter]
	if err := json.Unmarshal(data, &got); err != nil {
		panic(err)
	}

	// got == exp
	// got.V -> *TypeC{V: 42.24}
}

type Greeter interface{ Hello() }

type TypeC struct{ V float32 }

func (*TypeC) Hello() {}

func (Interface[I]) MarshalJSON

func (i Interface[I]) MarshalJSON() ([]byte, error)

func (*Interface[I]) UnmarshalJSON

func (i *Interface[I]) UnmarshalJSON(data []byte) error

type LinesCodec

type LinesCodec struct{}

LinesCodec is a json codec that uses the application/jsonlines

func (LinesCodec) MakeListEncoder

func (s LinesCodec) MakeListEncoder(w io.Writer) codec.ListEncoder

func (LinesCodec) Marshal

func (s LinesCodec) Marshal(v any) ([]byte, error)

func (LinesCodec) NewListDecoder

func (s LinesCodec) NewListDecoder(w io.ReadCloser) codec.ListDecoder

func (LinesCodec) Unmarshal

func (s LinesCodec) Unmarshal(data []byte, ptr any) error

type TypeID

type TypeID string

func (TypeID) IsZero

func (id TypeID) IsZero() bool

func (TypeID) String

func (id TypeID) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL