Skip to content

Commit

Permalink
Forms API: parse out characters that break json (#12404)
Browse files Browse the repository at this point in the history
* parse out characters that break json

* fix comments
  • Loading branch information
gmrabian authored Apr 14, 2023
1 parent 46a0a22 commit aba8097
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion modules/forms_api/app/services/forms_api/pdf_filler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,22 @@ def mapped_data
b = binding
b.local_variable_set(:data, data)
result = ERB.new(template).result(b)
JSON.parse(result)
JSON.parse(escape_json_string(result))
end

def metadata
klass = "FormsApi::#{form_number.titleize.gsub(' ', '')}".constantize.new(data)
klass.metadata("tmp/#{form_number}-tmp.pdf")
end

def escape_json_string(str)
# remove characters that will break the json parser
# \u0000-\u001f: control characters in the ASCII table,
# characters such as null, tab, line feed, and carriage return
# \u0080-\u009f: control characters in the Latin-1 Supplement block of Unicode
# \u2000-\u201f: various punctuation and other non-printable characters in Unicode,
# including various types of spaces, dashes, and quotation marks.
str.gsub(/[\u0000-\u001f\u0080-\u009f\u2000-\u201f]/, ' ')
end
end
end
4 changes: 2 additions & 2 deletions modules/forms_api/spec/fixtures/form_json/vba_26_4555.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@
"postal_code": "54890"
}
},
"remarks": "Lengthy remarks"
}
"remarks": "Lengthy remarks here \nabout what is needed\tand such"
}

0 comments on commit aba8097

Please sign in to comment.