Golang 超快JSON 解析

Golang 超快JSON 解析,基于 github.com/json-iterator/go

package main

import (


type StructOfTag struct {
	field1 string `json:"field-1"`
	field2 string `json:"-"`
	field3 int    `json:",string"`

func str2byte(s string) []byte {
	x := (*[2]uintptr)(unsafe.Pointer(&s))
	h := [3]uintptr{x[0], x[1], x[1]}
	return *(*[]byte)(unsafe.Pointer(&h))
func byte2str(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))

func b2s(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))

// s2b converts string to a byte slice without memory allocation.
// Note it may break if string and/or slice header will change
// in the future go versions.
func s2b(s string) []byte {
	sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
	bh := reflect.SliceHeader{
		Data: sh.Data,
		Len:  sh.Len,
		Cap:  sh.Len,
	return *(*[]byte)(unsafe.Pointer(&bh))

func main() {

	struct_ := StructOfTag{}
	//jsoniter.Unmarshal([]byte(`{"field-1": "hello", "field2": "", "field3": "100"}`), &struct_)
	jsoniter.Unmarshal(s2b(`{"field-1": "hello", "field2": "", "field3": "100"}`), &struct_)




{hello  100}
本文网址: https://golangnote.com/topic/143.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Captcha image
Relative Articles