Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-sh committed Dec 13, 2018
0 parents commit 56d9c1b
Show file tree
Hide file tree
Showing 18 changed files with 1,785 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
/.settings/
/src/main/lombok/lombok.config
/src/test/lombok/lombok.config
/src/lombok.config
/target/
/.checkstyle
/.classpath
/.project
2 changes: 2 additions & 0 deletions .travis.yml
@@ -0,0 +1,2 @@
language: java
install: true
7 changes: 7 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,7 @@
Copyright (c) Lars Knickrehm

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57 changes: 57 additions & 0 deletions README.md
@@ -0,0 +1,57 @@
# JSON DOM
A DOM implementation for JSON. While DOM is widely used for XML structured data, it can be useful for JSON data, too. These classes wrap generic JSON elements to fit the DOM interfaces.

## Getting started
Here's a Maven dependency example:

<dependency>
<groupId>de.lars-sh</groupId>
<artifactId>json-dom</artifactId>
<version><!-- TODO --></version>
</dependency>

To learn more about the JSON DOM structure check out JavaDoc of the package `de.larssh.json.dom`.

## JSON Parsers
JSON DOM does not come with its own JSON parser or JSON element objects. Instead the interface `JsonDomValue` is made up to integrate existing parsers.

### Using GSON
JSON DOM comes with a GSON implementation called `GsonDomValue`. Take a look at the following example to get a DOM Document out of a GSON JSON element.

// The JsonElement is part of GSON
JsonElement jsonElement = ...;

// At first the GSON object need to be wrapped using its JSON DOM implementation: GsonDomValue
GsonDomValue gsonDomValue = new GsonDomValue(jsonElement);

// Finally you can either create a DOM Document out of it...
JsonDomDocument<JsonElement> jsonDomDocument = new JsonDomDocument(gsonDomValue);

// ...or even use the helper methods inside JsonDomXPathExpressions to evaluate XPathExpressions to JSON elements.
XPathExpression xPathExpression = ...;
JsonElement jsonElement = JsonDomXPathExpressions.getJsonElement(jsonDomDocument, xPathExpression);

Note: JSON DOM does not come with GSON dependencies itself. To use `GsonDomValue` please add GSON to your dependencies.

### Using Jackson
JSON DOM comes with a Jackson implementation called `JacksonDomValue`. Take a look at the following example coding to get a DOM Document out of a Jackson JSON node.

// The JsonNode is part of Jackson
JsonNode jsonNode = ...;

// At first the Jackson object need to be wrapped using its JSON DOM implementation: JacksonDomValue
JacksonDomValue jacksonDomValue = new JacksonDomValue(jsonNode);

// Finally you can either create a DOM Document out of it...
JsonDomDocument<JsonNode> jsonDomDocument = new JsonDomDocument(jacksonDomValue);

// ...or even use the helper methods inside JsonDomXPathExpressions to evaluate XPathExpressions to JSON elements.
XPathExpression xPathExpression = ...;
JsonNode jsonNode = JsonDomXPathExpressions.getJsonElement(jsonDomDocument, xPathExpression);

Note: JSON DOM does not come with Jackson dependencies itself. To use `JacksonDomValue` please add Jackson to your dependencies.

### Using any other JSON parser
The interface `JsonDomValue` is used to wrap elements as JSON DOM compatible value. Implement it for your concerns and feel free to push your code back to this repository.

Working with your custom JSON DOM value implementation works similar to the above GSON and Jackson implementations.
63 changes: 63 additions & 0 deletions pom.xml
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>json-dom</artifactId>
<version>0.9.0</version>

<name>JSON DOM</name>
<description>A DOM implementation for JSON. While DOM is widely used for XML structured data, it can be useful for JSON data, too. These classes wrap generic JSON elements to fit the DOM interfaces.</description>
<url>https://github.com/lars-sh/json-dom</url>

<parent>
<groupId>de.lars-sh</groupId>
<artifactId>parent</artifactId>
<version>0.9.1</version>
</parent>

<licenses>
<license>
<name>MIT License</name>
<url>https://opensource.org/licenses/MIT</url>
</license>
</licenses>

<developers>
<developer>
<name>Lars Knickrehm</name>
<email>mail@lars-sh.de</email>
<url>https://lars-sh.de/</url>
</developer>
</developers>

<scm>
<connection>scm:git:git@github.com:lars-sh/json-dom.git</connection>
<developerConnection>scm:git:git@github.com:lars-sh/json-dom.git</developerConnection>
<url>https://github.com/lars-sh/json-dom</url>
</scm>

<issueManagement>
<system>GitHub Issues</system>
<url>https://github.com/lars-sh/json-dom/issues</url>
</issueManagement>

<ciManagement>
<system>Travis</system>
<url>https://travis-ci.org/lars-sh/json-dom</url>
</ciManagement>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
99 changes: 99 additions & 0 deletions src/main/lombok/de/larssh/json/dom/GsonDomValue.java
@@ -0,0 +1,99 @@
package de.larssh.json.dom;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.w3c.dom.DOMException;

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Implementation of {@link JsonDomValue} for {@link com.google.gson.Gson} and
* its {@link JsonElement}.
*/
@Getter
@RequiredArgsConstructor
@EqualsAndHashCode(onParam_ = { @Nullable })
public class GsonDomValue implements JsonDomValue<JsonElement> {
/**
* Wrapped JSON element
*
* @return wrapped JSON element
*/
JsonElement jsonElement;

/** {@inheritDoc} */
@NonNull
@Override
public Map<String, GsonDomValue> getChildren() {
final Map<String, GsonDomValue> children = new LinkedHashMap<>();
final JsonElement element = getJsonElement();
if (element.isJsonArray()) {
int index = 0;
for (final JsonElement child : element.getAsJsonArray()) {
children.put(JsonDomElement.ARRAY_ITEM_NODE_NAME_PREFIX + Integer.toString(index),
new GsonDomValue(child));
index += 1;
}
} else if (element.isJsonObject()) {
for (final Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) {
children.put(entry.getKey(), new GsonDomValue(entry.getValue()));
}
}
return children;
}

/** {@inheritDoc} */
@NonNull
@Override
public String getTextValue() {
final JsonElement element = getJsonElement();
return element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()
? element.getAsString()
: element.toString();
}

/** {@inheritDoc} */
@NonNull
@Override
public JsonDomType getType() {
final JsonElement element = getJsonElement();
if (element.isJsonArray()) {
return JsonDomType.ARRAY;
}
if (element.isJsonNull()) {
return JsonDomType.NULL;
}
if (element.isJsonObject()) {
return JsonDomType.OBJECT;
}

final JsonPrimitive jsonPrimitive = element.getAsJsonPrimitive();
if (jsonPrimitive.isBoolean()) {
return JsonDomType.BOOLEAN;
}
if (jsonPrimitive.isNumber()) {
return JsonDomType.NUMBER;
}
if (jsonPrimitive.isString()) {
return JsonDomType.STRING;
}

throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Unknown JSON data type.");
}

/** {@inheritDoc} */
@NonNull
@Override
public String toString() {
return getJsonElement().toString();
}
}
92 changes: 92 additions & 0 deletions src/main/lombok/de/larssh/json/dom/JacksonDomValue.java
@@ -0,0 +1,92 @@
package de.larssh.json.dom;

import java.util.LinkedHashMap;
import java.util.Map;

import org.w3c.dom.DOMException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Implementation of {@link JsonDomValue} for {@link com.fasterxml.jackson} and
* its {@link JsonNode}.
*/
@Getter
@RequiredArgsConstructor
@EqualsAndHashCode(onParam_ = { @Nullable })
public class JacksonDomValue implements JsonDomValue<JsonNode> {
/**
* Wrapped JSON element
*
* @return wrapped JSON element
*/
JsonNode jsonElement;

/** {@inheritDoc} */
@NonNull
@Override
public Map<String, JacksonDomValue> getChildren() {
final Map<String, JacksonDomValue> children = new LinkedHashMap<>();
final JsonNode node = getJsonElement();
if (node.isArray()) {
int index = 0;
for (final JsonNode child : node) {
children.put(JsonDomElement.ARRAY_ITEM_NODE_NAME_PREFIX + Integer.toString(index),
new JacksonDomValue(child));
index += 1;
}
} else if (node.isObject()) {
node.fields()
.forEachRemaining(entry -> children.put(entry.getKey(), new JacksonDomValue(entry.getValue())));
}
return children;
}

/** {@inheritDoc} */
@NonNull
@Override
public String getTextValue() {
final JsonNode node = getJsonElement();
return node.isTextual() ? node.asText() : node.toString();
}

/** {@inheritDoc} */
@NonNull
@Override
public JsonDomType getType() {
final JsonNodeType nodeType = getJsonElement().getNodeType();
if (nodeType == JsonNodeType.ARRAY) {
return JsonDomType.ARRAY;
}
if (nodeType == JsonNodeType.BOOLEAN) {
return JsonDomType.BOOLEAN;
}
if (nodeType == JsonNodeType.NULL) {
return JsonDomType.NULL;
}
if (nodeType == JsonNodeType.NUMBER) {
return JsonDomType.NUMBER;
}
if (nodeType == JsonNodeType.OBJECT) {
return JsonDomType.OBJECT;
}
if (nodeType == JsonNodeType.STRING) {
return JsonDomType.STRING;
}
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Unknown JSON node type.");
}

/** {@inheritDoc} */
@NonNull
@Override
public String toString() {
return getJsonElement().toString();
}
}

0 comments on commit 56d9c1b

Please sign in to comment.