Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Patch to make Lasso 8.x’s JSON tags handle escaped characters in strings properly

View json.lasso.patch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
--- /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
Something went wrong with that request. Please try again.