Hey there! If you're looking to supercharge your JSON serialization in Node.js, fast-json-stringify is a fantastic option. The key to getting the most out of this library is understanding how to create effective schemas.
In this notebook, we'll dive deep into schema creation for fast-json-stringify - from basic types to complex nested structures, references, patterns, and more. You'll learn how to craft schemas that not only boost performance but also handle all your data serialization needs.
$ npm install fast-json-stringify added 11 packages in 2s 33 packages are looking for funding run `npm fund` for details
Fast-json-stringify loaded successfully!
Let's start with the fundamental schema types in fast-json-stringify. The library supports all the standard JSON Schema types.
String schema result: {"name":"Product","description":"A great product"}
Number schema result: {"integer":42,"float":3.14159}
Boolean schema result: {"isActive":true,"hasFeature":false}
Null schema results: {"value":null,"optionalValue":null} {"value":null,"optionalValue":"I have a value"}
Arrays are common in JSON data. Let's see how to define schemas for different types of arrays.
String array schema result: {"tags":["javascript","nodejs","json"]}
Object array schema result: {"users":[{"id":1,"name":"Alice","email":"alice@example.com"},{"id":2,"name":"Bob","email":"bob@example.com"},{"id":3,"name":"Charlie","email":"charlie@example.com"}]}
Tuple schema result: {"coordinates":[10.5,20.3,"Point A"]}
Real-world JSON often contains deeply nested objects. Let's see how to handle them.
Nested object schema result: {"user":{"id":42,"name":"John Doe","contact":{"email":"john@example.com","phone":"555-1234","address":{"street":"123 Main St","city":"Anytown","zipCode":"12345"}}}}
For complex schemas, you can use references to avoid repetition and make your schemas more maintainable.
Schema with references result: {"name":"Jane Smith","homeAddress":{"street":"456 Oak Ave","city":"Someville","state":"CA","zipCode":"94321"},"workAddress":{"street":"789 Corporate Blvd","city":"Businesstown","state":"CA","zipCode":"94567"}}
Sometimes you need to handle objects with dynamic keys. Pattern properties let you define schemas for keys that match a pattern.
Pattern properties schema result: {"name":"System Stats","metric_cpu":45.2,"metric_memory":72.8,"metric_disk":30.5,"flag_active":true,"flag_warning":false}
You can control how unknown properties are handled using the additionalProperties
keyword.
Schema with additional properties allowed: {"id":123,"name":"Product","price":99.99,"inStock":true}
Schema with additional properties disallowed: {"id":123,"name":"Product"}
Schema with typed additional properties: {"id":123,"name":"Product","category":"Electronics","description":"A great product"}
While fast-json-stringify doesn't validate data, the required
keyword can help document which properties are expected.
Let's put it all together with a real-world example of an API response schema.
Successful API response: {"success":true,"timestamp":"2025-03-13T19:02:07.135Z","data":{"users":[{"id":1,"username":"user1","email":"user1@example.com","role":"admin"},{"id":2,"username":"user2","email":"user2@example.com","role":"user"},{"id":3,"username":"user3","email":"user3@example.com","role":"user"}],"pagination":{"page":1,"pageSize":10,"totalPages":3,"totalItems":25}},"error":null}
Error API response: {"success":false,"timestamp":"2025-03-13T19:02:07.822Z","data":{"users":[],"pagination":{"page":0,"pageSize":0,"totalPages":0,"totalItems":0}},"error":{"code":"AUTH_ERROR","message":"Authentication failed. Please log in again."}}
When creating schemas for fast-json-stringify, keep these performance tips in mind:
additionalProperties: false
can improve performance by ignoring unexpected fields.Creating effective schemas is the key to getting the most out of fast-json-stringify. With the techniques we've covered, you can build schemas for everything from simple objects to complex nested structures with dynamic properties.
Remember that fast-json-stringify is focused on serialization performance, not validation. If you need validation too, consider using a library like Ajv alongside fast-json-stringify.