Skip to content
This repository has been archived by the owner on May 24, 2019. It is now read-only.
/ json Public archive

基于json tag扩展encode规则,实现struct动态(字段的取舍)编码为不同JSON结构,更自由的选择、忽略部分字段。echo-web示范

License

Notifications You must be signed in to change notification settings

hb-go/json

Repository files navigation

JSON encode扩展

基于json标签扩展encode规则,实现struct动态编码为不同JSON结构字段的取舍

  • golang struct转json提供了字段名、字段筛选-、空值忽略omitempty等标签,使用中对于选择性忽略部分字段的支持较弱
  • 可以定义新struct或者用map,实现自己想要的不同json结构
  • 在这里尝试扩展json tag规则的方式来支持同一struct解析为不同的json结构
  • 目前仅做了动态的字段取舍的规则,在此基础上可以进一步扩展,如多种字段名定义等

json标签格式

filter:[{preKey1}.{subKey1};{preKey2}.{subKey2}]

`json:"f,filter:preKey1.subKey1;preKey2;preKey2"`
`json:"f,filter:*.subKey"`
`json:"f,filter:preKey.*"`
`json:"f,filter:*.*"`

示范

struct

type A struct {
	F  string `json:"f,filter:*"`
	F1 string `json:"f_1,filter:a1"`
	F2 string `json:"f_2,filter:a2"`

	B  B `json:"b,filter:*.*"`
	B1 B `json:"b_1,filter:*.b1"`
	B2 B `json:"b_2,filter:a2.b2"`
	B3 B `json:"b_3,filter:a1.*;a2.a2"`
}

type B struct {
	F  string `json:"f,filter:*"`
	F1 string `json:"f_1,filter:b1"`
	F2 string `json:"f_2,filter:b2"`

	C C `json:"c,filter:*.*"`
	C1 C `json:"c_1,filter:b1.*"`
	C2 C `json:"c_3,filter:*.b1"`
}

type C struct {
	F  string `json:"f,filter:*"`
	F1 string `json:"f_1,filter:b1"`
	F2 string `json:"f_2,filter:b2"`
}
json.MarshalFilterIndent(i, "", "", "\t")
json.MarshalFilterIndent(i, "*", "", "\t")
json.MarshalFilterIndent(i, "a1", "", "\t")
json.MarshalFilterIndent(i, "a2", "", "\t")
http://{API_HOST}/json/encode
http://{API_HOST}/json/encode?filter=*
http://{API_HOST}/json/encode?filter=a1
http://{API_HOST}/json/encode?filter=a2
""json
{
	"f": "",
	"f_1": "",
	"f_2": "",
	"b": {
		"f": "",
		"f_1": "",
		"f_2": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": "",
			"f_2": ""
		}
	},
	"b_1": {
		"f": "",
		"f_1": "",
		"f_2": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": "",
			"f_2": ""
		}
	},
	"b_2": {
		"f": "",
		"f_1": "",
		"f_2": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": "",
			"f_2": ""
		}
	},
	"b_3": {
		"f": "",
		"f_1": "",
		"f_2": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": "",
			"f_2": ""
		}
	}
}
"*"json
{
	"f": "",
	"f_1": "",
	"f_2": "",
	"b": {
		"f": "",
		"f_1": "",
		"f_2": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": ""
		}
	},
	"b_1": {
		"f": "",
		"f_1": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": ""
		}
	},
	"b_2": {
		"f": "",
		"f_1": "",
		"f_2": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": ""
		}
	},
	"b_3": {
		"f": "",
		"f_1": "",
		"f_2": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": ""
		}
	}
}
"a1"json
{
	"f": "",
	"f_1": "",
	"b": {
		"f": "",
		"f_1": "",
		"f_2": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": ""
		}
	},
	"b_1": {
		"f": "",
		"f_1": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": ""
		}
	},
	"b_3": {
		"f": "",
		"f_1": "",
		"f_2": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": ""
		}
	}
}
"a2"json
{
	"f": "",
	"f_2": "",
	"b": {
		"f": "",
		"f_1": "",
		"f_2": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": ""
		}
	},
	"b_1": {
		"f": "",
		"f_1": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_1": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": ""
		}
	},
	"b_2": {
		"f": "",
		"f_2": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": ""
		}
	},
	"b_3": {
		"f": "",
		"c": {
			"f": "",
			"f_1": "",
			"f_2": ""
		},
		"c_3": {
			"f": "",
			"f_1": ""
		}
	}
}

About

基于json tag扩展encode规则,实现struct动态(字段的取舍)编码为不同JSON结构,更自由的选择、忽略部分字段。echo-web示范

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages