Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-hykin authored May 26, 2023
1 parent cb9219f commit 9930e75
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ json.dumps(list_containing_your_object)
```
And it simply throws an error no matter how you customize your object

# How do I use this?
# How do I use this for my class?

`pip install json-fix`

Expand All @@ -30,7 +30,16 @@ class YOUR_CLASS:
return "a built-in object that is natually json-able"
```

If you want control over classes that are defined externally (datetime, numpy.array, tensor, etc), use the override_table
# How do I change how someone elses class is jsonified?

There's 2 ways; the aggressive `override_table` or the more collaboration-friendly `fallback_table`.

## Override Table

If a pip module defines a class, you can control how it is json-dumped, even if they defined a `.__json__()` method, by using `json.override_table`.
- Note! The "when" (in when-a-rule-is-added) can be very important. Whatever rule was most-recently added will have the highest priority. So, even if a pip module uses the override table, you can override their override by doing `import that_module` and THEN adding your rule to the override table.
- Note 2! The override table is capable of changing how built-in types are dumped, be careful!

```python
import json_fix # import this before the JSON.dumps gets called
import json
Expand All @@ -43,5 +52,11 @@ class_checker = lambda obj: isinstance(obj, SomeClassYouDidntDefine)
# then assign it to a function that does the converting
json.override_table[class_checker] = lambda obj_of_that_class: json.loads(obj_of_that_class.to_json())

json.dumps([ 1, 2, SomeClassYouDidntDefine() ], indent=2)
json.dumps([ 1, 2, SomeClassYouDidntDefine() ], indent=2) # dumps as expected
```

## Fallback Table

One of the best uses of the fallback table is safely adding wide-sweeping defaults. For example, making all custom classes default to `.__dict__` even if they don't have a .__json__() method, or checking for a `.__repr__()` method and using that.

You can add to the `json.fallback_table` just like the `override_table`. The only difference is; if there's nothing in the override table, and the class doesn't have a `.__json__()` method, then the fallback_table will be used.

0 comments on commit 9930e75

Please sign in to comment.