本文整理汇总了Golang中github.com/pquerna/ffjson/ffjson.Unmarshal函数的典型用法代码示例。如果您正苦于以下问题:Golang Unmarshal函数的具体用法?Golang Unmarshal怎么用?Golang Unmarshal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Unmarshal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: HandleAddCustomEventHandlers
func HandleAddCustomEventHandlers(e *tree_event.Event, api_cmd Command) {
var (
out = &WriterCallback{BufferMaxSize: 1024}
handler_data map[string]interface{}
err tree_lib.TreeError
ev_data []byte
)
err.From = tree_lib.FROM_ADD_CUSTOM_EVENT
err.Err = ffjson.Unmarshal(api_cmd.Data, &handler_data)
if !err.IsNull() {
tree_log.Error(err.From, err.Error())
return
}
out.OutCallback = func(data []byte, ended bool) {
cb_cmd := api_cmd
cb_cmd.Ended = ended
cb_cmd.Data = data
ev_data, err.Err = ffjson.Marshal(cb_cmd)
if !err.IsNull() {
tree_log.Error(err.From, err.Error())
return
}
SendCommandCallback(e, ev_data)
}
defer out.End()
event_name := handler_data["name"].(string)
handles_interfaces := handler_data["handlers"].([]interface{})
var (
handles_interfaces_data []byte
handlers []custom_event.Handler
)
handles_interfaces_data, err.Err = ffjson.Marshal(handles_interfaces)
if !err.IsNull() {
tree_log.Error(err.From, err.Error())
return
}
err.Err = ffjson.Unmarshal(handles_interfaces_data, &handlers)
if !err.IsNull() {
tree_log.Error(err.From, err.Error())
return
}
err = custom_event.ON(event_name, handlers...)
if !err.IsNull() {
tree_log.Error(err.From, err.Error())
}
}
示例2: parseError
// Looks for an error message in the response body and parses it into a
// B2Error object
func (c *B2) parseError(body []byte) error {
b2err := &B2Error{}
if ffjson.Unmarshal(body, b2err) != nil {
return nil
}
return b2err
}
示例3: GetUsername
func GetUsername(authorization string) (username, token string, err error) {
var data TokenData
var dataSp []string
var body []byte
token, err = GetAccessToken(authorization)
if err != nil {
return
}
body, err = GetUsernameFromToken(token)
if err != nil {
return
}
err = ffjson.Unmarshal(body, &data)
if err != nil {
return
}
dataSp = strings.Split(data.Aeris, "+")
if len(dataSp) != 2 {
err = errors.New("username errror.")
return
}
username = "+" + dataSp[1]
return
}
示例4: HandleContainerCommand
func HandleContainerCommand(ev *tree_event.Event, cmd Command) {
var (
out = &WriterCallback{BufferMaxSize: 1024}
docker_cmd = tree_docker.DockerCmd{}
err tree_lib.TreeError
ev_data []byte
)
err.From = tree_lib.FROM_HANDLE_CONTAINER_COMMAND
err.Err = ffjson.Unmarshal(cmd.Data, &docker_cmd)
if !err.IsNull() {
tree_log.Error(err.From, "unable to unmarshal command data as a docker command -> ", err.Error())
return
}
out.OutCallback = func(data []byte, ended bool) {
cb_cmd := cmd
cb_cmd.Ended = ended
cb_cmd.Data = data
ev_data, err.Err = ffjson.Marshal(cb_cmd)
if !err.IsNull() {
tree_log.Error(err.From, err.Error())
return
}
SendCommandCallback(ev, ev_data)
}
defer out.End()
tree_docker.ContainerCommands(&docker_cmd, out)
}
示例5: wsSubscribe
func (server *Server) wsSubscribe(conn *websocket.Conn, p *proto.Proto) (userid int64, topics []int32, token string, err error) {
if err = p.ReadWebsocket(conn); err != nil {
return
}
if p.OpCode != define.OP_SUB_REQ {
err = ErrOperation
return
}
sub := new(proto.SubREQ)
err = ffjson.Unmarshal(p.Body, sub)
if err != nil {
return
}
sub.Server = server.Config.Id
if err = server.Subscribe(sub); err != nil {
return
}
userid = sub.UserId
topics = sub.Topics
token = sub.Token
p.Body = nil
p.OpCode = define.OP_SUB_RES
err = p.WriteWebsocket(conn)
return
}
示例6: Geocode
func (g *Geocoder) Geocode(q, countryCode string, offline bool) (
*jstruct.Location, error) {
res, err := g.geocodeFromCache(q, countryCode)
if err != nil || res != nil || offline {
return res, err
}
r, err := g.rawGeocode(q, countryCode)
if err != nil {
return nil, err
}
defer r.Close()
data, err := ioutil.ReadAll(&io.LimitedReader{
R: r,
N: 4 * 1024 * 1024,
})
if err != nil {
return nil, err
}
res = &jstruct.Location{}
err = ffjson.Unmarshal(data, res)
if err != nil {
return nil, err
}
key, _ := makeKeyAndCountryCode(q, countryCode)
err = g.cache.Put(key, data, buildLocation(res))
if err != nil {
return nil, err
}
return res, err
}
示例7: ParseConfigFile
func ParseConfigFile(file string) (conf TreeScaleConf, err tree_lib.TreeError) {
err.From = tree_lib.FROM_PARSE_CONFIG_FILE
switch strings.Replace(filepath.Ext(file), ".", "", 1) {
case "toml":
{
_, err.Err = toml.DecodeFile(file, &conf)
}
case "yaml":
{
var fdata []byte
fdata, err.Err = ioutil.ReadFile(file)
if !err.IsNull() {
return
}
err.Err = yaml.Unmarshal(fdata, &conf)
}
case "json":
{
var fdata []byte
fdata, err.Err = ioutil.ReadFile(file)
if !err.IsNull() {
return
}
err.Err = ffjson.Unmarshal(fdata, &conf)
}
}
return
}
示例8: Json2map
//json转map
func Json2map(jsonString string) (s map[string]interface{}, err error) {
var result map[string]interface{}
if err := ffjson.Unmarshal([]byte(jsonString), &result); err != nil {
return nil, err
}
return result, nil
}
示例9: GetParentInfo
func GetParentInfo(node string) (node_info.NodeInfo, tree_lib.TreeError) {
var (
err tree_lib.TreeError
pname string
)
err.From = tree_lib.FROM_GET_PARENT_INFO
err = ForEach(DB_NODE, func(key []byte, val []byte) error {
n := node_info.NodeInfo{}
err := ffjson.Unmarshal(val, &n)
if err != nil {
return err
}
if _, ok := tree_lib.ArrayContains(n.Childs, node); ok {
pname = n.Name
return errors.New("Just Err for break")
}
return nil
})
if len(pname) == 0 {
return node_info.NodeInfo{}, tree_lib.TreeError{}
}
// Node relations first element should be parent node
return GetNodeInfo(pname)
}
示例10: PushAll
// broadcast : push message to all users
func (h *HttpServer) PushAll(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
var (
bodyBytes []byte
err error
)
defer r.Body.Close()
if bodyBytes, err = ioutil.ReadAll(r.Body); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
msg := &proto.PushMsg{}
err = ffjson.Unmarshal(bodyBytes, msg)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := DefaultMQ.Broadcast(msg); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
示例11: PushTopic
// push message of topic to users who subscribe the topic
func (h *HttpServer) PushTopic(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
var (
bodyBytes []byte
err error
)
defer r.Body.Close()
if bodyBytes, err = ioutil.ReadAll(r.Body); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
reqmsg := &PushTopicREQ{}
err = ffjson.Unmarshal(bodyBytes, reqmsg)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
servers := DefaultServer.GetTopicServers(reqmsg.Topic)
if len(servers) == 0 {
http.Error(w, "no servers sub the topic", http.StatusInternalServerError)
return
}
if err = DefaultMQ.BroadcastTopic(servers, reqmsg.Topic, reqmsg.Msg); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
示例12: Request
// Request function for send requests to betfair via REST JSON
func (b *Betting) Request(reqStruct interface{}, url BetURL, method string, filter *Filter) error {
req, resp := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
urlBuild := bytes.NewBuffer([]byte{})
urlBuild.WriteString(string(url))
urlBuild.WriteString("/")
urlBuild.WriteString(method)
urlBuild.WriteString("/")
req.SetRequestURI(urlBuild.String())
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Application", b.ApiKey)
req.Header.Set("X-Authentication", b.SessionKey)
req.Header.SetMethod("POST")
if filter != nil {
filterBody, err := ffjson.Marshal(&filter)
if err != nil {
return err
}
req.SetBody(filterBody)
}
err := fasthttp.Do(req, resp)
if err != nil {
return err
}
if resp.StatusCode() == 400 {
err = ffjson.Unmarshal(resp.Body(), &bettingError)
if err != nil {
return err
}
return fmt.Errorf("Error with code - %s and string - %s", bettingError.Faultcode, bettingError.Faultstring)
}
err = ffjson.Unmarshal(resp.Body(), reqStruct)
if err != nil {
return err
}
return nil
}
示例13: BenchmarkFFJsonSmall
func BenchmarkFFJsonSmall(b *testing.B) {
for i := 0; i < b.N; i++ {
var data SmallPayload
ffjson.Unmarshal(smallFixture, &data)
nothing(data.Uuid, data.Tz, data.Ua, data.St)
}
}
示例14: enumerateStoredOffers
func enumerateStoredOffers(store *Store,
callback func(offer *jstruct.JsonOffer, do *DeletedOffer) error) error {
// Enumerate deleted offers
ids, err := store.ListDeletedIds()
if err != nil {
return err
}
for _, id := range ids {
deletedIds, err := store.ListDeletedOffers(id)
if err != nil {
return err
}
for _, deleted := range deletedIds {
data, err := store.GetDeleted(deleted.Id)
if err != nil {
return err
}
js := &jstruct.JsonOffer{}
err = ffjson.Unmarshal(data, js)
if err != nil {
return err
}
err = callback(js, &deleted)
if err != nil {
return err
}
}
}
// Enumerate valid offers
ids, err = store.List()
if err != nil {
return err
}
for _, id := range ids {
offer, err := getStoreJsonOffer(store, id)
if err != nil {
return err
}
if offer == nil {
fmt.Printf("skipping %s\n", id)
data, err := store.Get(id)
if err != nil {
fmt.Printf("error %s: %s\n", id, err)
} else if len(data) == 0 {
fmt.Printf("error %s: nil\n", id)
} else {
fmt.Printf("error %s: %s\n", id, string(data))
}
continue
}
err = callback(offer, nil)
if err != nil {
return err
}
}
return nil
}
示例15: PushMQ
// push
// does invalidate msg, if it is invalid, ignore it
func (c *CometRpc) PushMQ(msg []byte) (err error) {
m := &proto.MQMsg{}
if err = ffjson.Unmarshal(msg, m); err != nil {
log.Errorf("json.Unmarshal(%s) error(%s)", msg, err)
return
}
c.push(m)
return
}