JsonDifferently
¶
- class differently.JsonDifferently(a: Optional[Any], b: Optional[Any], color: bool = True)¶
Visualises the differences between two objects as JSON.
Use the string representation or
render()
to render.- Parameters
a – First object
b – Second object
color – Include or exclude colour formatting (default is True)
Example
from differently import JsonDifferently from typing import List, TypedDict class PersonDict(TypedDict): name: str movies: List[str] a: PersonDict = { "name": "Bobby Pringles", "movies": ["Fire Everywhere", "The World Is Exploding"], } b: PersonDict = { "name": "Susan Cheddar", "movies": ["The World Is Exploding", "Watch Out For The Moon"], } diff = JsonDifferently(a, b, color=False) print(diff)
{ = { "movies": [ = "movies": [ "Fire Everywhere", x "The World Is Exploding" ~ "The World Is Exploding", > "Watch Out For The Moon" ], = ], "name": "Bobby Pringles" ~ "name": "Susan Cheddar" } = }
- static load(i: Union[pathlib.Path, str]) Any ¶
Deserialises JSON.
- Parameters
i – If i is a Path then deserialises the file, If i is a str then deserialises i.
- Returns
Deserialised object
- Raises
DeserializationError – raised if deserialisation fails
- render(writer: IO[str]) None ¶
Renders the visualisation to writer.
- Parameters
writer – Writer
Example
from differently import JsonDifferently from io import StringIO from typing import List, TypedDict class PersonDict(TypedDict): name: str movies: List[str] a: PersonDict = { "name": "Bobby Pringles", "movies": ["Fire Everywhere", "The World Is Exploding"], } b: PersonDict = { "name": "Susan Cheddar", "movies": ["The World Is Exploding", "Watch Out For The Moon"], } diff = JsonDifferently(a, b, color=False) writer = StringIO() diff.render(writer) print(writer.getvalue())
{ = { "movies": [ = "movies": [ "Fire Everywhere", x "The World Is Exploding" ~ "The World Is Exploding", > "Watch Out For The Moon" ], = ], "name": "Bobby Pringles" ~ "name": "Susan Cheddar" } = }