Skip to content

Commit

Permalink
Merge pull request #320 from zhengzepeng/master
Browse files Browse the repository at this point in the history
if map is pojo, write class name first
  • Loading branch information
AlexStocks authored Jul 19, 2022
2 parents 83d6845 + 2437f11 commit 303698c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,16 @@ func (e *Encoder) encMap(m interface{}) error {
return nil
}

// if pojo, write class name first
if p, ok := m.(POJO); ok {
e.buffer = encByte(e.buffer, BC_MAP)
e.buffer = encString(e.buffer, p.JavaClassName())
} else {
e.buffer = encByte(e.buffer, BC_MAP_UNTYPED)
}

keys = value.MapKeys()

e.buffer = encByte(e.buffer, BC_MAP_UNTYPED)
if len(keys) > 0 {
typ = value.Type().Key()
for i := 0; i < len(keys); i++ {
Expand Down
12 changes: 12 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,15 @@ func TestCustomMap(t *testing.T) {

testDecodeFramework(t, "customReplyListMapListMap", listMapListMap)
}

type CustomMap map[string]interface{}

func (dict *CustomMap) JavaClassName() string {
return "test.model.CustomMap"
}

func TestJavaMap(t *testing.T) {
customMap := &CustomMap{"Name": "Test"}
RegisterPOJO(customMap)
testJavaDecode(t, "customArgTypedFixed_CustomMap", customMap)
}
9 changes: 9 additions & 0 deletions test_hessian/src/main/java/test/TestCustomDecode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.test.A0;
import com.caucho.hessian.test.A1;

import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

import test.model.CustomMap;
import test.model.DateDemo;


Expand Down Expand Up @@ -214,4 +216,11 @@ public Object customArgTypedFixedList_HashSet() throws Exception {
HashSet o = (HashSet) input.readObject();
return o.contains(0) && o.contains(1);
}

public Object customArgTypedFixed_CustomMap() throws Exception {
CustomMap o = (CustomMap) input.readObject();
String value = (String) o.get("Name");
return "Test".equals(value);
}

}

0 comments on commit 303698c

Please sign in to comment.