fix: can't duplicately decode Serializer object (#144)
* fix: can't duplicately decode Serializer object
* separate imports
diff --git a/Makefile b/Makefile
index 0732d1e..0123897 100644
--- a/Makefile
+++ b/Makefile
@@ -18,5 +18,11 @@
lint2:
golangci-lint run
+
+format:
+ go fmt
+
test:
go test
+
+all: format test
\ No newline at end of file
diff --git a/object.go b/object.go
index 94f35eb..4756148 100644
--- a/object.go
+++ b/object.go
@@ -550,9 +550,7 @@
cls, _ = clsDef.(classInfo)
//add to slice
d.appendClsDef(cls)
- if c, ok := GetSerializer(cls.javaName); ok {
- return c.DecObject(d)
- }
+
return d.DecodeValue()
case tag == BC_OBJECT:
@@ -572,6 +570,10 @@
return d.decEnum(cls.javaName, TAG_READ)
}
+ if c, ok := GetSerializer(cls.javaName); ok {
+ return c.DecObject(d, typ, cls)
+ }
+
return d.decInstance(typ, cls)
case BC_OBJECT_DIRECT <= tag && tag <= (BC_OBJECT_DIRECT+OBJECT_DIRECT_MAX):
@@ -586,6 +588,10 @@
return d.decEnum(cls.javaName, TAG_READ)
}
+ if c, ok := GetSerializer(cls.javaName); ok {
+ return c.DecObject(d, typ, cls)
+ }
+
return d.decInstance(typ, cls)
default:
diff --git a/serialize.go b/serialize.go
index 829a83e..08cc45e 100644
--- a/serialize.go
+++ b/serialize.go
@@ -18,6 +18,10 @@
package hessian
import (
+ "reflect"
+)
+
+import (
big "github.com/dubbogo/gost/math/big"
)
@@ -28,7 +32,7 @@
type Serializer interface {
EncObject(*Encoder, POJO) error
- DecObject(*Decoder) (interface{}, error)
+ DecObject(*Decoder, reflect.Type, classInfo) (interface{}, error)
}
var serializerMap = make(map[string]Serializer, 16)
@@ -53,8 +57,8 @@
return e.encObject(decimal)
}
-func (DecimalSerializer) DecObject(d *Decoder) (interface{}, error) {
- dec, err := d.DecodeValue()
+func (DecimalSerializer) DecObject(d *Decoder, typ reflect.Type, cls classInfo) (interface{}, error) {
+ dec, err := d.decInstance(typ, cls)
if err != nil {
return nil, err
}
diff --git a/serialize_test.go b/serialize_test.go
index addfb65..1dff269 100644
--- a/serialize_test.go
+++ b/serialize_test.go
@@ -69,3 +69,25 @@
assert.Equal(t, content, r.(*big.Decimal).String())
})
}
+
+func TestDecimalListGoDecode(t *testing.T) {
+ data := []string{
+ "123.4",
+ "123.45",
+ "123.456",
+ }
+
+ out, err := decodeJavaResponse(`customReplyTypedFixedList_BigDecimal`, ``, false)
+ if err != nil {
+ t.Error(err)
+ return
+ }
+
+ resp := out.([]*big.Decimal)
+ for i := range data {
+ gotDecimal := resp[i]
+ if gotDecimal.String() != data[i] {
+ t.Errorf("java: %s go: %s", gotDecimal.String(), data[i])
+ }
+ }
+}
diff --git a/test_hessian/src/main/java/test/TestCustomReply.java b/test_hessian/src/main/java/test/TestCustomReply.java
index 6b40ac3..1f32a7e 100644
--- a/test_hessian/src/main/java/test/TestCustomReply.java
+++ b/test_hessian/src/main/java/test/TestCustomReply.java
@@ -350,6 +350,16 @@
output.flush();
}
+ public void customReplyTypedFixedList_BigDecimal() throws Exception {
+ BigDecimal[] decimals = new BigDecimal[]{
+ new BigDecimal("123.4"),
+ new BigDecimal("123.45"),
+ new BigDecimal("123.456"),
+ };
+ output.writeObject(decimals);
+ output.flush();
+ }
+
public void customReplyTypedFixedDateNull() throws Exception {
DateDemo demo = new DateDemo("zhangshan", null, null);
output.writeObject(demo);