github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/encoding/kmgJson/kmgJson.go (about)

     1  package kmgJson
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"github.com/bronze1man/kmg/kmgFile"
     7  	"github.com/bronze1man/kmg/typeTransform"
     8  	"io/ioutil"
     9  	"os"
    10  	"strings"
    11  )
    12  
    13  func ReadFile(path string, obj interface{}) error {
    14  	b, err := ioutil.ReadFile(path)
    15  	if err != nil {
    16  		return err
    17  	}
    18  	return json.Unmarshal(b, obj)
    19  }
    20  
    21  func MustReadFile(path string, obj interface{}) {
    22  	err := ReadFile(path, obj)
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  }
    27  
    28  func MustWriteFileIndent(path string, obj interface{}) {
    29  	kmgFile.MustMkdirForFile(path)
    30  	output, err := json.MarshalIndent(obj, "", "  ")
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	err = ioutil.WriteFile(path, output, os.FileMode(0777))
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  }
    39  
    40  //读取json文件,并修正json的类型问题(map key 必须是string的问题)
    41  func ReadFileTypeFix(path string, obj interface{}) error {
    42  	b, err := ioutil.ReadFile(path)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	var iobj interface{}
    47  	err = json.Unmarshal(b, &iobj)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	return typeTransform.Transform(iobj, obj)
    52  }
    53  
    54  func WriteFile(path string, obj interface{}) (err error) {
    55  	out, err := json.Marshal(obj)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	return ioutil.WriteFile(path, out, os.FileMode(0777))
    60  }
    61  
    62  //写入json文件,并修正json的类型问题(map key 必须是string的问题)
    63  func WriteFileTypeFix(path string, obj interface{}) (err error) {
    64  	//a simple work around
    65  	obj, err = TypeFixWhenMarshal(obj)
    66  	if err != nil {
    67  		return
    68  	}
    69  	outByte, err := json.Marshal(obj)
    70  	if err != nil {
    71  		return
    72  	}
    73  	return ioutil.WriteFile(path, outByte, os.FileMode(0777))
    74  }
    75  
    76  func UnmarshalNoType(r []byte) (interface{}, error) {
    77  	var obj interface{}
    78  	err := json.Unmarshal(r, &obj)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	return obj, nil
    83  }
    84  
    85  func MustUnmarshal(r []byte, obj interface{}) {
    86  	err := json.Unmarshal(r, &obj)
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  	return
    91  }
    92  
    93  func MustUnmarshalIgnoreEmptyString(jsonStr string, obj interface{}) {
    94  	if jsonStr == "" {
    95  		return
    96  	}
    97  	err := json.Unmarshal([]byte(jsonStr), &obj)
    98  	if err != nil {
    99  		panic(err)
   100  	}
   101  	return
   102  }
   103  
   104  func MustUnmarshalToMap(r []byte) (obj map[string]interface{}) {
   105  	err := json.Unmarshal(r, &obj)
   106  	if err != nil {
   107  		panic(err)
   108  	}
   109  	return obj
   110  }
   111  
   112  func MustUnmarshalToMapDeleteBOM(r []byte) (obj map[string]interface{}) {
   113  	r = DeleteBOM(r)
   114  	err := json.Unmarshal(r, &obj)
   115  	if err != nil {
   116  		panic(err)
   117  	}
   118  	return obj
   119  }
   120  
   121  // for debug to inspect content in obj
   122  func MustMarshalIndentToString(obj interface{}) string {
   123  	output, err := json.MarshalIndent(obj, "", "  ")
   124  	if err != nil {
   125  		panic(err)
   126  	}
   127  	return string(output)
   128  }
   129  
   130  var htmlUnescapeReplacer = strings.NewReplacer(`\u003c`, "<", `\u003e`, ">", `\u0026`, "&")
   131  
   132  func MarshalIndent(obj interface{}) ([]byte, error) {
   133  	b, err := json.MarshalIndent(obj, "", "  ")
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	return []byte(htmlUnescapeReplacer.Replace(string(b))), nil
   138  }
   139  
   140  func MustMarshal(obj interface{}) []byte {
   141  	output, err := json.Marshal(obj)
   142  	if err != nil {
   143  		panic(err)
   144  	}
   145  	return output
   146  }
   147  
   148  func MustMarshalToString(obj interface{}) string {
   149  	output, err := json.Marshal(obj)
   150  	if err != nil {
   151  		panic(err)
   152  	}
   153  	return string(output)
   154  }
   155  
   156  func DeleteBOM(fileBytes []byte) []byte {
   157  	trimmedBytes := bytes.Trim(fileBytes, "\xef\xbb\xbf")
   158  	return trimmedBytes
   159  }