-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4195f1e
commit ca5407c
Showing
7 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from pathlib import Path | ||
|
||
from griptape.loaders import JsonLoader | ||
|
||
# Load an image from disk | ||
JsonLoader().load("tests/resources/test.json") | ||
|
||
# You can also pass a Path object | ||
JsonLoader().load(Path("tests/resources/test.json")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
|
||
from attrs import define | ||
|
||
from griptape.artifacts import JsonArtifact | ||
from griptape.loaders import BaseFileLoader | ||
|
||
|
||
@define | ||
class JsonLoader(BaseFileLoader[JsonArtifact]): | ||
def parse(self, data: bytes) -> JsonArtifact: | ||
return JsonArtifact(json.loads(data), encoding=self.encoding) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "John Doe", | ||
"age": 30, | ||
"email": "[email protected]", | ||
"isActive": true, | ||
"roles": [ | ||
"user", | ||
"admin" | ||
], | ||
"address": { | ||
"street": "123 Main St", | ||
"city": "Anytown", | ||
"state": "CA", | ||
"postalCode": "12345" | ||
}, | ||
"phoneNumbers": [ | ||
{ | ||
"type": "home", | ||
"number": "555-555-5555" | ||
}, | ||
{ | ||
"type": "work", | ||
"number": "555-555-1234" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pytest | ||
|
||
from griptape.loaders import JsonLoader | ||
|
||
|
||
class TestJsonLoader: | ||
@pytest.fixture(params=["ascii", "utf-8", None]) | ||
def loader(self, request): | ||
encoding = request.param | ||
if encoding is None: | ||
return JsonLoader() | ||
else: | ||
return JsonLoader(encoding=encoding) | ||
|
||
@pytest.fixture(params=["path_from_resource_path"]) | ||
def create_source(self, request): | ||
return request.getfixturevalue(request.param) | ||
|
||
def test_load(self, loader, create_source): | ||
source = create_source("test.json") | ||
|
||
artifact = loader.load(source) | ||
|
||
assert artifact.value["name"] == "John Doe" | ||
assert artifact.encoding == loader.encoding | ||
|
||
def test_load_collection(self, loader, create_source): | ||
resource_paths = ["test.json"] | ||
sources = [create_source(resource_path) for resource_path in resource_paths] | ||
|
||
collection = loader.load_collection(sources) | ||
|
||
keys = {loader.to_key(source) for source in sources} | ||
assert collection.keys() == keys | ||
|
||
key = next(iter(keys)) | ||
artifact = collection[key] | ||
|
||
assert artifact.encoding == loader.encoding |