github.com/sandwich-go/boost@v1.3.29/misc/xgen/json2lua.go (about)

     1  package xgen
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"github.com/buger/jsonparser"
     8  	"github.com/sandwich-go/boost/xpanic"
     9  )
    10  
    11  func MustJSON2Lua(inBytes []byte) []byte {
    12  	gen := NewGeneratorWithSpaceIndent(4)
    13  	gen.P("return ")
    14  	json2LuaWithType(inBytes, jsonparser.Object, gen, 1)
    15  	return gen.Bytes()
    16  }
    17  
    18  func json2LuaWithType(inBytes []byte, dataType jsonparser.ValueType, gen *Gen, indent int) {
    19  	if dataType == jsonparser.Object {
    20  		gen.P("{")
    21  		gen.In()
    22  		err := jsonparser.ObjectEach(inBytes, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
    23  			if dataType == jsonparser.Object {
    24  				subGen := NewGeneratorWithSpaceIndent(4)
    25  				subGen.SetPrefixIndent(indent - 1)
    26  				subGen.In()
    27  				json2LuaWithType(value, dataType, subGen, indent+1)
    28  				gen.PFormat("[\"%s\"] = %s,", string(key), strings.TrimSpace(strings.TrimRight(subGen.String(), "\n")))
    29  			} else if dataType == jsonparser.Array {
    30  				subGen := NewGeneratorWithSpaceIndent(4)
    31  				subGen.SetPrefixIndent(indent - 1)
    32  				subGen.In()
    33  				json2LuaWithType(value, dataType, subGen, indent+1)
    34  				gen.PFormat("[\"%s\"] = %s,", string(key), strings.TrimSpace(strings.TrimRight(subGen.String(), "\n")))
    35  			} else if dataType == jsonparser.String {
    36  				gen.PFormat("[\"%s\"] = \"%s\",", string(key), string(value))
    37  			} else if dataType == jsonparser.Boolean || dataType == jsonparser.Number || dataType == jsonparser.Null || dataType == jsonparser.Unknown {
    38  				gen.PFormat("[\"%s\"] = %s,", string(key), string(value))
    39  			} else if dataType == jsonparser.NotExist {
    40  				return errors.New("unsupported type")
    41  			}
    42  			return nil
    43  		})
    44  		xpanic.WhenError(err)
    45  		gen.Out()
    46  		gen.P("}")
    47  	}
    48  	if dataType == jsonparser.Array {
    49  		gen.P("{")
    50  		gen.In()
    51  		_, err := jsonparser.ArrayEach(inBytes, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
    52  			if dataType == jsonparser.Object || dataType == jsonparser.Array {
    53  				subGen := NewGeneratorWithSpaceIndent(4)
    54  				subGen.SetPrefixIndent(indent - 1)
    55  				subGen.In()
    56  				json2LuaWithType(value, dataType, subGen, indent+1)
    57  				gen.PFormat("%s,", strings.TrimSpace(strings.TrimRight(subGen.String(), "\n")))
    58  			} else if dataType == jsonparser.String {
    59  				gen.PFormat("\"%s\",", string(value))
    60  			} else if dataType == jsonparser.Boolean || dataType == jsonparser.Number || dataType == jsonparser.Null || dataType == jsonparser.Unknown {
    61  				gen.PFormat("%s,", string(value))
    62  			} else if dataType == jsonparser.NotExist {
    63  				err = errors.New("unsupported type")
    64  			}
    65  		})
    66  		xpanic.WhenError(err)
    67  		gen.Out()
    68  		gen.P("}")
    69  	}
    70  }