]> 127.0.0.1 Git - part/.git/commitdiff
54
authorqydysky <qydysky@foxmail.com>
Sat, 5 Sep 2020 04:01:23 +0000 (12:01 +0800)
committerqydysky <qydysky@foxmail.com>
Sat, 5 Sep 2020 04:01:23 +0000 (12:01 +0800)
Json.go

diff --git a/Json.go b/Json.go
index 41c92cde66664f5248178dd2c1dfbbf3bcbf7c1e..196d0b686f60e06e56ba26ead7173af9bf9a3942 100644 (file)
--- a/Json.go
+++ b/Json.go
@@ -1,6 +1,11 @@
 package part
 
 import (
+       "io"
+       "os"
+       "errors"
+       "strings"
+       goej "encoding/json"
        "github.com/thedevsaddam/gojsonq/v2"
 )
 
@@ -8,6 +13,37 @@ type json struct {}
 
 func Json() (*json) {return &json{}}
 
+func (*json) Check(Source interface{}) error {
+       var jq *goej.Decoder
+       switch Source.(type) {
+    case string:
+               if Checkfile().IsExist(Source.(string)) {
+                       fileObj,err := os.OpenFile(Source.(string),os.O_RDONLY,0644)
+                       if err != nil {
+                               return err
+                       }
+                       defer fileObj.Close()
+                       jq = goej.NewDecoder(fileObj)
+               }else{
+                       jq = goej.NewDecoder(strings.NewReader(Source.(string)))
+               }
+       default:
+        return errors.New("json type != string")
+       }
+       
+       for {
+               _, err := jq.Token()
+               if err == io.EOF {
+                       break
+               }
+               if err != nil {
+                       return err
+               }
+       }
+
+       return nil
+}
+
 func (*json) GetValFrom(Source interface{},key string)interface {}{
        var jq *gojsonq.JSONQ
        switch Source.(type) {