Skip to content

Commit

Permalink
serialization improvements: tests and interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Escher <eclipse@erikescher.de>
  • Loading branch information
erikescher committed Jan 17, 2020
1 parent 72de344 commit e5fdb67
Show file tree
Hide file tree
Showing 36 changed files with 743 additions and 0 deletions.
13 changes: 13 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

<!-- ### Compile dependencies versions -->
<minimal-json.version>0.9.5</minimal-json.version>
<jackson-core.version>2.10.1</jackson-core.version>
<typesafe-config.version>1.3.4</typesafe-config.version>
<ssl-config-core.version>0.3.8</ssl-config-core.version>
<akka.version>2.5.27</akka.version>
Expand Down Expand Up @@ -109,6 +110,18 @@
<version>${minimal-json.version}</version>
</dependency>

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

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-cbor</artifactId>
<version>${jackson-core.version}</version>
</dependency>

<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
Expand Down
8 changes: 8 additions & 0 deletions json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
<groupId>com.eclipsesource.minimal-json</groupId>
<artifactId>minimal-json</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-cbor</artifactId>
</dependency>

<!-- ### Provided ### -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ public String toString() {
return value.toString();
}

@Override
public void writeValue(final SerializationContext serializationContext) {
// TODO implement
// Does it make more sense to implement this here or should I implement this in the concrete classes?
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.ditto.json;

import java.io.OutputStream;
import java.nio.ByteBuffer;

public class ByteBufferOutputStream extends OutputStream {

private final ByteBuffer destinationBuffer;

ByteBufferOutputStream(ByteBuffer destinationBuffer){
this.destinationBuffer = destinationBuffer;
}

@Override
public void write(final int b) {
// super specifies to ignore everything except the lower 8 bits.
destinationBuffer.put((byte) b);
}
}
62 changes: 62 additions & 0 deletions json/src/main/java/org/eclipse/ditto/json/CborFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.ditto.json;

import java.io.Reader;
import java.nio.ByteBuffer;

//TODO: move these Methods to JsonFactory

public final class CborFactory {

/*
* This utility class is not meant to be instantiated.
*/
private CborFactory(){
throw new AssertionError();
}

public static JsonValue readFrom(final byte[] bytes){
// TODO implement
return null;
}

public static JsonValue readFrom(final byte[] bytes, int offset, int length){
// TODO implement
return null;
}

public static JsonValue readFrom(ByteBuffer byteBuffer){
// TODO implement
return null;
}

public static JsonValue readFrom(Reader reader){
// TODO implement
return null;
}

public static byte[] toByteArray(JsonValue jsonValue){
// TODO implement by calling jsonValue.toByteArray with an appropriate Generator
return null;
}

public static ByteBuffer toByteBuffer(JsonValue jsonValue){
// TODO implement by calling jsonValue.toByteBuffer with an appropriate Generator
return null;
}

// TODO: support the equivalent of Reader

}
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ public String toString() {
return valueList.asJsonArrayString();
}

@Override
public void writeValue(final SerializationContext serializationContext) {
// TODO implement
}

@Immutable
static final class SoftReferencedValueList {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,9 @@ public String toString() {
return JsonFactory.nullLiteral().toString();
}

@Override
public void writeValue(final SerializationContext serializationContext) {
// TODO implement test
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ public String toString() {
return String.valueOf(value);
}

@Override
public void writeValue(final SerializationContext serializationContext) {
// TODO implement
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ public String toString() {
return "null";
}

@Override
public void writeValue(final SerializationContext serializationContext) {
// TODO implement
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,11 @@ public String toString() {
return fieldMap.asJsonObjectString();
}

@Override
public void writeValue(final SerializationContext serializationContext) {
// TODO implement
}

@Immutable
static final class SoftReferencedFieldMap {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,9 @@ public String toString() {
return JsonFactory.nullLiteral().toString();
}

@Override
public void writeValue(final SerializationContext serializationContext) {
// TODO implement
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public String toString() {
return result;
}

@Override
public void writeValue(final SerializationContext serializationContext) {
// TODO implement
}

private String createStringRepresentation() {
final JavaStringToEscapedJsonString javaStringToEscapedJsonString = JavaStringToEscapedJsonString.getInstance();
return javaStringToEscapedJsonString.apply(value);
Expand Down
9 changes: 9 additions & 0 deletions json/src/main/java/org/eclipse/ditto/json/JsonValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,13 @@ default String formatAsString() {
return isString() ? asString() : toString();
}

/**
* Writes this JsonValue into the provided serialization context.
* This is intended to be used by serialization logic only.
* Pass this object to the relevant methods in {@link JsonFactory} instead if you want its serialized representation.
*
* @param serializationContext The context for serialization bundling configuration and state needed for serialization.
*/
void writeValue(SerializationContext serializationContext);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.ditto.json;

import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

/**
* A class that bundles State and Configuration for serialization.
* It must be recreated for each serialization target.
*/
public class SerializationContext implements Closeable, Flushable {

private JsonGenerator jacksonGenerator;

/**
* Creates a Serialization context that writes to the designated target.
* @param jacksonFactory The JsonFactory (from Jackson) to use during serialization. It defines among other things the Format to use.
* @param outputStream The stream to write serialized data to. The stream is considered to be borrowed and will not be closed.
*/
public SerializationContext(JsonFactory jacksonFactory, OutputStream outputStream) throws IOException {
jacksonGenerator = jacksonFactory.createGenerator(outputStream);
}

/**
* Creates a Serialization context that writes to the designated target.
* @param jacksonFactory The JsonFactory (from Jackson) to use during serialization. It defines among other things the Format to use.
* @param targetBuffer The Buffer to write serialized data to. The Buffer is considered to be borrowed and will not be closed.
*/
public SerializationContext(JsonFactory jacksonFactory, ByteBuffer targetBuffer) throws IOException {
this(jacksonFactory, new ByteBufferOutputStream(targetBuffer));
}

JsonGenerator getJacksonGenerator() {
return jacksonGenerator;
}

/**
* Closes internal objects that need to be closed. Targets will be closed according to the promise during creation.
*/
@Override
public void close() throws IOException {
jacksonGenerator.close();
}

/**
* Flushes everything including targets.
*/
@Override
public void flush() throws IOException {
jacksonGenerator.flush();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.ditto.json;

import static org.eclipse.ditto.json.assertions.DittoJsonAssertions.assertThat;

import java.io.IOException;
import java.nio.ByteBuffer;

import org.junit.Test;

public class ByteBufferOutputStreamIT {

@Test
public void writesThroughToBuffer() throws IOException {
final int expectedCount = 11;
final ByteBuffer buffer = ByteBuffer.allocate(expectedCount);
final ByteBufferOutputStream outputStream = new ByteBufferOutputStream(buffer);


for (int i = 0; i < 5; i++) {
outputStream.write(i);
}
outputStream.write(new byte[]{7,8,9});
outputStream.write(new byte[]{10,11,12,13,14}, 1, 3);

assertThat(buffer.position()).isEqualTo(expectedCount - 1);
}
}
Loading

0 comments on commit e5fdb67

Please sign in to comment.