-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from harrel56/feature/kotlinx-json-provider
Feature/kotlinx json provider
- Loading branch information
Showing
8 changed files
with
273 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/main/java/dev/harrel/jsonschema/providers/KotlinxJsonNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package dev.harrel.jsonschema.providers; | ||
|
||
import dev.harrel.jsonschema.JsonNode; | ||
import dev.harrel.jsonschema.JsonNodeFactory; | ||
import dev.harrel.jsonschema.SimpleType; | ||
import kotlinx.serialization.json.*; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.util.*; | ||
|
||
public final class KotlinxJsonNode extends AbstractJsonNode<JsonElement> { | ||
private BigDecimal asNumber; | ||
|
||
private KotlinxJsonNode(JsonElement node, String jsonPointer) { | ||
super(Objects.requireNonNull(node), jsonPointer); | ||
} | ||
|
||
private KotlinxJsonNode(JsonElement node) { | ||
this(node, ""); | ||
} | ||
|
||
@Override | ||
public boolean asBoolean() { | ||
return Boolean.parseBoolean(((JsonPrimitive) node).getContent()); | ||
} | ||
|
||
@Override | ||
public String asString() { | ||
return ((JsonPrimitive) node).getContent(); | ||
} | ||
|
||
@Override | ||
public BigInteger asInteger() { | ||
return asNumber.toBigInteger(); | ||
} | ||
|
||
@Override | ||
public BigDecimal asNumber() { | ||
return asNumber; | ||
} | ||
|
||
@Override | ||
List<JsonNode> createArray() { | ||
JsonArray array = (JsonArray) node; | ||
List<JsonNode> result = new ArrayList<>(array.size()); | ||
for (int i = 0; i < array.size(); i++) { | ||
result.add(new KotlinxJsonNode(array.get(i), jsonPointer + "/" + i)); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
Map<String, JsonNode> createObject() { | ||
JsonObject object = (JsonObject) node; | ||
Map<String, JsonNode> result = MapUtil.newHashMap(object.size()); | ||
for (Map.Entry<String, JsonElement> entry : object.getEntries()) { | ||
result.put(entry.getKey(), new KotlinxJsonNode(entry.getValue(), this.jsonPointer + "/" + JsonNode.encodeJsonPointer(entry.getKey()))); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
SimpleType computeNodeType(JsonElement node) { | ||
if (node instanceof JsonNull) { | ||
return SimpleType.NULL; | ||
} else if (node instanceof JsonObject) { | ||
return SimpleType.OBJECT; | ||
} else if (node instanceof JsonArray) { | ||
return SimpleType.ARRAY; | ||
} else if (node instanceof JsonPrimitive) { | ||
JsonPrimitive primitive = (JsonPrimitive) node; | ||
if (primitive.isString()) { | ||
return SimpleType.STRING; | ||
} | ||
String content = primitive.getContent(); | ||
if ("true".equals(content) || "false".equals(content)) { | ||
return SimpleType.BOOLEAN; | ||
} | ||
asNumber = new BigDecimal(content); | ||
if (canConvertToInteger(asNumber)) { | ||
return SimpleType.INTEGER; | ||
} else { | ||
return SimpleType.NUMBER; | ||
} | ||
} else { | ||
throw new IllegalArgumentException(String.format("Unknown node class [%s]", node.getClass())); | ||
} | ||
} | ||
|
||
public static final class Factory implements JsonNodeFactory { | ||
private final Json json; | ||
|
||
public Factory() { | ||
this(Json.Default); | ||
} | ||
|
||
public Factory(Json json) { | ||
this.json = json; | ||
} | ||
|
||
@Override | ||
public KotlinxJsonNode wrap(Object node) { | ||
if (node instanceof KotlinxJsonNode) { | ||
return new KotlinxJsonNode(((KotlinxJsonNode) node).node); | ||
} else if (node instanceof JsonElement) { | ||
return new KotlinxJsonNode((JsonElement) node); | ||
} else { | ||
throw new IllegalArgumentException("Cannot wrap object which is not an instance of kotlinx.serialization.json.JsonElement"); | ||
} | ||
} | ||
|
||
@Override | ||
public KotlinxJsonNode create(String rawJson) { | ||
return new KotlinxJsonNode(json.parseToJsonElement(rawJson)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/test/java/dev/harrel/jsonschema/providers/KotlinxJsonTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package dev.harrel.jsonschema.providers; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import dev.harrel.jsonschema.JsonNode; | ||
import dev.harrel.jsonschema.JsonNodeFactory; | ||
import dev.harrel.jsonschema.SimpleType; | ||
import kotlinx.serialization.json.Json; | ||
import kotlinx.serialization.json.JsonElement; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class KotlinxJsonTest { | ||
private JsonNodeFactory createFactory() { | ||
return new KotlinxJsonNode.Factory(); | ||
} | ||
|
||
@Test | ||
void shouldWrapForValidArgument() { | ||
JsonElement object = Json.Default.parseToJsonElement("{}"); | ||
JsonNode wrap = createFactory().wrap(object); | ||
assertThat(wrap).isNotNull(); | ||
assertThat(wrap.getNodeType()).isEqualTo(SimpleType.OBJECT); | ||
} | ||
|
||
@Test | ||
void shouldFailWrapForInvalidArgument() throws JsonProcessingException { | ||
com.fasterxml.jackson.databind.JsonNode object = new ObjectMapper().readTree("{}"); | ||
JsonNodeFactory factory = createFactory(); | ||
assertThatThrownBy(() -> factory.wrap(object)) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Nested | ||
class SpecificationSuiteTest extends dev.harrel.jsonschema.SpecificationSuiteTest { | ||
@Override | ||
public JsonNodeFactory getJsonNodeFactory() { | ||
return createFactory(); | ||
} | ||
} | ||
|
||
@Nested | ||
class SupplementarySuiteTest extends dev.harrel.jsonschema.SupplementarySuiteTest { | ||
@Override | ||
public JsonNodeFactory getJsonNodeFactory() { | ||
return createFactory(); | ||
} | ||
} | ||
|
||
@Nested | ||
class Draft2020EvaluatorFactoryTest extends dev.harrel.jsonschema.Draft2020EvaluatorFactoryTest { | ||
@Override | ||
public JsonNodeFactory getJsonNodeFactory() { | ||
return createFactory(); | ||
} | ||
} | ||
|
||
@Nested | ||
class Draft2019EvaluatorFactoryTest extends dev.harrel.jsonschema.Draft2019EvaluatorFactoryTest { | ||
@Override | ||
public JsonNodeFactory getJsonNodeFactory() { | ||
return createFactory(); | ||
} | ||
} | ||
|
||
@Nested | ||
class JsonNodeFactoryTest extends dev.harrel.jsonschema.JsonNodeFactoryTest { | ||
@Override | ||
public JsonNodeFactory getJsonNodeFactory() { | ||
return createFactory(); | ||
} | ||
} | ||
|
||
@Nested | ||
class JsonNodeTest extends dev.harrel.jsonschema.JsonNodeTest { | ||
@Override | ||
public JsonNodeFactory getJsonNodeFactory() { | ||
return createFactory(); | ||
} | ||
} | ||
|
||
@Nested | ||
class MetaSchemaTest extends dev.harrel.jsonschema.MetaSchemaTest { | ||
@Override | ||
public JsonNodeFactory getJsonNodeFactory() { | ||
return createFactory(); | ||
} | ||
} | ||
|
||
@Nested | ||
class VocabulariesTest extends dev.harrel.jsonschema.VocabulariesTest { | ||
@Override | ||
public JsonNodeFactory getJsonNodeFactory() { | ||
return createFactory(); | ||
} | ||
} | ||
|
||
@Nested | ||
class JsonPointerEscapingTest extends dev.harrel.jsonschema.JsonPointerEscapingTest { | ||
@Override | ||
public JsonNodeFactory getJsonNodeFactory() { | ||
return createFactory(); | ||
} | ||
} | ||
|
||
@Nested | ||
class EvaluationPathTest extends dev.harrel.jsonschema.EvaluationPathTest { | ||
@Override | ||
public JsonNodeFactory getJsonNodeFactory() { | ||
return createFactory(); | ||
} | ||
} | ||
|
||
@Nested | ||
class DisabledSchemaValidationTest extends dev.harrel.jsonschema.DisabledSchemaValidationTest { | ||
@Override | ||
public JsonNodeFactory getJsonNodeFactory() { | ||
return createFactory(); | ||
} | ||
} | ||
} |