hpeter16
05/05/2025, 2:54 PMMatt Jones
05/05/2025, 3:20 PMhpeter16
05/05/2025, 5:08 PMbdw429s
05/05/2025, 5:34 PMzackster
05/05/2025, 6:10 PMzackster
05/06/2025, 8:21 AMtry
{
deserializeJson(json);
return true;
} catch (e){
return false;
}
the traditional approach of checking isJson then deserializing, usually end up parsing the json twice!!!dswitzer
05/06/2025, 11:32 AMisJSON() safety checks due to the change in behavior of deserializeJSON() not working with empty strings in Lucee 6, your comment on how isJSON() works made me go look at the code.
The code is at least shortcutting the logic if the string is empty to return false (which makes it fast in my use cases), but I wonder if there would be a significant speed difference on larger strings if there was at least a safety check that the first and last characters at least appear valid.
I wonder if updating the IsJSON.java 's call method with a check like the following would help performance on large strings:
String str = Caster.toString(obj, null);
String trimmed = str.trim();
char first = trimmed.charAt(0);
char last = trimmed.charAt(trimmed.length() - 1);
if(
StringUtil.isEmpty(trimmed)
||
// sanity check strings
(first == '"' && last != '"')
||
// sanity check objects
(first == '{' && last != '}')
||
// sanity check arrays
(first == '[' && last != ']')
||
// sanity check numbers
((Character.isDigit(first) || first == '-') && !Character.isDigit(last))
) return false;
// sanity check keywords
else if (
trimmed.equals("true") || trimmed.equals("false") || trimmed.equals("null")
) return true;
Just something to quickly sanitize before parsing.
I'll try to do some playing with this. I have one last day to play with Lucee 6 compatibility with our application, and I think I'll have some time to squeeze this in.zackster
05/06/2025, 11:37 AMzackster
05/06/2025, 11:48 AM