Help us shape the future of Go! Take the 2026 Go Developer Survey

marshaler

package module
v0.0.0-...-4fc1bc6 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2021 License: MIT Imports: 2 Imported by: 0

README

URL Marshaler

⚠ THIS LIB HAS BEEN INTEGRATED INTO https://github.com/xescugc/marshaler USE IT INSTEAD ⚠

GoDoc

It's a simple type that implements the Marshaler and Unmarshaler interfaces for a URL transforming it to an string.

Installation

$> go get github.com/xescugc/url-marshaler

Usage

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

	marshaler "github.com/xescugc/url-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 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