marshaler

package module
v0.0.0-...-e29dc40 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2021 License: MIT Imports: 3 Imported by: 2

README

Marshaler

Adds JSON (for now) Marshal and Unmarshal to different standard GO types

Installation

$> go get github.com/xescugc/marshaler

Usage

It supports 2 types:

Example

import (
	"encoding/json"
	"fmt"
	"net/url"

	"github.com/xescugc/marshaler"
)

type User struct {
  Name  string        `json:"name"`
  URL   marshaler.URL `json:"url"`
}

func main() {
  u, _ := url.Parse("http://example.com")
  usr := User{
    Name: "Pepito",
    URL:  marshaler.URL{
      URL: u,
    },
  }

  b, _ := json.Marshal(usr)

  fmt.Println(string(b))
  // { "name": "Pepito", "url": "http://example.com" }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CurrencyUnit

type CurrencyUnit struct {
	currency.Unit
}

URL extends the currency.Unit to add implementations to the Marshaler and Unmarshaler interfaces

func NewCurrencyUnit

func NewCurrencyUnit(u currency.Unit) CurrencyUnit

NewCurrencyUnit returns a new marshaler.CurrencyUnit from u

func (CurrencyUnit) MarshalJSON

func (u CurrencyUnit) MarshalJSON() ([]byte, error)

MarshalJSON transforms the CurrencyUnit to a String if it's not defined it'll use the default one which is currency.XXX

func (*CurrencyUnit) UnmarshalJSON

func (u *CurrencyUnit) UnmarshalJSON(b []byte) error

UnmarshalJSON transforms the b to a string

type URL

type URL struct {
	*url.URL
}

URL extends the url.URL to add implementations to the Marshaler and Unmarshaler interfaces

func NewURL

func NewURL(u *url.URL) URL

NewURL returns a new marshaler.URL from u

func (URL) MarshalJSON

func (u URL) MarshalJSON() ([]byte, error)

MarshalJSON transforms the url to a String

func (*URL) UnmarshalJSON

func (u *URL) UnmarshalJSON(b []byte) error

UnmarshalJSON transforms the b to a string

Jump to

Keyboard shortcuts

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