-
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
Showing
6 changed files
with
93 additions
and
28 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
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,29 @@ | ||
from __future__ import annotations | ||
from typing import TYPE_CHECKING, Any | ||
import json | ||
from attrs import define, field | ||
from griptape.artifacts import TextArtifact | ||
|
||
if TYPE_CHECKING: | ||
from griptape.artifacts import BaseArtifact | ||
|
||
|
||
@define | ||
class JsonArtifact(TextArtifact): | ||
value: Any = field(converter=json.dumps, metadata={"serializable": True}) # pyright: ignore reportRedeclaration | ||
|
||
@property | ||
def value(self) -> Any: | ||
return json.loads(self.value) | ||
|
||
def to_text(self) -> str: | ||
return json.dumps(self.value) | ||
|
||
def __add__(self, other: BaseArtifact) -> JsonArtifact: | ||
new = json.loads(other.to_text()) | ||
return JsonArtifact({**self.value, **new}) | ||
|
||
def __eq__(self, value: object) -> bool: | ||
if isinstance(value, str): | ||
return self.to_text() == value | ||
return self.value == value |
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,34 @@ | ||
import json | ||
import pytest | ||
from griptape.artifacts import JsonArtifact, TextArtifact | ||
|
||
|
||
class TestJsonArtifact: | ||
def test_value_type_conversion(self): | ||
assert JsonArtifact({"foo": "bar"}).value == json.loads(json.dumps({"foo": "bar"})) | ||
string = json.dumps({"foo": {}}) | ||
print(string) | ||
string = json.loads(string) | ||
print(string) | ||
|
||
def test___add__(self): | ||
assert JsonArtifact({"foo": "bar"}) + JsonArtifact({"value": "baz"}) == JsonArtifact( | ||
{"foo": "bar", "value": "baz"} | ||
) | ||
|
||
new = JsonArtifact({"foo": "bar"}) + JsonArtifact({"value": "baz"}) | ||
assert new == {"foo": "bar", "value": "baz"} | ||
|
||
assert JsonArtifact({"foo": "bar"}) + JsonArtifact({"foo": "baz"}) == JsonArtifact({"foo": "baz"}) | ||
with pytest.raises(json.JSONDecodeError): | ||
JsonArtifact({"foo": "bar"}) + TextArtifact("invalid json") | ||
|
||
def test___eq__(self): | ||
assert JsonArtifact({"foo": "bar"}) == json.dumps({"foo": "bar"}) | ||
assert JsonArtifact({"foo": "bar"}) == {"foo": "bar"} | ||
|
||
def test_to_text(self): | ||
assert JsonArtifact({"foo": "bar"}).to_text() == json.dumps({"foo": "bar"}) | ||
|
||
def test_to_bytes_encoding(self): | ||
assert JsonArtifact({"foo": "bar"}).to_bytes() == json.dumps({"foo": "bar"}).encode() |