SFJson

package
v0.0.0-...-21e6218 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2017 License: BSD-3-Clause Imports: 13 Imported by: 4

Documentation

Overview

json 组件封装,基于encoding/json

Index

Constants

This section is empty.

Variables

View Source
var (
	// json data type
	// TypeInt          = DataType(1) TODO golang json parse no int type
	TypeFloat  = DataType(2)
	TypeBool   = DataType(3)
	TypeString = DataType(4)
	TypeArray  = DataType(5)
	TypeMap    = DataType(6)

	// json not nil data type
	TypeNotnilFloat  = DataTypeNotnil(2)
	TypeNotnilBool   = DataTypeNotnil(3)
	TypeNotnilString = DataTypeNotnil(4)
	TypeNotnilArray  = DataTypeNotnil(5)
	TypeNotnilMap    = DataTypeNotnil(6)
)

Functions

func Marshal

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

*

  • parse json bytes data *
  • @param `v` handle object struct or map or slice
  • @param `nullTag` nil tag, field or data is nil replace
  • e.g.:
  • default "" - 如果空字符串不会将(空数据)字段或key添加到json解析的数据中
  • "null" - 空数据解析: {"Key":null}
  • "nil" - 空数据解析: {"Key":nil}
  • @param `boolTag` bool tag
  • e.g.:
  • default "" - {"KeyBool":trye|false}
  • "YES|NO" - {"KeyBool":YES|NO}
  • "1|0" - {"KeyBool":1|0}
  • @return []byte
  • @return error

func Unmarshal

func Unmarshal(jsonByte []byte, v interface{}) error

*

  • json bytes to struct or slice or map *
  • @param `jsonByte`
  • @param `v` pointer object

func ValidateArray

func ValidateArray(jsonArray interface{}, validDatas []interface{}) bool

*

  • validate array type data
  • can nesting validate *
  • `validData` :
  • [
  • TypeInt,
  • TypeNotnilString,
  • map[string]interface{}{
  • childField1: TypeNotnilString
  • childField2: TypeFloat
  • },
  • []interface{}{
  • TypeString,
  • TypeInt,
  • TypeNotnilString
  • }
  • ] *
  • @param `jsonArray` json array struct
  • @param `validData` valid data validate
  • @return valid data is true

func ValidateMap

func ValidateMap(jsonMap interface{}, validData map[string]interface{}) bool

*

  • validate map type data
  • can nesting validate *
  • `validData` :
  • {
  • Field1:TypeInt,
  • Field2:TypeNotnilString,
  • Field3:map[string]interface{}{
  • childField1: TypeNotnilString
  • childField2: TypeFloat
  • },
  • Field4:[]interface{}{
  • TypeString,
  • TypeInt,
  • TypeNotnilString
  • }
  • } *
  • @param `jsonMap` json map struct
  • @param `validData` valid data validate
  • @return valid data is true

Types

type DataType

type DataType int8

*

  • json struct data type *
  • value type is int|float|string|array|map

type DataTypeNotnil

type DataTypeNotnil int8

*

  • json struct data not nil type *
  • value is not null

type Json

type Json struct {
	// contains filtered or unexported fields
}

*

  • Json struct object

func NewJson

func NewJson(v interface{}, nullTag, boolTag string) (Json, error)

*

  • parse interface data new Json object *
  • @param `v` handle object struct or map or slice
  • @param `nullTag` nil tag, field or data is nil replace
  • e.g.:
  • default "" - 如果空字符串不会将(空数据)字段或key添加到json解析的数据中
  • "null" - 空数据解析: {"Key":null}
  • "nil" - 空数据解析: {"Key":nil}
  • @param `boolTag` bool tag
  • e.g.:
  • default "" - {"KeyBool":trye|false}
  • "YES|NO" - {"KeyBool":YES|NO}
  • "1|0" - {"KeyBool":1|0}
  • @return Json
  • @return error

func NewJsonNil

func NewJsonNil(isArray bool) Json

*

  • new nil Json struct object *
  • @param isArray YES nil info = [], NO nil info = {} *

func (*Json) Bytes

func (j *Json) Bytes() []byte

*

  • to json bytes *
  • @return

func (*Json) BytesFormat

func (j *Json) BytesFormat() []byte

*

  • to json format bytes *
  • @return

func (*Json) RawData

func (j *Json) RawData() interface{}

*

  • get json raw interface data *
  • @return

func (*Json) String

func (j *Json) String() string

*

  • to json string *
  • @return

func (*Json) WriteFilepath

func (j *Json) WriteFilepath(path string, format bool) error

*

*
*	@param `path`	output file path
*	@param `format` whether format output

Jump to

Keyboard shortcuts

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