github.heygears.com/openimsdk/tools@v0.0.49/utils/jsonutil/interface.go (about) 1 // Copyright © 2024 OpenIM open source community. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package jsonutil 16 17 type Json interface { 18 // Interface returns the underlying data 19 Interface() any 20 // Encode returns its marshaled data as `[]byte` 21 Encode() ([]byte, error) 22 // EncodePretty returns its marshaled data as `[]byte` with indentation 23 EncodePretty() ([]byte, error) 24 // Implements the json.Marshaler interface. 25 MarshalJSON() ([]byte, error) 26 // Set modifies `Json` map by `key` and `value` 27 // Useful for changing single key/value in a `Json` object easily. 28 Set(key string, val any) 29 // SetPath modifies `Json`, recursively checking/creating map keys for the supplied path, 30 // and then finally writing in the value 31 SetPath(branch []string, val any) 32 // Del modifies `Json` map by deleting `key` if it is present. 33 Del(key string) 34 // Get returns a pointer to a new `Json` object 35 // for `key` in its `map` representation 36 // 37 // useful for chaining operations (to traverse a nested JSON): 38 // js.Get("top_level").Get("dict").Get("value").Int() 39 Get(key string) Json 40 // GetPath searches for the item as specified by the branch 41 // without the need to deep dive using Get()'s. 42 // 43 // js.GetPath("top_level", "dict") 44 GetPath(branch ...string) Json 45 // CheckGet returns a pointer to a new `Json` object and 46 // a `bool` identifying success or failure 47 // 48 // useful for chained operations when success is important: 49 // if data, ok := js.Get("top_level").CheckGet("inner"); ok { 50 // log.Println(data) 51 // } 52 CheckGet(key string) (Json, bool) 53 // Map type asserts to `map` 54 Map() (map[string]any, error) 55 // Array type asserts to an `array` 56 Array() ([]any, error) 57 // Bool type asserts to `bool` 58 Bool() (bool, error) 59 // String type asserts to `string` 60 String() (string, error) 61 // Bytes type asserts to `[]byte` 62 Bytes() ([]byte, error) 63 // StringArray type asserts to an `array` of `string` 64 StringArray() ([]string, error) 65 66 // Implements the json.Unmarshaler interface. 67 UnmarshalJSON(p []byte) error 68 // Float64 coerces into a float64 69 Float64() (float64, error) 70 // Int coerces into an int 71 Int() (int, error) 72 // Int64 coerces into an int64 73 Int64() (int64, error) 74 // Uint64 coerces into an uint64 75 Uint64() (uint64, error) 76 }