解码任意数据

huangapple go评论90阅读模式
英文:

unmarshalling arbitrary data

问题

有没有一种方法可以将JSON数据进行分段解组(unmarshal)的方式进行编组(marshal)?

假设数据的上半部分是一个"code",用于指示如何处理下半部分...例如,根据"code"将下半部分解组(unmarshal)为特定的结构体。


有两个可能作为下半部分发送的结构体...

type Range struct {
    Start int
    End   int
}

type User struct {
    ID    int
    Pass  int
}

伪代码示例

可能是这样的...

message := &Message{
    Code: 4,
    &Range {
        Start: 1,
        End: 10,
    }
}

也可能是这样的...

message := &Message{
    Code: 3,
    &User {
        ID: 1,
        Pass: 1234,
    }
}

因此,在解组(unmarshalling)数据时,我可以...

// 从上半部分获取code
m := Message{}
err = json.UnmarshalTopHalf(byteArray, &m)
if m.Code == 4 {
    // 好的,code是4,让我们将其解组(unmarshal)为Range类型
    r := Range{}
    json.UnmarshalBottomHalf(byteArray, &r)
}

我查看了JSON & Go来学习如何编组(marshal)和解组(unmarshal)定义的结构体。我可以做到这一点,但我无法找到一种处理上述示例中的任意数据的方法...

英文:

Is there a way to marshall JSON data in such a way that it can be unmarshalled in parts / sections?

Let's say that the top half of data is a "code" which would signal what to do with the bottom half ... such as unmarshall the bottom half into a specific struct depending on the "code".


There are two structs that may be sent as the bottom half ...

type Range Struct {
    Start int
    End   int

}

type User struct {
    ID    int
    Pass  int
}

PSEUDO CODE EXAMPLE

It may look like this ...

message := &Message{
    Code: 4,
    &Range {
        Start: 1,
        End: 10,
    }
}

Itt may look like this ...

message := &Message{
    Code: 3,
    &User {
        ID: 1,
        Pass: 1234,
    }
}

So, when unmarshalling that data I could ...

// get code from top half
m := Message{}
err = json.UnMarshallTopHalf(byteArray, &m)
if m.Code == 4 {
    // ok, the code was four, lets unmarshall into type Range
    r := Range{}
    json.UnmarshalBottomHalf(byteArray, &r)
}

I have looked at JSON & Go to learn how to marshall and unmarshall defined structs. I can do this, but I cannot figure out a way for arbitrary data as in the example above ...

答案1

得分: 1

以下是翻译好的内容:

type Message struct {
    Code int `json:"cc"`
    Range *Range `json:"vvv,omitempty"`
    User *User `json:"fff,omitempty"`
}

如果给定的code等于x,则使用Range,如果等于Y,则使用User

英文:
type Message struct  {
    Code int `json:"cc"`
    Range *Range `json:"vvv,omitempty"`
    User *User `json:"fff,omitempty"`
}

then given code == x, use range, if Y, use User.

答案2

得分: 1

你可以先将底层的json.RawMessage解组成结构体,类似这样:

package main

import (
	"encoding/json"
	"fmt"
)

type Message struct {
	Code    int
	Payload json.RawMessage // 延迟解析,直到我们知道代码
}
type Range struct {
	Start int
	End   int
}
type User struct {
	ID   int
	Pass int
}

func MyUnmarshall(m []byte) {
	var message Message
	var payload interface{}
	json.Unmarshal(m, &message) // 延迟解析,直到我们知道代码
	switch message.Code {
	case 3:
		payload = new(User)
	case 4:
		payload = new(Range)
	}
	json.Unmarshal(message.Payload, payload) // 省略了错误检查以提高可读性
	fmt.Printf("\n%v%+v", message.Code, payload) // 处理数据
}

func main() {
	json := []byte(`{"Code": 4, "Payload": {"Start": 1, "End": 10}}`)
	MyUnmarshall(json)
	json = []byte(`{"Code": 3, "Payload": {"ID": 1, "Pass": 1234}}`)
	MyUnmarshall(json)
}

这段代码的作用是将JSON数据解析为不同的结构体类型,根据Code字段的值来确定使用哪个结构体类型进行解析。然后,你可以根据需要对解析后的数据进行处理。

英文:

You can unmarshall bottom half in json.RawMessage first, something like

package main

import (
	"encoding/json"
	"fmt"
)

type Message struct {
	Code    int
	Payload json.RawMessage // delay parsing until we know the code
}
type Range struct {
	Start int
	End   int
}
type User struct {
	ID   int
	Pass int
}

func MyUnmarshall(m []byte) {
	var message Message
	var payload interface{}
	json.Unmarshal(m, &message) // delay parsing until we know the color space
	switch message.Code {
	case 3:
		payload = new(User)
	case 4:
		payload = new(Range)
	}
	json.Unmarshal(message.Payload, payload) //err check ommited for readability
	fmt.Printf("\n%v%+v", message.Code, payload) //do something with data
}

func main() {
	json := []byte(`{"Code": 4, "Payload": {"Start": 1, "End": 10}}`)
	MyUnmarshall(json)
	json = []byte(`{"Code": 3, "Payload": {"ID": 1, "Pass": 1234}}`)
	MyUnmarshall(json)
}

huangapple
  • 本文由 发表于 2017年8月26日 23:21:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/45896930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定