Browse Source

增加注释提示json库的BUG

feature_wq
Sydonian 10 months ago
parent
commit
aaf21a2f91
2 changed files with 17 additions and 1 deletions
  1. +16
    -0
      utils/serder/serder_test.go
  2. +1
    -1
      utils/serder/union_handler.go

+ 16
- 0
utils/serder/serder_test.go View File

@@ -648,4 +648,20 @@ func Test_ObjectToJSON2(t *testing.T) {
So(err, ShouldBeNil)
So(ret, ShouldResemble, val)
})

}

func Test_ObjectToJSON3(t *testing.T) {
Convey("反序列化TypeUnion时,JSON中对应字段类型不对", t, func() {
type Base interface{}

union := types.NewTypeUnion[Base](&St1{}, &St2{})
UseTypeUnionInternallyTagged(&union, "Type")

v, err := JSONToObjectEx[[]Base]([]byte("[{\"Type\":\"St2\", \"Val\":[]}]"))
t.Logf("err: %v", err)
t.Logf("v: %+v", v[0])
So(err, ShouldNotBeNil)
// So(ret, ShouldResemble, val)
})
}

+ 1
- 1
utils/serder/union_handler.go View File

@@ -254,7 +254,7 @@ func (e *InternallyTaggedDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iter
// 否则New出来的是会是**T,这将导致后续的反序列化出问题
if typ.Kind() == reflect.Pointer {
val := reflect.New(typ.Elem())
raw.ToVal(val.Interface())
raw.ToVal(val.Interface()) // TODO 使用的库丢失了ToVal期间的错误信息,考虑换个库

retVal := reflect.NewAt(e.union.Union.UnionType, ptr)
retVal.Elem().Set(val)


Loading…
Cancel
Save