|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package json
-
- import (
- "reflect"
-
- jsoniter "github.com/json-iterator/go"
- "gitlink.org.cn/cloudream/common/pkgs/types"
- )
-
- type Config struct {
- unionHandler *UnionHandler
- exts []jsoniter.Extension
- }
-
- func New() *Config {
- return &Config{
- unionHandler: &UnionHandler{
- internallyTagged: make(map[reflect.Type]*anyTypeUnionInternallyTagged),
- externallyTagged: make(map[reflect.Type]*anyTypeUnionExternallyTagged),
- },
- }
- }
-
- func (c *Config) UseUnionInternallyTagged(u *types.AnyTypeUnion, tagField string) *Config {
- iu := &anyTypeUnionInternallyTagged{
- Union: u,
- TagField: tagField,
- TagToType: make(map[string]reflect.Type),
- }
-
- for _, eleType := range u.ElementTypes {
- iu.Add(eleType)
- }
-
- c.unionHandler.internallyTagged[u.UnionType] = iu
- return c
- }
-
- func (c *Config) UseUnionExternallyTagged(u *types.AnyTypeUnion) *Config {
- eu := &anyTypeUnionExternallyTagged{
- Union: u,
- TypeNameToType: make(map[string]reflect.Type),
- }
-
- for _, eleType := range u.ElementTypes {
- eu.Add(eleType)
- }
-
- c.unionHandler.externallyTagged[u.UnionType] = eu
- return c
- }
-
- func (c *Config) UseExtension(ext jsoniter.Extension) *Config {
- c.exts = append(c.exts, ext)
- return c
- }
-
- func (c *Config) Build() Serder {
- cfg := jsoniter.Config{}
- api := cfg.Froze()
-
- api.RegisterExtension(c.unionHandler)
-
- for _, ext := range c.exts {
- api.RegisterExtension(ext)
- }
-
- return Serder{
- cfg: *c,
- api: api,
- }
- }
|