Skip to content
Snippets Groups Projects
Commit d54c2ad3 authored by Eliott Sammier's avatar Eliott Sammier
Browse files

Fix JSON syntax errors

- Missing braces level
- Add escape function for JSON strings (beware of the backslashes)
parent 9f4e12af
No related branches found
No related tags found
No related merge requests found
Showing
with 85 additions and 41 deletions
{ {
"elements": [ "elements": [
{
}
] ]
} }
{ {
"elements": [ "elements": [
{
}
] ]
} }
{ {
"elements": [ "elements": [
"name": "pg313", {
"type": "radiogroup",
"choices": [ }
"<div class="STY_reponseQC" id="lienRep1"",
"<div class="STY_reponseQC" id="lienRep2"",
"<div class="STY_reponseQC" id="lienRep3"",
"<div class="STY_reponseQC" id="lienRep4""
],
"correctAnswer": ""
] ]
} }
{ {
"elements": [ "elements": [
"name": "pg400", {
"type": "radiogroup",
"choices": [ }
"<div class="STY_reponseQC" id="lienRep1"",
"<div class="STY_reponseQC" id="lienRep2"",
"<div class="STY_reponseQC" id="lienRep3"",
"<div class="STY_reponseQC" id="lienRep4""
],
"correctAnswer": ""
] ]
} }
{ {
"elements": [ "elements": [
{
}
] ]
} }
{ {
"elements": [ "elements": [
{
}
] ]
} }
{ {
"elements": [ "elements": [
{
}
] ]
} }
{ {
"elements": [ "elements": [
{
}
] ]
} }
{ {
"elements": [ "elements": [
{
}
] ]
} }
{ {
"elements": [ "elements": [
{
}
] ]
} }
{ {
"elements": [ "elements": [
{
}
] ]
} }
{ {
"elements": [ "elements": [
{
}
] ]
} }
{ {
"elements": [ "elements": [
"name": "pg966", {
"type": "radiogroup",
"choices": [ }
"<div class="STY_reponseQC" id="lienRep1"",
"<div class="STY_reponseQC" id="lienRep2"",
"<div class="STY_reponseQC" id="lienRep3"",
"<div class="STY_reponseQC" id="lienRep4""
],
"correctAnswer": ""
] ]
} }
...@@ -45,4 +45,5 @@ def apply_templates() -> str: ...@@ -45,4 +45,5 @@ def apply_templates() -> str:
# ==> Run STTL transformation # ==> Run STTL transformation
tr = Transformer.create(graph, MODULE_DIR + "/../templates/") tr = Transformer.create(graph, MODULE_DIR + "/../templates/")
return tr.transform() result = tr.transform()
return result
...@@ -27,3 +27,40 @@ function st:process(?x) { ...@@ -27,3 +27,40 @@ function st:process(?x) {
function mt:sep() { function mt:sep() {
st:format(",%s", st:nl()) st:format(",%s", st:nl())
} }
# Escape illegal characters for JSON strings
function mt:json_escape(?str) {
# Welcome to Backslash Hell! Buckle up mate, you're in for a ride...
# - The SPARQL syntax for string literals understands backslash escapes,
# which means '\n' is a newline character and '\\' is a literal backslash.
# - The replace() function takes a SPARQL/XPath regex as second argument,
# and the regex engine *also* understands backslash escapes, which means
# that if you want to replace an actual backslash, you need two in the
# regex, i.e. four in the string literal. For control characters like
# the newline, you need '\\n' (actual backslash + letter n)
# - The replacement string (3rd argument) is not a regex, but to replace the
# value of a capturing group, it uses the '$1' notation... which, you
# guessed it, can be escaped, therefore actual backslashes must be escaped
# just like in the regex.
# - We want to print a backslash as part of the replacement value (like
# '\n' which is valid JSON), so we have to escape it twice, i.e. 4 times
# in the string literal (or 8 times if we want to print two of them).
# - Finally, for some weird reason, '\b' (Backspace) is an exception and
# breaks the regex engine (deletes a character inside?) but is fine when
# unescaped.
replace(
replace(
replace(
replace(
replace(
replace(
replace(
?str,'\\\\', '\\\\\\\\'
), '\b', '\\\\b'
), '\\f', '\\\\f'
), '\\n', '\\\\n'
), '\\r', '\\\\r'
), '\\t', '\\\\t'
), '"', '\\\\"'
)
}
\ No newline at end of file
...@@ -13,12 +13,12 @@ template mt:qcu(?qcu) { ...@@ -13,12 +13,12 @@ template mt:qcu(?qcu) {
# while the rest of the template is only instantiated once. # while the rest of the template is only instantiated once.
# This is similar to a SQL 'GROUP BY every variable except these' # This is similar to a SQL 'GROUP BY every variable except these'
group { group {
'"' ?choice_html '"' '"' mt:json_escape(?choice_html) '"'
; separator=mt:sep() ; separator=mt:sep()
} }
} }
']' mt:sep() ']' mt:sep()
'"correctAnswer": "' ?correct_choice_html '"' '"correctAnswer": "' mt:json_escape(?correct_choice_html) '"'
} }
where { where {
?qcu a :ExerciceQC_QCU ?qcu a :ExerciceQC_QCU
......
...@@ -7,9 +7,13 @@ template mt:quiz(?quiz) { ...@@ -7,9 +7,13 @@ template mt:quiz(?quiz) {
'{' '{'
box { box {
'"elements": [' '"elements": ['
box {
"{"
box { box {
st:call-template(mt:qcu, ?quiz) st:call-template(mt:qcu, ?quiz)
} }
"}"
}
']' ']'
} }
'}' '}'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment