Skip to content

feat: add JSON parser implementation with switchable libraries - #809

Merged
mihir20 merged 29 commits into
mainfrom
mihir/jsonparser
Jul 2, 2025
Merged

mihir20 merged 29 commits into
mainfrom
mihir/jsonparser

Conversation

@mihir20

@mihir20 mihir20 commented May 20, 2025 •

Copy link
Copy Markdown
Contributor

Description

add JSON parser implementation with switchable libraries

Linear Ticket

  • pipe-2089

Security

  • The code changed/added as part of this pull request won't create any security issues with how the software is being used.

@mihir20
mihir20 requested a review from fracasula May 21, 2025 09:31
Comment thread jsonparser/jsonparser.go Resolved
Comment thread jsonparser/tidwall.go Resolved
@mihir20
mihir20 marked this pull request as draft May 22, 2025 06:33
@mihir20

mihir20 commented May 22, 2025

Copy link
Copy Markdown
Contributor Author

converting back to draft, I have some optimisations that I would try before requesting review again

Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/tidwall.go Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the validation really necessary or should we leave this responsibility to the devs? Imagine having the same main JSON document and from the same data []byte you might want to get multiple values:

var data[]byte
name := jp.GetString(data, "firstName") // it will validate data
surname := jp.GetString(data, "lastName") // it will validate data again1

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking, instead of just getting, setting and deleting, perhaps this pacakge should expose also a validate function. It doesn't matter if not all libs support it. If you set the grafana one and it doesn't have it (just saying I haven't verified), under the hood it can simply fallback to tidwall which supports validations.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have validation support in jsonrs maybe we can add there. wdyt?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's cool if either one of the two libs have Valid([]byte) then we have a way to do it 👍

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mihir20 can we have a Valid function then? I know jsonparser doesn't have it but we can use the tidwall one always, even if the user selected jsonparser as the preferred library.

Comment thread jsonparser/jsonparser_benchmark_test.go Resolved
@mihir20
mihir20 force-pushed the mihir/jsonparser branch 2 times, most recently from fb894c7 to 42eb852 Compare June 12, 2025 12:38
@mihir20
mihir20 marked this pull request as ready for review June 12, 2025 12:53
@mihir20
mihir20 requested review from atzoum and fracasula June 12, 2025 12:54
Comment thread jsonparser/grafana.go Resolved
Comment thread jsonparser/grafana.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/switcher.go Resolved
@mihir20
mihir20 force-pushed the mihir/jsonparser branch from e9ccf4e to a4d334c Compare June 24, 2025 06:51
@mihir20
mihir20 requested a review from fracasula June 24, 2025 07:03
Comment thread jsonparser/jsonparser.go Resolved
Comment thread jsonparser/jsonparser.go Resolved
Comment thread jsonparser/jsonparser.go Resolved
Comment thread jsonparser/jsonparser.go Resolved
Comment thread jsonparser/jsonparser.go Resolved
Comment thread jsonparser/jsonparser.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/jsonparser.go Resolved
Comment thread jsonparser/jsonparser.go Resolved
Comment thread jsonparser/tidwall.go Resolved
mihir20 added 5 commits July 1, 2025 12:58
- Introduce common error variables for consistent error reporting- Enhance error messages with more context for easier debugging
- Refactor error checks to use new error variables
- Update function comments to include error handling information
- Add new error types for specific parsing issues
- Refactor GetValue methods to use any instead of interface{}
- Remove unnecessary unmarshalling for complex types
- Update tests to reflect new error handling
- Set GrafanaLib as the default JSON parser
…dling

- Simplify GetValue function signature to return []byte instead of any
- Remove type conversion logic from grafanaJSONParser and tidwallJSONParser
- Update jsonparser interface to reflect new []byte return type- Improve documentation for getter interface methods
- Modify tests to expect []byte instead of string/float64/bool(nil) results
- Update Tidwall getter implementation to return []byte for string values- Adjust test cases to cover new byte slice return type for all value types
@mihir20
mihir20 force-pushed the mihir/jsonparser branch from 33cd2f3 to eff39dd Compare July 1, 2025 09:20
- Update GetValue to return raw string values including quotes
- Add dtype parameter to identify string type in grafana.go
- Update jsonparser.go with more detailed function comments
- Adjust tests to reflect new string value handling
- Remove unnecessary string handling in tidwall.go
@mihir20
mihir20 force-pushed the mihir/jsonparser branch from eff39dd to 181b88f Compare July 1, 2025 09:30
- Add new SafeGetter interface with methods like GetValueOrEmpty and GetBooleanOrFalse
- Implement SafeGetter methods in grafanaJSONParser and tidwallJSONParser
- Update switcher to use SafeGetter
- Rename 'keys' parameter to 'path' in all getter methods for consistency
@mihir20
mihir20 force-pushed the mihir/jsonparser branch from d762abf to bb5ee77 Compare July 1, 2025 11:06
…ndling

- Add support for string values in soft getter methods
- Implement GetValueOrEmpty, GetBooleanOrFalse, GetIntOrZero, GetFloatOrZero, and GetStringOrEmpty methods
- Update tests to cover new soft getter functionality
- Modify Switcher to support configurable soft getter library
@mihir20
mihir20 requested a review from atzoum July 1, 2025 12:35
Comment thread jsonparser/jsonparser.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/jsonparser.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/tidwall.go Resolved
mihir20 added 5 commits July 2, 2025 16:08
…nt data types

- Update GetValueOrEmpty to return raw value instead of string
- Refactor GetBoolean, GetInt, GetFloat, and GetString to use new parsing functions
- Add new functions to parse bool, int, float, and string values from raw data
- Remove unnecessary type checks in Tidwall parser
- Add support for parsing string boolean values in Grafana library
- Optimize boolean, integer and float parsing for both libraries
- Update benchmarks to include new test cases
- Improve test coverage for GetBooleanOrFalse, GetIntOrZero, GetFloatOrZero and GetStringOrEmpty methods
- Update comments for GetBooleanOrFalse, GetIntOrZero, GetFloatOrZero, and GetStringOrEmpty methods
- Add examples to illustrate behavior with different input types
- Clarify what is considered "parsable" for each method
- Update documentation in both grafana.go and jsonparser.go files
…g keys

- Add tests to check behavior when empty byte slice is provided
- Add tests to check behavior when no key is provided
- Cover edge cases for all data types (value, boolean, int, float, string)
- Add checks for empty path and keys in various JSON parsing functions
- Return appropriate error messages for empty path and keys
- Update tests to cover new validation cases
- Refactor tidwallJSONParser to use path instead of keys
@mihir20
mihir20 requested a review from atzoum July 2, 2025 13:02
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/tidwall.go Resolved
Comment thread jsonparser/grafana.go

// If the value is a string, wrap it in quotes as the library is specifically stripping quotes from string value
if dtype == jsonparser.String {
return []byte("\"" + string(value) + "\"")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to escape doublequotes in the string?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raw value would have quotes

@fracasula fracasula left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks pretty good, looking forward to use it. Well done 👍

Comment thread jsonparser/switcher_test.go Resolved
Comment thread jsonparser/jsonparser.go Resolved
- Add import for github.com/samber/lo package- Update error messages to be more informative
- Refactor code to use more descriptive variable names
- Improve comments and documentation
- Simplify some conditional checks
Comment thread jsonparser/tidwall.go Resolved
mihir20 added 2 commits July 2, 2025 21:01
…ions

- Improve path construction logic to handle empty keys and array indices
…ON bytes- Update test cases to use raw string literals (`...`) instead of interpreted string literals ("...") for JSON bytes- This change improves code readability and eliminates the need for escape characters
@mihir20
mihir20 merged commit d6a8eb1 into main Jul 2, 2025
@mihir20
mihir20 deleted the mihir/jsonparser branch July 2, 2025 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants