fury icon indicating copy to clipboard operation
fury copied to clipboard

[Golang] unmarshal to struct

Open Nyx2022 opened this issue 2 years ago • 3 comments

Is your feature request related to a problem? Please describe.

when i marshal a map in java,and send it by internet to a golang program,i only can unmarshal it to a interface{},but i want and need unmarshal it to a struct in golang so that i can get or set attribute easily,but no i unmarshal it to a struct ,i will get a err like:"reflect.Value.IsNil"

Describe the solution you'd like

support unmarshal to struct in golang

Additional context

Nyx2022 avatar Jan 28 '24 12:01 Nyx2022

Hi @Nyx2022, thanks for reporting this issue. Could you provide some test code here? Fury supports deserialized to struct directly:

a := &A{}
a.A1 = a
bytes, err := fury.Marshal(a)
require.Nil(t, err)
var a1 *A
err = fury.Unmarshal(bytes, &a1)
require.Nil(t, err)
require.Same(t, a1, a1.A1)

chaokunyang avatar Feb 02 '24 04:02 chaokunyang

Hi @Nyx2022, thanks for reporting this issue. Could you provide some test code here?嗨,感谢您报告此问题。你能在这里提供一些测试代码吗? Fury supports deserialized to struct directly:Fury 支持直接反序列化为 struct:

a := &A{}
a.A1 = a
bytes, err := fury.Marshal(a)
require.Nil(t, err)
var a1 *A
err = fury.Unmarshal(bytes, &a1)
require.Nil(t, err)
require.Same(t, a1, a1.A1)

i marshal some data in java and save bytes into a file,


    public static Map<String, String> createObject() {

        Map<String, String> m = new HashMap<String, String>();

        m.put("a", "4");
        m.put("b", "3");
        m.put("c", "2");
        m.put("d", "1");

        return m;
    }

    public static void main(String[] args) {

        System.out.println("Hello world!");


        Fury fury = Fury.builder().withLanguage(Language.XLANG)
                .withRefTracking(true).build();


        byte[] bytes = fury.serialize(createObject());

        System.out.println(bytes.length);


        try {
            Files.write(Paths.get("d:/fury.txt"), bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        System.out.println(new String(bytes));
        System.out.println(fury.deserialize(bytes));
    }

then read from file and unmarhal to a struct in golang:

the type T is

type T struct {
	A string `json:"a"`
	B string `json:"b"`
	C string `json:"c"`
	D string `json:"d"`
}

        fury := furygo.NewFury(true)
	

	file, err := os.ReadFile("d:/fury.txt")
	if err == nil && len(file) > 0 {
		var Caches21 map[interface{}]*T
		start1 := time.Now()
		err6 := fury.Unmarshal(file, &Caches21)
		if err6 == nil {
			fmt.Println("fury 反序列化耗时:" + time.Now().Sub(start1).String())
			fmt.Println(Caches21)
		}

	}

but the error occured : image

so how can i use fury in multi langs

Nyx2022 avatar Feb 18 '24 07:02 Nyx2022

val := event.Query{ Nat: 1, Ip: "193.1.2.3", Url: "http://baidu.com", Status: 0, } fury_ := fury.NewFury(true) if err := fury_.RegisterTagType("example.SomeClass", event.Query{}); err != nil { panic(err) }

bt, err := fury_.Marshal(&val)
if err != nil {
    b.Log(err)
    return
}

   _ = fury_.Unmarshal(bt, &newval)
   }
   报错 
   panic: reflect.Set: value of type *event.Query is not assignable to type event.Query

而且Readme里的文档 是用interface{}, 不是结构体。 测试无论是序列化反序列化 比Go标准库的json 的时间都不如

longbai avatar Mar 06 '24 10:03 longbai