nojson

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

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

Go to latest
Published: May 10, 2017 License: MIT Imports: 14 Imported by: 1

README

nojson

A Simple Golang library created in order to escape some Struct Fields easily while marshaling a custom struct object or an array of custom struct objects

Why I have created this library

While I was developing a REST API server I needed to hide some struct field values from the client if he was not allowed to see that part of the data. I was using an ORM library so I needed an easy way to create a filtered json response.

Installation

go get github.com/Maaarcocr/nojson

Example

import (
	"fmt"
	
	"github.com/Maaarcocr/nojson"
)

type Post struct{
	User string
	Likes int `nojson:"user"`
	Comments []string `nojson:"user;mod"`
}

func main(){
	examplePost := Post{"Marco", 5, []string{"good", "i like it"}}
	resultForUser, err1 := nojson.MarshalAndFilterBy("user", examplePost)
	resultForMod, err2 := nojson.MarshalAndFilterBy("mod", examplePost)
	if err1 == nil {
		fmt.Println(string(resultForUser))
	}
	if err2 == nil {
		fmt.Println(string(resultForMod))
	}
}

User Result:

{"User": "Marco"}

Mod Result:

{"Likes": 5, "User": "Marco"}

N.B.

  1. The function MarshalAndFilterBy accept struct, slice and array as types. (If you use different types it won't complain at compile time, but you will get nil as a result)
  2. The name used in the JSON result will be the Field Name (so there is no point in using the json tag to set a different name)
  3. The function MarshalAndFilterBy has []byte as return type.
  4. As you can see in the example if you want to add more than one tag you should use this style: tag1;tag2;...;tag100.
  5. There is no limit in the amount of tag that you can use.

P.S.

You can find other examples in the example folder

Documentation

Overview

Package json implements encoding and decoding of JSON as defined in RFC 4627. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions.

See "JSON and Go" for an introduction to this package: https://golang.org/doc/articles/json_and_go.html

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}, tag string) ([]byte, error)

Types

type InvalidUTF8Error

type InvalidUTF8Error struct {
	S string // the whole string value that caused the error
}

Before Go 1.2, an InvalidUTF8Error was returned by Marshal when attempting to encode a string value with invalid UTF-8 sequences. As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by replacing invalid bytes with the Unicode replacement rune U+FFFD. This error is no longer generated but is kept for backwards compatibility with programs that might mention it.

func (*InvalidUTF8Error) Error

func (e *InvalidUTF8Error) Error() string

type Marshaler

type Marshaler interface {
	MarshalJSON() ([]byte, error)
}

Marshaler is the interface implemented by types that can marshal themselves into valid JSON.

type MarshalerError

type MarshalerError struct {
	Type reflect.Type
	Err  error
}

func (*MarshalerError) Error

func (e *MarshalerError) Error() string

type Number

type Number string

func (Number) Float64

func (n Number) Float64() (float64, error)

Float64 returns the number as a float64.

func (Number) Int64

func (n Number) Int64() (int64, error)

Int64 returns the number as an int64.

func (Number) String

func (n Number) String() string

String returns the literal text of the number.

type SyntaxError

type SyntaxError struct {
	Offset int64 // error occurred after reading Offset bytes
	// contains filtered or unexported fields
}

A SyntaxError is a description of a JSON syntax error.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

type UnsupportedValueError

type UnsupportedValueError struct {
	Value reflect.Value
	Str   string
}

func (*UnsupportedValueError) Error

func (e *UnsupportedValueError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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