Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: serializer utilities PTL-1699 #2011

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: 'Serializer utilities'
sidebar_label: 'Serializer utilities'
id: serializer-util
keywords: [utils, utility, serializer, serialization, deserialization, complex, robust]
tags:
- utils
- utility
- serializer
- serialization
- deserialization
- complex
- robust
Gareth-Spencer-Genesis marked this conversation as resolved.
Show resolved Hide resolved
---

This contains a comprehensive JSON serialization and deserialization solution, designed to support complex data structures, including those involving big integers and custom object types, ensuring robust and flexible handling of JSON data.

## Key Features

- **`customNumberParser` Function:** Enhances the parsing of numeric values, supporting safe integers, floating-point numbers, and BigInt values.
- **`JSONSerializer` Interface:** Defines methods for serializing objects to JSON strings and deserializing JSON strings to JavaScript objects, with support for HTTP response objects and message event data.
- **`DefaultJSONSerializer` Class:** Implements the `JSONSerializer` interface, providing default serialization and deserialization behaviors that can be customized via the `JSONSerializerConfig`.
- **`JSONReplacer` and `JSONReviver` Functions:** Facilitate custom serialization and deserialization processes, allowing for the handling of special types, such as dates and NaN values, and the exclusion of properties with specific key prefixes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we get an example of tis this please?


## Examples

### JSON Serialization/Deserialization

The `JSONSerializer` DI Token enable the serialization and deserialization of complex objects, including BigInt values and custom object types, ensuring that JSON data is accurately represented in JavaScript.

Gareth-Spencer-Genesis marked this conversation as resolved.
Show resolved Hide resolved
```typescript
import { JSONSerializer } from '@genesislcap/foundation-utils';

Gareth-Spencer-Genesis marked this conversation as resolved.
Show resolved Hide resolved
export class MyService extends FASTElement {
@JSONSerializer serializer!: JSONSerializer; // DI token

const object = { date: new Date(), bigNumber: BigInt(12345678901234567890) };
const jsonString = serializer.serialize(object);
console.log(jsonString); // Outputs a JSON string with custom formatting

const parsedObject = serializer.deserialize(jsonString);
console.log(parsedObject); // Outputs the original object with date and bigNumber correctly parsed
}
```

### Configuring the Serializer

The default serializer behavior can be customized by providing a `JSONSerializerConfig` instance, which defines custom parsing and stringifying logic, including the handling of BigInt values and special object types.

### Custom Number Parsing

The `customNumberParser` function is crucial for correctly handling numerical values during deserialization, ensuring that numbers are accurately represented in the resulting JavaScript objects.

```typescript
import { customNumberParser } from '@genesislcap/foundation-utils';

const parsedNumber = customNumberParser("12345678901234567890");
console.log(parsedNumber); // Outputs a BigInt
```

## Key Points

- Utilize the provided serializer and parser utilities for consistent handling of JSON data, especially when dealing with large numbers or custom object types.
- Leverage the DI system to manage serializer instances, ensuring that custom configurations are centrally managed and easily accessible.
- Employ the `JSONReplacer` and `JSONReviver` functions for custom serialization and deserialization needs, such as handling dates or excluding private properties.