Skip to content

Conversation

@ljluestc
Copy link

Feature: JSON-Iterator Integration Example & Test Fixes

Description

This PR addresses issue #2810 by providing a complete, working example of how to integrate json-iterator/go with Gin. Additionally, it includes critical fixes for existing tests that were failing or behaving inconsistently when the -tags=jsoniter build tag was active.

Changes

1. New Example

  • Location: examples/json-iterator
  • Content: Added a json_iterator_test.go to the existing example. This allows developers to verify the integration in isolation without running the entire suite.
  • Goal: Demonstrates how to replace the default encoding/json binding with json-iterator for performance improvements.

2. Critical Test Fixes

Running go test -tags=jsoniter ./... previously caused failures. The following fixes ensure full compatibility:

  • gin_integration_test.go:

    • Issue: TestRunEmpty relied on the default port :8080. When running tests in parallel or on CI/CD (like GitHub Actions), this often resulted in bind: address already in use errors.
    • Fix: The test now dynamically allocates a random available port (using :0), eliminating port conflicts.
  • binding/binding_test.go:

    • Issue: TestUriBinding failed because json-iterator behaves slightly differently than encoding/json on error. Specifically, json-iterator may allocate an empty map before returning an error, whereas the standard library leaves it as nil.
    • Fix: Relaxed the assertion to accept either nil or an empty map when an error occurs, preserving correctness for both engines.
  • context_test.go:

    • Issue: TestContextBindRequestTooLarge expected a 413 Request Entity Too Large status code. However, the underlying json-iterator library returns a 400 Bad Request when the body size limit is exceeded.
    • Fix: Updated the test to explicitly accept 400 Bad Request when the jsoniter build tag is active, matching the library's actual behavior.

How to Verify

Run the Example

go run -tags=jsoniter examples/json-iterator/main.go
# Expected Output: Server starts on :8080

Run All Tests (with json-iterator)

go test -v -tags=jsoniter ./...
# Expected Output: All tests pass (ok)

Run Standard Tests (Regression Check)

go test -v ./...
# Expected Output: All tests pass (ok)

Checklist

  • Open pull request against the master branch.
  • All tests pass locally with -tags=jsoniter.
  • Standard tests pass (no regressions).
  • Documentation/Examples added.

Fixes #2810

@ljluestc ljluestc marked this pull request as draft January 24, 2026 17:40
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.

Need help with an example usecase of integrating and using json-iterator with gin-gonic

1 participant