Created
May 3, 2012 15:54
-
-
Save s4y/2586751 to your computer and use it in GitHub Desktop.
Patch to make Lasso 8.x’s JSON tags handle escaped characters in strings properly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- /a/JSON.lasso | |
+++ /b/JSON.lasso | |
@@ -113,6 +116,7 @@ | |
Define_Tag: 'JSON', -Namespace='Encode_', -Required='value', -Optional='options'; | |
+ Local: 'escapes' = Map('\\' = '\\', '"' = '"', '\r' = 'r', '\n' = 'n', '\t' = 't', '\f' = 'f', '\b' = 'b'); | |
Local: 'output' = ''; | |
Local: 'newoptions' = (Array: -Internal); | |
If: !(Local_Defined: 'options') || (#options->(IsA: 'array') == False); | |
@@ -133,7 +137,16 @@ | |
Else: (#value->(IsA: 'literal')); | |
#output += #value; | |
Else: (#value->(IsA: 'string')); | |
- #output += '"' + ((String: #value)->(Replace: '\"', '\\"') & (Replace: '\r', '\\r') & (Replace: '\n', '\\n') & (Replace: '\t', '\\t') & (Replace: '\f', '\\f') & (Replace: '\b', '\\b') &) + '"'; | |
+ #output += '"'; | |
+ Loop: (#value->Length); | |
+ Local('character' = #value->(Get: Loop_Count)); | |
+ #output->(Append: | |
+ (Match_RegExp('[\\x{0020}-\\x{21}\\x{23}-\\x{5b}\\x{5d}-\\x{10fff}]') == #character) ? #character | | |
+ '\\' + (#escapes->(Contains: #character) ? #escapes->(Find: #character) | | |
+ 'u' + String(Encode_Hex(#character))->PadLeading(4, '0')&) | |
+ ); | |
+ /Loop; | |
+ #output += '"'; | |
Else: (#value->(IsA: 'integer')) || (#value->(IsA: 'decimal')) || (#value->(IsA: 'boolean')); | |
#output += (String: #value); | |
Else: (#value->(IsA: 'null')); | |
@@ -191,13 +204,26 @@ | |
(#value == '') ? Return: Null; | |
Define_Tag: 'consume_string', -Required='ibytes'; | |
- Local: 'obytes' = bytes; | |
- local: 'temp' = 0; | |
- While: ((#temp := #ibytes->(export8bits: #temp)) != 34); | |
- #obytes->(import8bits: #temp); | |
- (#temp == 92) ? #obytes->(import8bits: #ibytes->export8bits); // Escape \ | |
+ Local: 'unescapes' = (map: 34 = '"', 92 = '\\', 98 = '\b', 102 = '\f', 110 = '\n', 114 = '\r', 116 = '\t'); | |
+ Local: 'temp' = 0, 'obytes' = Bytes; | |
+ While: ((#temp := #ibytes->export8bits) != 34); // '"' | |
+ If(#temp === 92); // '\' | |
+ #temp = #ibytes->export8bits; | |
+ If(#temp === 117); // 'u' | |
+ #obytes->(ImportString: (Decode_Hex: (String: #ibytes->(GetRange: #ibytes->Position + 1, 4)))->(ExportString: 'UTF-16'), 'UTF-8'); | |
+ #ibytes->(SetPosition: #ibytes->Position + 4); | |
+ Else; | |
+ If(#unescapes->(Contains: #temp)); | |
+ #obytes->(ImportString: #unescapes->(Find: #temp), 'UTF-8'); | |
+ Else; | |
+ #obytes->(Import8Bits: #temp); | |
+ /If; | |
+ /If; | |
+ Else; | |
+ #obytes->(Import8Bits: #temp); | |
+ /If; | |
/While; | |
- Local: 'output' = ((String: #obytes)->(Replace: '\\"', '\"') & (Replace: '\\r', '\r') & (Replace: '\\n', '\n') & (Replace: '\\t', '\t') & (Replace: '\\f', '\f') & (Replace: '\\b', '\b') &); | |
+ Local('output' = #obytes->(ExportString: 'UTF-8')); | |
If: #output->(BeginsWith: '<LassoNativeType>') && #output->(EndsWith: '</LassoNativeType>'); | |
Local: 'temp' = #output - '<LassoNativeType>' - '</LassoNativeType>'; | |
Local: 'output' = null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment