jsondto

package
v0.180.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

jsondto

The jsondto 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 DTOs.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

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

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

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

Types

type Array

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

import (
	"encoding/json"
	"go.llib.dev/frameless/pkg/jsondto"
)

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

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

	var result jsondto.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 TypeID

type TypeID string

type Typed added in v0.180.0

type Typed[T any] struct{ V T }
Example
package main

import ()

func main() {

}

func (Typed[T]) MarshalJSON added in v0.180.0

func (v Typed[T]) MarshalJSON() ([]byte, error)

func (*Typed[T]) UnmarshalJSON added in v0.180.0

func (v *Typed[T]) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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