优化自动生成json #3

Merged
zhangdeman merged 24 commits from feature/tree into master 2023-05-05 16:10:39 +08:00
Showing only changes of commit f760ab057a - Show all commits

View File

@@ -79,7 +79,7 @@ func (g *Generate) init() error {
return err return err
} }
// 获取值 // 获取值
if jsonValue, err = g.getValue(startIndex); nil != err { if jsonValue, startIndex, err = g.getValue(startIndex); nil != err {
return err return err
} }
fmt.Println(jsonKey, valueType, jsonValue, startIndex) fmt.Println(jsonKey, valueType, jsonValue, startIndex)
@@ -102,6 +102,7 @@ func (g *Generate) getKey(startIndex int) (string, int, error) {
startIndex++ startIndex++
continue continue
} }
if charStr == KeywordDoubleQuote && !hasStart { if charStr == KeywordDoubleQuote && !hasStart {
// 第一次遇见双引号 // 第一次遇见双引号
startIndex++ startIndex++
@@ -114,6 +115,14 @@ func (g *Generate) getKey(startIndex int) (string, int, error) {
break break
} }
if !hasStart {
if charStr == KeywordDoubleQuote || charStr == KeywordObjectStart || charStr == KeywordArrayStart || charStr == KeywordComma {
startIndex++
continue
}
return "", startIndex, errors.New("parse key : format is invalid")
}
if charStr == KeywordEscapeSymbol { if charStr == KeywordEscapeSymbol {
// 转义符 // 转义符
startIndex++ startIndex++
@@ -227,7 +236,7 @@ func (g *Generate) getValueType(startIndex int) (string, int, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 12:09 2023/3/30 // Date : 12:09 2023/3/30
func (g *Generate) getValue(startIndex int) (string, error) { func (g *Generate) getValue(startIndex int) (string, int, error) {
valArr := make([]string, 0) valArr := make([]string, 0)
isStart := false isStart := false
for startIndex < len(g.jsonDataByte) { for startIndex < len(g.jsonDataByte) {
@@ -262,7 +271,7 @@ func (g *Generate) getValue(startIndex int) (string, error) {
valArr = append(valArr, str) valArr = append(valArr, str)
startIndex++ startIndex++
} }
return strings.Join(valArr, ""), nil return strings.Join(valArr, ""), startIndex, nil
} }
// isObject 整体是否为对象 // isObject 整体是否为对象