data := `{ "name": "John Doe", "age": 30 }` lex := jlexer.Lexer{Data: []byte(data)} lex.WantSymbol('{') for !lex.IsEnd() { key := lex.UnsafeString() lex.WantColon() value := lex.String() fmt.Printf("%s=%s\n", key, value) lex.WantComma() } lex.WantSymbol('}')
data := `[{ "name": "John Doe", "age": 30 }, { "name": "Jane Doe", "age": 25 }]` lex := jlexer.Lexer{Data: []byte(data)} lex.WantSymbol('[') for !lex.IsEnd() { lex.WantSymbol('{') for !lex.IsEnd() { key := lex.UnsafeString() lex.WantColon() value := lex.String() fmt.Printf("%s=%s\n", key, value) lex.WantComma() } lex.WantSymbol('}') lex.WantComma() } lex.WantSymbol(']')This code creates a JSON array with object elements and uses the lexer to parse it. The lexer starts by checking if the first token is an opening bracket. It then reads the objects in the array until it encounters the closing bracket. Package Library: The package library is go-github.com/mailru/easyjson/jlexer. It is a fast and memory-efficient JSON deserializer that generates Go code from JSON schema.