映射到 JSON,但保留键顺序

我有一个地图对象,当使用json.Marshal(myMapObjct)Golang 对其进行序列化时,它会按字母顺序对键进行排序,这会导致数据处理方式出现问题。将 JSON 结构创建为字节切片后,保留键顺序很重要。是否可以以这种方式对其进行序列化,或者我是否需要为这种特定的边缘情况编写自己的序列化程序?


这是负责生成 JSON 结构的代码片段:


// serializedTraffic wraps around naturalTraffic and serializes map to string.

func serializedTraffic(payload string) (string, error) {

    trafficMap := naturalTraffic(string(payload))

    traffic, err := json.Marshal(trafficMap)

    return string(traffic), err

}


    // naturalTraffic obfuscates 'payload' into JSON-like structure.

func naturalTraffic(payload string) map[string]string {

    // Decide on how many keys there will be in the JSON structure.

    indexChar := 0

    maxChars := 126

    minChars := 16


    var jsonObject = make(map[string]string)


    // Build the JSON structure.

    for indexChar < len(payload) {

        rand.Seed(time.Now().UnixNano())

        chunkSize := rand.Intn(maxChars-minChars) + minChars

        if len(payload) < indexChar+chunkSize {

            chunkSize = len(payload) - indexChar

        }

        key := randomPopularWord()

        jsonObject[key] = base64.StdEncoding.EncodeToString([]byte(payload[indexChar : indexChar+chunkSize]))

        indexChar += chunkSize

    }


    return jsonObject

}


料青山看我应如是
浏览 109回答 1
1回答

繁花不似锦

根据建议,我提供了一种不同的结构来修复代码。&nbsp; &nbsp; type elem struct{ key, val string }&nbsp; &nbsp; type object []elem&nbsp; &nbsp; func (o object) MarshalJSON() (out []byte, err error) {&nbsp; &nbsp; &nbsp; &nbsp; if o == nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return []byte(`null`), nil&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if len(o) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return []byte(`{}`), nil&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; out = append(out, '{')&nbsp; &nbsp; &nbsp; &nbsp; for _, e := range o {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key, err := json.Marshal(e.key)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val, err := json.Marshal(e.val)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = append(out, key...)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = append(out, ':')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = append(out, val...)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = append(out, ',')&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // replace last ',' with '}'&nbsp; &nbsp; &nbsp; &nbsp; out[len(out)-1] = '}'&nbsp; &nbsp; &nbsp; &nbsp; return out, nil&nbsp; &nbsp; }&nbsp; &nbsp; // serializedTraffic wraps around naturalTraffic and serializes map to string.&nbsp; &nbsp; func serializedTraffic(payload string) (string, error) {&nbsp; &nbsp; &nbsp; &nbsp; trafficMap := naturalTraffic(string(payload))&nbsp; &nbsp; &nbsp; &nbsp; traffic, err := trafficMap.MarshalJSON()&nbsp; &nbsp; &nbsp; &nbsp; return string(traffic), err&nbsp; &nbsp; }&nbsp; &nbsp; // naturalTraffic obfuscates 'payload' into JSON-like structure.&nbsp; &nbsp; func naturalTraffic(payload string) object {&nbsp; &nbsp; &nbsp; &nbsp; // Decide on how many keys there will be in the JSON structure.&nbsp; &nbsp; &nbsp; &nbsp; indexChar := 0&nbsp; &nbsp; &nbsp; &nbsp; maxChars := 126&nbsp; &nbsp; &nbsp; &nbsp; minChars := 16&nbsp; &nbsp; &nbsp; &nbsp; var jsonObject object&nbsp; &nbsp; &nbsp; &nbsp; // Build the JSON structure.&nbsp; &nbsp; &nbsp; &nbsp; for indexChar < len(payload) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand.Seed(time.Now().UnixNano())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunkSize := rand.Intn(maxChars-minChars) + minChars&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(payload) < indexChar+chunkSize {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunkSize = len(payload) - indexChar&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key := randomPopularWord()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObject = append(jsonObject, elem{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key: key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val: base64.StdEncoding.EncodeToString([]byte(payload[indexChar : indexChar+chunkSize])),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indexChar += chunkSize&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return jsonObject&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go