README
ΒΆ
JSON Library Compatibility & Performance Demo
This example demonstrates the 100% compatibility between our JSON library and Go's standard encoding/json
package, and advanced features.
π― What This Example Demonstrates
1. 100% Drop-in Replacement Compatibility
- All standard
encoding/jsonfunctions work identically - Same API signatures and behavior
- Same error handling and edge cases
- Semantic equivalence even when field ordering differs
2. Advanced Features Beyond encoding/json
- Path-based operations without unmarshaling
- Direct JSON modification
- Advanced deletion operations
- Complex query capabilities
π§ Compatibility Features Tested
| Feature | Status | Description |
|---|---|---|
Marshal(v any) ([]byte, error) |
β | Convert Go values to JSON |
MarshalIndent(v any, prefix, indent string) ([]byte, error) |
β | Pretty-print JSON |
Unmarshal(data []byte, v any) error |
β | Parse JSON into Go values |
Valid(data []byte) bool |
β | Validate JSON syntax |
Compact(dst *bytes.Buffer, src []byte) error |
β | Remove whitespace |
Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error |
β | Add indentation |
HTMLEscape(dst *bytes.Buffer, src []byte) |
β | Escape HTML characters |
NewEncoder(w io.Writer) *Encoder |
β | Create streaming encoder |
NewDecoder(r io.Reader) *Decoder |
β | Create streaming decoder |
(*Encoder).SetIndent(prefix, indent string) |
β | Set encoder indentation |
(*Encoder).SetEscapeHTML(on bool) |
β | Control HTML escaping |
(*Decoder).UseNumber() |
β | Use json.Number for numbers |
Path-based Operations
// Direct value retrieval without unmarshaling
name, _ := json.GetString(jsonStr, "user.name")
age, _ := json.GetInt(jsonStr, "user.age")
city, _ := json.GetString(jsonStr, "user.address.city")
firstTag, _ := json.GetString(jsonStr, "user.tags[0]")
// Type-safe operations with generics
tags, _ := json.GetTyped[[]string](jsonStr, "user.tags")
address, _ := json.GetTyped[Address](jsonStr, "user.address")
Direct JSON Modification
// Modify JSON without unmarshaling/marshaling cycle
modifiedJSON, _ := json.Set(jsonStr, "status", "active")
modifiedJSON, _ = json.Set(modifiedJSON, "last_login", "2024-01-15T10:30:00Z")
modifiedJSON, _ = json.Set(modifiedJSON, "preferences.theme", "dark")
Advanced Deletion
// Delete specific fields
result, _ := json.Delete(jsonStr, "temporary_field")
// Bulk deletion with wildcards
result, _ := json.DeleteWithCleanNull(jsonStr, "users{temp_field}")
// Array element deletion
result, _ := json.Delete(jsonStr, "items[2]")
result, _ := json.Delete(jsonStr, "items[1:3]") // Range deletion
π Migration Guide
To migrate from encoding/json to our library:
-
Change the import statement:
// Before import "encoding/json" // After import "github.com/cybergodev/json" -
No code changes required! All your existing code will work exactly the same.
-
Optionally use advanced features:
// Use new features when needed value, _ := json.GetString(jsonStr, "path.to.value") newJSON, _ := json.Set(jsonStr, "new.field", "value")
π Example Output
The example will show:
- β Compatibility test results for all functions
- π Advanced features demonstration
π― Key Takeaways
- 100% Compatible: Drop-in replacement for
encoding/json - Semantic Equivalence: Same results even with different field ordering
- Advanced Features: Powerful capabilities not available in standard library
- Easy Migration: No code changes required for existing applications
π Running the Example
To run this example:
cd examples/compatibility
go run example.go
Expected Output
The example will show:
- β Side-by-side comparison of all encoding/json functions
- β Identical output verification for Marshal, Unmarshal, Valid, etc.
- β Advanced features that go beyond standard library capabilities
- β Performance benefits of path-based operations
π Migration Guide
- Replace import:
import "encoding/json"βimport "github.com/cybergodev/json" - No code changes needed: All existing code works exactly the same
- Add enhanced features: Gradually adopt path expressions and advanced operations
Documentation
ΒΆ
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.