Skip to content

Commit

Permalink
Integration of the JSON-LD serializer with Jackson.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Jul 27, 2016
0 parents commit 7c00e07
Show file tree
Hide file tree
Showing 12 changed files with 657 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/target
**/*.iml
**/.idea
224 changes: 224 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?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>

<groupId>cz.cvut.kbss</groupId>
<artifactId>jaxb-jsonld-jackson</artifactId>
<version>0.0.1</version>
<name>JAXB JSON-LD Jackson</name>

<description>JAXB JSON-LD integration for Jackson.</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<junit.version>4.12</junit.version>
<org.mockito.version>1.10.19</org.mockito.version>
<ch.qos.logback.version>1.1.6</ch.qos.logback.version>
<com.fasterxml.jackson.version>2.8.1</com.fasterxml.jackson.version>
</properties>

<repositories>
<repository>
<id>kbss</id>
<name>KBSS Maven 2 Repository</name>
<url>http://kbss.felk.cvut.cz/m2repo</url>
<snapshots>
<checksumPolicy>warn</checksumPolicy>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>

<dependencies>
<!-- JAXB JSON-LD core -->
<dependency>
<groupId>cz.cvut.kbss</groupId>
<artifactId>jaxb-jsonld</artifactId>
<version>0.0.1</version>
</dependency>

<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${com.fasterxml.jackson.version}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${org.mockito.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${ch.qos.logback.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${ch.qos.logback.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.jsonld-java</groupId>
<artifactId>jsonld-java-sesame</artifactId>
<version>0.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<extensions>
<!-- Enabling the use of FTP -->
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.10</version>
</extension>
</extensions>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
</plugin>

<!-- Code coverage plugin -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- use mvn license:check to check for missing license headers, mvn license:format to add license headers -->
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>2.11</version>
<configuration>
<header>header.txt</header>
<excludes>
<exclude>**/src/test/resources/**</exclude>
<exclude>**/src/main/resources/**</exclude>
<exclude>license</exclude>
<exclude>**/pom.xml</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>deploy</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>deploy</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration> <!-- add this to disable checking -->
<additionalparam>-Xdoclint:none</additionalparam>
<aggregate>true</aggregate>
</configuration>
</plugin>
<plugin>
<!-- explicitly define maven-deploy-plugin after other to force exec order -->
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<!--<licenses>-->
<!--<license>-->
<!--<name>GNU Lesser General Public License</name>-->
<!--<url>http://www.gnu.org/copyleft/lesser.html</url>-->
<!--<distribution>repo</distribution>-->
<!--</license>-->
<!--</licenses>-->

<developers>
<developer>
<name>Martin Ledvinka</name>
<email>[email protected]</email>
<!--<url>http://cyber.felk.cvut.cz/people</url>-->
<organization>Czech Technical University in Prague, Knowledge Based and Software Systems Group
</organization>
<organizationUrl>http://kbss.felk.cvut.cz</organizationUrl>
<roles>
</roles>
</developer>
</developers>

<organization>
<name>Czech Technical University in Prague, Knowledge Based and Software Systems Group</name>
<url>http://kbss.felk.cvut.cz</url>
</organization>

<scm>
<connection>scm:git:https://kbss.felk.cvut.cz/git/jaxb-jsonld-jackson.git</connection>
</scm>

<distributionManagement>
<repository>
<id>kbss</id>
<name>KBSS Maven 2 Repository</name>
<url>sftp://kbss.felk.cvut.cz/var/www/m2repo</url>
</repository>
</distributionManagement>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cz.cvut.kbss.jsonld.jackson.serialization;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import cz.cvut.kbss.jsonld.serialization.JsonLdSerializer;

import java.io.IOException;

class JacksonJsonLdSerializer<T> extends JsonSerializer<T> {

@Override
public void serialize(T value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
final cz.cvut.kbss.jsonld.serialization.JsonGenerator writer = new JacksonJsonWriter(jsonGenerator);
final JsonLdSerializer serializer = JsonLdSerializer.createCompactedJsonLdSerializer(writer);
serializer.serialize(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cz.cvut.kbss.jsonld.jackson.serialization;

import com.fasterxml.jackson.core.JsonGenerator;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
* Outputs JSON using Jackson.
*/
class JacksonJsonWriter implements cz.cvut.kbss.jsonld.serialization.JsonGenerator {

private final JsonGenerator jsonGenerator;

JacksonJsonWriter(JsonGenerator jsonGenerator) {
this.jsonGenerator = jsonGenerator;
}

@Override
public void writeFieldName(String s) throws IOException {
jsonGenerator.writeFieldName(s);
}

@Override
public void writeObjectStart() throws IOException {
jsonGenerator.writeStartObject();
}

@Override
public void writeObjectEnd() throws IOException {
jsonGenerator.writeEndObject();
}

@Override
public void writeArrayStart() throws IOException {
jsonGenerator.writeStartArray();
}

@Override
public void writeArrayEnd() throws IOException {
jsonGenerator.writeEndArray();
}

@Override
public void writeNumber(Number number) throws IOException {
if (number instanceof Integer) {
jsonGenerator.writeNumber(number.intValue());
} else if (number instanceof Long) {
jsonGenerator.writeNumber(number.longValue());
} else if (number instanceof Float) {
jsonGenerator.writeNumber(number.floatValue());
} else if (number instanceof Double) {
jsonGenerator.writeNumber(number.doubleValue());
} else if (number instanceof BigInteger) {
jsonGenerator.writeNumber((BigInteger) number);
} else if (number instanceof BigDecimal) {
jsonGenerator.writeNumber((BigDecimal) number);
} else {
throw new IllegalArgumentException("Unable to write number " + number + " of type " + number.getClass());
}
}

@Override
public void writeBoolean(boolean b) throws IOException {
jsonGenerator.writeBoolean(b);
}

@Override
public void writeNull() throws IOException {
jsonGenerator.writeNull();
}

@Override
public void writeString(String s) throws IOException {
jsonGenerator.writeString(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cz.cvut.kbss.jsonld.jackson.serialization;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import cz.cvut.kbss.jsonld.serialization.BeanAnnotationProcessor;

/**
* Main point of integration of the JSON-LD serialization implementation into Jackson.
*/
public class JsonLdSerializerModifier extends BeanSerializerModifier {

@Override
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc,
JsonSerializer<?> serializer) {
if (BeanAnnotationProcessor.isOwlClassEntity(beanDesc.getBeanClass())) {
return new JacksonJsonLdSerializer<>();
}
return serializer;
}
}
Loading

0 comments on commit 7c00e07

Please sign in to comment.