modulepackage
0.0.0-20250114201208-e81a63afd92c
Repository: https://github.com/markrosemaker/jsonutil.git
Documentation: pkg.go.dev
# README
JSON v2 Utilities
jsonutil
is a Go package that provides custom JSON v2 marshaling and unmarshaling functions for specific data types.
This package is particularly useful when you need to handle JSON encoding and decoding for types like time.Duration
and url.URL
in a customized manner.
Features
- Custom marshaler and unmarshaler for
url.URL
:URLMarshal
marshalsurl.URL
as a string.URLUnmarshal
unmarshalsurl.URL
from a string.
- Custom marshaler and unmarshaler for
time.Duration
:DurationMarshalIntSeconds
marshalstime.Duration
as an integer representing seconds.DurationUnmarshalIntSeconds
unmarshalstime.Duration
from an integer assuming it represents seconds.
Installation
To install the library, use the following command:
go get github.com/MarkRosemaker/jsonutil
Usage
Custom Marshaling and Unmarshaling for url.URL
To use the custom marshaler and unmarshaler for url.URL
, you can import the package and use the provided functions:
package main
import (
"fmt"
"net/url"
"github.com/MarkRosemaker/jsonutil"
"github.com/go-json-experiment/json"
)
var jsonOpts = json.JoinOptions(
json.WithMarshalers(json.NewMarshalers(json.MarshalFuncV2(jsonutil.URLMarshal))),
json.WithUnmarshalers(json.NewUnmarshalers(json.UnmarshalFuncV2(jsonutil.URLUnmarshal))),
)
type MyStruct struct {
Foo string `json:"foo"`
Link url.URL `json:"link"`
}
func main() {
out := &MyStruct{}
if err := json.Unmarshal([]byte(`{"foo":"bar","link":"https://example.com/"}`), out, jsonOpts); err != nil {
panic(err)
}
if out.Link.String() != "https://example.com/" {
panic("something went wrong")
}
res, err := json.Marshal(MyStruct{
Foo: "baz",
Link: url.URL{Scheme: "http", Host: "example.com", Path: "/some-path"},
}, jsonOpts)
if err != nil {
panic(err)
}
fmt.Println(string(res))
// Output: {"foo":"baz","link":"http://example.com/some-path"}
}
Custom Marshaling and Unmarshaling for time.Duration
To use the custom marshaler and unmarshaler for time.Duration
, you can import the package and use the provided functions:
package main
import (
"fmt"
"time"
"github.com/MarkRosemaker/jsonutil"
"github.com/go-json-experiment/json"
)
var jsonOpts = json.JoinOptions(
json.WithMarshalers(json.MarshalFuncV2(jsonutil.DurationMarshalIntSeconds)),
json.WithUnmarshalers(json.UnmarshalFuncV2(jsonutil.DurationUnmarshalIntSeconds)),
)
type MyStruct struct {
Foo string `json:"foo"`
Duration time.Duration `json:"duration"`
}
func main() {
out := &MyStruct{}
if err := json.Unmarshal([]byte(`{"foo":"bar","duration":3}`), out, jsonOpts); err != nil {
panic(err)
}
if out.Duration != 3*time.Second {
panic("something went wrong")
}
res, err := json.Marshal(MyStruct{
Foo: "baz",
Duration: time.Minute,
}, jsonOpts)
if err != nil {
panic(err)
}
fmt.Println(string(res))
// Output: {"foo":"baz","duration":60}
}
Contributing
If you have any contributions to make, please submit a pull request or open an issue on the GitHub repository.
License
This project is licensed under the MIT License. See the LICENSE file for details.
# Functions
DurationMarshalIntSeconds is a custom marshaler for time.Duration, marshaling them as integers representing seconds.
DurationUnmarshalIntSeconds is a custom unmarshaler for time.Duration, unmarshaling them from integers and assuming they represent seconds.
URLMarshal is a custom marshaler for URL values, marshaling them as strings.
URLUnmarshal is a custom unmarshaler for URL values, unmarshaling them from strings.