-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e691ceb
commit 1b1df8e
Showing
3 changed files
with
89 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package jsonx | ||
|
||
import "encoding/json" | ||
|
||
func BytesToObj[T any](s []byte) T { | ||
var v T | ||
err := json.Unmarshal(s, &v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return v | ||
} | ||
|
||
func BytesToObjE[T any](s []byte) (T, error) { | ||
var v T | ||
err := json.Unmarshal(s, &v) | ||
return v, err | ||
} | ||
|
||
func BytesToObjPtr[T any](s []byte) *T { | ||
var v T | ||
err := json.Unmarshal(s, &v) | ||
if err != nil || v == nil { | ||
return nil | ||
} | ||
return &v | ||
} | ||
|
||
func BytesToObjPtrE[T any](s []byte) (*T, error) { | ||
var v T | ||
err := json.Unmarshal(s, &v) | ||
if err != nil || v == nil { | ||
return nil, err | ||
} | ||
return &v, nil | ||
} | ||
|
||
func BytesToMap(s []byte) map[string]any { | ||
var v map[string]any | ||
err := json.Unmarshal(s, &v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return v | ||
} | ||
|
||
func BytesToMapE(s []byte) (map[string]any, error) { | ||
var v map[string]any | ||
err := json.Unmarshal(s, &v) | ||
return v, err | ||
} | ||
|
||
func BytesToArr(s []byte) []any { | ||
var v []any | ||
err := json.Unmarshal(s, &v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package jsonx | ||
|
||
func ToObj[T any](s string) T { | ||
return BytesToObj[T]([]byte(s)) | ||
} | ||
|
||
func ToObjE[T any](s string) (T, error) { | ||
return BytesToObjE[T]([]byte(s)) | ||
} | ||
|
||
func ToObjPtr[T any](s string) *T { | ||
return BytesToObjPtr[T]([]byte(s)) | ||
} | ||
|
||
func ToObjPtrE[T any](s string) (*T, error) { | ||
return BytesToObjPtrE[T]([]byte(s)) | ||
} | ||
|
||
func ToMap(s string) map[string]any { | ||
return BytesToMap([]byte(s)) | ||
} | ||
|
||
func ToMapE(s string) (map[string]any, error) { | ||
return BytesToMapE([]byte(s)) | ||
} | ||
|
||
func ToArr(s string) []any { | ||
return BytesToArr([]byte(s)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters