Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.79k stars 215 forks source link

Be able to override JSON.parse & JSON.stringify #230

Open Toub opened 5 years ago

Toub commented 5 years ago

I perfectly understand that this library just work with JSON objects and won't support Javascript objects such as Date (https://github.com/Starcounter-Jack/JSON-Patch/issues/122 https://github.com/Starcounter-Jack/JSON-Patch/issues/183).

However, one should be able to customize the serialization implementation in order to support custom different formats, using object-to-json and json-to-object.

Currently, one can parse/stringify before and after calling JSON-Patch but it would have better performances to avoid serializing the objects multiple times.

Could you simply extract JSON.parse & JSON.stringify in a service and provide a way to override it?

function applyPatch<T>(document: T, patch: Operation[], validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean): PatchResult<T>;

=>

function applyPatch<T>(document: T, patch: Operation[], validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean, options: {
   parseJSON: (json: string) => T, 
   formatJSON: (item: T} => string
): PatchResult<T>;