github.com/iDigitalFlame/xmt@v0.5.4/cmd/filter/x_json.go (about) 1 //go:build !nojson 2 // +build !nojson 3 4 // Copyright (C) 2020 - 2023 iDigitalFlame 5 // 6 // This program is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with this program. If not, see <https://www.gnu.org/licenses/>. 18 // 19 20 package filter 21 22 import "encoding/json" 23 24 // MarshalJSON will attempt to convert the data in this Filter into the returned 25 // JSON byte array. 26 func (f Filter) MarshalJSON() ([]byte, error) { 27 m := map[string]interface{}{"fallback": f.Fallback} 28 if f.PID != 0 { 29 m["pid"] = f.PID 30 } 31 if f.Session > Empty { 32 m["session"] = f.Session 33 } 34 if f.Elevated > Empty { 35 m["elevated"] = f.Elevated 36 } 37 if len(f.Exclude) > 0 { 38 m["exclude"] = f.Elevated 39 } 40 if len(f.Include) > 0 { 41 m["include"] = f.Include 42 } 43 return json.Marshal(m) 44 } 45 46 // UnmarshalJSON will attempt to parse the supplied JSON into this Filter. 47 func (f *Filter) UnmarshalJSON(b []byte) error { 48 var m map[string]json.RawMessage 49 if err := json.Unmarshal(b, &m); err != nil { 50 return err 51 } 52 if len(m) == 0 { 53 return nil 54 } 55 if v, ok := m["pid"]; ok { 56 if err := json.Unmarshal(v, &f.PID); err != nil { 57 return err 58 } 59 } 60 if v, ok := m["session"]; ok { 61 if err := json.Unmarshal(v, &f.Session); err != nil { 62 return err 63 } 64 } 65 if v, ok := m["elevated"]; ok { 66 if err := json.Unmarshal(v, &f.Elevated); err != nil { 67 return err 68 } 69 } 70 if v, ok := m["exclude"]; ok { 71 if err := json.Unmarshal(v, &f.Exclude); err != nil { 72 return err 73 } 74 } 75 if v, ok := m["include"]; ok { 76 if err := json.Unmarshal(v, &f.Include); err != nil { 77 return err 78 } 79 } 80 if v, ok := m["fallback"]; ok { 81 if err := json.Unmarshal(v, &f.Fallback); err != nil { 82 return err 83 } 84 } 85 return nil 86 } 87 func (b boolean) MarshalJSON() ([]byte, error) { 88 switch b { 89 case True: 90 return []byte(`"true"`), nil 91 case False: 92 return []byte(`"false"`), nil 93 default: 94 } 95 return []byte(`""`), nil 96 } 97 func (b *boolean) UnmarshalJSON(d []byte) error { 98 if len(d) == 0 { 99 *b = Empty 100 return nil 101 } 102 if d[0] == '"' && len(d) >= 1 { 103 switch d[1] { 104 case '1', 'T', 't': 105 *b = True 106 return nil 107 case '0', 'F', 'f': 108 *b = False 109 return nil 110 } 111 *b = Empty 112 return nil 113 } 114 switch d[0] { 115 case '1', 'T', 't': 116 *b = True 117 return nil 118 case '0', 'F', 'f': 119 *b = False 120 return nil 121 } 122 *b = Empty 123 return nil 124 }