Skip to content

Commit ebd7524

Browse files
authored
feat: Add crJson function on Reader (#279)
* fix: Binding for crJson * Update c2pa.py
1 parent 2dc5036 commit ebd7524

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "c2pa-python"
7-
version = "0.32.13"
7+
version = "0.32.14"
88
requires-python = ">=3.10"
99
description = "Python bindings for the C2PA Content Authenticity Initiative (CAI) library"
1010
readme = { file = "README.md", content-type = "text/markdown" }

src/c2pa/c2pa.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
'c2pa_reader_from_manifest_data_and_stream',
4949
'c2pa_reader_json',
5050
'c2pa_reader_detailed_json',
51+
'c2pa_reader_crjson',
5152
'c2pa_reader_resource_to_stream',
5253
'c2pa_reader_from_context',
5354
'c2pa_reader_with_stream',
@@ -561,6 +562,9 @@ def _setup_function(func, argtypes, restype=None):
561562
_setup_function(
562563
_lib.c2pa_reader_detailed_json, [
563564
ctypes.POINTER(C2paReader)], ctypes.c_void_p)
565+
_setup_function(
566+
_lib.c2pa_reader_crjson, [
567+
ctypes.POINTER(C2paReader)], ctypes.c_void_p)
564568
_setup_function(_lib.c2pa_reader_resource_to_stream, [ctypes.POINTER(
565569
C2paReader), ctypes.c_char_p, ctypes.POINTER(C2paStream)], ctypes.c_int64)
566570
_setup_function(
@@ -2631,6 +2635,27 @@ def detailed_json(self) -> str:
26312635

26322636
return _convert_to_py_string(result)
26332637

2638+
def crjson(self) -> str:
2639+
"""Get the manifest store as a crJSON string.
2640+
2641+
crJSON is a standardized JSON format for C2PA manifest data. This
2642+
call yields empty JSON ("{}") when there are no Content Credentials.
2643+
2644+
Returns:
2645+
The manifest store as a crJSON string.
2646+
2647+
Raises:
2648+
C2paError: If the Reader has been closed or the underlying C
2649+
call returns null.
2650+
"""
2651+
2652+
self._ensure_valid_state()
2653+
2654+
result = _lib.c2pa_reader_crjson(self._handle)
2655+
_check_ffi_operation_result(result, "Error parsing crJSON")
2656+
2657+
return _convert_to_py_string(result)
2658+
26342659
def _get_manifest_field(self, extractor):
26352660
"""Extract a field from (cached) manifest data, or None if unavailable.
26362661

tests/test_unit_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,19 @@ def test_stream_read_detailed_and_parse(self):
271271
title = manifest_store["manifests"][manifest_store["active_manifest"]]["claim"]["dc:title"]
272272
self.assertEqual(title, DEFAULT_TEST_FILE_NAME)
273273

274+
def test_stream_read_crjson_and_parse(self):
275+
with open(self.testPath, "rb") as file:
276+
reader = Reader("image/jpeg", file)
277+
crjson = reader.crjson()
278+
self.assertTrue(crjson)
279+
json.loads(crjson)
280+
281+
def test_stream_read_crjson_path_only(self):
282+
with Reader(self.testPath) as reader:
283+
crjson = reader.crjson()
284+
self.assertTrue(crjson)
285+
json.loads(crjson)
286+
274287
def test_stream_read_string_stream_path_only(self):
275288
with Reader(self.testPath) as reader:
276289
json_data = reader.json()

0 commit comments

Comments
 (0)