package main import ( "fmt" "github.com/mailru/easyjson/jlexer" ) func main() { var lex jlexer.Lexer lex.Consumed = "Test JSON String" if lex.UnsafeString() != "Test JSON String" { fmt.Println("Invalid JSON string") } }
package main import ( "fmt" "github.com/mailru/easyjson/jlexer" ) type Person struct { Name string `json:"name"` Age int `json:"age"` } func (p *Person) UnmarshalJSON(data []byte) error { lex := jlexer.Lexer{Data: data} for !lex.IsEnd() { field := lex.FieldName() switch string(field) { case "name": p.Name = lex.UnsafeString() case "age": p.Age = lex.Int() default: lex.Skip() } } return lex.Error() } func main() { jsonData := []byte(`{"name": "John Doe", "age": 25}`) p := &Person{} err = json.Unmarshal(jsonData, p) if err != nil { fmt.Println("Error: ", err) } fmt.Println(p.Name, p.Age) }This code snippet shows an example of how to use the jlexer package to unmarshal JSON data. In this example, we define a custom UnmarshalJSON function to parse the JSON data and fill in the fields of our Person struct. The UnsafeString() method is used to extract the value of the "name" field from the JSON data.