Code Monkey home page Code Monkey logo

json's Introduction

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": ""
		}
	}
}

json's People

Contributors

hb-chen avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.