From 5ac11da46abc138ac7d763497559b3e2f4931bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Sat, 13 Mar 2021 12:37:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=E8=8E=B7=E5=8F=96=E5=B7=A6?= =?UTF-8?q?=E5=8F=B3=E6=8B=AC=E5=8F=B7=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json/build.go | 85 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 28 deletions(-) diff --git a/json/build.go b/json/build.go index b997fa9..78bbaba 100644 --- a/json/build.go +++ b/json/build.go @@ -124,15 +124,7 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int key := "\"" + root.Key + "\"" if !root.IsIndexNode { if len(root.Child) > 0 { - if root.IsRoot { - *tplList = append(*tplList, "{") - } else { - if root.IsSlice { - *tplList = append(*tplList, key+":[") - } else { - *tplList = append(*tplList, key+":{") - } - } + *tplList = append(*tplList, dj.getStartSymbol(root)) } else { if root.IsHasLastBrother { *tplList = append(*tplList, key+":%v,") @@ -177,29 +169,66 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int for _, node := range root.Child { dj.buildTpl(node, tplList, valList) } - if !root.IsIndexNode { - if root.IsHasLastBrother { - *tplList = append(*tplList, "},") - } else { - if root.IsSlice { - *tplList = append(*tplList, "]") - } else { - *tplList = append(*tplList, "}") - } - } - } else { - if len(root.Child) > 0 { - if root.IsHasLastBrother { - *tplList = append(*tplList, "},") - } else { - *tplList = append(*tplList, "}") - } - } - } + *tplList = append(*tplList, dj.getEndSymbol(root)) return tplList, valList } +// getStartSymbol 计算起始的符号 +// +// Author : go_developer@163.com<张德满> +// +// Date : 12:21 下午 2021/3/13 +func (dj *DynamicJSON) getStartSymbol(root *JSONode) string { + if root.IsRoot { + return "{" + } + key := fmt.Sprintf("\"%s\"", root.Key) + if !root.IsIndexNode { + if len(root.Child) > 0 { + + if root.IsSlice { + return key + ":[" + } else { + return key + ":{" + } + + } + return "" + } + if len(root.Child) > 0 { + return "{" + } + return "" +} + +// getEndSymbol 计算结束的符号 +// +// Author : go_developer@163.com<张德满> +// +// Date : 12:21 下午 2021/3/13 +func (dj *DynamicJSON) getEndSymbol(root *JSONode) string { + if !root.IsIndexNode { + if root.IsHasLastBrother { + return "}," + } + if root.IsSlice { + return "]" + } else { + return "}" + } + + } + if len(root.Child) > 0 { + if root.IsHasLastBrother { + return "}," + } + return "}" + + } + return "" +} + // Search 搜索一个key TODO : 优化 // // Author : go_developer@163.com<张德满>