jsonlib

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

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

Go to latest
Published: Jul 27, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

go-jsonlib

This is a small json library for my own projects, if you are interested in the same functionalities, grab it and help yourself. :)

Examples

Get JSON data

Returns a JSON object from a URL

package main

import (
	"log"

	"github.com/d3sw/jsonlib"
)

func main() {
	url := "https://api.github.com/users/octocat"
	var res struct {
		ID   int64  `json:"id"`
		Name string `json:"name"`
	}
	if err := jsonlib.GetJSON(url, nil, &res); err != nil {
		log.Fatal("failed to get json from url", err)
	}
	log.Println("Got user info.", "ID:", res.ID, "name:", res.Name)
}

Post JSON data

Posts a JSON object to an URL and returns a JSON response

package main

import (
	"log"

	"github.com/d3sw/jsonlib"
)

func main() {
	url := "https://api.github.com/users/octocat"
	req := struct {
		ID   int64  `json:"id"`
		Name string `json:"name"`
	}{1234, "name1234"}
	var res interface{}
	if err := jsonlib.PostJSON(url, nil, &req, &res); err != nil {
		log.Fatal("failed to post to server.", err)
	}
	log.Println("Post never succeed.")
}

Converts JSON to XML

A single API to convert json to xml

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/ys-zhao/jsonlib"
)

func main() {
	url := "https://api.github.com/users/octocat"
	res, err := http.Get(url)
	if err != nil {
		fmt.Fatalf("main: failed to get json from '%s'", url)
	}
	defer res.Body.Close()
	data, _ := ioutil.ReadAll(res.Body)
	xmlStr, _ := jsonlib.JSON2XML(string(data), jsonlib.J2XWithRootTag("root"), jsonlib.J2XWithIndent(true, "", "  "))
	fmt.Printf("main: json2xml from '%s'\n", url)
	fmt.Println(xmlStr)
}

Converts xml to json

A single API to convert json to xml

package main

import (
	"fmt"

	"github.com/ys-zhao/jsonlib"
)

var xmlStr = `<root><name>foo</name><age>21</age></root>`
func main() {
	jsonStr, _ := jsonlib.XML2JSON(xmlStr, jsonlib.X2JWithOmitRoot(false), jsonlib.X2JWithIndent(true, "", "  "))
	fmt.Println("main: json2xml with root node...")
	fmt.Println(jsonStr)

	jsonStr, _ = jsonlib.XML2JSON(xmlStr, jsonlib.X2JWithOmitRoot(true), jsonlib.X2JWithIndent(true, "", "  "))
	fmt.Println("main: json2xml without root node...")
	fmt.Println(jsonStr)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = New(nil)

Default the default json library

Functions

func DeleteJSON

func DeleteJSON(url string, headers map[string]string, req interface{}, res interface{}) error

DeleteJSON ...

func GetJSON

func GetJSON(url string, headers map[string]string, res interface{}) error

GetJSON ...

func JSON2XML

func JSON2XML(jsonStr string, opts ...J2XOption) (string, error)

JSON2XML ...

func ParseJSONRequest

func ParseJSONRequest(r *http.Request, res interface{}) error

ParseJSONRequest ...

func PostJSON

func PostJSON(url string, headers map[string]string, req interface{}, res interface{}) error

PostJSON ...

func PutJSON

func PutJSON(url string, headers map[string]string, req interface{}, res interface{}) error

PutJSON ...

func RequestJSON

func RequestJSON(method, url string, headers map[string]string, req interface{}, res interface{}) error

RequestJSON ...

func XML2JSON

func XML2JSON(xmlStr string, opts ...X2JOption) (string, error)

XML2JSON ...

Types

type Error

type Error struct {
	InternalError error
	Message       string

	URL     string
	Method  string
	Request []byte

	Status   int
	Response []byte
}

Error struct

func (*Error) Error

func (m *Error) Error() string

type J2XOption

type J2XOption func(*J2XOptions) error

J2XOption is a function on the options for a json2xml.

func J2XWithIndent

func J2XWithIndent(withIndent bool, prefix, indent string) J2XOption

J2XWithIndent ...

func J2XWithRootTag

func J2XWithRootTag(rootTag string) J2XOption

J2XWithRootTag ...

type J2XOptions

type J2XOptions struct {
	WithIndent bool   // well formatted json
	Prefix     string // the json marshal prefix
	Indent     string // the json marshal indent
	RootTag    string // the json root tag
}

J2XOptions ...

func DefaultJ2XOptions

func DefaultJ2XOptions() *J2XOptions

DefaultJ2XOptions ...

type JSONLibrary

type JSONLibrary interface {
	RequestJSON(method, url string, headers map[string]string, req interface{}, res interface{}) error
	GetJSON(url string, headers map[string]string, res interface{}) error
	PostJSON(url string, headers map[string]string, req interface{}, res interface{}) error
	PutJSON(url string, headers map[string]string, req interface{}, res interface{}) error
	DeleteJSON(url string, headers map[string]string, req interface{}, res interface{}) error
	ParseJSONRequest(r *http.Request, res interface{}) error
	JSON2XML(jsonStr string, opts ...J2XOption) (string, error)
	XML2JSON(xmlStr string, opts ...X2JOption) (string, error)
}

JSONLibrary json library interface

func New

func New(client *http.Client) JSONLibrary

New return a new json library

type X2JOption

type X2JOption func(*X2JOptions) error

X2JOption is a function on the options for a xml2json.

func X2JWithIndent

func X2JWithIndent(withIndent bool, prefix, indent string) X2JOption

X2JWithIndent ...

func X2JWithOmitRoot

func X2JWithOmitRoot(omitRoot bool) X2JOption

X2JWithOmitRoot ...

type X2JOptions

type X2JOptions struct {
	WithIndent bool   // well formatted json
	Prefix     string // the json marshal prefix
	Indent     string // the json marshal indent
	OmitRoot   bool   // omit the root element
}

X2JOptions ...

func DefaultX2JOptions

func DefaultX2JOptions() *X2JOptions

DefaultX2JOptions ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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