bytedance / sonic

A blazingly fast JSON serializing & deserializing library
Apache License 2.0
6.79k stars 333 forks source link

Checking if a field has a certain JSON type #652

Open Azmisov opened 3 months ago

Azmisov commented 3 months ago

I'd like to parse unstructured JSON. For example, a field that is either a string or []string. As a suggestion, I feel there is the need for IsString, IsArray, etc methods. These would theoretically allow checking the type of an AstNode in a performant way. Currently, I am using two techniques to accomplish this, both of which seem poor:

  1. Call String or Array and check for errors
  2. Call Interface, then use reflection to check the type

If an IsString, etc method would be no more performant than either of these techniques, feel free to ignore my suggestion.

AsterDY commented 3 months ago

Why can ast.Node.Type() not satisfy it?