草庐IT

marshaling

全部标签

C# 性能 - 使用不安全指针代替 IntPtr 和 Marshal

问题我正在将C应用程序移植到C#中。C应用程序从第3方DLL调用许多函数,因此我在C#中为这些函数编写了P/Invoke包装器。其中一些C函数分配我必须在C#应用程序中使用的数据,因此我使用了IntPtr,Marshal.PtrToStructure和Marshal.Copy将native数据(数组和结构)复制到托管变量中。不幸的是,事实证明C#应用程序比C版本慢得多。快速性能分析表明,上述基于编码的数据复制是瓶颈。我正在考虑通过重写C#代码以使用指针来加速它。由于我没有使用C#中的不安全代码和指针的经验,因此我需要有关以下方面的专家意见问题:使用unsafe代码和指针代替IntPtr

去编程语言: json marshalling

我期望从以下代码中得到{"a":"42","b":"78"},但它并没有这样做。packagemainimport("encoding/json""fmt")typeSstruct{Aint`json:"a,string"`B*int`json:"b,string"`}funcmain(){varsSjson.Unmarshal([]byte(`{"a":"42","b":"78"}`),&s)m,_:=json.Marshal(s)fmt.Println(string(m))}我做错了什么吗? 最佳答案 这是go语言1.3的一个已

go - grpc:服务器无法编码响应:rpc错误:代码=内部desc = grpc:编码时出错:proto:Marshal用nil调用

理想情况下,以下RPC应该接收消息并编码为JSON。但是,遇到以下错误:ERROR:2018/08/1213:43:07grpc:serverfailedtoencoderesponse:rpcerror:code=Internaldesc=grpc:errorwhilemarshaling:proto:Marshalcalledwith无func(s*beaconServer)Transmit(ctxcontext.Context,batch*pb.Batch)(*pb.Empty,error){varempty*pb.EmptyvarmessageJSONbytes.Bufferm

json - 具有导出和未导出字段的 Golang Marshal/Unmarshal JSON

我见过很多编码/取消编码只有未导出字段的结构的方法。但是我怎样才能对混合字段执行此操作?给定一个结构:typeTeststruct{fieldAstring`json:"fieldA"`FieldBint`json:"fieldB"`FieldCstring`json:"fieldC"`}如何编写MarshalJSON/UnmarshalJSON函数,以便将fieldA与FieldB和FieldC一起传输?以下编译,但在我运行时溢出调用堆栈。我的猜测是我正在递归编码对象,但我不确定在编码时如何保留导出和未导出的字段。func(t*Test)MarshalJSON()([]byte,er

go - 假设特定结构上的 json.Marshall 不可能失败是否安全?

我已阅读thisquestion它询问json.Marshal是否可以在任何输入上失败,并且根据它看起来像我的情况的答案它不会失败。我的情况如下:我有一个特定的结构(没有嵌套,没有数组,只有字符串、各种类型的整数、bool值)。我需要将它编码成一个json。它会失败吗?更多specificexample:typesomestruct{F1string`json:"f1"`F2uint32`json:"f2"`F3int64`json:"f3"`F4bool`json:"f4"`}funcdoSomething(ssome)(string,error){data,err:=json.Ma

json.Marshal(struct) 返回 "{}"

typeTestObjectstruct{kindstring`json:"kind"`idstring`json:"id,omitempty"`namestring`json:"name"`emailstring`json:"email"`}funcTestCreateSingleItemResponse(t*testing.T){testObject:=new(TestObject)testObject.kind="TestObject"testObject.id="f73h5jf8"testObject.name="YuriGagarin"testObject.email="Yu

json - 在 Golang 中的 json.Marshal 之前添加键

我正在创建一个函数来获取对象数组并将其保存到Struct中。然后我想把它转换成JSON。funcGetCountry(msgstring)[]byte{varcountries[]*countryModel.Countrycountries=countryModel.GetAllCountry()jsResult,err:=json.Marshal(countries)iferr!=nil{logger.Error(err,"FailedonGetCountry")}returnjsResult}这是结构typeCountrystruct{Idint`json:"id"`Country

json - 自定义时间 Marshall Unmarshall

我有一个MysqlTime结构,它有自己的编码和解码。typeMysqlTimestruct{time.Time}constMYSQL_TIME_FORMAT="2006-01-0215:04:05"func(t*MysqlTime)UnmarshalJSON(b[]byte)(errerror){s:=strings.Trim(string(b),`\`)ifs=="null"{t.Time=time.Time{}return}t.Time,err=time.Parse(MYSQL_TIME_FORMAT,s)return}func(t*MysqlTime)MarshalJSON()

postgresql - 您如何在 UTC 而不是本地时区中对 pq.NullTime 进行 JSON Marshall?

我在postgres表中有日期。日期以UTC时区存储。来自python的示例。roster=Roster.objects.get(id=266438)roster.start_timestampOut[11]:datetime.datetime(2018,9,7,15,0,tzinfo=)当我从golib/pq中编码这些日期时,我的本地时区以某种方式被应用。func(nt*pq.NullTime)MarshalJSON()([]byte,error){if!nt.Valid{return[]byte("\"\""),nil}val:=fmt.Sprintf("\"%s\"",nt.Ti

go - 如何使用json marshal获取 map 值

我需要将json字符串转换为map。这是我的go程序。packagemainimport("encoding/json""fmt")funcmain(){str:=`{"Bangalore_City":"35_Temperature","NewYork_City":"31_Temperature","Copenhagen_City":"29_Temperature","hobbies":{"name":"username"}}`varmmap[string]interface{}json.Unmarshal([]byte(str),&m)fmt.Println(m["hobbies"]