dongyong6428 2017-03-08 17:15
浏览 136

Flatbuffers GoLang-在序列化和反序列化数据时无法理解我的错误,导致无法检索数据

I am new to Flatbuffers and GoLang. I am trying to implement a function that take converts an object to flatbuffer and retrieves the same object. Here is my code. Updated Code

func getannouncements(){
    annList := SR.GetFromDB().GetAllAnnouncementList()
    fmt.Println(annList)
    builder := flatbuffers.NewBuilder(1024)
    var thisobjlist [12] flatbuffers.UOffsetT
    for i,j := range annList{
        annTitle := builder.CreateString(j.AnnTitle)
        annText := builder.CreateString(j.AnnText)
        annDate := builder.CreateString(j.AnnDate)
        fb.AnnouncementStart(builder)
        fb.AnnouncementAddAnnId(builder,int32(j.AnnID))
        fb.AnnouncementAddAnnTitle(builder,annTitle)
        fb.AnnouncementAddAnnText(builder, annText)
        fb.AnnouncementAddAnnActive(builder,CR.BoolToByte(j.AnnActive))
        fb.AnnouncementAddAnnDate(builder,annDate)
        thisobj:= fb.AnnouncementEnd(builder)
        thisobjlist[i] = thisobj
    }
    fb.AnnouncementListStartAnnListVector(builder,len(annList))
    for _,j:=range thisobjlist{
        builder.PlaceUOffsetT(j)
    }
    finalObj := fb.AnnouncementListEnd(builder)
    builder.Finish(finalObj)
    buf:= builder.FinishedBytes()
    fmt.Println(buf)
    /*bufItem := new(bytes.Buffer)
    binary.Write(bufItem, binary.LittleEndian, buf)
    buf1 := bufItem.Bytes()
    buffyRead := bytes.NewReader(buf1)
    var buffy []byte
    binary.Read(buffyRead, binary.LittleEndian, &buffy)*/
    Anncmt:=  fb.GetRootAsAnnouncementList(buf,0)
    anns := new(fb.Announcement)
    if Anncmt.AnnList(anns,1){
        thisLists := anns.AnnTitle()
        fmt.Println(thisLists)
    }
    fmt.Println(Anncmt)
  }

Schema File

namespace FlatBufs;
table Announcement{
    AnnId:int;
    AnnTitle:string;
    AnnText:string;
    AnnDate:string;
    AnnActive:bool= false;
}

table AnnouncementList{
   AnnName:string;
   AnnList:[Announcement];
}
root_type AnnouncementList

The buf object is a byte array. However when I am generating the AnnGot obj I am still getting the almost same byte array as buf. So, When I read various posts on internet on this topic, I tried to convert that buf to binary type and then retrieve buf and tried to retrieve the data (as in the commented part of code). This time the object buffy doesn't have any data in it. I am still not clear what mistake I am making in this entire code.

Please point me in the right direction. Any help on this is much appreciated.

Thanks, Tom

  • 写回答

1条回答 默认 最新

  • dongli7870 2017-03-08 21:50
    关注

    Not sure why you're using two builders. Each builder is creating one FlatBuffer, so things you store in one won't be available in the other.

    A smaller problem is that you are nesting things (the Go implementation should guard against that, I believe?), i.e. you should finish building all announcements before you create a vector of them.. in your case this will all be mixed up.

    You also appear to make your root an AnnouncementList, but then you try to access it as an Announcement.. this won't work. It be easier to see if we could see your schema.

    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致