added parser tests
This commit is contained in:
parent
6599c3a3e1
commit
2cc05e3b3a
4 changed files with 98 additions and 2 deletions
|
|
@ -53,7 +53,7 @@ public class JavaObjectReaderFactory {
|
||||||
private static CtMethod createReadJsonMethod(CtClass serializerClass, Class<?> type) {
|
private static CtMethod createReadJsonMethod(CtClass serializerClass, Class<?> type) {
|
||||||
try {
|
try {
|
||||||
String readMethodBodySource = createReadMethodBodySource(type);
|
String readMethodBodySource = createReadMethodBodySource(type);
|
||||||
System.out.println(readMethodBodySource);
|
// System.out.println(readMethodBodySource);
|
||||||
return CtNewMethod.make(Modifier.PUBLIC, OBJECT_CLASS, "read", PARSER_PARAM, NO_EXCEPTIONS, readMethodBodySource, serializerClass);
|
return CtNewMethod.make(Modifier.PUBLIC, OBJECT_CLASS, "read", PARSER_PARAM, NO_EXCEPTIONS, readMethodBodySource, serializerClass);
|
||||||
} catch (CannotCompileException e) {
|
} catch (CannotCompileException e) {
|
||||||
throw new ClassCreationException(e);
|
throw new ClassCreationException(e);
|
||||||
|
|
|
||||||
|
|
@ -105,6 +105,10 @@ public class Parser extends Lexer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<?> parseArray() {
|
public List<?> parseArray() {
|
||||||
|
return parseArray(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<?> parseArray(Class<?> genericType) {
|
||||||
skipWhitespace();
|
skipWhitespace();
|
||||||
if (current != '[') {
|
if (current != '[') {
|
||||||
throw new JsonParseException("no list found");
|
throw new JsonParseException("no list found");
|
||||||
|
|
@ -112,7 +116,7 @@ public class Parser extends Lexer {
|
||||||
final List<Object> list = new ArrayList<>();
|
final List<Object> list = new ArrayList<>();
|
||||||
advance();
|
advance();
|
||||||
while (current != -1 && current != ']') {
|
while (current != -1 && current != ']') {
|
||||||
final Maybe<Object> maybeValue = parseValue();
|
final Maybe<Object> maybeValue = genericType == null ? parseValue() : parseValue(genericType);
|
||||||
if (!maybeValue.isPresent()) {
|
if (!maybeValue.isPresent()) {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
87
src/test/java/nl/sander/jsontoy2/ParserTest.java
Normal file
87
src/test/java/nl/sander/jsontoy2/ParserTest.java
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
package nl.sander.jsontoy2;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
public class ParserTest {
|
||||||
|
@Test
|
||||||
|
public void testFloat() {
|
||||||
|
assertEquals(31.415927F, new Parser(getInputStream("31.415927")).parseFloat());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDouble() {
|
||||||
|
assertEquals(3.1415927D, new Parser(getInputStream("3.1415927")).parseDouble());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInt() {
|
||||||
|
assertEquals(31415927, new Parser(getInputStream("31415927")).parseInteger());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testShort() {
|
||||||
|
assertEquals((short) 31415, new Parser(getInputStream("31415")).parseShort());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testByte() {
|
||||||
|
assertEquals((byte) 31, new Parser(getInputStream("31")).parseByte());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBooleanTrue() {
|
||||||
|
assertEquals(true, new Parser(getInputStream("true")).parseBoolean());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBooleanFalse() {
|
||||||
|
assertEquals(false, new Parser(getInputStream("false")).parseBoolean());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBooleanInvalid() {
|
||||||
|
assertThrows(JsonParseException.class, () -> new Parser(getInputStream("falsy")).parseBoolean());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testArray() {
|
||||||
|
assertEquals(List.of("3", "1", "4", "1"), new Parser(getInputStream("[\"3\",\"1\",\"4\",\"1\"]")).parseArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testArrayWithType() {
|
||||||
|
assertEquals(List.of(3D, 1D, 4D, 1D), new Parser(getInputStream("[3,1,4,1]")).parseArray(Double.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testObject() {
|
||||||
|
assertEquals(Map.of("pi", 3.1415927D), new Parser(getInputStream("{\"pi\":3.1415927}")).parseObject());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testObjectWithType() {
|
||||||
|
assertEquals(Map.of("pi", 3.1415927F), new Parser(getInputStream("{\"pi\":3.1415927}")).parseObject(PiBean.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testChar() {
|
||||||
|
assertEquals('3', new Parser(getInputStream("\"3\"")).parseCharacter());
|
||||||
|
}
|
||||||
|
|
||||||
|
private InputStream getInputStream(String jsonString) {
|
||||||
|
return new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
static class PiBean {
|
||||||
|
private float pi;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,11 @@ public class FloatBean {
|
||||||
public FloatBean() {
|
public FloatBean() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FloatBean(float value, Float value2) {
|
||||||
|
this.value = value;
|
||||||
|
this.value2 = value2;
|
||||||
|
}
|
||||||
|
|
||||||
public float getValue() {
|
public float getValue() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue