Skip to content

Commit

Permalink
Add some object/array getters and port more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
falkreon committed Feb 23, 2023
1 parent 3c6746d commit 1d937a1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public ValueElement asValueElement() {
return this;
}

public PrimitiveElement getPrimitive(int index) {
if (entries.get(index) instanceof PrimitiveElement prim) {
return prim;
} else {
return PrimitiveElement.ofNull();
}
}

//extends AbstractList<ValueElement> {

@Override
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/blue/endless/jankson/api/document/ObjectElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,38 @@ public PrimitiveElement getPrimitive(String key) {
return PrimitiveElement.ofNull();
}

/**
* Gets an array element if it exists in this object. If no element is mapped to the key, or if the value mapped to
* the key is not an array, an empty array will be returned.
* @param key the key whose corresponding value should be returned
* @return the value if it is present and an array, otherwise a synthetic empty array representing the missing element.
*/
public ArrayElement getArray(String key) {
for(KeyValuePairElement entry : entries) {
if (entry.getKey().equals(key) && entry.getValue() instanceof ArrayElement arr) {
return arr;
}
}

return new ArrayElement();
}

/**
* Gets an object element if it exists in this object. If no element is mapped to the key, or if the value mapped to
* the key is not an object, an empty object will be returned.
* @param key the key whose corresponding value should be returned
* @return the value if it is present and an object, otherwise a synthetic empty object representing the missing element.
*/
public ObjectElement getObject(String key) {
for(KeyValuePairElement entry : entries) {
if (entry.getKey().equals(key) && entry.getValue() instanceof ObjectElement obj) {
return obj;
}
}

return new ObjectElement();
}

//implements Map {
@Override
public int size() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ public void handleValue(LookaheadCodePointReader reader, BiConsumer<ElementType,
String value = StringValueParser.readStatic(reader);
elementConsumer.accept(ElementType.PRIMITIVE, value);
} else {
//TODO: Unquoted Strings etc.
throw new SyntaxError("Expected a value here, but couldn't decode it.", reader.getLine(), reader.getCharacter());
String maybeNull = reader.peekString(4);
if (maybeNull.equals("null")) {
reader.readString(4);
elementConsumer.accept(ElementType.PRIMITIVE, null);
} else {
//TODO: Unquoted Strings etc.
throw new SyntaxError("Expected a value here, but couldn't decode it.", reader.getLine(), reader.getCharacter());
}
}
}

Expand Down
41 changes: 21 additions & 20 deletions src/test/java/blue/endless/jankson/BasicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,38 +116,39 @@ public void testObjectContentCategories() throws IOException, SyntaxError {
Assertions.assertEquals(true, obj.getPrimitive("f").asBoolean().get());
Assertions.assertEquals(false, obj.getPrimitive("g").asBoolean().get());
Assertions.assertTrue(obj.getPrimitive("h").isNull());
} else {
Assertions.fail();
}
}

/* Unported 1.2.x tests */

@Test
public void testArrayContentCategories() {
public void testArrayContentCategories() throws IOException, SyntaxError {
String before = "{ 'a': ['hello', 42, 42.0, {}, [], true, false, null] }";

try {
JsonObject after = jankson.load(before);
Assertions.assertTrue(after.keySet().size()==1, "Object should contain one key");
ValueElement after = Jankson.readJson(before);

if (after instanceof ObjectElement obj) {
Assertions.assertEquals(1, obj.keySet().size());

JsonArray array = after.recursiveGet(JsonArray.class, "a");
Assertions.assertNotNull(array, "Recursive get of just 'a' should obtain an array.");
Assertions.assertEquals(8, array.size(), "Array should contain all declared elements and no more.");
ArrayElement array = obj.getArray("a");
Assertions.assertEquals(8, array.size());

Assertions.assertEquals(new JsonPrimitive("hello"), array.get(0), "Array should contain 'hello' at position 0");
Assertions.assertEquals(new JsonPrimitive(Long.valueOf(42)), array.get(1), "Array should contain 42 at position 1");
Assertions.assertEquals(new JsonPrimitive(Double.valueOf(42)), array.get(2),"Array should contain 42.0 at position 2");
Assertions.assertEquals(new JsonObject(), array.get(3), "Array should contain {} at position 3");
Assertions.assertEquals(new JsonArray(), array.get(4), "Array should contain [] at position 4");
Assertions.assertEquals(new JsonPrimitive(Boolean.TRUE), array.get(5), "Array should contain true at position 5");
Assertions.assertEquals(new JsonPrimitive(Boolean.FALSE), array.get(6), "Array should contain false at position 6");
Assertions.assertEquals(JsonNull.INSTANCE, array.get(7), "Array should contain null at position 7");
Assertions.assertEquals("hello", array.getPrimitive(0).asString().get());
Assertions.assertEquals(42, array.getPrimitive(1).asInt().getAsInt());
Assertions.assertEquals(42.0, array.getPrimitive(2).asDouble().getAsDouble());
Assertions.assertInstanceOf(ObjectElement.class, array.get(3));
Assertions.assertInstanceOf(ArrayElement.class, array.get(4));
Assertions.assertEquals(Boolean.TRUE, array.getPrimitive(5).asBoolean().get());
Assertions.assertEquals(Boolean.FALSE, array.getPrimitive(6).asBoolean().get());
Assertions.assertTrue(array.getPrimitive(7).isNull());

} catch (SyntaxError ex) {
Assertions.fail("Should not get a syntax error for a well-formed object: "+ex.getCompleteMessage());
} else {
Assertions.fail();
}
}

/* Unported 1.2.x tests */

@Test
public void testCommentAttribution() {
try {
Expand Down

0 comments on commit 1d937a1

Please sign in to comment.