config service: avoid raising a json exception on non json input

The config service accepts in strings instead of json strings, however
this causes the normal control flow to throw a json parsing exception.
Since this happens so frequently it is using a measurable amount of CPU
time, so avoid it in the common case by testing if the string is json
first
This commit is contained in:
Adam
2021-12-07 12:49:12 -05:00
parent a05655f412
commit 9cd7060035
2 changed files with 46 additions and 4 deletions

View File

@@ -26,6 +26,7 @@ package net.runelite.http.service.config;
import com.google.common.collect.ImmutableMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
@@ -53,4 +54,19 @@ public class ConfigServiceTest
assertTrue(ConfigService.validateJson("{\"key\": \"value\"}"));
assertTrue(ConfigService.validateJson("\n"));
}
@Test
public void testMaybeJson()
{
assertFalse(ConfigService.isMaybeJson("string"));
assertFalse(ConfigService.isMaybeJson("string with spaces"));
assertTrue(ConfigService.isMaybeJson("true"));
assertTrue(ConfigService.isMaybeJson("false"));
assertTrue(ConfigService.isMaybeJson("1"));
assertTrue(ConfigService.isMaybeJson("1.2"));
assertTrue(ConfigService.isMaybeJson("\"quote\""));
assertTrue(ConfigService.isMaybeJson("{\"key\": \"value\"}"));
assertTrue(ConfigService.isMaybeJson("[42]"));
}
}