Skip to content

Fix roundtrip lang literal conversion#146

Merged
kisoso merged 3 commits intoVisualMeaning:masterfrom
kisoso:fix/lang-escape
Feb 16, 2026
Merged

Fix roundtrip lang literal conversion#146
kisoso merged 3 commits intoVisualMeaning:masterfrom
kisoso:fix/lang-escape

Conversation

@kisoso
Copy link
Contributor

@kisoso kisoso commented Feb 11, 2026

For localized string literal.n3() might not be the best representation in json model.
For example, the output of Cyrillic о is '"\u043b"@bg'
The proposed casting to term.obj resolves to '"\\u043b"@bg' (the language tagged string is serialized) which is more inline with how we feed the string to transform (.as_json - applying json.dumps()) and how it is consumed (JSON.parse()). The simplest way of converting any datatype into a string is stringifying it.

The more important bit is the casting being invertible, allowing a process where a runner outputs a model.json that is sequentially used as input for a second run. Each time converting data to rdflib entities then exported to model.json in string format.

https://visual-meaning.atlassian.net/browse/SMP-3764
@VisualMeaning/devs

'"path\\\\with\\\\backslashes"@en'),
(rdflib.Literal('mixed: "quotes"\nand newlines\tand tabs', lang='en'),
'"mixed: \\"quotes\\"\\nand newlines\\tand tabs"@en'),
(rdflib.Literal('\u043e', lang='bg'), '"\\u043e"@bg'),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only this test is updated.

@@ -422,3 +427,32 @@ def test_from_identifier_lang_obj(self):
self.resolver.from_identifier('{"k": "v"}@en'),
rdflib.Literal('{"k": "v"}', lang='en')
Copy link
Contributor Author

@kisoso kisoso Feb 11, 2026

Choose a reason for hiding this comment

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

Should this actually need to resolve to rdflib.Literal({"k": "v"}, lang='en') ?

Copy link
Member

Choose a reason for hiding this comment

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

Likely not - RDF literals don't have a dict type really.

Copy link
Member

@bz2 bz2 left a comment

Choose a reason for hiding this comment

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

Thanks!

(rdflib.Literal('mixed: "quotes"\nand newlines\tand tabs', lang='en'),
'"mixed: \\"quotes\\"\\nand newlines\\tand tabs"@en'),
(rdflib.Literal('\u043e', lang='bg'), '"\\u043e"@bg'),
(rdflib.Literal('л', lang='bg'), '"\\u043b"@bg'),
Copy link
Member

Choose a reason for hiding this comment

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

This test is just the same as the one above now, but for the source code spelling?

(rdflib.Literal('{"o": "v"}', lang='en'), '{"o": "v"}@en'),
]
triples = [('_s', '_p', obj) for obj, _ in test_cases]
expected = [exp for _, exp in test_cases]
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, will just revert.

@@ -422,3 +427,32 @@ def test_from_identifier_lang_obj(self):
self.resolver.from_identifier('{"k": "v"}@en'),
rdflib.Literal('{"k": "v"}', lang='en')
Copy link
Member

Choose a reason for hiding this comment

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

Likely not - RDF literals don't have a dict type really.

]

for text, lang in test_texts:
with self.subTest(text=text, lang=lang):
Copy link
Member

Choose a reason for hiding this comment

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

Aha, you're using subTest() here.

@kisoso kisoso merged commit d289cde into VisualMeaning:master Feb 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants