Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "yaml-ld"
version = "1.1.16"
version = "1.1.17"
description = "YAML-LD for Python"
authors = ["Anatoly Scherbakov <altaisoft@gmail.com>"]
license = "MIT"
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-imports = 15
5 changes: 3 additions & 2 deletions yaml_ld/document_loaders/content_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from yarl import URL

from yaml_ld.document_parsers.base import BaseDocumentParser
from yaml_ld.document_parsers.json_parser import JSONDocumentParser
from yaml_ld.document_parsers.rdf_xml_parser import RDFXMLParser
from yaml_ld.document_parsers.turtle_parser import TurtleParser
from yaml_ld.document_parsers.yaml_parser import YAMLDocumentParser
Expand Down Expand Up @@ -95,8 +96,8 @@ def parser_by_content_type_map():
)

return {
'application/json': YAMLDocumentParser,
APPLICATION_LD_JSON: YAMLDocumentParser,
'application/json': JSONDocumentParser,
APPLICATION_LD_JSON: JSONDocumentParser,
'application/yaml': YAMLDocumentParser,
'application/x-yaml': YAMLDocumentParser,
'application/ld+yaml': YAMLDocumentParser,
Expand Down
44 changes: 44 additions & 0 deletions yaml_ld/document_parsers/json_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import io
import json

from yaml_ld.document_parsers.base import (
BaseDocumentParser,
DocumentLoaderOptions,
)
from yaml_ld.errors import (
DocumentIsScalar,
InvalidEncoding,
LoadingDocumentFailed,
)
from yaml_ld.models import JsonLdRecord


def ensure_not_scalar(document) -> JsonLdRecord | list[JsonLdRecord]:
"""Ensure document is not a scalar value."""
if not isinstance(document, (dict, list)):
raise DocumentIsScalar(document)

return document
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated ensure_not_scalar function already exists

Medium Severity

The ensure_not_scalar function is duplicated. An identical implementation already exists in yaml_ld/document_parsers/yaml_parser.py (lines 25-30). The markdown_parser.py already imports and uses the existing function from yaml_parser.py, which is the correct pattern to follow.

Fix in Cursor Fix in Web



class JSONDocumentParser(BaseDocumentParser):
"""Parse JSON and JSON-LD documents."""

def __call__(
self,
data_stream: io.BytesIO,
source: str,
options: DocumentLoaderOptions,
) -> JsonLdRecord | list[JsonLdRecord]:
"""Parse JSON document into LD."""
try:
text = data_stream.read().decode('utf-8')
except UnicodeDecodeError as unicode_decode_error:
raise InvalidEncoding() from unicode_decode_error

try:
document = json.loads(text)
except json.JSONDecodeError as json_error:
raise LoadingDocumentFailed(path=source) from json_error

return ensure_not_scalar(document)
Loading