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:
@@ -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]"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user